/
manage-round.js
85 lines (76 loc) · 2.05 KB
/
manage-round.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var create = require("../create-instance");
module.exports = {
generationZero: generationZero,
nextGeneration: nextGeneration
}
function generationZero(config){
var generationSize = config.generationSize,
schema = config.schema;
var cw_carGeneration = [];
for (var k = 0; k < generationSize; k++) {
var def = create.createGenerationZero(schema, function(){
return Math.random()
});
def.index = k;
cw_carGeneration.push(def);
}
return {
counter: 0,
generation: cw_carGeneration,
};
}
function nextGeneration(
previousState,
scores,
config
){
var champion_length = config.championLength,
generationSize = config.generationSize,
selectFromAllParents = config.selectFromAllParents;
var newGeneration = new Array();
var newborn;
for (var k = 0; k < champion_length; k++) {``
scores[k].def.is_elite = true;
scores[k].def.index = k;
newGeneration.push(scores[k].def);
}
var parentList = [];
for (k = champion_length; k < generationSize; k++) {
var parent1 = selectFromAllParents(scores, parentList);
var parent2 = parent1;
while (parent2 == parent1) {
parent2 = selectFromAllParents(scores, parentList, parent1);
}
var pair = [parent1, parent2]
parentList.push(pair);
newborn = makeChild(config,
pair.map(function(parent) { return scores[parent].def; })
);
newborn = mutate(config, newborn);
newborn.is_elite = false;
newborn.index = k;
newGeneration.push(newborn);
}
return {
counter: previousState.counter + 1,
generation: newGeneration,
};
}
function makeChild(config, parents){
var schema = config.schema,
pickParent = config.pickParent;
return create.createCrossBreed(schema, parents, pickParent)
}
function mutate(config, parent){
var schema = config.schema,
mutation_range = config.mutation_range,
gen_mutation = config.gen_mutation,
generateRandom = config.generateRandom;
return create.createMutatedClone(
schema,
generateRandom,
parent,
Math.max(mutation_range),
gen_mutation
)
}