-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.cpp
368 lines (331 loc) · 9.75 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
#include "AI.h"
AI::AI(string FEN, color computerColor)
: aiColor(computerColor), nodesVisited(0),
currentAge(0)
{
genChessData data;
data.genMoveData(); // Generates bitboards needed for move generation
board.setupBoard(FEN);
transpositionHash = ZobristHash(static_cast<size_t>(1e7));
}
AI::AI(string FEN, color computerColor, uint hashSize)
: aiColor(computerColor)
{
genChessData data;
data.genMoveData(); // Generates bitboards needed for move generation
board.setupBoard(FEN);
transpositionHash = ZobristHash(hashSize);
}
void AI::printBoard()
{
board.print();
}
/*
string AI::boardToString() const
{
string board = string(64, '_');
int c = 0;
for (auto p : board.pieces) {
for_bits(pos, p) {
board[63 - pos] = names[c];
}
c++;
}
return board;
}
void AI::writeToHistory(const Move& move)
{
gameHistory.push_back(pair<string, Move>(boardToString(), move));
}
*/
void AI::printDebug(string showPieces)
{
board.print();
for (auto p : showPieces){
auto drawPiece = getPieceIndex(p);
///printBitboard(chessBoard.pieces[drawPiece]);
///printBitboard(chessBoard.attacks[drawPiece]);
if (board.pieces[drawPiece])
printBitboardFigAttack(board.pieces[drawPiece], board.attacks[drawPiece], p);
}
}
pair<Move, Move> AI::getBestMove(color forPlayer, int maxDepth, bool uciInfo)
{
Timer timer, infoTimer;
timer.start();
Move bestMove, ponderMove;
vector<Move> pvLine;
for (targetDepth = 1; targetDepth <= maxDepth; targetDepth++) {
infoTimer.start();
NegaMax(-oo, oo, targetDepth, 0, forPlayer);
extractPrincipalVariation(board.hashKey, pvLine, targetDepth, forPlayer);
infoTimer.stop();
if (uciInfo) {
int val = transpositionHash.getValue(board.hashKey);
cout << "info depth " << targetDepth // Search depth
<< " score cp " << val; // Score of computer
if (abs(val) > 10000) {
cout << " mate " << oo - abs(val);
}
cout << " nodes " << nodesVisited // Total visited nodes
<< " nps " << (int)((double)nodesVisited / (infoTimer.getTime()*1e-6)) // Nodes per second
<< " time " << infoTimer.getTime()*1e-3 // Computation time in milliseconds
<< " pv "; // Principal variation of specified depth
for (const auto& move : pvLine) cout << move << ' ';
cout << '\n';
nodesVisited = 0;
}
bestMove = pvLine[0];
if(targetDepth > 1 && pvLine.size() > 1)
ponderMove = pvLine[1];
pvLine.clear();
}
timer.stop();
return pair<Move, Move> (bestMove, ponderMove);
}
int AI::NegaMax(int alpha, int beta, int depth, int ply, color side)
{
// 4Rnk1/pr3ppp/1p3q2/5NQ1/2p5/8/P4PPP/6K1 w - - 1 0
// Consistency check:
// The following FEN should be recognized as Zugzwang. Search should converge after 5 half-moves
// r4r1k/1pqb1B1p/p3p2B/2bpP2Q/8/1NP5/PP4PP/5R1K w - - 1 0
int oldAlpha = alpha;
auto &entry = transpositionHash.getEntry(board.hashKey);
nodesVisited++;
// Check if move was already evaluated
if (entry.terminal == 1) {
// Checkmate / Stalemate
return entry.value;
}
if (entry.search_depth >= depth) {
if (entry.flags & EXACT_VALUE) {
return entry.value;
}
else if (entry.flags & LOWER_BOUND) {
alpha = max(alpha, entry.value);
}
else if (entry.flags & UPPER_BOUND) {
beta = min(beta, entry.value);
}
if (alpha >= beta) {
return entry.value;
}
}
if (depth == 0) {
// Evaluate board & reduce Horizon-Effekt
return QuiescenceSearch(alpha, beta, side);
}
MoveList movelist;
board.updateAllAttacks();
board.generateMoveList<ALL>(movelist, side);
sortMoves(movelist, side);
bool checkedOnThisDepth = board.wasInCheck;
U64 pinnedOnThisDepth = board.pinned;
int legalMoves = 0, score = 0;
Move bestMove;
// Get first best move (estimate)
int visited = 0;
MoveList::iterator move = movelist.begin();
for(auto& move : movelist){
board.makeMove<FULL>(move, side);
if (board.isKingLeftInCheck(side, move, checkedOnThisDepth, pinnedOnThisDepth)) {
board.unMakeMove<FULL>(move, side);
// Get next best move (estimate)
continue;
}
legalMoves++;
if (legalMoves > 4) {
score = -NegaMax(-beta, -alpha, min(4, depth - 1), ply + 1, !side);
}
else {
score = -NegaMax(-beta, -alpha, depth - 1, ply + 1, !side);
}
board.unMakeMove<FULL>(move, side);
if (score > alpha) {
if (score >= beta) {
return beta; // Beta Cutoff
}
alpha = score;
bestMove = move;
}
}
if (legalMoves == 0) {
// No legal moves found.
board.updateAllAttacks();
if ((side == white && board.pieces[wk] & board.blackAtt)
|| (side == black && board.pieces[bk] & board.whiteAtt)) {
// Checkmate, end of game path
entry.search_depth = depth;
entry.terminal = true;
score = -oo + ply;
return score;
}
else {
// Stalemate, end of game path
entry.search_depth = depth;
entry.terminal = true;
score = 0;
return 0;
}
}
if (alpha > oldAlpha) {
// Better move found
pvTable.addPVMove(board.hashKey, bestMove);
entry.value = alpha;
entry.search_depth = depth;
}
if (alpha < oldAlpha) {
entry.flags = UPPER_BOUND;
}
else if (alpha >= beta)
entry.flags = LOWER_BOUND;
else entry.flags = EXACT_VALUE;
return alpha;
}
template<bool firstVisit> void AI::getNextMove(MoveList& mlist, MoveList::iterator& current, color side)
{
int bestValue = -oo, oldBestValue = -oo;
MoveList::iterator move = current;
for (; move != mlist.end(); ++move) {
oldBestValue = bestValue;
if (firstVisit) {
board.makeMove<HASH>(*move, side);
auto& entry = transpositionHash.getEntry(board.hashKey);
board.unMakeMove<HASH>(*move, side);
if (entry.search_depth != -1) {
// I. PV + Hash moves:
bestValue = max(bestValue, entry.value);
}
else if (move->mtype() == CAPTURE) {
// II. Captures
bestValue = max(bestValue, captureScore[move->movePiece() % 6][move->targetPiece() % 6] - 100000);
}
else if (move->mtype() == PAWN2 || (move->mtype() == MOVE && move->movePiece() % 6 == 0)) {
// III. Pawn pushes
bestValue = max(bestValue, -1000000);
}
if (bestValue != oldBestValue) {
// Improvement
iter_swap(current, move);
}
}
else {
if (move->mtype() == CAPTURE) {
// II. Captures
bestValue = max(bestValue, captureScore[move->movePiece() % 6][move->targetPiece() % 6] - 100000);
}
else if (move->mtype() == PAWN2 || (move->mtype() == MOVE && move->movePiece() % 6 == 0)) {
// III. Pawn pushes
bestValue = max(bestValue, -1000000);
}
if (bestValue != oldBestValue) {
// Improvement
iter_swap(current, move);
}
}
}
}
void inline AI::sortMoves(MoveList& movelist, color side)
{
stable_sort(movelist.begin(), movelist.end(), [&, this](const Move& m1, const Move& m2) {
board.makeMove<HASH>(m1, side);
auto& e1 = transpositionHash.getEntry(board.hashKey);
board.unMakeMove<HASH>(m1, side);
board.makeMove<HASH>(m2, side);
auto& e2 = transpositionHash.getEntry(board.hashKey);
board.unMakeMove<HASH>(m2, side);
int val1 = 0, val2 = 0;
if (e1.search_depth != -1) {
val1 = e1.value;
}
else if(m1.mtype() == CAPTURE){
val1 = captureScore[m1.movePiece() % 6][m1.targetPiece() % 6] - 100000;
}
if (e2.search_depth != -1) {
val2 = e2.value;
}
else if (m2.mtype() == CAPTURE) {
val2 = captureScore[m2.movePiece() % 6][m2.targetPiece() % 6] - 100000;
}
return val1 < val2;
});
}
int AI::QuiescenceSearch(int alpha, int beta, color side)
{
// Plays all (relevant) captures from current position
// Reduces Horizon Effect
nodesVisited++;
int standingPat = board.evaluate(side);
if (standingPat >= beta)
return beta;
if (alpha < standingPat)
alpha = standingPat;
MoveList mlist;
board.updateAllAttacks();
board.generateMoveList<CAPTURES_ONLY>(mlist, side);
// MVA-LLV scheme (search best captures first)
stable_sort(mlist.begin(), mlist.end(), [](const Move& m1, const Move& m2) {
return captureScore[m1.movePiece() % 6][m1.targetPiece() % 6]
> captureScore[m2.movePiece() % 6][m2.targetPiece() % 6];
});
bool checkOnThisDepth = board.wasInCheck;
U64 pinnedOnThisDepth = board.pinned;
for (const auto& capture : mlist) {
board.makeMove<FULL>(capture, side);
if (board.isKingLeftInCheck(side, capture, checkOnThisDepth, pinnedOnThisDepth)) {
board.unMakeMove<FULL>(capture, side);
continue;
}
int score = -QuiescenceSearch(-beta, -alpha, !side);
board.unMakeMove<FULL>(capture, side);
if (score >= beta)
return beta;
if (score > alpha)
alpha = score;
}
return alpha;
}
void AI::extractPrincipalVariation(const U64& key, vector<Move>& pvLine, int maxPrintDepth, color side)
{
const auto& entry = pvTable[key];
if (maxPrintDepth == 0 || entry.bestmove.invalid()) return;
board.makeMove<FULL>(entry.bestmove, side);
pvLine.push_back(entry.bestmove);
extractPrincipalVariation(board.hashKey, pvLine, maxPrintDepth - 1, !side);
board.unMakeMove<FULL>(entry.bestmove, side);
}
void AI::reset()
{
// Time consuming. Only call when ucinewgame is passed to engine
transpositionHash.clear();
pvTable.clear();
board.setupBoard("*");
aiColor = black;
}
void AI::setFen(string fenstring)
{
board.setupBoard(fenstring);
}
void AI::printAscii()
{
board.print();
}
void AI::playStringMoves(const vector<string>& moves, color side)
{
board.playStringMoves(moves, side);
sideToMove = moves.size() % 2 == 0 ? sideToMove : !sideToMove;
}
void AI::resetHash()
{
// Only use when "ucinewgame" is passed
transpositionHash.clear();
pvTable.clear();
}
bool AI::isUserMoveValid(const string& usermove, color side)
{
MoveList possibleMoves;
board.generateMoveList<ALL>(possibleMoves, side);
return any_of(possibleMoves.begin(), possibleMoves.end(), [&, this](const Move& move) {
return shortNotation(move) == usermove;
}) ? true : false;
}