-
Notifications
You must be signed in to change notification settings - Fork 0
/
Population.cs
240 lines (208 loc) · 6.54 KB
/
Population.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SnakeEvo
{
class Population
{
public Snake[] snakes;
public int gen = 1;
long globalBestFitness = 0;
public int currentBest = 4;
int leftTimeLive;
public Snake globalBestSnake;
int populationID;
public Population(int size,int leftTime)
{
snakes = new Snake[size];
for(int i = 0; i < size; i++)
{
snakes[i] = new Snake(leftTime);
}
populationID = Rnd.RandomNumber(0, 10000);
leftTimeLive = leftTime;
}
public void UpdateAlive()
{
for (int i = 0; i < snakes.Length; i++)
{
if (snakes[i].alive)
{
snakes[i].Look();
snakes[i].HighestFood();//
snakes[i].SetVelocity();
snakes[i].Move();
}
}
}
public bool Done()
{
for(int i=0; i <snakes.Length; i++)
{
if (snakes[i].alive)
{
return false;
}
}
return true;
}
public int StillAlive(out int lenAlive, out int lenAll)
{
int sum = 0;
lenAlive = 0;
lenAll = 0;
for (int i = 0; i < snakes.Length; i++)
{
if (snakes[i].alive)
{
sum++;
if (lenAlive < snakes[i].len)
{
lenAlive = snakes[i].len;
}
}
if (lenAll < snakes[i].len)
{
lenAll = snakes[i].len;
}
}
return sum;
}
public void CalcFitness(bool elitism)
{
for(int i = 0; i < snakes.Length; i++)
{
snakes[i].CalcFitness(elitism);
}
}
public void Mutate(float mr)
{
for ( int i = 1; i <snakes.Length; i++)
{
snakes[i].Mutate(mr);
}
}
public Snake SelectSnake()
{
long fitnessSum = 0;
for( int i=0; i<snakes.Length; i++)
{
fitnessSum += snakes[i].fitness;
}
long rand = (long)Math.Floor(Rnd.RandomDouble() * fitnessSum);
long runningSum = 0;
for(int i = 0; i < snakes.Length; i++)
{
runningSum += snakes[i].fitness;
if(runningSum > rand)
{
return snakes[i];
}
}
return snakes[0];
}
public Snake SelectParent (Snake[] parents)
{
return parents[Rnd.RandomNumber(0,parents.Length)];
}
public void SetBestSnake()
{
long max = 0;
int maxIndex = 0;
for (int i=0; i < snakes.Length; i++)
{
if(snakes[i].fitness > max)
{
max = snakes[i].fitness;
maxIndex = i;
}
}
if(max > globalBestFitness)
{
globalBestFitness = max;
globalBestSnake = snakes[maxIndex].Clone(leftTimeLive);
}
}
public void NaturalSelectionImproved(float mr , float percentOld)
{
int j = 0;
long fitnessPivot;
int parents = (int)(snakes.Length * percentOld);
int children = snakes.Length - parents;
long[] fitnessArray = new long[snakes.Length];
for (int i = 0; i < snakes.Length; i++)
{
fitnessArray[i] = snakes[i].fitness;
}
Array.Sort(fitnessArray);
Array.Reverse(fitnessArray);
fitnessPivot = fitnessArray[parents];
Snake[] newSnakes = new Snake[snakes.Length];
Snake[] parentsSnakes = new Snake[parents];
Snake[] childrenSnakes = new Snake[children];
for (int i = 0; i < snakes.Length; i++)
{
if(snakes[i].fitness >= fitnessPivot)
{
//snakes[i].Mutate(mr);
parentsSnakes[j] = snakes[i];
j++;
if( j == parentsSnakes.Length) {
break;
}
}
}
for (int i = 0; i < childrenSnakes.Length; i++)
{
Snake parent1 = SelectSnake();
Snake parent2 = SelectSnake();
Snake child = parent1.Crossover(parent2);
child.Mutate(mr);
child.family = 1;
childrenSnakes[i] = child;
}
SetBestSnake();
newSnakes[0] = globalBestSnake.Clone(leftTimeLive);
j = 0;
for (int i = 0; i < snakes.Length; i++)
{
if (i >= parentsSnakes.Length)
{
newSnakes[i] = childrenSnakes[j];
j++;
}
else
{
newSnakes[i] = parentsSnakes[i].Clone(leftTimeLive);
}
}
newSnakes[0] = globalBestSnake.Clone(leftTimeLive);
newSnakes[0].family = 2;
newSnakes[1] = globalBestSnake.Clone(leftTimeLive);
newSnakes[1].Mutate(mr);
newSnakes[1].family = 2;
snakes = newSnakes;
gen += 1;
currentBest = 4;
}
public void NaturalSelection(float mr)
{
Snake[] newSnakes = new Snake[snakes.Length];
SetBestSnake();
newSnakes[0] = globalBestSnake.Clone(leftTimeLive);
for( int i = 1; i < newSnakes.Length; i++)
{
Snake parent1 = SelectSnake();
Snake parent2 = SelectSnake();
Snake child = parent1.Crossover(parent2);
child.Mutate(mr);
newSnakes[i] = child;
}
snakes = newSnakes;
gen += 1;
currentBest = 4;
}
}
}