/
asearch.cpp
204 lines (165 loc) · 5.22 KB
/
asearch.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
#include "asearch.h"
#include "state.h"
#include "../utils/debugger.h"
#include "../utils/verbose.h"
#include <vector>
#include <algorithm>
namespace asearch {
ASearch::ASearch() {}
ASearch::~ASearch() {}
//------------------------------------------------------------------------------
// MINIMAX
//------------------------------------------------------------------------------
Move ASearch::Minimax(State *inState, int inMaxDepth)
{
mStatesVisited = 0;
Move best_move = -1;
Score best_score = -INF;
std::vector<Move> moves = inState->GetLegalMoves();
for (int i = 0, n = moves.size(); i < n; i++)
{
mStatesVisited++;
inState->MakeMove(moves[i]);
Score score = -MinimaxValue(inState, inMaxDepth - 1, 2);
inState->UndoMove(moves[i]);
if (score > best_score)
{
best_score = score;
best_move = moves[i];
}
}
DebugLine("Minimax states visited: " << mStatesVisited);
return best_move;
}
Score ASearch::MinimaxValue(State *inState, int inDepth, int inPly)
{
if (inDepth == 0 || inState->IsTerminal())
return inState->GetScore(inPly);
mStatesVisited++;
Score best_score = -INF;
std::vector<Move> moves = inState->GetLegalMoves();
for (int i = 0, n = moves.size(); i < n; i++)
{
inState->MakeMove(moves[i]);
Score score = -MinimaxValue(inState, inDepth - 1, inPly + 1);
inState->UndoMove(moves[i]);
if (score > best_score)
best_score = score;
}
return best_score;
}
//------------------------------------------------------------------------------
// ALPHABETA
//------------------------------------------------------------------------------
Move ASearch::AlphaBeta(State *inState, int inMaxDepth)
{
mStatesVisited = 0;
Move best_move = -1;
Score best_score = -INF;
std::vector<Move> moves = inState->GetLegalMoves();
for (int i = 0, n = moves.size(); i < n; i++)
{
mStatesVisited++;
inState->MakeMove(moves[i]);
Score score = -AlphaBetaValue(inState, 2, inMaxDepth - 1, -INF, -best_score);
inState->UndoMove(moves[i]);
if (score > best_score)
{
best_score = score;
best_move = moves[i];
}
}
DebugLine("AlphaBeta states visited: " << mStatesVisited);
return best_move;
}
Score ASearch::AlphaBetaValue(State *inState, int inPly, int inDepth, Score inAlpha,
Score inBeta)
{
if (inDepth == 0 || inState->IsTerminal())
return inState->Quiescence(inAlpha, inBeta, inPly);
mStatesVisited++;
std::vector<Move> moves = inState->GetLegalMoves();
for (int i = 0, n = moves.size(); i < n; i++)
{
inState->MakeMove(moves[i]);
Score score = -AlphaBetaValue(inState, inPly + 1, inDepth - 1, -inBeta, -inAlpha);
inState->UndoMove(moves[i]);
inAlpha = std::max<Score>(score, inAlpha);
if (inAlpha >= inBeta)
break;
}
return inAlpha;
}
//------------------------------------------------------------------------------
// NEGASCOUT
//------------------------------------------------------------------------------
Move ASearch::Negascout(State *inState, int inMaxDepth)
{
mStatesVisited = 0;
Move best_move = -1;
Score best_score = -INF;
int beta = INF;
std::vector<Move> moves = inState->GetLegalMoves();
for (int i = 0, n = moves.size(); i < n; i++)
{
mStatesVisited++;
inState->MakeMove(moves[i]);
Score val = -NegascoutValue(inState, 2, inMaxDepth - 1, -beta, -best_score);
if (i > 0 && best_score < val)
val = -NegascoutValue(inState, 2, inMaxDepth - 1, -INF, -best_score);
inState->UndoMove(moves[i]);
if (val > best_score)
{
best_score = val;
best_move = moves[i];
}
beta = best_score + 1;
}
DebugLine("Negascout states visited: " << mStatesVisited);
return best_move;
}
/// As defined at: http://en.wikipedia.org/wiki/Negascout
Score ASearch::NegascoutValue(State *inState, int inPly, int inDepth, Score inAlpha,
Score inBeta)
{
if (inDepth == 0 || inState->IsTerminal())
return inState->Quiescence(inAlpha, inBeta, inPly);
mStatesVisited++;
Move move;
Score score;
TTable::Flag flag = inState->Get(inDepth, inPly, move, score);
Score beta = inBeta; // store initial search window
switch (flag)
{
case TTable::EXACT: return score;
case TTable::UPPERBOUND: beta = std::min<Score>(beta, score); break;
case TTable::LOWERBOUND: inAlpha = std::max<Score>(inAlpha, score); break;
default: break;
}
if (inAlpha >= inBeta)
return score;
std::vector<Move> moves = inState->GetLegalMoves();
// If hash found, switch first move
if (flag)
{
std::vector<Move>::iterator i = std::find(moves.begin(), moves.end(), move);
std::swap<Move>(*i, moves[0]);
}
// Apply moves
for (int i = 0, n = moves.size(); i < n; i++)
{
inState->MakeMove(moves[i]);
move = moves[i];
score = -NegascoutValue(inState, inPly + 1, inDepth - 1, -beta, -inAlpha);
if (i > 0 && inAlpha < score && score < inBeta)
score = -NegascoutValue(inState, inPly + 1, inDepth - 1, -inBeta, -inAlpha);
inState->UndoMove(moves[i]);
inAlpha = std::max<int>(inAlpha, score);
if (inAlpha >= inBeta)
break;
beta = inAlpha + 1;
}
inState->Put(inDepth, inPly, inAlpha, beta, move, score);
return inAlpha;
}
} // namespace asearch