-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopulation.pde
More file actions
266 lines (230 loc) · 8.77 KB
/
Population.pde
File metadata and controls
266 lines (230 loc) · 8.77 KB
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
class Population {
ArrayList<Colony> colonies = new ArrayList<Colony>();
ArrayList<Colony> nextGenColonies = new ArrayList<Colony>();
ArrayList<Species> species = new ArrayList<Species>();
float totalAvgFitness = 0, avgFitness = 0;
int population = 0;
float topScore, generationTopScore;
int topScoreGeneration, noMoveTurns, lastSpeciesCount;
boolean movement;
Population() {}
void frame() {
generationElapsed++;
for (Colony colony : colonies) {
colony.frame();
}
for (Tile[] column : grid) {
for (Tile tile : column) {
tile.frame();
}
}
if (!movement) {
noMoveTurns++;
} else {
noMoveTurns = 0;
}
movement = false;
if (generationElapsed >= generationLength || population == 0 ||
(noMoveTurns > 3 && generationElapsed > 3)) {
generationElapsed = 0;
skipGeneration = false;
generationCount++;
generationIteration++;
if (generationIteration >= generationSpeed) {
generationIteration = 0;
}
noMoveTurns = 0;
endGeneration();
beginGeneration();
}
}
void firstGeneration() {
for (int i = 0; i < populationSize; i++) {
colonies.add(new Colony());
}
beginGeneration();
}
void beginGeneration() {
population = 0;
speciation();
noiseSeed(millis()); // ensures a different seed every generation
updateGenerationLength(generationCount);
setFood();
}
void endGeneration() {
generationTopScore = 0;
// Give some fitness for ants that have food, but haven't made it back
// to the hill, and for every ant that exists
for (Colony colony : colonies) {
for (Ant ant : colony.ants) {
colony.fitness += ant.foodLevel * 0.01;
colony.fitness += 0.001;
}
colony.fitness =
pow(colony.fitness, 2); // square it to be more greedy
}
grid.reset();
totalAvgFitness = 0;
for (Species specie : species) {
specie.fitnessSharing();
totalAvgFitness += specie.totalFitness;
}
avgFitness = totalAvgFitness / species.size();
lastSpeciesCount = species.size();
createOffspring();
for (int i = 0; i < species.size(); i++) {
Species specie = species.get(i);
specie.performNaturalSelection();
nextGenColonies.addAll(0, specie.colonies);
if (specie.colonies.isEmpty()) {
species.remove(specie);
i--;
}
for (Colony colony : specie.colonies) {
colony.reset();
}
}
colonies = (ArrayList<Colony>) nextGenColonies.clone();
nextGenColonies.clear();
for (Colony colony : colonies) {
if (!colony.spawned) {
colony.spawn();
}
colony.fitness = 0;
}
println(
"Generation",
generationCount,
"\ttop score:",
nf(generationTopScore, 0, 8),
"(" + nf(pow(generationTopScore, 0.5), 0, 8) + ")",
"\taverage score:",
nf(avgFitness, 0, 8),
"\tspecies count:",
lastSpeciesCount
);
}
void createOffspring() {
for (Species specie : species) {
// float debug = 0;
// for (Colony colony : specie.colonies) {
// debug += colony.fitness;
// }
// println("\nfor " + specie.id, debug, specie.totalFitness);
for (int i = 0; i < ceil(
(specie.totalFitness / totalAvgFitness) *
populationSize -
specie.colonies.size() * generationPercent
);
i++) {
Species chosenSpecies = specie;
if (specie.stagnantGenerations > dropoffAge) {
chosenSpecies = weightedRandomSpecies();
// println("instead chose " + chosenSpecies.id);
}
if (random(1) < 0.25) {
// Just mutate a random colony
Network network =
chosenSpecies.weightedRandomColony().network.copy();
nextGenColonies.add(new Colony(network.mutate()));
} else if (random(1) < 0.001) {
// Mutate between species
Colony colony1 = weightedRandomColony(),
colony2 = weightedRandomColony();
nextGenColonies.add(new Colony(colony1.network
.createOffspring(
colony2.network,
colony1.fitness,
colony2.fitness
)
.mutate()));
} else {
// Mutate within species
Colony colony1 = chosenSpecies.weightedRandomColony(),
colony2 = chosenSpecies.weightedRandomColony();
nextGenColonies.add(new Colony(colony1.network
.createOffspring(
colony2.network,
colony1.fitness,
colony2.fitness
)
.mutate()));
}
}
}
}
Species weightedRandomSpecies() {
float rand = random(totalAvgFitness);
for (int i = 0; i < species.size(); i++) {
Species specie = species.get(i);
rand -= specie.totalFitness;
if (rand <= 0) {
if (specie.stagnantGenerations > dropoffAge) {
i = 0;
rand = random(totalAvgFitness);
} else {
return specie;
}
}
}
return species.get(species.size() - 1);
}
Colony weightedRandomColony() {
float rand = random(totalAvgFitness);
for (int i = 0; i < colonies.size(); i++) {
Colony colony = colonies.get(i);
rand -= colony.fitness;
if (rand <= 0) {
if (colony.species.stagnantGenerations > dropoffAge) {
i = 0;
rand = random(totalAvgFitness);
} else {
return colony;
}
}
}
return colonies.get(colonies.size() - 1);
}
void setFood() {
updateFoodCutoff(generationCount);
for (int x = 0; x < grid.width; x++) {
for (int y = 0; y < grid.height; y++) {
Tile tile = grid.get(x, y);
if (tile.agent == null) { // no colony or ant on this tile
float noiseValue =
noise(x * noiseCoefficient, y * noiseCoefficient);
for (Tile neighbor : tile.neighbors()) {
if (neighbor.agent instanceof Colony) noiseValue = 0.5;
// start each colony with food surrounding it
}
// tile.pushFood(max(0, noiseValue - foodConcentration) / (1
// - foodConcentration));
tile.pushFood(noiseValue < foodCutoff ? 0 : noiseValue);
}
}
}
}
void speciation() {
for (Colony colony : colonies) {
if (colony.species == null) {
for (Species specie : species) {
if (specie.compare(colony)) {
colony.species = specie;
specie.colonies.add(colony);
break;
}
}
if (colony.species == null) {
species.add(new Species(colony));
}
colony.chooseColor();
}
colony.initialPopulation();
}
if (species.size() > numSpecies) {
compatibilityThreshold += compatibilityModifier;
} else if (species.size() < numSpecies) {
compatibilityThreshold -= compatibilityModifier;
}
}
}