| 1 |
|
| /* shg_search.c - the evaluation and the search. Phase 08 of plan_game_shogi. |
| 2 |
|
| * |
| 3 |
|
| * PERL-FREE, as the rest of the engine is. |
| 4 |
|
| * |
| 5 |
|
| * ---- WHAT THIS FILE TAKES FROM GAME-XIANGQI, AND WHY --------------------------------- |
| 6 |
|
| * |
| 7 |
|
| * FAIL-SOFT, AND A ROOT TIE IS NEVER PICKED. Game::Oware's bot got weaker with |
| 8 |
|
| * depth because fail-hard children all came back equal to alpha and the root |
| 9 |
|
| * drew among them; Game::Xiangqi's did the same in a different hat, pooling |
| 10 |
|
| * root moves whose scores EQUALLED the best when a fail-soft child that fails |
| 11 |
|
| * high returns a bound, not a value. Here, as there: the root list is shuffled |
| 12 |
|
| * by the seed once, only a STRICT improvement replaces the move, and the |
| 13 |
|
| * ordering is stable, so the first move to reach the best score is a uniform |
| 14 |
|
| * draw among the moves that truly tie. |
| 15 |
|
| * |
| 16 |
|
| * BOUNDED IN NODES, NEVER IN TIME. The site plays the bot inside a database |
| 17 |
|
| * transaction; a budget in seconds would make a bot game unreplayable. Checked |
| 18 |
|
| * every 1024 nodes and never switched off: Xiangqi's quiescence hung the |
| 19 |
|
| * opening position the one time its check was gated. |
| 20 |
|
| * |
| 21 |
|
| * ITERATIVE DEEPENING, so a spent budget leaves a whole shallower search. |
| 22 |
|
| * |
| 23 |
|
| * LEGALITY IS LAZY: moves are generated pseudo-legally and tested after |
| 24 |
|
| * do_move, only the ones searched. Xiangqi measured 90,000 nodes a second |
| 25 |
|
| * filtering every move first and 2.26 million generating them. |
| 26 |
|
| * |
| 27 |
|
| * ---- WHAT SHOGI ADDS --------------------------------------------------------------- |
| 28 |
|
| * |
| 29 |
|
| * DROPS ARE MOST OF THE MOVES (79 percent, measured-branching-2026-10-14.txt) |
| 30 |
|
| * and most are bad, so the ordering decides the depth: the table's move, |
| 31 |
|
| * captures by victim then attacker, promotions, the killers, quiet board |
| 32 |
|
| * moves, drops near either king, and the other drops last. Moves are PICKED |
| 33 |
|
| * best-first one at a time rather than sorted: with five hundred moves a node, |
| 34 |
|
| * a sort costs more than the cutoff it is for. |
| 35 |
|
| * |
| 36 |
|
| * A DROP IS GENERATED WITH ITS CHEAP RULES (an empty square, not dead, no |
| 37 |
|
| * second pawn on the file) and its dear ones are asked after it is made: the |
| 38 |
|
| * king, and for a pawn that checks, whether the defender has a board move |
| 39 |
|
| * (the pawn-drop mate rule, as shg_drop_fault asks it). |
| 40 |
|
| * |
| 41 |
|
| * MATERIAL NEVER LEAVES: a capture moves a piece from one side's board to the |
| 42 |
|
| * other side's hand, so the evaluation values a hand as well as a board. |
| 43 |
|
| */ |
| 44 |
|
| |
| 45 |
|
| #include <stdlib.h> |
| 46 |
|
| #include <string.h> |
| 47 |
|
| #include "shg_internal.h" |
| 48 |
|
| |
| 49 |
|
| /* ---- the evaluation ---------------------------------------------------------------- |
| 50 |
|
| * |
| 51 |
|
| * Centipawns from the side to move's view. THE VALUES ARE A GUESS and phase |
| 52 |
|
| * 08's ablation measures the terms built on them; the material term itself is |
| 53 |
|
| * not ablated, because without it nothing is being played. |
| 54 |
|
| */ |
| 55 |
|
| |
| 56 |
|
| static const int VALUE[SHG_KINDS] = { |
| 57 |
|
| 0, 100, 300, 350, 500, 550, 800, 1000, 0, /* P L N S G B R K */ |
| 58 |
|
| 550, 550, 550, 550, 1050, 1250 /* +P +L +N +S +B +R */ |
| 59 |
|
| }; |
| 60 |
|
| |
| 61 |
|
| /* A piece in hand is worth more than the same piece on the board: it can go |
| 62 |
|
| * anywhere. */ |
| 63 |
|
| #define HAND_BONUS_PCT 15 |
| 64 |
|
| |
| 65 |
|
| #define GUARD_BONUS 30 /* a gold, silver or gold-like next to its king */ |
| 66 |
|
| #define GUARD_MAX 3 |
| 67 |
|
| #define ATTACKER_PENALTY 25 /* an enemy piece within two squares of the king */ |
| 68 |
|
| #define HAND_THREAT 10 /* each enemy piece in hand that is not a pawn */ |
| 69 |
|
| #define PAWN_THREAT 3 |
| 70 |
|
| #define ENTERING_BONUS 200 |
| 71 |
|
| #define ENTERED_PIECE 10 |
| 72 |
|
| |
| 73 |
59084149 |
| static int distance(int a, int b) |
| 74 |
|
| { |
| 75 |
59084149 |
| int df = abs(a % SHG_FILES - b % SHG_FILES), dr = abs(a / SHG_FILES - b / SHG_FILES); |
| 76 |
59084149 |
| return df > dr ? df : dr; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
26355836 |
| static int is_guard(int kind) |
| 80 |
|
| { |
| 81 |
22587088 |
▶ | return kind == SHG_GOLD || kind == SHG_SILVER || kind == SHG_TOKIN |
Branch: 2 branches
| Branch | True | False | Total |
| return kind == SHG_GOLD || kind == SHG_SILVER || kind == SHG_TOKIN |
19909094 |
2677994 |
22587088 |
| return kind == SHG_GOLD || kind == SHG_SILVER || kind == SHG_TOKIN |
19604274 |
304820 |
19909094 |
|
| 82 |
48942924 |
▶ | || kind == SHG_PRO_LANCE || kind == SHG_PRO_KNIGHT || kind == SHG_PRO_SILVER; |
Branch: 4 branches
| Branch | True | False | Total |
| || kind == SHG_PRO_LANCE || kind == SHG_PRO_KNIGHT || kind == SHG_PRO_SILVER; |
22587088 |
3768748 |
26355836 |
| || kind == SHG_PRO_LANCE || kind == SHG_PRO_KNIGHT || kind == SHG_PRO_SILVER; |
19488172 |
116102 |
19604274 |
| || kind == SHG_PRO_LANCE || kind == SHG_PRO_KNIGHT || kind == SHG_PRO_SILVER; |
19313726 |
174446 |
19488172 |
| || kind == SHG_PRO_LANCE || kind == SHG_PRO_KNIGHT || kind == SHG_PRO_SILVER; |
42466 |
19271260 |
19313726 |
|
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
2602486 |
| static int side_score(const struct shg_board *b, int side, int flags) |
| 86 |
|
| { |
| 87 |
2602486 |
| int other = side == SHG_BLACK ? SHG_WHITE : SHG_BLACK; |
| 88 |
2602486 |
| int sq, kind, score = 0, guards = 0, king = b->king[side]; |
| 89 |
213403852 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) { |
210801366 |
2602486 |
213403852 |
|
| 90 |
210801366 |
| int p = b->cell[SHG_SQ_OF[sq]]; |
| 91 |
210801366 |
▶ | if (p == SHG_EMPTY) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == SHG_EMPTY) continue; |
152884720 |
57916646 |
210801366 |
|
| 92 |
57916646 |
▶ | if (SHG_SIDE_OF(p) == side) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SHG_SIDE_OF(p) == side) { |
28958323 |
28958323 |
57916646 |
|
| 93 |
28958323 |
| score += VALUE[SHG_KIND_OF(p)]; |
| 94 |
28958323 |
▶ | if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && sq != king |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && sq != king |
28958320 |
3 |
28958323 |
| if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && sq != king |
28958320 |
0 |
28958320 |
| if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && sq != king |
26355836 |
2602484 |
28958320 |
|
| 95 |
26355836 |
▶ | && is_guard(SHG_KIND_OF(p)) && distance(sq, king) == 1) |
Branch: 2 branches
| Branch | True | False | Total |
| && is_guard(SHG_KIND_OF(p)) && distance(sq, king) == 1) |
7084576 |
19271260 |
26355836 |
| && is_guard(SHG_KIND_OF(p)) && distance(sq, king) == 1) |
3297976 |
3786600 |
7084576 |
|
| 96 |
3297976 |
| guards++; |
| 97 |
|
| } |
| 98 |
28958323 |
▶ | else if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && distance(sq, king) <= 2) { |
Branch: 3 branches
| Branch | True | False | Total |
| else if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && distance(sq, king) <= 2) { |
28958320 |
3 |
28958323 |
| else if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && distance(sq, king) <= 2) { |
28958320 |
0 |
28958320 |
| else if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0 && distance(sq, king) <= 2) { |
856448 |
28101872 |
28958320 |
|
| 99 |
856448 |
| score -= ATTACKER_PENALTY; |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
20819888 |
▶ | for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) { |
18217402 |
2602486 |
20819888 |
|
| 103 |
18217402 |
| int n = b->hand[side][kind]; |
| 104 |
18217402 |
▶ | score += n * (flags & SHG_EVAL_NO_HAND_BONUS ? VALUE[kind] : VALUE[kind] * (100 + HAND_BONUS_PCT) / 100); |
Branch: 1 branch
| Branch | True | False | Total |
| score += n * (flags & SHG_EVAL_NO_HAND_BONUS ? VALUE[kind] : VALUE[kind] * (100 + HAND_BONUS_PCT) / 100); |
14 |
18217388 |
18217402 |
|
| 105 |
18217402 |
▶ | if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0) |
18217388 |
14 |
18217402 |
| if (!(flags & SHG_EVAL_NO_KING_SAFETY) && king >= 0) |
18217388 |
0 |
18217388 |
|
| 106 |
18217388 |
▶ | score -= b->hand[other][kind] * (kind == SHG_PAWN ? PAWN_THREAT : HAND_THREAT); |
Branch: 1 branch
| Branch | True | False | Total |
| score -= b->hand[other][kind] * (kind == SHG_PAWN ? PAWN_THREAT : HAND_THREAT); |
2602484 |
15614904 |
18217388 |
|
| 107 |
|
| } |
| 108 |
2602486 |
▶ | if (!(flags & SHG_EVAL_NO_KING_SAFETY)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(flags & SHG_EVAL_NO_KING_SAFETY)) |
2602484 |
2 |
2602486 |
|
| 109 |
2602484 |
| score += GUARD_BONUS * (guards > GUARD_MAX ? GUARD_MAX : guards); |
| 110 |
2602486 |
▶ | if (!(flags & SHG_EVAL_NO_ENTERING) && king >= 0 && SHG_ZONE[side][king]) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(flags & SHG_EVAL_NO_ENTERING) && king >= 0 && SHG_ZONE[side][king]) |
2602484 |
2 |
2602486 |
| if (!(flags & SHG_EVAL_NO_ENTERING) && king >= 0 && SHG_ZONE[side][king]) |
2602484 |
0 |
2602484 |
| if (!(flags & SHG_EVAL_NO_ENTERING) && king >= 0 && SHG_ZONE[side][king]) |
21 |
2602463 |
2602484 |
|
| 111 |
21 |
| score += ENTERING_BONUS + ENTERED_PIECE * shg_impasse_pieces(b, side); |
| 112 |
2602486 |
| return score; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
1301243 |
| int shg_evaluate(const struct shg_board *b, int flags) |
| 116 |
|
| { |
| 117 |
|
| int side; |
| 118 |
1301243 |
▶ | if (!b) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return 0; |
0 |
1301243 |
1301243 |
|
| 119 |
1301243 |
| side = b->to_move; |
| 120 |
1301243 |
| return side_score(b, side, flags) - side_score(b, side == SHG_BLACK ? SHG_WHITE : SHG_BLACK, flags); |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
|
| /* ---- moves for the search --------------------------------------------------------- */ |
| 124 |
|
| |
| 125 |
|
| #define MAX_PLY 64 |
| 126 |
|
| #define TT_BITS 17 |
| 127 |
|
| #define TT_SIZE (1 << TT_BITS) |
| 128 |
|
| #define TT_EXACT 1 |
| 129 |
|
| #define TT_LOWER 2 |
| 130 |
|
| #define TT_UPPER 3 |
| 131 |
|
| |
| 132 |
|
| struct tt_entry { |
| 133 |
|
| unsigned long long key; |
| 134 |
|
| int move; |
| 135 |
|
| short score; |
| 136 |
|
| signed char depth; |
| 137 |
|
| unsigned char flag; |
| 138 |
|
| }; |
| 139 |
|
| |
| 140 |
|
| struct search { |
| 141 |
|
| unsigned long long nodes; |
| 142 |
|
| unsigned long long budget; |
| 143 |
|
| int stopped; |
| 144 |
|
| int flags; |
| 145 |
|
| struct tt_entry *tt; |
| 146 |
|
| int killer[MAX_PLY][2]; |
| 147 |
|
| const unsigned long long *history; /* the game's positions before the root */ |
| 148 |
|
| int n_history; |
| 149 |
|
| unsigned long long path[MAX_PLY + 1]; /* the keys from the root to this node */ |
| 150 |
|
| int root_side; /* who is searching */ |
| 151 |
|
| }; |
| 152 |
|
| |
| 153 |
|
| /* CONTEMPT: a draw is scored a little against the side that is searching and |
| 154 |
|
| * a little for its opponent. Scored as an even 0, a repetition tied with every |
| 155 |
|
| * quiet move of a level position and the shuffle picked it: 30 of 100 test |
| 156 |
|
| * games still ended in sennichite after the history was given to the search. */ |
| 157 |
|
| #define CONTEMPT 50 |
| 158 |
|
| |
| 159 |
|
| /* A POSITION REACHED AGAIN SCORES AS A DRAW, whether the game stood in it |
| 160 |
|
| * before or this line of search already passed through it. Without it two |
| 161 |
|
| * bots repeated a quiet position into sennichite in half of 100 test games: |
| 162 |
|
| * the side that was ahead walked into the draw as readily as the side that was |
| 163 |
|
| * behind. The key names the side to move, so equal keys are the same position |
| 164 |
|
| * with the same player to move, as repetition counts one. This calls the first |
| 165 |
|
| * return a draw where the rule needs the fourth, the usual shortcut. */ |
| 166 |
1548843 |
| static int repeated(const struct search *s, unsigned long long key, int ply) |
| 167 |
|
| { |
| 168 |
|
| int i; |
| 169 |
5467061 |
▶ | for (i = ply - 2; i >= 0; i -= 2) if (s->path[i] == key) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = ply - 2; i >= 0; i -= 2) if (s->path[i] == key) return 1; |
4950 |
3918218 |
3923168 |
| for (i = ply - 2; i >= 0; i -= 2) if (s->path[i] == key) return 1; |
3923168 |
1543893 |
5467061 |
|
| 170 |
7281258 |
▶ | for (i = 0; i < s->n_history; i++) if (s->history[i] == key) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < s->n_history; i++) if (s->history[i] == key) return 1; |
346 |
5737365 |
5737711 |
| for (i = 0; i < s->n_history; i++) if (s->history[i] == key) return 1; |
5737711 |
1543547 |
7281258 |
|
| 171 |
1543547 |
| return 0; |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| /* The board moves the generator gives, then the drops with their cheap rules |
| 175 |
|
| * kept: an empty square, not dead, no second pawn on the file. */ |
| 176 |
1293568 |
| static int generate(const struct shg_board *b, int *out) |
| 177 |
|
| { |
| 178 |
1293568 |
| int n = shg_pseudo_moves(b, out, SHG_MOVES_MAX), side = b->to_move, kind, sq; |
| 179 |
|
| int pawn_file[SHG_FILES]; |
| 180 |
1293568 |
▶ | if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; |
0 |
1293568 |
1293568 |
|
| 181 |
1293568 |
| memset(pawn_file, 0, sizeof pawn_file); |
| 182 |
106072576 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) |
104779008 |
1293568 |
106072576 |
|
| 183 |
104779008 |
▶ | if (b->cell[SHG_SQ_OF[sq]] == SHG_PIECE(side, SHG_PAWN)) pawn_file[sq % SHG_FILES] = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->cell[SHG_SQ_OF[sq]] == SHG_PIECE(side, SHG_PAWN)) pawn_file[sq % SHG_FILES] = 1; |
4647094 |
100131914 |
104779008 |
|
| 184 |
10348544 |
▶ | for (kind = SHG_ROOK; kind >= SHG_PAWN; kind--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = SHG_ROOK; kind >= SHG_PAWN; kind--) { |
9054976 |
1293568 |
10348544 |
|
| 185 |
9054976 |
▶ | if (b->hand[side][kind] < 1) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->hand[side][kind] < 1) continue; |
8880898 |
174078 |
9054976 |
|
| 186 |
14274396 |
▶ | for (sq = 0; sq < SHG_SQUARES && n < SHG_MOVES_MAX; sq++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES && n < SHG_MOVES_MAX; sq++) { |
14100318 |
174078 |
14274396 |
| for (sq = 0; sq < SHG_SQUARES && n < SHG_MOVES_MAX; sq++) { |
14100318 |
0 |
14100318 |
|
| 187 |
14100318 |
| int last = SHG_LAST[side][sq]; |
| 188 |
14100318 |
▶ | if (b->cell[SHG_SQ_OF[sq]] != SHG_EMPTY) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->cell[SHG_SQ_OF[sq]] != SHG_EMPTY) continue; |
6069269 |
8031049 |
14100318 |
|
| 189 |
8031049 |
▶ | if ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) continue; |
4295843 |
3735206 |
8031049 |
| if ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) continue; |
307885 |
3987958 |
4295843 |
| if ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) continue; |
433868 |
3609223 |
4043091 |
|
| 190 |
7597181 |
▶ | if (kind == SHG_KNIGHT && last <= 1) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind == SHG_KNIGHT && last <= 1) continue; |
694747 |
6902434 |
7597181 |
| if (kind == SHG_KNIGHT && last <= 1) continue; |
173021 |
521726 |
694747 |
|
| 191 |
7424160 |
▶ | if (kind == SHG_PAWN && pawn_file[sq % SHG_FILES]) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind == SHG_PAWN && pawn_file[sq % SHG_FILES]) continue; |
3336357 |
4087803 |
7424160 |
| if (kind == SHG_PAWN && pawn_file[sq % SHG_FILES]) continue; |
2505952 |
830405 |
3336357 |
|
| 192 |
4918208 |
| out[n++] = SHG_DROP(kind, sq); |
| 193 |
|
| } |
| 194 |
|
| } |
| 195 |
1293568 |
| return n; |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
|
| /* After `move` was made by `mover`: its own king safe, and a pawn dropped with |
| 199 |
|
| * check not mating (the defender, now to move, must have a board move). */ |
| 200 |
3156948 |
| static int legal_after(const struct shg_board *b, int mover, int move) |
| 201 |
|
| { |
| 202 |
3156948 |
| int other = mover == SHG_BLACK ? SHG_WHITE : SHG_BLACK; |
| 203 |
3156948 |
▶ | if (b->king[mover] >= 0 && shg_attacked(b, b->king[mover], other)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->king[mover] >= 0 && shg_attacked(b, b->king[mover], other)) return 0; |
3156948 |
0 |
3156948 |
| if (b->king[mover] >= 0 && shg_attacked(b, b->king[mover], other)) return 0; |
66256 |
3090692 |
3156948 |
|
| 204 |
3090692 |
▶ | if (SHG_MOVE_DROP(move) && SHG_MOVE_KIND(move) == SHG_PAWN |
Branch: 2 branches
| Branch | True | False | Total |
| if (SHG_MOVE_DROP(move) && SHG_MOVE_KIND(move) == SHG_PAWN |
719291 |
2371401 |
3090692 |
| if (SHG_MOVE_DROP(move) && SHG_MOVE_KIND(move) == SHG_PAWN |
61708 |
657583 |
719291 |
|
| 205 |
61708 |
▶ | && shg_in_check(b, other) && !shg_any_board_move(b)) |
Branch: 2 branches
| Branch | True | False | Total |
| && shg_in_check(b, other) && !shg_any_board_move(b)) |
4602 |
57106 |
61708 |
| && shg_in_check(b, other) && !shg_any_board_move(b)) |
18 |
4584 |
4602 |
|
| 206 |
18 |
| return 0; |
| 207 |
3090674 |
| return 1; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
5210329 |
| static int order_score(const struct shg_board *b, const struct search *s, int move, int tt_move, int ply) |
| 211 |
|
| { |
| 212 |
5210329 |
| int to = SHG_MOVE_TO(move), other = b->to_move == SHG_BLACK ? SHG_WHITE : SHG_BLACK; |
| 213 |
5210329 |
▶ | if (move == tt_move) return 1000000; |
Branch: 1 branch
| Branch | True | False | Total |
| if (move == tt_move) return 1000000; |
101838 |
5108491 |
5210329 |
|
| 214 |
5108491 |
▶ | if (!SHG_MOVE_DROP(move)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SHG_MOVE_DROP(move)) { |
4711029 |
397462 |
5108491 |
|
| 215 |
4711029 |
| int victim = b->cell[SHG_SQ_OF[to]]; |
| 216 |
4711029 |
| int mover = b->cell[SHG_SQ_OF[SHG_MOVE_FROM(move)]]; |
| 217 |
4711029 |
▶ | if (victim != SHG_EMPTY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (victim != SHG_EMPTY) |
559715 |
4151314 |
4711029 |
|
| 218 |
559715 |
| return 100000 + VALUE[SHG_KIND_OF(victim)] * 4 - VALUE[SHG_KIND_OF(mover)] / 10; |
| 219 |
4151314 |
▶ | if (SHG_MOVE_PROMOTE(move)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SHG_MOVE_PROMOTE(move)) |
17031 |
4134283 |
4151314 |
|
| 220 |
17031 |
| return 50000 + VALUE[SHG_PROMOTED_OF[SHG_KIND_OF(mover)]] - VALUE[SHG_KIND_OF(mover)]; |
| 221 |
4134283 |
▶ | if (ply < MAX_PLY && (move == s->killer[ply][0] || move == s->killer[ply][1])) return 40000; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ply < MAX_PLY && (move == s->killer[ply][0] || move == s->killer[ply][1])) return 40000; |
4134283 |
0 |
4134283 |
| if (ply < MAX_PLY && (move == s->killer[ply][0] || move == s->killer[ply][1])) return 40000; |
3994522 |
139761 |
4134283 |
| if (ply < MAX_PLY && (move == s->killer[ply][0] || move == s->killer[ply][1])) return 40000; |
84104 |
3910418 |
3994522 |
|
| 222 |
3910418 |
| return 1000; |
| 223 |
|
| } |
| 224 |
397462 |
▶ | if ((b->king[other] >= 0 && distance(to, b->king[other]) <= 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((b->king[other] >= 0 && distance(to, b->king[other]) <= 2) |
397462 |
0 |
397462 |
| if ((b->king[other] >= 0 && distance(to, b->king[other]) <= 2) |
335044 |
62418 |
397462 |
|
| 225 |
335044 |
▶ | || (b->king[b->to_move] >= 0 && distance(to, b->king[b->to_move]) <= 1)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (b->king[b->to_move] >= 0 && distance(to, b->king[b->to_move]) <= 1)) |
335044 |
0 |
335044 |
| || (b->king[b->to_move] >= 0 && distance(to, b->king[b->to_move]) <= 1)) |
28714 |
306330 |
335044 |
|
| 226 |
91132 |
| return 500; |
| 227 |
306330 |
| return 0; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| /* Picks the best-scored move left in [i, n) into place i. */ |
| 231 |
2041727 |
| static void pick(int *moves, int *score, int i, int n) |
| 232 |
|
| { |
| 233 |
2041727 |
| int j, best = i, t; |
| 234 |
35528841 |
▶ | for (j = i + 1; j < n; j++) if (score[j] > score[best]) best = j; |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = i + 1; j < n; j++) if (score[j] > score[best]) best = j; |
640083 |
32847031 |
33487114 |
| for (j = i + 1; j < n; j++) if (score[j] > score[best]) best = j; |
33487114 |
2041727 |
35528841 |
|
| 235 |
2041727 |
▶ | if (best == i) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (best == i) return; |
1577526 |
464201 |
2041727 |
|
| 236 |
464201 |
| t = moves[i]; moves[i] = moves[best]; moves[best] = t; |
| 237 |
464201 |
| t = score[i]; score[i] = score[best]; score[best] = t; |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| /* Compared on every call, not every 1024th node: the mate search counts nodes |
| 241 |
|
| * in a loop of its own, and a count that steps over the multiple of 1024 the |
| 242 |
|
| * check waits for would never stop. That happened, measured: 30,720 nodes on |
| 243 |
|
| * a budget of 400. */ |
| 244 |
2849673 |
| static int spent(struct search *s) |
| 245 |
|
| { |
| 246 |
2849673 |
▶ | if (s->stopped) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->stopped) return 1; |
0 |
2849673 |
2849673 |
|
| 247 |
2849673 |
▶ | if (s->nodes >= s->budget) s->stopped = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->nodes >= s->budget) s->stopped = 1; |
273 |
2849400 |
2849673 |
|
| 248 |
2849673 |
| return s->stopped; |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
|
| /* ---- the mate search --------------------------------------------------------------- |
| 252 |
|
| * |
| 253 |
|
| * At the horizon: a check that leaves the defender no legal move. Tried only for |
| 254 |
|
| * moves reaching within two squares of the enemy king, where a check from a |
| 255 |
|
| * stepper or a drop has to be; a slider's long check is left to the main |
| 256 |
|
| * search. Phase 08's G measures whether it is worth its nodes. |
| 257 |
|
| */ |
| 258 |
1022679 |
| static int mates_in_one(struct shg_board *b, struct search *s) |
| 259 |
|
| { |
| 260 |
|
| int moves[SHG_MOVES_MAX]; |
| 261 |
1022679 |
| int n, i, us = b->to_move, them = us == SHG_BLACK ? SHG_WHITE : SHG_BLACK; |
| 262 |
1022679 |
▶ | if (b->king[them] < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->king[them] < 0) return 0; |
0 |
1022679 |
1022679 |
|
| 263 |
1022679 |
| n = generate(b, moves); |
| 264 |
23326405 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
22308747 |
1017658 |
23326405 |
|
| 265 |
|
| struct shg_undo u; |
| 266 |
22308747 |
| int found = 0; |
| 267 |
22308747 |
▶ | if (distance(SHG_MOVE_TO(moves[i]), b->king[them]) > 2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (distance(SHG_MOVE_TO(moves[i]), b->king[them]) > 2) continue; |
20987140 |
1321607 |
22308747 |
|
| 268 |
1321607 |
▶ | if (!shg_do_move(b, moves[i], &u)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(b, moves[i], &u)) continue; |
0 |
1321607 |
1321607 |
|
| 269 |
1321607 |
| s->nodes++; |
| 270 |
1321607 |
▶ | if (legal_after(b, us, moves[i]) && shg_in_check(b, them) && !shg_has_legal_move(b)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (legal_after(b, us, moves[i]) && shg_in_check(b, them) && !shg_has_legal_move(b)) |
1280485 |
41122 |
1321607 |
| if (legal_after(b, us, moves[i]) && shg_in_check(b, them) && !shg_has_legal_move(b)) |
303685 |
976800 |
1280485 |
| if (legal_after(b, us, moves[i]) && shg_in_check(b, them) && !shg_has_legal_move(b)) |
5021 |
298664 |
303685 |
|
| 271 |
5021 |
| found = 1; |
| 272 |
1321607 |
| shg_undo_move(b, &u); |
| 273 |
1321607 |
▶ | if (found) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) return 1; |
5021 |
1316586 |
1321607 |
|
| 274 |
|
| } |
| 275 |
1017658 |
| return 0; |
| 276 |
|
| } |
| 277 |
|
| |
| 278 |
|
| /* ---- quiescence: captures only ------------------------------------------------------ */ |
| 279 |
|
| |
| 280 |
1300702 |
| static int quiesce(struct shg_board *b, struct search *s, int alpha, int beta, int ply) |
| 281 |
|
| { |
| 282 |
|
| int moves[SHG_MOVES_MAX], score[SHG_MOVES_MAX]; |
| 283 |
1300702 |
| int n, i, best, us = b->to_move; |
| 284 |
1300702 |
| s->nodes++; |
| 285 |
1300702 |
| best = shg_evaluate(b, s->flags); |
| 286 |
1300702 |
▶ | if (spent(s) || ply >= MAX_PLY - 1) return best; |
Branch: 2 branches
| Branch | True | False | Total |
| if (spent(s) || ply >= MAX_PLY - 1) return best; |
1300557 |
145 |
1300702 |
| if (spent(s) || ply >= MAX_PLY - 1) return best; |
0 |
1300557 |
1300557 |
|
| 287 |
1300557 |
▶ | if (best >= beta) return best; |
Branch: 1 branch
| Branch | True | False | Total |
| if (best >= beta) return best; |
1018141 |
282416 |
1300557 |
|
| 288 |
282416 |
▶ | if (best > alpha) alpha = best; |
Branch: 1 branch
| Branch | True | False | Total |
| if (best > alpha) alpha = best; |
55403 |
227013 |
282416 |
|
| 289 |
282416 |
| n = shg_pseudo_moves(b, moves, SHG_MOVES_MAX); |
| 290 |
282416 |
▶ | if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; |
0 |
282416 |
282416 |
|
| 291 |
7641831 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
7359415 |
282416 |
7641831 |
|
| 292 |
7359415 |
| int victim = b->cell[SHG_SQ_OF[SHG_MOVE_TO(moves[i])]]; |
| 293 |
7359415 |
▶ | score[i] = victim == SHG_EMPTY ? -1 : order_score(b, s, moves[i], 0, ply); |
Branch: 1 branch
| Branch | True | False | Total |
| score[i] = victim == SHG_EMPTY ? -1 : order_score(b, s, moves[i], 0, ply); |
492576 |
6866839 |
7359415 |
|
| 294 |
|
| } |
| 295 |
506423 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
506421 |
2 |
506423 |
|
| 296 |
|
| struct shg_undo u; |
| 297 |
|
| int v; |
| 298 |
506421 |
| pick(moves, score, i, n); |
| 299 |
582211 |
▶ | if (score[i] < 0) break; /* the captures are all in front */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (score[i] < 0) break; /* the captures are all in front */ |
206386 |
300035 |
506421 |
|
| 300 |
317026 |
▶ | if (!shg_do_move(b, moves[i], &u)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(b, moves[i], &u)) continue; |
0 |
300035 |
300035 |
|
| 301 |
300035 |
▶ | if (!legal_after(b, us, moves[i])) { shg_undo_move(b, &u); continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!legal_after(b, us, moves[i])) { shg_undo_move(b, &u); continue; } |
16991 |
283044 |
300035 |
|
| 302 |
283044 |
| v = -quiesce(b, s, -beta, -alpha, ply + 1); |
| 303 |
283044 |
| shg_undo_move(b, &u); |
| 304 |
283044 |
▶ | if (s->stopped) return best; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->stopped) return best; |
238 |
282806 |
283044 |
|
| 305 |
282806 |
▶ | if (v > best) best = v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > best) best = v; |
135440 |
147366 |
282806 |
|
| 306 |
282806 |
▶ | if (best > alpha) alpha = best; |
Branch: 1 branch
| Branch | True | False | Total |
| if (best > alpha) alpha = best; |
82311 |
200495 |
282806 |
|
| 307 |
282806 |
▶ | if (alpha >= beta) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha >= beta) break; |
75790 |
207016 |
282806 |
|
| 308 |
|
| } |
| 309 |
282178 |
| return best; |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
|
| /* ---- the search ---------------------------------------------------------------------- */ |
| 313 |
|
| |
| 314 |
1548971 |
| static int alphabeta(struct shg_board *b, struct search *s, int depth, int alpha, int beta, int ply) |
| 315 |
|
| { |
| 316 |
|
| int moves[SHG_MOVES_MAX], score[SHG_MOVES_MAX]; |
| 317 |
1548971 |
| int n, i, best = -SHG_MATE_SCORE * 2, tt_move = 0, alpha0 = alpha, played = 0, us = b->to_move; |
| 318 |
1548971 |
| unsigned long long key = b->key; |
| 319 |
1548971 |
| struct tt_entry *e = &s->tt[key & (TT_SIZE - 1)]; |
| 320 |
|
| |
| 321 |
1548971 |
| s->nodes++; |
| 322 |
1548971 |
▶ | if (spent(s)) return shg_evaluate(b, s->flags); |
Branch: 1 branch
| Branch | True | False | Total |
| if (spent(s)) return shg_evaluate(b, s->flags); |
128 |
1548843 |
1548971 |
|
| 323 |
1548843 |
▶ | if (ply <= MAX_PLY) s->path[ply] = key; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ply <= MAX_PLY) s->path[ply] = key; |
1548843 |
0 |
1548843 |
|
| 324 |
1548843 |
▶ | if (ply > 0 && repeated(s, key, ply)) return us == s->root_side ? -CONTEMPT : CONTEMPT; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ply > 0 && repeated(s, key, ply)) return us == s->root_side ? -CONTEMPT : CONTEMPT; |
1548843 |
0 |
1548843 |
| if (ply > 0 && repeated(s, key, ply)) return us == s->root_side ? -CONTEMPT : CONTEMPT; |
5296 |
1543547 |
1548843 |
| if (ply > 0 && repeated(s, key, ply)) return us == s->root_side ? -CONTEMPT : CONTEMPT; |
4809 |
487 |
5296 |
|
| 325 |
1543547 |
▶ | if (e->key == key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->key == key) { |
354011 |
1189536 |
1543547 |
|
| 326 |
354011 |
| tt_move = e->move; |
| 327 |
354011 |
▶ | if (e->depth >= depth) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->depth >= depth) { |
252678 |
101333 |
354011 |
|
| 328 |
252678 |
▶ | if (e->flag == TT_EXACT) return e->score; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->flag == TT_EXACT) return e->score; |
2372 |
250306 |
252678 |
|
| 329 |
250306 |
▶ | if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score; |
245899 |
4407 |
250306 |
| if (e->flag == TT_LOWER && e->score > alpha) alpha = e->score; |
245229 |
670 |
245899 |
|
| 330 |
250306 |
▶ | if (e->flag == TT_UPPER && e->score < beta) beta = e->score; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->flag == TT_UPPER && e->score < beta) beta = e->score; |
4407 |
245899 |
250306 |
| if (e->flag == TT_UPPER && e->score < beta) beta = e->score; |
2410 |
1997 |
4407 |
|
| 331 |
250306 |
▶ | if (alpha >= beta) return e->score; |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha >= beta) return e->score; |
247598 |
2708 |
250306 |
|
| 332 |
|
| } |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
|
| /* At the horizon the loop below never runs. A side in check is tested |
| 336 |
|
| * exactly, since a missed mate is the one error this search may not make; |
| 337 |
|
| * then, unless switched off, a mate in one for the side to move. */ |
| 338 |
1293577 |
▶ | if (depth <= 0 || ply >= MAX_PLY - 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth <= 0 || ply >= MAX_PLY - 1) { |
270889 |
1022688 |
1293577 |
| if (depth <= 0 || ply >= MAX_PLY - 1) { |
0 |
270889 |
270889 |
|
| 339 |
1022688 |
▶ | if (shg_in_check(b, us) && !shg_has_legal_move(b)) return -SHG_MATE_SCORE + ply; |
Branch: 2 branches
| Branch | True | False | Total |
| if (shg_in_check(b, us) && !shg_has_legal_move(b)) return -SHG_MATE_SCORE + ply; |
10915 |
1011773 |
1022688 |
| if (shg_in_check(b, us) && !shg_has_legal_move(b)) return -SHG_MATE_SCORE + ply; |
9 |
10906 |
10915 |
|
| 340 |
1022679 |
▶ | if (!(s->flags & SHG_SEARCH_NO_MATE) && mates_in_one(b, s)) return SHG_MATE_SCORE - ply - 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(s->flags & SHG_SEARCH_NO_MATE) && mates_in_one(b, s)) return SHG_MATE_SCORE - ply - 1; |
1022679 |
0 |
1022679 |
| if (!(s->flags & SHG_SEARCH_NO_MATE) && mates_in_one(b, s)) return SHG_MATE_SCORE - ply - 1; |
5021 |
1017658 |
1022679 |
|
| 341 |
1017658 |
| return quiesce(b, s, alpha, beta, ply); |
| 342 |
|
| } |
| 343 |
|
| |
| 344 |
270889 |
| n = generate(b, moves); |
| 345 |
4979259 |
▶ | for (i = 0; i < n; i++) score[i] = order_score(b, s, moves[i], tt_move, ply); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) score[i] = order_score(b, s, moves[i], tt_move, ply); |
4708370 |
270889 |
4979259 |
|
| 346 |
1617835 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
1535306 |
82529 |
1617835 |
|
| 347 |
|
| struct shg_undo u; |
| 348 |
|
| int v; |
| 349 |
1535306 |
| pick(moves, score, i, n); |
| 350 |
1543467 |
▶ | if (!shg_do_move(b, moves[i], &u)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(b, moves[i], &u)) continue; |
0 |
1535306 |
1535306 |
|
| 351 |
1535306 |
▶ | if (!legal_after(b, us, moves[i])) { shg_undo_move(b, &u); continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!legal_after(b, us, moves[i])) { shg_undo_move(b, &u); continue; } |
8161 |
1527145 |
1535306 |
|
| 352 |
1527145 |
| played++; |
| 353 |
1527145 |
| v = -alphabeta(b, s, depth - 1, -beta, -alpha, ply + 1); |
| 354 |
1527145 |
| shg_undo_move(b, &u); |
| 355 |
1527145 |
▶ | if (s->stopped) return best > -SHG_MATE_SCORE * 2 ? best : shg_evaluate(b, s->flags); |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->stopped) return best > -SHG_MATE_SCORE * 2 ? best : shg_evaluate(b, s->flags); |
859 |
1526286 |
1527145 |
| if (s->stopped) return best > -SHG_MATE_SCORE * 2 ? best : shg_evaluate(b, s->flags); |
403 |
456 |
859 |
|
| 356 |
1526286 |
▶ | if (v > best) { best = v; tt_move = moves[i]; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > best) { best = v; tt_move = moves[i]; } |
323234 |
1203052 |
1526286 |
|
| 357 |
1526286 |
▶ | if (best > alpha) alpha = best; |
Branch: 1 branch
| Branch | True | False | Total |
| if (best > alpha) alpha = best; |
212700 |
1313586 |
1526286 |
|
| 358 |
1526286 |
▶ | if (alpha >= beta) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (alpha >= beta) { |
187501 |
1338785 |
1526286 |
|
| 359 |
187501 |
▶ | if (!SHG_MOVE_DROP(moves[i]) && b->cell[SHG_SQ_OF[SHG_MOVE_TO(moves[i])]] == SHG_EMPTY |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SHG_MOVE_DROP(moves[i]) && b->cell[SHG_SQ_OF[SHG_MOVE_TO(moves[i])]] == SHG_EMPTY |
187497 |
4 |
187501 |
| if (!SHG_MOVE_DROP(moves[i]) && b->cell[SHG_SQ_OF[SHG_MOVE_TO(moves[i])]] == SHG_EMPTY |
176022 |
11475 |
187497 |
|
| 360 |
176022 |
▶ | && ply < MAX_PLY && s->killer[ply][0] != moves[i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| && ply < MAX_PLY && s->killer[ply][0] != moves[i]) { |
176022 |
0 |
176022 |
| && ply < MAX_PLY && s->killer[ply][0] != moves[i]) { |
13168 |
162854 |
176022 |
|
| 361 |
13168 |
| s->killer[ply][1] = s->killer[ply][0]; |
| 362 |
13168 |
| s->killer[ply][0] = moves[i]; |
| 363 |
|
| } |
| 364 |
187501 |
| break; |
| 365 |
|
| } |
| 366 |
|
| } |
| 367 |
|
| /* NO LEGAL MOVE IS A LOSS, in check or not (phase 04). Nearer is worse. */ |
| 368 |
270030 |
▶ | if (!played) return -SHG_MATE_SCORE + ply; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!played) return -SHG_MATE_SCORE + ply; |
7 |
270023 |
270030 |
|
| 369 |
|
| |
| 370 |
|
| /* FAIL-SOFT: best is returned, never alpha. */ |
| 371 |
270023 |
| e->key = key; |
| 372 |
270023 |
| e->move = tt_move; |
| 373 |
270023 |
▶ | e->score = (short) (best > 32000 ? 32000 : best < -32000 ? -32000 : best); |
Branch: 2 branches
| Branch | True | False | Total |
| e->score = (short) (best > 32000 ? 32000 : best < -32000 ? -32000 : best); |
270023 |
0 |
270023 |
| e->score = (short) (best > 32000 ? 32000 : best < -32000 ? -32000 : best); |
270023 |
0 |
270023 |
|
| 374 |
270023 |
| e->depth = (signed char) depth; |
| 375 |
270023 |
▶ | e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER : best >= beta ? TT_LOWER : TT_EXACT); |
Branch: 2 branches
| Branch | True | False | Total |
| e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER : best >= beta ? TT_LOWER : TT_EXACT); |
197880 |
72143 |
270023 |
| e->flag = (unsigned char) (best <= alpha0 ? TT_UPPER : best >= beta ? TT_LOWER : TT_EXACT); |
187501 |
10379 |
197880 |
|
| 376 |
270023 |
| return best; |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| /* The root list is shuffled by the seed once, before anything is searched; |
| 380 |
|
| * splitmix32, so a search is the same everywhere given its seed. */ |
| 381 |
288 |
| static void shuffle(int *moves, int n, unsigned int seed) |
| 382 |
|
| { |
| 383 |
|
| int i; |
| 384 |
288 |
▶ | unsigned int x = seed ? seed : 0x9E3779B9u; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int x = seed ? seed : 0x9E3779B9u; |
280 |
8 |
288 |
|
| 385 |
9383 |
▶ | for (i = n - 1; i > 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = n - 1; i > 0; i--) { |
9095 |
288 |
9383 |
|
| 386 |
|
| unsigned int z; |
| 387 |
|
| int j, t; |
| 388 |
9095 |
| x += 0x9E3779B9u; |
| 389 |
9095 |
| z = x; |
| 390 |
9095 |
| z = (z ^ (z >> 16)) * 0x21F0AAADu; |
| 391 |
9095 |
| z = (z ^ (z >> 15)) * 0x735A2D97u; |
| 392 |
9095 |
| z = z ^ (z >> 15); |
| 393 |
9095 |
| j = (int) (z % (unsigned int) (i + 1)); |
| 394 |
9095 |
| t = moves[i]; moves[i] = moves[j]; moves[j] = t; |
| 395 |
|
| } |
| 396 |
288 |
| } |
| 397 |
|
| |
| 398 |
289 |
| int shg_search(const struct shg_board *start, unsigned long long budget, int max_depth, |
| 399 |
|
| unsigned int seed, int flags, const unsigned long long *history, |
| 400 |
|
| int n_history, struct shg_search_info *info) |
| 401 |
|
| { |
| 402 |
|
| int moves[SHG_MOVES_MAX]; |
| 403 |
289 |
| int n, i, depth, best_move, best_score = 0; |
| 404 |
|
| struct search s; |
| 405 |
|
| struct shg_board b; |
| 406 |
|
| |
| 407 |
289 |
▶ | if (info) { info->nodes = 0; info->depth = 0; info->score = 0; info->stopped = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (info) { info->nodes = 0; info->depth = 0; info->score = 0; info->stopped = 0; } |
289 |
0 |
289 |
|
| 408 |
289 |
▶ | if (!start) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!start) return 0; |
0 |
289 |
289 |
|
| 409 |
289 |
| n = shg_legal(start, moves, SHG_MOVES_MAX); |
| 410 |
289 |
▶ | if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; |
0 |
289 |
289 |
|
| 411 |
289 |
▶ | if (n == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return 0; |
1 |
288 |
289 |
|
| 412 |
|
| |
| 413 |
288 |
| b = *start; |
| 414 |
288 |
| memset(&s, 0, sizeof s); |
| 415 |
288 |
▶ | s.budget = budget ? budget : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| s.budget = budget ? budget : 1; |
288 |
0 |
288 |
|
| 416 |
288 |
| s.flags = flags; |
| 417 |
288 |
| s.history = history; |
| 418 |
288 |
▶ | s.n_history = history && n_history > 0 ? n_history : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| s.n_history = history && n_history > 0 ? n_history : 0; |
170 |
118 |
288 |
| s.n_history = history && n_history > 0 ? n_history : 0; |
170 |
0 |
170 |
|
| 419 |
288 |
| s.path[0] = b.key; |
| 420 |
288 |
| s.root_side = b.to_move; |
| 421 |
288 |
| s.tt = (struct tt_entry *) calloc(TT_SIZE, sizeof(struct tt_entry)); |
| 422 |
|
| |
| 423 |
288 |
| shuffle(moves, n, seed); |
| 424 |
|
| /* Then a stable sort by kind of move: captures and promotions before quiet |
| 425 |
|
| * moves, drops last. Stable, so moves of one kind keep the shuffled order and |
| 426 |
|
| * a tie within a kind is still a fair draw; and a budget too small for one |
| 427 |
|
| * iteration has looked at taking material first. */ |
| 428 |
|
| { |
| 429 |
|
| int sc[SHG_MOVES_MAX], j; |
| 430 |
9671 |
▶ | for (i = 0; i < n; i++) sc[i] = order_score(&b, &s, moves[i], 0, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) sc[i] = order_score(&b, &s, moves[i], 0, 0); |
9383 |
288 |
9671 |
|
| 431 |
9383 |
▶ | for (i = 1; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < n; i++) { |
9095 |
288 |
9383 |
|
| 432 |
9095 |
| int m = moves[i], v = sc[i]; |
| 433 |
76720 |
▶ | for (j = i - 1; j >= 0 && sc[j] < v; j--) { moves[j + 1] = moves[j]; sc[j + 1] = sc[j]; } |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = i - 1; j >= 0 && sc[j] < v; j--) { moves[j + 1] = moves[j]; sc[j + 1] = sc[j]; } |
76463 |
257 |
76720 |
| for (j = i - 1; j >= 0 && sc[j] < v; j--) { moves[j + 1] = moves[j]; sc[j + 1] = sc[j]; } |
67625 |
8838 |
76463 |
|
| 434 |
9095 |
| moves[j + 1] = m; sc[j + 1] = v; |
| 435 |
|
| } |
| 436 |
|
| } |
| 437 |
288 |
| best_move = moves[0]; |
| 438 |
288 |
▶ | if (!s.tt) return best_move; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s.tt) return best_move; |
0 |
288 |
288 |
|
| 439 |
288 |
▶ | if (max_depth < 1 || max_depth > MAX_PLY - 2) max_depth = MAX_PLY - 2; |
Branch: 2 branches
| Branch | True | False | Total |
| if (max_depth < 1 || max_depth > MAX_PLY - 2) max_depth = MAX_PLY - 2; |
12 |
276 |
288 |
| if (max_depth < 1 || max_depth > MAX_PLY - 2) max_depth = MAX_PLY - 2; |
0 |
12 |
12 |
|
| 440 |
|
| |
| 441 |
1236 |
▶ | for (depth = 1; depth <= max_depth; depth++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (depth = 1; depth <= max_depth; depth++) { |
1226 |
10 |
1236 |
|
| 442 |
1226 |
| int local_best = -SHG_MATE_SCORE * 2, local_move = best_move, alpha = -SHG_MATE_SCORE * 2; |
| 443 |
|
| /* The last iteration's move first; the rest keep the shuffled order. */ |
| 444 |
24655 |
▶ | for (i = 1; i < n; i++) if (moves[i] == best_move) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 1; i < n; i++) if (moves[i] == best_move) { |
151 |
23429 |
23580 |
| for (i = 1; i < n; i++) if (moves[i] == best_move) { |
23580 |
1075 |
24655 |
|
| 445 |
151 |
| int t = moves[i]; |
| 446 |
151 |
| memmove(moves + 1, moves, (size_t) i * sizeof(int)); |
| 447 |
151 |
| moves[0] = t; |
| 448 |
151 |
| break; |
| 449 |
|
| } |
| 450 |
22779 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
21826 |
953 |
22779 |
|
| 451 |
|
| struct shg_undo u; |
| 452 |
|
| int v; |
| 453 |
21826 |
| shg_do_move(&b, moves[i], &u); |
| 454 |
21826 |
| v = -alphabeta(&b, &s, depth - 1, -SHG_MATE_SCORE * 2, -alpha, 1); |
| 455 |
21826 |
| shg_undo_move(&b, &u); |
| 456 |
21826 |
▶ | if (s.stopped) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.stopped) break; |
273 |
21553 |
21826 |
|
| 457 |
|
| /* STRICTLY GREATER: a tie is never picked. The first move to reach |
| 458 |
|
| * the best score keeps it, and the shuffle made "first" a fair draw. */ |
| 459 |
21553 |
▶ | if (v > local_best) { local_best = v; local_move = moves[i]; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > local_best) { local_best = v; local_move = moves[i]; } |
1263 |
20290 |
21553 |
|
| 460 |
21553 |
▶ | if (local_best > alpha) alpha = local_best; |
Branch: 1 branch
| Branch | True | False | Total |
| if (local_best > alpha) alpha = local_best; |
1263 |
20290 |
21553 |
|
| 461 |
|
| } |
| 462 |
|
| /* A STOPPED ITERATION STILL COUNTS for every root move it finished. Each |
| 463 |
|
| * improvement above is an exact score (the child's window is open below, |
| 464 |
|
| * so only a fail-high returns a bound, and a fail-high is never an |
| 465 |
|
| * improvement), and the last iteration's move was searched first, so |
| 466 |
|
| * whatever beat it really is better at this depth. Only an iteration |
| 467 |
|
| * that finished no move at all is thrown away. */ |
| 468 |
1226 |
▶ | if (s.stopped) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.stopped) { |
273 |
953 |
1226 |
|
| 469 |
273 |
▶ | if (local_best > -SHG_MATE_SCORE * 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (local_best > -SHG_MATE_SCORE * 2) { |
121 |
152 |
273 |
|
| 470 |
121 |
| best_move = local_move; |
| 471 |
121 |
| best_score = local_best; |
| 472 |
|
| } |
| 473 |
273 |
| break; |
| 474 |
|
| } |
| 475 |
953 |
| best_move = local_move; |
| 476 |
953 |
| best_score = local_best; |
| 477 |
953 |
▶ | if (info) { info->depth = depth; info->score = best_score; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (info) { info->depth = depth; info->score = best_score; } |
953 |
0 |
953 |
|
| 478 |
953 |
▶ | if (best_score > SHG_MATE_BOUND || best_score < -SHG_MATE_BOUND) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (best_score > SHG_MATE_BOUND || best_score < -SHG_MATE_BOUND) break; |
948 |
5 |
953 |
| if (best_score > SHG_MATE_BOUND || best_score < -SHG_MATE_BOUND) break; |
948 |
0 |
948 |
|
| 479 |
|
| } |
| 480 |
288 |
▶ | if (info) { info->nodes = s.nodes; info->stopped = s.stopped; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (info) { info->nodes = s.nodes; info->stopped = s.stopped; } |
288 |
0 |
288 |
|
| 481 |
288 |
| free(s.tt); |
| 482 |
288 |
| return best_move; |
| 483 |
|
| } |