MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / reproduce

Function reproduce

examples/machine_learning/geneticalgorithm.cpp:34–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32}
33
34void reproduce(array& searchSpace, array& sampleX, array& sampleY,
35 array& sampleZ, const int nSamples, const int n) {
36 // Get fittest parents
37 array selection = selectFittest(sampleZ, nSamples);
38 array parentsX = sampleX(selection);
39 array parentsY = sampleY(selection);
40 int bits = (int)log2(n);
41
42 // Divide selection in two
43 array parentsX1 = parentsX.rows(0, parentsX.elements() / 2 - 1);
44 array parentsX2 =
45 parentsX.rows(parentsX.elements() / 2, parentsX.elements() - 1);
46 array parentsY1 = parentsY.rows(0, parentsY.elements() / 2 - 1);
47 array parentsY2 =
48 parentsY.rows(parentsY.elements() / 2, parentsY.elements() - 1);
49
50 // Get crossover points (at which bit to crossover) and construct bit masks
51 // from them
52 array crossover = randu(nSamples / 4, u32) % bits;
53 array lowermask = (1 << crossover) - 1;
54 array uppermask = INT_MAX - lowermask;
55
56 // Create children as the cross between two parents
57 array childrenX1 = (parentsX1 & uppermask) + (parentsX2 & lowermask);
58 array childrenY1 = (parentsY1 & uppermask) + (parentsY2 & lowermask);
59
60 array childrenX2 = (parentsX2 & uppermask) + (parentsX1 & lowermask);
61 array childrenY2 = (parentsY2 & uppermask) + (parentsY1 & lowermask);
62
63 // Join two new sets
64 sampleX = join(0, childrenX1, childrenX2);
65 sampleY = join(0, childrenY1, childrenY2);
66
67 // Create mutant children
68 array mutantX = sampleX;
69 array mutantY = sampleY;
70
71 // Flip a random bit to vary the gene pool
72 mutantX = mutantX ^ (1 << (randu(nSamples / 2, u32) % bits));
73 mutantY = mutantY ^ (1 << (randu(nSamples / 2, u32) % bits));
74
75 sampleX = join(0, sampleX, mutantX);
76 sampleY = join(0, sampleY, mutantY);
77
78 // Update the value of each sample with the new coordinates
79 sampleZ = update(searchSpace, sampleX, sampleY, n);
80}
81
82void initSamples(array& searchSpace, array& sampleX, array& sampleY,
83 array& sampleZ, const int nSamples, const int n) {

Callers 1

reproducePrintFunction · 0.85

Calls 7

selectFittestFunction · 0.85
log2Function · 0.85
randuFunction · 0.85
rowsMethod · 0.80
updateFunction · 0.70
joinFunction · 0.50
elementsMethod · 0.45

Tested by

no test coverage detected