Repository navigation
Expand file tree
/
Copy pathgeneticAlgorithm.js
More file actions
378 lines (330 loc) · 12.2 KB
/
Copy pathgeneticAlgorithm.js
File metadata and controls
378 lines (330 loc) · 12.2 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
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
/**
* Creates generations of a specified size by assigning random weights to an object.
* @param {Array} parents Array containing genome weights. W
* @returns {Array} Generation array containing the genome weights.
*/
function createGeneration(sortedGenomes) {
generationNum++;
var generation = [];
var generationSize = 50;
if (sortedGenomes === undefined) { // Initialization
for (var i = 0; i < generationSize; i++) {
var genomeWeights = {
rowsCleared: Math.random() - 0.5,
weightedHeight: Math.random() - 0.5,
cumulativeHeight: Math.random() - 0.5,
relativeHeight: Math.random() - 0.5,
holes: Math.random() - 0.5,
roughness: Math.random() - 0.5
}
generation.push(genomeWeights);
}
} else {
var mutationRate = 0.1; // Degree of mutation
var mutationProbability = 0.05;
var geneticDiversity = 0.1;
function randomParent() { // Returns random genome of the top X% wrt fitness where X is the geneticDiversity.
return sortedGenomes[Math.floor(Math.random()*Math.floor(sortedGenomes.length*geneticDiversity))];
}
for (var i = 0; i < generationSize; i++) {
var baseGenome = crossBreed([randomParent(), randomParent()]);
var genomeWeights = {
rowsCleared: baseGenome.rowsCleared,
weightedHeight: baseGenome.weightedHeight,
cumulativeHeight: baseGenome.cumulativeHeight,
relativeHeight: baseGenome.relativeHeight,
holes: baseGenome.holes,
roughness: baseGenome.roughness
}
if (Math.random() < mutationProbability) { // base value + ((Random float from -1 to 1) * mutationRate)
genomeWeights.rowsCleared = baseGenome.rowsCleared + (((2*Math.random()) - 1) * mutationRate);
}
if (Math.random() < mutationProbability) {
genomeWeights.weightedHeight = baseGenome.weightedHeight + (((2*Math.random()) - 1) * mutationRate);
}
if (Math.random() < mutationProbability) {
genomeWeights.cumulativeHeight = baseGenome.cumulativeHeight + (((2*Math.random()) - 1) * mutationRate);
}
if (Math.random() < mutationProbability) {
genomeWeights.relativeHeight = baseGenome.relativeHeight + (((2*Math.random()) - 1) * mutationRate);
}
if (Math.random() < mutationProbability) {
genomeWeights.holes = baseGenome.holes + (((2*Math.random()) - 1) * mutationRate);
}
if (Math.random() < mutationProbability) {
genomeWeights.roughness = baseGenome.roughness + (((2*Math.random()) - 1) * mutationRate);
}
generation.push(genomeWeights);
}
}
return generation;
}
/**
* "Kills" a genome which does a few things:
* 1) Sets up the next genome.
* 2) Resets the board and starts game.
* OR
* 1) Ends generation.
* 2) Creates next generation and starts game.
*/
function terminateGenome() {
currGenome['fitness'] = score;
if (currGeneration.length != 0) {
currGenerationDeaths.push(currGenome);
currGenome = currGeneration.pop();
gameOver = false;
reset(startBoard);
spawnShape(true);
} else {
currGenerationDeaths.push(currGenome);
// Merge sort used to sort by fitness -- parents are sampled from the top tier
function merge(arr1, arr2) { // arr1 and arr2 are two sorted arrays
var sortedArr = [];
while (arr1.length != 0 && arr2.length != 0) {
if (arr1[0].fitness > arr2[0].fitness) {
if ((arr1[0].fitness) > bestGenome.fitness) { // Highest to lowest sorting
bestGenome = arr1[0];
}
sortedArr.push(arr1.shift());
} else {
sortedArr.push(arr2.shift());
}
}
while (arr1.length != 0) {
sortedArr.push(arr1.shift());
}
while (arr2.length != 0) {
sortedArr.push(arr2.shift());
}
return sortedArr;
}
function mergeSort(array) {
if (array.length == 1) {
return array;
} else {
var mid = Math.floor((array.length)/2);
var arr1 = array.slice(0, mid);
var arr2 = array.slice(mid, array.length);
arr1 = mergeSort(arr1);
arr2 = mergeSort(arr2);
return (merge(arr1, arr2));
}
}
var sortedGenomes = mergeSort(currGenerationDeaths);
currGenerationDeaths = [];
gameOver = false;
currGeneration = createGeneration(sortedGenomes);
}
}
/**
* Produces child genome from two parent genomes whose weights are a mix of the parents.
* @param {Array} parents Array of two genomes.
* @returns {genome} Genome made approx. 50/50 of each of the genomes in the inputted array.
*/
function crossBreed(parents) {
var randomParent = function () {
return parents[Math.floor(Math.random() * 2)];
}
var genomeWeights = {
rowsCleared: randomParent().rowsCleared,
weightedHeight: randomParent().weightedHeight,
cumulativeHeight: randomParent().cumulativeHeight,
relativeHeight: randomParent().relativeHeight,
holes: randomParent().holes,
roughness: randomParent().roughness
}
return genomeWeights;
}
/**
* Saves current state of the board in an object and returns that object.
*/
function saveBoard() {
var move = {
gameboard: copyGameboard(gameboard.gameboard),
pieces: copyPieces(gameboard.pieces),
activeShape: gameboard.activeShape.copy(),
rand_shapes: Array.from(rand_shapes),
score: score,
level: level,
lines_cleared: lines_cleared
};
return move;
}
/**
* Resets the board to a passed game state.
* @param move Formerly saved game state returned with saveBoard().
*/
function reset(move) {
gameboard.gameboard = copyGameboard(move.gameboard);
gameboard.pieces = copyPieces(move.pieces);
gameboard.activeShape = move.activeShape.copy();
rand_shapes = Array.from(move.rand_shapes);
score = move.score;
level = move.level;
lines_cleared = move.lines_cleared;
loop();
}
/**
* Tests all possible moves and returns an array of them.
* @return possibleMoves Array of of all possible moves .
*/
function getPossibleMoves() {
var possibleMovesArr = [];
var startPosition = saveBoard(); // Saves very start position
for (var rotationState = 0; rotationState < 4; rotationState++) {
for (var i = 0; i < 10; i++) {
while(gameboard.moveLeft()) { // Moves all the way to the left
}
for (var j = 0; j < i; j++) { // Moves incrementally to the right (10 times til the end)
gameboard.moveRight();
}
while(gameboard.moveDown(true, true)) { // Moves all the way down
}
var currentState = saveBoard();
currentState["rowsCleared"] = currentState.lines_cleared - startPosition.lines_cleared;
possibleMovesArr.push(saveBoard());
reset(startPosition);
}
gameboard.rotate();
startPosition = saveBoard();
}
return possibleMovesArr;
}
/**
* Returns the best move as calculated by the provided genome.
* @param {move} possibleMovesArr Array containing all possible moves.
* @param {genomeWeights} genome Genome that contains all the weights.
* @returns {move} Returns the best move.
*/
function getBestMove(possibleMovesArr, genome) {
var maxRating = -9999;
var bestMove;
for (let move = 0; move < possibleMovesArr.length; move++) {
var currMove = possibleMovesArr[move];
var kpis = getMoveKPIs(currMove);
var rating = (kpis.rowsCleared * genome.rowsCleared) + (kpis.weightedHeight * genome.weightedHeight);
rating += (kpis.cumulativeHeight * genome.cumulativeHeight) + (kpis.relativeHeight * genome.relativeHeight);
rating += (kpis.holes * genome.holes) + (kpis.roughness * genome.roughness);
if (rating > maxRating) {
maxRating = rating;
bestMove = currMove;
}
}
return bestMove;
}
/**
* Gets and executes the best possible move based on the provided genome.
* @param {genomeWeights} genome Object containing the weights.
*/
function makeBestMove(genome) {
if (geneticAlgCB.checked()) {
if (genome.rowsCleared === undefined) { // Checking if it was called from ticking the checkbox
genome = currGenome;
}
var possibleMoves = getPossibleMoves();
var bestMove = getBestMove(possibleMoves, genome);
reset(bestMove);
} else {
terminateGenome();
}
}
/**
* Helper function that returns a deep copy of the gameboard array.
* @param gameboard Gameboard array
*/
function copyGameboard(gameboard) {
newGameboard = [];
gameboard.forEach(column => {
var newColumn = [];
column.forEach(block => {
if (!(block === null)) {
newColumn.push(block.copy());
} else {
newColumn.push(null);
}
});
newGameboard.push(newColumn);
});
return newGameboard;
}
/**
* Helper functipn that returns a deep copy of the pieces array
* @param pieces Pieces array
*/
function copyPieces(pieces) {
var newPieces = [];
pieces.forEach(position => {
var newPosition = [];
position.forEach(coordinate => {
newPosition.push(coordinate);
});
newPieces.push(newPosition);
});
return newPieces;
}
/**
* Gets the amount of full lines when provided with a pieces array.
* @param {Array} pieces Array of activePieces.
*/
function getFullLines(pieces) {
let row_status = {}; // Object that holds the row indices as keys and the number of blocks in the row as an entry
let full_lines = []; // Holds the indices of all of the full rows
pieces.forEach(function (position) { // Counts the number of blocks in each row.
let curr_row = position[1];
row_status[curr_row] = (row_status[curr_row] || 0) + 1;
});
Object.entries(row_status).forEach(function (row) { // Finds the lines that are full
if (row[1] == 10) {
full_lines.push(parseInt(row[0]));
}
});
return full_lines;
}
/**
* Gets key performance indicators that the quality of a move is judged on.
* @param {move} move A move object.
*/
function getMoveKPIs(move) {
var rowsCleared = getFullLines(move.pieces).length;
// Getting array of column heights to get other KPIs
var columnHeights = []
var holes = 0;
move.gameboard.forEach(function (column) {
var colHeight = 0;
var blockAbove = false;
for (var row = 0; row < 20; row++) {
if (column[row] != null && blockAbove === false) {
colHeight = row;
blockAbove = true;
} else if (column[row] === null && blockAbove === true) {
holes++;
}
}
columnHeights.push(20 - colHeight);
});
// Getting weighted height
var highestColumn = Math.max.apply(null, columnHeights);
var lowestColumn = Math.min.apply(null, columnHeights);
var weightedHeight = Math.pow(highestColumn, 1.5);
// Getting cumulative heights
var cumulativeHeight = 0;
columnHeights.forEach(function (height) {
cumulativeHeight += height;
});
// Getting relative height
var relativeHeight = highestColumn - lowestColumn;
// Getting roughness
var roughness = 0;
for (var i = 0; i < columnHeights.length - 1; i++) {
roughness += Math.abs(columnHeights[i] - columnHeights[i + 1]);
}
var kpis = {
rowsCleared: rowsCleared,
weightedHeight: weightedHeight,
cumulativeHeight: cumulativeHeight,
relativeHeight: relativeHeight,
holes: holes,
roughness: roughness
};
return kpis;
}