-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathPopulation.js
165 lines (138 loc) · 5.46 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
let alreadyShowingSnow = false;
class Population {
constructor(size) {
this.popSize = size;
this.players = [];
for (let i = 0; i < size; i++) {
this.players.push(new Player());
}
this.showingFail = false;
this.failPlayerNo = 0;
this.bestPlayerIndex = 0;
this.currentHighestPlayerIndex = 0;
this.fitnessSum = 0;
this.gen = 1;
this.bestHeight = 0;
this.showingLevelNo = 0;
this.currentBestLevelReached = 0;
this.purgeTheSlackers = false;
this.reachedBestLevelAtActionNo = 0;
this.newLevelReached = false;
this.cloneOfBestPlayerFromPreviousGeneration = this.players[0].clone();
}
Update() {
for (let i = 0; i < this.players.length; i++) {
this.players[i].Update();
// if(this.players[i].currentLevelNo >0 && !this.showingFail ){
// this.showingFail= true;
// this.failPlayerNo = i;
// this.ResetAllPlayers()
// }
}
}
SetBestPlayer() {
this.bestPlayerIndex = 0;
this.newLevelReached = false;
for (let i = 0; i < this.players.length; i++) {
if (this.players[i].bestHeightReached > this.players[this.bestPlayerIndex].bestHeightReached) {
this.bestPlayerIndex = i;
}
}
if (this.players[this.bestPlayerIndex].bestLevelReached > this.currentBestLevelReached) {
this.currentBestLevelReached = this.players[this.bestPlayerIndex].bestLevelReached;
this.newLevelReached = true;
this.reachedBestLevelAtActionNo = this.players[this.bestPlayerIndex].bestLevelReachedOnActionNo;
print("NEW LEVEL, action number", this.reachedBestLevelAtActionNo)
}
this.bestHeight = this.players[this.bestPlayerIndex].bestHeightReached;
}
SetCurrentHighestPlayer() {
this.currentHighestPlayerIndex = 0;
for (let i = 0; i < this.players.length; i++) {
if (this.players[i].GetGlobalHeight() > this.players[this.currentHighestPlayerIndex].GetGlobalHeight()) {
this.currentHighestPlayerIndex = i;
}
}
}
Show() {
this.SetCurrentHighestPlayer()
let highestPlayer = this.players[this.currentHighestPlayerIndex];
let highestLevelNo = this.players[this.currentHighestPlayerIndex].currentLevelNo;
if(highestPlayer.currentLevelNo > highestPlayer.bestLevelReached && !highestPlayer.progressionCoinPickedUp){
highestLevelNo -=1;
}
showLevel(highestLevelNo);
alreadyShowingSnow = false;
this.showingLevelNo = highestLevelNo;
for (let i = 0; i < this.players.length; i++) {
if (this.players[i].currentLevelNo >= highestLevelNo - 1 && this.players[i].currentLevelNo <=highestLevelNo ) {
this.players[i].Show();
}
}
// this.ShowPopulationInfo();
}
ResetAllPlayers() {
for (let i = 0; i < this.players.length; i++) {
this.players[i].ResetPlayer();
}
}
IncreasePlayerMoves(increaseBy) {
for (let i = 0; i < this.players.length; i++) {
this.players[i].brain.increaseMoves(increaseBy);
}
}
AllPlayersFinished() {
for (let i = 0; i < this.players.length; i++) {
if (!this.players[i].hasFinishedInstructions) {
return false;
}
}
return true;
}
NaturalSelection() {
let nextGen = [];
this.SetBestPlayer();
this.CalculateFitnessSum();
this.cloneOfBestPlayerFromPreviousGeneration = this.players[this.bestPlayerIndex].clone();
nextGen.push(this.players[this.bestPlayerIndex].clone());
for (let i = 1; i < this.players.length; i++) {
let parent = this.SelectParent();
let baby = parent.clone()
// if the parent fell to the previous level then mutate the baby at the action that caused them to fall
if(parent.fellToPreviousLevel){
baby.brain.mutateActionNumber(parent.fellOnActionNo);
}
baby.brain.mutate();
nextGen.push(baby);
}
this.players = [];
for (let i = 0; i < nextGen.length; i++) {
this.players[i] = nextGen[i];
// if(!this.newLevelReached && this.currentBestLevelReached !== 0){// && this.currentBestLevelReached !== 37){
// this.players[i].loadStartOfBestLevelPlayerState();
// }<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
this.gen++;
}
CalculateFitnessSum() {
this.fitnessSum = 0;
for (let i = 0; i < this.players.length; i++) {
this.players[i].CalculateFitness();
// if (this.players[i].bestLevelReached < this.players[this.bestPlayerIndex].bestLevelReached) {
// this.players[i].fitness = 0;
// }<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
this.fitnessSum += this.players[i].fitness;
}
}
SelectParent() {
let rand = random(this.fitnessSum);
let runningSum = 0;
for (let i = 0; i < this.players.length; i++) {
runningSum += this.players[i].fitness;
if (runningSum > rand) {
return this.players[i];
}
}
return null;
}
}