Move group_atari_check() et al. to tactics/1lib.c.
[pachi/ann.git] / playout / moggy.c
blob14ea34e88a939b7c163a47040e4802aed157bb37
1 /* Playout policy by stochastically applying a fixed set of decision
2 * rules in given order - modelled after the intelligent playouts
3 * in the Mogo engine. */
5 #include <assert.h>
6 #include <math.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 #define DEBUG
11 #include "board.h"
12 #include "debug.h"
13 #include "joseki/base.h"
14 #include "mq.h"
15 #include "pattern3.h"
16 #include "playout.h"
17 #include "playout/moggy.h"
18 #include "random.h"
19 #include "tactics/1lib.h"
20 #include "tactics/ladder.h"
21 #include "tactics/selfatari.h"
22 #include "uct/prior.h"
24 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
26 /* Whether to avoid capturing/atariing doomed groups (this is big
27 * performance hit and may reduce playouts balance; it does increase
28 * the strength, but not quite proportionally to the performance). */
29 //#define NO_DOOMED_GROUPS
32 /* Move queue tags: */
33 enum mq_tag {
34 MQ_KO = 1,
35 MQ_LATARI,
36 MQ_L2LIB,
37 MQ_PAT3,
38 MQ_GATARI,
39 MQ_JOSEKI,
40 MQ_MAX
44 /* Note that the context can be shared by multiple threads! */
46 struct moggy_policy {
47 unsigned int lcapturerate, atarirate, capturerate, patternrate, korate, josekirate;
48 unsigned int selfatarirate, alwaysccaprate;
49 unsigned int fillboardtries;
50 int koage;
51 /* Whether to look for patterns around second-to-last move. */
52 bool pattern2;
53 /* Whether, when self-atari attempt is detected, to play the other
54 * group's liberty if that is non-self-atari. */
55 bool selfatari_other;
56 /* Whether to always pick from moves capturing all groups in
57 * global_atari_check(). */
58 bool capcheckall;
60 struct joseki_dict *jdict;
61 struct pattern3s patterns;
63 /* Gamma values for queue tags - correspond to probabilities. */
64 /* XXX: Tune. */
65 double mq_prob[MQ_MAX], tenuki_prob;
69 static char moggy_patterns_src[][11] = {
70 /* hane pattern - enclosing hane */
71 "XOX"
72 "..."
73 "???",
74 /* hane pattern - non-cutting hane */
75 "XO."
76 "..."
77 "?.?",
78 /* hane pattern - magari */
79 "XO?"
80 "X.."
81 "x.?",
82 /* hane pattern - thin hane */
83 "XOO"
84 "..."
85 "?.?" "X",
86 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
87 ".O."
88 "X.."
89 "...",
90 /* cut1 pattern (kiri) - unprotected cut */
91 "XO?"
92 "O.o"
93 "?o?",
94 /* cut1 pattern (kiri) - peeped cut */
95 "XO?"
96 "O.X"
97 "???",
98 /* cut2 pattern (de) */
99 "?X?"
100 "O.O"
101 "ooo",
102 /* cut keima (not in Mogo) */
103 "OX?"
104 "o.O"
105 "???", /* o?? has some pathological tsumego cases */
106 /* side pattern - chase */
107 "X.?"
108 "O.?"
109 "##?",
110 /* side pattern - block side cut */
111 "OX?"
112 "X.O"
113 "###",
114 /* side pattern - block side connection */
115 "?X?"
116 "x.O"
117 "###",
118 /* side pattern - sagari (SUSPICIOUS) */
119 "?XO"
120 "x.x" /* Mogo has "x.?" */
121 "###" /* Mogo has "X" */,
122 /* side pattern - throw-in (SUSPICIOUS) */
123 #if 0
124 "?OX"
125 "o.O"
126 "?##" "X",
127 #endif
128 /* side pattern - cut (SUSPICIOUS) */
129 "?OX"
130 "X.O"
131 "###" /* Mogo has "X" */,
133 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
135 static inline bool
136 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
138 struct moggy_policy *pp = p->data;
139 /* Check if 3x3 pattern is matched by given move... */
140 if (!pattern3_move_here(&pp->patterns, b, m))
141 return false;
142 /* ...and the move is not obviously stupid. */
143 if (is_bad_selfatari(b, m->color, m->coord))
144 return false;
145 /* Ladder moves are stupid. */
146 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
147 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
148 return false;
149 return true;
152 static void
153 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
155 struct move m2 = { .coord = c, .color = color };
156 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
157 mq_add(q, c, 1<<MQ_PAT3);
160 /* Check if we match any pattern around given move (with the other color to play). */
161 static void
162 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
164 /* Suicides do not make any patterns and confuse us. */
165 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
166 return;
168 foreach_8neighbor(b, m->coord) {
169 apply_pattern_here(p, b, c, stone_other(m->color), q);
170 } foreach_8neighbor_end;
172 if (mm) { /* Second move for pattern searching */
173 foreach_8neighbor(b, mm->coord) {
174 if (coord_is_8adjecent(m->coord, c, b))
175 continue;
176 apply_pattern_here(p, b, c, stone_other(m->color), q);
177 } foreach_8neighbor_end;
180 if (PLDEBUGL(5))
181 mq_print(q, b, "Pattern");
185 static void
186 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
188 struct moggy_policy *pp = p->data;
189 if (!pp->jdict)
190 return;
192 for (int i = 0; i < 4; i++) {
193 hash_t h = b->qhash[i] & joseki_hash_mask;
194 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
195 if (!cc) continue;
196 for (; !is_pass(*cc); cc++) {
197 if (coord_quadrant(*cc, b) != i)
198 continue;
199 mq_add(q, *cc, 1<<MQ_JOSEKI);
203 if (q->moves > 0 && PLDEBUGL(5))
204 mq_print(q, b, "Joseki");
207 static void
208 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
210 if (b->clen == 0)
211 return;
213 struct moggy_policy *pp = p->data;
214 if (pp->capcheckall) {
215 for (int g = 0; g < b->clen; g++)
216 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
217 if (PLDEBUGL(5))
218 mq_print(q, b, "Global atari");
219 return;
222 int g_base = fast_random(b->clen);
223 for (int g = g_base; g < b->clen; g++) {
224 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
225 if (q->moves > 0) {
226 /* XXX: Try carrying on. */
227 if (PLDEBUGL(5))
228 mq_print(q, b, "Global atari");
229 return;
232 for (int g = 0; g < g_base; g++) {
233 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
234 if (q->moves > 0) {
235 /* XXX: Try carrying on. */
236 if (PLDEBUGL(5))
237 mq_print(q, b, "Global atari");
238 return;
241 return;
244 static void
245 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
247 struct moggy_policy *pp = p->data;
249 /* Did the opponent play a self-atari? */
250 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
251 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
254 foreach_neighbor(b, m->coord, {
255 group_t g = group_at(b, c);
256 if (!g || board_group_info(b, g).libs != 1)
257 continue;
258 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
261 if (PLDEBUGL(5))
262 mq_print(q, b, "Local atari");
265 static bool
266 miai_2lib(struct board *b, group_t group, enum stone color)
268 bool can_connect = false, can_pull_out = false;
269 /* We have miai if we can either connect on both libs,
270 * or connect on one lib and escape on another. (Just
271 * having two escape routes can be risky.) We must make
272 * sure that we don't consider following as miai:
273 * X X X O
274 * X . . O
275 * O O X O - left dot would be pull-out, right dot connect */
276 foreach_neighbor(b, board_group_info(b, group).lib[0], {
277 enum stone cc = board_at(b, c);
278 if (cc == S_NONE && cc != board_at(b, board_group_info(b, group).lib[1])) {
279 can_pull_out = true;
280 } else if (cc != color) {
281 continue;
284 group_t cg = group_at(b, c);
285 if (cg && cg != group && board_group_info(b, cg).libs > 1)
286 can_connect = true;
288 foreach_neighbor(b, board_group_info(b, group).lib[1], {
289 enum stone cc = board_at(b, c);
290 if (c == board_group_info(b, group).lib[0])
291 continue;
292 if (cc == S_NONE && can_connect) {
293 return true;
294 } else if (cc != color) {
295 continue;
298 group_t cg = group_at(b, c);
299 if (cg && cg != group && board_group_info(b, cg).libs > 1)
300 return (can_connect || can_pull_out);
302 return false;
305 static void
306 check_group_atari(struct board *b, group_t group, enum stone owner,
307 enum stone to_play, struct move_queue *q)
309 for (int i = 0; i < 2; i++) {
310 coord_t lib = board_group_info(b, group).lib[i];
311 assert(board_at(b, lib) == S_NONE);
312 if (!board_is_valid_play(b, to_play, lib))
313 continue;
315 /* Don't play at the spot if it is extremely short
316 * of liberties... */
317 /* XXX: This looks harmful, could significantly
318 * prefer atari to throwin:
320 * XXXOOOOOXX
321 * .OO.....OX
322 * XXXOOOOOOX */
323 #if 0
324 if (neighbor_count_at(b, lib, stone_other(owner)) + immediate_liberty_count(b, lib) < 2)
325 continue;
326 #endif
328 /* If the move is too "lumpy", do not play it:
330 * #######
331 * ..O.X.X <- always play the left one!
332 * OXXXXXX */
333 if (neighbor_count_at(b, lib, stone_other(owner)) + neighbor_count_at(b, lib, S_OFFBOARD) == 3)
334 continue;
336 #ifdef NO_DOOMED_GROUPS
337 /* If the owner can't play at the spot, we don't want
338 * to bother either. */
339 if (is_bad_selfatari(b, owner, lib))
340 continue;
341 #endif
343 /* Of course we don't want to play bad selfatari
344 * ourselves, if we are the attacker... */
345 if (
346 #ifdef NO_DOOMED_GROUPS
347 to_play != owner &&
348 #endif
349 is_bad_selfatari(b, to_play, lib))
350 continue;
352 /* Tasty! Crispy! Good! */
353 mq_add(q, lib, 1<<MQ_L2LIB);
354 mq_nodup(q);
358 static void
359 group_2lib_check(struct playout_policy *p, struct board *b, group_t group, enum stone to_play,
360 struct move_queue *q)
362 enum stone color = board_at(b, group_base(group));
363 assert(color != S_OFFBOARD && color != S_NONE);
365 if (PLDEBUGL(5))
366 fprintf(stderr, "[%s] 2lib check of color %d\n",
367 coord2sstr(group, b), color);
369 /* Do not try to atari groups that cannot be harmed. */
370 if (miai_2lib(b, group, color))
371 return;
373 check_group_atari(b, group, color, to_play, q);
375 /* Can we counter-atari another group, if we are the defender? */
376 if (to_play != color)
377 return;
378 foreach_in_group(b, group) {
379 foreach_neighbor(b, c, {
380 if (board_at(b, c) != stone_other(color))
381 continue;
382 group_t g2 = group_at(b, c);
383 if (board_group_info(b, g2).libs == 1) {
384 /* We can capture a neighbor. */
385 mq_add(q, board_group_info(b, g2).lib[0], 1<<MQ_L2LIB);
386 mq_nodup(q);
387 continue;
389 if (board_group_info(b, g2).libs != 2)
390 continue;
391 check_group_atari(b, g2, color, to_play, q);
393 } foreach_in_group_end;
396 static void
397 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
399 /* Does the opponent have just two liberties? */
400 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
401 group_2lib_check(p, b, group_at(b, m->coord), stone_other(m->color), q);
402 #if 0
403 /* We always prefer to take off an enemy chain liberty
404 * before pulling out ourselves. */
405 /* XXX: We aren't guaranteed to return to that group
406 * later. */
407 if (q->moves)
408 return q->move[fast_random(q->moves)];
409 #endif
412 /* Then he took a third liberty from neighboring chain? */
413 foreach_neighbor(b, m->coord, {
414 group_t g = group_at(b, c);
415 if (!g || board_group_info(b, g).libs != 2)
416 continue;
417 group_2lib_check(p, b, g, stone_other(m->color), q);
420 if (PLDEBUGL(5))
421 mq_print(q, b, "Local 2lib");
424 coord_t
425 fillboard_check(struct playout_policy *p, struct board *b)
427 struct moggy_policy *pp = p->data;
428 unsigned int fbtries = b->flen / 8;
429 if (pp->fillboardtries < fbtries)
430 fbtries = pp->fillboardtries;
432 for (unsigned int i = 0; i < fbtries; i++) {
433 coord_t coord = b->f[fast_random(b->flen)];
434 if (immediate_liberty_count(b, coord) != 4)
435 continue;
436 foreach_diag_neighbor(b, coord) {
437 if (board_at(b, c) != S_NONE)
438 goto next_try;
439 } foreach_diag_neighbor_end;
440 return coord;
441 next_try:;
443 return pass;
446 coord_t
447 playout_moggy_partchoose(struct playout_policy *p, struct board *b, enum stone to_play)
449 struct moggy_policy *pp = p->data;
451 if (PLDEBUGL(5))
452 board_print(b, stderr);
454 /* Ko fight check */
455 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
456 && b->moves - b->last_ko_age < pp->koage
457 && pp->korate > fast_random(100)) {
458 if (board_is_valid_play(b, to_play, b->last_ko.coord)
459 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
460 return b->last_ko.coord;
463 /* Local checks */
464 if (!is_pass(b->last_move.coord)) {
465 /* Local group in atari? */
466 if (pp->lcapturerate > fast_random(100)) {
467 struct move_queue q; q.moves = 0;
468 local_atari_check(p, b, &b->last_move, &q);
469 if (q.moves > 0)
470 return mq_pick(&q);
473 /* Local group can be PUT in atari? */
474 if (pp->atarirate > fast_random(100)) {
475 struct move_queue q; q.moves = 0;
476 local_2lib_check(p, b, &b->last_move, &q);
477 if (q.moves > 0)
478 return mq_pick(&q);
481 /* Check for patterns we know */
482 if (pp->patternrate > fast_random(100)) {
483 struct move_queue q; q.moves = 0;
484 apply_pattern(p, b, &b->last_move,
485 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
486 &q);
487 if (q.moves > 0)
488 return mq_pick(&q);
492 /* Global checks */
494 /* Any groups in atari? */
495 if (pp->capturerate > fast_random(100)) {
496 struct move_queue q; q.moves = 0;
497 global_atari_check(p, b, to_play, &q);
498 if (q.moves > 0)
499 return mq_pick(&q);
502 /* Joseki moves? */
503 if (pp->josekirate > fast_random(100)) {
504 struct move_queue q; q.moves = 0;
505 joseki_check(p, b, to_play, &q);
506 if (q.moves > 0)
507 return mq_pick(&q);
510 /* Fill board */
511 if (pp->fillboardtries > 0) {
512 coord_t c = fillboard_check(p, b);
513 if (!is_pass(c))
514 return c;
517 return pass;
520 /* Pick a move from queue q, giving different likelihoods to moves
521 * based on their tags. */
522 coord_t
523 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
525 struct moggy_policy *pp = p->data;
527 /* First, merge all entries for a move. */
528 /* We use a naive O(N^2) since the average length of the queue
529 * is about 1.4. */
530 for (unsigned int i = 0; i < q->moves; i++) {
531 for (unsigned int j = i + 1; j < q->moves; j++) {
532 if (q->move[i] != q->move[j])
533 continue;
534 q->tag[i] |= q->tag[j];
535 q->moves--;
536 q->tag[j] = q->tag[q->moves];
537 q->move[j] = q->move[q->moves];
541 /* Now, construct a probdist. */
542 fixp_t total = 0;
543 fixp_t pd[q->moves];
544 for (unsigned int i = 0; i < q->moves; i++) {
545 double val = 1.0;
546 assert(q->tag[i] != 0);
547 for (int j = 1; j < MQ_MAX; j++)
548 if (q->tag[i] & (1<<j)) {
549 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
550 val *= pp->mq_prob[j];
552 pd[i] = double_to_fixp(val);
553 total += pd[i];
555 total += double_to_fixp(pp->tenuki_prob);
557 /* Finally, pick a move! */
558 fixp_t stab = fast_irandom(total);
559 for (unsigned int i = 0; i < q->moves; i++) {
560 //fprintf(stderr, "%s(%x) %f (%f/%f)\n", coord2sstr(q->move[i], b), q->tag[i], fixp_to_double(stab), fixp_to_double(pd[i]), fixp_to_double(total));
561 if (stab < pd[i])
562 return q->move[i];
563 stab -= pd[i];
566 /* Tenuki. */
567 assert(stab < double_to_fixp(pp->tenuki_prob));
568 return pass;
571 coord_t
572 playout_moggy_fullchoose(struct playout_policy *p, struct board *b, enum stone to_play)
574 struct moggy_policy *pp = p->data;
575 struct move_queue q; q.moves = 0;
577 if (PLDEBUGL(5))
578 board_print(b, stderr);
580 /* Ko fight check */
581 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
582 && b->moves - b->last_ko_age < pp->koage
583 && pp->korate > fast_random(100)) {
584 if (board_is_valid_play(b, to_play, b->last_ko.coord)
585 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
586 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
589 /* Local checks */
590 if (!is_pass(b->last_move.coord)) {
591 /* Local group in atari? */
592 if (pp->lcapturerate > fast_random(100)) {
593 local_atari_check(p, b, &b->last_move, &q);
596 /* Local group can be PUT in atari? */
597 if (pp->atarirate > fast_random(100)) {
598 local_2lib_check(p, b, &b->last_move, &q);
601 /* Check for patterns we know */
602 if (pp->patternrate > fast_random(100)) {
603 apply_pattern(p, b, &b->last_move,
604 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
605 &q);
609 /* Global checks */
611 /* Any groups in atari? */
612 if (pp->capturerate > fast_random(100)) {
613 global_atari_check(p, b, to_play, &q);
616 /* Joseki moves? */
617 if (pp->josekirate > fast_random(100)) {
618 joseki_check(p, b, to_play, &q);
621 #if 0
622 /* Average length of the queue is 1.4 move. */
623 printf("MQL %d ", q.moves);
624 for (unsigned int i = 0; i < q.moves; i++)
625 printf("%s ", coord2sstr(q.move[i], b));
626 printf("\n");
627 #endif
629 if (q.moves > 0)
630 return mq_tagged_choose(p, b, to_play, &q);
632 /* Fill board */
633 if (pp->fillboardtries > 0) {
634 coord_t c = fillboard_check(p, b);
635 if (!is_pass(c))
636 return c;
639 return pass;
643 static coord_t
644 selfatari_cousin(struct board *b, enum stone color, coord_t coord)
646 group_t groups[4]; int groups_n = 0;
647 foreach_neighbor(b, coord, {
648 enum stone s = board_at(b, c);
649 if (s != color) continue;
650 group_t g = group_at(b, c);
651 if (board_group_info(b, g).libs == 2)
652 groups[groups_n++] = g;
655 if (!groups_n)
656 return pass;
657 group_t group = groups[fast_random(groups_n)];
659 coord_t lib2 = board_group_other_lib(b, group, coord);
660 if (is_bad_selfatari(b, color, lib2))
661 return pass;
662 return lib2;
665 void
666 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
668 struct moggy_policy *pp = p->data;
669 struct board *b = map->b;
670 struct move_queue q; q.moves = 0;
672 if (board_group_info(b, g).libs > 2)
673 return;
675 if (PLDEBUGL(5)) {
676 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
677 board_print(b, stderr);
680 if (board_group_info(b, g).libs == 2) {
681 if (!pp->atarirate)
682 return;
683 group_2lib_check(p, b, g, map->to_play, &q);
684 while (q.moves--) {
685 coord_t coord = q.move[q.moves];
686 if (PLDEBUGL(5))
687 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
688 int assess = games / 2;
689 add_prior_value(map, coord, 1, assess);
691 return;
694 /* This group, sir, is in atari! */
696 coord_t ladder = pass;
697 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
698 while (q.moves--) {
699 coord_t coord = q.move[q.moves];
701 /* _Never_ play here if this move plays out
702 * a caught ladder. */
703 if (coord == ladder && !board_playing_ko_threat(b)) {
704 /* Note that the opposite is not guarded against;
705 * we do not advise against capturing a laddered
706 * group (but we don't encourage it either). Such
707 * a move can simplify tactical situations if we
708 * can afford it. */
709 if (map->to_play != board_at(b, g))
710 continue;
711 /* FIXME: We give the malus even if this move
712 * captures another group. */
713 if (PLDEBUGL(5))
714 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
715 add_prior_value(map, coord, 0, games);
716 continue;
719 if (!pp->capturerate && !pp->lcapturerate)
720 continue;
722 if (PLDEBUGL(5))
723 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
724 int assess = games * 2;
725 add_prior_value(map, coord, 1, assess);
729 void
730 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
732 struct moggy_policy *pp = p->data;
733 struct board *b = map->b;
735 if (PLDEBUGL(5)) {
736 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
737 board_print(b, stderr);
740 /* Is this move a self-atari? */
741 if (pp->selfatarirate) {
742 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
743 if (PLDEBUGL(5))
744 fprintf(stderr, "0.0: self-atari\n");
745 add_prior_value(map, coord, 0, games);
746 if (!pp->selfatari_other)
747 return;
748 /* If we can play on the other liberty of the
749 * endangered group, do! */
750 coord = selfatari_cousin(b, map->to_play, coord);
751 if (is_pass(coord))
752 return;
753 if (PLDEBUGL(5))
754 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
755 add_prior_value(map, coord, 1.0, games);
756 return;
760 /* Pattern check */
761 if (pp->patternrate) {
762 struct move m = { .color = map->to_play, .coord = coord };
763 if (test_pattern3_here(p, b, &m)) {
764 if (PLDEBUGL(5))
765 fprintf(stderr, "1.0: pattern\n");
766 add_prior_value(map, coord, 1, games);
770 return;
773 void
774 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
776 struct moggy_policy *pp = p->data;
778 /* First, go through all endangered groups. */
779 for (group_t g = 1; g < board_size2(map->b); g++)
780 if (group_at(map->b, g) == g)
781 playout_moggy_assess_group(p, map, g, games);
783 /* Then, assess individual moves. */
784 if (!pp->patternrate && !pp->selfatarirate)
785 return;
786 foreach_free_point(map->b) {
787 if (map->consider[c])
788 playout_moggy_assess_one(p, map, c, games);
789 } foreach_free_point_end;
792 bool
793 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
795 struct moggy_policy *pp = p->data;
797 /* The idea is simple for now - never allow self-atari moves.
798 * They suck in general, but this also permits us to actually
799 * handle seki in the playout stage. */
801 if (fast_random(100) >= pp->selfatarirate) {
802 if (PLDEBUGL(5))
803 fprintf(stderr, "skipping sar test\n");
804 return true;
806 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
807 if (selfatari) {
808 if (PLDEBUGL(5))
809 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
810 stone2str(m->color), coord2sstr(m->coord, b));
811 if (pp->selfatari_other) {
812 /* Ok, try the other liberty of the atari'd group. */
813 coord_t c = selfatari_cousin(b, m->color, m->coord);
814 if (is_pass(c)) return false;
815 if (PLDEBUGL(5))
816 fprintf(stderr, "___ Redirecting to other lib %s\n",
817 coord2sstr(c, b));
818 m->coord = c;
819 return true;
821 return false;
823 return true;
827 struct playout_policy *
828 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
830 struct playout_policy *p = calloc2(1, sizeof(*p));
831 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
832 p->data = pp;
833 p->choose = playout_moggy_partchoose;
834 p->assess = playout_moggy_assess;
835 p->permit = playout_moggy_permit;
837 pp->jdict = jdict;
839 int rate = 90;
841 pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate
842 = pp->selfatarirate = pp->josekirate = -1U;
843 pp->korate = 0; pp->koage = 4;
844 pp->alwaysccaprate = 0;
845 pp->selfatari_other = true;
847 /* C is stupid. */
848 double mq_prob_default[MQ_MAX] = {
849 [MQ_KO] = 6.0,
850 [MQ_LATARI] = 5.0,
851 [MQ_L2LIB] = 4.0,
852 [MQ_PAT3] = 3.0,
853 [MQ_GATARI] = 2.0,
854 [MQ_JOSEKI] = 1.0,
856 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
858 if (arg) {
859 char *optspec, *next = arg;
860 while (*next) {
861 optspec = next;
862 next += strcspn(next, ":");
863 if (*next) { *next++ = 0; } else { *next = 0; }
865 char *optname = optspec;
866 char *optval = strchr(optspec, '=');
867 if (optval) *optval++ = 0;
869 if (!strcasecmp(optname, "debug") && optval) {
870 p->debug_level = atoi(optval);
871 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
872 pp->lcapturerate = atoi(optval);
873 } else if (!strcasecmp(optname, "atarirate") && optval) {
874 pp->atarirate = atoi(optval);
875 } else if (!strcasecmp(optname, "capturerate") && optval) {
876 pp->capturerate = atoi(optval);
877 } else if (!strcasecmp(optname, "patternrate") && optval) {
878 pp->patternrate = atoi(optval);
879 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
880 pp->selfatarirate = atoi(optval);
881 } else if (!strcasecmp(optname, "korate") && optval) {
882 pp->korate = atoi(optval);
883 } else if (!strcasecmp(optname, "josekirate") && optval) {
884 pp->josekirate = atoi(optval);
885 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
886 pp->alwaysccaprate = atoi(optval);
887 } else if (!strcasecmp(optname, "rate") && optval) {
888 rate = atoi(optval);
889 } else if (!strcasecmp(optname, "fillboardtries")) {
890 pp->fillboardtries = atoi(optval);
891 } else if (!strcasecmp(optname, "koage") && optval) {
892 pp->koage = atoi(optval);
893 } else if (!strcasecmp(optname, "pattern2")) {
894 pp->pattern2 = optval && *optval == '0' ? false : true;
895 } else if (!strcasecmp(optname, "selfatari_other")) {
896 pp->selfatari_other = optval && *optval == '0' ? false : true;
897 } else if (!strcasecmp(optname, "capcheckall")) {
898 pp->capcheckall = optval && *optval == '0' ? false : true;
899 } else if (!strcasecmp(optname, "fullchoose")) {
900 p->choose = optval && *optval == '0' ? playout_moggy_partchoose : playout_moggy_fullchoose;
901 } else if (!strcasecmp(optname, "mqprob") && optval) {
902 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
903 for (int i = 1; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
904 optval++;
905 pp->mq_prob[i] = atof(optval);
907 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
908 pp->tenuki_prob = atof(optval);
909 } else {
910 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
911 exit(1);
915 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
916 if (pp->atarirate == -1U) pp->atarirate = rate;
917 if (pp->capturerate == -1U) pp->capturerate = rate;
918 if (pp->patternrate == -1U) pp->patternrate = rate;
919 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
920 if (pp->korate == -1U) pp->korate = rate;
921 if (pp->josekirate == -1U) pp->josekirate = rate;
922 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
924 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
926 return p;