MCPcopy Create free account
hub / github.com/FidoProject/Fido / crossover

Method crossover

src/GeneticAlgo.cpp:19–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17}
18
19void GeneticAlgo::crossover(net::NeuralNet mom, net::NeuralNet dad, net::NeuralNet *offspring1, net::NeuralNet *offspring2) {
20 float crossoverDeterminer = (float)rand() / (float)RAND_MAX;
21 if(crossoverDeterminer > crossoverRate) {
22 *offspring1 = mom;
23 *offspring2 = dad;
24 return;
25 }
26
27 std::vector<double> offspring1Weights;
28 std::vector<double> offspring2Weights;
29 std::vector<double> momWeights = mom.getWeights();
30 std::vector<double> dadWeights = dad.getWeights();
31
32 /// Crossover index must be a minimum of 1 and a maxiumum of the second to last index of the weights
33 int crossoverIndex = (rand() % (momWeights.size() - 2)) + 1;
34
35 for(int a = 0; a < crossoverIndex; a++) {
36 offspring1Weights.push_back(momWeights[a]);
37 offspring2Weights.push_back(dadWeights[a]);
38 }
39 for(unsigned int a = crossoverIndex; a < momWeights.size(); a++) {
40 offspring1Weights.push_back(dadWeights[a]);
41 offspring2Weights.push_back(momWeights[a]);
42 }
43
44 *offspring1 = net::NeuralNet(mom);
45 offspring1->setWeights(offspring1Weights);
46 *offspring2 = net::NeuralNet(dad);
47 offspring2->setWeights(offspring2Weights);
48
49 std::vector<double>().swap(offspring1Weights);
50 std::vector<double>().swap(offspring2Weights);
51 std::vector<double>().swap(momWeights);
52 std::vector<double>().swap(dadWeights);
53}
54
55void GeneticAlgo::mutate(net::NeuralNet *net) {
56 std::vector<double> weights = net->getWeights();

Callers

nothing calls this directly

Calls 5

getWeightsMethod · 0.80
setWeightsMethod · 0.80
NeuralNetClass · 0.50
sizeMethod · 0.45
swapMethod · 0.45

Tested by

no test coverage detected