-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.cpp
429 lines (384 loc) · 18 KB
/
ai.cpp
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// Created by Rafael on 10/10/2019.
//
#include "ai.h"
#include <string.h>
#include <fstream>
const unsigned int seed = time(0);
mt19937_64 rng(seed);
double mutationRate = 0.05;
double mutationStep = 0.2;
int moveLimit = 500;
int filledSpotCount(const unsigned char *pField) {
int filledSpots=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 0; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]!=0)
filledSpots++;
}
return filledSpots;
}
int maximumWellDepth(const unsigned char *pField) {
int depthSize=0,maximumDepth=0,nAltitude=maximumAltitude(pField);
for (int px = 1; px < nFieldWidth - 1; px++){
int py=nFieldHeight-nAltitude-1;
while(pField[py * nFieldWidth + px]==0){
py++;
depthSize=nFieldHeight-py-1;
}
if(maximumDepth<depthSize)
maximumDepth=depthSize;
}
return maximumDepth;
}
int weightedFilledSpotCount(const unsigned char *pField) {
int filledSpots=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 0; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]!=0)
filledSpots=filledSpots+1*(nFieldHeight-py-1);
}
return filledSpots;
}
int maximumAltitude(const unsigned char *pField) {
int maxBlockPos=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 0; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]!=0 && (nFieldHeight-py-1)>maxBlockPos)
maxBlockPos=(nFieldHeight-py-1);
}
return maxBlockPos;
}
int holeCount(const unsigned char *pField) {
int nCount=0,pOldY;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 1; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]==0 && pField[(py-1) * nFieldWidth + px]!=0){
nCount++;
pOldY=py+1;
while(pField[pOldY * nFieldWidth + px]==0){
nCount++;
pOldY++;
}
}
}
return nCount;
}
int connectedHolesCount(const unsigned char *pField) {
int nCount=0,pOldY;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 1; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]==0 && pField[(py-1) * nFieldWidth + px]!=0){
nCount++;
pOldY=py+1;
}
}
return nCount;
}
int deepestHole(const unsigned char *pField) {
int deepestHolePos=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 1; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]==0 && pField[(py-1) * nFieldWidth + px]!=0 && pField[(py) * nFieldWidth + px+1]!=0 && pField[(py) * nFieldWidth + px-1]!=0)
deepestHolePos=(nFieldHeight-py-1);
}
return deepestHolePos;
}
int sumOfAllHoles(const unsigned char *pField) {
int sumOfAllHoles=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 1; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]==0 && pField[(py-1) * nFieldWidth + px]!=0 && pField[(py) * nFieldWidth + px+1]!=0 && pField[(py) * nFieldWidth + px-1]!=0)
sumOfAllHoles+=(nFieldHeight-py-1);
}
return sumOfAllHoles;
}
int rowTransitions(const unsigned char *pField) {
int nCount=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 1; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]==0 && ((pField[(py+1) * nFieldWidth + px]!=0 && pField[(py+1) * nFieldWidth + px]!=9) || (pField[(py-1) * nFieldWidth + px]!=0 && pField[(py-1) * nFieldWidth + px]!=9)))
nCount++;
}
return nCount;
}
int aggregateHeight(const unsigned char *pField) {
int nHeightCount=0;
for (int px = 1; px < nFieldWidth - 1; px++){
for (int py = 1; py < nFieldHeight - 1; py++)
if(pField[py * nFieldWidth + px]!=0){
nHeightCount+=(nFieldHeight-py-1);
break;
}
}
return nHeightCount;
}
vector <PossibleMoves> getAllPossibleMoves(unsigned char *pField, int nCurrentX, int nCurrentY, int nCurrentRotation, int nCurrentPiece) {
unsigned char *pFieldCopy = nullptr;
pFieldCopy = new unsigned char[nFieldWidth * nFieldHeight];
memcpy(pFieldCopy, pField, 12 * 18);
vector<PossibleMoves> vectorPossibleMoves;
int oldCurrentY=nCurrentY,oldCurrentX=nCurrentX,oldRotation=nCurrentRotation;
bool failRotation=false,failXMove=false;
// Each rotation
for (int rotation = 0; rotation < 4; rotation++){
nCurrentRotation=oldRotation;
nCurrentY = oldCurrentY;
nCurrentX = oldCurrentX;
failRotation = false;
// Test if piece can be rotated
for (int i = 0; i <= rotation; i++)
if (collisionCheck(nCurrentPiece, nCurrentRotation + i, nCurrentX, nCurrentY, pFieldCopy))
nCurrentRotation += i;
else {
failRotation = true;
break;
}
// Each position
for (int position = -5; position < 5; position++) {
if(!failRotation){
// Restore stuff
memcpy(pFieldCopy, pField, 12 * 18);
nCurrentY = oldCurrentY;
nCurrentX = oldCurrentX;
failXMove=false;
// Test if piece can be moved in X
if(position>0){
for(int i=0;i<position; i++)
if(collisionCheck(nCurrentPiece, nCurrentRotation, nCurrentX + 1, nCurrentY, pFieldCopy))
nCurrentX += 1;
else{
failXMove=true;
break;
}
}else{
for(int i=0;i>position; i--)
if(collisionCheck(nCurrentPiece, nCurrentRotation, nCurrentX - 1, nCurrentY, pFieldCopy))
nCurrentX -= 1;
else{
failXMove=true;
break;
}
}
if (!failXMove) {
// Move piece to the bottom of the field
while (collisionCheck(nCurrentPiece, nCurrentRotation, nCurrentX, nCurrentY + 1, pFieldCopy))
nCurrentY++;
// Lock the piece in place
for (int px = 0; px < 4; px++)
for (int py = 0; py < 4; py++)
if (tetromino[nCurrentPiece][rotate(px, py, nCurrentRotation)] != L'.')
pFieldCopy[(nCurrentY + py) * nFieldWidth + (nCurrentX + px)] = nCurrentPiece + 1;
double rating = 0;
Scores thisScore;
thisScore.filledSpotCount = filledSpotCount(pFieldCopy);
thisScore.weightedFilledSpotCount = weightedFilledSpotCount(pFieldCopy);
thisScore.maximumAltitude = maximumAltitude(pFieldCopy);
thisScore.holeCount = holeCount(pFieldCopy);
thisScore.connectedHolesCount = connectedHolesCount(pFieldCopy);
thisScore.maximumWellDepth = maximumWellDepth(pFieldCopy);
//thisScore.deepestHole = deepestHole(pFieldCopy);
//thisScore.sumOfAllHoles = sumOfAllHoles(pFieldCopy);
thisScore.nLinesCleared = nLinesCleared;
thisScore.rowTransitions = rowTransitions(pFieldCopy);
// thisScore.yPiecePos = nCurrentY;
// thisScore.aggregateHeight = aggregateHeight(pFieldCopy);
rating += genomes.at(currentGenome).filledSpotCount*thisScore.filledSpotCount;
rating += genomes.at(currentGenome).weightedFilledSpotCount*thisScore.weightedFilledSpotCount;
rating += genomes.at(currentGenome).maximumAltitude * thisScore.maximumAltitude;
rating += genomes.at(currentGenome).holeCount * thisScore.holeCount;
rating += genomes.at(currentGenome).connectedHolesCount * thisScore.connectedHolesCount;
rating += genomes.at(currentGenome).maximumWellDepth * thisScore.maximumWellDepth;
//rating += genomes.at(currentGenome).deepestHole*thisScore.deepestHole;
//rating += genomes.at(currentGenome).sumOfAllHoles*thisScore.sumOfAllHoles;
rating += genomes.at(currentGenome).nLinesCleared * thisScore.nLinesCleared;
rating += genomes.at(currentGenome).rowTransitions * thisScore.rowTransitions;
// rating += genomes.at(currentGenome).yPiecePos * thisScore.yPiecePos;
// rating += genomes.at(currentGenome).aggregateHeight * thisScore.aggregateHeight;
if(gameOverCheck(pFieldCopy,nCurrentPiece))
rating-=500;
PossibleMoves thisMove;
thisMove.rating = rating;
thisMove.rotation = nCurrentRotation;
thisMove.xPos = nCurrentX;
thisMove.yPos = nCurrentY;
thisMove.scores = thisScore;
vectorPossibleMoves.push_back(thisMove);
}
}
}
}
delete[] pFieldCopy;
return vectorPossibleMoves;
}
// Creates the initial population of genomes, each with random genes.
void
createInitialPopulation(unsigned char *pField, int &nCurrentX, int &nCurrentY, int &nCurrentRotation, int nCurrentPiece,
int nScore, ofstream &outputFile) {
uniform_real_distribution<double> range(-45000.0, 45000.0);
Genome thisGenome;
// For a given population size
for (int i = 0; i < populationSize+1; i++) {
// Randomly initialize the values that make up a genome
// These are all weight values that are updated through evolution
thisGenome.id=range(rng);
thisGenome.fitness=-1;
thisGenome.filledSpotCount=range(rng);
thisGenome.weightedFilledSpotCount=range(rng);
thisGenome.maximumAltitude=range(rng);
thisGenome.holeCount=range(rng);
thisGenome.connectedHolesCount=range(rng);
thisGenome.maximumWellDepth=range(rng);
//thisGenome.deepestHole=range(rng);
//thisGenome.sumOfAllHoles=range(rng);
thisGenome.nLinesCleared=range(rng);
thisGenome.rowTransitions=range(rng);
// thisGenome.yPiecePos=range(rng);
// thisGenome.aggregateHeight=range(rng);
genomes.push_back(thisGenome);
}
evaluateNext(pField, nCurrentX, nCurrentY, nCurrentRotation, nCurrentPiece, nScore, outputFile);
}
void makeNextMove(unsigned char *pField, int &nCurrentX, int &nCurrentY, int &nCurrentRotation, int nCurrentPiece, int nScore) {
movesTaken++;
double highestRating=-DBL_MAX,highestMove=0;
// Get all the possible moves
vector<PossibleMoves> possibleMoves = getAllPossibleMoves(pField,nCurrentX,nCurrentY,nCurrentRotation,nCurrentPiece);
// Get best move rated by this genome
for (int i = 0; i < possibleMoves.size(); i++) {
if(possibleMoves.at(i).rating>highestRating) {
highestRating = possibleMoves.at(i).rating;
highestMove=i;
}
}
// Do move
PossibleMoves bestMove = possibleMoves.at(highestMove);
nCurrentRotation=bestMove.rotation;
nCurrentX=bestMove.xPos;
nCurrentY=bestMove.yPos;
}
void evaluateNext(unsigned char *pField, int &nCurrentX, int &nCurrentY, int &nCurrentRotation, int nCurrentPiece,
int nScore, ofstream &outputFile) {
nTimesPlayed++;
genomes.at(currentGenome).fitness+=nScore;
if(nTimesPlayed == maxTimePlayed){
genomes.at(currentGenome).fitness/=nTimesPlayed;
nTimesPlayed=0;
currentGenome++;
if (currentGenome == genomes.size()){
outputFile.open("output.csv", ios::out | ios::in | ios::app);
outputFile << bestNLines;
outputFile << ";";
outputFile.close();
evolve();
bestNLines=0;
}
}
// Reset moves taken
movesTaken = 0;
// And make the next move
makeNextMove(pField,nCurrentX,nCurrentY,nCurrentRotation,nCurrentPiece,nScore);
}
void evolve() {
currentGenome = 0;
generation++;
bGameOver=true;
// Sort genomes by fitness
for (int i = 0; i < genomes.size()-1; i++)
for (int j = 0; j < genomes.size()-i-1; j++)
if (genomes.at(j).fitness < genomes.at(j+1).fitness){
Genome temp = genomes.at(j);
genomes.at(j) = genomes.at(j+1);
genomes.at(j+1) = temp;
}
// Add a copy of the fittest genome to the nobles list and sort it
noble.push_back(genomes.at(0));
if(noble.size()>1){
for (int i = 0; i < noble.size()-1; i++)
for (int j = 0; j < noble.size()-i-1; j++)
if(noble.size()>1)
if (noble.at(j).fitness < noble.at(j+1).fitness){
Genome temp = noble.at(j);
noble.at(j) = noble.at(j+1);
noble.at(j+1) = temp;
}
if(noble.size()>5)
noble.pop_back();
}
// Kill the worst of the population
while(genomes.size() > populationSize/2)
genomes.pop_back();
// Create children array
vector<Genome> children;
uniform_int_distribution<int> rangeChild(0, genomes.size()-1);
for(int i=0;i<populationSize/4;i++) {
children.push_back(makeChild(genomes.at(rangeChild(rng)), genomes.at(rangeChild(rng))));
children.push_back(makeChild(genomes.at(rangeChild(rng)), genomes.at(0)));
}
// Add nobles to genomes array
for(Genome n : noble){
n.fitness=-1;
genomes.pop_back();
genomes.push_back(n);
}
// Add children to genomes array
for(Genome child : children)
genomes.push_back(child);
}
Genome makeChild(Genome &mum, Genome &dad) {
const unsigned int seed = time(0);
mt19937_64 rng(seed);
uniform_real_distribution<double> rangeCrossingOver(0, 1);
uniform_real_distribution<double> rangeAjustment(0, mutationStep);
Genome child;
double choice;
int choiceRound;
// Weighted
child.maximumAltitude=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.maximumAltitude + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.maximumAltitude;
child.holeCount=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.holeCount + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.holeCount;
child.connectedHolesCount=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.connectedHolesCount + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.connectedHolesCount;
child.maximumWellDepth=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.maximumWellDepth + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.maximumWellDepth;
child.nLinesCleared=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.nLinesCleared + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.nLinesCleared;
child.rowTransitions=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.rowTransitions + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.rowTransitions;
// child.yPiecePos=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.yPiecePos + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.yPiecePos;
// child.aggregateHeight=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.aggregateHeight + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.aggregateHeight;
//child.deepestHole=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.deepestHole + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.deepestHole;
child.weightedFilledSpotCount=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.weightedFilledSpotCount + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.weightedFilledSpotCount;
child.filledSpotCount=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.filledSpotCount + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.filledSpotCount;
//child.sumOfAllHoles=((mum.fitness)/((mum.fitness)+(dad.fitness)))*mum.sumOfAllHoles + ((dad.fitness)/((mum.fitness)+(dad.fitness)))*dad.sumOfAllHoles;
child.fitness=-1;
// We mutate each parameter using our mutationstep
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.maximumAltitude*=rangeAjustment(rng) : child.maximumAltitude/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.connectedHolesCount*=rangeAjustment(rng) : child.connectedHolesCount/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.holeCount*=rangeAjustment(rng) : child.holeCount/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.maximumWellDepth*=rangeAjustment(rng) : child.maximumWellDepth/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.nLinesCleared*=rangeAjustment(rng) : child.nLinesCleared/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.weightedFilledSpotCount*=rangeAjustment(rng) : child.weightedFilledSpotCount/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.filledSpotCount*=rangeAjustment(rng) : child.filledSpotCount/=rangeAjustment(rng);
}
if (rangeCrossingOver(rng) < mutationRate) {
choice=rangeCrossingOver(rng);choiceRound=round(choice);
choiceRound ? child.rowTransitions*=rangeAjustment(rng) : child.rowTransitions/=rangeAjustment(rng);
}
return child;
}