-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.js
38 lines (34 loc) · 761 Bytes
/
ga.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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Neuro-Evolution Flappy Bird
function nextGeneration() {
console.log('next generation');
calculateFitness();
for (let i = 0; i < TOTAL; i++) {
activeEle[i] = pickOne();
}
eliminatedEle = [];
}
function pickOne() {
let index = 0;
let r = random(1);
while (r > 0) {
r = r - eliminatedEle[index].fitness;
index++;
}
index--;
let bird = eliminatedEle[index];
let child = new PongElement(floor(random(100,255)), bird.brain);
child.mutate();
return child;
}
function calculateFitness() {
let sum = 0;
for (let bird of eliminatedEle) {
sum += bird.score;
}
for (let bird of eliminatedEle) {
bird.fitness = bird.score / sum;
}
}