go_search.c

filter: Statementstmt 100% 179 / 179 Branchbran 86.0% 148 / 172 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.1% 327 / 351 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« go_engine.c ↑ Summary
1 /* go_search.c - the playouts, and the confidence bound over the root moves.
2 *
3 * PERL-FREE, like go_engine.c, and under the same rules: no perl.h, no stdio.h,
4 * no rand, no time. t/24-no-io.t scans this file too.
5 *
6 * ALPHA-BETA DOES NOT WORK ON GO. There is no static evaluation to search with
7 * (whether a stone is strong depends on whether its group will live, which is
8 * the thing an evaluation was wanted for) and the branching factor is 361 at
9 * the root. So the leaf value here is the result of playing the position out at
10 * random and counting it, which needs no evaluation function at all.
11 *
12 * EVERYTHING IS INTEGER, the confidence bound included. A float in the selection
13 * formula is an implementation-defined rounding in the middle of a move choice,
14 * and this bot has to return the same move on every machine or the replay of a
15 * bot game stops reproducing.
16 */
17  
18 #include <stdlib.h>
19 #include <string.h>
20  
21 #include "go_abi.h"
22  
23 /* go_engine.c's table, fetched once. Using the published ABI rather than
24 * reaching for its statics keeps this file a consumer like any other, and keeps
25 * the engine's internals the engine's business. */
26 static const struct go_abi *EN = NULL;
27 76672 ▶static void need_engine(void) { if (!EN) EN = go_abi_table(); }
Branch: 1 branch
BranchTrueFalseTotal
static void need_engine(void) { if (!EN) EN = go_abi_table(); } 11 76661 76672
28  
29 /* ---------------------------------------------------------------- xorshift32 --
30 *
31 * The whole generator, and the reason it is this one rather than something
32 * wider, is in go_abi.h: a 64-bit product on a perl with 32-bit integers goes
33 * silently through an NV and loses its low bits, so a generator anybody might
34 * mirror from Perl has to live in add, xor and shift.
35 */
36 6202289 static unsigned int go_prng_next(unsigned int *state)
37 {
38 unsigned int x;
39 6202289 ▶ if (!state) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!state) return 0; 0 6202289 6202289
40  
41 /* ZERO IS A FIXED POINT of xorshift, so a zero state would make every
42 * playout identical while every test still passed. Corrected rather than
43 * accepted, and the constant is arbitrary but pinned. */
44 6202289 ▶ if (*state == 0) *state = 2463534242u;
Branch: 1 branch
BranchTrueFalseTotal
if (*state == 0) *state = 2463534242u; 3 6202286 6202289
45  
46 6202289 x = *state;
47 6202289 x ^= x << 13;
48 6202289 x ^= x >> 17;
49 6202289 x ^= x << 5;
50 6202289 *state = x;
51 6202289 return x;
52 }
53  
54 /* ------------------------------------------------------------------ the eye --
55 *
56 * A TRUE EYE, and the definition is what makes a playout terminate at all. A
57 * uniformly random legal move fills its own eyes, which kills its own groups,
58 * which means the position never settles and the playout runs to its cap.
59 *
60 * a point is a true eye for colour C when
61 * every orthogonal neighbour is C or off the board, AND
62 * of the diagonals that are on the board,
63 * at most one is not C -- in the middle
64 * none is not C -- on an edge or in a corner
65 *
66 * THE EDGE CLAUSE IS THE HALF EVERYBODY LEAVES OUT, and leaving it out makes a
67 * playout fill real eyes on the first line. t/20-bot-eyes.t has a position for
68 * each clause.
69 */
70 227009370 static int is_true_eye(const struct go_board *b, int pt, int colour)
71 {
72 227009370 int i, on_edge = 0, wrong = 0;
73 int diag[4];
74  
75 370295600 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 353594158 16701442 370295600
76 353594158 int c = b->colour[pt + b->nbr[i]];
77 353594158 ▶ if (c == GO_BORDER) { on_edge = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == GO_BORDER) { on_edge = 1; continue; } 33454285 320139873 353594158
78 320139873 ▶ if (c != colour) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c != colour) return 0; 210307928 109831945 320139873
79 }
80  
81 16701442 diag[0] = pt - 1 - b->stride;
82 16701442 diag[1] = pt + 1 - b->stride;
83 16701442 diag[2] = pt - 1 + b->stride;
84 16701442 diag[3] = pt + 1 + b->stride;
85  
86 83507210 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 66805768 16701442 83507210
87 66805768 int c = b->colour[diag[i]];
88 66805768 ▶ if (c == GO_BORDER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c == GO_BORDER) continue; 18063275 48742493 66805768
89 48742493 ▶ if (c != colour) wrong++;
Branch: 1 branch
BranchTrueFalseTotal
if (c != colour) wrong++; 12884371 35858122 48742493
90 }
91  
92 16701442 ▶ return on_edge ? (wrong == 0) : (wrong <= 1);
Branch: 1 branch
BranchTrueFalseTotal
return on_edge ? (wrong == 0) : (wrong <= 1); 8040765 8660677 16701442
93 }
94  
95 /* ---------------------------------------------------------------- a playout --
96 *
97 * Random legal moves, alternating, until both players pass or the cap trips.
98 * Returns the AREA score difference in points, from black's side.
99 *
100 * The board is played on DESTRUCTIVELY, so the caller hands over a copy. The
101 * history is null, which the engine reads as simple ko and nothing else: see
102 * go_abi.h for why a playout does not carry a superko history.
103 */
104 75330 static int playout_on(struct go_board *b, int colour, unsigned int *state,
105 int *moves_out, int *capped_out)
106 {
107 int choices[GO_MAX_PTS];
108 struct go_played res;
109 struct go_score s;
110 75330 int passes = 0, moves = 0, cap, size;
111  
112 75330 need_engine();
113 75330 size = (EN->size_of)(b);
114 75330 cap = 3 * size * size;
115  
116 6631974 ▶ while (passes < 2 && moves < cap) {
Branch: 2 branches
BranchTrueFalseTotal
while (passes < 2 && moves < cap) { 6556644 75330 6631974
while (passes < 2 && moves < cap) { 6556644 0 6556644
117 6556644 int n = 0, r, c;
118  
119 72072646 ▶ for (r = 0; r < size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < size; r++) { 65516002 6556644 72072646
120 765117518 ▶ for (c = 0; c < size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < size; c++) { 699601516 65516002 765117518
121 699601516 int pt = (EN->point_of)(b, c, r);
122 699601516 ▶ if ((EN->at)(b, pt) != GO_EMPTY) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((EN->at)(b, pt) != GO_EMPTY) continue; 452718536 246882980 699601516
123 246882980 ▶ if ((EN->legal)(b, NULL, pt, colour) != GO_OK) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((EN->legal)(b, NULL, pt, colour) != GO_OK) continue; 19873610 227009370 246882980
124 227009370 ▶ if (is_true_eye(b, pt, colour)) continue; /* never its own eye */
Branch: 1 branch
BranchTrueFalseTotal
if (is_true_eye(b, pt, colour)) continue; /* never its own eye */ 11294776 215714594 227009370
125 215714594 choices[n++] = pt;
126 }
127 }
128  
129 6556644 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 403848 6152796 6556644
130 403848 (EN->pass)(b, colour);
131 403848 passes++;
132 } else {
133 6152796 (EN->play)(b, NULL, choices[go_prng_next(state) % (unsigned int)n],
134 colour, &res);
135 6152796 passes = 0;
136 6152796 moves++;
137 }
138 6556644 ▶ colour = GO_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
colour = GO_OTHER(colour); 3283489 3273155 6556644
139 }
140  
141 75330 ▶ if (moves >= cap && capped_out) (*capped_out)++;
Branch: 2 branches
BranchTrueFalseTotal
if (moves >= cap && capped_out) (*capped_out)++; 0 75330 75330
if (moves >= cap && capped_out) (*capped_out)++; 0 0 0
142 75330 ▶ if (moves_out) *moves_out += moves;
Branch: 1 branch
BranchTrueFalseTotal
if (moves_out) *moves_out += moves; 72390 2940 75330
143  
144 /* AREA SCORING, because a playout has nobody to agree dead stones with.
145 * Komi is not applied here: the search compares playouts against each
146 * other, and a constant added to every one of them changes nothing. */
147 75330 (EN->score_area)(b, &s);
148 75330 return s.area_b - s.area_w;
149 }
150  
151 120 static int go_playout(const struct go_board *b, int colour,
152 unsigned int *state, int *moves_out)
153 {
154 struct go_board *work;
155 int diff;
156  
157 120 need_engine();
158 120 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 120 120
159 120 work = (EN->board_copy)(b);
160 120 ▶ if (!work) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!work) return 0; 0 120 120
161  
162 120 diff = playout_on(work, colour, state, moves_out, NULL);
163 120 (EN->board_drop)(work);
164 120 return diff;
165 }
166  
167 /* ----------------------------------------------------- the confidence bound --
168 *
169 * UCB1, in integers. Integer square root by Newton, and a floor-log-2 standing
170 * in for the natural log: the difference between them is a constant factor, and
171 * the constant is a tunable rather than a fact.
172 */
173 1359457 static unsigned int isqrt32(unsigned int n)
174 {
175 unsigned int x, y;
176 1359457 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 0 1359457 1359457
177 1359457 x = n;
178 for (;;) {
179 18647170 y = (x + n / x) / 2;
180 18647170 ▶ if (y >= x) break;
Branch: 1 branch
BranchTrueFalseTotal
if (y >= x) break; 1359457 17287713 18647170
181 17287713 x = y;
182 }
183 1359457 return x;
184 }
185  
186 1359457 static int ilog2i(unsigned int n)
187 {
188 1359457 int r = 0;
189 11779689 ▶ while (n >>= 1) r++;
Branch: 1 branch
BranchTrueFalseTotal
while (n >>= 1) r++; 10420232 1359457 11779689
190 1359457 return r;
191 }
192  
193 /* In permille. An unvisited move sorts above every visited one, so the first
194 * pass over the root tries each move once, IN POINT ORDER. */
195 #define GO_UNVISITED (1 << 28)
196  
197 /* The fewest samples at which a root move's win count means anything. A single
198 * playout of a Go position is close to a coin flip, so below this the selection
199 * is reading noise. Used to cut the root set down to what the budget can
200 * actually compare: see the note in go_search. */
201 #define GO_MIN_VISITS 8
202  
203 1422750 static int ucb_permille(int wins, int visits, int total, int explore)
204 {
205 int exploit, term, lg;
206  
207 1422750 ▶ if (visits <= 0) return GO_UNVISITED;
Branch: 1 branch
BranchTrueFalseTotal
if (visits <= 0) return GO_UNVISITED; 63293 1359457 1422750
208  
209 1359457 exploit = (wins * 1000) / visits;
210  
211 1359457 ▶ lg = ilog2i((unsigned int)(total < 2 ? 2 : total));
Branch: 1 branch
BranchTrueFalseTotal
lg = ilog2i((unsigned int)(total < 2 ? 2 : total)); 1358284 1173 1359457
212 1359457 term = (int)isqrt32((unsigned int)((lg * 1000000) / visits));
213  
214 1359457 return exploit + (explore * term) / 1000;
215 }
216  
217 /* ------------------------------------------------------------- the search ----
218 *
219 * FLAT, meaning the tree is one level: an array of visits and wins indexed by
220 * point, over the root moves the caller handed in. That is what the plan asked
221 * for and it is what a beginner-strength bot needs; a deep tree is strength for
222 * a lot more memory and a much harder determinism argument.
223 *
224 * `allowed` is the root moves the CALLER filtered, which is how this file never
225 * has to see the superko history. A point of -1 is a pass.
226 */
227 1173 static void go_search(const struct go_board *b, int colour,
228 const int *allowed, int nallowed,
229 unsigned int seed, int playouts, int explore,
230 struct go_search *out)
231 {
232 int visits[GO_MAX_PTS + 1];
233 int wins[GO_MAX_PTS + 1];
234 int roots[GO_MAX_PTS + 1]; /* the root moves actually searched */
235 1173 unsigned int state = seed;
236 1173 int i, done = 0, best = 0, nroots = 0;
237 1173 int pass_slot = GO_MAX_PTS; /* the pass lives past the last real point */
238  
239 1173 need_engine();
240 1173 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 1173 1173
241 1173 out->point = -1;
242 1173 out->playouts = 0;
243 1173 out->visits = 0;
244 1173 out->win_permille = 0;
245 1173 out->capped = 0;
246 1173 out->moves = 0;
247 1173 ▶ if (!b || !allowed || nallowed <= 0) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !allowed || nallowed <= 0) return; 1173 0 1173
if (!b || !allowed || nallowed <= 0) return; 1173 0 1173
if (!b || !allowed || nallowed <= 0) return; 0 1173 1173
248  
249 519639 ▶ for (i = 0; i <= GO_MAX_PTS; i++) { visits[i] = 0; wins[i] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= GO_MAX_PTS; i++) { visits[i] = 0; wins[i] = 0; } 518466 1173 519639
250  
251 /* ---- THE BUDGET DECIDES HOW MANY MOVES CAN BE COMPARED AT ALL ----------
252 *
253 * This is arithmetic about the search and not knowledge about Go, which is
254 * why it is here rather than in a heuristic.
255 *
256 * With P playouts spread over M root moves, each move gets P/M samples. A
257 * single playout of a Go position is close to a coin flip, so at one or two
258 * samples a move's win count carries no information and the selection
259 * resolves to whatever the tie-break is. MEASURED: on a 19x19 board with
260 * 361 root moves and 300 playouts the search chose the top left corner
261 * every time, because every move ended on one visit.
262 *
263 * So when the budget cannot afford GO_MIN_VISITS samples per move, the root
264 * set is CUT DOWN to the number it can afford, chosen by the seed. A
265 * shallow search over a random forty moves is a real search result; a
266 * one-sample look at every move is a sorting artifact.
267 *
268 * The shuffle is Fisher-Yates over a copy, driven by the same PRNG as the
269 * playouts, so the subset is a function of the seed and nothing else.
270 */
271 48310 ▶ for (i = 0; i < nallowed && i <= GO_MAX_PTS; i++) roots[i] = allowed[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nallowed && i <= GO_MAX_PTS; i++) roots[i] = allowed[i]; 47137 1173 48310
for (i = 0; i < nallowed && i <= GO_MAX_PTS; i++) roots[i] = allowed[i]; 47137 0 47137
272 1173 nroots = nallowed;
273  
274 1173 ▶ if (playouts / GO_MIN_VISITS < nroots) {
Branch: 1 branch
BranchTrueFalseTotal
if (playouts / GO_MIN_VISITS < nroots) { 1047 126 1173
275 1047 int keep = playouts / GO_MIN_VISITS;
276 1047 ▶ if (keep < 1) keep = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (keep < 1) keep = 1; 0 1047 1047
277  
278 46441 ▶ for (i = nroots - 1; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = nroots - 1; i > 0; i--) { 45394 1047 46441
279 45394 unsigned int j = go_prng_next(&state) % (unsigned int)(i + 1);
280 45394 int tmp = roots[i];
281 45394 roots[i] = roots[j];
282 45394 roots[j] = tmp;
283 }
284 1047 nroots = keep;
285 }
286  
287 73443 ▶ while (done < playouts) {
Branch: 1 branch
BranchTrueFalseTotal
while (done < playouts) { 72270 1173 73443
288 72270 int pick = -1, pick_slot = -1, pick_score = -1;
289 struct go_board *work;
290 struct go_played res;
291 int diff;
292  
293 /* selection, with ties to the LOWER POINT INDEX so the choice is total
294 * and does not depend on the order anything was stored in */
295 1495020 ▶ for (i = 0; i < nroots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nroots; i++) { 1422750 72270 1495020
296 1422750 int pt = roots[i];
297 1422750 ▶ int slot = pt < 0 ? pass_slot : pt;
Branch: 1 branch
BranchTrueFalseTotal
int slot = pt < 0 ? pass_slot : pt; 32350 1390400 1422750
298 1422750 int score = ucb_permille(wins[slot], visits[slot], done, explore);
299 1422750 ▶ if (score > pick_score) {
Branch: 1 branch
BranchTrueFalseTotal
if (score > pick_score) { 168788 1253962 1422750
300 168788 pick_score = score;
301 168788 pick = pt;
302 168788 pick_slot = slot;
303 }
304 }
305 72270 ▶ if (pick_slot < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (pick_slot < 0) break; 0 72270 72270
306  
307 72270 work = (EN->board_copy)(b);
308 72270 ▶ if (!work) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!work) break; 0 72270 72270
309  
310 72270 ▶ if (pick < 0) (EN->pass)(work, colour);
Branch: 1 branch
BranchTrueFalseTotal
if (pick < 0) (EN->pass)(work, colour); 5171 67099 72270
311 67099 else (EN->play)(work, NULL, pick, colour, &res);
312  
313 72270 ▶ diff = playout_on(work, GO_OTHER(colour), &state, &out->moves, &out->capped);
Branch: 1 branch
BranchTrueFalseTotal
diff = playout_on(work, GO_OTHER(colour), &state, &out->moves, &out->capped); 36570 35700 72270
314 72270 (EN->board_drop)(work);
315  
316 /* A win from the SEARCHING colour's side. A draw counts for nobody,
317 * which on an integer area difference means only an exact zero. */
318 72270 visits[pick_slot]++;
319 72270 ▶ if (colour == GO_BLACK) { if (diff > 0) wins[pick_slot]++; }
Branch: 2 branches
BranchTrueFalseTotal
if (colour == GO_BLACK) { if (diff > 0) wins[pick_slot]++; } 36570 35700 72270
if (colour == GO_BLACK) { if (diff > 0) wins[pick_slot]++; } 25908 10662 36570
320 35700 ▶ else { if (diff < 0) wins[pick_slot]++; }
Branch: 1 branch
BranchTrueFalseTotal
else { if (diff < 0) wins[pick_slot]++; } 16201 19499 35700
321  
322 72270 done++;
323 }
324  
325 1173 out->playouts = done;
326  
327 /* THE MOVE IS THE MOST WINS, then the most visits, then the lower point.
328 *
329 * "Most visited" is the standard choice and it is the RIGHT one for a
330 * search whose budget exceeded its root move count, because the bound
331 * concentrates visits on the move it likes and the visit count is what it
332 * spent the budget deciding.
333 *
334 * IT IS WRONG WHEN THE BUDGET DID NOT. The first pass over the root visits
335 * every move once, in point order, so on a 19x19 board with 361 root moves
336 * and a few hundred playouts every move ends on one visit and "most
337 * visited" resolves to the LOWEST POINT INDEX: the bot played the top left
338 * corner every single time. That was measured, not reasoned about.
339 *
340 * Ordering by wins first fixes it without a heuristic and without changing
341 * the well-funded case: where visits are concentrated the most-visited move
342 * is also the most-won, and where they are not, wins is the only thing in
343 * the table that means anything.
344 */
345 1173 best = -1;
346 8938 ▶ for (i = 0; i < nroots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nroots; i++) { 7765 1173 8938
347 7765 int pt = roots[i];
348 7765 ▶ int slot = pt < 0 ? pass_slot : pt;
Branch: 1 branch
BranchTrueFalseTotal
int slot = pt < 0 ? pass_slot : pt; 408 7357 7765
349 7765 ▶ if (best < 0) { best = slot; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (best < 0) { best = slot; continue; } 1173 6592 7765
350 6592 ▶ if (wins[slot] > wins[best]) { best = slot; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (wins[slot] > wins[best]) { best = slot; continue; } 753 5839 6592
351 5839 ▶ if (wins[slot] == wins[best] && visits[slot] > visits[best]) best = slot;
Branch: 2 branches
BranchTrueFalseTotal
if (wins[slot] == wins[best] && visits[slot] > visits[best]) best = slot; 1612 4227 5839
if (wins[slot] == wins[best] && visits[slot] > visits[best]) best = slot; 15 1597 1612
352 }
353 1173 ▶ if (best < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (best < 0) return; 0 1173 1173
354  
355 1173 ▶ out->point = (best == pass_slot) ? -1 : best;
Branch: 1 branch
BranchTrueFalseTotal
out->point = (best == pass_slot) ? -1 : best; 1103 70 1173
356 1173 out->visits = visits[best];
357 1173 ▶ out->win_permille = visits[best] ? (wins[best] * 1000) / visits[best] : 0;
Branch: 1 branch
BranchTrueFalseTotal
out->win_permille = visits[best] ? (wins[best] * 1000) / visits[best] : 0; 1173 0 1173
358 }
359  
360 /* -------------------------------------------------------------- dead stones --
361 *
362 * PLAYOUTS AND NOT HEURISTICS. From the stopped position, play it out N times
363 * and see whose the points end up being; a chain whose own points mostly end up
364 * the opponent's is dead. That is what `final_status_list dead` means in every
365 * playout engine, and it costs nothing new because the playout is already here.
366 */
367 49 static int go_dead_guess(const struct go_board *b, unsigned int seed,
368 int playouts, int *out)
369 {
370 int lost[GO_MAX_PTS];
371 int seen[GO_MAX_PTS];
372 signed char owner[GO_MAX_PTS];
373 int chain[GO_MAX_PTS];
374 49 unsigned int state = seed;
375 49 int i, n = 0, r, c, size, pts;
376  
377 49 need_engine();
378 49 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 49 49
379 49 size = (EN->size_of)(b);
380 49 pts = (EN->stride_of)(b) * (EN->stride_of)(b);
381  
382 5978 ▶ for (i = 0; i < pts; i++) { lost[i] = 0; seen[i] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pts; i++) { lost[i] = 0; seen[i] = 0; } 5929 49 5978
383  
384 2989 ▶ for (i = 0; i < playouts; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < playouts; i++) { 2940 49 2989
385 2940 struct go_board *work = (EN->board_copy)(b);
386 int j;
387 2940 ▶ if (!work) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!work) break; 0 2940 2940
388  
389 /* Black to play is arbitrary and it does not matter: the question is
390 * whose the points end up being, and both sides get to answer. */
391 2940 (void)playout_on(work, GO_BLACK, &state, NULL, NULL);
392 2940 (EN->territory)(work, NULL, 0, NULL, 0, owner);
393  
394 358680 ▶ for (j = 0; j < pts; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pts; j++) { 355740 2940 358680
395 355740 int was = (EN->at)(b, j);
396 355740 ▶ if (was != GO_BLACK && was != GO_WHITE) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (was != GO_BLACK && was != GO_WHITE) continue; 225840 129900 355740
if (was != GO_BLACK && was != GO_WHITE) continue; 208080 17760 225840
397 147660 seen[j]++;
398 /* the point ended up the opponent's, either as territory or under
399 * an opposing stone */
400 147660 ▶ if (owner[j] == GO_OTHER(was) || (EN->at)(work, j) == GO_OTHER(was))
Branch: 4 branches
BranchTrueFalseTotal
if (owner[j] == GO_OTHER(was) || (EN->at)(work, j) == GO_OTHER(was)) 129900 17760 147660
if (owner[j] == GO_OTHER(was) || (EN->at)(work, j) == GO_OTHER(was)) 141725 5935 147660
if (owner[j] == GO_OTHER(was) || (EN->at)(work, j) == GO_OTHER(was)) 126311 15414 141725
if (owner[j] == GO_OTHER(was) || (EN->at)(work, j) == GO_OTHER(was)) 25374 116351 141725
401 31309 lost[j]++;
402 }
403 2940 (EN->board_drop)(work);
404 }
405  
406 /* A chain is dead when MOST of its stones mostly ended up the opponent's.
407 * Per chain rather than per stone, because life and death is a property of
408 * a chain and half a dead group is not a thing. */
409 5978 ▶ for (i = 0; i < pts; i++) seen[i] = seen[i] ? seen[i] : 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pts; i++) seen[i] = seen[i] ? seen[i] : 0; 5929 49 5978
410  
411 490 ▶ for (r = 0; r < size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < size; r++) { 441 49 490
412 4410 ▶ for (c = 0; c < size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < size; c++) { 3969 441 4410
413 3969 int pt = (EN->point_of)(b, c, r);
414 3969 int len, j, dead_stones = 0, root_seen = 0;
415  
416 3969 ▶ if ((EN->at)(b, pt) != GO_BLACK && (EN->at)(b, pt) != GO_WHITE) continue;
Branch: 2 branches
BranchTrueFalseTotal
if ((EN->at)(b, pt) != GO_BLACK && (EN->at)(b, pt) != GO_WHITE) continue; 1804 2165 3969
if ((EN->at)(b, pt) != GO_BLACK && (EN->at)(b, pt) != GO_WHITE) continue; 1508 296 1804
417  
418 2461 len = (EN->chain_at)(b, pt, chain);
419 2461 ▶ if (len <= 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) continue; 0 2461 2461
420  
421 /* only from the chain's lowest point, so each chain is judged once */
422 {
423 2461 int low = chain[0];
424 156937 ▶ for (j = 1; j < len; j++) if (chain[j] < low) low = chain[j];
Branch: 2 branches
BranchTrueFalseTotal
for (j = 1; j < len; j++) if (chain[j] < low) low = chain[j]; 5665 148811 154476
for (j = 1; j < len; j++) if (chain[j] < low) low = chain[j]; 154476 2461 156937
425 2461 ▶ if (low != pt) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (low != pt) continue; 2314 147 2461
426 }
427  
428 2608 ▶ for (j = 0; j < len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; j++) { 2461 147 2608
429 2461 ▶ if (!seen[chain[j]]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen[chain[j]]) continue; 0 2461 2461
430 2461 root_seen++;
431 2461 ▶ if (lost[chain[j]] * 2 > seen[chain[j]]) dead_stones++;
Branch: 1 branch
BranchTrueFalseTotal
if (lost[chain[j]] * 2 > seen[chain[j]]) dead_stones++; 146 2315 2461
432 }
433  
434 147 ▶ if (root_seen && dead_stones * 2 > root_seen) {
Branch: 2 branches
BranchTrueFalseTotal
if (root_seen && dead_stones * 2 > root_seen) { 147 0 147
if (root_seen && dead_stones * 2 > root_seen) { 47 100 147
435 167 ▶ for (j = 0; j < len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; j++) { 120 47 167
436 120 ▶ if (out) out[n] = chain[j];
Branch: 1 branch
BranchTrueFalseTotal
if (out) out[n] = chain[j]; 120 0 120
437 120 n++;
438 }
439 }
440 }
441 }
442  
443 49 return n;
444 }
445  
446 /* ------------------------------------------------------------- the exports ---
447 *
448 * Appended to go_engine.c's table by go_search_install, which is called from
449 * go_abi_table. Filled in BY NAME, for the reason the rest of the table is.
450 */
451 30 void go_search_install(struct go_abi *t)
452 {
453 30 t->prng_next = go_prng_next;
454 30 t->playout = go_playout;
455 30 t->search = go_search;
456 30 t->dead_guess = go_dead_guess;
457 30 }
« go_engine.c ↑ Summary