-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopulation.js
More file actions
51 lines (47 loc) · 1.17 KB
/
Copy pathpopulation.js
File metadata and controls
51 lines (47 loc) · 1.17 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
function Population(){
this.rockets = [];
this.populationSize = 25;
this.matingPool = [];
for(var i=0; i<this.populationSize; i++){
this.rockets[i] = new Rocket();
}
this.evaluate = function(){
var maxfit = 0;
for(var i=0; i<this.populationSize; i++){
this.rockets[i].calculateFitness();
if(this.rockets[i].fitness > maxfit){
maxfit = this.rockets[i].fitness;
}
}
for(var i=0; i<this.populationSize; i++){
if(this.rockets[i].completed == true){
success += 1;
}
this.rockets[i].fitness /= maxfit;
}
this.matingPool = [];
for(var i=0; i<this.populationSize; i++){
var n = this.rockets[i].fitness * 100;
for(var j=0; j<n; j++){
this.matingPool.push(this.rockets[i]);
}
}
}
this.selection = function(){
var newRockets = [];
for(var i=0; i<this.rockets.length; i++){
var parentA = random(this.matingPool).dna;
var parentB = random(this.matingPool).dna;
var child = parentA.crossOver(parentB);
child.mutation();
newRockets[i] = new Rocket(child);
}
this.rockets = newRockets;
}
this.run = function(){
for(var i=0; i<this.populationSize; i++){
this.rockets[i].update();
this.rockets[i].show();
}
}
}