xq_search.c

filter: Statementstmt 100% 183 / 183 Branchbran 90.9% 160 / 176 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.5% 343 / 359 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xq_moves.c ↑ Summary xq_mate.c »
1 /* xq_search.c - the evaluation and the search. Phase 09 of plan_xiangqi.
2 *
3 * PERL-FREE, as the rest of the engine is.
4 *
5 * ---- FAIL-SOFT, AND THE REASON IS ONE PARAGRAPH --------------------------------
6 *
7 * Game::Oware shipped fail-HARD alpha-beta feeding a tie-break and its bot got
8 * STRICTLY WORSE WITH DEPTH: `_alphabeta` returned `$alpha`, so every child that
9 * failed low came back exactly equal to the best score, the root drew uniformly
10 * from that pool, and deeper search raised alpha sooner and so clamped more
11 * children. More search produced more randomness. Level 2 lost 0-10 to level 1
12 * before anybody suspected the search rather than the evaluation, and what ruled
13 * the evaluation out was sweeping every weight to zero and seeing no change.
14 *
15 * So: fail-soft, and a full window at the root. This is at the top of the file
16 * because the bug is invisible in any single game and looks exactly like a bad
17 * evaluation.
18 *
19 * ---- BOUNDED IN WORK, NEVER IN TIME (D6) ----------------------------------------
20 *
21 * The site plays the bot inside the move transaction. A search bounded by
22 * seconds makes the length of a database transaction a property of how loaded
23 * the box is, and makes a bot game unreplayable, because a loaded machine would
24 * choose a different move from an idle one and the site's whole verification
25 * story goes with it.
26 *
27 * The budget is tested every 1024 nodes. NO alarm, NO ualarm, NO signals:
28 * Chess::Plisco uses them and therefore needs a watcher class on the site to
29 * stop a search at all. This one stops itself, which is why the site's adapter
30 * has no Watcher.pm and says so.
31 *
32 * Spent through ITERATIVE DEEPENING, so running out always leaves a complete
33 * search at a shallower depth rather than half of a deeper one.
34 *
35 * ---- LEGALITY IS LAZY, AND THAT IS WHERE THE SPEED IS ----------------------------
36 *
37 * The first version of this file called `gen_legal` at every node and searched
38 * 90,000 nodes a second, while perft over the same generator runs at 2.26 MILLION.
39 * The difference was not the evaluation, which was measured at 4 microseconds
40 * against a node cost of 11: it was that `gen_legal` plays and unplays all forty
41 * moves to filter them, and a node that takes a beta cutoff on its second move
42 * had paid for thirty-eight it never looked at.
43 *
44 * So the search generates PSEUDO-LEGAL moves and tests legality inside the loop,
45 * after `do_move`, on the moves it actually searches. The test is the same one
46 * `gen_legal` uses and is not a second opinion: own general not attacked, and the
47 * two generals not facing.
48 *
49 * The cost of this is that a node does not know whether it has NO legal move
50 * until the loop ends, so the mate and stalemate score moved to the bottom of the
51 * loop. At a horizon node, where the loop does not run at all, a side that is IN
52 * CHECK is still tested exactly, because `in_check` is a tenth of a microsecond
53 * and a missed mate is the one error the search may not make. A horizon
54 * STALEMATE, which is a loss in this game, is scored as an evaluation instead:
55 * the next iteration of the deepening sees it as an ordinary node and rules on
56 * it, and buying it one iteration earlier would cost a full filter at every leaf.
57 */
58  
59 #include <stdlib.h>
60 #include <string.h>
61 #include "xq_abi.h"
62  
63 5428792533 static const struct xq_abi *E(void) { return xq_abi_table(); }
64  
65 #define AT(b, pt) ((E())->at((b), (pt)))
66  
67 /* ---- the evaluation, four terms, integers only (D5) ----------------------------
68 *
69 * In centi-soldiers. `value_of` is phase 05's table and is used rather than a
70 * second one here, because `is_exchange` needs the same numbers and two tables
71 * would drift.
72 *
73 * THE VALUES, and their source: the conventional table every xiangqi primer
74 * gives, chariot 9, cannon 4.5, horse 4, advisor and elephant 2, soldier 1 and 2
75 * once across the river. Wikipedia's own endgame section qualifies it in ways
76 * the flat numbers cannot: three soldiers on the seventh rank are about a
77 * chariot, "in the opening and the middlegame ... the cannon is stronger since
78 * platforms are plentiful and the horse is often blocked", and five soldiers on
79 * the tenth rank cannot force mate at all. A phase-tapered table is the obvious
80 * later improvement and this is not it.
81 */
82  
83 #define MOBILITY_WEIGHT 2
84 #define ADVANCE_WEIGHT 4
85 #define OPEN_FILE_PENALTY 30
86 #define HOME_GUARD_BONUS 6
87  
88 /* =head2 material
89 *
90 * The term that dominates, and the only one that is not a heuristic. A soldier
91 * doubles once it is across the river, which is the one positional fact the
92 * material term carries itself because it is a rule rather than a judgement.
93 */
94 5023033 static int term_material(const struct xq_board *b, int side)
95 {
96 5023033 int pt, score = 0;
97 668063389 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 663040356 5023033 668063389
98 int p, v;
99 663040356 ▶ if (!(E())->on_board(pt)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->on_board(pt)) continue; 210967386 452072970 663040356
100 452072970 p = AT(b, pt);
101 452072970 ▶ if (!XQ_IS_PIECE(p)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!XQ_IS_PIECE(p)) continue; 130581969 321491001 452072970
if (!XQ_IS_PIECE(p)) continue; 0 130581969 130581969
102 130581969 v = (E())->value_of(p);
103 130581969 ▶ if (XQ_KIND(p) == XQ_SOLDIER && (E())->crossed_river(pt, XQ_COLOUR(p)))
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_KIND(p) == XQ_SOLDIER && (E())->crossed_river(pt, XQ_COLOUR(p))) 34447369 96134600 130581969
if (XQ_KIND(p) == XQ_SOLDIER && (E())->crossed_river(pt, XQ_COLOUR(p))) 2059507 32387862 34447369
104 2059507 v *= 2;
105 130581969 ▶ score += (XQ_COLOUR(p) == side) ? v : -v;
Branch: 1 branch
BranchTrueFalseTotal
score += (XQ_COLOUR(p) == side) ? v : -v; 65204832 65377137 130581969
106 }
107 5023033 return score;
108 }
109  
110 /* =head2 mobility
111 *
112 * Doing more work in this game than in chess, and that is why it is here at all.
113 * A horse with three legs blocked is worth nearly nothing and the material term
114 * cannot see it; a cannon with no screen in front of it is a chariot that cannot
115 * take. Counted as pseudo-legal moves, because the legality filter costs a do
116 * and an undo per move and the difference is noise at this weight.
117 */
118 5023033 static int term_mobility(struct xq_board *b, int side)
119 {
120 int buf[XQ_MAX_MOVES];
121 5023033 int keep = (E())->side(b);
122 int mine, theirs;
123  
124 5023033 (E())->set_side(b, side);
125 5023033 mine = (E())->gen_moves(b, buf);
126 5023033 ▶ (E())->set_side(b, XQ_OTHER(side));
Branch: 1 branch
BranchTrueFalseTotal
(E())->set_side(b, XQ_OTHER(side)); 2791530 2231503 5023033
127 5023033 theirs = (E())->gen_moves(b, buf);
128 5023033 (E())->set_side(b, keep);
129 5023033 return (mine - theirs) * MOBILITY_WEIGHT;
130 }
131  
132 /* =head2 the general's safety
133 *
134 * The palace's own term, and THE OPEN FILE IS THE ONE TO GET RIGHT because of
135 * the flying general: a general on a file with nothing in front of it is a
136 * general that can be answered by the enemy general alone, and every chariot and
137 * cannon on that file is worth more than its square suggests.
138 *
139 * Its advisors and elephants still at home are the other half, because they are
140 * the only pieces that can defend inside the palace at all.
141 */
142 10046066 static int term_general(const struct xq_board *b, int side)
143 {
144 10046066 int g = (E())->find(b, side | XQ_GENERAL);
145 10046066 int score = 0, pt, file, open = 1;
146  
147 10046066 ▶ if (!g) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return 0; 0 10046066 10046066
148 10046066 file = (E())->file_of(g);
149  
150 1336126778 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 1326080712 10046066 1336126778
151 int p;
152 1326080712 ▶ if (!(E())->on_board(pt)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->on_board(pt)) continue; 421934772 904145940 1326080712
153 904145940 p = AT(b, pt);
154 904145940 ▶ if (XQ_COLOUR(p) != side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(p) != side) continue; 773563971 130581969 904145940
155 130581969 ▶ if ((XQ_KIND(p) == XQ_ADVISOR || XQ_KIND(p) == XQ_ELEPHANT)
Branch: 2 branches
BranchTrueFalseTotal
if ((XQ_KIND(p) == XQ_ADVISOR || XQ_KIND(p) == XQ_ELEPHANT) 111189640 19392329 130581969
if ((XQ_KIND(p) == XQ_ADVISOR || XQ_KIND(p) == XQ_ELEPHANT) 19070222 92119418 111189640
156 38462551 ▶ && !(E())->crossed_river(pt, side))
Branch: 1 branch
BranchTrueFalseTotal
&& !(E())->crossed_river(pt, side)) 38462551 0 38462551
157 38462551 score += HOME_GUARD_BONUS;
158 130581969 ▶ if (pt != g && (E())->file_of(pt) == file) open = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt != g && (E())->file_of(pt) == file) open = 0; 120535903 10046066 130581969
if (pt != g && (E())->file_of(pt) == file) open = 0; 10086171 110449732 120535903
159 }
160 10046066 ▶ if (open) score -= OPEN_FILE_PENALTY;
Branch: 1 branch
BranchTrueFalseTotal
if (open) score -= OPEN_FILE_PENALTY; 2624626 7421440 10046066
161 10046066 return score;
162 }
163  
164 /* =head2 soldier advance
165 *
166 * Counted BEYOND the crossing only, because the crossing itself is already in
167 * the material term and counting it twice would make a soldier stepping over
168 * the river look like winning a piece.
169 */
170 5023033 static int term_advance(const struct xq_board *b, int side)
171 {
172 5023033 int pt, score = 0;
173 668063389 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 663040356 5023033 668063389
174 int p, r;
175 663040356 ▶ if (!(E())->on_board(pt)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->on_board(pt)) continue; 210967386 452072970 663040356
176 452072970 p = AT(b, pt);
177 452072970 ▶ if (XQ_KIND(p) != XQ_SOLDIER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_KIND(p) != XQ_SOLDIER) continue; 417625601 34447369 452072970
178 34447369 ▶ if (!(E())->crossed_river(pt, XQ_COLOUR(p))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->crossed_river(pt, XQ_COLOUR(p))) continue; 32387862 2059507 34447369
179 2059507 r = (E())->rank_of(pt);
180 2059507 ▶ r = (XQ_COLOUR(p) == XQ_RED) ? r - 5 : 4 - r;
Branch: 1 branch
BranchTrueFalseTotal
r = (XQ_COLOUR(p) == XQ_RED) ? r - 5 : 4 - r; 51047 2008460 2059507
181 2059507 ▶ score += (XQ_COLOUR(p) == side) ? r * ADVANCE_WEIGHT : -r * ADVANCE_WEIGHT;
Branch: 1 branch
BranchTrueFalseTotal
score += (XQ_COLOUR(p) == side) ? r * ADVANCE_WEIGHT : -r * ADVANCE_WEIGHT; 832433 1227074 2059507
182 }
183 5023033 return score;
184 }
185  
186 5023033 static int xq_evaluate(struct xq_board *b)
187 {
188 5023033 int side = (E())->side(b);
189 5023033 return term_material(b, side)
190 5023033 + term_mobility(b, side)
191 5023033 ▶ + term_general(b, side) - term_general(b, XQ_OTHER(side))
Branch: 1 branch
BranchTrueFalseTotal
+ term_general(b, side) - term_general(b, XQ_OTHER(side)) 2791530 2231503 5023033
192 5023033 + term_advance(b, side);
193 }
194  
195 /* ---- the transposition table ------------------------------------------------------
196 *
197 * ALLOCATED PER SEARCH AND FREED AFTER IT. Chess/Bot.pm measured Plisco's at
198 * about 2.5 times its megabytes in RSS, which on a pre-forked web worker is the
199 * kind of thing that is paid once per worker and never given back.
200 */
201 #define TT_BITS 16
202 #define TT_SIZE (1 << TT_BITS)
203  
204 #define TT_EXACT 1
205 #define TT_LOWER 2
206 #define TT_UPPER 3
207  
208 struct tt_entry {
209 unsigned long long key;
210 int move;
211 short score;
212 signed char depth;
213 unsigned char flag;
214 };
215  
216 struct search {
217 unsigned long long nodes;
218 unsigned long long budget;
219 int stopped;
220 struct tt_entry *tt;
221 unsigned int seed;
222 };
223  
224 /* ---- move ordering ------------------------------------------------------------------
225 *
226 * The transposition move first, then captures by what they take. No killers and
227 * no history in this version, deliberately: they are cheap to add later and
228 * their ABSENCE is easy to measure, where their presence hides how much the rest
229 * of the ordering is doing.
230 */
231 2819983 static void order(struct xq_board *b, int *moves, int n, int tt_move)
232 {
233 int score[XQ_MAX_MOVES];
234 int i, j;
235 103380405 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 100560422 2819983 103380405
236 100560422 int victim = AT(b, (E())->move_to(moves[i]));
237 100560422 int mover = AT(b, (E())->move_from(moves[i]));
238 100560422 score[i] = (moves[i] == tt_move) ? 1000000
239 205620667 ▶ : XQ_IS_PIECE(victim)
Branch: 2 branches
BranchTrueFalseTotal
: XQ_IS_PIECE(victim) 100496561 63861 100560422
: XQ_IS_PIECE(victim) 4563684 0 4563684
240 4563684 ? 1000 + (E())->value_of(victim) - (E())->value_of(mover) / 10
241 105060245 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 4563684 95932877 100496561
242 }
243 100560422 ▶ for (i = 1; i < n; i++) { /* insertion sort: n is small and it is stable */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { /* insertion sort: n is small and it is stable */ 97740439 2819983 100560422
244 97740439 int m = moves[i], sc = score[i];
245 185926861 ▶ for (j = i - 1; j >= 0 && score[j] < sc; j--) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = i - 1; j >= 0 && score[j] < sc; j--) { 182923816 3003045 185926861
for (j = i - 1; j >= 0 && score[j] < sc; j--) { 88186422 94737394 182923816
246 88186422 moves[j + 1] = moves[j];
247 88186422 score[j + 1] = score[j];
248 }
249 97740439 moves[j + 1] = m;
250 97740439 score[j + 1] = sc;
251 }
252 2819983 }
253  
254 /* The legality test, AFTER a move has been played, for the side that played it.
255 * It is `gen_legal`'s own test and not a second opinion: if these two ever
256 * disagreed the search would search moves the facade refuses, and the bot would
257 * offer the site a move the site rejects.
258 *
259 * AND IT IS ONE CALL, NOT TWO. This read `!in_check(b, us) && !generals_face(b)`
260 * for most of phase 09, mirroring `gen_legal` line for line, and the second half
261 * was DEAD: `in_check` already carries the flying-general rule, and returns true
262 * for BOTH sides when the two generals see each other down an open file. Note that
263 * `attacked` does not, which is why the redundancy was not obvious from reading it.
264 *
265 * Found by a mutation that dropped the second call and which NOTHING caught, at
266 * which point the question stopped being "where is the missing test" and became
267 * "is this a mutation at all". It was not. Proved by measurement and not by
268 * argument: with the call and without it, a 400,000-node search explores
269 * 400,384 nodes either way, reaches the same depth, returns the same move, and
270 * twenty self-played moves are identical. Removing it is 5.5% faster, which at one
271 * call per searched node is worth having.
272 *
273 * `gen_legal` in xq_moves.c keeps its explicit call. It is the perft path and the
274 * oracle runs through it, so it is not being touched for 5.5% on a different
275 * function. */
276 5829507 static int legal_after(struct xq_board *b, int us)
277 {
278 5829507 return !(E())->in_check(b, us);
279 }
280  
281 /* THE ROOT LIST IS SHUFFLED BY THE SEED, ONCE, BEFORE ANYTHING IS SEARCHED.
282 *
283 * This is the whole tie-break. Only a STRICT improvement takes the move at the
284 * root, so the move that wins is the first one to reach the best score, and a
285 * random starting order makes "first to reach it" a uniform draw among the moves
286 * that genuinely tie. It costs no nodes at all, where scoring every root move
287 * exactly costs a ply.
288 *
289 * THE SEAT IS IN THE SEED and the caller puts it there. Without it both bots in a
290 * bot-versus-bot game shuffle identically, open identically every time, and a
291 * soak measures nothing: Game::Goofspiel shipped exactly that.
292 *
293 * splitmix32 rather than rand(): the engine is deterministic given its seed, and
294 * a bot game that cannot be replayed move for move breaks the site's whole
295 * verification story.
296 */
297 615 static void shuffle(int *moves, int n, unsigned int seed)
298 {
299 int i;
300 615 ▶ unsigned int x = seed ? seed : 0x9E3779B9u;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int x = seed ? seed : 0x9E3779B9u; 585 30 615
301 22400 ▶ for (i = n - 1; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i > 0; i--) { 21785 615 22400
302 unsigned int z;
303 int j, t;
304 21785 x += 0x9E3779B9u;
305 21785 z = x;
306 21785 z = (z ^ (z >> 16)) * 0x21F0AAADu;
307 21785 z = (z ^ (z >> 15)) * 0x735A2D97u;
308 21785 z = z ^ (z >> 15);
309 21785 j = (int) (z % (unsigned int) (i + 1));
310 21785 t = moves[i]; moves[i] = moves[j]; moves[j] = t;
311 }
312 615 }
313  
314 /* THE BUDGET CHECK IS NEVER DISABLED, NOT EVEN FOR THE FIRST ITERATION, and that
315 * is a fact learned the hard way. `quiesce` is bounded by NOTHING ELSE: it
316 * searches captures, and captures do reduce material, but the branching is wide
317 * enough that a tactical position explodes. An attempt to guarantee that the
318 * first iteration always completes, by gating this check off until the root had
319 * one full iteration in hand, HUNG THE SEARCH on the opening position. A budget
320 * that can be switched off is not a budget. */
321 7851470 static int spent(struct search *s)
322 {
323 7851470 ▶ if (s->stopped) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s->stopped) return 1; 0 7851470 7851470
324 7851470 ▶ if ((s->nodes & 1023) == 0 && s->nodes >= s->budget) s->stopped = 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((s->nodes & 1023) == 0 && s->nodes >= s->budget) s->stopped = 1; 7663 7843807 7851470
if ((s->nodes & 1023) == 0 && s->nodes >= s->budget) s->stopped = 1; 593 7070 7663
325 7851470 return s->stopped;
326 }
327  
328 /* ---- quiescence ---------------------------------------------------------------------
329 *
330 * Captures only. Without it the search hands back a score taken in the middle of
331 * an exchange, which in this game is worse than in chess: a cannon's capture
332 * arrives from a distance and over a screen, so a quiet-looking position is
333 * routinely one move from losing a chariot.
334 */
335 5022305 static int quiesce(struct xq_board *b, struct search *s, int alpha, int beta)
336 {
337 int moves[XQ_MAX_MOVES];
338 5022305 int n, i, best, us = (E())->side(b);
339  
340 5022305 s->nodes++;
341 5022305 ▶ if (spent(s)) return xq_evaluate(b);
Branch: 1 branch
BranchTrueFalseTotal
if (spent(s)) return xq_evaluate(b); 414 5021891 5022305
342  
343 5021891 best = xq_evaluate(b);
344 5021891 ▶ if (best >= beta) return best;
Branch: 1 branch
BranchTrueFalseTotal
if (best >= beta) return best; 2573084 2448807 5021891
345 2448807 ▶ if (best > alpha) alpha = best;
Branch: 1 branch
BranchTrueFalseTotal
if (best > alpha) alpha = best; 325151 2123656 2448807
346  
347 2448807 n = (E())->gen_moves(b, moves);
348 2448807 order(b, moves, n, 0);
349 52519903 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 51050862 1469041 52519903
350 struct xq_undo u;
351 int score;
352 51050862 ▶ if (!XQ_IS_PIECE(AT(b, (E())->move_to(moves[i])))) continue; /* captures only */
Branch: 2 branches
BranchTrueFalseTotal
if (!XQ_IS_PIECE(AT(b, (E())->move_to(moves[i])))) continue; /* captures only */ 2754776 48296086 51050862
if (!XQ_IS_PIECE(AT(b, (E())->move_to(moves[i])))) continue; /* captures only */ 0 2754776 2754776
353 2754776 (E())->do_move(b, moves[i], &u);
354 2754776 ▶ if (legal_after(b, us)) {
Branch: 1 branch
BranchTrueFalseTotal
if (legal_after(b, us)) { 2617099 137677 2754776
355 2617099 score = -quiesce(b, s, -beta, -alpha);
356 2617099 (E())->undo_move(b, &u);
357 2617099 ▶ if (s->stopped) return best;
Branch: 1 branch
BranchTrueFalseTotal
if (s->stopped) return best; 593 2616506 2617099
358 2616506 ▶ if (score > best) best = score;
Branch: 1 branch
BranchTrueFalseTotal
if (score > best) best = score; 1738552 877954 2616506
359 2616506 ▶ if (best > alpha) alpha = best;
Branch: 1 branch
BranchTrueFalseTotal
if (best > alpha) alpha = best; 1135548 1480958 2616506
360 2616506 ▶ if (alpha >= beta) break;
Branch: 1 branch
BranchTrueFalseTotal
if (alpha >= beta) break; 979173 1637333 2616506
361 }
362 137677 else (E())->undo_move(b, &u);
363 }
364 2448214 return best;
365 }
366  
367 /* ---- the search ------------------------------------------------------------------- */
368  
369 2829165 static int alphabeta(struct xq_board *b, struct search *s,
370 int depth, int alpha, int beta, int ply)
371 {
372 int moves[XQ_MAX_MOVES];
373 2829165 int n, i, best = -XQ_MATE_SCORE * 2, tt_move = 0;
374 2829165 int alpha0 = alpha, played = 0, us = (E())->side(b);
375 2829165 unsigned long long key = (E())->key(b);
376 2829165 struct tt_entry *e = &s->tt[key & (TT_SIZE - 1)];
377  
378 2829165 s->nodes++;
379 2829165 ▶ if (spent(s)) return xq_evaluate(b);
Branch: 1 branch
BranchTrueFalseTotal
if (spent(s)) return xq_evaluate(b); 179 2828986 2829165
380  
381 2828986 ▶ if (e->key == key) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->key == key) { 114885 2714101 2828986
382 114885 tt_move = e->move;
383 114885 ▶ if (e->depth >= depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->depth >= depth) { 53629 61256 114885
384 53629 ▶ if (e->flag == TT_EXACT) return e->score;
Branch: 1 branch
BranchTrueFalseTotal
if (e->flag == TT_EXACT) return e->score; 615 53014 53629
385 53014 ▶ 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; 51452 1562 53014
if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score; 51231 221 51452
386 53014 ▶ 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; 1562 51452 53014
if (e->flag == TT_UPPER && e->score < beta) beta = e->score; 1339 223 1562
387 53014 ▶ if (alpha >= beta) return e->score;
Branch: 1 branch
BranchTrueFalseTotal
if (alpha >= beta) return e->score; 51803 1211 53014
388 }
389 }
390  
391 /* At the horizon the loop below never runs, so a mate would go unseen. A side
392 * IN CHECK is therefore tested exactly, which costs a tenth of a microsecond
393 * for the test and a full filter only on the nodes that are actually in
394 * check. See the head of this file for the stalemate that is not tested. */
395 2776568 ▶ if (depth <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) { 2407359 369209 2776568
396 2407359 ▶ if ((E())->in_check(b, us)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->in_check(b, us)) { 66959 2340400 2407359
397 int esc[XQ_MAX_MOVES];
398 66959 ▶ if ((E())->gen_legal(b, esc) == 0) return -XQ_MATE_SCORE + ply;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->gen_legal(b, esc) == 0) return -XQ_MATE_SCORE + ply; 2153 64806 66959
399 }
400 2405206 return quiesce(b, s, alpha, beta);
401 }
402  
403 369209 n = (E())->gen_moves(b, moves);
404 369209 order(b, moves, n, tt_move);
405 3139412 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3074731 64681 3139412
406 struct xq_undo u;
407 int score;
408 3074731 (E())->do_move(b, moves[i], &u);
409 3074731 ▶ if (!legal_after(b, us)) { (E())->undo_move(b, &u); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!legal_after(b, us)) { (E())->undo_move(b, &u); continue; } 299946 2774785 3074731
410 2774785 played++;
411 2774785 score = -alphabeta(b, s, depth - 1, -beta, -alpha, ply + 1);
412 2774785 (E())->undo_move(b, &u);
413 2774785 ▶ if (s->stopped) return best > -XQ_MATE_SCORE * 2 ? best : xq_evaluate(b);
Branch: 2 branches
BranchTrueFalseTotal
if (s->stopped) return best > -XQ_MATE_SCORE * 2 ? best : xq_evaluate(b); 1313 2773472 2774785
if (s->stopped) return best > -XQ_MATE_SCORE * 2 ? best : xq_evaluate(b); 498 815 1313
414 2773472 ▶ if (score > best) { best = score; tt_move = moves[i]; }
Branch: 1 branch
BranchTrueFalseTotal
if (score > best) { best = score; tt_move = moves[i]; } 578458 2195014 2773472
415 2773472 ▶ if (best > alpha) alpha = best;
Branch: 1 branch
BranchTrueFalseTotal
if (best > alpha) alpha = best; 398028 2375444 2773472
416 2773472 ▶ if (alpha >= beta) break;
Branch: 1 branch
BranchTrueFalseTotal
if (alpha >= beta) break; 303215 2470257 2773472
417 }
418  
419 /* NO LEGAL MOVE IS A LOSS, in check or not: stalemate is a loss in this game
420 * and a search that returned a draw here would play for one. The score is
421 * adjusted by PLY so a mate in one beats a mate in three. */
422 367896 ▶ if (!played) return -XQ_MATE_SCORE + ply;
Branch: 1 branch
BranchTrueFalseTotal
if (!played) return -XQ_MATE_SCORE + ply; 864 367032 367896
423  
424 /* FAIL-SOFT: `best` is returned, never `alpha`. See the head of this file. */
425 367032 e->key = key;
426 367032 e->move = tt_move;
427 367032 ▶ e->score = (short) (best > 32000 ? 32000 : best < -32000 ? -32000 : best);
Branch: 2 branches
BranchTrueFalseTotal
e->score = (short) (best > 32000 ? 32000 : best < -32000 ? -32000 : best); 367032 0 367032
e->score = (short) (best > 32000 ? 32000 : best < -32000 ? -32000 : best); 367032 0 367032
428 367032 e->depth = (signed char) depth;
429 367032 ▶ e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER
Branch: 2 branches
BranchTrueFalseTotal
e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER 311340 55692 367032
e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER 303215 8125 311340
430 : best >= beta ? TT_LOWER : TT_EXACT);
431 367032 return best;
432 }
433  
434 /* ONE SEARCH, TWO ENTRIES. `search_best` is this with the depth ceiling, so the
435 * depth-limited search that UCCI needs cannot drift away from the one the bot
436 * plays. See the header on why a depth limit exists at all. */
437 616 static int xq_search_to_depth(struct xq_board *b, int max_depth,
438 unsigned long long budget,
439 unsigned int seed, struct xq_search_info *info)
440 {
441 int moves[XQ_MAX_MOVES];
442 int n, i, depth;
443 616 int best_move = 0, best_score = 0;
444 struct search s;
445  
446 616 ▶ if (info) { info->nodes = 0; info->depth = 0; info->score = 0; info->stopped = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (info) { info->nodes = 0; info->depth = 0; info->score = 0; info->stopped = 0; } 616 0 616
447  
448 616 n = (E())->gen_legal(b, moves);
449 616 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 1 615 616
450  
451 615 s.nodes = 0;
452 615 ▶ s.budget = budget ? budget : 1;
Branch: 1 branch
BranchTrueFalseTotal
s.budget = budget ? budget : 1; 615 0 615
453 615 s.stopped = 0;
454 615 s.seed = seed;
455 615 s.tt = (struct tt_entry *) calloc(TT_SIZE, sizeof(struct tt_entry));
456 615 ▶ if (!s.tt) return moves[0];
Branch: 1 branch
BranchTrueFalseTotal
if (!s.tt) return moves[0]; 0 615 615
457  
458 /* SHUFFLED FIRST, so that a budget too small to finish even depth 1 still
459 * hands back a move drawn by the seed rather than whatever the generator
460 * happened to put first. See `shuffle`. */
461 615 shuffle(moves, n, seed);
462 615 best_move = moves[0];
463  
464 /* ITERATIVE DEEPENING, so a spent budget always leaves a COMPLETE search at
465 * a shallower depth rather than half of a deeper one. */
466 615 ▶ if (max_depth < 1) max_depth = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (max_depth < 1) max_depth = 1; 0 615 615
467 615 ▶ if (max_depth > 64) max_depth = 64;
Branch: 1 branch
BranchTrueFalseTotal
if (max_depth > 64) max_depth = 64; 0 615 615
468  
469 1973 ▶ for (depth = 1; depth <= max_depth; depth++) {
Branch: 1 branch
BranchTrueFalseTotal
for (depth = 1; depth <= max_depth; depth++) { 1967 6 1973
470 int scores[XQ_MAX_MOVES];
471 1967 int local_best = -XQ_MATE_SCORE * 2, alpha = -XQ_MATE_SCORE * 2;
472  
473 1967 order(b, moves, n, best_move);
474 55754 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 54380 1374 55754
475 struct xq_undo u;
476 54380 (E())->do_move(b, moves[i], &u);
477  
478 54380 scores[i] = -alphabeta(b, &s, depth - 1, -XQ_MATE_SCORE * 2, -alpha, 1);
479 54380 (E())->undo_move(b, &u);
480 54380 ▶ if (s.stopped) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s.stopped) break; 593 53787 54380
481  
482 /* STRICTLY GREATER, AND THE TIE-BREAK IS THE SHUFFLE ABOVE.
483 *
484 * THIS IS THE PHASE'S SECOND BUG AND THE COMMENT THAT USED TO BE HERE
485 * CLAIMED THE OPPOSITE OF WHAT THE CODE DID. The root raised `alpha`,
486 * which is ordinary alpha-beta and right, and then pooled every
487 * `scores[i]` equal to the best and picked among them with the seed,
488 * which is wrong: with the window cut at `-alpha` a root move that is
489 * not an improvement makes its child fail high, and a fail-soft child
490 * that fails high returns a BOUND and not a value. With an integer
491 * evaluation that bound lands EXACTLY on alpha very often, so the pool
492 * filled with moves that were merely not-better and the bot played one
493 * of them at random.
494 *
495 * It got WORSE WITH DEPTH, because a deeper search raises alpha sooner
496 * and cuts more children off at the bound. Measured, sixty games:
497 * rung 40000 scored 38.3% against rung 8000, and 13 of the 44 opening
498 * moves came back "tied". That is Game::Oware's bug in a different
499 * hat, and the paragraph at the head of this file was written to
500 * prevent it and did not.
501 *
502 * The fix is not a full window, which was tried and costs a whole ply
503 * of depth at every rung. It is that A TIE IS NEVER PICKED AT ALL:
504 * only a STRICT improvement replaces the move, so the move that wins
505 * is the FIRST one to reach the best score. The root list is shuffled
506 * by the seed before the search, and `order` below is a stable sort,
507 * so first-to-reach-it IS a uniform draw among the moves that truly
508 * tie. No extra nodes, and the bound never enters the choice. */
509 53787 ▶ if (scores[i] > local_best) {
Branch: 1 branch
BranchTrueFalseTotal
if (scores[i] > local_best) { 3598 50189 53787
510 3598 local_best = scores[i];
511 3598 best_move = moves[i];
512 }
513 53787 ▶ if (local_best > alpha) alpha = local_best;
Branch: 1 branch
BranchTrueFalseTotal
if (local_best > alpha) alpha = local_best; 3598 50189 53787
514 }
515 1983 ▶ if (s.stopped) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s.stopped) break; 593 1374 1967
516  
517 1374 ▶ if (info) { info->depth = depth; info->score = local_best; }
Branch: 1 branch
BranchTrueFalseTotal
if (info) { info->depth = depth; info->score = local_best; } 1374 0 1374
518 1374 ▶ if (XQ_IS_MATE(local_best)) break; /* nothing deeper can improve on mate */
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_IS_MATE(local_best)) break; /* nothing deeper can improve on mate */ 1358 16 1374
if (XQ_IS_MATE(local_best)) break; /* nothing deeper can improve on mate */ 1358 0 1358
519 }
520  
521 615 free(s.tt);
522 615 ▶ if (info) { info->nodes = s.nodes; info->stopped = s.stopped; }
Branch: 1 branch
BranchTrueFalseTotal
if (info) { info->nodes = s.nodes; info->stopped = s.stopped; } 615 0 615
523 (void) best_score;
524 615 return best_move;
525 }
526  
527 /* ---- installed into the table by xq_engine.c ---------------------------------------- */
528  
529 610 static int xq_search_best(struct xq_board *b, unsigned long long budget,
530 unsigned int seed, struct xq_search_info *info)
531 {
532 610 return xq_search_to_depth(b, 64, budget, seed, info);
533 }
534  
535 void xq_search_install(struct xq_abi *t);
536  
537 28 void xq_search_install(struct xq_abi *t)
538 {
539 28 t->evaluate = xq_evaluate;
540 28 t->search_best = xq_search_best;
541 28 t->search_to_depth = xq_search_to_depth;
542 28 }
« xq_moves.c ↑ Summary xq_mate.c »