ru_search.c

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 140 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 289 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ru_moves.c ↑ Summary
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 0 static void ru_weights_default(struct ru_weights *w)
37 {
38 int i;
39 0 ▶ if (!w) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!w) return; 0 0 0
40 0 w->exposed = 0;
41 0 w->rosette = 0;
42 0 w->entry = 0;
43 0 ▶ for (i = 0; i < 5; i++) w->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) w->reserved[i] = 0; 0 0 0
44 }
45  
46 0 static int progress(const struct ru_board *b, const struct ru_rules *r, int side)
47 {
48 0 int len = (RU->route_len)(r->route);
49 0 int total = (RU->home)(b, side) * (len + 1);
50 int step;
51 0 ▶ for (step = 1; step <= len; step++)
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) 0 0 0
52 0 ▶ 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)) 0 0 0
53 0 total += step;
54 0 return total;
55 }
56  
57 /* The chance, in sixteenths, that the dice roll exactly `distance`. */
58 0 static int chance16(const struct ru_rules *r, int distance)
59 {
60 0 int n = (RU->chance_count)(r->dice, r->zero_rolls);
61 int i, roll, weight, denominator;
62 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
63 0 (RU->chance)(r->dice, r->zero_rolls, i, &roll, &weight, &denominator);
64 0 ▶ if (roll == distance) return weight * 16 / denominator;
Branch: 1 branch
BranchTrueFalseTotal
if (roll == distance) return weight * 16 / denominator; 0 0 0
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 0 static int exposure(const struct ru_board *b, const struct ru_rules *r, int side)
76 {
77 0 int len = (RU->route_len)(r->route);
78 0 int enemy = RU_OTHER(side);
79 0 int total = 0, step, distance;
80  
81 0 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 0 0 0
82 0 int cell = (RU->route_cell)(r->route, side, step);
83 0 int theirs, chance = 0;
84 0 ▶ if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue; 0 0 0
85 0 ▶ if (!(RU->route_shared)(r->route, cell)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->route_shared)(r->route, cell)) continue; 0 0 0
86 0 ▶ if (r->safe_rosettes && (RU->is_rosette)(cell)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->safe_rosettes && (RU->is_rosette)(cell)) continue; 0 0 0
if (r->safe_rosettes && (RU->is_rosette)(cell)) continue; 0 0 0
87  
88 0 theirs = (RU->route_step)(r->route, enemy, cell);
89 0 ▶ for (distance = 1; distance <= RU_ROLL_MAX; distance++) {
Branch: 1 branch
BranchTrueFalseTotal
for (distance = 1; distance <= RU_ROLL_MAX; distance++) { 0 0 0
90 0 int from = theirs - distance;
91 0 ▶ if (from < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (from < 0) continue; 0 0 0
92 0 ▶ if (from == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (from == 0) { 0 0 0
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 0 ▶ 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)) { 0 0 0
96 0 chance += chance16(r, distance);
97 }
98 }
99 0 ▶ if (chance > 16) chance = 16;
Branch: 1 branch
BranchTrueFalseTotal
if (chance > 16) chance = 16; 0 0 0
100 0 total += chance * step;
101 }
102 0 return total;
103 }
104  
105 /* A side's pieces standing on rosettes both sides visit. */
106 0 static int rosettes_held(const struct ru_board *b, const struct ru_rules *r, int side)
107 {
108 0 int len = (RU->route_len)(r->route);
109 0 int n = 0, step;
110 0 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 0 0 0
111 0 int cell = (RU->route_cell)(r->route, side, step);
112 0 ▶ if ((RU->at)(b, cell) == RU_PIECE_OF(side)
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, cell) == RU_PIECE_OF(side) 0 0 0
113 0 ▶ && (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++; 0 0 0
&& (RU->is_rosette)(cell) && (RU->route_shared)(r->route, cell)) n++; 0 0 0
114 }
115 0 return n;
116 }
117  
118 0 static void standard_rules(struct ru_rules *r) { (RU->rules_named)(RU_RULES_FINKEL, r); }
119  
120 0 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 0 ▶ 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; 0 0 0
if (!b || (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK)) return 0; 0 0 0
if (!b || (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK)) return 0; 0 0 0
126 0 ▶ if (!rules) { standard_rules(&standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { standard_rules(&standard); rules = &standard; } 0 0 0
127 0 ▶ if (!(RU->rules_ok)(rules)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->rules_ok)(rules)) return 0; 0 0 0
128 0 enemy = RU_OTHER(side);
129  
130 0 v = 16 * (progress(b, rules, side) - progress(b, rules, enemy));
131 0 ▶ if (w && w->exposed)
Branch: 2 branches
BranchTrueFalseTotal
if (w && w->exposed) 0 0 0
if (w && w->exposed) 0 0 0
132 0 v -= w->exposed * (exposure(b, rules, side) - exposure(b, rules, enemy)) / 16;
133 0 ▶ if (w && w->rosette)
Branch: 2 branches
BranchTrueFalseTotal
if (w && w->rosette) 0 0 0
if (w && w->rosette) 0 0 0
134 0 v += w->rosette * (rosettes_held(b, rules, side) - rosettes_held(b, rules, enemy));
135 0 ▶ if (w && w->entry)
Branch: 2 branches
BranchTrueFalseTotal
if (w && w->entry) 0 0 0
if (w && w->entry) 0 0 0
136 0 v -= w->entry * ((RU->hand)(b, side) - (RU->hand)(b, enemy));
137 0 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 0 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 0 int n = (RU->moves)(b, rules, roll, m, RU_MOVES_MAX);
151 0 int k, capture = -1, rosette = -1;
152 0 ▶ if (n <= 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return -1; 0 0 0
153 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
154 0 ▶ if (m[k].captures) capture = k;
Branch: 1 branch
BranchTrueFalseTotal
if (m[k].captures) capture = k; 0 0 0
155 0 ▶ if (m[k].rosette) rosette = k;
Branch: 1 branch
BranchTrueFalseTotal
if (m[k].rosette) rosette = k; 0 0 0
156 }
157 0 ▶ if (capture >= 0) return capture;
Branch: 1 branch
BranchTrueFalseTotal
if (capture >= 0) return capture; 0 0 0
158 0 ▶ if (rosette >= 0) return rosette;
Branch: 1 branch
BranchTrueFalseTotal
if (rosette >= 0) return rosette; 0 0 0
159 0 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 0 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 0 int n = (RU->moves)(b, s->rules, roll, m, RU_MOVES_MAX);
195 0 int k, v, chosen = 0, ours;
196  
197 0 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 0 0 0
198 0 (RU->forfeit)(b, &u);
199 0 v = value(s, b, depth - 1);
200 0 (RU->unapply)(b, &u);
201 0 return v;
202 }
203 0 ours = (RU->side)(b) == s->root;
204 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
205 0 (RU->apply)(b, s->rules, &m[k], &u);
206 0 v = value(s, b, depth - 1);
207 0 (RU->unapply)(b, &u);
208 0 ▶ if (s->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s->stopped) return 0; 0 0 0
209 0 ▶ if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v;
Branch: 4 branches
BranchTrueFalseTotal
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 0 0 0
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 0 0 0
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 0 0 0
if (k == 0 || (ours ? v > chosen : v < chosen)) chosen = v; 0 0 0
210 }
211 0 return chosen;
212 }
213  
214 0 static int value(struct search *s, struct ru_board *b, int depth)
215 {
216 0 int rolls, i, roll, weight, denominator = 1, sum = 0, winner;
217  
218 0 ▶ 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; } 0 0 0
if (!s->unbounded && s->nodes >= s->budget) { s->stopped = 1; return 0; } 0 0 0
219 0 s->nodes++;
220  
221 0 winner = (RU->winner)(b, s->rules);
222 0 ▶ 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); 0 0 0
if (winner >= 0) return winner == s->root ? RU_WIN + depth : -(RU_WIN + depth); 0 0 0
223 0 ▶ 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 0 0
224 0 ▶ 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); 0 0 0
225  
226 0 rolls = (RU->chance_count)(s->rules->dice, s->rules->zero_rolls);
227 0 ▶ for (i = 0; i < rolls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rolls; i++) { 0 0 0
228 0 (RU->chance)(s->rules->dice, s->rules->zero_rolls, i, &roll, &weight, &denominator);
229 0 sum += weight * best(s, b, roll, depth);
230 0 ▶ if (s->stopped) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s->stopped) return 0; 0 0 0
231 }
232 0 return sum / denominator;
233 }
234  
235 0 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 0 int n, depth, k, chosen = 0;
247  
248 0 ▶ if (!info) info = &local;
Branch: 1 branch
BranchTrueFalseTotal
if (!info) info = &local; 0 0 0
249 0 info->nodes = 0;
250 0 info->depth = 0;
251 0 info->value = 0;
252 0 info->stopped = 0;
253  
254 0 ▶ if (!board) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!board) return -1; 0 0 0
255 0 ▶ if (!rules) { standard_rules(&standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { standard_rules(&standard); rules = &standard; } 0 0 0
256 0 ▶ if (!weights) { ru_weights_default(&plain); weights = &plain; }
Branch: 1 branch
BranchTrueFalseTotal
if (!weights) { ru_weights_default(&plain); weights = &plain; } 0 0 0
257 0 ▶ if ((RU->status)(board, rules) != RU_ONGOING) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->status)(board, rules) != RU_ONGOING) return -1; 0 0 0
258  
259 0 b = (RU->board_copy)(board);
260 0 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 0 0
261 0 n = (RU->moves)(b, rules, roll, m, RU_MOVES_MAX);
262 0 ▶ if (n <= 0) { (RU->board_drop)(b); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { (RU->board_drop)(b); return -1; } 0 0 0
263 0 ▶ 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; 0 0 0
if (max_depth <= 0 || max_depth > RU_DEPTH_MAX) max_depth = RU_DEPTH_MAX; 0 0 0
264  
265 0 s.rules = rules;
266 0 s.weights = weights;
267 0 s.root = (RU->side)(b);
268 0 s.nodes = 0;
269 0 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 0 ▶ for (depth = 1; depth <= max_depth; depth++) {
Branch: 1 branch
BranchTrueFalseTotal
for (depth = 1; depth <= max_depth; depth++) { 0 0 0
276 0 int best_k = 0, best_v = 0, v;
277 0 s.unbounded = (depth == 1);
278 0 s.stopped = 0;
279 0 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 0 0 0
280 0 (RU->apply)(b, rules, &m[k], &u);
281 0 v = value(&s, b, depth - 1);
282 0 (RU->unapply)(b, &u);
283 0 ▶ if (s.stopped) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s.stopped) break; 0 0 0
284 0 ▶ 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; } 0 0 0
if (k == 0 || v >= best_v) { best_v = v; best_k = k; } 0 0 0
285 }
286 0 ▶ if (s.stopped) { info->stopped = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (s.stopped) { info->stopped = 1; break; } 0 0 0
287 0 chosen = best_k;
288 0 info->depth = depth;
289 0 info->value = best_v;
290 0 ▶ if (n == 1) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) break; 0 0 0
291 0 ▶ if (s.nodes >= s.budget) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s.nodes >= s.budget) break; 0 0 0
292 }
293  
294 0 info->nodes = s.nodes;
295 0 (RU->board_drop)(b);
296 0 return chosen;
297 }
298  
299 /* ---- the table ------------------------------------------------------------------- */
300  
301 0 void ru_search_install(struct ru_abi *t)
302 {
303 0 RU = t;
304 0 t->weights_default = ru_weights_default;
305 0 t->evaluate = ru_evaluate;
306 0 t->greedy = ru_greedy;
307 0 t->search = ru_search;
308 0 }
« ru_moves.c ↑ Summary