-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneticEvolution.js
49 lines (46 loc) · 1.32 KB
/
geneticEvolution.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
function nextGeneration(){
// calc fitness
calculateFitness();
for(let i=0; i<totalPopulation; i++){
helicopters[i] = pickRandom();
}
// start again (copied from setup function)
diedHeli = [];
bestDistance = 0;
obstacles = [];
obstacles.push(new Obstacle(width, random(100, height - 200), obsWidth, obsHeight));
obstacles.push(new Obstacle(width + (width/2), random(100, height - 200), obsWidth, obsHeight));
upperWalls = [];
lowerWalls = [];
lastHeight = 10;
for(let x=0; x<=width+2*wallWidth; x+=wallsObs){
upperWalls.push(new Obstacle(x, 0, wallsObs, wallWidth));
lowerWalls.push(new Obstacle(x, height - wallWidth, wallsObs, wallWidth));
}
}
function pickRandom(){
let index = 0;
let r = random(1);
while(r > 0){
r = r - diedHeli[index].fitness;
index++;
}
index--;
let heli = diedHeli[index];
let child = new Helicopter(heli.brain);
child.mutate();
return child;
}
function calculateFitness(){
let totalScore = 0;
let bestFitness = 0;
for(let i=0; i<diedHeli.length; i++){
totalScore += diedHeli[i].distance;
}
for(let i=0; i<diedHeli.length; i++){
diedHeli[i].fitness = diedHeli[i].distance / totalScore;
if(diedHeli[i].distance > bestFitness)
bestFitness = diedHeli[i].distance;
}
if(bestFitness > overallBest) overallBest = bestFitness;
}