bd_search.c

filter: Statementstmt 97.5% 197 / 202 Branchbran 84.0% 185 / 220 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.5% 382 / 422 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« bd_rules.c ↑ Summary bd_moves.c »
1 /* bd_search.c - choosing a move: an evaluation, and a search bounded in nodes.
2 *
3 * PERL-FREE, as the other bd_*.c files are.
4 *
5 * INTEGER ONLY. A score is an int and nothing here is a float, so the same
6 * position, budget and seed give the same move on every machine and every
7 * compiler.
8 *
9 * THE SEARCH NEVER READS A CLOCK. Its budget is a count of nodes, asked about
10 * every 1,024 of them, and the move it returns is the best move of the last
11 * iteration that FINISHED. A loaded machine therefore plays the move an idle
12 * one plays, and a game against it can be played again.
13 *
14 * THE SEARCH KNOWS HOW A GAME ENDS. It plays its moves into a copy of the game,
15 * so a line that repeats a position for the last time the rule set allows, or
16 * runs into the ply cap, is scored as the draw it is. A search that did not
17 * would walk into drawn positions believing it was winning them.
18 *
19 * It is a plain negamax with alpha-beta, deepened one ply at a time, with a
20 * table of positions already searched and the previous iteration's best move
21 * tried first. Nothing here is clever, and the reason is the use it is put to:
22 * the second thing this file is for, after choosing a move, is measuring
23 * whether the two sides of this game win about as often as each other.
24 */
25  
26 #include <stdlib.h>
27 #include <string.h>
28 #include "bd_abi.h"
29  
30 static const struct bd_abi *T = NULL;
31  
32 /* ---- the weights ---------------------------------------------------------------
33 *
34 * The evaluation is scored FOR THE ATTACKERS and negated for the defenders.
35 * Every term is one int in one struct, so a measurement can vary one and leave
36 * the rest.
37 */
38  
39 12676 static void bd_weights_default(struct bd_weights *w)
40 {
41 12676 ▶ if (!w) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!w) return; 0 12676 12676
42 12676 memset(w, 0, sizeof(*w));
43 12676 w->attacker = 100;
44 12676 w->defender = 160;
45 12676 w->lane_one = 450;
46 12676 w->lane_two = 70;
47 12676 w->freedom = 4;
48 12676 w->corner_guard = 14;
49 12676 w->ring = 22;
50 }
51  
52 14306234 static const struct bd_weights *weights_or_default(const struct bd_weights *w)
53 {
54 static struct bd_weights DEFAULT;
55 static int ready = 0;
56 14306234 ▶ if (w) return w;
Branch: 1 branch
BranchTrueFalseTotal
if (w) return w; 14306234 0 14306234
57 0 ▶ if (!ready) { bd_weights_default(&DEFAULT); ready = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ready) { bd_weights_default(&DEFAULT); ready = 1; } 0 0 0
58 0 return &DEFAULT;
59 }
60  
61 /* ---- the evaluation ------------------------------------------------------------- */
62  
63 static const int ORTHO[4] = { -1, 1, -BD_STRIDE, BD_STRIDE };
64  
65 582752593 static int is_home(int sq, const struct bd_variant *v)
66 {
67 int f, r;
68 582752593 ▶ if ((T->is_corner)(sq)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if ((T->is_corner)(sq)) return 1; 25613204 557139389 582752593
69 557139389 ▶ if (!v->escape_edge) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->escape_edge) return 0; 557139389 0 557139389
70 0 f = (T->file_of)(sq);
71 0 r = (T->rank_of)(sq);
72 0 ▶ return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1;
Branch: 4 branches
BranchTrueFalseTotal
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
73 }
74  
75 /* Where the king could stop if he moved in direction `d` from `from`, written
76 * into `out`. The same walk the move generator makes, for one piece. */
77 479479088 static int king_slide(const struct bd_board *b, const struct bd_variant *v, int from, int d, int *out)
78 {
79 479479088 int sq = from + d, n = 0;
80 1088902707 ▶ while ((T->at)(b, sq) == BD_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
while ((T->at)(b, sq) == BD_EMPTY) { 609423619 479479088 1088902707
81 609423619 ▶ if ((T->is_throne)(sq)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((T->is_throne)(sq)) { 33065358 576358261 609423619
82 33065358 ▶ if (v->throne_reentry) out[n++] = sq;
Branch: 1 branch
BranchTrueFalseTotal
if (v->throne_reentry) out[n++] = sq; 0 33065358 33065358
83 33065358 ▶ if (!v->throne_pass) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!v->throne_pass) break; 0 33065358 33065358
84 }
85 576358261 else out[n++] = sq;
86 609423619 sq += d;
87 }
88 479479088 return n;
89 }
90  
91 /* How many squares the king wins on can he reach in ONE move. The whole of the
92 * tactics of this game hangs on this number: with the defenders to move and it
93 * above zero the game is won, and with the attackers to move it is a threat
94 * that must be answered now. */
95 21821463 static int king_lanes(const struct bd_board *b, const struct bd_variant *v)
96 {
97 21821463 int king = (T->king_square)(b), d, i, n, one = 0;
98 int stops[BD_SIZE];
99 21821463 ▶ if (king < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (king < 0) return 0; 0 21821463 21821463
100 109107315 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 87285852 21821463 109107315
101 87285852 n = king_slide(b, v, king, ORTHO[d], stops);
102 210836241 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 123550389 87285852 210836241
103 123550389 ▶ if (is_home(stops[i], v)) one++;
Branch: 1 branch
BranchTrueFalseTotal
if (is_home(stops[i], v)) one++; 9443166 114107223 123550389
104 }
105 21821463 return one;
106 }
107  
108 /* THE SEVEN TERMS.
109 *
110 * material attackers left, and defenders left, each with its own weight
111 * lane_one squares the king wins on that he can reach in ONE move
112 * lane_two such squares he can reach in two and not in one
113 * freedom how many squares the king can move to at all
114 * corner_guard attackers on the three squares that close a corner
115 * ring attackers standing next to the king
116 *
117 * The first two lanes are what the game is about: a king with a line to a
118 * corner has won unless it is closed at once, and a king with two has won.
119 */
120 14298078 static int bd_evaluate(const struct bd_board *b, const struct bd_variant *v, const struct bd_weights *w)
121 {
122 static const int GUARD[4][3][2] = {
123 { { 0, 2 }, { 1, 1 }, { 2, 0 } },
124 { { 6, 2 }, { 5, 1 }, { 4, 0 } },
125 { { 0, 4 }, { 1, 5 }, { 2, 6 } },
126 { { 6, 4 }, { 5, 5 }, { 4, 6 } }
127 };
128 struct bd_variant dv;
129 int score, king, d, e, i, c;
130 14298078 int one = 0, two = 0, freedom = 0, ring = 0, guards = 0;
131 int home_one[BD_CELLS];
132 int stops[BD_SIZE], n;
133  
134 14298078 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 14298078 14298078
135 14298078 ▶ if (!v) { (T->variant_default)(&dv); v = &dv; }
Branch: 1 branch
BranchTrueFalseTotal
if (!v) { (T->variant_default)(&dv); v = &dv; } 0 14298078 14298078
136 14298078 w = weights_or_default(w);
137  
138 14298078 score = w->attacker * (T->count)(b, BD_ATTACKER) - w->defender * (T->count)(b, BD_DEFENDER);
139  
140 71490390 ▶ for (c = 0; c < 4; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < 4; c++) 57192312 14298078 71490390
141 228769248 ▶ for (i = 0; i < 3; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) 171576936 57192312 228769248
142 171576936 ▶ if ((T->at)(b, (T->square_of)(GUARD[c][i][0], GUARD[c][i][1])) == BD_ATTACKER) guards++;
Branch: 1 branch
BranchTrueFalseTotal
if ((T->at)(b, (T->square_of)(GUARD[c][i][0], GUARD[c][i][1])) == BD_ATTACKER) guards++; 23946459 147630477 171576936
143 14298078 score += w->corner_guard * guards;
144  
145 14298078 king = (T->king_square)(b);
146 14298078 ▶ if (king < 0) return score;
Branch: 1 branch
BranchTrueFalseTotal
if (king < 0) return score; 0 14298078 14298078
147  
148 14298078 memset(home_one, 0, sizeof(home_one));
149 71490390 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 57192312 14298078 71490390
150 57192312 ▶ if ((T->at)(b, king + ORTHO[d]) == BD_ATTACKER) ring++;
Branch: 1 branch
BranchTrueFalseTotal
if ((T->at)(b, king + ORTHO[d]) == BD_ATTACKER) ring++; 7297207 49895105 57192312
151 57192312 n = king_slide(b, v, king, ORTHO[d], stops);
152 57192312 freedom += n;
153 126645369 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 69453057 57192312 126645369
154 69453057 ▶ if (is_home(stops[i], v) && !home_one[stops[i]]) { home_one[stops[i]] = 1; one++; }
Branch: 2 branches
BranchTrueFalseTotal
if (is_home(stops[i], v) && !home_one[stops[i]]) { home_one[stops[i]] = 1; one++; } 904 69452153 69453057
if (is_home(stops[i], v) && !home_one[stops[i]]) { home_one[stops[i]] = 1; one++; } 904 0 904
155 }
156 {
157 int home_two[BD_CELLS];
158 14298078 memset(home_two, 0, sizeof(home_two));
159 71490390 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 57192312 14298078 71490390
160 57192312 n = king_slide(b, v, king, ORTHO[d], stops);
161 126645369 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 69453057 57192312 126645369
162 int reach[BD_SIZE], m, j;
163 69453057 ▶ if (is_home(stops[i], v)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (is_home(stops[i], v)) continue; 904 69452153 69453057
164 347260765 ▶ for (e = 0; e < 4; e++) {
Branch: 1 branch
BranchTrueFalseTotal
for (e = 0; e < 4; e++) { 277808612 69452153 347260765
165 277808612 m = king_slide(b, v, stops[i], ORTHO[e], reach);
166 591710370 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 313901758 277808612 591710370
167 313901758 int sq = reach[j];
168 313901758 ▶ if (sq == king) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (sq == king) continue; 0 313901758 313901758
169 313901758 ▶ if (is_home(sq, v) && !home_one[sq] && !home_two[sq]) { home_two[sq] = 1; two++; }
Branch: 3 branches
BranchTrueFalseTotal
if (is_home(sq, v) && !home_one[sq] && !home_two[sq]) { home_two[sq] = 1; two++; } 16027639 297874119 313901758
if (is_home(sq, v) && !home_one[sq] && !home_two[sq]) { home_two[sq] = 1; two++; } 16026569 1070 16027639
if (is_home(sq, v) && !home_one[sq] && !home_two[sq]) { home_two[sq] = 1; two++; } 14541069 1485500 16026569
170 }
171 }
172 }
173 }
174 }
175  
176 14298078 score += w->ring * ring;
177 14298078 score -= w->lane_one * one;
178 14298078 score -= w->lane_two * two;
179 14298078 score -= w->freedom * freedom;
180 14298078 return score;
181 }
182  
183 /* ---- the table of positions already searched ------------------------------------ */
184  
185 #define TT_EXACT 0
186 #define TT_LOWER 1
187 #define TT_UPPER 2
188  
189 struct tt_entry {
190 unsigned long long key;
191 int move;
192 int score;
193 short depth;
194 unsigned char flag;
195 unsigned char used;
196 };
197  
198 #define BD_INF (BD_WIN_SCORE + 1000)
199  
200 /* how deep one line may run, the extension included */
201 #define BD_PLY_MAX 96
202  
203 struct ctx {
204 struct bd_game *g;
205 struct bd_variant v;
206 const struct bd_weights *w;
207 unsigned long long nodes, budget, seed;
208 int may_stop;
209 int stopped;
210 struct tt_entry *tt;
211 size_t mask;
212 int killer[BD_PLY_MAX][2];
213 };
214  
215  
216 /* The order moves are tried in: the move the table remembers, the king going
217 * home, a move that cut this depth off a moment ago in another line, moves
218 * that land beside an enemy, the king's other moves, the rest. It
219 * changes how many nodes a search costs and never which move a search to a
220 * fixed depth returns, apart from the choice among moves of equal score, which
221 * `salt` settles and a test holds to that. */
222 632695 static unsigned long long mix(unsigned long long z)
223 {
224 632695 z += 0x9E3779B97F4A7C15ULL;
225 632695 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
226 632695 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
227 632695 return z ^ (z >> 31);
228 }
229  
230 2529779 static void order_moves(const struct ctx *c, const struct bd_board *b, int *list, int n, int first, unsigned long long salt, int ply)
231 {
232 int key[BD_MOVES_MAX];
233 2529779 int i, j, side = (T->side)(b);
234  
235 83774040 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 81244261 2529779 83774040
236 81244261 int from = (T->move_from)(list[i]), to = (T->move_to)(list[i]);
237 81244261 int piece = (T->at)(b, from);
238 81244261 int k = 0, d;
239 81244261 ▶ if (list[i] == first) k += 100000;
Branch: 1 branch
BranchTrueFalseTotal
if (list[i] == first) k += 100000; 274932 80969329 81244261
240 81244261 ▶ if (ply >= 0 && ply < BD_PLY_MAX && (list[i] == c->killer[ply][0] || list[i] == c->killer[ply][1])) k += 20000;
Branch: 4 branches
BranchTrueFalseTotal
if (ply >= 0 && ply < BD_PLY_MAX && (list[i] == c->killer[ply][0] || list[i] == c->killer[ply][1])) k += 20000; 80611566 632695 81244261
if (ply >= 0 && ply < BD_PLY_MAX && (list[i] == c->killer[ply][0] || list[i] == c->killer[ply][1])) k += 20000; 80611566 0 80611566
if (ply >= 0 && ply < BD_PLY_MAX && (list[i] == c->killer[ply][0] || list[i] == c->killer[ply][1])) k += 20000; 78574176 2037390 80611566
if (ply >= 0 && ply < BD_PLY_MAX && (list[i] == c->killer[ply][0] || list[i] == c->killer[ply][1])) k += 20000; 1597415 76976761 78574176
241 81244261 ▶ if (piece == BD_KING) k += is_home(to, &c->v) ? 50000 : 300;
Branch: 2 branches
BranchTrueFalseTotal
if (piece == BD_KING) k += is_home(to, &c->v) ? 50000 : 300; 6394332 74849929 81244261
if (piece == BD_KING) k += is_home(to, &c->v) ? 50000 : 300; 140591 6253741 6394332
242 335751705 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 281698801 54052904 335751705
243 281698801 int beside = (T->at)(b, to + ORTHO[d]);
244 281698801 ▶ if (BD_IS_PIECE(beside) && (T->side_of)(beside) != side) { k += 1000; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (BD_IS_PIECE(beside) && (T->side_of)(beside) != side) { k += 1000; break; } 107262747 174436054 281698801
if (BD_IS_PIECE(beside) && (T->side_of)(beside) != side) { k += 1000; break; } 78568851 28693896 107262747
if (BD_IS_PIECE(beside) && (T->side_of)(beside) != side) { k += 1000; break; } 27191357 51377494 78568851
245 }
246 81244261 ▶ if (salt) k += (int) (mix(salt ^ ((unsigned long long) list[i] * 0x100000001B3ULL)) & 0xFF);
Branch: 1 branch
BranchTrueFalseTotal
if (salt) k += (int) (mix(salt ^ ((unsigned long long) list[i] * 0x100000001B3ULL)) & 0xFF); 632695 80611566 81244261
247 81244261 key[i] = k;
248 }
249 81244261 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 78714482 2529779 81244261
250 78714482 int m = list[i], k = key[i];
251 386851338 ▶ for (j = i - 1; j >= 0 && key[j] < k; j--) { list[j + 1] = list[j]; key[j + 1] = key[j]; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = i - 1; j >= 0 && key[j] < k; j--) { list[j + 1] = list[j]; key[j + 1] = key[j]; } 382628512 4222826 386851338
for (j = i - 1; j >= 0 && key[j] < k; j--) { list[j + 1] = list[j]; key[j + 1] = key[j]; } 308136856 74491656 382628512
252 78714482 list[j + 1] = m; key[j + 1] = k;
253 }
254 2529779 }
255  
256 255618 static int score_of_outcome(int outcome, int side, int ply)
257 {
258 255618 int winner = (T->winner_of)(outcome);
259 255618 ▶ if (winner < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (winner < 0) return 0; 23761 231857 255618
260 231857 ▶ return winner == side ? BD_WIN_SCORE - ply : -(BD_WIN_SCORE - ply);
Branch: 1 branch
BranchTrueFalseTotal
return winner == side ? BD_WIN_SCORE - ply : -(BD_WIN_SCORE - ply); 0 231857 231857
261 }
262  
263 /* THE EXTENSION. A search that stops at a fixed depth stops, as often as not,
264 * with the king one move from a corner, and scores that as a quiet position.
265 * So at the horizon the king's lanes are counted first:
266 *
267 * the defenders to move with a lane open he walks out: a win, and exact
268 * the attackers to move with a lane open they must answer it NOW, so the
269 * line is searched one ply further
270 *
271 * IT CANNOT RUN AWAY, and needs no counter to stop it. The one extra ply is
272 * the attackers', and at the end of it the defenders are to move, where the
273 * search never extends: it returns a win if the lane is still open and the
274 * evaluation if it is not. Because the extension depends on the position alone
275 * and not on how the line got there, a position is worth the same by every
276 * road, which is what lets the table of positions be trusted.
277 *
278 * Without it the attackers of this file lost every game they played in
279 * eight moves, to combinations one ply past where they were looking. */
280 24636250 static int negamax(struct ctx *c, int depth, int alpha, int beta, int ply)
281 {
282 24636250 const struct bd_board *b = (T->game_board)(c->g);
283 int list[BD_MOVES_MAX];
284 24636250 int n, i, best = -BD_INF, best_move = 0, first = 0, alpha0 = alpha;
285 24636250 int side = (T->side)(b);
286 int outcome;
287 unsigned long long key;
288 24636250 struct tt_entry *e = NULL;
289  
290 24636250 c->nodes++;
291 24636250 ▶ if (c->may_stop && (c->nodes & 1023ULL) == 0 && c->nodes >= c->budget) c->stopped = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (c->may_stop && (c->nodes & 1023ULL) == 0 && c->nodes >= c->budget) c->stopped = 1; 24273356 362894 24636250
if (c->may_stop && (c->nodes & 1023ULL) == 0 && c->nodes >= c->budget) c->stopped = 1; 23142 24250214 24273356
if (c->may_stop && (c->nodes & 1023ULL) == 0 && c->nodes >= c->budget) c->stopped = 1; 3261 19881 23142
292 24636250 ▶ if (c->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c->stopped) return 0; 3261 24632989 24636250
293  
294 24632989 outcome = (T->game_outcome)(c->g);
295 24632989 ▶ if (outcome != BD_ONGOING) return score_of_outcome(outcome, side, ply);
Branch: 1 branch
BranchTrueFalseTotal
if (outcome != BD_ONGOING) return score_of_outcome(outcome, side, ply); 255618 24377371 24632989
296  
297 24377371 ▶ if (depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) { 21821463 2555908 24377371
298 21821463 int lanes = king_lanes(b, &c->v);
299 21821463 ▶ if (lanes && side == BD_DEFENDERS) return BD_WIN_SCORE - ply - 1;
Branch: 2 branches
BranchTrueFalseTotal
if (lanes && side == BD_DEFENDERS) return BD_WIN_SCORE - ply - 1; 7527901 14293562 21821463
if (lanes && side == BD_DEFENDERS) return BD_WIN_SCORE - ply - 1; 7282896 245005 7527901
300 14538567 ▶ if (lanes && ply < BD_PLY_MAX - 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (lanes && ply < BD_PLY_MAX - 2) { 245005 14293562 14538567
if (lanes && ply < BD_PLY_MAX - 2) { 245005 0 245005
301 245005 depth = 1;
302 }
303 else {
304 14293562 int s = bd_evaluate(b, &c->v, c->w);
305 14293562 ▶ return side == BD_ATTACKERS ? s : -s;
Branch: 1 branch
BranchTrueFalseTotal
return side == BD_ATTACKERS ? s : -s; 10200605 4092957 14293562
306 }
307 }
308  
309 /* game_push left "can the side to move, move?" unasked, so it is asked
310 * here, by the list this node needs anyway */
311 2800913 n = (T->gen_moves)(b, &c->v, list, BD_MOVES_MAX);
312 2800913 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 0 2800913 2800913
313 2800913 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 2800913 2800913
314  
315 2800913 key = (T->key)(b);
316 2800913 ▶ if (c->tt) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tt) { 2800913 0 2800913
317 2800913 e = &c->tt[(size_t) key & c->mask];
318 2800913 ▶ if (e->used && e->key == key) {
Branch: 2 branches
BranchTrueFalseTotal
if (e->used && e->key == key) { 627643 2173270 2800913
if (e->used && e->key == key) { 554220 73423 627643
319 554220 first = e->move;
320 554220 ▶ if (e->depth >= depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->depth >= depth) { 299261 254959 554220
321 299261 ▶ if (e->flag == TT_EXACT) return e->score;
Branch: 1 branch
BranchTrueFalseTotal
if (e->flag == TT_EXACT) return e->score; 6869 292392 299261
322 292392 ▶ if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score;
Branch: 2 branches
BranchTrueFalseTotal
if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score; 286547 5845 292392
if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score; 285554 993 286547
323 292392 ▶ if (e->flag == TT_UPPER && e->score < beta) beta = e->score;
Branch: 2 branches
BranchTrueFalseTotal
if (e->flag == TT_UPPER && e->score < beta) beta = e->score; 5845 286547 292392
if (e->flag == TT_UPPER && e->score < beta) beta = e->score; 5277 568 5845
324 292392 ▶ if (alpha >= beta) return e->score;
Branch: 1 branch
BranchTrueFalseTotal
if (alpha >= beta) return e->score; 282923 9469 292392
325 }
326 }
327 }
328  
329 2511121 order_moves(c, b, list, n, first, 0, ply);
330  
331 24634831 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 24076267 558564 24634831
332 int s;
333 24076267 (T->game_push)(c->g, list[i], NULL);
334 24076267 s = -negamax(c, depth - 1, -beta, -alpha, ply + 1);
335 24076267 (T->game_undo)(c->g);
336 24076267 ▶ if (c->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c->stopped) return 0; 7937 24068330 24076267
337 24068330 ▶ if (s > best) { best = s; best_move = list[i]; }
Branch: 1 branch
BranchTrueFalseTotal
if (s > best) { best = s; best_move = list[i]; } 3576352 20491978 24068330
338 24068330 ▶ if (best > alpha) alpha = best;
Branch: 1 branch
BranchTrueFalseTotal
if (best > alpha) alpha = best; 2444467 21623863 24068330
339 24068330 ▶ if (alpha >= beta) {
Branch: 1 branch
BranchTrueFalseTotal
if (alpha >= beta) { 1944620 22123710 24068330
340 1944620 ▶ if (ply < BD_PLY_MAX && c->killer[ply][0] != list[i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ply < BD_PLY_MAX && c->killer[ply][0] != list[i]) { 1944620 0 1944620
if (ply < BD_PLY_MAX && c->killer[ply][0] != list[i]) { 544874 1399746 1944620
341 544874 c->killer[ply][1] = c->killer[ply][0];
342 544874 c->killer[ply][0] = list[i];
343 }
344 1944620 break;
345 }
346 }
347  
348 /* A score that is a win or a loss carries the distance from the ROOT, which
349 * is the wrong distance for the same position met by another road, so those
350 * are left out of the table. */
351 2503184 ▶ if (e && best > -(BD_WIN_SCORE - 1000) && best < BD_WIN_SCORE - 1000) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && best > -(BD_WIN_SCORE - 1000) && best < BD_WIN_SCORE - 1000) { 2503184 0 2503184
if (e && best > -(BD_WIN_SCORE - 1000) && best < BD_WIN_SCORE - 1000) { 2306526 196658 2503184
if (e && best > -(BD_WIN_SCORE - 1000) && best < BD_WIN_SCORE - 1000) { 1930658 375868 2306526
352 1930658 e->used = 1;
353 1930658 e->key = key;
354 1930658 e->move = best_move;
355 1930658 e->score = best;
356 1930658 e->depth = (short) depth;
357 1930658 ▶ e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER : best >= beta ? TT_LOWER : TT_EXACT);
Branch: 2 branches
BranchTrueFalseTotal
e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER : best >= beta ? TT_LOWER : TT_EXACT); 1677454 253204 1930658
e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER : best >= beta ? TT_LOWER : TT_EXACT); 1567528 109926 1677454
358 }
359 2503184 return best;
360 }
361  
362 #define BD_DEPTH_MAX 60
363  
364 /* THE MOVE RETURNED IS THE BEST OF THE LAST ITERATION THAT FINISHED. An
365 * iteration cut short by the budget has looked at some of the moves and not
366 * others, and its "best so far" is the best of an arbitrary subset.
367 *
368 * The first iteration is never cut short, so there is always a move to
369 * return; it costs one node a legal move and a budget smaller than that is
370 * simply exceeded by it.
371 *
372 * `seed` settles the choice among root moves of equal score, and nothing else.
373 */
374 8156 static int bd_search(const struct bd_game *game, const struct bd_weights *w,
375 unsigned long long budget, unsigned long long seed, int max_depth,
376 struct bd_search_info *info)
377 {
378 struct ctx c;
379 int root[BD_MOVES_MAX];
380 8156 int n, depth, i, best_move = 0, best_score = 0, done = 0;
381 8156 size_t size = 1024;
382  
383 8156 ▶ if (info) memset(info, 0, sizeof(*info));
Branch: 1 branch
BranchTrueFalseTotal
if (info) memset(info, 0, sizeof(*info)); 8156 0 8156
384 8156 ▶ if (!game) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!game) return 0; 0 8156 8156
385  
386 8156 memset(&c, 0, sizeof(c));
387 8156 c.g = (T->game_copy)(game);
388 8156 ▶ if (!c.g) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c.g) return 0; 0 8156 8156
389 8156 (T->game_variant)(c.g, &c.v);
390 8156 c.w = weights_or_default(w);
391 8156 c.budget = budget;
392 8156 ▶ c.seed = seed ? seed : 1;
Branch: 1 branch
BranchTrueFalseTotal
c.seed = seed ? seed : 1; 8155 1 8156
393  
394 8156 n = (T->game_moves)(c.g, root, BD_MOVES_MAX);
395 8156 ▶ if (n <= 0) { (T->game_drop)(c.g); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { (T->game_drop)(c.g); return 0; } 2 8154 8156
396 8154 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 8154 8154
397  
398 29753 ▶ while (size < budget && size < ((size_t) 1 << 20)) size <<= 1;
Branch: 2 branches
BranchTrueFalseTotal
while (size < budget && size < ((size_t) 1 << 20)) size <<= 1; 23193 6560 29753
while (size < budget && size < ((size_t) 1 << 20)) size <<= 1; 21599 1594 23193
399 8154 c.tt = (struct tt_entry *) calloc(size, sizeof(struct tt_entry));
400 8154 c.mask = size - 1;
401  
402 8154 ▶ if (max_depth <= 0 || max_depth > BD_DEPTH_MAX) max_depth = BD_DEPTH_MAX;
Branch: 2 branches
BranchTrueFalseTotal
if (max_depth <= 0 || max_depth > BD_DEPTH_MAX) max_depth = BD_DEPTH_MAX; 1594 6560 8154
if (max_depth <= 0 || max_depth > BD_DEPTH_MAX) max_depth = BD_DEPTH_MAX; 0 1594 1594
403  
404 19441 ▶ for (depth = 1; depth <= max_depth; depth++) {
Branch: 1 branch
BranchTrueFalseTotal
for (depth = 1; depth <= max_depth; depth++) { 18658 783 19441
405 18658 int alpha = -BD_INF, beta = BD_INF, it_best = -BD_INF, it_move = 0;
406 18658 const struct bd_board *b = (T->game_board)(c.g);
407  
408 18658 c.may_stop = depth > 1;
409 18658 order_moves(&c, b, root, n, best_move, c.seed, -1);
410  
411 575380 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 559983 15397 575380
412 int s;
413 559983 (T->game_push)(c.g, root[i], NULL);
414 559983 s = -negamax(&c, depth - 1, -beta, -alpha, 1);
415 559983 (T->game_undo)(c.g);
416 559983 ▶ if (c.stopped) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c.stopped) break; 3261 556722 559983
417 556722 ▶ if (s > it_best) { it_best = s; it_move = root[i]; }
Branch: 1 branch
BranchTrueFalseTotal
if (s > it_best) { it_best = s; it_move = root[i]; } 35334 521388 556722
418 556722 ▶ if (it_best > alpha) alpha = it_best;
Branch: 1 branch
BranchTrueFalseTotal
if (it_best > alpha) alpha = it_best; 35334 521388 556722
419 }
420 18658 ▶ if (c.stopped) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c.stopped) break; 3261 15397 18658
421  
422 15397 best_move = it_move;
423 15397 best_score = it_best;
424 15397 done = depth;
425  
426 /* a win or a loss that is forced has been seen to its end, and looking
427 * further would only find the same thing more slowly */
428 15397 ▶ if (best_score > BD_WIN_SCORE - 1000 || best_score < -(BD_WIN_SCORE - 1000)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (best_score > BD_WIN_SCORE - 1000 || best_score < -(BD_WIN_SCORE - 1000)) break; 12503 2894 15397
if (best_score > BD_WIN_SCORE - 1000 || best_score < -(BD_WIN_SCORE - 1000)) break; 11777 726 12503
429 11777 ▶ if (c.nodes >= c.budget) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c.nodes >= c.budget) break; 490 11287 11777
430 }
431  
432 8154 ▶ if (info) {
Branch: 1 branch
BranchTrueFalseTotal
if (info) { 8154 0 8154
433 8154 info->nodes = c.nodes;
434 8154 info->depth = done;
435 8154 info->score = best_score;
436 8154 info->stopped = c.stopped;
437 }
438 8154 free(c.tt);
439 8154 (T->game_drop)(c.g);
440 8154 return best_move;
441 }
442  
443 /* ---- installed into the table by bd_engine.c -------------------------------------- */
444  
445 void bd_search_install(struct bd_abi *t);
446  
447 59 void bd_search_install(struct bd_abi *t)
448 {
449 59 T = t;
450 59 t->weights_default = bd_weights_default;
451 59 t->evaluate = bd_evaluate;
452 59 t->search = bd_search;
453 59 }
« bd_rules.c ↑ Summary bd_moves.c »