forked from NoobMaster00769/AI-Game-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathids_engine.cpp
More file actions
211 lines (159 loc) · 6.38 KB
/
Copy pathids_engine.cpp
File metadata and controls
211 lines (159 loc) · 6.38 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
#include "ids_engine.h"
#include "movegen.h"
#include "evaluator.h"
#include <chrono>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
static int idsNodeCounter = 0;
static int minimaxIDS(Board& board, int depth, int alpha, int beta, bool isMaximizing) {
idsNodeCounter++;
if (depth == 0) {
return evaluate(board);
}
vector<Move> moves = generateLegalMoves(board);
if (moves.empty()) {
if (board.isInCheck(board.sideToMove)) {
if (isMaximizing) {
return -MATE_SCORE + (100 - depth);
} else {
return MATE_SCORE - (100 - depth);
}
}
return 0;
}
if (board.isDraw()) {
return 0;
}
orderMoves(board, moves);
if (isMaximizing) {
int bestScore = -INF_SCORE;
for (const Move& move : moves) {
board.makeMove(move);
int score = minimaxIDS(board, depth - 1, alpha, beta, false);
board.unmakeMove(move);
if (score > bestScore) bestScore = score;
if (score > alpha) alpha = score;
if (beta <= alpha) break;
}
return bestScore;
} else {
int bestScore = INF_SCORE;
for (const Move& move : moves) {
board.makeMove(move);
int score = minimaxIDS(board, depth - 1, alpha, beta, true);
board.unmakeMove(move);
if (score < bestScore) bestScore = score;
if (score < beta) beta = score;
if (beta <= alpha) break;
}
return bestScore;
}
}
IDSResult findBestMoveIDS(Board& board, int maxDepth) {
IDSResult result;
result.totalNodes = 0;
result.totalTimeMs = 0;
result.score = 0;
result.bestMove = Move();
auto totalStart = chrono::high_resolution_clock::now();
vector<Move> moves = generateLegalMoves(board);
if (moves.empty()) {
return result;
}
bool isMaximizing = (board.sideToMove == WHITE);
Move bestMoveSoFar = moves[0];
// Print IDS header
cout << endl;
cout << " +=========================================================+" << endl;
cout << " | ITERATIVE DEEPENING SEARCH (IDS) |" << endl;
cout << " | Move Ordering Optimization Demo |" << endl;
cout << " +---------+-----------+-----------+-----------+-----------+" << endl;
cout << " | Depth | Nodes | Time(ms) | Best Move | Score |" << endl;
cout << " +---------+-----------+-----------+-----------+-----------+" << endl;
for (int depth = 1; depth <= maxDepth; depth++) {
auto iterStart = chrono::high_resolution_clock::now();
idsNodeCounter = 0;
orderMoves(board, moves);
if (depth > 1) {
for (int i = 0; i < (int)moves.size(); i++) {
if (moves[i] == bestMoveSoFar) {
swap(moves[0], moves[i]);
break;
}
}
}
int bestScore = isMaximizing ? -INF_SCORE : INF_SCORE;
Move bestMove = moves[0];
int alpha = -INF_SCORE;
int beta = INF_SCORE;
for (const Move& move : moves) {
board.makeMove(move);
int score = minimaxIDS(board, depth - 1, alpha, beta, !isMaximizing);
board.unmakeMove(move);
if (isMaximizing) {
if (score > bestScore) {
bestScore = score;
bestMove = move;
}
if (score > alpha) alpha = score;
} else {
if (score < bestScore) {
bestScore = score;
bestMove = move;
}
if (score < beta) beta = score;
}
}
auto iterEnd = chrono::high_resolution_clock::now();
double iterTime = (double)chrono::duration_cast<chrono::milliseconds>(
iterEnd - iterStart).count();
IDSIterationStats stats;
stats.depth = depth;
stats.nodesSearched = idsNodeCounter;
stats.bestMove = bestMove;
stats.score = bestScore;
stats.timeMs = iterTime;
result.iterations.push_back(stats);
result.totalNodes += idsNodeCounter;
bestMoveSoFar = bestMove;
cout << " | " << setw(5) << left << depth << right
<< " | " << setw(9) << idsNodeCounter
<< " | " << setw(9) << fixed << setprecision(0) << iterTime
<< " | " << setw(9) << left << bestMove.toString() << right
<< " | " << setw(9) << fixed << setprecision(1) << (bestScore / 100.0)
<< " |" << endl;
}
auto totalEnd = chrono::high_resolution_clock::now();
result.totalTimeMs = (double)chrono::duration_cast<chrono::milliseconds>(
totalEnd - totalStart).count();
cout << " +---------+-----------+-----------+-----------+-----------+" << endl;
cout << " | Total nodes searched: " << setw(10) << left << result.totalNodes << right
<< " |" << endl;
cout << " | Total time: " << setw(8) << fixed << setprecision(0)
<< result.totalTimeMs << " ms"
<< " |" << endl;
cout << " +---------+-----------+-----------+-----------+-----------+" << endl;
if (result.iterations.size() >= 2) {
cout << endl;
cout << " --- Move Ordering Efficiency ---" << endl;
cout << " IDS reuses the best move from depth (D-1) to order" << endl;
cout << " moves at depth D. Combined with MVV-LVA capture" << endl;
cout << " ordering, this tightens alpha-beta bounds early," << endl;
cout << " pruning large portions of the game tree." << endl;
cout << endl;
for (int i = 1; i < (int)result.iterations.size(); i++) {
double ratio = (result.iterations[i - 1].nodesSearched > 0)
? (double)result.iterations[i].nodesSearched / result.iterations[i - 1].nodesSearched
: 0;
cout << " Depth " << result.iterations[i - 1].depth
<< " -> " << result.iterations[i].depth
<< ": branching factor ~" << fixed << setprecision(1) << ratio
<< "x (ideal ≈ sqrt(b))" << endl;
}
}
result.bestMove = bestMoveSoFar;
result.score = result.iterations.back().score;
return result;
}