sth_search.c

filter: Statementstmt 100% 253 / 253 Branchbran 84.9% 248 / 292 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.9% 501 / 545 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sth_rules.c ↑ Summary bin/sternhalma »
1 /* sth_search.c - an evaluation, and a search bounded in positions looked at.
2 *
3 * PERL-FREE by design: see include/sth_abi.h.
4 *
5 * THIS FILE'S SCOPE, version 6. How good a position is for a seat, in
6 * integers; the move a seat should make, by alpha-beta for two seats racing
7 * and by a paranoid search or max-n for more; and the weakest player there
8 * is, a random choice among the moves that go forward.
9 *
10 * A BUDGET IS POSITIONS, NEVER SECONDS, so a test asks the same thing on a
11 * fast machine and a slow one. The first iteration always finishes; after
12 * that, a search that runs out plays the best move of the last iteration it
13 * finished, never the first move in the list.
14 */
15  
16 #include <stdlib.h>
17 #include <string.h>
18 #include "sth_internal.h"
19  
20 /* ---- tables for the evaluation --------------------------------------------- */
21  
22 static int TIP[STH_POINTS]; /* each point's tip hole */
23 static int DIST[STH_POINTS][STH_HOLES]; /* hex distance to that tip */
24 static int HOME_CLS[2][STH_POINTS][STH_CLASSES]; /* 10- and 15-hole homes */
25 static int IN_HOME[2][STH_POINTS][STH_HOLES];
26 static int SEARCH_READY = 0;
27  
28 3413565421 static int hexd(int a, int b)
29 {
30 3413565421 int dx = STH_HX[a] - STH_HX[b], dy = STH_HY[a] - STH_HY[b], dz = -dx - dy;
31 3413565421 ▶ if (dx < 0) dx = -dx;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) dx = -dx; 1530265235 1883300186 3413565421
32 3413565421 ▶ if (dy < 0) dy = -dy;
Branch: 1 branch
BranchTrueFalseTotal
if (dy < 0) dy = -dy; 1493510709 1920054712 3413565421
33 3413565421 ▶ if (dz < 0) dz = -dz;
Branch: 1 branch
BranchTrueFalseTotal
if (dz < 0) dz = -dz; 1490396500 1923168921 3413565421
34 3413565421 return (dx + dy + dz) / 2;
35 }
36  
37 791567617 static int cls_of(int h)
38 {
39 791567617 return ((STH_HX[h] + 8) & 1) | (((STH_HY[h] + 8) & 1) << 1);
40 }
41  
42 366372 static void search_init(void)
43 {
44 int p, h, i, k, n;
45 366372 ▶ if (SEARCH_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (SEARCH_READY) return; 366363 9 366372
46 9 sth_tables_init();
47 63 ▶ for (p = 0; p < STH_POINTS; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < STH_POINTS; p++) { 54 9 63
48 54 TIP[p] = STH_POINT_LIST[p][0];
49 594 ▶ for (i = 0; i < STH_POINT_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) { 540 54 594
50 540 n = 0;
51 3780 ▶ for (k = 0; k < STH_DIRS; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < STH_DIRS; k++) 3240 540 3780
52 3240 n += STH_HOLE_AT[STH_SQ_OF[STH_POINT_LIST[p][i]] + STH_STEP[k]] >= 0;
53 540 ▶ if (n == 2) TIP[p] = STH_POINT_LIST[p][i];
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) TIP[p] = STH_POINT_LIST[p][i]; 54 486 540
54 }
55 6588 ▶ for (h = 0; h < STH_HOLES; h++) DIST[p][h] = hexd(h, TIP[p]);
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) DIST[p][h] = hexd(h, TIP[p]); 6534 54 6588
56 54 memset(HOME_CLS[0][p], 0, sizeof HOME_CLS[0][p]);
57 54 memset(HOME_CLS[1][p], 0, sizeof HOME_CLS[1][p]);
58 54 memset(IN_HOME[0][p], 0, sizeof IN_HOME[0][p]);
59 54 memset(IN_HOME[1][p], 0, sizeof IN_HOME[1][p]);
60 594 ▶ for (i = 0; i < STH_POINT_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) { 540 54 594
61 540 h = STH_POINT_LIST[p][i];
62 540 HOME_CLS[0][p][cls_of(h)]++;
63 540 HOME_CLS[1][p][cls_of(h)]++;
64 540 IN_HOME[0][p][h] = IN_HOME[1][p][h] = 1;
65 }
66 324 ▶ for (i = 0; i < STH_FRONT_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_FRONT_HOLES; i++) { 270 54 324
67 270 h = STH_FRONT[p][i];
68 270 HOME_CLS[1][p][cls_of(h)]++;
69 270 IN_HOME[1][p][h] = 1;
70 }
71 }
72 9 SEARCH_READY = 1;
73 }
74  
75 729416 void sth_weights_default(struct sth_weights *w)
76 {
77 729416 memset(w, 0, sizeof *w);
78 729416 w->distance = STH_W_DISTANCE;
79 729416 w->straggler = STH_W_STRAGGLER;
80 729416 w->classes = STH_W_CLASSES;
81 729416 w->blocking = STH_W_BLOCKING;
82 729416 }
83  
84 81412991 static int home_of(const struct sth_board *b, int seat)
85 {
86 81412991 return (STH_LAYOUT[b->seats][seat] + 3) % STH_POINTS;
87 }
88  
89 219024473 static int in_list(const int *list, int n, int x)
90 {
91 int i;
92 221355321 ▶ for (i = 0; i < n; i++) if (list[i] == x) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (list[i] == x) return 1; 624374 2330848 2955222
for (i = 0; i < n; i++) if (list[i] == x) return 1; 2955222 218400099 221355321
93 218400099 return 0;
94 }
95  
96 58601279 static int racing(const struct sth_board *b, int seat)
97 {
98 58601279 ▶ return !in_list(b->finished, b->finished_n, seat) && !in_list(b->resigned, b->resigned_n, seat);
Branch: 2 branches
BranchTrueFalseTotal
return !in_list(b->finished, b->finished_n, seat) && !in_list(b->resigned, b->resigned_n, seat); 58289732 311547 58601279
return !in_list(b->finished, b->finished_n, seat) && !in_list(b->resigned, b->resigned_n, seat); 58289732 0 58289732
99 }
100  
101 /* EVERY SEAT'S SCORE, higher better. A seat home scores STH_WIN_SCORE.
102 * Otherwise minus the weighted sum of four things a racing seat wants small:
103 *
104 * distance for a piece in its home, its distance to the home's tip; for
105 * a piece outside, the cheapest way into an empty home hole,
106 * the walk there and that hole's own distance to the tip; all
107 * less what a full home sums to, so 0 is home. Measuring an
108 * outside piece to the tip instead left a seat with one piece
109 * out and the empty hole at the edge of its home with nothing
110 * to gain by walking toward it, and four-seat games stalled
111 * there for thousands of plies (07)
112 * straggler the distance of its rearmost piece to the tip: a piece left
113 * behind has nothing to hop over later
114 * classes how far its pieces' hop classes are from its home's
115 * blocking pairs of a piece in the home and an empty hole deeper in */
116 20934357 static void evaluate_all(const struct sth_board *b, const struct sth_weights *w, int *score)
117 {
118 20934357 int s, h, k, big = b->pieces == STH_PIECES_MAX;
119 int at[STH_SEATS_MAX][STH_PIECES_MAX], n[STH_SEATS_MAX];
120 20934357 memset(n, 0, sizeof n);
121 2553991554 ▶ for (h = 0; h < STH_HOLES; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) { 2533057197 20934357 2553991554
122 2533057197 int c = b->cell[STH_SQ_OF[h]];
123 2533057197 ▶ if (STH_IS_PIECE(c) && n[c] < STH_PIECES_MAX) at[c][n[c]++] = h;
Branch: 2 branches
BranchTrueFalseTotal
if (STH_IS_PIECE(c) && n[c] < STH_PIECES_MAX) at[c][n[c]++] = h; 794690057 1738367140 2533057197
if (STH_IS_PIECE(c) && n[c] < STH_PIECES_MAX) at[c][n[c]++] = h; 794690057 0 794690057
124 }
125 100859347 ▶ for (s = 0; s < b->seats; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < b->seats; s++) { 79924990 20934357 100859347
126 79924990 int p = home_of(b, s), floor = 0, mismatch = 0, blocking = 0, sum = 0, worst = 0, i, j;
127 79924990 ▶ int nholes = big ? STH_POINT_HOLES + STH_FRONT_HOLES : STH_POINT_HOLES;
Branch: 1 branch
BranchTrueFalseTotal
int nholes = big ? STH_POINT_HOLES + STH_FRONT_HOLES : STH_POINT_HOLES; 0 79924990 79924990
128 79924990 int holes[STH_POINT_HOLES + STH_FRONT_HOLES], free_[STH_POINT_HOLES + STH_FRONT_HOLES], nfree = 0;
129 int cls[STH_CLASSES];
130 79924990 ▶ if (in_list(b->finished, b->finished_n, s)) { score[s] = STH_WIN_SCORE; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (in_list(b->finished, b->finished_n, s)) { score[s] = STH_WIN_SCORE; continue; } 312379 79612611 79924990
131 79612611 memset(cls, 0, sizeof cls);
132 875738721 ▶ for (i = 0; i < STH_POINT_HOLES; i++) holes[i] = STH_POINT_LIST[p][i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) holes[i] = STH_POINT_LIST[p][i]; 796126110 79612611 875738721
133 79612611 ▶ if (big) for (i = 0; i < STH_FRONT_HOLES; i++) holes[STH_POINT_HOLES + i] = STH_FRONT[p][i];
Branch: 2 branches
BranchTrueFalseTotal
if (big) for (i = 0; i < STH_FRONT_HOLES; i++) holes[STH_POINT_HOLES + i] = STH_FRONT[p][i]; 0 79612611 79612611
if (big) for (i = 0; i < STH_FRONT_HOLES; i++) holes[STH_POINT_HOLES + i] = STH_FRONT[p][i]; 0 0 0
134 875738721 ▶ for (i = 0; i < nholes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nholes; i++) { 796126110 79612611 875738721
135 796126110 floor += DIST[p][holes[i]];
136 796126110 ▶ if (b->cell[STH_SQ_OF[holes[i]]] == STH_EMPTY) free_[nfree++] = holes[i];
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[STH_SQ_OF[holes[i]]] == STH_EMPTY) free_[nfree++] = holes[i]; 385178036 410948074 796126110
137 }
138 871178878 ▶ for (k = 0; k < n[s]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n[s]; k++) { 791566267 79612611 871178878
139 791566267 int piece = at[s][k], cost = DIST[p][piece];
140 791566267 ▶ if (!IN_HOME[big][p][piece] && nfree) {
Branch: 2 branches
BranchTrueFalseTotal
if (!IN_HOME[big][p][piece] && nfree) { 698042626 93523641 791566267
if (!IN_HOME[big][p][piece] && nfree) { 693544236 4498390 698042626
141 693544236 int best = -1;
142 4107103123 ▶ for (j = 0; j < nfree; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nfree; j++) { 3413558887 693544236 4107103123
143 3413558887 int c = hexd(piece, free_[j]) + DIST[p][free_[j]];
144 3413558887 ▶ if (best < 0 || c < best) best = c;
Branch: 2 branches
BranchTrueFalseTotal
if (best < 0 || c < best) best = c; 2720014651 693544236 3413558887
if (best < 0 || c < best) best = c; 87321902 2632692749 2720014651
145 }
146 693544236 cost = best;
147 }
148 791566267 sum += cost;
149 791566267 ▶ if (DIST[p][piece] > worst) worst = DIST[p][piece];
Branch: 1 branch
BranchTrueFalseTotal
if (DIST[p][piece] > worst) worst = DIST[p][piece]; 259677276 531888991 791566267
150 791566267 cls[cls_of(piece)]++;
151 }
152 398063055 ▶ for (k = 0; k < STH_CLASSES; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < STH_CLASSES; k++) { 318450444 79612611 398063055
153 318450444 int dk = cls[k] - HOME_CLS[big][p][k];
154 318450444 mismatch += dk < 0 ? -dk : dk;
155 }
156 464790647 ▶ for (i = 0; i < nfree; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nfree; i++) 385178036 79612611 464790647
157 4236958396 ▶ for (j = 0; j < nholes; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nholes; j++) 3851780360 385178036 4236958396
158 3851780360 ▶ if (b->cell[STH_SQ_OF[holes[j]]] == s && DIST[p][holes[j]] > DIST[p][free_[i]]) blocking++;
Branch: 2 branches
BranchTrueFalseTotal
if (b->cell[STH_SQ_OF[holes[j]]] == s && DIST[p][holes[j]] > DIST[p][free_[i]]) blocking++; 393133416 3458646944 3851780360
if (b->cell[STH_SQ_OF[holes[j]]] == s && DIST[p][holes[j]] > DIST[p][free_[i]]) blocking++; 108089020 285044396 393133416
159 79612611 score[s] = -(w->distance * (sum - floor) + w->straggler * worst
160 79612611 + w->classes * mismatch + w->blocking * blocking);
161 }
162 20934357 }
163  
164 361958 int sth_evaluate(const struct sth_board *b, int seat, const struct sth_weights *w)
165 {
166 int score[STH_SEATS_MAX];
167 361958 search_init();
168 361958 ▶ if (!b || seat < 0 || seat >= b->seats) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || seat < 0 || seat >= b->seats) return 0; 361958 0 361958
if (!b || seat < 0 || seat >= b->seats) return 0; 361958 0 361958
if (!b || seat < 0 || seat >= b->seats) return 0; 0 361958 361958
169 361958 evaluate_all(b, w, score);
170 361958 return score[seat];
171 }
172  
173 /* A position's value to one seat: its own score less the best score of a
174 * rival still racing. Two seats: the difference. More: the race for the next
175 * place is against whoever is nearest. */
176 20576979 static int value_for(const struct sth_board *b, const int *score, int seat)
177 {
178 20576979 int r, best = 0, any = 0;
179 99755237 ▶ for (r = 0; r < b->seats; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < b->seats; r++) { 79178258 20576979 99755237
180 79178258 ▶ if (r == seat || !racing(b, r)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r == seat || !racing(b, r)) continue; 58601279 20576979 79178258
if (r == seat || !racing(b, r)) continue; 311547 58289732 58601279
181 58289732 ▶ if (!any || score[r] > best) best = score[r];
Branch: 2 branches
BranchTrueFalseTotal
if (!any || score[r] > best) best = score[r]; 37712753 20576979 58289732
if (!any || score[r] > best) best = score[r]; 9652754 28059999 37712753
182 58289732 any = 1;
183 }
184 20576979 ▶ return any ? score[seat] - best : score[seat];
Branch: 1 branch
BranchTrueFalseTotal
return any ? score[seat] - best : score[seat]; 20576979 0 20576979
185 }
186  
187 /* A SEAT PLACED, scored by its place. Home is always worth more than any
188 * position in which the seat is still racing, and an earlier place more than
189 * a later one; a game that ends with the seat still out is worth less than
190 * anything. The first version scored places from a win for first to a loss
191 * for last, linearly: so third of four, home, came out worse than racing on,
192 * and a seat one step from home stepped back and forth for ever rather than
193 * go in (07: every four- and six-seat stall). Sooner is better and later
194 * worse, by the plies it is away. */
195 11408 static int placed_value(const struct sth_board *b, int seat, int ply)
196 {
197 11408 int place = sth_place_of(b, seat);
198 11408 ▶ if (place <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (place <= 0) return 0; 0 11408 11408
199 11408 ▶ if (in_list(b->finished, b->finished_n, seat))
Branch: 1 branch
BranchTrueFalseTotal
if (in_list(b->finished, b->finished_n, seat)) 356 11052 11408
200 356 return STH_WIN_SCORE + (b->seats - place) * STH_PLACE_STEP - ply;
201 11052 return -STH_WIN_SCORE + (b->seats - place) * STH_PLACE_STEP + ply;
202 }
203  
204 /* ---- the search -------------------------------------------------------------- */
205  
206 struct tt_entry {
207 unsigned long long key;
208 int score;
209 short depth;
210 short flag; /* 1 exact, 2 lower bound, 3 upper bound */
211 short from, to;
212 };
213  
214 #define TT_BITS 16
215 #define TT_SIZE (1 << TT_BITS)
216  
217 struct ctx {
218 const struct sth_weights *w;
219 unsigned long long budget, nodes, seed;
220 int root, stopped, mode;
221 struct tt_entry *tt;
222 };
223  
224 188192 static unsigned long long mix(unsigned long long x)
225 {
226 188192 x ^= x >> 33; x *= 0xff51afd7ed558ccdULL; x ^= x >> 33; x *= 0xc4ceb9fe1a85ec53ULL; x ^= x >> 33;
227 188192 return x;
228 }
229  
230 186528 static unsigned long long tie(const struct ctx *c, int from, int to)
231 {
232 186528 return mix(c->seed * 0x9E3779B97F4A7C15ULL + (unsigned long long) (from * 131 + to + 1));
233 }
234  
235 /* Moves of the seat to move, best-looking first: the ones that take a piece
236 * furthest toward home. A pass is from = -1. */
237 1486449 static int ordered_moves(const struct sth_board *b, int *from, int *to, int prefer_from, int prefer_to)
238 {
239 static int gain[STH_LEGAL_MAX];
240 int n, i, j, p, g, f, t;
241 1486449 n = sth_legal(b, from, to, STH_LEGAL_MAX);
242 1486449 ▶ if (n > STH_LEGAL_MAX) n = STH_LEGAL_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_LEGAL_MAX) n = STH_LEGAL_MAX; 0 1486449 1486449
243 1486449 ▶ if (n == 0) { from[0] = to[0] = -1; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { from[0] = to[0] = -1; return 1; } 112 1486337 1486449
244 1486337 p = home_of(b, b->to_move);
245 66242016 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 64755679 1486337 66242016
246 64755679 gain[i] = DIST[p][from[i]] - DIST[p][to[i]];
247 64755679 ▶ if (from[i] == prefer_from && to[i] == prefer_to) gain[i] = 1000;
Branch: 2 branches
BranchTrueFalseTotal
if (from[i] == prefer_from && to[i] == prefer_to) gain[i] = 1000; 609927 64145752 64755679
if (from[i] == prefer_from && to[i] == prefer_to) gain[i] = 1000; 115650 494277 609927
248 }
249 64755679 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 63269342 1486337 64755679
250 63269342 g = gain[i]; f = from[i]; t = to[i];
251 704326312 ▶ for (j = i; j > 0 && gain[j - 1] < g; j--) { gain[j] = gain[j - 1]; from[j] = from[j - 1]; to[j] = to[j - 1]; }
Branch: 2 branches
BranchTrueFalseTotal
for (j = i; j > 0 && gain[j - 1] < g; j--) { gain[j] = gain[j - 1]; from[j] = from[j - 1]; to[j] = to[j - 1]; } 701060368 3265944 704326312
for (j = i; j > 0 && gain[j - 1] < g; j--) { gain[j] = gain[j - 1]; from[j] = from[j - 1]; to[j] = to[j - 1]; } 641056970 60003398 701060368
252 63269342 gain[j] = g; from[j] = f; to[j] = t;
253 }
254 1486337 return n;
255 }
256  
257 22330626 static int apply(struct sth_board *b, int from, int to, struct sth_undo *u)
258 {
259 22330626 ▶ if (from < 0) return sth_pass(b, u) == STH_PLAY_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (from < 0) return sth_pass(b, u) == STH_PLAY_OK; 112 22330514 22330626
260 22330514 return sth_play_trusted(b, from, to, u) == STH_PLAY_OK;
261 }
262  
263 20581517 static int leaf(const struct sth_board *b, const struct ctx *c, int ply)
264 {
265 int score[STH_SEATS_MAX];
266 20581517 ▶ if (b->over || in_list(b->finished, b->finished_n, c->root)) return placed_value(b, c->root, ply);
Branch: 2 branches
BranchTrueFalseTotal
if (b->over || in_list(b->finished, b->finished_n, c->root)) return placed_value(b, c->root, ply); 20570180 11337 20581517
if (b->over || in_list(b->finished, b->finished_n, c->root)) return placed_value(b, c->root, ply); 71 20570109 20570180
267 20570109 evaluate_all(b, c->w, score);
268 20570109 return value_for(b, score, c->root);
269 }
270  
271 /* Alpha-beta over the root seat's value: the root maximises, every other seat
272 * minimises it. With two seats that is the plain game; with more it is the
273 * paranoid assumption, every rival against the root. */
274 22201510 static int ab(struct sth_board *b, struct ctx *c, int depth, int alpha, int beta, int ply)
275 {
276 int from[STH_LEGAL_MAX], to[STH_LEGAL_MAX], n, i, best, maximise, v;
277 22201510 int pf = -2, pt = -2, bf = -2, bt = -2, a0 = alpha, b0 = beta;
278 struct tt_entry *e;
279 struct sth_undo u;
280  
281 22201510 c->nodes++;
282 22201510 ▶ if (depth == 0 || b->over || in_list(b->finished, b->finished_n, c->root)) return leaf(b, c, ply);
Branch: 3 branches
BranchTrueFalseTotal
if (depth == 0 || b->over || in_list(b->finished, b->finished_n, c->root)) return leaf(b, c, ply); 1630041 20571469 22201510
if (depth == 0 || b->over || in_list(b->finished, b->finished_n, c->root)) return leaf(b, c, ply); 1620014 10027 1630041
if (depth == 0 || b->over || in_list(b->finished, b->finished_n, c->root)) return leaf(b, c, ply); 21 1619993 1620014
283 1619993 ▶ if (c->nodes > c->budget) { c->stopped = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->nodes > c->budget) { c->stopped = 1; return 0; } 969 1619024 1619993
284  
285 1619024 e = &c->tt[b->key & (TT_SIZE - 1)];
286 1619024 ▶ if (e->key == b->key && e->depth >= depth) {
Branch: 2 branches
BranchTrueFalseTotal
if (e->key == b->key && e->depth >= depth) { 253959 1365065 1619024
if (e->key == b->key && e->depth >= depth) { 142271 111688 253959
287 142271 ▶ if (e->flag == 1) return e->score;
Branch: 1 branch
BranchTrueFalseTotal
if (e->flag == 1) return e->score; 1037 141234 142271
288 141234 ▶ if (e->flag == 2 && e->score >= beta) return e->score;
Branch: 2 branches
BranchTrueFalseTotal
if (e->flag == 2 && e->score >= beta) return e->score; 70861 70373 141234
if (e->flag == 2 && e->score >= beta) return e->score; 70774 87 70861
289 70460 ▶ if (e->flag == 3 && e->score <= alpha) return e->score;
Branch: 2 branches
BranchTrueFalseTotal
if (e->flag == 3 && e->score <= alpha) return e->score; 70373 87 70460
if (e->flag == 3 && e->score <= alpha) return e->score; 68794 1579 70373
290 }
291 1478419 ▶ if (e->key == b->key) { pf = e->from; pt = e->to; }
Branch: 1 branch
BranchTrueFalseTotal
if (e->key == b->key) { pf = e->from; pt = e->to; } 113354 1365065 1478419
292  
293 1478419 n = ordered_moves(b, from, to, pf, pt);
294 1478419 maximise = b->to_move == c->root;
295 1478419 ▶ best = maximise ? -STH_INF : STH_INF;
Branch: 1 branch
BranchTrueFalseTotal
best = maximise ? -STH_INF : STH_INF; 862429 615990 1478419
296 22524484 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22014369 510115 22524484
297 22014369 ▶ if (!apply(b, from[i], to[i], &u)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!apply(b, from[i], to[i], &u)) continue; 0 22014369 22014369
298 22014369 v = ab(b, c, depth - 1, alpha, beta, ply + 1);
299 22014369 sth_unplay(b, &u);
300 22014369 ▶ if (c->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c->stopped) return 0; 1070 22013299 22014369
301 22013299 ▶ if (maximise ? v > best : v < best) { best = v; bf = from[i]; bt = to[i]; }
Branch: 2 branches
BranchTrueFalseTotal
if (maximise ? v > best : v < best) { best = v; bf = from[i]; bt = to[i]; } 1802048 20211251 22013299
if (maximise ? v > best : v < best) { best = v; bf = from[i]; bt = to[i]; } 1532023 20481276 22013299
302 22013299 ▶ if (maximise) { if (best > alpha) alpha = best; }
Branch: 2 branches
BranchTrueFalseTotal
if (maximise) { if (best > alpha) alpha = best; } 1802048 20211251 22013299
if (maximise) { if (best > alpha) alpha = best; } 867703 934345 1802048
303 20211251 ▶ else { if (best < beta) beta = best; }
Branch: 1 branch
BranchTrueFalseTotal
else { if (best < beta) beta = best; } 212008 19999243 20211251
304 22013299 ▶ if (alpha >= beta) break;
Branch: 1 branch
BranchTrueFalseTotal
if (alpha >= beta) break; 967234 21046065 22013299
305 }
306 1477349 e->key = b->key;
307 1477349 e->depth = (short) depth;
308 1477349 e->score = best;
309 1477349 ▶ e->flag = (short) (best <= a0 ? 3 : best >= b0 ? 2 : 1);
Branch: 2 branches
BranchTrueFalseTotal
e->flag = (short) (best <= a0 ? 3 : best >= b0 ? 2 : 1); 1345530 131819 1477349
e->flag = (short) (best <= a0 ? 3 : best >= b0 ? 2 : 1); 1254992 90538 1345530
310 1477349 e->from = (short) bf;
311 1477349 e->to = (short) bt;
312 1477349 return best;
313 }
314  
315 /* Max-n: every seat maximises its own value. A vector comes back, one value
316 * a seat; no pruning applies, so it looks less deep for the same budget. */
317 2468 static void maxn(struct sth_board *b, struct ctx *c, int depth, int ply, int *out)
318 {
319 2468 int from[STH_LEGAL_MAX], to[STH_LEGAL_MAX], n, i, s, mover, have = 0;
320 int v[STH_SEATS_MAX], score[STH_SEATS_MAX];
321 struct sth_undo u;
322  
323 2468 c->nodes++;
324 4758 ▶ if (depth > 0 && !b->over && c->nodes > c->budget) { c->stopped = 1; return; }
Branch: 3 branches
BranchTrueFalseTotal
if (depth > 0 && !b->over && c->nodes > c->budget) { c->stopped = 1; return; } 178 2290 2468
if (depth > 0 && !b->over && c->nodes > c->budget) { c->stopped = 1; return; } 178 0 178
if (depth > 0 && !b->over && c->nodes > c->budget) { c->stopped = 1; return; } 0 178 178
325 2468 ▶ if (depth == 0 || b->over) {
Branch: 2 branches
BranchTrueFalseTotal
if (depth == 0 || b->over) { 178 2290 2468
if (depth == 0 || b->over) { 0 178 178
326 2290 ▶ if (b->over) { for (s = 0; s < b->seats; s++) out[s] = placed_value(b, s, ply); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (b->over) { for (s = 0; s < b->seats; s++) out[s] = placed_value(b, s, ply); return; } 0 2290 2290
if (b->over) { for (s = 0; s < b->seats; s++) out[s] = placed_value(b, s, ply); return; } 0 0 0
327 2290 evaluate_all(b, c->w, score);
328 9160 ▶ for (s = 0; s < b->seats; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < b->seats; s++) 6870 2290 9160
329 6870 ▶ out[s] = in_list(b->finished, b->finished_n, s) ? placed_value(b, s, ply) : value_for(b, score, s);
Branch: 1 branch
BranchTrueFalseTotal
out[s] = in_list(b->finished, b->finished_n, s) ? placed_value(b, s, ply) : value_for(b, score, s); 0 6870 6870
330 2290 return;
331 }
332 178 mover = b->to_move;
333 178 n = ordered_moves(b, from, to, -2, -2);
334 2290 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2112 178 2290
335 2112 ▶ if (!apply(b, from[i], to[i], &u)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!apply(b, from[i], to[i], &u)) continue; 0 2112 2112
336 2112 maxn(b, c, depth - 1, ply + 1, v);
337 2112 sth_unplay(b, &u);
338 2112 ▶ if (c->stopped) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->stopped) return; 0 2112 2112
339 2112 ▶ if (!have || v[mover] > out[mover]) { memcpy(out, v, sizeof v); have = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!have || v[mover] > out[mover]) { memcpy(out, v, sizeof v); have = 1; } 1934 178 2112
if (!have || v[mover] > out[mover]) { memcpy(out, v, sizeof v); have = 1; } 75 1859 1934
340 }
341 }
342  
343 /* With two seats racing the paranoid search is the plain game; with more it
344 * is the cheaper of the two to explain, and 07 measured it against max-n. */
345 2750 static int pick_mode(int mode)
346 {
347 2750 ▶ return mode == STH_SEARCH_MAXN ? STH_SEARCH_MAXN : STH_SEARCH_PARANOID;
Branch: 1 branch
BranchTrueFalseTotal
return mode == STH_SEARCH_MAXN ? STH_SEARCH_MAXN : STH_SEARCH_PARANOID; 15 2735 2750
348 }
349  
350 304516 static int seen_before(unsigned long long key, const unsigned long long *avoid, int avoid_n)
351 {
352 int i;
353 6063090 ▶ for (i = 0; i < avoid_n; i++) if (avoid[i] == key) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < avoid_n; i++) if (avoid[i] == key) return 1; 5236 5758574 5763810
for (i = 0; i < avoid_n; i++) if (avoid[i] == key) return 1; 5763810 299280 6063090
354 299280 return 0;
355 }
356  
357 /* A SEAT'S OWN PIECES NEVER GO BACK TO AN ARRANGEMENT THEY HAVE HAD, unless
358 * every move takes them back to one. Shuffling a piece inside its home costs
359 * nothing, and at depth 2 a seat can see that walking its last piece forward
360 * hands a rival a hop: so seats shuffle, and the game never ends (07 found it
361 * between searching players). Asked of the whole board, the rule caught two
362 * seats and missed four, whose other seats keep the board from ever quite
363 * repeating; asked of the mover's own pieces it catches a shuffle whatever
364 * the rest of the board does. */
365 310633 unsigned long long sth_own_key(const struct sth_board *b, int seat)
366 {
367 310633 unsigned long long k = 0;
368 int h;
369 310633 ▶ if (!b || seat < 0 || seat >= b->seats) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || seat < 0 || seat >= b->seats) return 0; 310633 0 310633
if (!b || seat < 0 || seat >= b->seats) return 0; 310633 0 310633
if (!b || seat < 0 || seat >= b->seats) return 0; 0 310633 310633
370 37897226 ▶ for (h = 0; h < STH_HOLES; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) 37586593 310633 37897226
371 37586593 ▶ if (b->cell[STH_SQ_OF[h]] == seat) k ^= STH_ZOB[seat][h];
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[STH_SQ_OF[h]] == seat) k ^= STH_ZOB[seat][h]; 3100666 34485927 37586593
372 310633 return k;
373 }
374  
375 2750 int sth_search(const struct sth_board *b0, const struct sth_weights *w, unsigned long long budget,
376 unsigned long long seed, int max_depth, int mode,
377 const unsigned long long *avoid, int avoid_n,
378 int *best_from, int *best_to, struct sth_search_info *info)
379 {
380 struct sth_board b;
381 struct ctx c;
382 2750 int from[STH_LEGAL_MAX], to[STH_LEGAL_MAX], n, i, depth, done_from = -1, done_to = -1, done_score = 0;
383 2750 int done_depth = 0;
384 struct sth_undo u;
385  
386 2750 search_init();
387 2750 ▶ if (info) memset(info, 0, sizeof *info);
Branch: 1 branch
BranchTrueFalseTotal
if (info) memset(info, 0, sizeof *info); 2750 0 2750
388 2750 ▶ if (!b0 || b0->over) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b0 || b0->over) return 0; 2750 0 2750
if (!b0 || b0->over) return 0; 0 2750 2750
389 2750 b = *b0;
390 2750 memset(&c, 0, sizeof c);
391 2750 c.w = w;
392 2750 ▶ c.budget = budget ? budget : 1;
Branch: 1 branch
BranchTrueFalseTotal
c.budget = budget ? budget : 1; 2750 0 2750
393 2750 ▶ c.seed = seed ? seed : 1;
Branch: 1 branch
BranchTrueFalseTotal
c.seed = seed ? seed : 1; 2750 0 2750
394 2750 c.root = b.to_move;
395 2750 c.mode = pick_mode(mode);
396 2750 c.tt = (struct tt_entry *) calloc(TT_SIZE, sizeof(struct tt_entry));
397 2750 ▶ if (!c.tt) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c.tt) return 0; 0 2750 2750
398  
399 2750 n = ordered_moves(&b, from, to, -2, -2);
400 {
401 2750 int fresh = 0;
402 126293 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 123543 2750 126293
403 123543 ▶ if (!apply(&b, from[i], to[i], &u)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!apply(&b, from[i], to[i], &u)) continue; 0 123543 123543
404 123543 ▶ if (!seen_before(sth_own_key(&b, c.root), avoid, avoid_n)) fresh++;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen_before(sth_own_key(&b, c.root), avoid, avoid_n)) fresh++; 121412 2131 123543
405 123543 sth_unplay(&b, &u);
406 }
407 2750 ▶ if (!fresh) avoid_n = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!fresh) avoid_n = 0; 1 2749 2750
408 }
409  
410 6819 ▶ for (depth = 1; max_depth == 0 || depth <= max_depth; depth++) {
Branch: 2 branches
BranchTrueFalseTotal
for (depth = 1; max_depth == 0 || depth <= max_depth; depth++) { 3280 3539 6819
for (depth = 1; max_depth == 0 || depth <= max_depth; depth++) { 1822 1717 3539
411 5102 int it_from = -1, it_to = -1, it_best = -STH_INF, pf = done_from, pt = done_to;
412 5102 unsigned long long it_tie = 0;
413 5102 n = ordered_moves(&b, from, to, pf, pt);
414 194735 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 190602 4133 194735
415 int v;
416 190602 ▶ if (!apply(&b, from[i], to[i], &u)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!apply(&b, from[i], to[i], &u)) continue; 0 190602 190602
417 190602 ▶ if (avoid_n && seen_before(sth_own_key(&b, c.root), avoid, avoid_n)) { sth_unplay(&b, &u); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (avoid_n && seen_before(sth_own_key(&b, c.root), avoid, avoid_n)) { sth_unplay(&b, &u); continue; } 180973 9629 190602
if (avoid_n && seen_before(sth_own_key(&b, c.root), avoid, avoid_n)) { sth_unplay(&b, &u); continue; } 3105 177868 180973
418 187497 ▶ if (c.mode == STH_SEARCH_MAXN) {
Branch: 1 branch
BranchTrueFalseTotal
if (c.mode == STH_SEARCH_MAXN) { 356 187141 187497
419 int vec[STH_SEATS_MAX];
420 356 maxn(&b, &c, depth - 1, 1, vec);
421 356 v = vec[c.root];
422 }
423 187141 else v = ab(&b, &c, depth - 1, -STH_INF, STH_INF, 1);
424 187497 sth_unplay(&b, &u);
425 187497 ▶ if (c.stopped && depth > 1) break;
Branch: 2 branches
BranchTrueFalseTotal
if (c.stopped && depth > 1) break; 969 186528 187497
if (c.stopped && depth > 1) break; 969 0 969
426 186528 c.stopped = 0;
427 {
428 186528 unsigned long long t = tie(&c, from[i], to[i]);
429 186528 ▶ if (v > it_best || (v == it_best && t < it_tie)) { it_best = v; it_from = from[i]; it_to = to[i]; it_tie = t; }
Branch: 3 branches
BranchTrueFalseTotal
if (v > it_best || (v == it_best && t < it_tie)) { it_best = v; it_from = from[i]; it_to = to[i]; it_tie = t; } 180857 5671 186528
if (v > it_best || (v == it_best && t < it_tie)) { it_best = v; it_from = from[i]; it_to = to[i]; it_tie = t; } 11143 169714 180857
if (v > it_best || (v == it_best && t < it_tie)) { it_best = v; it_from = from[i]; it_to = to[i]; it_tie = t; } 2328 8815 11143
430 }
431 }
432 5102 ▶ if (c.stopped && depth > 1) break;
Branch: 2 branches
BranchTrueFalseTotal
if (c.stopped && depth > 1) break; 969 4133 5102
if (c.stopped && depth > 1) break; 969 0 969
433 4133 done_from = it_from; done_to = it_to; done_score = it_best; done_depth = depth;
434 4133 ▶ if (c.nodes >= c.budget) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c.nodes >= c.budget) break; 33 4100 4133
435 4100 ▶ if (n == 1) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) break; 0 4100 4100
436 4100 ▶ if (done_score >= STH_WIN_SCORE - 1000 || done_score <= -STH_WIN_SCORE + 1000) break;
Branch: 2 branches
BranchTrueFalseTotal
if (done_score >= STH_WIN_SCORE - 1000 || done_score <= -STH_WIN_SCORE + 1000) break; 4074 26 4100
if (done_score >= STH_WIN_SCORE - 1000 || done_score <= -STH_WIN_SCORE + 1000) break; 4070 4 4074
437 4070 ▶ if (depth >= 64) break;
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= 64) break; 1 4069 4070
438 }
439 2750 free(c.tt);
440 2750 ▶ if (best_from) *best_from = done_from;
Branch: 1 branch
BranchTrueFalseTotal
if (best_from) *best_from = done_from; 2750 0 2750
441 2750 ▶ if (best_to) *best_to = done_to;
Branch: 1 branch
BranchTrueFalseTotal
if (best_to) *best_to = done_to; 2750 0 2750
442 2750 ▶ if (info) {
Branch: 1 branch
BranchTrueFalseTotal
if (info) { 2750 0 2750
443 2750 info->nodes = c.nodes;
444 2750 info->depth = done_depth;
445 2750 info->score = done_score;
446 2750 info->stopped = c.stopped;
447 }
448 2750 return 1;
449 }
450  
451 /* THE WEAKEST PLAYER: a seeded random choice among the moves that take a piece
452 * nearer the tip of its home, or among all of them when none does. */
453 1664 int sth_random_forward(const struct sth_board *b, unsigned long long seed, int *best_from, int *best_to)
454 {
455 1664 int from[STH_LEGAL_MAX], to[STH_LEGAL_MAX], fwd[STH_LEGAL_MAX], n, i, k = 0, p, pick;
456 1664 search_init();
457 1664 ▶ if (!b || b->over) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || b->over) return 0; 1664 0 1664
if (!b || b->over) return 0; 0 1664 1664
458 1664 n = sth_legal(b, from, to, STH_LEGAL_MAX);
459 1664 ▶ if (n > STH_LEGAL_MAX) n = STH_LEGAL_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_LEGAL_MAX) n = STH_LEGAL_MAX; 0 1664 1664
460 1664 ▶ if (n == 0) { *best_from = *best_to = -1; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { *best_from = *best_to = -1; return 1; } 0 1664 1664
461 1664 p = home_of(b, b->to_move);
462 82256 ▶ for (i = 0; i < n; i++) if (DIST[p][to[i]] < DIST[p][from[i]]) fwd[k++] = i;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (DIST[p][to[i]] < DIST[p][from[i]]) fwd[k++] = i; 25401 55191 80592
for (i = 0; i < n; i++) if (DIST[p][to[i]] < DIST[p][from[i]]) fwd[k++] = i; 80592 1664 82256
463 1664 ▶ seed = mix((seed ? seed : 1) ^ b->key);
Branch: 1 branch
BranchTrueFalseTotal
seed = mix((seed ? seed : 1) ^ b->key); 1664 0 1664
464 1664 ▶ pick = k ? fwd[seed % (unsigned long long) k] : (int) (seed % (unsigned long long) n);
Branch: 1 branch
BranchTrueFalseTotal
pick = k ? fwd[seed % (unsigned long long) k] : (int) (seed % (unsigned long long) n); 1570 94 1664
465 1664 *best_from = from[pick];
466 1664 *best_to = to[pick];
467 1664 return 1;
468 }
« sth_rules.c ↑ Summary bin/sternhalma »