-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPopulation.js
173 lines (141 loc) · 5.58 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Population {
constructor(size) {
this.players = [];
this.fitnessSum = 0.0;
this.gen = 1;
this.bestPlayer =0;
this.minStep = 10000;
this.genPlayers = [];
this.bestFitness = 0;
this.solutionFound = false;
for (var i = 0; i< size; i++) {
this.players[i] = new Player();
}
}
//------------------------------------------------------------------------------------------------------------------------------
//show all players
show() {
if (!showBest) {
for (var i = 1; i< this.players.length; i++) {
this.players[i].show();
}
}
this.players[0].show();
}
//-------------------------------------------------------------------------------------------------------------------------------
//update all players
update() {
for (var i = 0; i< this.players.length; i++) {
if (this.players[i].brain.step > this.minStep) {//if the player has already taken more steps than the best player has taken to reach the goal
this.players[i].dead = true;//then it dead
} else {
this.players[i].update();
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------
//calculate all the fitnesses
calculateFitness() {
for (var i = 0; i< this.players.length; i++) {
this.players[i].calculateFitness();
}
}
//------------------------------------------------------------------------------------------------------------------------------------
//returns whether all the players are either dead or have reached the goal
allPlayersDead() {
for (var i = 0; i< this.players.length; i++) {
if (!this.players[i].dead && !this.players[i].reachedGoal) {
return false;
}
}
print("bah:");
return true;
}
//-------------------------------------------------------------------------------------------------------------------------------------
//gets the next generation of players
naturalSelection() {
var newPlayers = [];//next gen
this.setBestPlayer();
this.calculateFitnessSum();
//the champion lives on
newPlayers[0] = this.players[this.bestPlayer].gimmeBaby();
newPlayers[0].isBest = true;
for (var i = 1; i< populationSize; i++) {
//select parent based on fitness
var parent = this.selectParent();
//get baby from them
newPlayers[i] = parent.gimmeBaby();
}
// this.players = newPlayers.slice();
this.players = [];
for(var i = 0 ; i< newPlayers.length;i++){
this.players[i] = newPlayers[i];
}
this.gen ++;
}
//--------------------------------------------------------------------------------------------------------------------------------------
//you get it
calculateFitnessSum() {
this.fitnessSum = 0;
for (var i = 0; i< this.players.length; i++) {
this.fitnessSum += this.players[i].fitness;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
//chooses player from the population to return randomly(considering fitness)
//this function works by randomly choosing a value between 0 and the sum of all the fitnesses
//then go through all the players and add their fitness to a running sum and if that sum is greater than the random value generated that player is chosen
//since players with a higher fitness function add more to the running sum then they have a higher chance of being chosen
selectParent() {
var rand = random(this.fitnessSum);
var runningSum = 0;
for (var i = 0; i< this.players.length; i++) {
runningSum+= this.players[i].fitness;
if (runningSum > rand) {
return this.players[i];
}
}
//should never get to this point
return null;
}
//------------------------------------------------------------------------------------------------------------------------------------------
//mutates all the brains of the babies
mutateDemBabies() {
for (var i = 1; i< this.players.length; i++) {
this.players[i].brain.mutate(this.players[i].deathByDot, this.players[i].deathAtStep);
this.players[i].deathByDot = false;
this.players[i].gen = this.gen;
}
this.players[0].deathByDot = false;
this.players[0].gen = this.gen;
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//finds the player with the highest fitness and sets it as the best player
setBestPlayer() {
var max = 0;
var maxIndex = 0;
for (var i = 0; i< this.players.length; i++) {
if (this.players[i].fitness > max) {
max = this.players[i].fitness;
maxIndex = i;
}
}
this.bestPlayer = maxIndex;
if (max > this.bestFitness) {
this.bestFitness = max;
this.genPlayers.push(this.players[this.bestPlayer].gimmeBaby());
}
//if this player reached the goal then reset the minimum number of steps it takes to get to the goal
if (this.players[this.bestPlayer].reachedGoal) {
this.minStep = this.players[this.bestPlayer].brain.step;
this.solutionFound = true;
}
}
increaseMoves() {
if (this.players[0].brain.directions.length < 10000000 && !this.solutionFound) {
for (var i = 0; i< this.players.length; i++) {
this.players[i].brain.increaseMoves();
}
}
}
}