-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPopulation.jack
404 lines (367 loc) · 14.4 KB
/
Population.jack
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* Ohh boy, we have a lot to go over in this one. The Population module is
* where all the magic happens, and is what makes the genetic algorithm
* genetic.
*/
class Population {
static Array dots;
static Array fitnessCache;
static int bestDotFitness;
static Array newBrainDirections;
static int gen;
static int populationCount;
static int brainSize;
static boolean onlyBest;
static String allocatingString;
static String escapeString;
function void init() {
// let dots = null;
let allocatingString = "Allocating dot memory...";
let escapeString = "Hold escape in the simulation to open the menu.";
}
function void config(int _brainSize, boolean _onlyBest,
int initialBestDotFitness) {
var int i;
var Dot dot;
var Brain brain;
var Array directions;
var int diff;
var int remainingExtendedHeap;
var int remainingHeap;
do Output.moveCursor(11, 22);
do Output.printString(allocatingString);
do Output.moveCursor(15, 9);
do Output.printString(escapeString);
// later TODO if i implement reallocation
// if (~(dots = null)) {
// while (i < populationCount) {
// let dot = dots[i];
// do dot.dispose();
// let i = i + 1;
// }
// let i = 0;
// do dots.dispose();
// do fitnessCache.dispose();
// while (i < (populationCount - 1)) {
// let tmp = newBrainDirections[i];
// if (~(tmp > 16384)) {
// do tmp.dispose();
// let i = i + 1;
// } else {
// let i = populationCount;
// }
// }
// let i = 0;
// do newBrainDirections.dispose();
// do Memory.deFrag();
// }
let gen = 1;
let brainSize = _brainSize;
let onlyBest = _onlyBest;
let bestDotFitness = initialBestDotFitness;
let dot = Dot.new();
let brain = dot.getBrain();
let directions = brain.getDirections();
// It's about to get real ugly. We're going to do some surgery on the
// RAM, so if you're a bit squeamish please stand by.
let diff = (directions[-1] - dot) + 2;
let remainingHeap = 16384 - dot;
let remainingExtendedHeap = 24576 - dot;
/*
// populationCount upper bound
// dots
populationCount + 2
// dots memory
+ populationCount * diff
// fitnessCache
+ populationCount + 2
// newBrainDirections
+ (populationCount - 1) + 2
// newBrainDirections memory
+ (populationCount - 1) * (brainSize + 3)
// remaining space
= remainingExtendedHeap
populationCount + 2 + populationCount * diff + populationCount + 2 +
(populationCount - 1) + 2 + (populationCount - 1) * (brainSize + 3) =
remainingExtendedHeap
p + 2 + pd + p + 2 + p - 1 + 2 + (p - 1) * (b + 3) = remainingExtendedHeap
p + 2 + pd + p + 2 + p - 1 + 2 + pb + 3p - b - 3 = remainingExtendedHeap
p + pd + p + p + pb + p + p + p = remainingExtendedHeap - 2 + b
p(1 + d + 1 + 1 + b + 1 + 1 + 1) = remainingExtendedHeap - 2 + b
p = (remainingExtendedHeap + b - 2) / (d + b + 6)
// populationCount lower bound
// dots
populationCount + 2
// dots memory
+ populationCount * diff
// fitnessCache
+ populationCount + 2
// newBrainDirections
+ (populationCount - 1) + 2
// remaining space
= remainingHeap
populationCount + 2 + populationCount * diff + populationCount + 2 +
(populationCount - 1) + 2 = remainingHeap
p + 2 + pd + p + 2 + p - 1 + 2 = remainingHeap
p + pd + p + p = remainingHeap - 5
p(1 + d + 1 + 1) = remainingHeap - 5
p = (remainingHeap - 5) / (d + 3)
*/
let populationCount = Math.min(
(remainingExtendedHeap + brainSize - 2) / (diff + brainSize + 6),
(remainingHeap - 5) / (diff + 3)
);
let fitnessCache = Array.new(populationCount);
let dots = Array.new(populationCount);
let dots[0] = dot;
let i = 1;
while (i < populationCount) {
let dots[i] = Dot.new();
let i = i + 1;
}
let i = 0;
// serves as temporary swap memory for later on
// -1 because the best dot won't be changed
let newBrainDirections = Array.new(populationCount - 1);
if (~(newBrainDirections < 16387)) {
do Sys.error(6);
}
do Screen.setColor(false);
do Screen.drawRectangle(177, 122, 369, 133);
do Screen.drawRectangle(73, 166, 449, 177);
do Screen.setColor(true);
while (i < (populationCount - 1)) {
let newBrainDirections[i] = Array.new(brainSize + 1);
let i = i + 1;
}
do Screen.clearScreen();
}
function void toggleOnlyBest() {
let onlyBest = ~onlyBest;
}
function int getGen() {
return gen;
}
function int getBestDotFitness() {
return bestDotFitness;
}
function void update(boolean firstPairComponent) {
var int i;
var Dot dot;
while (i < populationCount) {
let dot = dots[i];
do dot.update((~onlyBest) | (i = 0), firstPairComponent);
let i = i + 1;
}
}
function boolean allDotsDead() {
var int i;
var Dot dot;
while (i < populationCount) {
let dot = dots[i];
if (~dot.getDead())
return false;
let i = i + 1;
}
return true;
}
function void naturalSelection() {
// This is the abracadabra of the program. Let's dive in.
var Dot dot;
var Dot bestDot;
var Brain brain;
var Brain bestDotBrain;
var int dotFitness;
var int i;
var int j;
var int selectionSum;
var int selectionSumCoef;
var int randFitness;
var int randFitnessCoef;
var int fitnessSum;
var int fitnessSumCoef;
var int randTo32000;
var int randToMinStep;
var Array directions;
var Array newDirections;
var int scaleCache;
var boolean mutated;
var int dynamicMutationRateTimes32;
var int minStep;
var int dotMinStepCache;
// Before we start, it's important to remember that any variable
// ending with Coef is a counter for the number of times the variable
// has overflown as a primitive way to represent larget numbers. You may
// see strange artichmetic done on these types of variables, but all
// it's really doing is preventing overflow.
// STEP 1: Calculate fitness of every dot and find the best one. We're
// also calculating the sum of all fitnesses to use for later.
// safe to set as 1 because 1) calcualteFitness return >= 1 and 2)
// bestDot can never be null after this loop
let bestDotFitness = 1;
while (i < populationCount) {
let dot = dots[i];
let dotFitness = dot.calculateFitness();
let fitnessCache[i] = dotFitness;
if (dotFitness > bestDotFitness) {
let bestDotFitness = dotFitness;
let bestDot = dot;
// ensures brain and bestDotBrain is only defined when necessary
} else if (dotFitness = bestDotFitness) {
let brain = dot.getBrain();
// doesnt run when bestDot is null
let bestDotBrain = bestDot.getBrain();
if (brain.getStep() < bestDotBrain.getStep()) {
let bestDot = dot;
}
}
let fitnessSum = fitnessSum + dotFitness;
if (fitnessSum < 0) {
let fitnessSum = fitnessSum + ~32767;
let fitnessSumCoef = fitnessSumCoef + 1;
}
let i = i + 1;
}
if (bestDot.getReachedGoal()) {
let brain = bestDot.getBrain();
let dotMinStepCache = brain.getStep();
do Dot.setMinStep(dotMinStepCache);
}
// STEP 2: Select a weighted random dot to be the parent of the next
// generation. For the number of dots, we select a random number between
// 0 and the fitness sum from step one. We then keep adding up dot
// fitnesses until it exceeds that number, and the dot that exceeds it
// is the parent. In a way, this acts as a weighted random selection
// where the higher the fitness, the more likely it is to be selected.
let i = 0;
while (i < (populationCount - 1)) {
if (fitnessSumCoef = 0) {
let randFitnessCoef = 0;
} else {
let scaleCache = 32767 / fitnessSumCoef;
let randFitnessCoef = 32767;
while (randFitnessCoef > fitnessSumCoef) {
// fitnessSumCoef = 296, randFitnessCoef = 32698
// this results in randFitnessCoef = 297 which is out of bounds
let randFitnessCoef = Math.abs(Util.random()) / scaleCache;
}
}
let randFitness = Math.abs(Util.random());
if (randFitnessCoef = fitnessSumCoef) {
if (fitnessSum = 0) {
let randFitness = 0;
} else {
let scaleCache = 32767 / fitnessSum;
while (randFitness > fitnessSum) {
// same with this it can also go out of bounds
let randFitness = Math.abs(Util.random()) / scaleCache;
}
}
}
let selectionSum = 0;
let selectionSumCoef = 0;
let j = 0;
while (j < populationCount) {
let selectionSum = selectionSum + fitnessCache[j];
if (selectionSum < 0) {
let selectionSum = selectionSum + ~32767;
let selectionSumCoef = selectionSumCoef + 1;
}
if ((selectionSumCoef > randFitnessCoef) |
((selectionSumCoef = randFitnessCoef) &
(selectionSum > randFitness))) {
let dot = dots[j];
let j = populationCount; // break
}
let j = j + 1;
}
let brain = dot.getBrain();
let directions = brain.getDirections();
let newDirections = newBrainDirections[i];
if (dotMinStepCache = 0) {
let minStep = brain.getStep();
} else {
let minStep = dotMinStepCache;
}
// STEP 3: Mutate the parent's brain and store it in the swap memory
// allocated earlier. Did I say swap memory? I also meant the screen
// memory! Indeed, because we don't have enough RAM, we temporarily
// use the screen as a memory buffer. This explains why you see
// static on the screen during natural selection :)
// where 1530 came from is a long story. If I decide to fully
// explain it, here's a conversation I had with my friend for
// reference for future me:
// https://discord.com/channels/@me/890505752064716811/1173360871511183390
// in short, the mutation rate is a function of the amount of steps
// in the brain such that the mutation rate binomial distribution
// is roughly constant
let dynamicMutationRateTimes32 = Math.min(1000, 1530 / minStep) * 32;
let mutated = false;
let j = 0;
while (j < minStep) {
// scaleCache = 32767 / 1000 = 32
let randTo32000 = Math.abs(Util.random());
while (~(randTo32000 < 32000)) {
let randTo32000 = Math.abs(Util.random());
}
if (randTo32000 < dynamicMutationRateTimes32) {
let newDirections[j] = AccelerationVectorPair.random();
let mutated = true;
} else {
let newDirections[j] = directions[j];
}
let j = j + 1;
}
let newDirections[brainSize] = minStep; // cache for later
if (~mutated) {
// if nothing mutated that's no fun! Force it to mutate anyways
// through the use of radiation exposure
let scaleCache = 32767 / minStep;
let randToMinStep = minStep;
// randToMinStep can be negative Math.abs does not guarantee
// positive (-32768)
while (~((randToMinStep > -1) & (randToMinStep < minStep))) {
let randToMinStep = Math.abs(Util.random()) / scaleCache;
}
let newDirections[randToMinStep] = AccelerationVectorPair.random();
}
let i = i + 1;
}
// STEP 4: Copy the swap memory back to the RAM and reinstantiate
// everything.
let i = 0;
while (i < populationCount) {
let dot = dots[i];
let brain = dot.getBrain();
let directions = brain.getDirections();
do dot.instantiate();
do brain.instantiate();
let j = 0;
if (~(i = 0)) {
let newDirections = newBrainDirections[i - 1];
let minStep = newDirections[brainSize];
} else if (dot = bestDot) {
let minStep = 0;
} else {
let brain = bestDot.getBrain();
let newDirections = brain.getDirections();
let minStep = brainSize;
}
while (j < minStep) {
let directions[j] = newDirections[j];
let j = j + 1;
}
let i = i + 1;
}
let gen = gen + 1;
}
function void instantiateDots() {
var int i;
var Dot dot;
while (i < populationCount) {
let dot = dots[i];
do dot.instantiate();
let i = i + 1;
}
}
}