Moggy mq: Move from playout/moggy.c to mq.h
[pachi/json.git] / playout / moggy.c
blobc0fc813450ee38c47415ca2cd97429463ea6afaa
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 #define DEBUG
7 #include "board.h"
8 #include "debug.h"
9 #include "mq.h"
10 #include "pattern3.h"
11 #include "playout.h"
12 #include "playout/moggy.h"
13 #include "random.h"
14 #include "tactics.h"
15 #include "uct/prior.h"
17 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
20 /* Note that the context can be shared by multiple threads! */
22 struct moggy_policy {
23 bool ladders, ladderassess, borderladders, assess_local;
24 int lcapturerate, atarirate, capturerate, patternrate;
25 int selfatarirate;
26 int fillboardtries;
27 /* Whether to look for patterns around second-to-last move. */
28 bool pattern2;
30 struct pattern3s patterns;
34 static char moggy_patterns_src[][11] = {
35 /* hane pattern - enclosing hane */
36 "XOX"
37 "..."
38 "???",
39 /* hane pattern - non-cutting hane */
40 "XO."
41 "..."
42 "?.?",
43 /* hane pattern - magari */
44 "XO?"
45 "X.."
46 "x.?",
47 /* hane pattern - thin hane */
48 "XOO"
49 "..."
50 "?.?" "X",
51 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
52 ".O."
53 "X.."
54 "...",
55 /* cut1 pattern (kiri) - unprotected cut */
56 "XO?"
57 "O.o"
58 "?o?",
59 /* cut1 pattern (kiri) - peeped cut */
60 "XO?"
61 "O.X"
62 "???",
63 /* cut2 pattern (de) */
64 "?X?"
65 "O.O"
66 "ooo",
67 /* cut keima (not in Mogo) */
68 "OX?"
69 "o.O"
70 "???", /* o?? has some pathological tsumego cases */
71 /* side pattern - chase */
72 "X.?"
73 "O.?"
74 "##?",
75 /* side pattern - weirdness (SUSPICIOUS) */
76 "?X?"
77 "X.O"
78 "###",
79 /* side pattern - sagari (SUSPICIOUS) */
80 "?XO"
81 "x.x" /* Mogo has "x.?" */
82 "###" /* Mogo has "X" */,
83 /* side pattern - throw-in (SUSPICIOUS) */
84 #if 0
85 "?OX"
86 "o.O"
87 "?##" "X",
88 #endif
89 /* side pattern - cut (SUSPICIOUS) */
90 "?OX"
91 "X.O"
92 "###" /* Mogo has "X" */,
94 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
97 static void
98 apply_pattern_here(struct playout_policy *p,
99 struct board *b, struct move *m, struct move_queue *q)
101 struct moggy_policy *pp = p->data;
102 if (test_pattern3_here(&pp->patterns, b, m))
103 mq_add(q, m->coord);
106 /* Check if we match any pattern around given move (with the other color to play). */
107 static coord_t
108 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm)
110 struct move_queue q;
111 q.moves = 0;
113 /* Suicides do not make any patterns and confuse us. */
114 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
115 return pass;
117 foreach_neighbor(b, m->coord, {
118 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
119 if (board_is_valid_move(b, &m2))
120 apply_pattern_here(p, b, &m2, &q);
122 foreach_diag_neighbor(b, m->coord) {
123 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
124 if (board_is_valid_move(b, &m2))
125 apply_pattern_here(p, b, &m2, &q);
126 } foreach_diag_neighbor_end;
128 if (mm) { /* Second move for pattern searching */
129 foreach_neighbor(b, mm->coord, {
130 if (coord_is_8adjecent(m->coord, c, b))
131 continue;
132 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
133 if (board_is_valid_move(b, &m2))
134 apply_pattern_here(p, b, &m2, &q);
136 foreach_diag_neighbor(b, mm->coord) {
137 if (coord_is_8adjecent(m->coord, c, b))
138 continue;
139 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
140 if (board_is_valid_move(b, &m2))
141 apply_pattern_here(p, b, &m2, &q);
142 } foreach_diag_neighbor_end;
145 if (PLDEBUGL(5)) {
146 fprintf(stderr, "Pattern candidate moves: ");
147 for (int i = 0; i < q.moves; i++) {
148 fprintf(stderr, "%s ", coord2sstr(q.move[i], b));
150 fprintf(stderr, "\n");
153 int i = fast_random(q.moves);
154 return q.moves ? q.move[i] : pass;
159 /* Is this ladder breaker friendly for the one who catches ladder. */
160 static bool
161 ladder_catcher(struct board *b, int x, int y, enum stone laddered)
163 enum stone breaker = board_atxy(b, x, y);
164 return breaker == stone_other(laddered) || breaker == S_OFFBOARD;
167 static bool
168 ladder_catches(struct playout_policy *p, struct board *b, coord_t coord, group_t laddered)
170 struct moggy_policy *pp = p->data;
172 /* This is very trivial and gets a lot of corner cases wrong.
173 * We need this to be just very fast. One important point is
174 * that we sometimes might not notice a ladder but if we do,
175 * it should always work; thus we can use this for strong
176 * negative hinting safely. */
178 enum stone lcolor = board_at(b, group_base(laddered));
179 int x = coord_x(coord, b), y = coord_y(coord, b);
181 if (PLDEBUGL(6))
182 fprintf(stderr, "ladder check - does %s play out %s's laddered group %s?\n",
183 coord2sstr(coord, b), stone2str(lcolor), coord2sstr(laddered, b));
185 /* First, special-case first-line "ladders". This is a huge chunk
186 * of ladders we actually meet and want to play. */
187 if (pp->borderladders
188 && neighbor_count_at(b, coord, S_OFFBOARD) == 1
189 && neighbor_count_at(b, coord, lcolor) == 1) {
190 if (PLDEBUGL(5))
191 fprintf(stderr, "border ladder\n");
192 /* Direction along border; xd is horiz. border, yd vertical. */
193 int xd = 0, yd = 0;
194 if (board_atxy(b, x + 1, y) == S_OFFBOARD || board_atxy(b, x - 1, y) == S_OFFBOARD)
195 yd = 1;
196 else
197 xd = 1;
198 /* Direction from the border; -1 is above/left, 1 is below/right. */
199 int dd = (board_atxy(b, x + yd, y + xd) == S_OFFBOARD) ? 1 : -1;
200 if (PLDEBUGL(6))
201 fprintf(stderr, "xd %d yd %d dd %d\n", xd, yd, dd);
202 /* | ? ?
203 * | . O #
204 * | c X #
205 * | . O #
206 * | ? ? */
207 /* This is normally caught, unless we have friends both above
208 * and below... */
209 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor
210 && board_atxy(b, x - xd * 2, y - yd * 2) == lcolor)
211 return false;
212 /* ...or can't block where we need because of shortage
213 * of liberties. */
214 int libs1 = board_group_info(b, group_atxy(b, x + xd - yd * dd, y + yd - xd * dd)).libs;
215 int libs2 = board_group_info(b, group_atxy(b, x - xd - yd * dd, y - yd - xd * dd)).libs;
216 if (PLDEBUGL(6))
217 fprintf(stderr, "libs1 %d libs2 %d\n", libs1, libs2);
218 if (libs1 < 2 && libs2 < 2)
219 return false;
220 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor && libs1 < 3)
221 return false;
222 if (board_atxy(b, x - xd * 2, y - yd * 2) == lcolor && libs2 < 3)
223 return false;
224 return true;
227 if (!pp->ladders)
228 return false;
230 /* Figure out the ladder direction */
231 int xd, yd;
232 xd = board_atxy(b, x + 1, y) == S_NONE ? 1 : board_atxy(b, x - 1, y) == S_NONE ? -1 : 0;
233 yd = board_atxy(b, x, y + 1) == S_NONE ? 1 : board_atxy(b, x, y - 1) == S_NONE ? -1 : 0;
235 if (!xd || !yd) {
236 if (PLDEBUGL(5))
237 fprintf(stderr, "no ladder, too little space; self-atari?\n");
238 return false;
241 /* For given (xd,yd), we have two possibilities where to move
242 * next. Consider (-1,-1):
243 * n X . n c X
244 * c O X X O #
245 * X # # . X #
247 bool horiz_first = ladder_catcher(b, x, y - yd, lcolor); // left case
248 bool vert_first = ladder_catcher(b, x - xd, y, lcolor); // right case
250 /* We don't have to look at the other 'X' in the position - if it
251 * wouldn't be there, the group wouldn't be in atari. */
253 /* We do only tight ladders, not loose ladders. Furthermore,
254 * the ladders need to be simple:
255 * . X . . . X
256 * c O X supported . c O unsupported
257 * X # # X O #
259 assert(!(horiz_first && vert_first));
260 if (!horiz_first && !vert_first) {
261 /* TODO: In case of basic non-simple ladder, play out both variants. */
262 if (PLDEBUGL(5))
263 fprintf(stderr, "non-simple ladder\n");
264 return false;
267 /* We do that below for further moves, but now initially - check
268 * that at 'c', we aren't putting any of the catching stones
269 * in atari. */
270 #if 1 // this might be broken?
271 #define check_catcher_danger(b, x_, y_) do { \
272 if (board_atxy(b, (x_), (y_)) != S_OFFBOARD \
273 && board_group_info(b, group_atxy(b, (x_), (y_))).libs <= 2) { \
274 if (PLDEBUGL(5)) \
275 fprintf(stderr, "ladder failed - atari at the beginning\n"); \
276 return false; \
277 } } while (0)
279 if (horiz_first) {
280 check_catcher_danger(b, x, y - yd);
281 check_catcher_danger(b, x - xd, y + yd);
282 } else {
283 check_catcher_danger(b, x - xd, y);
284 check_catcher_danger(b, x + xd, y - yd);
286 #undef check_catcher_danger
287 #endif
289 #define ladder_check(xd1_, yd1_, xd2_, yd2_, xd3_, yd3_) \
290 if (board_atxy(b, x, y) != S_NONE) { \
291 /* Did we hit a stone when playing out ladder? */ \
292 if (ladder_catcher(b, x, y, lcolor)) \
293 return true; /* ladder works */ \
294 if (board_group_info(b, group_atxy(b, x, y)).lib[0] > 0) \
295 return false; /* friend that's not in atari himself */ \
296 } else { \
297 /* No. So we are at new position. \
298 * We need to check indirect ladder breakers. */ \
299 /* . 2 x 3 . \
300 * . x o O 1 <- only at O we can check for o at 2 \
301 * x o o x . otherwise x at O would be still deadly \
302 * o o x . . \
303 * We check for o and x at 1, these are vital. \
304 * We check only for o at 2; x at 2 would mean we \
305 * need to fork (one step earlier). */ \
306 coord_t c1 = coord_xy(b, x + (xd1_), y + (yd1_)); \
307 enum stone s1 = board_at(b, c1); \
308 if (s1 == lcolor) return false; \
309 if (s1 == stone_other(lcolor)) { \
310 /* One more thing - if the position at 3 is \
311 * friendly and safe, we escaped anyway! */ \
312 coord_t c3 = coord_xy(b, x + (xd3_), y + (yd3_)); \
313 return board_at(b, c3) != lcolor \
314 || board_group_info(b, group_at(b, c3)).libs < 2; \
316 enum stone s2 = board_atxy(b, x + (xd2_), y + (yd2_)); \
317 if (s2 == lcolor) return false; \
318 /* Then, can X actually "play" 1 in the ladder? */ \
319 if (neighbor_count_at(b, c1, lcolor) + neighbor_count_at(b, c1, S_OFFBOARD) >= 2) \
320 return false; /* It would be self-atari! */ \
322 #define ladder_horiz do { if (PLDEBUGL(6)) fprintf(stderr, "%d,%d horiz step (%d,%d)\n", x, y, xd, yd); x += xd; ladder_check(xd, 0, -2 * xd, yd, 0, yd); } while (0)
323 #define ladder_vert do { if (PLDEBUGL(6)) fprintf(stderr, "%d,%d vert step of (%d,%d)\n", x, y, xd, yd); y += yd; ladder_check(0, yd, xd, -2 * yd, xd, 0); } while (0)
325 if (ladder_catcher(b, x - xd, y, lcolor))
326 ladder_horiz;
327 do {
328 ladder_vert;
329 ladder_horiz;
330 } while (1);
334 static coord_t
335 can_be_captured(struct playout_policy *p, struct board *b, enum stone capturer, coord_t c, enum stone to_play)
337 if (board_at(b, c) != stone_other(capturer)
338 || board_group_info(b, group_at(b, c)).libs > 1)
339 return pass;
341 coord_t capture = board_group_info(b, group_at(b, c)).lib[0];
342 if (PLDEBUGL(6))
343 fprintf(stderr, "can capture group %d (%s)?\n",
344 group_at(b, c), coord2sstr(capture, b));
345 struct move m; m.color = to_play; m.coord = capture;
346 /* Does that move even make sense? */
347 if (!board_is_valid_move(b, &m))
348 return pass;
349 /* Make sure capturing the group will actually
350 * do us any good. */
351 else if (is_bad_selfatari(b, to_play, capture))
352 return pass;
354 return capture;
357 static bool
358 can_be_rescued(struct playout_policy *p, struct board *b, group_t group, enum stone color, coord_t lib)
360 /* Does playing on the liberty rescue the group? */
361 if (!is_bad_selfatari(b, color, lib))
362 return true;
364 /* Then, maybe we can capture one of our neighbors? */
365 foreach_in_group(b, group) {
366 foreach_neighbor(b, c, {
367 if (!is_pass(can_be_captured(p, b, color, c, color)))
368 return true;
370 } foreach_in_group_end;
371 return false;
374 static void
375 group_atari_check(struct playout_policy *p, struct board *b, group_t group, enum stone to_play, struct move_queue *q, coord_t *ladder)
377 int qmoves_prev = q->moves;
379 /* We don't use @to_play almost anywhere since any moves here are good
380 * for both defender and attacker. */
382 enum stone color = board_at(b, group_base(group));
383 coord_t lib = board_group_info(b, group).lib[0];
385 assert(color != S_OFFBOARD && color != S_NONE);
386 if (PLDEBUGL(5))
387 fprintf(stderr, "[%s] atariiiiiiiii %s of color %d\n", coord2sstr(group, b), coord2sstr(lib, b), color);
388 assert(board_at(b, lib) == S_NONE);
390 /* Do not bother with kos. */
391 if (group_is_onestone(b, group)
392 && neighbor_count_at(b, lib, color) + neighbor_count_at(b, lib, S_OFFBOARD) == 4)
393 return;
395 /* Can we capture some neighbor? */
396 foreach_in_group(b, group) {
397 foreach_neighbor(b, c, {
398 coord_t capture = can_be_captured(p, b, color, c, to_play);
399 if (is_pass(capture))
400 continue;
402 mq_add(q, capture);
403 mq_nodup(q);
405 } foreach_in_group_end;
407 struct move m; m.color = to_play; m.coord = lib;
408 if (!board_is_valid_move(b, &m))
409 return;
411 /* Do not suicide... */
412 if (is_bad_selfatari(b, to_play, lib))
413 return;
414 /* Do not remove group that cannot be saved by the opponent. */
415 if (to_play != color && !can_be_rescued(p, b, group, color, lib))
416 return;
417 if (PLDEBUGL(6))
418 fprintf(stderr, "...escape route valid\n");
420 /* ...or play out ladders. */
421 if (ladder_catches(p, b, lib, group)) {
422 /* Sometimes we want to keep the ladder move in the
423 * queue in order to discourage it. */
424 if (!ladder)
425 return;
426 else
427 *ladder = lib;
429 if (PLDEBUGL(6))
430 fprintf(stderr, "...no ladder\n");
432 if (to_play != color) {
433 /* We are the attacker! In that case, throw away the moves
434 * that defend our groups, since we can capture the culprit. */
435 q->moves = qmoves_prev;
438 mq_add(q, lib);
439 mq_nodup(q);
442 static coord_t
443 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play)
445 struct move_queue q;
446 q.moves = 0;
448 if (b->clen == 0)
449 return pass;
451 int g_base = fast_random(b->clen);
452 for (int g = g_base; g < b->clen; g++) {
453 group_atari_check(p, b, group_at(b, group_base(b->c[g])), to_play, &q, NULL);
454 if (q.moves > 0)
455 return q.move[fast_random(q.moves)];
457 for (int g = 0; g < g_base; g++) {
458 group_atari_check(p, b, group_at(b, group_base(b->c[g])), to_play, &q, NULL);
459 if (q.moves > 0)
460 return q.move[fast_random(q.moves)];
462 return pass;
465 static coord_t
466 local_atari_check(struct playout_policy *p, struct board *b, struct move *m)
468 struct move_queue q;
469 q.moves = 0;
471 /* Did the opponent play a self-atari? */
472 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
473 group_atari_check(p, b, group_at(b, m->coord), stone_other(m->color), &q, NULL);
476 foreach_neighbor(b, m->coord, {
477 group_t g = group_at(b, c);
478 if (!g || board_group_info(b, g).libs != 1)
479 continue;
480 group_atari_check(p, b, g, stone_other(m->color), &q, NULL);
483 if (PLDEBUGL(5)) {
484 fprintf(stderr, "Local atari candidate moves: ");
485 for (int i = 0; i < q.moves; i++) {
486 fprintf(stderr, "%s ", coord2sstr(q.move[i], b));
488 fprintf(stderr, "\n");
491 int i = fast_random(q.moves);
492 return q.moves ? q.move[i] : pass;
495 static bool
496 miai_2lib(struct board *b, group_t group, enum stone color)
498 bool can_connect = false, can_pull_out = false;
499 /* We have miai if we can either connect on both libs,
500 * or connect on one lib and escape on another. (Just
501 * having two escape routes can be risky.) */
502 foreach_neighbor(b, board_group_info(b, group).lib[0], {
503 enum stone cc = board_at(b, c);
504 if (cc == S_NONE && cc != board_group_info(b, group).lib[1]) {
505 can_pull_out = true;
506 } else if (cc != color) {
507 continue;
510 group_t cg = group_at(b, c);
511 if (cg && cg != group && board_group_info(b, cg).libs > 1)
512 can_connect = true;
514 foreach_neighbor(b, board_group_info(b, group).lib[1], {
515 enum stone cc = board_at(b, c);
516 if (cc == S_NONE && cc != board_group_info(b, group).lib[0] && can_connect) {
517 return true;
518 } else if (cc != color) {
519 continue;
522 group_t cg = group_at(b, c);
523 if (cg && cg != group && board_group_info(b, cg).libs > 1)
524 return (can_connect || can_pull_out);
526 return false;
529 static void
530 group_2lib_check(struct playout_policy *p, struct board *b, group_t group, enum stone to_play, struct move_queue *q)
532 enum stone color = board_at(b, group_base(group));
533 assert(color != S_OFFBOARD && color != S_NONE);
535 if (PLDEBUGL(5))
536 fprintf(stderr, "[%s] 2lib check of color %d\n",
537 coord2sstr(group, b), color);
539 /* Do not try to atari groups that cannot be harmed. */
540 if (miai_2lib(b, group, color))
541 return;
543 for (int i = 0; i < 2; i++) {
544 coord_t lib = board_group_info(b, group).lib[i];
545 assert(board_at(b, lib) == S_NONE);
546 struct move m; m.color = to_play; m.coord = lib;
547 if (!board_is_valid_move(b, &m))
548 continue;
550 /* Don't play at the spot if it is extremely short
551 * of liberties... */
552 /* XXX: This looks harmful, could significantly
553 * prefer atari to throwin:
555 * XXXOOOOOXX
556 * .OO.....OX
557 * XXXOOOOOOX */
558 #if 0
559 if (neighbor_count_at(b, lib, stone_other(color)) + immediate_liberty_count(b, lib) < 2)
560 continue;
561 #endif
563 /* If the owner can't play at the spot, we don't want
564 * to bother either. */
565 if (is_bad_selfatari(b, color, lib))
566 continue;
568 /* Of course we don't want to play bad selfatari
569 * ourselves, if we are the attacker... */
570 if (to_play != color && is_bad_selfatari(b, to_play, lib))
571 continue;
573 /* Tasty! Crispy! Good! */
574 mq_add(q, lib);
578 static coord_t
579 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m)
581 struct move_queue q;
582 q.moves = 0;
584 /* Does the opponent have just two liberties? */
585 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
586 group_2lib_check(p, b, group_at(b, m->coord), stone_other(m->color), &q);
587 #if 0
588 /* We always prefer to take off an enemy chain liberty
589 * before pulling out ourselves. */
590 /* XXX: We aren't guaranteed to return to that group
591 * later. */
592 if (q.moves)
593 return q.move[fast_random(q.moves)];
594 #endif
597 /* Then he took a third liberty from neighboring chain? */
598 foreach_neighbor(b, m->coord, {
599 group_t g = group_at(b, c);
600 if (!g || board_group_info(b, g).libs != 2)
601 continue;
602 group_2lib_check(p, b, g, stone_other(m->color), &q);
605 if (PLDEBUGL(5)) {
606 fprintf(stderr, "Local 2lib candidate moves: ");
607 for (int i = 0; i < q.moves; i++) {
608 fprintf(stderr, "%s ", coord2sstr(q.move[i], b));
610 fprintf(stderr, "\n");
613 int i = fast_random(q.moves);
614 return q.moves ? q.move[i] : pass;
617 coord_t
618 playout_moggy_choose(struct playout_policy *p, struct board *b, enum stone to_play)
620 struct moggy_policy *pp = p->data;
621 coord_t c;
623 if (PLDEBUGL(5))
624 board_print(b, stderr);
626 /* Local checks */
627 if (!is_pass(b->last_move.coord)) {
628 /* Local group in atari? */
629 if (pp->lcapturerate > fast_random(100)) {
630 c = local_atari_check(p, b, &b->last_move);
631 if (!is_pass(c))
632 return c;
635 /* Local group can be PUT in atari? */
636 if (pp->atarirate > fast_random(100)) {
637 c = local_2lib_check(p, b, &b->last_move);
638 if (!is_pass(c))
639 return c;
642 /* Check for patterns we know */
643 if (pp->patternrate > fast_random(100)) {
644 c = apply_pattern(p, b, &b->last_move,
645 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL);
646 if (!is_pass(c))
647 return c;
651 /* Global checks */
653 /* Any groups in atari? */
654 if (pp->capturerate > fast_random(100)) {
655 c = global_atari_check(p, b, to_play);
656 if (!is_pass(c))
657 return c;
660 /* Fill board */
661 int fbtries = b->flen / 8;
662 for (int i = 0; i < (fbtries < pp->fillboardtries ? fbtries : pp->fillboardtries); i++) {
663 coord_t coord = b->f[fast_random(b->flen)];
664 if (is_pass(coord) || immediate_liberty_count(b, coord) != 4)
665 continue;
666 foreach_diag_neighbor(b, coord) {
667 if (board_at(b, c) != S_NONE)
668 goto next_try;
669 } foreach_diag_neighbor_end;
670 return coord;
671 next_try:;
674 return pass;
678 static int
679 assess_local_bonus(struct playout_policy *p, struct board *board, coord_t a, coord_t b, int games)
681 struct moggy_policy *pp = p->data;
682 if (!pp->assess_local)
683 return games;
685 int dx = abs(coord_x(a, board) - coord_x(b, board));
686 int dy = abs(coord_y(a, board) - coord_y(b, board));
687 /* adjecent move, directly or diagonally? */
688 if (dx + dy <= 1 + (dx && dy))
689 return games;
690 else
691 return games / 2;
694 void
695 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
697 struct moggy_policy *pp = p->data;
698 struct board *b = map->b;
699 struct move_queue q; q.moves = 0;
701 if (board_group_info(b, g).libs > 2)
702 return;
704 if (PLDEBUGL(5)) {
705 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
706 board_print(b, stderr);
709 if (board_group_info(b, g).libs == 2) {
710 if (!pp->atarirate)
711 return;
712 group_2lib_check(p, b, g, map->to_play, &q);
713 while (q.moves--) {
714 coord_t coord = q.move[q.moves];
715 if (PLDEBUGL(5))
716 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
717 int assess = assess_local_bonus(p, b, b->last_move.coord, coord, games) / 2;
718 add_prior_value(map, coord, assess, assess);
720 return;
723 /* This group, sir, is in atari! */
725 if (!pp->capturerate && !pp->lcapturerate && !pp->ladderassess)
726 return;
728 coord_t ladder = pass;
729 group_atari_check(p, b, g, map->to_play, &q, &ladder);
730 while (q.moves--) {
731 coord_t coord = q.move[q.moves];
733 /* _Never_ play here if this move plays out
734 * a caught ladder. */
735 if (coord == ladder) {
736 /* Note that the opposite is not guarded against;
737 * we do not advise against capturing a laddered
738 * group (but we don't encourage it either). Such
739 * a move can simplify tactical situations if we
740 * can afford it. */
741 if (!pp->ladderassess || map->to_play != board_at(b, g))
742 continue;
743 /* FIXME: We give the malus even if this move
744 * captures another group. */
745 if (PLDEBUGL(5))
746 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
747 add_prior_value(map, coord, -games, games);
748 continue;
751 if (!pp->capturerate && !pp->lcapturerate)
752 continue;
754 if (PLDEBUGL(5))
755 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
756 int assess = assess_local_bonus(p, b, b->last_move.coord, coord, games) * 2;
757 add_prior_value(map, coord, assess, assess);
762 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
764 struct moggy_policy *pp = p->data;
765 struct board *b = map->b;
767 if (PLDEBUGL(5)) {
768 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
769 board_print(b, stderr);
772 /* Is this move a self-atari? */
773 if (pp->selfatarirate) {
774 if (is_bad_selfatari(b, map->to_play, coord)) {
775 if (PLDEBUGL(5))
776 fprintf(stderr, "0.0: self-atari\n");
777 return -games;
781 /* Pattern check */
782 if (pp->patternrate) {
783 struct move m = { .color = map->to_play, .coord = coord };
784 if (test_pattern3_here(&pp->patterns, b, &m)) {
785 if (PLDEBUGL(5))
786 fprintf(stderr, "1.0: pattern\n");
787 return assess_local_bonus(p, b, b->last_move.coord, coord, games);
791 return 0;
794 void
795 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
797 struct moggy_policy *pp = p->data;
799 /* First, go through all endangered groups. */
800 if (pp->lcapturerate || pp->capturerate || pp->atarirate || pp->ladderassess)
801 for (group_t g = 1; g < board_size2(map->b); g++)
802 if (group_at(map->b, g) == g)
803 playout_moggy_assess_group(p, map, g, games);
805 /* Then, assess individual moves. */
806 if (!pp->patternrate && !pp->selfatarirate)
807 return;
808 foreach_point(map->b) {
809 if (!map->consider[c])
810 continue;
811 int assess = playout_moggy_assess_one(p, map, c, games);
812 if (!assess)
813 continue;
814 add_prior_value(map, c, assess, abs(assess));
815 } foreach_point_end;
818 bool
819 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
821 struct moggy_policy *pp = p->data;
823 /* The idea is simple for now - never allow self-atari moves.
824 * They suck in general, but this also permits us to actually
825 * handle seki in the playout stage. */
827 if (fast_random(100) >= pp->selfatarirate) {
828 if (PLDEBUGL(5))
829 fprintf(stderr, "skipping sar test\n");
830 return true;
832 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
833 if (PLDEBUGL(5) && selfatari)
834 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
835 stone2str(m->color), coord2sstr(m->coord, b));
836 return !selfatari;
840 struct playout_policy *
841 playout_moggy_init(char *arg)
843 struct playout_policy *p = calloc(1, sizeof(*p));
844 struct moggy_policy *pp = calloc(1, sizeof(*pp));
845 p->data = pp;
846 p->choose = playout_moggy_choose;
847 p->assess = playout_moggy_assess;
848 p->permit = playout_moggy_permit;
850 int rate = 90;
852 pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate = pp->selfatarirate = -1;
853 pp->ladders = pp->borderladders = true;
854 pp->ladderassess = true;
856 if (arg) {
857 char *optspec, *next = arg;
858 while (*next) {
859 optspec = next;
860 next += strcspn(next, ":");
861 if (*next) { *next++ = 0; } else { *next = 0; }
863 char *optname = optspec;
864 char *optval = strchr(optspec, '=');
865 if (optval) *optval++ = 0;
867 if (!strcasecmp(optname, "lcapturerate") && optval) {
868 pp->lcapturerate = atoi(optval);
869 } else if (!strcasecmp(optname, "atarirate") && optval) {
870 pp->atarirate = atoi(optval);
871 } else if (!strcasecmp(optname, "capturerate") && optval) {
872 pp->capturerate = atoi(optval);
873 } else if (!strcasecmp(optname, "patternrate") && optval) {
874 pp->patternrate = atoi(optval);
875 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
876 pp->selfatarirate = atoi(optval);
877 } else if (!strcasecmp(optname, "rate") && optval) {
878 rate = atoi(optval);
879 } else if (!strcasecmp(optname, "fillboardtries")) {
880 pp->fillboardtries = atoi(optval);
881 } else if (!strcasecmp(optname, "ladders")) {
882 pp->ladders = optval && *optval == '0' ? false : true;
883 } else if (!strcasecmp(optname, "borderladders")) {
884 pp->borderladders = optval && *optval == '0' ? false : true;
885 } else if (!strcasecmp(optname, "ladderassess")) {
886 pp->ladderassess = optval && *optval == '0' ? false : true;
887 } else if (!strcasecmp(optname, "assess_local")) {
888 pp->assess_local = optval && *optval == '0' ? false : true;
889 } else if (!strcasecmp(optname, "pattern2")) {
890 pp->pattern2 = optval && *optval == '0' ? false : true;
891 } else {
892 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
893 exit(1);
897 if (pp->lcapturerate == -1) pp->lcapturerate = rate;
898 if (pp->atarirate == -1) pp->atarirate = rate;
899 if (pp->capturerate == -1) pp->capturerate = rate;
900 if (pp->patternrate == -1) pp->patternrate = rate;
901 if (pp->selfatarirate == -1) pp->selfatarirate = rate;
903 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
905 return p;