private void doMutation(MLP m, out MLP mOut) { double probability = r.NextDouble(); if (probability <= mutationRate) { if (evolveWeights) { m.mutateWeights(); } if (evolveFunction) { m.mutateFunction(); } } mOut = m; }
private void doCrossover(MLP m1, MLP m2, out MLP m1Out, out MLP m2Out) { double probability = r.NextDouble(); if (probability <= crossoverRate) { if (evolveWeights) { int nodeIndex = r.Next(0, 5); //test swap Node n = m1.nodes[nodeIndex]; m1.nodes[nodeIndex] = m2.nodes[nodeIndex]; m2.nodes[nodeIndex] = n; double weight = m1.getWeight(nodeIndex); m1.setWeight(nodeIndex, m2.getWeight(nodeIndex)); m1.setWeight(nodeIndex, weight); int nodeIndex2 = r.Next(0, 5); while (nodeIndex2 == nodeIndex) { nodeIndex2 = r.Next(0, 5); } nodeIndex = nodeIndex2; //test swap n = m1.nodes[nodeIndex]; m1.nodes[nodeIndex] = m2.nodes[nodeIndex]; m2.nodes[nodeIndex] = n; weight = m1.getWeight(nodeIndex); m1.setWeight(nodeIndex, m2.getWeight(nodeIndex)); m1.setWeight(nodeIndex, weight); } if (evolveFunction) { MLP.Activation a = m1.activation; m1.activation = m2.activation; m2.activation = a; } } m1Out = m1; m2Out = m2; }