-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulation.js
More file actions
60 lines (49 loc) · 1.39 KB
/
Copy pathpopulation.js
File metadata and controls
60 lines (49 loc) · 1.39 KB
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
class Population {
constructor() {
this.rockets = [];
this.mattingPool = [];
this.popsize = 100;
for(let i=0;i<this.popsize;i++) {
this.rockets.push(new Rocket());
}
}
evaluate() {
let maxfit = 0;
for(let i=0; i<this.popsize;i++) {
this.rockets[i].calcFitness();
maxfit = max(maxfit, this.rockets[i].fitness);
}
for(let i=0;i<this.popsize;i++) {
this.rockets[i].fitness /= maxfit;
}
////////// SELECTION //////////
this.mattingPool = [];
for(let i=0;i<this.popsize;i++) {
let n = this.rockets[i].fitness * 100;
for(let j=0;j<n;j++) {
this.mattingPool.push(this.rockets[i]);
}
}
}
selection() {
let newRockets = []
for(let i=0;i<this.popsize;i++) {
let parentA = random(this.mattingPool).dna;
let parentB = random(this.mattingPool).dna;
let childDNA = parentA.crossover(parentB);
childDNA.mutation();
newRockets[i] = new Rocket(childDNA);
}
this.rockets = newRockets;
}
run() {
for(let i=0;i<this.popsize;i++) {
this.rockets[i].update();
}
}
show() {
for(let i=0;i<this.popsize;i++) {
this.rockets[i].show();
}
}
}