Merge branch 'master' into libmap
[pachi.git] / playout / moggy.c
blob4a16fa5bc1a18f6975a758270e05499d918fe136
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 "tactics/goals.h"
25 #include "uct/prior.h"
27 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
30 /* In case "seqchoose" move picker is enabled (i.e. no "fullchoose"
31 * parameter passed), we stochastically apply fixed set of decision
32 * rules in given order.
34 * In "fullchoose" mode, we instead build a move queue of variously
35 * tagged candidates, then consider a probability distribution over
36 * them and pick a move from that. */
38 /* Move queue tags. Some may be even undesirable - these moves then
39 * receive a penalty; penalty tags should be used only when it is
40 * certain the move would be considered anyway. */
41 enum mq_tag {
42 MQ_KO = 0,
43 MQ_LATARI,
44 MQ_L2LIB,
45 #define MQ_LADDER MQ_L2LIB /* XXX: We want to fit in char still! */
46 MQ_LNLIB,
47 MQ_PAT3,
48 MQ_GATARI,
49 MQ_JOSEKI,
50 MQ_NAKADE,
51 MQ_MAX
55 #define PAT3_N 15
57 /* Note that the context can be shared by multiple threads! */
59 struct moggy_policy {
60 unsigned int lcapturerate, atarirate, nlibrate, ladderrate, capturerate, patternrate, korate, josekirate, nakaderate, eyefixrate;
61 unsigned int selfatarirate, eyefillrate, alwaysccaprate;
62 unsigned int fillboardtries;
63 int koage;
64 /* Whether to look for patterns around second-to-last move. */
65 bool pattern2;
66 /* Whether, when self-atari attempt is detected, to play the other
67 * group's liberty if that is non-self-atari. */
68 bool selfatari_other;
69 /* Whether to read out ladders elsewhere than near the board
70 * in the playouts. Note that such ladder testing is currently
71 * a fairly expensive operation. */
72 bool middle_ladder;
74 /* 1lib settings: */
75 /* Whether to always pick from moves capturing all groups in
76 * global_atari_check(). */
77 bool capcheckall;
78 /* Prior stone weighting. Weight of each stone between
79 * cap_stone_min and cap_stone_max is (assess*100)/cap_stone_denom. */
80 int cap_stone_min, cap_stone_max;
81 int cap_stone_denom;
83 /* 2lib settings: */
84 bool atari_def_no_hopeless;
85 bool atari_miaisafe;
87 /* nlib settings: */
88 int nlib_count;
90 struct joseki_dict *jdict;
91 struct pattern3s patterns;
93 double pat3_gammas[PAT3_N];
95 /* Gamma values for queue tags - correspond to probabilities. */
96 /* XXX: Tune. */
97 bool fullchoose;
98 double mq_prob[MQ_MAX], tenuki_prob;
102 static char moggy_patterns_src[PAT3_N][11] = {
103 /* hane pattern - enclosing hane */ /* 0.52 */
104 "XOX"
105 "..."
106 "???",
107 /* hane pattern - non-cutting hane */ /* 0.53 */
108 "YO."
109 "..."
110 "?.?",
111 /* hane pattern - magari */ /* 0.32 */
112 "XO?"
113 "X.."
114 "x.?",
115 /* hane pattern - thin hane */ /* 0.22 */
116 "XOO"
117 "..."
118 "?.?" "X",
119 /* generic pattern - katatsuke or diagonal attachment; similar to magari */ /* 0.37 */
120 ".Q."
121 "Y.."
122 "...",
123 /* cut1 pattern (kiri) - unprotected cut */ /* 0.28 */
124 "XO?"
125 "O.o"
126 "?o?",
127 /* cut1 pattern (kiri) - peeped cut */ /* 0.21 */
128 "XO?"
129 "O.X"
130 "???",
131 /* cut2 pattern (de) */ /* 0.19 */
132 "?X?"
133 "O.O"
134 "ooo",
135 /* cut keima (not in Mogo) */ /* 0.82 */
136 "OX?"
137 "?.O"
138 "?o?", /* oo? has some pathological tsumego cases */
139 /* side pattern - chase */ /* 0.12 */
140 "X.?"
141 "O.?"
142 "##?",
143 /* side pattern - block side cut */ /* 0.20 */
144 "OX?"
145 "X.O"
146 "###",
147 /* side pattern - block side connection */ /* 0.11 */
148 "?X?"
149 "x.O"
150 "###",
151 /* side pattern - sagari (SUSPICIOUS) */ /* 0.16 */
152 "?XQ"
153 "x.x" /* Mogo has "x.?" */
154 "###" /* Mogo has "X" */,
155 #if 0
156 /* side pattern - throw-in (SUSPICIOUS) */
157 "?OX"
158 "o.O"
159 "?##" "X",
160 #endif
161 /* side pattern - cut (SUSPICIOUS) */ /* 0.57 */
162 "?OY"
163 "Y.O"
164 "###" /* Mogo has "X" */,
165 /* side pattern - eye piercing:
166 * # O O O .
167 * # O . O .
168 * # . . . .
169 * # # # # # */
170 /* side pattern - make eye */ /* 0.44 */
171 "?X."
172 "Q.X"
173 "###",
174 #if 0
175 "Oxx"
176 "..."
177 "###",
178 #endif
180 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
182 static inline bool
183 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m, bool middle_ladder, double *gamma)
185 struct moggy_policy *pp = p->data;
186 /* Check if 3x3 pattern is matched by given move... */
187 char pi = -1;
188 if (!pattern3_move_here(&pp->patterns, b, m, &pi))
189 return false;
190 /* ...and the move is not obviously stupid. */
191 if (is_bad_selfatari(b, m->color, m->coord))
192 return false;
193 /* Ladder moves are stupid. */
194 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
195 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor, middle_ladder)
196 && !can_countercapture(b, board_at(b, group_base(atari_neighbor)),
197 atari_neighbor, m->color, NULL, 0))
198 return false;
199 //fprintf(stderr, "%s: %d (%.3f)\n", coord2sstr(m->coord, b), (int) pi, pp->pat3_gammas[(int) pi]);
200 if (gamma)
201 *gamma = pp->pat3_gammas[(int) pi];
202 return true;
205 static void
206 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q, fixp_t *gammas)
208 struct moggy_policy *pp = p->data;
209 struct move m2 = { .coord = c, .color = color };
210 double gamma;
211 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2, pp->middle_ladder, &gamma)) {
212 mq_gamma_add(q, gammas, c, gamma, 1<<MQ_PAT3);
216 /* Check if we match any pattern around given move (with the other color to play). */
217 static void
218 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q, fixp_t *gammas)
220 /* Suicides do not make any patterns and confuse us. */
221 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
222 return;
224 foreach_8neighbor(b, m->coord) {
225 apply_pattern_here(p, b, c, stone_other(m->color), q, gammas);
226 } foreach_8neighbor_end;
228 if (mm) { /* Second move for pattern searching */
229 foreach_8neighbor(b, mm->coord) {
230 if (coord_is_8adjecent(m->coord, c, b))
231 continue;
232 apply_pattern_here(p, b, c, stone_other(m->color), q, gammas);
233 } foreach_8neighbor_end;
236 if (PLDEBUGL(5))
237 mq_gamma_print(q, gammas, b, "Pattern");
241 static void
242 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
244 struct moggy_policy *pp = p->data;
245 if (!pp->jdict)
246 return;
248 for (int i = 0; i < 4; i++) {
249 hash_t h = b->qhash[i] & joseki_hash_mask;
250 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
251 if (!cc) continue;
252 for (; !is_pass(*cc); cc++) {
253 if (coord_quadrant(*cc, b) != i)
254 continue;
255 if (board_is_valid_play(b, to_play, *cc))
256 continue;
257 mq_add(q, *cc, 1<<MQ_JOSEKI);
261 if (q->moves > 0 && PLDEBUGL(5))
262 mq_print(q, b, "Joseki");
265 static void
266 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
268 if (b->clen == 0)
269 return;
271 struct moggy_policy *pp = p->data;
272 if (pp->capcheckall) {
273 for (int g = 0; g < b->clen; g++)
274 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
275 if (PLDEBUGL(5))
276 mq_print(q, b, "Global atari");
277 if (pp->fullchoose)
278 return;
281 int g_base = fast_random(b->clen);
282 for (int g = g_base; g < b->clen; g++) {
283 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
284 if (q->moves > 0) {
285 /* XXX: Try carrying on. */
286 if (PLDEBUGL(5))
287 mq_print(q, b, "Global atari");
288 if (pp->fullchoose)
289 return;
292 for (int g = 0; g < g_base; g++) {
293 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
294 if (q->moves > 0) {
295 /* XXX: Try carrying on. */
296 if (PLDEBUGL(5))
297 mq_print(q, b, "Global atari");
298 if (pp->fullchoose)
299 return;
304 static void
305 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
307 struct moggy_policy *pp = p->data;
309 /* Did the opponent play a self-atari? */
310 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
311 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, pp->middle_ladder, 1<<MQ_LATARI);
314 foreach_neighbor(b, m->coord, {
315 group_t g = group_at(b, c);
316 if (!g || board_group_info(b, g).libs != 1)
317 continue;
318 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, pp->middle_ladder, 1<<MQ_LATARI);
321 if (PLDEBUGL(5))
322 mq_print(q, b, "Local atari");
326 static void
327 local_ladder_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
329 group_t group = group_at(b, m->coord);
331 if (board_group_info(b, group).libs != 2)
332 return;
334 for (int i = 0; i < 2; i++) {
335 coord_t chase = board_group_info(b, group).lib[i];
336 coord_t escape = board_group_info(b, group).lib[1 - i];
337 if (wouldbe_ladder(b, group, escape, chase, board_at(b, group)))
338 mq_add(q, chase, 1<<MQ_LADDER);
341 if (q->moves > 0 && PLDEBUGL(5))
342 mq_print(q, b, "Ladder");
346 static void
347 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct libmap_mq *q)
349 struct moggy_policy *pp = p->data;
350 group_t group = group_at(b, m->coord), group2 = 0;
352 /* Does the opponent have just two liberties? */
353 if (board_group_info(b, group).libs == 2) {
354 group_2lib_check(b, group, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
355 #if 0
356 /* We always prefer to take off an enemy chain liberty
357 * before pulling out ourselves. */
358 /* XXX: We aren't guaranteed to return to that group
359 * later. */
360 if (q->moves)
361 return q->move[fast_random(q->moves)];
362 #endif
365 /* Then he took a third liberty from neighboring chain? */
366 foreach_neighbor(b, m->coord, {
367 group_t g = group_at(b, c);
368 if (!g || g == group || g == group2 || board_group_info(b, g).libs != 2)
369 continue;
370 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
371 group2 = g; // prevent trivial repeated checks
374 if (PLDEBUGL(5))
375 libmap_mq_print(q, b, "Local 2lib");
378 static void
379 local_nlib_check(struct playout_policy *p, struct board *b, struct move *m, struct libmap_mq *q)
381 struct moggy_policy *pp = p->data;
382 enum stone color = stone_other(m->color);
384 /* Attacking N-liberty groups in general is probably
385 * not feasible. What we are primarily concerned about is
386 * counter-attacking groups that have two physical liberties,
387 * but three effective liberties:
389 * . O . . . . #
390 * O O X X X X #
391 * . X O O X . #
392 * . X O . O X #
393 * . X O O . X #
394 * # # # # # # #
396 * The time for this to come is when the opponent took a liberty
397 * of ours, making a few-liberty group. Therefore, we focus
398 * purely on defense.
400 * There is a tradeoff - down to how many liberties we need to
401 * be to start looking? nlib_count=3 will work for the left black
402 * group (2lib-solver will suggest connecting the false eye), but
403 * not for top black group (it is too late to start playing 3-3
404 * capturing race). Also, we cannot prevent stupidly taking an
405 * outside liberty ourselves; the higher nlib_count, the higher
406 * the chance we withstand this.
408 * However, higher nlib_count means that we will waste more time
409 * checking non-urgent or alive groups, and we will play silly
410 * or wasted moves around alive groups. */
412 group_t group2 = 0;
413 foreach_8neighbor(b, m->coord) {
414 group_t g = group_at(b, c);
415 if (!g || group2 == g || board_at(b, c) != color)
416 continue;
417 if (board_group_info(b, g).libs < 3 || board_group_info(b, g).libs > pp->nlib_count)
418 continue;
419 group_nlib_defense_check(b, g, color, q, 1<<MQ_LNLIB);
420 group2 = g; // prevent trivial repeated checks
421 } foreach_8neighbor_end;
423 if (PLDEBUGL(5))
424 libmap_mq_print(q, b, "Local nlib");
427 static coord_t
428 nakade_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play)
430 coord_t empty = pass;
431 foreach_neighbor(b, m->coord, {
432 if (board_at(b, c) != S_NONE)
433 continue;
434 if (is_pass(empty)) {
435 empty = c;
436 continue;
438 if (!coord_is_8adjecent(c, empty, b)) {
439 /* Seems like impossible nakade
440 * shape! */
441 return pass;
444 assert(!is_pass(empty));
446 coord_t nakade = nakade_point(b, empty, stone_other(to_play));
447 if (PLDEBUGL(5) && !is_pass(nakade))
448 fprintf(stderr, "Nakade: %s\n", coord2sstr(nakade, b));
449 return nakade;
452 static void
453 eye_fix_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play, struct move_queue *q)
455 /* The opponent could have filled an approach liberty for
456 * falsifying an eye like these:
458 * # # # # # # X . X X O O last_move == 1
459 * X X 2 O 1 O X X 2 O 1 O => suggest 2
460 * X . X X O . X . X X O .
461 * X X O O . . X X O O . O
463 * This case seems pretty common (e.g. Zen-Ishida game). */
465 /* Iterator for walking coordinates in a clockwise fashion
466 * (nei8 jumps "over" the middle point, inst. of "around). */
467 int size = board_size(b);
468 int nei8_clockwise[10] = { -size-1, 1, 1, size, size, -1, -1, -size, -size, 1 };
470 /* This is sort of like a cross between foreach_diag_neighbor
471 * and foreach_8neighbor. */
472 coord_t c = m->coord;
473 for (int dni = 0; dni < 8; dni += 2) {
474 // one diagonal neighbor
475 coord_t c0 = c + nei8_clockwise[dni];
476 // adjecent staight neighbor
477 coord_t c1 = c0 + nei8_clockwise[dni + 1];
478 // and adjecent another diagonal neighbor
479 coord_t c2 = c1 + nei8_clockwise[dni + 2];
481 /* The last move must have a pair of unfriendly diagonal
482 * neighbors separated by a friendly stone. */
483 //fprintf(stderr, "inv. %s(%s)-%s(%s)-%s(%s), imm. libcount %d\n", coord2sstr(c0, b), stone2str(board_at(b, c0)), coord2sstr(c1, b), stone2str(board_at(b, c1)), coord2sstr(c2, b), stone2str(board_at(b, c2)), immediate_liberty_count(b, c1));
484 if ((board_at(b, c0) == to_play || board_at(b, c0) == S_OFFBOARD)
485 && board_at(b, c1) == m->color
486 && (board_at(b, c2) == to_play || board_at(b, c2) == S_OFFBOARD)
487 /* The friendly stone then must have an empty neighbor... */
488 /* XXX: This works only for single stone, not e.g. for two
489 * stones in a row */
490 && immediate_liberty_count(b, c1) > 0) {
491 foreach_neighbor(b, c1, {
492 if (c == m->coord || board_at(b, c) != S_NONE)
493 continue;
494 /* ...and the neighbor must potentially falsify
495 * an eye. */
496 coord_t falsifying = c;
497 foreach_diag_neighbor(b, falsifying) {
498 if (board_at(b, c) != S_NONE)
499 continue;
500 if (!board_is_eyelike(b, c, to_play))
501 continue;
502 /* We don't care about eyes that already
503 * _are_ false (board_is_false_eyelike())
504 * but that can become false. Therefore,
505 * either ==1 diagonal neighbor is
506 * opponent's (except in atari) or ==2
507 * are board edge. */
508 coord_t falsified = c;
509 int color_diag_libs[S_MAX] = {0};
510 foreach_diag_neighbor(b, falsified) {
511 if (board_at(b, c) == m->color && board_group_info(b, group_at(b, c)).libs == 1) {
512 /* Suggest capturing a falsifying stone in atari. */
513 mq_add(q, board_group_info(b, group_at(b, c)).lib[0], 0);
514 } else {
515 color_diag_libs[board_at(b, c)]++;
517 } foreach_diag_neighbor_end;
518 if (color_diag_libs[m->color] == 1 || (color_diag_libs[m->color] == 0 && color_diag_libs[S_OFFBOARD] == 2)) {
519 /* That's it. Fill the falsifying
520 * liberty before it's too late! */
521 mq_add(q, falsifying, 0);
523 } foreach_diag_neighbor_end;
527 c = c1;
530 if (q->moves > 0 && PLDEBUGL(5))
531 mq_print(q, b, "Eye fix");
534 coord_t
535 fillboard_check(struct playout_policy *p, struct board *b)
537 struct moggy_policy *pp = p->data;
538 unsigned int fbtries = b->flen / 8;
539 if (pp->fillboardtries < fbtries)
540 fbtries = pp->fillboardtries;
542 for (unsigned int i = 0; i < fbtries; i++) {
543 coord_t coord = b->f[fast_random(b->flen)];
544 if (immediate_liberty_count(b, coord) != 4)
545 continue;
546 foreach_diag_neighbor(b, coord) {
547 if (board_at(b, c) != S_NONE)
548 goto next_try;
549 } foreach_diag_neighbor_end;
550 return coord;
551 next_try:
554 return pass;
557 coord_t
558 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
560 struct moggy_policy *pp = p->data;
562 if (PLDEBUGL(5))
563 board_print(b, stderr);
565 /* Ko fight check */
566 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
567 && b->moves - b->last_ko_age < pp->koage
568 && pp->korate > fast_random(100)) {
569 if (board_is_valid_play(b, to_play, b->last_ko.coord)
570 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
571 return b->last_ko.coord;
574 /* Local checks */
575 if (!is_pass(b->last_move.coord)) {
576 /* Nakade check */
577 if (pp->nakaderate > fast_random(100)
578 && immediate_liberty_count(b, b->last_move.coord) > 0) {
579 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
580 if (!is_pass(nakade))
581 return nakade;
584 /* Local group in atari? */
585 if (pp->lcapturerate > fast_random(100)) {
586 struct move_queue q; q.moves = 0;
587 local_atari_check(p, b, &b->last_move, &q);
588 if (q.moves > 0)
589 return mq_pick(&q);
592 /* Local group trying to escape ladder? */
593 if (pp->ladderrate > fast_random(100)) {
594 struct move_queue q; q.moves = 0;
595 local_ladder_check(p, b, &b->last_move, &q);
596 if (q.moves > 0)
597 return mq_pick(&q);
600 /* Local group can be PUT in atari? */
601 if (pp->atarirate > fast_random(100)) {
602 struct libmap_mq q; q.mq.moves = 0;
603 local_2lib_check(p, b, &b->last_move, &q);
604 coord_t c = libmap_queue_mqpick(b, &q);
605 if (!is_pass(c))
606 return c;
609 /* Local group reduced some of our groups to 3 libs? */
610 if (pp->nlibrate > fast_random(100)) {
611 struct libmap_mq q; q.mq.moves = 0;
612 local_nlib_check(p, b, &b->last_move, &q);
613 coord_t c = libmap_queue_mqpick(b, &q);
614 if (!is_pass(c))
615 return c;
618 /* Some other semeai-ish shape checks */
619 if (pp->eyefixrate > fast_random(100)) {
620 struct move_queue q; q.moves = 0;
621 eye_fix_check(p, b, &b->last_move, to_play, &q);
622 if (q.moves > 0)
623 return mq_pick(&q);
626 /* Check for patterns we know */
627 if (pp->patternrate > fast_random(100)) {
628 struct move_queue q; q.moves = 0;
629 fixp_t gammas[MQL];
630 apply_pattern(p, b, &b->last_move,
631 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
632 &q, gammas);
633 if (q.moves > 0)
634 return mq_gamma_pick(&q, gammas);
638 /* Global checks */
640 /* Any groups in atari? */
641 if (pp->capturerate > fast_random(100)) {
642 struct move_queue q; q.moves = 0;
643 global_atari_check(p, b, to_play, &q);
644 if (q.moves > 0)
645 return mq_pick(&q);
648 /* Joseki moves? */
649 if (pp->josekirate > fast_random(100)) {
650 struct move_queue q; q.moves = 0;
651 joseki_check(p, b, to_play, &q);
652 if (q.moves > 0)
653 return mq_pick(&q);
656 /* Fill board */
657 if (pp->fillboardtries > 0) {
658 coord_t c = fillboard_check(p, b);
659 if (!is_pass(c))
660 return c;
663 return pass;
666 /* Pick a move from queue q, giving different likelihoods to moves
667 * based on their tags. */
668 coord_t
669 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
671 struct moggy_policy *pp = p->data;
673 /* First, merge all entries for a move. */
674 /* We use a naive O(N^2) since the average length of the queue
675 * is about 1.4. */
676 for (unsigned int i = 0; i < q->moves; i++) {
677 for (unsigned int j = i + 1; j < q->moves; j++) {
678 if (q->move[i] != q->move[j])
679 continue;
680 q->tag[i] |= q->tag[j];
681 q->moves--;
682 q->tag[j] = q->tag[q->moves];
683 q->move[j] = q->move[q->moves];
687 /* Now, construct a probdist. */
688 fixp_t total = 0;
689 fixp_t pd[q->moves];
690 for (unsigned int i = 0; i < q->moves; i++) {
691 double val = 1.0;
692 assert(q->tag[i] != 0);
693 for (int j = 0; j < MQ_MAX; j++)
694 if (q->tag[i] & (1<<j)) {
695 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
696 val *= pp->mq_prob[j];
698 pd[i] = double_to_fixp(val);
699 total += pd[i];
701 total += double_to_fixp(pp->tenuki_prob);
703 /* Finally, pick a move! */
704 fixp_t stab = fast_irandom(total);
705 if (PLDEBUGL(5)) {
706 fprintf(stderr, "Pick (total %.3f stab %.3f): ", fixp_to_double(total), fixp_to_double(stab));
707 for (unsigned int i = 0; i < q->moves; i++) {
708 fprintf(stderr, "%s(%x:%.3f) ", coord2sstr(q->move[i], b), q->tag[i], fixp_to_double(pd[i]));
710 fprintf(stderr, "\n");
712 for (unsigned int i = 0; i < q->moves; i++) {
713 //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));
714 if (stab < pd[i])
715 return q->move[i];
716 stab -= pd[i];
719 /* Tenuki. */
720 assert(stab < double_to_fixp(pp->tenuki_prob));
721 return pass;
724 coord_t
725 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
727 struct moggy_policy *pp = p->data;
728 struct move_queue q; q.moves = 0;
730 if (PLDEBUGL(5))
731 board_print(b, stderr);
733 /* Ko fight check */
734 if (pp->korate > 0 && !is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
735 && b->moves - b->last_ko_age < pp->koage) {
736 if (board_is_valid_play(b, to_play, b->last_ko.coord)
737 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
738 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
741 /* Local checks */
742 if (!is_pass(b->last_move.coord)) {
743 /* Nakade check */
744 if (pp->nakaderate > 0 && immediate_liberty_count(b, b->last_move.coord) > 0) {
745 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
746 if (!is_pass(nakade))
747 mq_add(&q, nakade, 1<<MQ_NAKADE);
750 /* Local group in atari? */
751 if (pp->lcapturerate > 0)
752 local_atari_check(p, b, &b->last_move, &q);
754 /* Local group trying to escape ladder? */
755 if (pp->ladderrate > 0)
756 local_ladder_check(p, b, &b->last_move, &q);
758 struct libmap_mq lmq = { .mq = { .moves = 0 } };
760 /* Local group can be PUT in atari? */
761 if (pp->atarirate > 0)
762 local_2lib_check(p, b, &b->last_move, &lmq);
764 /* Local group reduced some of our groups to 3 libs? */
765 if (pp->nlibrate > 0)
766 local_nlib_check(p, b, &b->last_move, &lmq);
768 mq_append(&q, &lmq.mq);
770 /* Some other semeai-ish shape checks */
771 if (pp->eyefixrate > 0)
772 eye_fix_check(p, b, &b->last_move, to_play, &q);
774 /* Check for patterns we know */
775 if (pp->patternrate > 0) {
776 fixp_t gammas[MQL];
777 apply_pattern(p, b, &b->last_move,
778 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
779 &q, gammas);
780 /* FIXME: Use the gammas. */
784 /* Global checks */
786 /* Any groups in atari? */
787 if (pp->capturerate > 0)
788 global_atari_check(p, b, to_play, &q);
790 /* Joseki moves? */
791 if (pp->josekirate > 0)
792 joseki_check(p, b, to_play, &q);
794 #if 0
795 /* Average length of the queue is 1.4 move. */
796 printf("MQL %d ", q.moves);
797 for (unsigned int i = 0; i < q.moves; i++)
798 printf("%s ", coord2sstr(q.move[i], b));
799 printf("\n");
800 #endif
802 if (q.moves > 0)
803 return mq_tagged_choose(p, b, to_play, &q);
805 /* Fill board */
806 if (pp->fillboardtries > 0) {
807 coord_t c = fillboard_check(p, b);
808 if (!is_pass(c))
809 return c;
812 return pass;
816 void
817 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
819 struct moggy_policy *pp = p->data;
820 struct board *b = map->b;
822 if (board_group_info(b, g).libs > pp->nlib_count)
823 return;
825 if (PLDEBUGL(5)) {
826 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
827 board_print(b, stderr);
830 if (board_group_info(b, g).libs > 2) {
831 if (!pp->nlibrate)
832 return;
833 if (board_at(b, g) != map->to_play)
834 return; // we do only defense
835 /* TODO: Tie libmap info into tree search. */
836 struct libmap_mq q; q.mq.moves = 0;
837 group_nlib_defense_check(b, g, map->to_play, &q, 0);
838 while (q.mq.moves--) {
839 coord_t coord = q.mq.move[q.mq.moves];
840 if (PLDEBUGL(5))
841 fprintf(stderr, "1.0: nlib %s\n", coord2sstr(coord, b));
842 int assess = games / 2;
843 add_prior_value(map, coord, 1, assess);
845 return;
848 if (board_group_info(b, g).libs == 2) {
849 if (pp->ladderrate) {
850 /* Make sure to play the correct liberty in case
851 * this is a group that can be caught in a ladder. */
852 bool ladderable = false;
853 for (int i = 0; i < 2; i++) {
854 coord_t chase = board_group_info(b, g).lib[i];
855 coord_t escape = board_group_info(b, g).lib[1 - i];
856 if (wouldbe_ladder(b, g, escape, chase, board_at(b, g))) {
857 add_prior_value(map, chase, 1, games);
858 ladderable = true;
861 if (ladderable)
862 return; // do not suggest the other lib at all
865 if (!pp->atarirate)
866 return;
867 struct libmap_mq q; q.mq.moves = 0;
868 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
869 while (q.mq.moves--) {
870 coord_t coord = q.mq.move[q.mq.moves];
871 if (PLDEBUGL(5))
872 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
873 int assess = games / 2;
874 add_prior_value(map, coord, 1, assess);
876 return;
879 /* This group, sir, is in atari! */
881 struct move_queue q; q.moves = 0;
882 coord_t ladder = pass;
883 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, true, 0);
884 while (q.moves--) {
885 coord_t coord = q.move[q.moves];
887 /* _Never_ play here if this move plays out
888 * a caught ladder. */
889 if (coord == ladder && !board_playing_ko_threat(b)) {
890 /* Note that the opposite is not guarded against;
891 * we do not advise against capturing a laddered
892 * group (but we don't encourage it either). Such
893 * a move can simplify tactical situations if we
894 * can afford it. */
895 if (map->to_play != board_at(b, g))
896 continue;
897 /* FIXME: We give the malus even if this move
898 * captures another group. */
899 if (PLDEBUGL(5))
900 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
901 add_prior_value(map, coord, 0, games);
902 continue;
905 if (!pp->capturerate && !pp->lcapturerate)
906 continue;
908 int assess = games * 2;
909 if (pp->cap_stone_denom > 0) {
910 int stones = group_stone_count(b, g, pp->cap_stone_max) - (pp->cap_stone_min-1);
911 assess += (stones > 0 ? stones : 0) * games * 100 / pp->cap_stone_denom;
913 if (PLDEBUGL(5))
914 fprintf(stderr, "1.0 (%d): atari %s\n", assess, coord2sstr(coord, b));
915 add_prior_value(map, coord, 1, assess);
919 void
920 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
922 struct moggy_policy *pp = p->data;
923 struct board *b = map->b;
925 if (PLDEBUGL(5)) {
926 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
927 board_print(b, stderr);
930 /* Is this move a self-atari? */
931 if (pp->selfatarirate) {
932 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
933 if (PLDEBUGL(5))
934 fprintf(stderr, "0.0: self-atari\n");
935 add_prior_value(map, coord, 0, games);
936 if (!pp->selfatari_other)
937 return;
938 /* If we can play on the other liberty of the
939 * endangered group, do! */
940 coord = selfatari_cousin(b, map->to_play, coord, NULL);
941 if (is_pass(coord))
942 return;
943 if (PLDEBUGL(5))
944 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
945 add_prior_value(map, coord, 1.0, games);
946 return;
950 /* Pattern check */
951 if (pp->patternrate) {
952 // XXX: Use gamma value?
953 struct move m = { .color = map->to_play, .coord = coord };
954 if (test_pattern3_here(p, b, &m, true, NULL)) {
955 if (PLDEBUGL(5))
956 fprintf(stderr, "1.0: pattern\n");
957 add_prior_value(map, coord, 1, games);
961 return;
964 void
965 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
967 struct moggy_policy *pp = p->data;
969 /* First, go through all endangered groups. */
970 for (group_t g = 1; g < board_size2(map->b); g++)
971 if (group_at(map->b, g) == g)
972 playout_moggy_assess_group(p, map, g, games);
974 /* Then, assess individual moves. */
975 if (!pp->patternrate && !pp->selfatarirate)
976 return;
977 foreach_free_point(map->b) {
978 if (map->consider[c])
979 playout_moggy_assess_one(p, map, c, games);
980 } foreach_free_point_end;
983 bool
984 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
986 struct moggy_policy *pp = p->data;
988 /* The idea is simple for now - never allow self-atari moves.
989 * They suck in general, but this also permits us to actually
990 * handle seki in the playout stage. */
992 if (fast_random(100) >= pp->selfatarirate) {
993 if (PLDEBUGL(5))
994 fprintf(stderr, "skipping sar test\n");
995 goto sar_skip;
997 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
998 if (selfatari) {
999 if (PLDEBUGL(5))
1000 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
1001 stone2str(m->color), coord2sstr(m->coord, b));
1002 if (pp->selfatari_other) {
1003 /* Ok, try the other liberty of the atari'd group. */
1004 coord_t c = selfatari_cousin(b, m->color, m->coord, NULL);
1005 if (is_pass(c)) return false;
1006 if (PLDEBUGL(5))
1007 fprintf(stderr, "___ Redirecting to other lib %s\n",
1008 coord2sstr(c, b));
1009 m->coord = c;
1010 return true;
1012 return false;
1014 sar_skip:
1016 /* Check if we don't seem to be filling our eye. This should
1017 * happen only for false eyes, but some of them are in fact
1018 * real eyes with diagonal filled by a dead stone. Prefer
1019 * to counter-capture in that case. */
1020 if (fast_random(100) >= pp->eyefillrate) {
1021 if (PLDEBUGL(5))
1022 fprintf(stderr, "skipping eyefill test\n");
1023 goto eyefill_skip;
1025 bool eyefill = board_is_eyelike(b, m->coord, m->color);
1026 if (eyefill) {
1027 foreach_diag_neighbor(b, m->coord) {
1028 if (board_at(b, c) != stone_other(m->color))
1029 continue;
1030 switch (board_group_info(b, group_at(b, c)).libs) {
1031 case 1: /* Capture! */
1032 c = board_group_info(b, group_at(b, c)).lib[0];
1033 if (PLDEBUGL(5))
1034 fprintf(stderr, "___ Redirecting to capture %s\n",
1035 coord2sstr(c, b));
1036 m->coord = c;
1037 return true;
1038 case 2: /* Try to switch to some 2-lib neighbor. */
1039 for (int i = 0; i < 2; i++) {
1040 coord_t l = board_group_info(b, group_at(b, c)).lib[i];
1041 if (board_is_one_point_eye(b, l, board_at(b, c)))
1042 continue;
1043 if (is_bad_selfatari(b, m->color, l))
1044 continue;
1045 m->coord = l;
1046 return true;
1048 break;
1050 } foreach_diag_neighbor_end;
1053 eyefill_skip:
1054 return true;
1058 struct playout_policy *
1059 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
1061 struct playout_policy *p = calloc2(1, sizeof(*p));
1062 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
1063 p->data = pp;
1064 p->choose = playout_moggy_seqchoose;
1065 p->assess = playout_moggy_assess;
1066 p->permit = playout_moggy_permit;
1068 pp->jdict = jdict;
1070 /* These settings are tuned for 19x19 play with several threads
1071 * on reasonable time limits (i.e., rather large number of playouts).
1072 * XXX: no 9x9 tuning has been done recently. */
1073 int rate = board_large(b) ? 80 : 90;
1075 pp->lcapturerate = pp->atarirate = pp->nlibrate
1076 = pp->josekirate = -1U;
1077 pp->patternrate = pp->eyefixrate = 100;
1078 pp->nlibrate = 20;
1079 pp->nakaderate = 20;
1080 pp->pattern2 = true;
1081 pp->lcapturerate = 90;
1082 pp->korate = 20; pp->koage = 4;
1083 pp->alwaysccaprate = 40;
1084 pp->eyefillrate = 60;
1086 /* selfatarirate is slightly special, since to avoid playing some
1087 * silly move that stays on the board, it needs to block it many
1088 * times during a simulation - we'd like that to happen in most
1089 * simulations, so we try to use a very high selfatarirate.
1090 * XXX: Perhaps it would be better to permanently ban moves in
1091 * the current simulation after testing them once. */
1092 pp->selfatarirate = 95;
1093 pp->selfatari_other = true;
1095 pp->cap_stone_min = 2;
1096 pp->cap_stone_max = 15;
1097 pp->cap_stone_denom = 200;
1099 pp->atari_def_no_hopeless = !board_large(b);
1100 pp->atari_miaisafe = true;
1101 pp->nlib_count = 4;
1103 /* C is stupid. */
1104 double mq_prob_default[MQ_MAX] = {
1105 [MQ_KO] = 6.0,
1106 [MQ_NAKADE] = 5.5,
1107 [MQ_LATARI] = 5.0,
1108 [MQ_L2LIB] = 4.0,
1109 [MQ_LNLIB] = 3.5,
1110 [MQ_PAT3] = 3.0,
1111 [MQ_GATARI] = 2.0,
1112 [MQ_JOSEKI] = 1.0,
1114 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
1116 /* Default 3x3 pattern gammas tuned on 15x15 with 500s/game on
1117 * i7-3770 single thread using 40000 CLOP games. */
1118 double pat3_gammas_default[PAT3_N] = {
1119 0.52, 0.53, 0.32, 0.22, 0.37, 0.28, 0.21, 0.19, 0.82,
1120 0.12, 0.20, 0.11, 0.16, 0.57, 0.44
1122 memcpy(pp->pat3_gammas, pat3_gammas_default, sizeof(pp->pat3_gammas));
1124 if (arg) {
1125 char *optspec, *next = arg;
1126 while (*next) {
1127 optspec = next;
1128 next += strcspn(next, ":");
1129 if (*next) { *next++ = 0; } else { *next = 0; }
1131 char *optname = optspec;
1132 char *optval = strchr(optspec, '=');
1133 if (optval) *optval++ = 0;
1135 if (!strcasecmp(optname, "debug") && optval) {
1136 p->debug_level = atoi(optval);
1137 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
1138 pp->lcapturerate = atoi(optval);
1139 } else if (!strcasecmp(optname, "ladderrate") && optval) {
1140 pp->ladderrate = atoi(optval);
1141 } else if (!strcasecmp(optname, "atarirate") && optval) {
1142 pp->atarirate = atoi(optval);
1143 } else if (!strcasecmp(optname, "nlibrate") && optval) {
1144 pp->nlibrate = atoi(optval);
1145 } else if (!strcasecmp(optname, "capturerate") && optval) {
1146 pp->capturerate = atoi(optval);
1147 } else if (!strcasecmp(optname, "patternrate") && optval) {
1148 pp->patternrate = atoi(optval);
1149 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
1150 pp->selfatarirate = atoi(optval);
1151 } else if (!strcasecmp(optname, "eyefillrate") && optval) {
1152 pp->eyefillrate = atoi(optval);
1153 } else if (!strcasecmp(optname, "korate") && optval) {
1154 pp->korate = atoi(optval);
1155 } else if (!strcasecmp(optname, "josekirate") && optval) {
1156 pp->josekirate = atoi(optval);
1157 } else if (!strcasecmp(optname, "nakaderate") && optval) {
1158 pp->nakaderate = atoi(optval);
1159 } else if (!strcasecmp(optname, "eyefixrate") && optval) {
1160 pp->eyefixrate = atoi(optval);
1161 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
1162 pp->alwaysccaprate = atoi(optval);
1163 } else if (!strcasecmp(optname, "rate") && optval) {
1164 rate = atoi(optval);
1165 } else if (!strcasecmp(optname, "fillboardtries")) {
1166 pp->fillboardtries = atoi(optval);
1167 } else if (!strcasecmp(optname, "koage") && optval) {
1168 pp->koage = atoi(optval);
1169 } else if (!strcasecmp(optname, "pattern2")) {
1170 pp->pattern2 = optval && *optval == '0' ? false : true;
1171 } else if (!strcasecmp(optname, "selfatari_other")) {
1172 pp->selfatari_other = optval && *optval == '0' ? false : true;
1173 } else if (!strcasecmp(optname, "capcheckall")) {
1174 pp->capcheckall = optval && *optval == '0' ? false : true;
1175 } else if (!strcasecmp(optname, "cap_stone_min") && optval) {
1176 pp->cap_stone_min = atoi(optval);
1177 } else if (!strcasecmp(optname, "cap_stone_max") && optval) {
1178 pp->cap_stone_max = atoi(optval);
1179 } else if (!strcasecmp(optname, "cap_stone_denom") && optval) {
1180 pp->cap_stone_denom = atoi(optval);
1181 } else if (!strcasecmp(optname, "atari_miaisafe")) {
1182 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
1183 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
1184 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
1185 } else if (!strcasecmp(optname, "nlib_count") && optval) {
1186 pp->nlib_count = atoi(optval);
1187 } else if (!strcasecmp(optname, "middle_ladder")) {
1188 pp->middle_ladder = optval && *optval == '0' ? false : true;
1189 } else if (!strcasecmp(optname, "fullchoose")) {
1190 pp->fullchoose = true;
1191 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
1192 } else if (!strcasecmp(optname, "mqprob") && optval) {
1193 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
1194 for (int i = 0; *optval && i < MQ_MAX; i++) {
1195 pp->mq_prob[i] = atof(optval);
1196 optval += strcspn(optval, "%");
1197 if (*optval) optval++;
1199 } else if (!strcasecmp(optname, "pat3gammas") && optval) {
1200 /* PAT3_N %-separated floating point values */
1201 for (int i = 0; *optval && i < PAT3_N; i++) {
1202 pp->pat3_gammas[i] = atof(optval);
1203 optval += strcspn(optval, "%");
1204 if (*optval) optval++;
1206 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
1207 pp->tenuki_prob = atof(optval);
1208 } else {
1209 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
1210 exit(1);
1214 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
1215 if (pp->atarirate == -1U) pp->atarirate = rate;
1216 if (pp->nlibrate == -1U) pp->nlibrate = rate;
1217 if (pp->capturerate == -1U) pp->capturerate = rate;
1218 if (pp->patternrate == -1U) pp->patternrate = rate;
1219 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
1220 if (pp->eyefillrate == -1U) pp->eyefillrate = rate;
1221 if (pp->korate == -1U) pp->korate = rate;
1222 if (pp->josekirate == -1U) pp->josekirate = rate;
1223 if (pp->ladderrate == -1U) pp->ladderrate = rate;
1224 if (pp->nakaderate == -1U) pp->nakaderate = rate;
1225 if (pp->eyefixrate == -1U) pp->eyefixrate = rate;
1226 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
1228 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
1230 return p;