-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonteCarloAlgorithm.cpp
180 lines (159 loc) · 7.18 KB
/
MonteCarloAlgorithm.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
#include <stdexcept>
#include <algorithm>
#include "MonteCarloAlgorithm.h"
MonteCarloAlgorithm::MonteCarloAlgorithm(int simulationsThreshold, std::size_t movesToSimulate) :
Algorithm(), simulationsThreshold(simulationsThreshold), movesToSimulate(movesToSimulate),
bestSimulation(nullptr) {
}
const Coords MonteCarloAlgorithm::calculateRedMove() const {
return bestSimulation->getFirstMoveAsCoords();
}
const Coords MonteCarloAlgorithm::calculateBlueMove() const {
return bestSimulation->getFirstMoveAsCoords();
}
const Coords MonteCarloAlgorithm::calculateGreyMove() const {
return bestSimulation->getFirstMoveAsCoords();
}
SlideDirection MonteCarloAlgorithm::calculateSlide() const {
return bestSimulation->getFirstMoveAsSlideDirection();
}
void MonteCarloAlgorithm::ensureValidState() {
if (gameGameProgressPtr->getMoveCounter() >= 2) {
// remove simulation where the first two moves do not equal the last done moves
simulations.remove_if([this](const Simulation& simulation) {
if (simulation.getMovesCount() <= 2) {
return true;
}
switch (gameBoardPtr->getGameRhythmState()) {
case GR_BLUE:
return !simulation.checkFirstTwoMoves(gameGameProgressPtr->getLastDoneMoveAsSlideDirection(),
gameGameProgressPtr->getLastReadMoveAsSlideDirection());
case GR_RED:
return !simulation.checkFirstTwoMoves(gameGameProgressPtr->getLastDoneMoveAsSlideDirection(),
gameGameProgressPtr->getLastReadMoveAsCoords());
case GR_GREY:
case GR_SLIDE1:
return !simulation.checkFirstTwoMoves(gameGameProgressPtr->getLastDoneMoveAsCoords(),
gameGameProgressPtr->getLastReadMoveAsCoords());
case GR_SLIDE2:
return !simulation.checkFirstTwoMoves(gameGameProgressPtr->getLastDoneMoveAsCoords(),
gameGameProgressPtr->getLastReadMoveAsSlideDirection());
default:
return false;
}
});
// remove the first two moves of the remaining simulations since those two have just been executed
for (Simulation& simulation : simulations) {
simulation.removeFirstTwo();
}
// we updated simulations, bestSimulation should update as well
updateBestSimulation();
}
if (shouldSimulateMore()) {
simulate(movesToSimulate);
}
}
void MonteCarloAlgorithm::updateBestSimulation() {
if (distance(simulations.begin(), simulations.end()) == 0) {
bestSimulation = nullptr;
} else {
bestSimulation = &(*max_element(simulations.begin(), simulations.end()));
}
}
bool MonteCarloAlgorithm::shouldSimulateMore() const {
return distance(simulations.begin(), simulations.end()) < simulationsThreshold;
}
void MonteCarloAlgorithm::simulate(std::size_t movesToCalculate) {
while (movesToCalculate > 0) {
movesToCalculate -= simulateGame(movesToCalculate);
}
updateBestSimulation();
}
std::size_t MonteCarloAlgorithm::simulateGame(std::size_t maxMovesInSimulation) {
// A copy for the local board is needed, as it'll be used as a reference in the random functions below.
// now we only use the local(Board/GameState) variables after this, as we don't want to change the 6561Game board.
Board localBoard = *gameBoardPtr;
unsigned short localMoveCounter = gameGameProgressPtr->getMoveCounter();
Simulation simulation;
bool simulationValid = true;
std::size_t gameMaxMoves = Game6561::MAX_MOVES;
std::size_t maxMoveCounter = std::min(maxMovesInSimulation + localMoveCounter, gameMaxMoves);
while (simulationValid && (localMoveCounter < maxMoveCounter)) {
switch (localBoard.getGameRhythmState()) {
case GR_BLUE:
simulationValid = addRandomCoordsToSimulation(simulation, localBoard, BLUE);
break;
case GR_RED:
simulationValid = addRandomCoordsToSimulation(simulation, localBoard, RED);
break;
case GR_GREY:
simulationValid = addRandomCoordsToSimulation(simulation, localBoard, GREY);
break;
case GR_SLIDE1:
case GR_SLIDE2:
simulationValid = addRandomSlideToSimulation(simulation, localBoard);
break;
}
simulation.maxScore = std::max(simulation.maxScore, localBoard.getBoardScore());
++localMoveCounter;
}
if (simulation.getMovesCount() > 0) {
simulations.push_front(simulation);
}
return simulation.getMovesCount();
}
unsigned short MonteCarloAlgorithm::generateRandomNumber(unsigned short max,
unsigned short exclusionList[] /*= nullptr*/,
unsigned short exclusionListLength /*= 0*/) {
unsigned short rnd = randomMonteCarloPolicy.generateRandomNumber(max - exclusionListLength);
for (unsigned short i = 0; i < exclusionListLength; ++i) {
if (rnd >= exclusionList[i]) {
rnd++;
}
}
return rnd;
}
bool MonteCarloAlgorithm::addRandomSlideToSimulation(Simulation& simulation, Board& board) {
unsigned short invalidSlides[4];
unsigned short invalidSlidesLength = 0;
while (invalidSlidesLength < 4) {
unsigned short slideNr = generateRandomNumber(3, invalidSlides, invalidSlidesLength);
SlideDirection slideDirection = static_cast<SlideDirection>(slideNr);
if (board.isSlideValid(slideDirection)) {
board.slide(slideDirection);
simulation.addMove(slideDirection);
return true;
}
invalidSlides[invalidSlidesLength] = slideNr;
++invalidSlidesLength;
std::sort(invalidSlides, invalidSlides + invalidSlidesLength);
}
return false;
}
bool MonteCarloAlgorithm::addRandomCoordsToSimulation(Simulation& simulation, Board& board, PieceColor color) {
unsigned short nonEmptyCoords[16];
unsigned short nonEmptyCoordsLength = getNonEmptyCoords(nonEmptyCoords, board);
if (nonEmptyCoordsLength == 16) {
return false;
}
int rnd = generateRandomNumber(15, nonEmptyCoords, nonEmptyCoordsLength);
Coords coords(static_cast<coord>(rnd & 0x3), static_cast<coord>(rnd >> 2));
simulation.addMove(coords);
board.setPiece(coords, color);
return true;
}
unsigned short MonteCarloAlgorithm::getNonEmptyCoords(unsigned short nonEmptyCoords[], const Board& board) {
unsigned short nonEmptyCoordsLength = 0;
for (coord column = 0; column < 4; ++column) {
for (coord row = 0; row < 4; ++row) {
if (!board.getPiece(row, column).empty()) {
nonEmptyCoords[nonEmptyCoordsLength] = coordsToUShort(row, column);
++nonEmptyCoordsLength;
}
}
}
return nonEmptyCoordsLength;
}
unsigned short MonteCarloAlgorithm::coordsToUShort(coord row, coord column) {
return row + (column << 2);
}