-
Notifications
You must be signed in to change notification settings - Fork 1
/
Neat.js
executable file
·321 lines (262 loc) · 9.48 KB
/
Neat.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
class Neat{
// return a non-redundant connection
static add_link(from, to){
let new_connection = new ConnectionGene(from, to);
let returned_connection = Neat.check_connection(new_connection, Neat.connections);
if(returned_connection != null){
return returned_connection;
} else {
// create a new connection also add to Neat.connections
new_connection.innovation_number = Neat.connections.length + 1;
Neat.connections.push(new_connection);
new_connection.weight = (Math.random() * 2 - 1);
new_connection.enabled = true;
return new_connection;
}
}
// method to check whether a connection exists in a list of connections
// if exists -> return copy of that connection
// else -> return null
static check_connection(con, connection_list){
for(let con1 of connection_list){
if(con.from.innovation_number == con1.from.innovation_number && con.from.x == con1.from.x && con.to.innovation_number == con1.to.innovation_number && con.to.x == con1.to.x){
// return a copy of connection from Neat.connections
con.innovation_number = con1.innovation_number;
con.weight = Math.random() * 2 - 1;
con.enabled = true;
return con;
}
}
return null;
}
static insert_link_sorted(link, connections_list){
//insert new connection in ascending order of its innovation number
for(let i = connections_list.length -1 ; i >= 0; i--){
if(link.innovation_number > connections_list[i].innovation_number){
connections_list.splice(i+1, 0, link);
// add the new connection to connections list of "to" node which will be used to calculate its data
link.to.connections.push(link);
break;
}
}
}
static choose_one(array, sum = 1){
let number = Math.random() * sum;
for(let i = 0; i < array.length; i++){
number -= array[i];
if(number <= 0){
return i;
}
}
}
static sigmoid(x){
return 1/(1 + Math.exp(-4.9 * x));
}
static evolve(){
noOfGenerations++;
Neat.speciation();
Neat.sortSpecies();
Neat.kill_the_weaklings();
Neat.remove_stale_species();
Neat.remove_bad_species();
Neat.createNewGeneration();
// console.log(Neat.genomes.length);
}
// reset kill count, species score, genome's score
// clear all the genomes leaving only one in the species
static reset(){
for(let sample of Neat.species){
for(let dino of sample.dinos){
dino.species = undefined;
}
let representative = sample.dinos[Math.floor(Math.random() * sample.dinos.length)];
sample.dinos = [representative];
representative.species = sample;
}
}
static speciation(){
Neat.reset();
let found = false;
for(let dino of Neat.dinos){
if(dino.species != undefined){
continue;
}
found = false;
for(let sample of Neat.species){
if(dino.distance(sample.representative) < Neat.threshold){
dino.species = sample;
sample.dinos.push(dino);
found = true;
break;
}
}
if(!found){
Neat.species.push(new Species(dino));
}
}
}
static sortSpecies(){
for(let sample of Neat.species){
// sort all the genomes of a species in descending order
sample.dinos.sort((a, b) => b.fitness - a.fitness);
if(sample.dinos[0].fitness > sample.bestScore){
sample.staleness = 0;
sample.bestScore = sample.dinos[0].score;
} else {
sample.staleness++;
}
}
}
static kill_the_weaklings(){
for(let sample of Neat.species){
let kill_amount = Math.floor(Neat.kill_rate * sample.dinos.length);
while(kill_amount--){
sample.dinos[sample.dinos.length - 1].species = undefined;
sample.dinos.pop();
// Neat.kill_count++;
}
}
Neat.explicit_fitness_sharing();
}
static remove_bad_species(){
for(let i = 0; i < Neat.species.length; i++){
let total_sum = Neat.calculateSpeciesFitness();
if((Neat.species[i].score/total_sum) * Neat.dinos.length < 1){
Neat.species[i].dinos = [];
// remove the element at ith index
Neat.species.splice(i, 1);
}
}
}
static remove_stale_species(){
Neat.calculateSpeciesFitness();
Neat.species.sort((a, b) => b.score - a.score);
for(let i = 2; i < Neat.species.length; i++){
if(Neat.species[i].staleness >= 15){
Neat.species[i].dinos = [];
// remove the element at ith index
Neat.species.splice(i, 1)
}
}
}
static createNewGeneration(){
let nextGen = [];
// console.log('+++++++++++++++++++++')
let total_sum = Neat.calculateSpeciesFitness();
for(let i = 0; i < Neat.species.length; i++){
nextGen.push(Neat.species[i].dinos[0].clone());
// console.log('1st push ');
let noOfChildren = 0;
if(total_sum != 0){
noOfChildren = Math.floor((Neat.species[i].score/total_sum) * Neat.dinos.length) - 1;
}
for(let j = 0; j < noOfChildren; j++){
nextGen.push(Neat.species[i].giveMeBaby());
// console.log('2nd push ');
}
}
while(nextGen.length < population){
// console.log(nextGen.length)
nextGen.push(Neat.species[0].giveMeBaby());
// console.log('3nd push ');
// console.log(nextGen.length)
}
// console.log(Neat.species.length)
// console.log(nextGen.length)
// console.log('--------------------')
Neat.dinos = nextGen;
dino = nextGen;
}
// static reproduce(){
// // console.log(Neat.species);
// for(let i = 0; i < Neat.kill_count; i++){
// let species = Neat.selectMostProbableSpecies();
// let genome1 = Neat.selectMostProbableGenome(species);
// let genome2 = Neat.selectMostProbableGenome(species);
// let baby = genome1.crossover(genome2);
// species.genomes.push(baby);
// }
// }
// static mutate(){
// Neat.genomes = [];
// for(let sample of Neat.species){
// for(let genome of sample.genomes){
// genome.mutate();
// Neat.genomes.push(genome);
// }
// }
// }
// static selectMostProbableSpecies(){
// let random_number = Math.random() * Neat.calculateSpeciesFitness();
// // console.log(Neat.species[0].genomes[0].score);
// for(let i = 0; i < Neat.species.length; i++){
// // console.log(Neat.species[i].score)
// random_number = random_number - Neat.species[i].score;
// // console.log(random_number)
// if(random_number <= 0){
// // console.log(Neat.species[i]);
// return Neat.species[i];
// }
// }
// }
static selectMostProbableDino(species){
let sum = 0;
for(let dino of species.dinos){
sum += dino.fitness;
}
let random_number = Math.random() * sum;
for(let i = 0; i < species.dinos.length; i++){
random_number = random_number - species.dinos[i].fitness;
if(random_number <= 0){
return species.dinos[i];
}
}
return species.dinos[0];
}
static calculateSpeciesFitness(){
let total_sum = 0;
for(let sample of Neat.species){
sample.score = 0;
for(let dino of sample.dinos){
// console.log(genome.score);
sample.score += dino.fitness;
// console.log(sample.score)
}
// sample.score /= sample.genomes.length;
total_sum += sample.score;
// console.log(total_sum)
}
return total_sum;
}
// static calculateSpeciesAverageFitness(){
// let total_sum = 0;
// for(let sample of Neat.species){
// sample.score = 0;
// for(let genome of sample.genomes){
// // console.log(genome.score);
// sample.score += genome.score;
// // console.log(sample.score)
// }
// sample.score /= sample.genomes.length;
// total_sum += sample.score;
// // console.log(total_sum)
// }
// return total_sum;
// }
static explicit_fitness_sharing(){
for(let sample of Neat.species){
for(let dino of sample.dinos){
dino.fitness /= sample.dinos.length;
}
}
}
}
Neat.connections = [];
Neat.C1 = 1;
Neat.C2 = 1;
Neat.C3 = 0.5;
Neat.threshold = 3;
Neat.kill_rate = 0.5;
// static kill_count = 0;
Neat.dinos = [];
Neat.species = [];