ru_search.c

filter: Statementstmt 97.9% 146 / 149 Branchbran 85.7% 120 / 140 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.0% 266 / 289 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ru_moves.c ↑ Summary ru_engine.c »
1 /* ru_search.c - choosing a move.
2 *
3 * PERL-FREE, like the rest, and it reaches the board only through the table.
4 *
5 * THIS FILE'S SCOPE. An evaluation of a position in whole numbers, a choice
6 * made without looking ahead, and a search that looks ahead through the dice.
7 *
8 * THERE IS NO FLOAT HERE. A chance is a weight over a denominator, both whole
9 * numbers from the chance table; a value is an int; and a division rounds
10 * toward zero whichever sign it has, so that a position and its mirror are
11 * valued alike.
12 *
13 * NOTHING HERE READS A CLOCK. A search is bounded in NODES, so that the same
14 * position, roll and budget give the same move on every machine and under any
15 * load.
16 */
17  
18 #include <stddef.h>
19 #include "ru_abi.h"
20  
21 static const struct ru_abi *RU = NULL;
22  
23 /* ---- the evaluation --------------------------------------------------------------
24 *
25 * In SIXTEENTHS OF A STEP, for one side, and it is the other side's with the
26 * sign turned.
27 *
28 * The plain evaluation is progress: every step each of a side's pieces has
29 * taken, a piece at home counting one more than the route is long and a piece
30 * in hand counting nothing, less the same for the other side.
31 *
32 * Three terms may be added to it, each with a weight that is 0 unless a caller
33 * sets it.
34 */
35  
36 6143 static void ru_weights_default(struct ru_weights *w)
37 {
38 int i;
39 6143 ▶ if (!w) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!w) return; 0 6143 6143
40 6143 w->exposed = 0;
41 6143 w->rosette = 0;
42 6143 w->entry = 0;
43 36858 ▶ for (i = 0; i < 5; i++) w->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) w->reserved[i] = 0; 30715 6143 36858
44 }
45  
46 11127868 static int progress(const struct ru_board *b, const struct ru_rules *r, int side)
47 {
48 11127868 int len = (RU->route_len)(r->route);
49 11127868 int total = (RU->home)(b, side) * (len + 1);
50 int step;
51 176106308 ▶ for (step = 1; step <= len; step++)
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) 164978440 11127868 176106308
52 164978440 ▶ if ((RU->at)(b, (RU->route_cell)(r->route, side, step)) == RU_PIECE_OF(side))
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, (RU->route_cell)(r->route, side, step)) == RU_PIECE_OF(side)) 33916709 131061731 164978440
53 33916709 total += step;
54 11127868 return total;
55 }
56  
57 /* The chance, in sixteenths, that the dice roll exactly `distance`. */
58 13520238 static int chance16(const struct ru_rules *r, int distance)
59 {
60 13520238 int n = (RU->chance_count)(r->dice, r->zero_rolls);
61 int i, roll, weight, denominator;
62 43507740 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 43507740 0 43507740
63 43507740 (RU->chance)(r->dice, r->zero_rolls, i, &roll, &weight, &denominator);
64 43507740 ▶ if (roll == distance) return weight * 16 / denominator;
Branch: 1 branch
BranchTrueFalseTotal
if (roll == distance) return weight * 16 / denominator; 13520238 29987502 43507740
65 }
66 0 return 0;
67 }
68  
69 /* What a side stands to lose to the enemy's next roll: for each of its pieces
70 * an enemy piece could land on, the chance of the roll that does it times the
71 * steps the piece would be sent back. In sixteenths of a step.
72 *
73 * A piece is asked of ITS OWN step and its attacker of the ENEMY's step for
74 * the same cell, which on the long route is a different number. */
75 10513298 static int exposure(const struct ru_board *b, const struct ru_rules *r, int side)
76 {
77 10513298 int len = (RU->route_len)(r->route);
78 10513298 int enemy = RU_OTHER(side);
79 10513298 int total = 0, step, distance;
80  
81 166613630 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 156100332 10513298 166613630
82 156100332 int cell = (RU->route_cell)(r->route, side, step);
83 156100332 int theirs, chance = 0;
84 156100332 ▶ if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue; 123979742 32120590 156100332
85 32120590 ▶ if (!(RU->route_shared)(r->route, cell)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->route_shared)(r->route, cell)) continue; 17462883 14657707 32120590
86 14657707 ▶ if (r->safe_rosettes && (RU->is_rosette)(cell)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->safe_rosettes && (RU->is_rosette)(cell)) continue; 8146106 6511601 14657707
if (r->safe_rosettes && (RU->is_rosette)(cell)) continue; 1228416 6917690 8146106
87  
88 13429291 theirs = (RU->route_step)(r->route, enemy, cell);
89 67146455 ▶ for (distance = 1; distance <= RU_ROLL_MAX; distance++) {
Branch: 1 branch
BranchTrueFalseTotal
for (distance = 1; distance <= RU_ROLL_MAX; distance++) { 53717164 13429291 67146455
90 53717164 int from = theirs - distance;
91 53717164 ▶ if (from < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (from < 0) continue; 0 53717164 53717164
92 53717164 ▶ if (from == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (from == 0) { 0 53717164 53717164
93 0 ▶ if ((RU->hand)(b, enemy) > 0) chance += chance16(r, distance);
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->hand)(b, enemy) > 0) chance += chance16(r, distance); 0 0 0
94 }
95 53717164 ▶ else if ((RU->at)(b, (RU->route_cell)(r->route, enemy, from)) == RU_PIECE_OF(enemy)) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((RU->at)(b, (RU->route_cell)(r->route, enemy, from)) == RU_PIECE_OF(enemy)) { 13520238 40196926 53717164
96 13520238 chance += chance16(r, distance);
97 }
98 }
99 13429291 ▶ if (chance > 16) chance = 16;
Branch: 1 branch
BranchTrueFalseTotal
if (chance > 16) chance = 16; 0 13429291 13429291
100 13429291 total += chance * step;
101 }
102 10513298 return total;
103 }
104  
105 /* A side's pieces standing on rosettes both sides visit. */
106 6056214 static int rosettes_held(const struct ru_board *b, const struct ru_rules *r, int side)
107 {
108 6056214 int len = (RU->route_len)(r->route);
109 6056214 int n = 0, step;
110 90843210 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 84786996 6056214 90843210
111 84786996 int cell = (RU->route_cell)(r->route, side, step);
112 84786996 ▶ if ((RU->at)(b, cell) == RU_PIECE_OF(side)
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, cell) == RU_PIECE_OF(side) 19369128 65417868 84786996
113 19369128 ▶ && (RU->is_rosette)(cell) && (RU->route_shared)(r->route, cell)) n++;
Branch: 2 branches
BranchTrueFalseTotal
&& (RU->is_rosette)(cell) && (RU->route_shared)(r->route, cell)) n++; 4724719 14644409 19369128
&& (RU->is_rosette)(cell) && (RU->route_shared)(r->route, cell)) n++; 1228417 3496302 4724719
114 }
115 6056214 return n;
116 }
117  
118 0 static void standard_rules(struct ru_rules *r) { (RU->rules_named)(RU_RULES_FINKEL, r); }
119  
120 5563935 static int ru_evaluate(const struct ru_board *b, const struct ru_rules *rules,
121 const struct ru_weights *w, int side)
122 {
123 struct ru_rules standard;
124 int enemy, v;
125 5563935 ▶ if (!b || (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK)) return 0; 5563935 0 5563935
if (!b || (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK)) return 0; 2896645 2667290 5563935
if (!b || (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK)) return 0; 1 2896644 2896645
126 5563934 ▶ if (!rules) { standard_rules(&standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { standard_rules(&standard); rules = &standard; } 0 5563934 5563934
127 5563934 ▶ if (!(RU->rules_ok)(rules)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->rules_ok)(rules)) return 0; 0 5563934 5563934
128 5563934 enemy = RU_OTHER(side);
129  
130 5563934 v = 16 * (progress(b, rules, side) - progress(b, rules, enemy));
131 5563934 ▶ if (w && w->exposed)
Branch: 2 branches
BranchTrueFalseTotal
if (w && w->exposed) 5563934 0 5563934
if (w && w->exposed) 5256649 307285 5563934
132 5256649 v -= w->exposed * (exposure(b, rules, side) - exposure(b, rules, enemy)) / 16;
133 5563934 ▶ if (w && w->rosette)
Branch: 2 branches
BranchTrueFalseTotal
if (w && w->rosette) 5563934 0 5563934
if (w && w->rosette) 3028107 2535827 5563934
134 3028107 v += w->rosette * (rosettes_held(b, rules, side) - rosettes_held(b, rules, enemy));
135 5563934 ▶ if (w && w->entry)
Branch: 2 branches
BranchTrueFalseTotal
if (w && w->entry) 5563934 0 5563934
if (w && w->entry) 3028107 2535827 5563934
136 3028107 v -= w->entry * ((RU->hand)(b, side) - (RU->hand)(b, enemy));
137 5563934 return v;
138 }
139  
140 /* ---- a choice without looking ahead -------------------------------------------------
141 *
142 * Of the moves the roll allows: one that captures, if any does; else one that
143 * lands on a rosette, if any does; else the piece furthest along. Where two
144 * moves are alike in that, the one further along.
145 */
146  
147 2237 static int ru_greedy(const struct ru_board *b, const struct ru_rules *rules, int roll)
148 {
149 struct ru_move m[RU_MOVES_MAX];
150 2237 int n = (RU->moves)(b, rules, roll, m, RU_MOVES_MAX);
151 2237 int k, capture = -1, rosette = -1;
152 2237 ▶ if (n <= 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return -1; 1 2236 2237
153 7146 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 4910 2236 7146
154 4910 ▶ if (m[k].captures) capture = k;
Branch: 1 branch
BranchTrueFalseTotal
if (m[k].captures) capture = k; 249 4661 4910
155 4910 ▶ if (m[k].rosette) rosette = k;
Branch: 1 branch
BranchTrueFalseTotal
if (m[k].rosette) rosette = k; 695 4215 4910
156 }
157 2236 ▶ if (capture >= 0) return capture;
Branch: 1 branch
BranchTrueFalseTotal
if (capture >= 0) return capture; 241 1995 2236
158 1995 ▶ if (rosette >= 0) return rosette;
Branch: 1 branch
BranchTrueFalseTotal
if (rosette >= 0) return rosette; 586 1409 1995
159 1409 return n - 1;
160 }
161  
162 /* ---- the search ---------------------------------------------------------------------
163 *
164 * Expectiminimax. A position's value is the sum, over every roll the dice can
165 * make, of that roll's weight times the value of the best move it allows,
166 * over the denominator. "Best" is the highest for the side the search is for
167 * and the lowest for the other.
168 *
169 * THE VALUE IS ALWAYS FOR ONE SIDE, fixed where the search starts, and whether
170 * a level takes the highest or the lowest is asked of the board each time. It
171 * is NOT a sign turned on every ply: after a rosette the same side moves
172 * again, and a turned sign is wrong on exactly those plies.
173 *
174 * A roll that allows nothing is a level like any other: the turn is lost, and
175 * the search goes on from the other side.
176 */
177  
178 struct search {
179 const struct ru_rules *rules;
180 const struct ru_weights *weights;
181 int root;
182 unsigned long long nodes;
183 unsigned long long budget;
184 int unbounded; /* the first depth always finishes */
185 int stopped;
186 };
187  
188 static int value(struct search *s, struct ru_board *b, int depth);
189  
190 2312845 static int best(struct search *s, struct ru_board *b, int roll, int depth)
191 {
192 struct ru_move m[RU_MOVES_MAX];
193 struct ru_undo u;
194 2312845 int n = (RU->moves)(b, s->rules, roll, m, RU_MOVES_MAX);
195 2312845 int k, v, chosen = 0, ours;
196  
197 2312845 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 287516 2025329 2312845
198 287516 (RU->forfeit)(b, &u);
199 287516 v = value(s, b, depth - 1);
200 287516 (RU->unapply)(b, &u);
201 287516 return v;
202 }
203 2025329 ours = (RU->side)(b) == s->root;
204 7772594 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 5747287 2025307 7772594
205 5747287 (RU->apply)(b, s->rules, &m[k], &u);
206 5747287 v = value(s, b, depth - 1);
207 5747287 (RU->unapply)(b, &u);
208 5747287 ▶ if (s->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s->stopped) return 0; 22 5747265 5747287
209 5747265 ▶ if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v;
Branch: 4 branches
BranchTrueFalseTotal
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 3721943 2025322 5747265
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 1687918 2034025 3721943
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 609433 1078485 1687918
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 800953 1233072 2034025
210 }
211 2025307 return chosen;
212 }
213  
214 6071144 static int value(struct search *s, struct ru_board *b, int depth)
215 {
216 6071144 int rolls, i, roll, weight, denominator = 1, sum = 0, winner;
217  
218 6071144 ▶ if (!s->unbounded && s->nodes >= s->budget) { s->stopped = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!s->unbounded && s->nodes >= s->budget) { s->stopped = 1; return 0; } 6056657 14487 6071144
if (!s->unbounded && s->nodes >= s->budget) { s->stopped = 1; return 0; } 13 6056644 6056657
219 6071131 s->nodes++;
220  
221 6071131 winner = (RU->winner)(b, s->rules);
222 6071131 ▶ if (winner >= 0) return winner == s->root ? RU_WIN + depth : -(RU_WIN + depth);
Branch: 2 branches
BranchTrueFalseTotal
if (winner >= 0) return winner == s->root ? RU_WIN + depth : -(RU_WIN + depth); 151 6070980 6071131
if (winner >= 0) return winner == s->root ? RU_WIN + depth : -(RU_WIN + depth); 24 127 151
223 6070980 ▶ if ((RU->status)(b, s->rules) != RU_ONGOING) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->status)(b, s->rules) != RU_ONGOING) return 0; 0 6070980 6070980
224 6070980 ▶ if (depth <= 0) return ru_evaluate(b, s->rules, s->weights, s->root);
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) return ru_evaluate(b, s->rules, s->weights, s->root); 5563915 507065 6070980
225  
226 507065 rolls = (RU->chance_count)(s->rules->dice, s->rules->zero_rolls);
227 2819885 ▶ for (i = 0; i < rolls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rolls; i++) { 2312845 507040 2819885
228 2312845 (RU->chance)(s->rules->dice, s->rules->zero_rolls, i, &roll, &weight, &denominator);
229 2312845 sum += weight * best(s, b, roll, depth);
230 2312845 ▶ if (s->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s->stopped) return 0; 25 2312820 2312845
231 }
232 507040 return sum / denominator;
233 }
234  
235 6121 static int ru_search(const struct ru_board *board, const struct ru_rules *rules, int roll,
236 const struct ru_weights *weights, unsigned long long budget, int max_depth,
237 struct ru_search_info *info)
238 {
239 struct ru_rules standard;
240 struct ru_weights plain;
241 struct ru_search_info local;
242 struct ru_move m[RU_MOVES_MAX];
243 struct ru_undo u;
244 struct ru_board *b;
245 struct search s;
246 6121 int n, depth, k, chosen = 0;
247  
248 6121 ▶ if (!info) info = &local;
Branch: 1 branch
BranchTrueFalseTotal
if (!info) info = &local; 0 6121 6121
249 6121 info->nodes = 0;
250 6121 info->depth = 0;
251 6121 info->value = 0;
252 6121 info->stopped = 0;
253  
254 6121 ▶ if (!board) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!board) return -1; 0 6121 6121
255 6121 ▶ if (!rules) { standard_rules(&standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { standard_rules(&standard); rules = &standard; } 0 6121 6121
256 6121 ▶ if (!weights) { ru_weights_default(&plain); weights = &plain; }
Branch: 1 branch
BranchTrueFalseTotal
if (!weights) { ru_weights_default(&plain); weights = &plain; } 0 6121 6121
257 6121 ▶ if ((RU->status)(board, rules) != RU_ONGOING) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->status)(board, rules) != RU_ONGOING) return -1; 1 6120 6121
258  
259 6120 b = (RU->board_copy)(board);
260 6120 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 6120 6120
261 6120 n = (RU->moves)(b, rules, roll, m, RU_MOVES_MAX);
262 6120 ▶ if (n <= 0) { (RU->board_drop)(b); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { (RU->board_drop)(b); return -1; } 57 6063 6120
263 6063 ▶ if (max_depth <= 0 || max_depth > RU_DEPTH_MAX) max_depth = RU_DEPTH_MAX;
Branch: 2 branches
BranchTrueFalseTotal
if (max_depth <= 0 || max_depth > RU_DEPTH_MAX) max_depth = RU_DEPTH_MAX; 6042 21 6063
if (max_depth <= 0 || max_depth > RU_DEPTH_MAX) max_depth = RU_DEPTH_MAX; 0 6042 6042
264  
265 6063 s.rules = rules;
266 6063 s.weights = weights;
267 6063 s.root = (RU->side)(b);
268 6063 s.nodes = 0;
269 6063 s.budget = budget;
270  
271 /* MOVES OF EQUAL VALUE GO TO THE LAST OF THEM, which is the piece furthest
272 * along. That is a measurement and not a taste: with ties to the first,
273 * the piece least far along, a search one level deep lost 386 games in 400
274 * to the choice made without looking ahead (plan_game_royalur/07). */
275 19170 ▶ for (depth = 1; depth <= max_depth; depth++) {
Branch: 1 branch
BranchTrueFalseTotal
for (depth = 1; depth <= max_depth; depth++) { 14184 4986 19170
276 14184 int best_k = 0, best_v = 0, v;
277 14184 s.unbounded = (depth == 1);
278 14184 s.stopped = 0;
279 50512 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 36341 14171 50512
280 36341 (RU->apply)(b, rules, &m[k], &u);
281 36341 v = value(&s, b, depth - 1);
282 36341 (RU->unapply)(b, &u);
283 36341 ▶ if (s.stopped) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s.stopped) break; 13 36328 36341
284 36328 ▶ if (k == 0 || v >= best_v) { best_v = v; best_k = k; }
Branch: 2 branches
BranchTrueFalseTotal
if (k == 0 || v >= best_v) { best_v = v; best_k = k; } 22150 14178 36328
if (k == 0 || v >= best_v) { best_v = v; best_k = k; } 10522 11628 22150
285 }
286 14184 ▶ if (s.stopped) { info->stopped = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (s.stopped) { info->stopped = 1; break; } 13 14171 14184
287 14171 chosen = best_k;
288 14171 info->depth = depth;
289 14171 info->value = best_v;
290 14171 ▶ if (n == 1) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) break; 1056 13115 14171
291 13115 ▶ if (s.nodes >= s.budget) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s.nodes >= s.budget) break; 8 13107 13115
292 }
293  
294 6063 info->nodes = s.nodes;
295 6063 (RU->board_drop)(b);
296 6063 return chosen;
297 }
298  
299 /* ---- the table ------------------------------------------------------------------- */
300  
301 96 void ru_search_install(struct ru_abi *t)
302 {
303 96 RU = t;
304 96 t->weights_default = ru_weights_default;
305 96 t->evaluate = ru_evaluate;
306 96 t->greedy = ru_greedy;
307 96 t->search = ru_search;
308 96 }
« ru_moves.c ↑ Summary ru_engine.c »