TESTS: 09368 9x9 tests
[pachi/peepo.git] / playout / moggy.c
blob8b4bd69992012f9a88e535f654207c2cd5a9840a
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 struct group_state {
35 enum {
36 G_ATARI,
37 G_2LIB, /* Unused. */
38 G_SAFE /* Unused. */
39 } status:2;
41 /* Below, we keep track of each trait for each |color_to_play */
42 int capturable_ready:2; // is @capturable meaningful?
43 int capturable:2;
45 int can_countercapture_ready:2;
46 int can_countercapture:2;
49 /* Cache of evaluation of various board features. */
50 struct board_state {
51 int bsize2;
52 hash_t hash;
53 struct group_state *groups; /* [board_size2()], indexed by group_t */
54 unsigned char *groups_known; /* Bitmap of known groups. */
57 /* Using board cache: this turns out to be actually a 10% slowdown,
58 * since we reuse data in the cache only very little within single
59 * move. */
60 // #define CACHE_STATE
61 /* Reusing board cache across moves if they are successive on the
62 * board; only cache entries within cfg distance 2 of the last move
63 * are cleared. */
64 // #define PERSISTENT_STATE
66 #ifdef CACHE_STATE
67 static __thread struct board_state *ss;
69 static bool
70 board_state_reuse(struct board_state *s, struct board *b)
72 /* Decide how much of the board state we can reuse. */
73 /* We do not cache ladder decisions, so we don't have
74 * to worry about this. */
75 coord_t c = b->last_move.coord;
77 if (unlikely(is_pass(c))) {
78 /* Passes don't change anything. */
79 return true;
82 if (unlikely(board_at(b, c) == S_NONE)) {
83 /* Suicide is hopeless. */
84 return false;
87 /* XXX: we can make some moves self-atari. */
89 if (neighbor_count_at(b, c, S_BLACK) + neighbor_count_at(b, c, S_WHITE) == 0) {
90 /* We are not taking off liberties of any other stones. */
91 return true;
94 return false;
97 static inline struct board_state *
98 board_state_init(struct board *b)
100 if (ss) {
101 if (ss->bsize2 != board_size2(b)) {
102 free(ss->groups);
103 free(ss->groups_known);
104 free(ss); ss = NULL;
106 #ifdef PERSISTENT_STATE
107 /* Only one stone added to the board, nothing removed. */
108 else if (ss->hash == (b->hash ^ hash_at(b, b->last_move.coord, b->last_move.color))) {
109 ss->hash = b->hash;
110 if (likely(board_state_reuse(ss, b)))
111 return ss;
113 #endif
115 if (!ss) {
116 ss = malloc(sizeof(*ss));
117 ss->bsize2 = board_size2(b);
118 ss->groups = malloc(board_size2(b) * sizeof(*ss->groups));
119 ss->groups_known = malloc(board_size2(b) / 8 + 1);
121 ss->hash = b->hash;
122 memset(ss->groups_known, 0, board_size2(b) / 8 + 1);
123 return ss;
126 #define group_is_known(s, g) (s->groups_known[g >> 3] & (1 << (g & 7)))
127 #define group_set_known(s, g) (s->groups_known[g >> 3] |= (1 << (g & 7)))
128 #define group_trait_ready(s, g, color, gstat, trait) do { \
129 if (!group_is_known(s, g)) { \
130 memset(&s->groups[g], 0, sizeof(s->groups[g])); \
131 group_set_known(s, g); \
133 s->groups[g].status = gstat; \
134 s->groups[g].trait ## _ready |= color; \
135 } while (0)
136 #define group_trait_is_ready(s, g, color, trait) (s->groups[g].trait ## _ready & color)
137 #define group_trait_set(s, g, color, trait, val) s->groups[g].trait = (s->groups[g].trait & ~color) | (!!val * color)
138 #define group_trait_get(s, g, color, trait) (s->groups[g].trait & color)
140 #else
142 #define board_state_init(b) NULL
143 #define group_is_known(s, g) false
144 #define group_set_known(s, g)
145 #define group_trait_ready(s, g, color, gstat, trait)
146 #define group_trait_is_ready(s, g, color, trait) false
147 #define group_trait_set(s, g, color, trait, val)
148 #define group_trait_get(s, g, color, trait) false
149 #endif
152 static char moggy_patterns_src[][11] = {
153 /* hane pattern - enclosing hane */
154 "XOX"
155 "..."
156 "???",
157 /* hane pattern - non-cutting hane */
158 "XO."
159 "..."
160 "?.?",
161 /* hane pattern - magari */
162 "XO?"
163 "X.."
164 "x.?",
165 /* hane pattern - thin hane */
166 "XOO"
167 "..."
168 "?.?" "X",
169 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
170 ".O."
171 "X.."
172 "...",
173 /* cut1 pattern (kiri) - unprotected cut */
174 "XO?"
175 "O.o"
176 "?o?",
177 /* cut1 pattern (kiri) - peeped cut */
178 "XO?"
179 "O.X"
180 "???",
181 /* cut2 pattern (de) */
182 "?X?"
183 "O.O"
184 "ooo",
185 /* cut keima (not in Mogo) */
186 "OX?"
187 "o.O"
188 "???", /* o?? has some pathological tsumego cases */
189 /* side pattern - chase */
190 "X.?"
191 "O.?"
192 "##?",
193 /* side pattern - weirdness (SUSPICIOUS) */
194 "?X?"
195 "X.O"
196 "###",
197 /* side pattern - sagari (SUSPICIOUS) */
198 "?XO"
199 "x.x" /* Mogo has "x.?" */
200 "###" /* Mogo has "X" */,
201 /* side pattern - throw-in (SUSPICIOUS) */
202 #if 0
203 "?OX"
204 "o.O"
205 "?##" "X",
206 #endif
207 /* side pattern - cut (SUSPICIOUS) */
208 "?OX"
209 "X.O"
210 "###" /* Mogo has "X" */,
212 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
214 static void
215 apply_pattern_here(struct playout_policy *p,
216 struct board *b, struct move *m, struct move_queue *q)
218 struct moggy_policy *pp = p->data;
219 if (test_pattern3_here(&pp->patterns, b, m))
220 mq_add(q, m->coord);
223 /* Check if we match any pattern around given move (with the other color to play). */
224 static coord_t
225 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm)
227 struct move_queue q;
228 q.moves = 0;
230 /* Suicides do not make any patterns and confuse us. */
231 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
232 return pass;
234 foreach_neighbor(b, m->coord, {
235 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
236 if (board_is_valid_move(b, &m2))
237 apply_pattern_here(p, b, &m2, &q);
239 foreach_diag_neighbor(b, m->coord) {
240 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
241 if (board_is_valid_move(b, &m2))
242 apply_pattern_here(p, b, &m2, &q);
243 } foreach_diag_neighbor_end;
245 if (mm) { /* Second move for pattern searching */
246 foreach_neighbor(b, mm->coord, {
247 if (coord_is_8adjecent(m->coord, c, b))
248 continue;
249 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
250 if (board_is_valid_move(b, &m2))
251 apply_pattern_here(p, b, &m2, &q);
253 foreach_diag_neighbor(b, mm->coord) {
254 if (coord_is_8adjecent(m->coord, c, b))
255 continue;
256 struct move m2; m2.coord = c; m2.color = stone_other(m->color);
257 if (board_is_valid_move(b, &m2))
258 apply_pattern_here(p, b, &m2, &q);
259 } foreach_diag_neighbor_end;
262 if (PLDEBUGL(5))
263 mq_print(&q, b, "Pattern");
265 return mq_pick(&q);
269 static bool
270 can_be_captured(struct playout_policy *p, struct board_state *s,
271 struct board *b, group_t g, enum stone to_play)
273 if (group_is_known(s, g) && group_trait_is_ready(s, g, to_play, capturable)) {
274 /* We have already seen this group. */
275 assert(s->groups[g].status == G_ATARI);
276 if (group_trait_get(s, g, to_play, capturable))
277 return true;
278 else
279 return false;
282 /* Cache miss. Set up cache entry, default at capturable = false. */
283 group_trait_ready(s, g, to_play, G_ATARI, capturable);
285 coord_t capture = board_group_info(b, g).lib[0];
286 if (PLDEBUGL(6))
287 fprintf(stderr, "can capture group %d (%s)?\n",
288 g, coord2sstr(capture, b));
289 /* Does playing on the liberty usefully capture the group? */
290 struct move m; m.color = to_play; m.coord = capture;
291 if (board_is_valid_move(b, &m) && !is_bad_selfatari(b, to_play, capture)) {
292 group_trait_set(s, g, to_play, capturable, true);
293 return true;
296 return false;
299 /* For given position @c, decide if this is a group that is in danger from
300 * @capturer and @to_play can do anything about it (play at the last
301 * liberty to either capture or escape). */
302 /* Note that @to_play is important; e.g. consider snapback, it's good
303 * to play at the last liberty by attacker, but not defender. */
304 static __attribute__((always_inline)) bool
305 capturable_group(struct playout_policy *p, struct board_state *s,
306 struct board *b, enum stone capturer, coord_t c,
307 enum stone to_play)
309 group_t g = group_at(b, c);
310 if (likely(board_at(b, c) != stone_other(capturer)
311 || board_group_info(b, g).libs > 1))
312 return false;
314 return can_be_captured(p, s, b, g, to_play);
317 /* For given atari group @group owned by @owner, decide if @to_play
318 * can save it / keep it in danger by dealing with one of the
319 * neighboring groups. */
320 static bool
321 can_countercapture(struct playout_policy *p, struct board_state *s,
322 struct board *b, enum stone owner, group_t g,
323 enum stone to_play, struct move_queue *q)
325 if (group_is_known(s, g) && group_trait_is_ready(s, g, to_play, can_countercapture)) {
326 /* We have already seen this group. */
327 assert(s->groups[g].status == G_ATARI);
328 if (group_trait_get(s, g, to_play, can_countercapture)) {
329 if (q) { /* Scan for countercapture liberties. */
330 goto scan;
332 return true;
333 } else {
334 return false;
338 /* Cache miss. Set up cache entry, default at can_countercapture = true. */
339 group_trait_ready(s, g, to_play, G_ATARI, can_countercapture);
340 group_trait_set(s, g, to_play, can_countercapture, true);
342 scan:;
343 int qmoves_prev = q ? q->moves : 0;
345 foreach_in_group(b, g) {
346 foreach_neighbor(b, c, {
347 if (!capturable_group(p, s, b, owner, c, to_play))
348 continue;
350 if (!q) {
351 return true;
353 mq_add(q, board_group_info(b, group_at(b, c)).lib[0]);
354 mq_nodup(q);
356 } foreach_in_group_end;
358 bool can = q ? q->moves > qmoves_prev : false;
359 group_trait_set(s, g, to_play, can_countercapture, can);
360 return can;
363 static bool
364 can_be_rescued(struct playout_policy *p, struct board_state *s,
365 struct board *b, group_t group, enum stone color)
367 /* Does playing on the liberty rescue the group? */
368 if (can_be_captured(p, s, b, group, color))
369 return true;
371 /* Then, maybe we can capture one of our neighbors? */
372 return can_countercapture(p, s, b, color, group, color, NULL);
375 static void
376 group_atari_check(struct playout_policy *p, struct board *b, group_t group, enum stone to_play,
377 struct move_queue *q, coord_t *ladder, struct board_state *s)
379 struct moggy_policy *pp = p->data;
380 int qmoves_prev = q->moves;
382 /* We don't use @to_play almost anywhere since any moves here are good
383 * for both defender and attacker. */
385 enum stone color = board_at(b, group_base(group));
386 coord_t lib = board_group_info(b, group).lib[0];
388 assert(color != S_OFFBOARD && color != S_NONE);
389 if (PLDEBUGL(5))
390 fprintf(stderr, "[%s] atariiiiiiiii %s of color %d\n",
391 coord2sstr(group, b), coord2sstr(lib, b), color);
392 assert(board_at(b, lib) == S_NONE);
394 /* Do not bother with kos. */
395 if (group_is_onestone(b, group)
396 && neighbor_count_at(b, lib, color) + neighbor_count_at(b, lib, S_OFFBOARD) == 4)
397 return;
399 /* Can we capture some neighbor? */
400 can_countercapture(p, s, b, color, group, to_play, q);
402 /* Do not suicide... */
403 if (!can_be_captured(p, s, b, group, color))
404 return;
405 /* Do not remove group that cannot be saved by the opponent. */
406 if (to_play != color && !can_be_rescued(p, s, b, group, color))
407 return;
408 if (PLDEBUGL(6))
409 fprintf(stderr, "...escape route valid\n");
411 /* ...or play out ladders. */
412 if (is_ladder(b, lib, group, pp->borderladders, pp->ladders)) {
413 /* Sometimes we want to keep the ladder move in the
414 * queue in order to discourage it. */
415 if (!ladder)
416 return;
417 else
418 *ladder = lib;
420 if (PLDEBUGL(6))
421 fprintf(stderr, "...no ladder\n");
423 if (to_play != color) {
424 /* We are the attacker! In that case, throw away the moves
425 * that defend our groups, since we can capture the culprit. */
426 q->moves = qmoves_prev;
429 mq_add(q, lib);
430 mq_nodup(q);
433 static coord_t
434 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct board_state *s)
436 struct move_queue q;
437 q.moves = 0;
439 if (b->clen == 0)
440 return pass;
442 int g_base = fast_random(b->clen);
443 for (int g = g_base; g < b->clen; g++) {
444 group_atari_check(p, b, group_at(b, group_base(b->c[g])), to_play, &q, NULL, s);
445 if (q.moves > 0)
446 return mq_pick(&q);
448 for (int g = 0; g < g_base; g++) {
449 group_atari_check(p, b, group_at(b, group_base(b->c[g])), to_play, &q, NULL, s);
450 if (q.moves > 0)
451 return mq_pick(&q);
453 return pass;
456 static coord_t
457 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct board_state *s)
459 struct move_queue q;
460 q.moves = 0;
462 /* Did the opponent play a self-atari? */
463 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
464 group_atari_check(p, b, group_at(b, m->coord), stone_other(m->color), &q, NULL, s);
467 foreach_neighbor(b, m->coord, {
468 group_t g = group_at(b, c);
469 if (!g || board_group_info(b, g).libs != 1)
470 continue;
471 group_atari_check(p, b, g, stone_other(m->color), &q, NULL, s);
474 if (PLDEBUGL(5))
475 mq_print(&q, b, "Local atari");
477 return mq_pick(&q);
480 static bool
481 miai_2lib(struct board *b, group_t group, enum stone color)
483 bool can_connect = false, can_pull_out = false;
484 /* We have miai if we can either connect on both libs,
485 * or connect on one lib and escape on another. (Just
486 * having two escape routes can be risky.) */
487 foreach_neighbor(b, board_group_info(b, group).lib[0], {
488 enum stone cc = board_at(b, c);
489 if (cc == S_NONE && cc != board_group_info(b, group).lib[1]) {
490 can_pull_out = true;
491 } else if (cc != color) {
492 continue;
495 group_t cg = group_at(b, c);
496 if (cg && cg != group && board_group_info(b, cg).libs > 1)
497 can_connect = true;
499 foreach_neighbor(b, board_group_info(b, group).lib[1], {
500 enum stone cc = board_at(b, c);
501 if (cc == S_NONE && cc != board_group_info(b, group).lib[0] && can_connect) {
502 return true;
503 } else if (cc != color) {
504 continue;
507 group_t cg = group_at(b, c);
508 if (cg && cg != group && board_group_info(b, cg).libs > 1)
509 return (can_connect || can_pull_out);
511 return false;
514 static void
515 group_2lib_check(struct playout_policy *p, struct board *b, group_t group, enum stone to_play,
516 struct move_queue *q, struct board_state *s)
518 enum stone color = board_at(b, group_base(group));
519 assert(color != S_OFFBOARD && color != S_NONE);
521 if (PLDEBUGL(5))
522 fprintf(stderr, "[%s] 2lib check of color %d\n",
523 coord2sstr(group, b), color);
525 /* Do not try to atari groups that cannot be harmed. */
526 if (miai_2lib(b, group, color))
527 return;
529 for (int i = 0; i < 2; i++) {
530 coord_t lib = board_group_info(b, group).lib[i];
531 assert(board_at(b, lib) == S_NONE);
532 struct move m; m.color = to_play; m.coord = lib;
533 if (!board_is_valid_move(b, &m))
534 continue;
536 /* Don't play at the spot if it is extremely short
537 * of liberties... */
538 /* XXX: This looks harmful, could significantly
539 * prefer atari to throwin:
541 * XXXOOOOOXX
542 * .OO.....OX
543 * XXXOOOOOOX */
544 #if 0
545 if (neighbor_count_at(b, lib, stone_other(color)) + immediate_liberty_count(b, lib) < 2)
546 continue;
547 #endif
549 /* If the owner can't play at the spot, we don't want
550 * to bother either. */
551 if (is_bad_selfatari(b, color, lib))
552 continue;
554 /* Of course we don't want to play bad selfatari
555 * ourselves, if we are the attacker... */
556 if (to_play != color && is_bad_selfatari(b, to_play, lib))
557 continue;
559 /* Tasty! Crispy! Good! */
560 mq_add(q, lib);
564 static coord_t
565 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct board_state *s)
567 struct move_queue q;
568 q.moves = 0;
570 /* Does the opponent have just two liberties? */
571 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
572 group_2lib_check(p, b, group_at(b, m->coord), stone_other(m->color), &q, s);
573 #if 0
574 /* We always prefer to take off an enemy chain liberty
575 * before pulling out ourselves. */
576 /* XXX: We aren't guaranteed to return to that group
577 * later. */
578 if (q.moves)
579 return q.move[fast_random(q.moves)];
580 #endif
583 /* Then he took a third liberty from neighboring chain? */
584 foreach_neighbor(b, m->coord, {
585 group_t g = group_at(b, c);
586 if (!g || board_group_info(b, g).libs != 2)
587 continue;
588 group_2lib_check(p, b, g, stone_other(m->color), &q, s);
591 if (PLDEBUGL(5))
592 mq_print(&q, b, "Local 2lib");
594 return mq_pick(&q);
597 coord_t
598 playout_moggy_choose(struct playout_policy *p, struct board *b, enum stone to_play)
600 struct moggy_policy *pp = p->data;
601 coord_t c;
603 struct board_state *s = board_state_init(b);
605 if (PLDEBUGL(5))
606 board_print(b, stderr);
608 /* Local checks */
609 if (!is_pass(b->last_move.coord)) {
610 /* Local group in atari? */
611 if (pp->lcapturerate > fast_random(100)) {
612 c = local_atari_check(p, b, &b->last_move, s);
613 if (!is_pass(c))
614 return c;
617 /* Local group can be PUT in atari? */
618 if (pp->atarirate > fast_random(100)) {
619 c = local_2lib_check(p, b, &b->last_move, s);
620 if (!is_pass(c))
621 return c;
624 /* Check for patterns we know */
625 if (pp->patternrate > fast_random(100)) {
626 c = apply_pattern(p, b, &b->last_move,
627 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL);
628 if (!is_pass(c))
629 return c;
633 /* Global checks */
635 /* Any groups in atari? */
636 if (pp->capturerate > fast_random(100)) {
637 c = global_atari_check(p, b, to_play, s);
638 if (!is_pass(c))
639 return c;
642 /* Fill board */
643 int fbtries = b->flen / 8;
644 for (int i = 0; i < (fbtries < pp->fillboardtries ? fbtries : pp->fillboardtries); i++) {
645 coord_t coord = b->f[fast_random(b->flen)];
646 if (is_pass(coord) || immediate_liberty_count(b, coord) != 4)
647 continue;
648 foreach_diag_neighbor(b, coord) {
649 if (board_at(b, c) != S_NONE)
650 goto next_try;
651 } foreach_diag_neighbor_end;
652 return coord;
653 next_try:;
656 return pass;
660 static int
661 assess_local_bonus(struct playout_policy *p, struct board *board, coord_t a, coord_t b, int games)
663 struct moggy_policy *pp = p->data;
664 if (!pp->assess_local)
665 return games;
667 int dx = abs(coord_x(a, board) - coord_x(b, board));
668 int dy = abs(coord_y(a, board) - coord_y(b, board));
669 /* adjecent move, directly or diagonally? */
670 if (dx + dy <= 1 + (dx && dy))
671 return games;
672 else
673 return games / 2;
676 void
677 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games,
678 struct board_state *s)
680 struct moggy_policy *pp = p->data;
681 struct board *b = map->b;
682 struct move_queue q; q.moves = 0;
684 if (board_group_info(b, g).libs > 2)
685 return;
687 if (PLDEBUGL(5)) {
688 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
689 board_print(b, stderr);
692 if (board_group_info(b, g).libs == 2) {
693 if (!pp->atarirate)
694 return;
695 group_2lib_check(p, b, g, map->to_play, &q, s);
696 while (q.moves--) {
697 coord_t coord = q.move[q.moves];
698 if (PLDEBUGL(5))
699 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
700 int assess = assess_local_bonus(p, b, b->last_move.coord, coord, games) / 2;
701 add_prior_value(map, coord, 1, assess);
703 return;
706 /* This group, sir, is in atari! */
708 if (!pp->capturerate && !pp->lcapturerate && !pp->ladderassess)
709 return;
711 coord_t ladder = pass;
712 group_atari_check(p, b, g, map->to_play, &q, &ladder, s);
713 while (q.moves--) {
714 coord_t coord = q.move[q.moves];
716 /* _Never_ play here if this move plays out
717 * a caught ladder. */
718 if (coord == ladder) {
719 /* Note that the opposite is not guarded against;
720 * we do not advise against capturing a laddered
721 * group (but we don't encourage it either). Such
722 * a move can simplify tactical situations if we
723 * can afford it. */
724 if (!pp->ladderassess || map->to_play != board_at(b, g))
725 continue;
726 /* FIXME: We give the malus even if this move
727 * captures another group. */
728 if (PLDEBUGL(5))
729 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
730 add_prior_value(map, coord, 0, games);
731 continue;
734 if (!pp->capturerate && !pp->lcapturerate)
735 continue;
737 if (PLDEBUGL(5))
738 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
739 int assess = assess_local_bonus(p, b, b->last_move.coord, coord, games) * 2;
740 add_prior_value(map, coord, 1, assess);
744 void
745 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
747 struct moggy_policy *pp = p->data;
748 struct board *b = map->b;
750 if (PLDEBUGL(5)) {
751 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
752 board_print(b, stderr);
755 /* Is this move a self-atari? */
756 if (pp->selfatarirate) {
757 if (is_bad_selfatari(b, map->to_play, coord)) {
758 if (PLDEBUGL(5))
759 fprintf(stderr, "0.0: self-atari\n");
760 add_prior_value(map, coord, 0, games);
761 return;
765 /* Pattern check */
766 if (pp->patternrate) {
767 struct move m = { .color = map->to_play, .coord = coord };
768 if (test_pattern3_here(&pp->patterns, b, &m)) {
769 if (PLDEBUGL(5))
770 fprintf(stderr, "1.0: pattern\n");
771 int assess = assess_local_bonus(p, b, b->last_move.coord, coord, games);
772 add_prior_value(map, coord, 1, assess);
776 return;
779 void
780 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
782 struct moggy_policy *pp = p->data;
784 struct board_state *s = board_state_init(map->b);
786 /* First, go through all endangered groups. */
787 if (pp->lcapturerate || pp->capturerate || pp->atarirate || pp->ladderassess)
788 for (group_t g = 1; g < board_size2(map->b); g++)
789 if (group_at(map->b, g) == g)
790 playout_moggy_assess_group(p, map, g, games, s);
792 /* Then, assess individual moves. */
793 if (!pp->patternrate && !pp->selfatarirate)
794 return;
795 foreach_point(map->b) {
796 if (map->consider[c])
797 playout_moggy_assess_one(p, map, c, games);
798 } foreach_point_end;
801 bool
802 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
804 struct moggy_policy *pp = p->data;
806 /* The idea is simple for now - never allow self-atari moves.
807 * They suck in general, but this also permits us to actually
808 * handle seki in the playout stage. */
810 if (fast_random(100) >= pp->selfatarirate) {
811 if (PLDEBUGL(5))
812 fprintf(stderr, "skipping sar test\n");
813 return true;
815 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
816 if (PLDEBUGL(5) && selfatari)
817 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
818 stone2str(m->color), coord2sstr(m->coord, b));
819 return !selfatari;
823 struct playout_policy *
824 playout_moggy_init(char *arg)
826 struct playout_policy *p = calloc(1, sizeof(*p));
827 struct moggy_policy *pp = calloc(1, sizeof(*pp));
828 p->data = pp;
829 p->choose = playout_moggy_choose;
830 p->assess = playout_moggy_assess;
831 p->permit = playout_moggy_permit;
833 int rate = 90;
835 pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate = pp->selfatarirate = -1;
836 pp->ladders = pp->borderladders = true;
837 pp->ladderassess = true;
839 if (arg) {
840 char *optspec, *next = arg;
841 while (*next) {
842 optspec = next;
843 next += strcspn(next, ":");
844 if (*next) { *next++ = 0; } else { *next = 0; }
846 char *optname = optspec;
847 char *optval = strchr(optspec, '=');
848 if (optval) *optval++ = 0;
850 if (!strcasecmp(optname, "lcapturerate") && optval) {
851 pp->lcapturerate = atoi(optval);
852 } else if (!strcasecmp(optname, "atarirate") && optval) {
853 pp->atarirate = atoi(optval);
854 } else if (!strcasecmp(optname, "capturerate") && optval) {
855 pp->capturerate = atoi(optval);
856 } else if (!strcasecmp(optname, "patternrate") && optval) {
857 pp->patternrate = atoi(optval);
858 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
859 pp->selfatarirate = atoi(optval);
860 } else if (!strcasecmp(optname, "rate") && optval) {
861 rate = atoi(optval);
862 } else if (!strcasecmp(optname, "fillboardtries")) {
863 pp->fillboardtries = atoi(optval);
864 } else if (!strcasecmp(optname, "ladders")) {
865 pp->ladders = optval && *optval == '0' ? false : true;
866 } else if (!strcasecmp(optname, "borderladders")) {
867 pp->borderladders = optval && *optval == '0' ? false : true;
868 } else if (!strcasecmp(optname, "ladderassess")) {
869 pp->ladderassess = optval && *optval == '0' ? false : true;
870 } else if (!strcasecmp(optname, "assess_local")) {
871 pp->assess_local = optval && *optval == '0' ? false : true;
872 } else if (!strcasecmp(optname, "pattern2")) {
873 pp->pattern2 = optval && *optval == '0' ? false : true;
874 } else {
875 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
876 exit(1);
880 if (pp->lcapturerate == -1) pp->lcapturerate = rate;
881 if (pp->atarirate == -1) pp->atarirate = rate;
882 if (pp->capturerate == -1) pp->capturerate = rate;
883 if (pp->patternrate == -1) pp->patternrate = rate;
884 if (pp->selfatarirate == -1) pp->selfatarirate = rate;
886 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
888 return p;