Moggy local_2lib_check(): Avoid duplicate 2-lib checks
[pachi/json.git] / playout / moggy.c
blob635902a65e1352c2854a726233f77f13d5f6e9fd
1 /* Heuristical playout (and tree prior) policy modelled primarily after
2 * the description of the Mogo engine. */
4 #include <assert.h>
5 #include <math.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #define DEBUG
10 #include "board.h"
11 #include "debug.h"
12 #include "joseki/base.h"
13 #include "mq.h"
14 #include "pattern3.h"
15 #include "playout.h"
16 #include "playout/moggy.h"
17 #include "random.h"
18 #include "tactics/1lib.h"
19 #include "tactics/2lib.h"
20 #include "tactics/nlib.h"
21 #include "tactics/ladder.h"
22 #include "tactics/nakade.h"
23 #include "tactics/selfatari.h"
24 #include "uct/prior.h"
26 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
29 /* In case "seqchoose" move picker is enabled (i.e. no "fullchoose"
30 * parameter passed), we stochastically apply fixed set of decision
31 * rules in given order.
33 * In "fullchoose" mode, we instead build a move queue of variously
34 * tagged candidates, then consider a probability distribution over
35 * them and pick a move from that. */
37 /* Move queue tags. Some may be even undesirable - these moves then
38 * receive a penalty; penalty tags should be used only when it is
39 * certain the move would be considered anyway. */
40 enum mq_tag {
41 MQ_KO = 0,
42 MQ_LATARI,
43 MQ_L2LIB,
44 MQ_LNLIB,
45 MQ_PAT3,
46 MQ_GATARI,
47 MQ_JOSEKI,
48 MQ_NAKADE,
49 MQ_MAX
53 /* Note that the context can be shared by multiple threads! */
55 struct moggy_policy {
56 unsigned int lcapturerate, atarirate, nlibrate, capturerate, patternrate, korate, josekirate, nakaderate;
57 unsigned int selfatarirate, alwaysccaprate;
58 unsigned int fillboardtries;
59 int koage;
60 /* Whether to look for patterns around second-to-last move. */
61 bool pattern2;
62 /* Whether, when self-atari attempt is detected, to play the other
63 * group's liberty if that is non-self-atari. */
64 bool selfatari_other;
65 /* Whether to always pick from moves capturing all groups in
66 * global_atari_check(). */
67 bool capcheckall;
68 /* 2lib settings: */
69 bool atari_def_no_hopeless;
70 bool atari_miaisafe;
71 /* nlib settings: */
72 int nlib_count;
74 struct joseki_dict *jdict;
75 struct pattern3s patterns;
77 /* Gamma values for queue tags - correspond to probabilities. */
78 /* XXX: Tune. */
79 double mq_prob[MQ_MAX], tenuki_prob;
83 static char moggy_patterns_src[][11] = {
84 /* hane pattern - enclosing hane */
85 "XOX"
86 "..."
87 "???",
88 /* hane pattern - non-cutting hane */
89 "YO."
90 "..."
91 "?.?",
92 /* hane pattern - magari */
93 "XO?"
94 "X.."
95 "x.?",
96 /* hane pattern - thin hane */
97 "XOO"
98 "..."
99 "?.?" "X",
100 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
101 ".Q."
102 "Y.."
103 "...",
104 /* cut1 pattern (kiri) - unprotected cut */
105 "XO?"
106 "O.o"
107 "?o?",
108 /* cut1 pattern (kiri) - peeped cut */
109 "XO?"
110 "O.X"
111 "???",
112 /* cut2 pattern (de) */
113 "?X?"
114 "O.O"
115 "ooo",
116 /* cut keima (not in Mogo) */
117 "OX?"
118 "o.O"
119 "???", /* o?? has some pathological tsumego cases */
120 /* side pattern - chase */
121 "X.?"
122 "O.?"
123 "##?",
124 /* side pattern - block side cut */
125 "OX?"
126 "X.O"
127 "###",
128 /* side pattern - block side connection */
129 "?X?"
130 "x.O"
131 "###",
132 /* side pattern - sagari (SUSPICIOUS) */
133 "?XQ"
134 "x.x" /* Mogo has "x.?" */
135 "###" /* Mogo has "X" */,
136 /* side pattern - throw-in (SUSPICIOUS) */
137 #if 0
138 "?OX"
139 "o.O"
140 "?##" "X",
141 #endif
142 /* side pattern - cut (SUSPICIOUS) */
143 "?OY"
144 "Y.O"
145 "###" /* Mogo has "X" */,
146 /* side pattern - eye piercing:
147 * # O O O .
148 * # O . O .
149 * # . . . .
150 * # # # # # */
151 #if 0
152 "Oxx"
153 "..."
154 "###",
155 #endif
157 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
159 static inline bool
160 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
162 struct moggy_policy *pp = p->data;
163 /* Check if 3x3 pattern is matched by given move... */
164 if (!pattern3_move_here(&pp->patterns, b, m))
165 return false;
166 /* ...and the move is not obviously stupid. */
167 if (is_bad_selfatari(b, m->color, m->coord))
168 return false;
169 /* Ladder moves are stupid. */
170 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
171 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
172 return false;
173 return true;
176 static void
177 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
179 struct move m2 = { .coord = c, .color = color };
180 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
181 mq_add(q, c, 1<<MQ_PAT3);
184 /* Check if we match any pattern around given move (with the other color to play). */
185 static void
186 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
188 /* Suicides do not make any patterns and confuse us. */
189 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
190 return;
192 foreach_8neighbor(b, m->coord) {
193 apply_pattern_here(p, b, c, stone_other(m->color), q);
194 } foreach_8neighbor_end;
196 if (mm) { /* Second move for pattern searching */
197 foreach_8neighbor(b, mm->coord) {
198 if (coord_is_8adjecent(m->coord, c, b))
199 continue;
200 apply_pattern_here(p, b, c, stone_other(m->color), q);
201 } foreach_8neighbor_end;
204 if (PLDEBUGL(5))
205 mq_print(q, b, "Pattern");
209 static void
210 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
212 struct moggy_policy *pp = p->data;
213 if (!pp->jdict)
214 return;
216 for (int i = 0; i < 4; i++) {
217 hash_t h = b->qhash[i] & joseki_hash_mask;
218 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
219 if (!cc) continue;
220 for (; !is_pass(*cc); cc++) {
221 if (coord_quadrant(*cc, b) != i)
222 continue;
223 mq_add(q, *cc, 1<<MQ_JOSEKI);
227 if (q->moves > 0 && PLDEBUGL(5))
228 mq_print(q, b, "Joseki");
231 static void
232 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
234 if (b->clen == 0)
235 return;
237 struct moggy_policy *pp = p->data;
238 if (pp->capcheckall) {
239 for (int g = 0; g < b->clen; g++)
240 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
241 if (PLDEBUGL(5))
242 mq_print(q, b, "Global atari");
243 return;
246 int g_base = fast_random(b->clen);
247 for (int g = g_base; g < b->clen; g++) {
248 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
249 if (q->moves > 0) {
250 /* XXX: Try carrying on. */
251 if (PLDEBUGL(5))
252 mq_print(q, b, "Global atari");
253 return;
256 for (int g = 0; g < g_base; g++) {
257 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
258 if (q->moves > 0) {
259 /* XXX: Try carrying on. */
260 if (PLDEBUGL(5))
261 mq_print(q, b, "Global atari");
262 return;
265 return;
268 static void
269 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
271 struct moggy_policy *pp = p->data;
273 /* Did the opponent play a self-atari? */
274 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
275 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
278 foreach_neighbor(b, m->coord, {
279 group_t g = group_at(b, c);
280 if (!g || board_group_info(b, g).libs != 1)
281 continue;
282 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
285 if (PLDEBUGL(5))
286 mq_print(q, b, "Local atari");
290 static void
291 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
293 struct moggy_policy *pp = p->data;
294 group_t group = group_at(b, m->coord), group2 = 0;
296 /* Does the opponent have just two liberties? */
297 if (board_group_info(b, group).libs == 2) {
298 group_2lib_check(b, group, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
299 #if 0
300 /* We always prefer to take off an enemy chain liberty
301 * before pulling out ourselves. */
302 /* XXX: We aren't guaranteed to return to that group
303 * later. */
304 if (q->moves)
305 return q->move[fast_random(q->moves)];
306 #endif
309 /* Then he took a third liberty from neighboring chain? */
310 foreach_neighbor(b, m->coord, {
311 group_t g = group_at(b, c);
312 if (!g || g == group || g == group2 || board_group_info(b, g).libs != 2)
313 continue;
314 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
315 group2 = g; // prevent trivial repeated checks
318 if (PLDEBUGL(5))
319 mq_print(q, b, "Local 2lib");
322 static void
323 local_nlib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
325 struct moggy_policy *pp = p->data;
326 enum stone color = stone_other(m->color);
328 /* Attacking N-liberty groups in general is probably
329 * not feasible. What we are primarily concerned about is
330 * counter-attacking groups that have two physical liberties,
331 * but three effective liberties:
333 * . O . . . . #
334 * O O X X X X #
335 * . X O O X . #
336 * . X O . O X #
337 * . X O O . X #
338 * # # # # # # #
340 * The time for this to come is when the opponent took a liberty
341 * of ours, making a few-liberty group. Therefore, we focus
342 * purely on defense.
344 * There is a tradeoff - down to how many liberties we need to
345 * be to start looking? nlib_count=3 will work for the left black
346 * group (2lib-solver will suggest connecting the false eye), but
347 * not for top black group (it is too late to start playing 3-3
348 * capturing race). Also, we cannot prevent stupidly taking an
349 * outside liberty ourselves; the higher nlib_count, the higher
350 * the chance we withstand this.
352 * However, higher nlib_count means that we will waste more time
353 * checking non-urgent or alive groups, and we will play silly
354 * or wasted moves around alive groups. */
356 group_t group2 = 0;
357 foreach_8neighbor(b, m->coord) {
358 group_t g = group_at(b, c);
359 if (!g || group2 == g || board_at(b, c) != color)
360 continue;
361 if (board_group_info(b, g).libs < 3 || board_group_info(b, g).libs > pp->nlib_count)
362 continue;
363 group_nlib_defense_check(b, g, color, q, 1<<MQ_LNLIB);
364 group2 = g; // prevent trivial repeated checks
365 } foreach_8neighbor_end;
367 if (PLDEBUGL(5))
368 mq_print(q, b, "Local nlib");
371 static coord_t
372 nakade_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play)
374 coord_t empty = pass;
375 foreach_neighbor(b, m->coord, {
376 if (board_at(b, c) != S_NONE)
377 continue;
378 if (is_pass(empty)) {
379 empty = c;
380 continue;
382 if (!coord_is_8adjecent(c, empty, b)) {
383 /* Seems like impossible nakade
384 * shape! */
385 return pass;
388 assert(!is_pass(empty));
390 coord_t nakade = nakade_point(b, empty, stone_other(to_play));
391 if (PLDEBUGL(5) && !is_pass(nakade))
392 fprintf(stderr, "Nakade: %s\n", coord2sstr(nakade, b));
393 return nakade;
396 coord_t
397 fillboard_check(struct playout_policy *p, struct board *b)
399 struct moggy_policy *pp = p->data;
400 unsigned int fbtries = b->flen / 8;
401 if (pp->fillboardtries < fbtries)
402 fbtries = pp->fillboardtries;
404 for (unsigned int i = 0; i < fbtries; i++) {
405 coord_t coord = b->f[fast_random(b->flen)];
406 if (immediate_liberty_count(b, coord) != 4)
407 continue;
408 foreach_diag_neighbor(b, coord) {
409 if (board_at(b, c) != S_NONE)
410 goto next_try;
411 } foreach_diag_neighbor_end;
412 return coord;
413 next_try:
416 return pass;
419 coord_t
420 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
422 struct moggy_policy *pp = p->data;
424 if (PLDEBUGL(5))
425 board_print(b, stderr);
427 /* Ko fight check */
428 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
429 && b->moves - b->last_ko_age < pp->koage
430 && pp->korate > fast_random(100)) {
431 if (board_is_valid_play(b, to_play, b->last_ko.coord)
432 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
433 return b->last_ko.coord;
436 /* Local checks */
437 if (!is_pass(b->last_move.coord)) {
438 /* Nakade check */
439 if (pp->nakaderate > fast_random(100)
440 && immediate_liberty_count(b, b->last_move.coord) > 0) {
441 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
442 if (!is_pass(nakade))
443 return nakade;
446 /* Local group in atari? */
447 if (pp->lcapturerate > fast_random(100)) {
448 struct move_queue q; q.moves = 0;
449 local_atari_check(p, b, &b->last_move, &q);
450 if (q.moves > 0)
451 return mq_pick(&q);
454 /* Local group can be PUT in atari? */
455 if (pp->atarirate > fast_random(100)) {
456 struct move_queue q; q.moves = 0;
457 local_2lib_check(p, b, &b->last_move, &q);
458 if (q.moves > 0)
459 return mq_pick(&q);
462 /* Local group reduced some of our groups to 3 libs? */
463 if (pp->nlibrate > fast_random(100)) {
464 struct move_queue q; q.moves = 0;
465 local_nlib_check(p, b, &b->last_move, &q);
466 if (q.moves > 0)
467 return mq_pick(&q);
470 /* Check for patterns we know */
471 if (pp->patternrate > fast_random(100)) {
472 struct move_queue q; q.moves = 0;
473 apply_pattern(p, b, &b->last_move,
474 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
475 &q);
476 if (q.moves > 0)
477 return mq_pick(&q);
481 /* Global checks */
483 /* Any groups in atari? */
484 if (pp->capturerate > fast_random(100)) {
485 struct move_queue q; q.moves = 0;
486 global_atari_check(p, b, to_play, &q);
487 if (q.moves > 0)
488 return mq_pick(&q);
491 /* Joseki moves? */
492 if (pp->josekirate > fast_random(100)) {
493 struct move_queue q; q.moves = 0;
494 joseki_check(p, b, to_play, &q);
495 if (q.moves > 0)
496 return mq_pick(&q);
499 /* Fill board */
500 if (pp->fillboardtries > 0) {
501 coord_t c = fillboard_check(p, b);
502 if (!is_pass(c))
503 return c;
506 return pass;
509 /* Pick a move from queue q, giving different likelihoods to moves
510 * based on their tags. */
511 coord_t
512 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
514 struct moggy_policy *pp = p->data;
516 /* First, merge all entries for a move. */
517 /* We use a naive O(N^2) since the average length of the queue
518 * is about 1.4. */
519 for (unsigned int i = 0; i < q->moves; i++) {
520 for (unsigned int j = i + 1; j < q->moves; j++) {
521 if (q->move[i] != q->move[j])
522 continue;
523 q->tag[i] |= q->tag[j];
524 q->moves--;
525 q->tag[j] = q->tag[q->moves];
526 q->move[j] = q->move[q->moves];
530 /* Now, construct a probdist. */
531 fixp_t total = 0;
532 fixp_t pd[q->moves];
533 for (unsigned int i = 0; i < q->moves; i++) {
534 double val = 1.0;
535 assert(q->tag[i] != 0);
536 for (int j = 0; j < MQ_MAX; j++)
537 if (q->tag[i] & (1<<j)) {
538 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
539 val *= pp->mq_prob[j];
541 pd[i] = double_to_fixp(val);
542 total += pd[i];
544 total += double_to_fixp(pp->tenuki_prob);
546 /* Finally, pick a move! */
547 fixp_t stab = fast_irandom(total);
548 for (unsigned int i = 0; i < q->moves; i++) {
549 //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));
550 if (stab < pd[i])
551 return q->move[i];
552 stab -= pd[i];
555 /* Tenuki. */
556 assert(stab < double_to_fixp(pp->tenuki_prob));
557 return pass;
560 coord_t
561 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
563 struct moggy_policy *pp = p->data;
564 struct move_queue q; q.moves = 0;
566 if (PLDEBUGL(5))
567 board_print(b, stderr);
569 /* Ko fight check */
570 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
571 && b->moves - b->last_ko_age < pp->koage) {
572 if (board_is_valid_play(b, to_play, b->last_ko.coord)
573 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
574 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
577 /* Local checks */
578 if (!is_pass(b->last_move.coord)) {
579 /* Nakade check */
580 if (immediate_liberty_count(b, b->last_move.coord) > 0) {
581 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
582 if (!is_pass(nakade))
583 mq_add(&q, nakade, 1<<MQ_NAKADE);
586 /* Local group in atari? */
587 local_atari_check(p, b, &b->last_move, &q);
589 /* Local group can be PUT in atari? */
590 local_2lib_check(p, b, &b->last_move, &q);
592 /* Local group reduced some of our groups to 3 libs? */
593 local_nlib_check(p, b, &b->last_move, &q);
595 /* Check for patterns we know */
596 apply_pattern(p, b, &b->last_move,
597 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
598 &q);
601 /* Global checks */
603 /* Any groups in atari? */
604 global_atari_check(p, b, to_play, &q);
606 /* Joseki moves? */
607 joseki_check(p, b, to_play, &q);
609 #if 0
610 /* Average length of the queue is 1.4 move. */
611 printf("MQL %d ", q.moves);
612 for (unsigned int i = 0; i < q.moves; i++)
613 printf("%s ", coord2sstr(q.move[i], b));
614 printf("\n");
615 #endif
617 if (q.moves > 0)
618 return mq_tagged_choose(p, b, to_play, &q);
620 /* Fill board */
621 if (pp->fillboardtries > 0) {
622 coord_t c = fillboard_check(p, b);
623 if (!is_pass(c))
624 return c;
627 return pass;
631 void
632 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
634 struct moggy_policy *pp = p->data;
635 struct board *b = map->b;
636 struct move_queue q; q.moves = 0;
638 if (board_group_info(b, g).libs > pp->nlib_count)
639 return;
641 if (PLDEBUGL(5)) {
642 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
643 board_print(b, stderr);
646 if (board_group_info(b, g).libs > 2) {
647 if (!pp->nlibrate)
648 return;
649 if (board_at(b, g) != map->to_play)
650 return; // we do only defense
651 group_nlib_defense_check(b, g, map->to_play, &q, 0);
652 while (q.moves--) {
653 coord_t coord = q.move[q.moves];
654 if (PLDEBUGL(5))
655 fprintf(stderr, "1.0: nlib %s\n", coord2sstr(coord, b));
656 int assess = games / 2;
657 add_prior_value(map, coord, 1, assess);
659 return;
662 if (board_group_info(b, g).libs == 2) {
663 if (!pp->atarirate)
664 return;
665 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
666 while (q.moves--) {
667 coord_t coord = q.move[q.moves];
668 if (PLDEBUGL(5))
669 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
670 int assess = games / 2;
671 add_prior_value(map, coord, 1, assess);
673 return;
676 /* This group, sir, is in atari! */
678 coord_t ladder = pass;
679 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
680 while (q.moves--) {
681 coord_t coord = q.move[q.moves];
683 /* _Never_ play here if this move plays out
684 * a caught ladder. */
685 if (coord == ladder && !board_playing_ko_threat(b)) {
686 /* Note that the opposite is not guarded against;
687 * we do not advise against capturing a laddered
688 * group (but we don't encourage it either). Such
689 * a move can simplify tactical situations if we
690 * can afford it. */
691 if (map->to_play != board_at(b, g))
692 continue;
693 /* FIXME: We give the malus even if this move
694 * captures another group. */
695 if (PLDEBUGL(5))
696 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
697 add_prior_value(map, coord, 0, games);
698 continue;
701 if (!pp->capturerate && !pp->lcapturerate)
702 continue;
704 if (PLDEBUGL(5))
705 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
706 int assess = games * 2;
707 add_prior_value(map, coord, 1, assess);
711 void
712 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
714 struct moggy_policy *pp = p->data;
715 struct board *b = map->b;
717 if (PLDEBUGL(5)) {
718 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
719 board_print(b, stderr);
722 /* Is this move a self-atari? */
723 if (pp->selfatarirate) {
724 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
725 if (PLDEBUGL(5))
726 fprintf(stderr, "0.0: self-atari\n");
727 add_prior_value(map, coord, 0, games);
728 if (!pp->selfatari_other)
729 return;
730 /* If we can play on the other liberty of the
731 * endangered group, do! */
732 coord = selfatari_cousin(b, map->to_play, coord);
733 if (is_pass(coord))
734 return;
735 if (PLDEBUGL(5))
736 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
737 add_prior_value(map, coord, 1.0, games);
738 return;
742 /* Pattern check */
743 if (pp->patternrate) {
744 struct move m = { .color = map->to_play, .coord = coord };
745 if (test_pattern3_here(p, b, &m)) {
746 if (PLDEBUGL(5))
747 fprintf(stderr, "1.0: pattern\n");
748 add_prior_value(map, coord, 1, games);
752 return;
755 void
756 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
758 struct moggy_policy *pp = p->data;
760 /* First, go through all endangered groups. */
761 for (group_t g = 1; g < board_size2(map->b); g++)
762 if (group_at(map->b, g) == g)
763 playout_moggy_assess_group(p, map, g, games);
765 /* Then, assess individual moves. */
766 if (!pp->patternrate && !pp->selfatarirate)
767 return;
768 foreach_free_point(map->b) {
769 if (map->consider[c])
770 playout_moggy_assess_one(p, map, c, games);
771 } foreach_free_point_end;
774 bool
775 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
777 struct moggy_policy *pp = p->data;
779 /* The idea is simple for now - never allow self-atari moves.
780 * They suck in general, but this also permits us to actually
781 * handle seki in the playout stage. */
783 if (fast_random(100) >= pp->selfatarirate) {
784 if (PLDEBUGL(5))
785 fprintf(stderr, "skipping sar test\n");
786 return true;
788 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
789 if (selfatari) {
790 if (PLDEBUGL(5))
791 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
792 stone2str(m->color), coord2sstr(m->coord, b));
793 if (pp->selfatari_other) {
794 /* Ok, try the other liberty of the atari'd group. */
795 coord_t c = selfatari_cousin(b, m->color, m->coord);
796 if (is_pass(c)) return false;
797 if (PLDEBUGL(5))
798 fprintf(stderr, "___ Redirecting to other lib %s\n",
799 coord2sstr(c, b));
800 m->coord = c;
801 return true;
803 return false;
805 return true;
809 struct playout_policy *
810 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
812 struct playout_policy *p = calloc2(1, sizeof(*p));
813 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
814 p->data = pp;
815 p->choose = playout_moggy_seqchoose;
816 p->assess = playout_moggy_assess;
817 p->permit = playout_moggy_permit;
819 pp->jdict = jdict;
821 /* These settings are tuned for 19x19 play with several threads
822 * on reasonable time limits (i.e., rather large number of playouts).
823 * XXX: no 9x9 tuning has been done recently. */
824 int rate = board_large(b) ? 80 : 90;
826 pp->lcapturerate = pp->atarirate = pp->nlibrate = pp->patternrate
827 = pp->selfatarirate = pp->josekirate = -1U;
828 if (board_large(b)) {
829 pp->lcapturerate = pp->patternrate = 100;
830 pp->nlibrate = 20;
831 pp->pattern2 = true;
833 pp->korate = 20; pp->koage = 4;
834 pp->alwaysccaprate = 20;
835 pp->selfatari_other = true;
836 pp->atari_def_no_hopeless = !board_large(b);
837 pp->atari_miaisafe = true;
838 pp->nlib_count = 4;
840 /* C is stupid. */
841 double mq_prob_default[MQ_MAX] = {
842 [MQ_KO] = 6.0,
843 [MQ_NAKADE] = 5.5,
844 [MQ_LATARI] = 5.0,
845 [MQ_L2LIB] = 4.0,
846 [MQ_LNLIB] = 3.5,
847 [MQ_PAT3] = 3.0,
848 [MQ_GATARI] = 2.0,
849 [MQ_JOSEKI] = 1.0,
851 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
853 if (arg) {
854 char *optspec, *next = arg;
855 while (*next) {
856 optspec = next;
857 next += strcspn(next, ":");
858 if (*next) { *next++ = 0; } else { *next = 0; }
860 char *optname = optspec;
861 char *optval = strchr(optspec, '=');
862 if (optval) *optval++ = 0;
864 if (!strcasecmp(optname, "debug") && optval) {
865 p->debug_level = atoi(optval);
866 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
867 pp->lcapturerate = atoi(optval);
868 } else if (!strcasecmp(optname, "atarirate") && optval) {
869 pp->atarirate = atoi(optval);
870 } else if (!strcasecmp(optname, "nlibrate") && optval) {
871 pp->nlibrate = atoi(optval);
872 } else if (!strcasecmp(optname, "capturerate") && optval) {
873 pp->capturerate = atoi(optval);
874 } else if (!strcasecmp(optname, "patternrate") && optval) {
875 pp->patternrate = atoi(optval);
876 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
877 pp->selfatarirate = atoi(optval);
878 } else if (!strcasecmp(optname, "korate") && optval) {
879 pp->korate = atoi(optval);
880 } else if (!strcasecmp(optname, "josekirate") && optval) {
881 pp->josekirate = atoi(optval);
882 } else if (!strcasecmp(optname, "nakaderate") && optval) {
883 pp->nakaderate = atoi(optval);
884 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
885 pp->alwaysccaprate = atoi(optval);
886 } else if (!strcasecmp(optname, "rate") && optval) {
887 rate = atoi(optval);
888 } else if (!strcasecmp(optname, "fillboardtries")) {
889 pp->fillboardtries = atoi(optval);
890 } else if (!strcasecmp(optname, "koage") && optval) {
891 pp->koage = atoi(optval);
892 } else if (!strcasecmp(optname, "pattern2")) {
893 pp->pattern2 = optval && *optval == '0' ? false : true;
894 } else if (!strcasecmp(optname, "selfatari_other")) {
895 pp->selfatari_other = optval && *optval == '0' ? false : true;
896 } else if (!strcasecmp(optname, "capcheckall")) {
897 pp->capcheckall = optval && *optval == '0' ? false : true;
898 } else if (!strcasecmp(optname, "atari_miaisafe")) {
899 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
900 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
901 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
902 } else if (!strcasecmp(optname, "nlib_count") && optval) {
903 pp->nlib_count = atoi(optval);
904 } else if (!strcasecmp(optname, "fullchoose")) {
905 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
906 } else if (!strcasecmp(optname, "mqprob") && optval) {
907 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
908 for (int i = 0; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
909 optval++;
910 pp->mq_prob[i] = atof(optval);
912 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
913 pp->tenuki_prob = atof(optval);
914 } else {
915 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
916 exit(1);
920 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
921 if (pp->atarirate == -1U) pp->atarirate = rate;
922 if (pp->capturerate == -1U) pp->capturerate = rate;
923 if (pp->patternrate == -1U) pp->patternrate = rate;
924 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
925 if (pp->korate == -1U) pp->korate = rate;
926 if (pp->josekirate == -1U) pp->josekirate = rate;
927 if (pp->nakaderate == -1U) pp->nakaderate = rate;
928 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
930 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
932 return p;