Merge pull request #50 from lemonsqueeze/can_countercap
[pachi.git] / playout / moggy.c
blob8666eb895617ff2e6a32357d2feb707df36f47dc
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 #define MQ_LADDER MQ_L2LIB /* XXX: We want to fit in char still! */
45 MQ_LNLIB,
46 MQ_PAT3,
47 MQ_GATARI,
48 MQ_JOSEKI,
49 MQ_NAKADE,
50 MQ_MAX
54 #define PAT3_N 15
56 /* Note that the context can be shared by multiple threads! */
58 struct moggy_policy {
59 unsigned int lcapturerate, atarirate, nlibrate, ladderrate, capturerate, patternrate, korate, josekirate, nakaderate, eyefixrate;
60 unsigned int selfatarirate, eyefillrate, alwaysccaprate;
61 unsigned int fillboardtries;
62 int koage;
63 /* Whether to look for patterns around second-to-last move. */
64 bool pattern2;
65 /* Whether, when self-atari attempt is detected, to play the other
66 * group's liberty if that is non-self-atari. */
67 bool selfatari_other;
68 /* Whether to read out ladders elsewhere than near the board
69 * in the playouts. Note that such ladder testing is currently
70 * a fairly expensive operation. */
71 bool middle_ladder;
73 /* 1lib settings: */
74 /* Whether to always pick from moves capturing all groups in
75 * global_atari_check(). */
76 bool capcheckall;
77 /* Prior stone weighting. Weight of each stone between
78 * cap_stone_min and cap_stone_max is (assess*100)/cap_stone_denom. */
79 int cap_stone_min, cap_stone_max;
80 int cap_stone_denom;
82 /* 2lib settings: */
83 bool atari_def_no_hopeless;
84 bool atari_miaisafe;
86 /* nlib settings: */
87 int nlib_count;
89 struct joseki_dict *jdict;
90 struct pattern3s patterns;
92 double pat3_gammas[PAT3_N];
94 /* Gamma values for queue tags - correspond to probabilities. */
95 /* XXX: Tune. */
96 bool fullchoose;
97 double mq_prob[MQ_MAX], tenuki_prob;
101 static char moggy_patterns_src[PAT3_N][11] = {
102 /* hane pattern - enclosing hane */ /* 0.52 */
103 "XOX"
104 "..."
105 "???",
106 /* hane pattern - non-cutting hane */ /* 0.53 */
107 "YO."
108 "..."
109 "?.?",
110 /* hane pattern - magari */ /* 0.32 */
111 "XO?"
112 "X.."
113 "x.?",
114 /* hane pattern - thin hane */ /* 0.22 */
115 "XOO"
116 "..."
117 "?.?" "X",
118 /* generic pattern - katatsuke or diagonal attachment; similar to magari */ /* 0.37 */
119 ".Q."
120 "Y.."
121 "...",
122 /* cut1 pattern (kiri) - unprotected cut */ /* 0.28 */
123 "XO?"
124 "O.o"
125 "?o?",
126 /* cut1 pattern (kiri) - peeped cut */ /* 0.21 */
127 "XO?"
128 "O.X"
129 "???",
130 /* cut2 pattern (de) */ /* 0.19 */
131 "?X?"
132 "O.O"
133 "ooo",
134 /* cut keima (not in Mogo) */ /* 0.82 */
135 "OX?"
136 "?.O"
137 "?o?", /* oo? has some pathological tsumego cases */
138 /* side pattern - chase */ /* 0.12 */
139 "X.?"
140 "O.?"
141 "##?",
142 /* side pattern - block side cut */ /* 0.20 */
143 "OX?"
144 "X.O"
145 "###",
146 /* side pattern - block side connection */ /* 0.11 */
147 "?X?"
148 "x.O"
149 "###",
150 /* side pattern - sagari (SUSPICIOUS) */ /* 0.16 */
151 "?XQ"
152 "x.x" /* Mogo has "x.?" */
153 "###" /* Mogo has "X" */,
154 #if 0
155 /* side pattern - throw-in (SUSPICIOUS) */
156 "?OX"
157 "o.O"
158 "?##" "X",
159 #endif
160 /* side pattern - cut (SUSPICIOUS) */ /* 0.57 */
161 "?OY"
162 "Y.O"
163 "###" /* Mogo has "X" */,
164 /* side pattern - eye piercing:
165 * # O O O .
166 * # O . O .
167 * # . . . .
168 * # # # # # */
169 /* side pattern - make eye */ /* 0.44 */
170 "?X."
171 "Q.X"
172 "###",
173 #if 0
174 "Oxx"
175 "..."
176 "###",
177 #endif
179 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
181 static inline bool
182 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m, bool middle_ladder, double *gamma)
184 struct moggy_policy *pp = p->data;
185 /* Check if 3x3 pattern is matched by given move... */
186 char pi = -1;
187 if (!pattern3_move_here(&pp->patterns, b, m, &pi))
188 return false;
189 /* ...and the move is not obviously stupid. */
190 if (is_bad_selfatari(b, m->color, m->coord))
191 return false;
192 /* Ladder moves are stupid. */
193 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
194 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor, middle_ladder)
195 && !can_countercapture(b, atari_neighbor, NULL, 0))
196 return false;
197 //fprintf(stderr, "%s: %d (%.3f)\n", coord2sstr(m->coord, b), (int) pi, pp->pat3_gammas[(int) pi]);
198 if (gamma)
199 *gamma = pp->pat3_gammas[(int) pi];
200 return true;
203 static void
204 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q, fixp_t *gammas)
206 struct moggy_policy *pp = p->data;
207 struct move m2 = { .coord = c, .color = color };
208 double gamma;
209 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2, pp->middle_ladder, &gamma)) {
210 mq_gamma_add(q, gammas, c, gamma, 1<<MQ_PAT3);
214 /* Check if we match any pattern around given move (with the other color to play). */
215 static void
216 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q, fixp_t *gammas)
218 /* Suicides do not make any patterns and confuse us. */
219 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
220 return;
222 foreach_8neighbor(b, m->coord) {
223 apply_pattern_here(p, b, c, stone_other(m->color), q, gammas);
224 } foreach_8neighbor_end;
226 if (mm) { /* Second move for pattern searching */
227 foreach_8neighbor(b, mm->coord) {
228 if (coord_is_8adjecent(m->coord, c, b))
229 continue;
230 apply_pattern_here(p, b, c, stone_other(m->color), q, gammas);
231 } foreach_8neighbor_end;
234 if (PLDEBUGL(5))
235 mq_gamma_print(q, gammas, b, "Pattern");
239 static void
240 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
242 struct moggy_policy *pp = p->data;
243 if (!pp->jdict)
244 return;
246 for (int i = 0; i < 4; i++) {
247 hash_t h = b->qhash[i] & joseki_hash_mask;
248 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
249 if (!cc) continue;
250 for (; !is_pass(*cc); cc++) {
251 if (coord_quadrant(*cc, b) != i)
252 continue;
253 if (!board_is_valid_play(b, to_play, *cc))
254 continue;
255 mq_add(q, *cc, 1<<MQ_JOSEKI);
259 if (q->moves > 0 && PLDEBUGL(5))
260 mq_print(q, b, "Joseki");
263 static void
264 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
266 if (b->clen == 0)
267 return;
269 struct moggy_policy *pp = p->data;
270 if (pp->capcheckall) {
271 for (int g = 0; g < b->clen; g++)
272 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
273 if (PLDEBUGL(5))
274 mq_print(q, b, "Global atari");
275 if (pp->fullchoose)
276 return;
279 int g_base = fast_random(b->clen);
280 for (int g = g_base; g < b->clen; g++) {
281 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
282 if (q->moves > 0) {
283 /* XXX: Try carrying on. */
284 if (PLDEBUGL(5))
285 mq_print(q, b, "Global atari");
286 if (pp->fullchoose)
287 return;
290 for (int g = 0; g < g_base; g++) {
291 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
292 if (q->moves > 0) {
293 /* XXX: Try carrying on. */
294 if (PLDEBUGL(5))
295 mq_print(q, b, "Global atari");
296 if (pp->fullchoose)
297 return;
302 static void
303 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
305 struct moggy_policy *pp = p->data;
307 /* Did the opponent play a self-atari? */
308 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
309 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, pp->middle_ladder, 1<<MQ_LATARI);
312 foreach_neighbor(b, m->coord, {
313 group_t g = group_at(b, c);
314 if (!g || board_group_info(b, g).libs != 1)
315 continue;
316 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, pp->middle_ladder, 1<<MQ_LATARI);
319 if (PLDEBUGL(5))
320 mq_print(q, b, "Local atari");
324 static void
325 local_ladder_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
327 group_t group = group_at(b, m->coord);
329 if (board_group_info(b, group).libs != 2)
330 return;
332 for (int i = 0; i < 2; i++) {
333 coord_t chase = board_group_info(b, group).lib[i];
334 coord_t escape = board_group_info(b, group).lib[1 - i];
335 if (wouldbe_ladder(b, group, escape, chase, board_at(b, group)))
336 mq_add(q, chase, 1<<MQ_LADDER);
339 if (q->moves > 0 && PLDEBUGL(5))
340 mq_print(q, b, "Ladder");
344 static void
345 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
347 struct moggy_policy *pp = p->data;
348 group_t group = group_at(b, m->coord), group2 = 0;
350 /* Does the opponent have just two liberties? */
351 if (board_group_info(b, group).libs == 2) {
352 group_2lib_check(b, group, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
353 #if 0
354 /* We always prefer to take off an enemy chain liberty
355 * before pulling out ourselves. */
356 /* XXX: We aren't guaranteed to return to that group
357 * later. */
358 if (q->moves)
359 return q->move[fast_random(q->moves)];
360 #endif
363 /* Then he took a third liberty from neighboring chain? */
364 foreach_neighbor(b, m->coord, {
365 group_t g = group_at(b, c);
366 if (!g || g == group || g == group2 || board_group_info(b, g).libs != 2)
367 continue;
368 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
369 group2 = g; // prevent trivial repeated checks
372 if (PLDEBUGL(5))
373 mq_print(q, b, "Local 2lib");
376 static void
377 local_nlib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
379 struct moggy_policy *pp = p->data;
380 enum stone color = stone_other(m->color);
382 /* Attacking N-liberty groups in general is probably
383 * not feasible. What we are primarily concerned about is
384 * counter-attacking groups that have two physical liberties,
385 * but three effective liberties:
387 * . O . . . . #
388 * O O X X X X #
389 * . X O O X . #
390 * . X O . O X #
391 * . X O O . X #
392 * # # # # # # #
394 * The time for this to come is when the opponent took a liberty
395 * of ours, making a few-liberty group. Therefore, we focus
396 * purely on defense.
398 * There is a tradeoff - down to how many liberties we need to
399 * be to start looking? nlib_count=3 will work for the left black
400 * group (2lib-solver will suggest connecting the false eye), but
401 * not for top black group (it is too late to start playing 3-3
402 * capturing race). Also, we cannot prevent stupidly taking an
403 * outside liberty ourselves; the higher nlib_count, the higher
404 * the chance we withstand this.
406 * However, higher nlib_count means that we will waste more time
407 * checking non-urgent or alive groups, and we will play silly
408 * or wasted moves around alive groups. */
410 group_t group2 = 0;
411 foreach_8neighbor(b, m->coord) {
412 group_t g = group_at(b, c);
413 if (!g || group2 == g || board_at(b, c) != color)
414 continue;
415 if (board_group_info(b, g).libs < 3 || board_group_info(b, g).libs > pp->nlib_count)
416 continue;
417 group_nlib_defense_check(b, g, color, q, 1<<MQ_LNLIB);
418 group2 = g; // prevent trivial repeated checks
419 } foreach_8neighbor_end;
421 if (PLDEBUGL(5))
422 mq_print(q, b, "Local nlib");
425 static coord_t
426 nakade_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play)
428 coord_t empty = pass;
429 foreach_neighbor(b, m->coord, {
430 if (board_at(b, c) != S_NONE)
431 continue;
432 if (is_pass(empty)) {
433 empty = c;
434 continue;
436 if (!coord_is_8adjecent(c, empty, b)) {
437 /* Seems like impossible nakade
438 * shape! */
439 return pass;
442 assert(!is_pass(empty));
444 coord_t nakade = nakade_point(b, empty, stone_other(to_play));
445 if (PLDEBUGL(5) && !is_pass(nakade))
446 fprintf(stderr, "Nakade: %s\n", coord2sstr(nakade, b));
447 return nakade;
450 static void
451 eye_fix_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play, struct move_queue *q)
453 /* The opponent could have filled an approach liberty for
454 * falsifying an eye like these:
456 * # # # # # # X . X X O O last_move == 1
457 * X X 2 O 1 O X X 2 O 1 O => suggest 2
458 * X . X X O . X . X X O .
459 * X X O O . . X X O O . O
461 * This case seems pretty common (e.g. Zen-Ishida game). */
463 /* Iterator for walking coordinates in a clockwise fashion
464 * (nei8 jumps "over" the middle point, inst. of "around). */
465 int size = board_size(b);
466 int nei8_clockwise[10] = { -size-1, 1, 1, size, size, -1, -1, -size, -size, 1 };
468 /* This is sort of like a cross between foreach_diag_neighbor
469 * and foreach_8neighbor. */
470 coord_t c = m->coord;
471 for (int dni = 0; dni < 8; dni += 2) {
472 // one diagonal neighbor
473 coord_t c0 = c + nei8_clockwise[dni];
474 // adjecent staight neighbor
475 coord_t c1 = c0 + nei8_clockwise[dni + 1];
476 // and adjecent another diagonal neighbor
477 coord_t c2 = c1 + nei8_clockwise[dni + 2];
479 /* The last move must have a pair of unfriendly diagonal
480 * neighbors separated by a friendly stone. */
481 //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));
482 if ((board_at(b, c0) == to_play || board_at(b, c0) == S_OFFBOARD)
483 && board_at(b, c1) == m->color
484 && (board_at(b, c2) == to_play || board_at(b, c2) == S_OFFBOARD)
485 /* The friendly stone then must have an empty neighbor... */
486 /* XXX: This works only for single stone, not e.g. for two
487 * stones in a row */
488 && immediate_liberty_count(b, c1) > 0) {
489 foreach_neighbor(b, c1, {
490 if (c == m->coord || board_at(b, c) != S_NONE)
491 continue;
492 /* ...and the neighbor must potentially falsify
493 * an eye. */
494 coord_t falsifying = c;
495 foreach_diag_neighbor(b, falsifying) {
496 if (board_at(b, c) != S_NONE)
497 continue;
498 if (!board_is_eyelike(b, c, to_play))
499 continue;
500 /* We don't care about eyes that already
501 * _are_ false (board_is_false_eyelike())
502 * but that can become false. Therefore,
503 * either ==1 diagonal neighbor is
504 * opponent's (except in atari) or ==2
505 * are board edge. */
506 coord_t falsified = c;
507 int color_diag_libs[S_MAX] = {0};
508 foreach_diag_neighbor(b, falsified) {
509 if (board_at(b, c) == m->color && board_group_info(b, group_at(b, c)).libs == 1) {
510 /* Suggest capturing a falsifying stone in atari. */
511 mq_add(q, board_group_info(b, group_at(b, c)).lib[0], 0);
512 } else {
513 color_diag_libs[board_at(b, c)]++;
515 } foreach_diag_neighbor_end;
516 if (color_diag_libs[m->color] == 1 || (color_diag_libs[m->color] == 0 && color_diag_libs[S_OFFBOARD] == 2)) {
517 /* That's it. Fill the falsifying
518 * liberty before it's too late! */
519 mq_add(q, falsifying, 0);
521 } foreach_diag_neighbor_end;
525 c = c1;
528 if (q->moves > 0 && PLDEBUGL(5))
529 mq_print(q, b, "Eye fix");
532 static coord_t
533 fillboard_check(struct playout_policy *p, struct board *b)
535 struct moggy_policy *pp = p->data;
536 unsigned int fbtries = b->flen / 8;
537 if (pp->fillboardtries < fbtries)
538 fbtries = pp->fillboardtries;
540 for (unsigned int i = 0; i < fbtries; i++) {
541 coord_t coord = b->f[fast_random(b->flen)];
542 if (immediate_liberty_count(b, coord) != 4)
543 continue;
544 foreach_diag_neighbor(b, coord) {
545 if (board_at(b, c) != S_NONE)
546 goto next_try;
547 } foreach_diag_neighbor_end;
548 return coord;
549 next_try:
552 return pass;
555 static coord_t
556 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
558 struct moggy_policy *pp = p->data;
560 if (PLDEBUGL(5))
561 board_print(b, stderr);
563 /* Ko fight check */
564 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
565 && b->moves - b->last_ko_age < pp->koage
566 && pp->korate > fast_random(100)) {
567 if (board_is_valid_play(b, to_play, b->last_ko.coord)
568 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
569 return b->last_ko.coord;
572 /* Local checks */
573 if (!is_pass(b->last_move.coord)) {
574 /* Local group in atari? */
575 if (pp->lcapturerate > fast_random(100)) {
576 struct move_queue q; q.moves = 0;
577 local_atari_check(p, b, &b->last_move, &q);
578 if (q.moves > 0)
579 return mq_pick(&q);
582 /* Local group trying to escape ladder? */
583 if (pp->ladderrate > fast_random(100)) {
584 struct move_queue q; q.moves = 0;
585 local_ladder_check(p, b, &b->last_move, &q);
586 if (q.moves > 0)
587 return mq_pick(&q);
590 /* Local group can be PUT in atari? */
591 if (pp->atarirate > fast_random(100)) {
592 struct move_queue q; q.moves = 0;
593 local_2lib_check(p, b, &b->last_move, &q);
594 if (q.moves > 0)
595 return mq_pick(&q);
598 /* Local group reduced some of our groups to 3 libs? */
599 if (pp->nlibrate > fast_random(100)) {
600 struct move_queue q; q.moves = 0;
601 local_nlib_check(p, b, &b->last_move, &q);
602 if (q.moves > 0)
603 return mq_pick(&q);
606 /* Some other semeai-ish shape checks */
607 if (pp->eyefixrate > fast_random(100)) {
608 struct move_queue q; q.moves = 0;
609 eye_fix_check(p, b, &b->last_move, to_play, &q);
610 if (q.moves > 0)
611 return mq_pick(&q);
614 /* Nakade check */
615 if (pp->nakaderate > fast_random(100)
616 && immediate_liberty_count(b, b->last_move.coord) > 0) {
617 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
618 if (!is_pass(nakade))
619 return nakade;
622 /* Check for patterns we know */
623 if (pp->patternrate > fast_random(100)) {
624 struct move_queue q; q.moves = 0;
625 fixp_t gammas[MQL];
626 apply_pattern(p, b, &b->last_move,
627 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
628 &q, gammas);
629 if (q.moves > 0)
630 return mq_gamma_pick(&q, gammas);
634 /* Global checks */
636 /* Any groups in atari? */
637 if (pp->capturerate > fast_random(100)) {
638 struct move_queue q; q.moves = 0;
639 global_atari_check(p, b, to_play, &q);
640 if (q.moves > 0)
641 return mq_pick(&q);
644 /* Joseki moves? */
645 if (pp->josekirate > fast_random(100)) {
646 struct move_queue q; q.moves = 0;
647 joseki_check(p, b, to_play, &q);
648 if (q.moves > 0)
649 return mq_pick(&q);
652 /* Fill board */
653 if (pp->fillboardtries > 0) {
654 coord_t c = fillboard_check(p, b);
655 if (!is_pass(c))
656 return c;
659 return pass;
662 /* Pick a move from queue q, giving different likelihoods to moves
663 * based on their tags. */
664 static coord_t
665 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
667 struct moggy_policy *pp = p->data;
669 /* First, merge all entries for a move. */
670 /* We use a naive O(N^2) since the average length of the queue
671 * is about 1.4. */
672 for (unsigned int i = 0; i < q->moves; i++) {
673 for (unsigned int j = i + 1; j < q->moves; j++) {
674 if (q->move[i] != q->move[j])
675 continue;
676 q->tag[i] |= q->tag[j];
677 q->moves--;
678 q->tag[j] = q->tag[q->moves];
679 q->move[j] = q->move[q->moves];
683 /* Now, construct a probdist. */
684 fixp_t total = 0;
685 fixp_t pd[q->moves];
686 for (unsigned int i = 0; i < q->moves; i++) {
687 double val = 1.0;
688 assert(q->tag[i] != 0);
689 for (int j = 0; j < MQ_MAX; j++)
690 if (q->tag[i] & (1<<j)) {
691 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
692 val *= pp->mq_prob[j];
694 pd[i] = double_to_fixp(val);
695 total += pd[i];
697 total += double_to_fixp(pp->tenuki_prob);
699 /* Finally, pick a move! */
700 fixp_t stab = fast_irandom(total);
701 if (PLDEBUGL(5)) {
702 fprintf(stderr, "Pick (total %.3f stab %.3f): ", fixp_to_double(total), fixp_to_double(stab));
703 for (unsigned int i = 0; i < q->moves; i++) {
704 fprintf(stderr, "%s(%x:%.3f) ", coord2sstr(q->move[i], b), q->tag[i], fixp_to_double(pd[i]));
706 fprintf(stderr, "\n");
708 for (unsigned int i = 0; i < q->moves; i++) {
709 //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));
710 if (stab < pd[i])
711 return q->move[i];
712 stab -= pd[i];
715 /* Tenuki. */
716 assert(stab < double_to_fixp(pp->tenuki_prob));
717 return pass;
720 static coord_t
721 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
723 struct moggy_policy *pp = p->data;
724 struct move_queue q; q.moves = 0;
726 if (PLDEBUGL(5))
727 board_print(b, stderr);
729 /* Ko fight check */
730 if (pp->korate > 0 && !is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
731 && b->moves - b->last_ko_age < pp->koage) {
732 if (board_is_valid_play(b, to_play, b->last_ko.coord)
733 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
734 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
737 /* Local checks */
738 if (!is_pass(b->last_move.coord)) {
739 /* Local group in atari? */
740 if (pp->lcapturerate > 0)
741 local_atari_check(p, b, &b->last_move, &q);
743 /* Local group trying to escape ladder? */
744 if (pp->ladderrate > 0)
745 local_ladder_check(p, b, &b->last_move, &q);
747 /* Local group can be PUT in atari? */
748 if (pp->atarirate > 0)
749 local_2lib_check(p, b, &b->last_move, &q);
751 /* Local group reduced some of our groups to 3 libs? */
752 if (pp->nlibrate > 0)
753 local_nlib_check(p, b, &b->last_move, &q);
755 /* Some other semeai-ish shape checks */
756 if (pp->eyefixrate > 0)
757 eye_fix_check(p, b, &b->last_move, to_play, &q);
759 /* Nakade check */
760 if (pp->nakaderate > 0 && immediate_liberty_count(b, b->last_move.coord) > 0) {
761 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
762 if (!is_pass(nakade))
763 mq_add(&q, nakade, 1<<MQ_NAKADE);
766 /* Check for patterns we know */
767 if (pp->patternrate > 0) {
768 fixp_t gammas[MQL];
769 apply_pattern(p, b, &b->last_move,
770 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
771 &q, gammas);
772 /* FIXME: Use the gammas. */
776 /* Global checks */
778 /* Any groups in atari? */
779 if (pp->capturerate > 0)
780 global_atari_check(p, b, to_play, &q);
782 /* Joseki moves? */
783 if (pp->josekirate > 0)
784 joseki_check(p, b, to_play, &q);
786 #if 0
787 /* Average length of the queue is 1.4 move. */
788 printf("MQL %d ", q.moves);
789 for (unsigned int i = 0; i < q.moves; i++)
790 printf("%s ", coord2sstr(q.move[i], b));
791 printf("\n");
792 #endif
794 if (q.moves > 0)
795 return mq_tagged_choose(p, b, to_play, &q);
797 /* Fill board */
798 if (pp->fillboardtries > 0) {
799 coord_t c = fillboard_check(p, b);
800 if (!is_pass(c))
801 return c;
804 return pass;
808 static void
809 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
811 struct moggy_policy *pp = p->data;
812 struct board *b = map->b;
813 struct move_queue q; q.moves = 0;
815 if (board_group_info(b, g).libs > pp->nlib_count)
816 return;
818 if (PLDEBUGL(5)) {
819 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
820 board_print(b, stderr);
823 if (board_group_info(b, g).libs > 2) {
824 if (!pp->nlibrate)
825 return;
826 if (board_at(b, g) != map->to_play)
827 return; // we do only defense
828 group_nlib_defense_check(b, g, map->to_play, &q, 0);
829 while (q.moves--) {
830 coord_t coord = q.move[q.moves];
831 if (PLDEBUGL(5))
832 fprintf(stderr, "1.0: nlib %s\n", coord2sstr(coord, b));
833 int assess = games / 2;
834 add_prior_value(map, coord, 1, assess);
836 return;
839 if (board_group_info(b, g).libs == 2) {
840 if (pp->ladderrate) {
841 /* Make sure to play the correct liberty in case
842 * this is a group that can be caught in a ladder. */
843 bool ladderable = false;
844 for (int i = 0; i < 2; i++) {
845 coord_t chase = board_group_info(b, g).lib[i];
846 coord_t escape = board_group_info(b, g).lib[1 - i];
847 if (wouldbe_ladder(b, g, escape, chase, board_at(b, g))) {
848 add_prior_value(map, chase, 1, games);
849 ladderable = true;
852 if (ladderable)
853 return; // do not suggest the other lib at all
856 if (!pp->atarirate)
857 return;
858 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
859 while (q.moves--) {
860 coord_t coord = q.move[q.moves];
861 if (PLDEBUGL(5))
862 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
863 int assess = games / 2;
864 add_prior_value(map, coord, 1, assess);
866 return;
869 /* This group, sir, is in atari! */
871 coord_t ladder = pass;
872 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, true, 0);
873 while (q.moves--) {
874 coord_t coord = q.move[q.moves];
876 /* _Never_ play here if this move plays out
877 * a caught ladder. */
878 if (coord == ladder && !board_playing_ko_threat(b)) {
879 /* Note that the opposite is not guarded against;
880 * we do not advise against capturing a laddered
881 * group (but we don't encourage it either). Such
882 * a move can simplify tactical situations if we
883 * can afford it. */
884 if (map->to_play != board_at(b, g))
885 continue;
886 /* FIXME: We give the malus even if this move
887 * captures another group. */
888 if (PLDEBUGL(5))
889 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
890 add_prior_value(map, coord, 0, games);
891 continue;
894 if (!pp->capturerate && !pp->lcapturerate)
895 continue;
897 int assess = games * 2;
898 if (pp->cap_stone_denom > 0) {
899 int stones = group_stone_count(b, g, pp->cap_stone_max) - (pp->cap_stone_min-1);
900 assess += (stones > 0 ? stones : 0) * games * 100 / pp->cap_stone_denom;
902 if (PLDEBUGL(5))
903 fprintf(stderr, "1.0 (%d): atari %s\n", assess, coord2sstr(coord, b));
904 add_prior_value(map, coord, 1, assess);
908 static void
909 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
911 struct moggy_policy *pp = p->data;
912 struct board *b = map->b;
914 if (PLDEBUGL(5)) {
915 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
916 board_print(b, stderr);
919 /* Is this move a self-atari? */
920 if (pp->selfatarirate) {
921 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
922 if (PLDEBUGL(5))
923 fprintf(stderr, "0.0: self-atari\n");
924 add_prior_value(map, coord, 0, games);
925 if (!pp->selfatari_other)
926 return;
927 /* If we can play on the other liberty of the
928 * endangered group, do! */
929 coord = selfatari_cousin(b, map->to_play, coord, NULL);
930 if (is_pass(coord))
931 return;
932 if (PLDEBUGL(5))
933 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
934 add_prior_value(map, coord, 1.0, games);
935 return;
939 /* Pattern check */
940 if (pp->patternrate) {
941 // XXX: Use gamma value?
942 struct move m = { .color = map->to_play, .coord = coord };
943 if (test_pattern3_here(p, b, &m, true, NULL)) {
944 if (PLDEBUGL(5))
945 fprintf(stderr, "1.0: pattern\n");
946 add_prior_value(map, coord, 1, games);
950 return;
953 static void
954 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
956 struct moggy_policy *pp = p->data;
958 /* First, go through all endangered groups. */
959 for (group_t g = 1; g < board_size2(map->b); g++)
960 if (group_at(map->b, g) == g)
961 playout_moggy_assess_group(p, map, g, games);
963 /* Then, assess individual moves. */
964 if (!pp->patternrate && !pp->selfatarirate)
965 return;
966 foreach_free_point(map->b) {
967 if (map->consider[c])
968 playout_moggy_assess_one(p, map, c, games);
969 } foreach_free_point_end;
972 static bool
973 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
975 struct moggy_policy *pp = p->data;
977 /* The idea is simple for now - never allow self-atari moves.
978 * They suck in general, but this also permits us to actually
979 * handle seki in the playout stage. */
981 if (fast_random(100) >= pp->selfatarirate) {
982 if (PLDEBUGL(5))
983 fprintf(stderr, "skipping sar test\n");
984 goto sar_skip;
986 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
987 if (selfatari) {
988 if (PLDEBUGL(5))
989 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
990 stone2str(m->color), coord2sstr(m->coord, b));
991 if (pp->selfatari_other) {
992 /* Ok, try the other liberty of the atari'd group. */
993 coord_t c = selfatari_cousin(b, m->color, m->coord, NULL);
994 if (is_pass(c)) return false;
995 if (PLDEBUGL(5))
996 fprintf(stderr, "___ Redirecting to other lib %s\n",
997 coord2sstr(c, b));
998 m->coord = c;
999 return true;
1001 return false;
1003 sar_skip:
1005 /* Check if we don't seem to be filling our eye. This should
1006 * happen only for false eyes, but some of them are in fact
1007 * real eyes with diagonal filled by a dead stone. Prefer
1008 * to counter-capture in that case. */
1009 if (fast_random(100) >= pp->eyefillrate) {
1010 if (PLDEBUGL(5))
1011 fprintf(stderr, "skipping eyefill test\n");
1012 goto eyefill_skip;
1014 bool eyefill = board_is_eyelike(b, m->coord, m->color);
1015 if (eyefill) {
1016 foreach_diag_neighbor(b, m->coord) {
1017 if (board_at(b, c) != stone_other(m->color))
1018 continue;
1019 switch (board_group_info(b, group_at(b, c)).libs) {
1020 case 1: /* Capture! */
1021 c = board_group_info(b, group_at(b, c)).lib[0];
1022 if (PLDEBUGL(5))
1023 fprintf(stderr, "___ Redirecting to capture %s\n",
1024 coord2sstr(c, b));
1025 m->coord = c;
1026 return true;
1027 case 2: /* Try to switch to some 2-lib neighbor. */
1028 for (int i = 0; i < 2; i++) {
1029 coord_t l = board_group_info(b, group_at(b, c)).lib[i];
1030 if (board_is_one_point_eye(b, l, board_at(b, c)))
1031 continue;
1032 if (is_bad_selfatari(b, m->color, l))
1033 continue;
1034 m->coord = l;
1035 return true;
1037 break;
1039 } foreach_diag_neighbor_end;
1042 eyefill_skip:
1043 return true;
1046 struct playout_policy *
1047 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
1049 struct playout_policy *p = calloc2(1, sizeof(*p));
1050 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
1051 p->data = pp;
1052 p->choose = playout_moggy_seqchoose;
1053 p->assess = playout_moggy_assess;
1054 p->permit = playout_moggy_permit;
1055 /* no p->done: calling engine owns jdict and should call joseki_done() */
1057 pp->jdict = jdict;
1059 /* These settings are tuned for 19x19 play with several threads
1060 * on reasonable time limits (i.e., rather large number of playouts).
1061 * XXX: no 9x9 tuning has been done recently. */
1062 int rate = board_large(b) ? 80 : 90;
1064 pp->patternrate = pp->eyefixrate = 100;
1065 pp->lcapturerate = 90;
1066 pp->atarirate = pp->josekirate = -1U;
1067 pp->nakaderate = 60;
1068 pp->korate = 40; pp->koage = 4;
1069 pp->alwaysccaprate = 40;
1070 pp->eyefillrate = 60;
1071 pp->nlibrate = 25;
1073 /* selfatarirate is slightly special, since to avoid playing some
1074 * silly move that stays on the board, it needs to block it many
1075 * times during a simulation - we'd like that to happen in most
1076 * simulations, so we try to use a very high selfatarirate.
1077 * XXX: Perhaps it would be better to permanently ban moves in
1078 * the current simulation after testing them once.
1079 * XXX: We would expect the above to be the case, but since some
1080 * unclear point, selfatari 95 -> 60 gives a +~50Elo boost against
1081 * GNUGo. This might be indicative of some bug, FIXME bisect? */
1082 pp->selfatarirate = 60;
1083 pp->selfatari_other = true;
1085 pp->pattern2 = true;
1087 pp->cap_stone_min = 2;
1088 pp->cap_stone_max = 15;
1089 pp->cap_stone_denom = 200;
1091 pp->atari_def_no_hopeless = !board_large(b);
1092 pp->atari_miaisafe = true;
1093 pp->nlib_count = 4;
1095 /* C is stupid. */
1096 double mq_prob_default[MQ_MAX] = {
1097 [MQ_KO] = 6.0,
1098 [MQ_NAKADE] = 5.5,
1099 [MQ_LATARI] = 5.0,
1100 [MQ_L2LIB] = 4.0,
1101 [MQ_LNLIB] = 3.5,
1102 [MQ_PAT3] = 3.0,
1103 [MQ_GATARI] = 2.0,
1104 [MQ_JOSEKI] = 1.0,
1106 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
1108 /* Default 3x3 pattern gammas tuned on 15x15 with 500s/game on
1109 * i7-3770 single thread using 40000 CLOP games. */
1110 double pat3_gammas_default[PAT3_N] = {
1111 0.52, 0.53, 0.32, 0.22, 0.37, 0.28, 0.21, 0.19, 0.82,
1112 0.12, 0.20, 0.11, 0.16, 0.57, 0.44
1114 memcpy(pp->pat3_gammas, pat3_gammas_default, sizeof(pp->pat3_gammas));
1116 if (arg) {
1117 char *optspec, *next = arg;
1118 while (*next) {
1119 optspec = next;
1120 next += strcspn(next, ":");
1121 if (*next) { *next++ = 0; } else { *next = 0; }
1123 char *optname = optspec;
1124 char *optval = strchr(optspec, '=');
1125 if (optval) *optval++ = 0;
1127 if (!strcasecmp(optname, "debug") && optval) {
1128 p->debug_level = atoi(optval);
1129 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
1130 pp->lcapturerate = atoi(optval);
1131 } else if (!strcasecmp(optname, "ladderrate") && optval) {
1132 /* Note that ladderrate is considered obsolete;
1133 * it is ineffective and superseded by the
1134 * prune_ladders prior. */
1135 pp->ladderrate = atoi(optval);
1136 } else if (!strcasecmp(optname, "atarirate") && optval) {
1137 pp->atarirate = atoi(optval);
1138 } else if (!strcasecmp(optname, "nlibrate") && optval) {
1139 pp->nlibrate = atoi(optval);
1140 } else if (!strcasecmp(optname, "capturerate") && optval) {
1141 pp->capturerate = atoi(optval);
1142 } else if (!strcasecmp(optname, "patternrate") && optval) {
1143 pp->patternrate = atoi(optval);
1144 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
1145 pp->selfatarirate = atoi(optval);
1146 } else if (!strcasecmp(optname, "eyefillrate") && optval) {
1147 pp->eyefillrate = atoi(optval);
1148 } else if (!strcasecmp(optname, "korate") && optval) {
1149 pp->korate = atoi(optval);
1150 } else if (!strcasecmp(optname, "josekirate") && optval) {
1151 pp->josekirate = atoi(optval);
1152 } else if (!strcasecmp(optname, "nakaderate") && optval) {
1153 pp->nakaderate = atoi(optval);
1154 } else if (!strcasecmp(optname, "eyefixrate") && optval) {
1155 pp->eyefixrate = atoi(optval);
1156 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
1157 pp->alwaysccaprate = atoi(optval);
1158 } else if (!strcasecmp(optname, "rate") && optval) {
1159 rate = atoi(optval);
1160 } else if (!strcasecmp(optname, "fillboardtries")) {
1161 pp->fillboardtries = atoi(optval);
1162 } else if (!strcasecmp(optname, "koage") && optval) {
1163 pp->koage = atoi(optval);
1164 } else if (!strcasecmp(optname, "pattern2")) {
1165 pp->pattern2 = optval && *optval == '0' ? false : true;
1166 } else if (!strcasecmp(optname, "selfatari_other")) {
1167 pp->selfatari_other = optval && *optval == '0' ? false : true;
1168 } else if (!strcasecmp(optname, "capcheckall")) {
1169 pp->capcheckall = optval && *optval == '0' ? false : true;
1170 } else if (!strcasecmp(optname, "cap_stone_min") && optval) {
1171 pp->cap_stone_min = atoi(optval);
1172 } else if (!strcasecmp(optname, "cap_stone_max") && optval) {
1173 pp->cap_stone_max = atoi(optval);
1174 } else if (!strcasecmp(optname, "cap_stone_denom") && optval) {
1175 pp->cap_stone_denom = atoi(optval);
1176 } else if (!strcasecmp(optname, "atari_miaisafe")) {
1177 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
1178 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
1179 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
1180 } else if (!strcasecmp(optname, "nlib_count") && optval) {
1181 pp->nlib_count = atoi(optval);
1182 } else if (!strcasecmp(optname, "middle_ladder")) {
1183 pp->middle_ladder = optval && *optval == '0' ? false : true;
1184 } else if (!strcasecmp(optname, "fullchoose")) {
1185 pp->fullchoose = true;
1186 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
1187 } else if (!strcasecmp(optname, "mqprob") && optval) {
1188 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
1189 for (int i = 0; *optval && i < MQ_MAX; i++) {
1190 pp->mq_prob[i] = atof(optval);
1191 optval += strcspn(optval, "%");
1192 if (*optval) optval++;
1194 } else if (!strcasecmp(optname, "pat3gammas") && optval) {
1195 /* PAT3_N %-separated floating point values */
1196 for (int i = 0; *optval && i < PAT3_N; i++) {
1197 pp->pat3_gammas[i] = atof(optval);
1198 optval += strcspn(optval, "%");
1199 if (*optval) optval++;
1201 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
1202 pp->tenuki_prob = atof(optval);
1203 } else {
1204 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
1205 exit(1);
1209 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
1210 if (pp->atarirate == -1U) pp->atarirate = rate;
1211 if (pp->nlibrate == -1U) pp->nlibrate = rate;
1212 if (pp->capturerate == -1U) pp->capturerate = rate;
1213 if (pp->patternrate == -1U) pp->patternrate = rate;
1214 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
1215 if (pp->eyefillrate == -1U) pp->eyefillrate = rate;
1216 if (pp->korate == -1U) pp->korate = rate;
1217 if (pp->josekirate == -1U) pp->josekirate = rate;
1218 if (pp->ladderrate == -1U) pp->ladderrate = rate;
1219 if (pp->nakaderate == -1U) pp->nakaderate = rate;
1220 if (pp->eyefixrate == -1U) pp->eyefixrate = rate;
1221 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
1223 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
1225 return p;