-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation.js
157 lines (124 loc) · 3.49 KB
/
Population.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const FOOD_EAT_SCORE = 1
const DIE_SCORE = -1
const H_NODES = 16
const LAYERS = 2
const MUTATION_RATE = 0.5
class Population {
constructor(pop_size) {
this.pop = []
this.foods = []
this.highscore = 0
this.evolution = createVector(0,0);
this.gen = 0
for (let i = 0;i < pop_size;i++) {
this.pop[i] = new Snake()
}
this.bestSnake = {}
this.bestSnakeFitness = 0;
this.bestSnakeScore = 0
this.fitnessSum = 0
}
handleFrame() {
if (this.allDead()) {
this.calculateFitness();
this.naturalSelection();
this.highscore = this.bestSnake.score;
} else {
this.moveSnakes()
}
}
calculateFitness() {
for (let i =0;i < this.pop.length;i++) {
this.pop[i].fitness = 1.0/(this.pop[i].score*this.pop[i].score)
}
}
calculateFitnessSum() {
let sum = 0
for (let i =0;i < this.pop.length;i++) {
this.sum += this.pop[i].fitness
}
return sum
}
setBestSnake() {
let max = 0;
let maxIndex = 0;
for(let i = 0; i < this.pop.length; i++) {
if(this.pop[i].fitness > max) {
max = this.pop[i].fitness;
maxIndex = i;
}
}
if(max > this.bestFitness) {
this.bestSnakeFitness = max;
this.bestSnake = this.pop[maxIndex].cloneForReplay();
this.bestSnakeScore = this.pop[maxIndex].score;
} else {
if (this.bestSnake) {
this.bestSnake = this.bestSnake
} else {
this.bestSnake = this.pop[0]
}
}
}
selectParent() {
let rand = random(this.fitnessSum);
let summation = 0;
for(let i = 0; i < this.pop.length; i++) {
summation += this.pop[i].fitness;
if(summation > rand) {
return this.pop[i];
}
}
return this.pop[0];
}
naturalSelection() {
let newSnakes = []
this.setBestSnake();
this.fitnessSum = this.calculateFitnessSum();
if (!this.bestSnake.x) {
this.bestSnake = this.pop[0]
}
newSnakes[0] = this.bestSnake.clone(); //add the best snake of the prior generation into the new generation
for(let i = 1; i < this.pop.length; i++) {
let child = this.selectParent().crossover(this.selectParent());
child.mutate();
newSnakes[i] = child;
}
this.pop = []
for ( let i =0; i <newSnakes.length-1;i++) {
this.pop[i] = newSnakes[i].clone();
}
this.evolution.add(this.bestSnakeScore);
this.gen+=1;
}
allDead() {
for (let i =0;i < this.pop.length;i++) {
if (!this.pop[i].dead) {
return false;
}
}
return true;
}
moveSnakes() {
for (let i =0;i < this.pop.length;i++) {
let s = this.pop[i]
s.update()
s.show()
if (s.eat()) {
s.score += FOOD_EAT_SCORE
}
if (s.death()) {
s.score -= DIE_SCORE
}
let state = s.getState()
let action = s.takeAction(state)
}
}
showFood() {
for (let i =0;i < this.foods.length;i++) {
this.foods[i].show()
}
}
reset() {
}
}