-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpecies.js
executable file
·50 lines (40 loc) · 1.53 KB
/
Species.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
class Species{
constructor(representative){
this.representative = representative; // a player which will represent this species
this.representative.species = this;
this.players = [this.representative]; // list of players which are in this species
this.fitness = 0; // average fitness of the species
this.staleness = 0; // measure of how many generations have the species lived without increase in its best Score
this.bestFitness = 0; // best ever fitness score achived by its member
}
// returns a baby of this species
giveMeBaby(){
let baby;
// no crossover 25% of the time
if(Math.random() < 0.25){
baby = this.selectMostProbablePlayer();
} else {
let player1 = this.selectMostProbablePlayer();
let player2 = this.selectMostProbablePlayer();
baby = player1.crossover(player2);
}
// mutate the baby
baby.mutate();
return baby;
}
// return the most probable player
selectMostProbablePlayer(){
let sum = 0;
for(let player of this.players){
sum += player.fitness;
}
let random_number = Math.random() * sum;
for(let i = 0; i < this.players.length; i++){
random_number = random_number - this.players[i].fitness;
if(random_number <= 0){
return this.players[i];
}
}
return this.players[0];
}
}