Merge pull request #47 from lemonsqueeze/board_undo
[pachi.git] / playout / moggy.c
blob3cf77e35a2e0fd6ad6122c1d9de98370c4e166d0
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, board_at(b, group_base(atari_neighbor)),
196 atari_neighbor, m->color, NULL, 0))
197 return false;
198 //fprintf(stderr, "%s: %d (%.3f)\n", coord2sstr(m->coord, b), (int) pi, pp->pat3_gammas[(int) pi]);
199 if (gamma)
200 *gamma = pp->pat3_gammas[(int) pi];
201 return true;
204 static void
205 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q, fixp_t *gammas)
207 struct moggy_policy *pp = p->data;
208 struct move m2 = { .coord = c, .color = color };
209 double gamma;
210 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2, pp->middle_ladder, &gamma)) {
211 mq_gamma_add(q, gammas, c, gamma, 1<<MQ_PAT3);
215 /* Check if we match any pattern around given move (with the other color to play). */
216 static void
217 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q, fixp_t *gammas)
219 /* Suicides do not make any patterns and confuse us. */
220 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
221 return;
223 foreach_8neighbor(b, m->coord) {
224 apply_pattern_here(p, b, c, stone_other(m->color), q, gammas);
225 } foreach_8neighbor_end;
227 if (mm) { /* Second move for pattern searching */
228 foreach_8neighbor(b, mm->coord) {
229 if (coord_is_8adjecent(m->coord, c, b))
230 continue;
231 apply_pattern_here(p, b, c, stone_other(m->color), q, gammas);
232 } foreach_8neighbor_end;
235 if (PLDEBUGL(5))
236 mq_gamma_print(q, gammas, b, "Pattern");
240 static void
241 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
243 struct moggy_policy *pp = p->data;
244 if (!pp->jdict)
245 return;
247 for (int i = 0; i < 4; i++) {
248 hash_t h = b->qhash[i] & joseki_hash_mask;
249 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
250 if (!cc) continue;
251 for (; !is_pass(*cc); cc++) {
252 if (coord_quadrant(*cc, b) != i)
253 continue;
254 if (!board_is_valid_play(b, to_play, *cc))
255 continue;
256 mq_add(q, *cc, 1<<MQ_JOSEKI);
260 if (q->moves > 0 && PLDEBUGL(5))
261 mq_print(q, b, "Joseki");
264 static void
265 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
267 if (b->clen == 0)
268 return;
270 struct moggy_policy *pp = p->data;
271 if (pp->capcheckall) {
272 for (int g = 0; g < b->clen; g++)
273 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
274 if (PLDEBUGL(5))
275 mq_print(q, b, "Global atari");
276 if (pp->fullchoose)
277 return;
280 int g_base = fast_random(b->clen);
281 for (int g = g_base; g < b->clen; g++) {
282 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
283 if (q->moves > 0) {
284 /* XXX: Try carrying on. */
285 if (PLDEBUGL(5))
286 mq_print(q, b, "Global atari");
287 if (pp->fullchoose)
288 return;
291 for (int g = 0; g < g_base; g++) {
292 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, pp->middle_ladder, 1<<MQ_GATARI);
293 if (q->moves > 0) {
294 /* XXX: Try carrying on. */
295 if (PLDEBUGL(5))
296 mq_print(q, b, "Global atari");
297 if (pp->fullchoose)
298 return;
303 static void
304 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
306 struct moggy_policy *pp = p->data;
308 /* Did the opponent play a self-atari? */
309 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
310 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, pp->middle_ladder, 1<<MQ_LATARI);
313 foreach_neighbor(b, m->coord, {
314 group_t g = group_at(b, c);
315 if (!g || board_group_info(b, g).libs != 1)
316 continue;
317 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, pp->middle_ladder, 1<<MQ_LATARI);
320 if (PLDEBUGL(5))
321 mq_print(q, b, "Local atari");
325 static void
326 local_ladder_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
328 group_t group = group_at(b, m->coord);
330 if (board_group_info(b, group).libs != 2)
331 return;
333 for (int i = 0; i < 2; i++) {
334 coord_t chase = board_group_info(b, group).lib[i];
335 coord_t escape = board_group_info(b, group).lib[1 - i];
336 if (wouldbe_ladder(b, group, escape, chase, board_at(b, group)))
337 mq_add(q, chase, 1<<MQ_LADDER);
340 if (q->moves > 0 && PLDEBUGL(5))
341 mq_print(q, b, "Ladder");
345 static void
346 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
348 struct moggy_policy *pp = p->data;
349 group_t group = group_at(b, m->coord), group2 = 0;
351 /* Does the opponent have just two liberties? */
352 if (board_group_info(b, group).libs == 2) {
353 group_2lib_check(b, group, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
354 #if 0
355 /* We always prefer to take off an enemy chain liberty
356 * before pulling out ourselves. */
357 /* XXX: We aren't guaranteed to return to that group
358 * later. */
359 if (q->moves)
360 return q->move[fast_random(q->moves)];
361 #endif
364 /* Then he took a third liberty from neighboring chain? */
365 foreach_neighbor(b, m->coord, {
366 group_t g = group_at(b, c);
367 if (!g || g == group || g == group2 || board_group_info(b, g).libs != 2)
368 continue;
369 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
370 group2 = g; // prevent trivial repeated checks
373 if (PLDEBUGL(5))
374 mq_print(q, b, "Local 2lib");
377 static void
378 local_nlib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
380 struct moggy_policy *pp = p->data;
381 enum stone color = stone_other(m->color);
383 /* Attacking N-liberty groups in general is probably
384 * not feasible. What we are primarily concerned about is
385 * counter-attacking groups that have two physical liberties,
386 * but three effective liberties:
388 * . O . . . . #
389 * O O X X X X #
390 * . X O O X . #
391 * . X O . O X #
392 * . X O O . X #
393 * # # # # # # #
395 * The time for this to come is when the opponent took a liberty
396 * of ours, making a few-liberty group. Therefore, we focus
397 * purely on defense.
399 * There is a tradeoff - down to how many liberties we need to
400 * be to start looking? nlib_count=3 will work for the left black
401 * group (2lib-solver will suggest connecting the false eye), but
402 * not for top black group (it is too late to start playing 3-3
403 * capturing race). Also, we cannot prevent stupidly taking an
404 * outside liberty ourselves; the higher nlib_count, the higher
405 * the chance we withstand this.
407 * However, higher nlib_count means that we will waste more time
408 * checking non-urgent or alive groups, and we will play silly
409 * or wasted moves around alive groups. */
411 group_t group2 = 0;
412 foreach_8neighbor(b, m->coord) {
413 group_t g = group_at(b, c);
414 if (!g || group2 == g || board_at(b, c) != color)
415 continue;
416 if (board_group_info(b, g).libs < 3 || board_group_info(b, g).libs > pp->nlib_count)
417 continue;
418 group_nlib_defense_check(b, g, color, q, 1<<MQ_LNLIB);
419 group2 = g; // prevent trivial repeated checks
420 } foreach_8neighbor_end;
422 if (PLDEBUGL(5))
423 mq_print(q, b, "Local nlib");
426 static coord_t
427 nakade_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play)
429 coord_t empty = pass;
430 foreach_neighbor(b, m->coord, {
431 if (board_at(b, c) != S_NONE)
432 continue;
433 if (is_pass(empty)) {
434 empty = c;
435 continue;
437 if (!coord_is_8adjecent(c, empty, b)) {
438 /* Seems like impossible nakade
439 * shape! */
440 return pass;
443 assert(!is_pass(empty));
445 coord_t nakade = nakade_point(b, empty, stone_other(to_play));
446 if (PLDEBUGL(5) && !is_pass(nakade))
447 fprintf(stderr, "Nakade: %s\n", coord2sstr(nakade, b));
448 return nakade;
451 static void
452 eye_fix_check(struct playout_policy *p, struct board *b, struct move *m, enum stone to_play, struct move_queue *q)
454 /* The opponent could have filled an approach liberty for
455 * falsifying an eye like these:
457 * # # # # # # X . X X O O last_move == 1
458 * X X 2 O 1 O X X 2 O 1 O => suggest 2
459 * X . X X O . X . X X O .
460 * X X O O . . X X O O . O
462 * This case seems pretty common (e.g. Zen-Ishida game). */
464 /* Iterator for walking coordinates in a clockwise fashion
465 * (nei8 jumps "over" the middle point, inst. of "around). */
466 int size = board_size(b);
467 int nei8_clockwise[10] = { -size-1, 1, 1, size, size, -1, -1, -size, -size, 1 };
469 /* This is sort of like a cross between foreach_diag_neighbor
470 * and foreach_8neighbor. */
471 coord_t c = m->coord;
472 for (int dni = 0; dni < 8; dni += 2) {
473 // one diagonal neighbor
474 coord_t c0 = c + nei8_clockwise[dni];
475 // adjecent staight neighbor
476 coord_t c1 = c0 + nei8_clockwise[dni + 1];
477 // and adjecent another diagonal neighbor
478 coord_t c2 = c1 + nei8_clockwise[dni + 2];
480 /* The last move must have a pair of unfriendly diagonal
481 * neighbors separated by a friendly stone. */
482 //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));
483 if ((board_at(b, c0) == to_play || board_at(b, c0) == S_OFFBOARD)
484 && board_at(b, c1) == m->color
485 && (board_at(b, c2) == to_play || board_at(b, c2) == S_OFFBOARD)
486 /* The friendly stone then must have an empty neighbor... */
487 /* XXX: This works only for single stone, not e.g. for two
488 * stones in a row */
489 && immediate_liberty_count(b, c1) > 0) {
490 foreach_neighbor(b, c1, {
491 if (c == m->coord || board_at(b, c) != S_NONE)
492 continue;
493 /* ...and the neighbor must potentially falsify
494 * an eye. */
495 coord_t falsifying = c;
496 foreach_diag_neighbor(b, falsifying) {
497 if (board_at(b, c) != S_NONE)
498 continue;
499 if (!board_is_eyelike(b, c, to_play))
500 continue;
501 /* We don't care about eyes that already
502 * _are_ false (board_is_false_eyelike())
503 * but that can become false. Therefore,
504 * either ==1 diagonal neighbor is
505 * opponent's (except in atari) or ==2
506 * are board edge. */
507 coord_t falsified = c;
508 int color_diag_libs[S_MAX] = {0};
509 foreach_diag_neighbor(b, falsified) {
510 if (board_at(b, c) == m->color && board_group_info(b, group_at(b, c)).libs == 1) {
511 /* Suggest capturing a falsifying stone in atari. */
512 mq_add(q, board_group_info(b, group_at(b, c)).lib[0], 0);
513 } else {
514 color_diag_libs[board_at(b, c)]++;
516 } foreach_diag_neighbor_end;
517 if (color_diag_libs[m->color] == 1 || (color_diag_libs[m->color] == 0 && color_diag_libs[S_OFFBOARD] == 2)) {
518 /* That's it. Fill the falsifying
519 * liberty before it's too late! */
520 mq_add(q, falsifying, 0);
522 } foreach_diag_neighbor_end;
526 c = c1;
529 if (q->moves > 0 && PLDEBUGL(5))
530 mq_print(q, b, "Eye fix");
533 static coord_t
534 fillboard_check(struct playout_policy *p, struct board *b)
536 struct moggy_policy *pp = p->data;
537 unsigned int fbtries = b->flen / 8;
538 if (pp->fillboardtries < fbtries)
539 fbtries = pp->fillboardtries;
541 for (unsigned int i = 0; i < fbtries; i++) {
542 coord_t coord = b->f[fast_random(b->flen)];
543 if (immediate_liberty_count(b, coord) != 4)
544 continue;
545 foreach_diag_neighbor(b, coord) {
546 if (board_at(b, c) != S_NONE)
547 goto next_try;
548 } foreach_diag_neighbor_end;
549 return coord;
550 next_try:
553 return pass;
556 static coord_t
557 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
559 struct moggy_policy *pp = p->data;
561 if (PLDEBUGL(5))
562 board_print(b, stderr);
564 /* Ko fight check */
565 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
566 && b->moves - b->last_ko_age < pp->koage
567 && pp->korate > fast_random(100)) {
568 if (board_is_valid_play(b, to_play, b->last_ko.coord)
569 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
570 return b->last_ko.coord;
573 /* Local checks */
574 if (!is_pass(b->last_move.coord)) {
575 /* Local group in atari? */
576 if (pp->lcapturerate > fast_random(100)) {
577 struct move_queue q; q.moves = 0;
578 local_atari_check(p, b, &b->last_move, &q);
579 if (q.moves > 0)
580 return mq_pick(&q);
583 /* Local group trying to escape ladder? */
584 if (pp->ladderrate > fast_random(100)) {
585 struct move_queue q; q.moves = 0;
586 local_ladder_check(p, b, &b->last_move, &q);
587 if (q.moves > 0)
588 return mq_pick(&q);
591 /* Local group can be PUT in atari? */
592 if (pp->atarirate > fast_random(100)) {
593 struct move_queue q; q.moves = 0;
594 local_2lib_check(p, b, &b->last_move, &q);
595 if (q.moves > 0)
596 return mq_pick(&q);
599 /* Local group reduced some of our groups to 3 libs? */
600 if (pp->nlibrate > fast_random(100)) {
601 struct move_queue q; q.moves = 0;
602 local_nlib_check(p, b, &b->last_move, &q);
603 if (q.moves > 0)
604 return mq_pick(&q);
607 /* Some other semeai-ish shape checks */
608 if (pp->eyefixrate > fast_random(100)) {
609 struct move_queue q; q.moves = 0;
610 eye_fix_check(p, b, &b->last_move, to_play, &q);
611 if (q.moves > 0)
612 return mq_pick(&q);
615 /* Nakade check */
616 if (pp->nakaderate > fast_random(100)
617 && immediate_liberty_count(b, b->last_move.coord) > 0) {
618 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
619 if (!is_pass(nakade))
620 return nakade;
623 /* Check for patterns we know */
624 if (pp->patternrate > fast_random(100)) {
625 struct move_queue q; q.moves = 0;
626 fixp_t gammas[MQL];
627 apply_pattern(p, b, &b->last_move,
628 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
629 &q, gammas);
630 if (q.moves > 0)
631 return mq_gamma_pick(&q, gammas);
635 /* Global checks */
637 /* Any groups in atari? */
638 if (pp->capturerate > fast_random(100)) {
639 struct move_queue q; q.moves = 0;
640 global_atari_check(p, b, to_play, &q);
641 if (q.moves > 0)
642 return mq_pick(&q);
645 /* Joseki moves? */
646 if (pp->josekirate > fast_random(100)) {
647 struct move_queue q; q.moves = 0;
648 joseki_check(p, b, to_play, &q);
649 if (q.moves > 0)
650 return mq_pick(&q);
653 /* Fill board */
654 if (pp->fillboardtries > 0) {
655 coord_t c = fillboard_check(p, b);
656 if (!is_pass(c))
657 return c;
660 return pass;
663 /* Pick a move from queue q, giving different likelihoods to moves
664 * based on their tags. */
665 static coord_t
666 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
668 struct moggy_policy *pp = p->data;
670 /* First, merge all entries for a move. */
671 /* We use a naive O(N^2) since the average length of the queue
672 * is about 1.4. */
673 for (unsigned int i = 0; i < q->moves; i++) {
674 for (unsigned int j = i + 1; j < q->moves; j++) {
675 if (q->move[i] != q->move[j])
676 continue;
677 q->tag[i] |= q->tag[j];
678 q->moves--;
679 q->tag[j] = q->tag[q->moves];
680 q->move[j] = q->move[q->moves];
684 /* Now, construct a probdist. */
685 fixp_t total = 0;
686 fixp_t pd[q->moves];
687 for (unsigned int i = 0; i < q->moves; i++) {
688 double val = 1.0;
689 assert(q->tag[i] != 0);
690 for (int j = 0; j < MQ_MAX; j++)
691 if (q->tag[i] & (1<<j)) {
692 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
693 val *= pp->mq_prob[j];
695 pd[i] = double_to_fixp(val);
696 total += pd[i];
698 total += double_to_fixp(pp->tenuki_prob);
700 /* Finally, pick a move! */
701 fixp_t stab = fast_irandom(total);
702 if (PLDEBUGL(5)) {
703 fprintf(stderr, "Pick (total %.3f stab %.3f): ", fixp_to_double(total), fixp_to_double(stab));
704 for (unsigned int i = 0; i < q->moves; i++) {
705 fprintf(stderr, "%s(%x:%.3f) ", coord2sstr(q->move[i], b), q->tag[i], fixp_to_double(pd[i]));
707 fprintf(stderr, "\n");
709 for (unsigned int i = 0; i < q->moves; i++) {
710 //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));
711 if (stab < pd[i])
712 return q->move[i];
713 stab -= pd[i];
716 /* Tenuki. */
717 assert(stab < double_to_fixp(pp->tenuki_prob));
718 return pass;
721 static coord_t
722 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
724 struct moggy_policy *pp = p->data;
725 struct move_queue q; q.moves = 0;
727 if (PLDEBUGL(5))
728 board_print(b, stderr);
730 /* Ko fight check */
731 if (pp->korate > 0 && !is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
732 && b->moves - b->last_ko_age < pp->koage) {
733 if (board_is_valid_play(b, to_play, b->last_ko.coord)
734 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
735 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
738 /* Local checks */
739 if (!is_pass(b->last_move.coord)) {
740 /* Local group in atari? */
741 if (pp->lcapturerate > 0)
742 local_atari_check(p, b, &b->last_move, &q);
744 /* Local group trying to escape ladder? */
745 if (pp->ladderrate > 0)
746 local_ladder_check(p, b, &b->last_move, &q);
748 /* Local group can be PUT in atari? */
749 if (pp->atarirate > 0)
750 local_2lib_check(p, b, &b->last_move, &q);
752 /* Local group reduced some of our groups to 3 libs? */
753 if (pp->nlibrate > 0)
754 local_nlib_check(p, b, &b->last_move, &q);
756 /* Some other semeai-ish shape checks */
757 if (pp->eyefixrate > 0)
758 eye_fix_check(p, b, &b->last_move, to_play, &q);
760 /* Nakade check */
761 if (pp->nakaderate > 0 && immediate_liberty_count(b, b->last_move.coord) > 0) {
762 coord_t nakade = nakade_check(p, b, &b->last_move, to_play);
763 if (!is_pass(nakade))
764 mq_add(&q, nakade, 1<<MQ_NAKADE);
767 /* Check for patterns we know */
768 if (pp->patternrate > 0) {
769 fixp_t gammas[MQL];
770 apply_pattern(p, b, &b->last_move,
771 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
772 &q, gammas);
773 /* FIXME: Use the gammas. */
777 /* Global checks */
779 /* Any groups in atari? */
780 if (pp->capturerate > 0)
781 global_atari_check(p, b, to_play, &q);
783 /* Joseki moves? */
784 if (pp->josekirate > 0)
785 joseki_check(p, b, to_play, &q);
787 #if 0
788 /* Average length of the queue is 1.4 move. */
789 printf("MQL %d ", q.moves);
790 for (unsigned int i = 0; i < q.moves; i++)
791 printf("%s ", coord2sstr(q.move[i], b));
792 printf("\n");
793 #endif
795 if (q.moves > 0)
796 return mq_tagged_choose(p, b, to_play, &q);
798 /* Fill board */
799 if (pp->fillboardtries > 0) {
800 coord_t c = fillboard_check(p, b);
801 if (!is_pass(c))
802 return c;
805 return pass;
809 static void
810 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
812 struct moggy_policy *pp = p->data;
813 struct board *b = map->b;
814 struct move_queue q; q.moves = 0;
816 if (board_group_info(b, g).libs > pp->nlib_count)
817 return;
819 if (PLDEBUGL(5)) {
820 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
821 board_print(b, stderr);
824 if (board_group_info(b, g).libs > 2) {
825 if (!pp->nlibrate)
826 return;
827 if (board_at(b, g) != map->to_play)
828 return; // we do only defense
829 group_nlib_defense_check(b, g, map->to_play, &q, 0);
830 while (q.moves--) {
831 coord_t coord = q.move[q.moves];
832 if (PLDEBUGL(5))
833 fprintf(stderr, "1.0: nlib %s\n", coord2sstr(coord, b));
834 int assess = games / 2;
835 add_prior_value(map, coord, 1, assess);
837 return;
840 if (board_group_info(b, g).libs == 2) {
841 if (pp->ladderrate) {
842 /* Make sure to play the correct liberty in case
843 * this is a group that can be caught in a ladder. */
844 bool ladderable = false;
845 for (int i = 0; i < 2; i++) {
846 coord_t chase = board_group_info(b, g).lib[i];
847 coord_t escape = board_group_info(b, g).lib[1 - i];
848 if (wouldbe_ladder(b, g, escape, chase, board_at(b, g))) {
849 add_prior_value(map, chase, 1, games);
850 ladderable = true;
853 if (ladderable)
854 return; // do not suggest the other lib at all
857 if (!pp->atarirate)
858 return;
859 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
860 while (q.moves--) {
861 coord_t coord = q.move[q.moves];
862 if (PLDEBUGL(5))
863 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
864 int assess = games / 2;
865 add_prior_value(map, coord, 1, assess);
867 return;
870 /* This group, sir, is in atari! */
872 coord_t ladder = pass;
873 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, true, 0);
874 while (q.moves--) {
875 coord_t coord = q.move[q.moves];
877 /* _Never_ play here if this move plays out
878 * a caught ladder. */
879 if (coord == ladder && !board_playing_ko_threat(b)) {
880 /* Note that the opposite is not guarded against;
881 * we do not advise against capturing a laddered
882 * group (but we don't encourage it either). Such
883 * a move can simplify tactical situations if we
884 * can afford it. */
885 if (map->to_play != board_at(b, g))
886 continue;
887 /* FIXME: We give the malus even if this move
888 * captures another group. */
889 if (PLDEBUGL(5))
890 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
891 add_prior_value(map, coord, 0, games);
892 continue;
895 if (!pp->capturerate && !pp->lcapturerate)
896 continue;
898 int assess = games * 2;
899 if (pp->cap_stone_denom > 0) {
900 int stones = group_stone_count(b, g, pp->cap_stone_max) - (pp->cap_stone_min-1);
901 assess += (stones > 0 ? stones : 0) * games * 100 / pp->cap_stone_denom;
903 if (PLDEBUGL(5))
904 fprintf(stderr, "1.0 (%d): atari %s\n", assess, coord2sstr(coord, b));
905 add_prior_value(map, coord, 1, assess);
909 static void
910 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
912 struct moggy_policy *pp = p->data;
913 struct board *b = map->b;
915 if (PLDEBUGL(5)) {
916 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
917 board_print(b, stderr);
920 /* Is this move a self-atari? */
921 if (pp->selfatarirate) {
922 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
923 if (PLDEBUGL(5))
924 fprintf(stderr, "0.0: self-atari\n");
925 add_prior_value(map, coord, 0, games);
926 if (!pp->selfatari_other)
927 return;
928 /* If we can play on the other liberty of the
929 * endangered group, do! */
930 coord = selfatari_cousin(b, map->to_play, coord, NULL);
931 if (is_pass(coord))
932 return;
933 if (PLDEBUGL(5))
934 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
935 add_prior_value(map, coord, 1.0, games);
936 return;
940 /* Pattern check */
941 if (pp->patternrate) {
942 // XXX: Use gamma value?
943 struct move m = { .color = map->to_play, .coord = coord };
944 if (test_pattern3_here(p, b, &m, true, NULL)) {
945 if (PLDEBUGL(5))
946 fprintf(stderr, "1.0: pattern\n");
947 add_prior_value(map, coord, 1, games);
951 return;
954 static void
955 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
957 struct moggy_policy *pp = p->data;
959 /* First, go through all endangered groups. */
960 for (group_t g = 1; g < board_size2(map->b); g++)
961 if (group_at(map->b, g) == g)
962 playout_moggy_assess_group(p, map, g, games);
964 /* Then, assess individual moves. */
965 if (!pp->patternrate && !pp->selfatarirate)
966 return;
967 foreach_free_point(map->b) {
968 if (map->consider[c])
969 playout_moggy_assess_one(p, map, c, games);
970 } foreach_free_point_end;
973 static bool
974 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
976 struct moggy_policy *pp = p->data;
978 /* The idea is simple for now - never allow self-atari moves.
979 * They suck in general, but this also permits us to actually
980 * handle seki in the playout stage. */
982 if (fast_random(100) >= pp->selfatarirate) {
983 if (PLDEBUGL(5))
984 fprintf(stderr, "skipping sar test\n");
985 goto sar_skip;
987 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
988 if (selfatari) {
989 if (PLDEBUGL(5))
990 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
991 stone2str(m->color), coord2sstr(m->coord, b));
992 if (pp->selfatari_other) {
993 /* Ok, try the other liberty of the atari'd group. */
994 coord_t c = selfatari_cousin(b, m->color, m->coord, NULL);
995 if (is_pass(c)) return false;
996 if (PLDEBUGL(5))
997 fprintf(stderr, "___ Redirecting to other lib %s\n",
998 coord2sstr(c, b));
999 m->coord = c;
1000 return true;
1002 return false;
1004 sar_skip:
1006 /* Check if we don't seem to be filling our eye. This should
1007 * happen only for false eyes, but some of them are in fact
1008 * real eyes with diagonal filled by a dead stone. Prefer
1009 * to counter-capture in that case. */
1010 if (fast_random(100) >= pp->eyefillrate) {
1011 if (PLDEBUGL(5))
1012 fprintf(stderr, "skipping eyefill test\n");
1013 goto eyefill_skip;
1015 bool eyefill = board_is_eyelike(b, m->coord, m->color);
1016 if (eyefill) {
1017 foreach_diag_neighbor(b, m->coord) {
1018 if (board_at(b, c) != stone_other(m->color))
1019 continue;
1020 switch (board_group_info(b, group_at(b, c)).libs) {
1021 case 1: /* Capture! */
1022 c = board_group_info(b, group_at(b, c)).lib[0];
1023 if (PLDEBUGL(5))
1024 fprintf(stderr, "___ Redirecting to capture %s\n",
1025 coord2sstr(c, b));
1026 m->coord = c;
1027 return true;
1028 case 2: /* Try to switch to some 2-lib neighbor. */
1029 for (int i = 0; i < 2; i++) {
1030 coord_t l = board_group_info(b, group_at(b, c)).lib[i];
1031 if (board_is_one_point_eye(b, l, board_at(b, c)))
1032 continue;
1033 if (is_bad_selfatari(b, m->color, l))
1034 continue;
1035 m->coord = l;
1036 return true;
1038 break;
1040 } foreach_diag_neighbor_end;
1043 eyefill_skip:
1044 return true;
1047 struct playout_policy *
1048 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
1050 struct playout_policy *p = calloc2(1, sizeof(*p));
1051 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
1052 p->data = pp;
1053 p->choose = playout_moggy_seqchoose;
1054 p->assess = playout_moggy_assess;
1055 p->permit = playout_moggy_permit;
1056 /* no p->done: calling engine owns jdict and should call joseki_done() */
1058 pp->jdict = jdict;
1060 /* These settings are tuned for 19x19 play with several threads
1061 * on reasonable time limits (i.e., rather large number of playouts).
1062 * XXX: no 9x9 tuning has been done recently. */
1063 int rate = board_large(b) ? 80 : 90;
1065 pp->patternrate = pp->eyefixrate = 100;
1066 pp->lcapturerate = 90;
1067 pp->atarirate = pp->josekirate = -1U;
1068 pp->nakaderate = 60;
1069 pp->korate = 40; pp->koage = 4;
1070 pp->alwaysccaprate = 40;
1071 pp->eyefillrate = 60;
1072 pp->nlibrate = 25;
1074 /* selfatarirate is slightly special, since to avoid playing some
1075 * silly move that stays on the board, it needs to block it many
1076 * times during a simulation - we'd like that to happen in most
1077 * simulations, so we try to use a very high selfatarirate.
1078 * XXX: Perhaps it would be better to permanently ban moves in
1079 * the current simulation after testing them once.
1080 * XXX: We would expect the above to be the case, but since some
1081 * unclear point, selfatari 95 -> 60 gives a +~50Elo boost against
1082 * GNUGo. This might be indicative of some bug, FIXME bisect? */
1083 pp->selfatarirate = 60;
1084 pp->selfatari_other = true;
1086 pp->pattern2 = true;
1088 pp->cap_stone_min = 2;
1089 pp->cap_stone_max = 15;
1090 pp->cap_stone_denom = 200;
1092 pp->atari_def_no_hopeless = !board_large(b);
1093 pp->atari_miaisafe = true;
1094 pp->nlib_count = 4;
1096 /* C is stupid. */
1097 double mq_prob_default[MQ_MAX] = {
1098 [MQ_KO] = 6.0,
1099 [MQ_NAKADE] = 5.5,
1100 [MQ_LATARI] = 5.0,
1101 [MQ_L2LIB] = 4.0,
1102 [MQ_LNLIB] = 3.5,
1103 [MQ_PAT3] = 3.0,
1104 [MQ_GATARI] = 2.0,
1105 [MQ_JOSEKI] = 1.0,
1107 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
1109 /* Default 3x3 pattern gammas tuned on 15x15 with 500s/game on
1110 * i7-3770 single thread using 40000 CLOP games. */
1111 double pat3_gammas_default[PAT3_N] = {
1112 0.52, 0.53, 0.32, 0.22, 0.37, 0.28, 0.21, 0.19, 0.82,
1113 0.12, 0.20, 0.11, 0.16, 0.57, 0.44
1115 memcpy(pp->pat3_gammas, pat3_gammas_default, sizeof(pp->pat3_gammas));
1117 if (arg) {
1118 char *optspec, *next = arg;
1119 while (*next) {
1120 optspec = next;
1121 next += strcspn(next, ":");
1122 if (*next) { *next++ = 0; } else { *next = 0; }
1124 char *optname = optspec;
1125 char *optval = strchr(optspec, '=');
1126 if (optval) *optval++ = 0;
1128 if (!strcasecmp(optname, "debug") && optval) {
1129 p->debug_level = atoi(optval);
1130 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
1131 pp->lcapturerate = atoi(optval);
1132 } else if (!strcasecmp(optname, "ladderrate") && optval) {
1133 /* Note that ladderrate is considered obsolete;
1134 * it is ineffective and superseded by the
1135 * prune_ladders prior. */
1136 pp->ladderrate = atoi(optval);
1137 } else if (!strcasecmp(optname, "atarirate") && optval) {
1138 pp->atarirate = atoi(optval);
1139 } else if (!strcasecmp(optname, "nlibrate") && optval) {
1140 pp->nlibrate = atoi(optval);
1141 } else if (!strcasecmp(optname, "capturerate") && optval) {
1142 pp->capturerate = atoi(optval);
1143 } else if (!strcasecmp(optname, "patternrate") && optval) {
1144 pp->patternrate = atoi(optval);
1145 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
1146 pp->selfatarirate = atoi(optval);
1147 } else if (!strcasecmp(optname, "eyefillrate") && optval) {
1148 pp->eyefillrate = atoi(optval);
1149 } else if (!strcasecmp(optname, "korate") && optval) {
1150 pp->korate = atoi(optval);
1151 } else if (!strcasecmp(optname, "josekirate") && optval) {
1152 pp->josekirate = atoi(optval);
1153 } else if (!strcasecmp(optname, "nakaderate") && optval) {
1154 pp->nakaderate = atoi(optval);
1155 } else if (!strcasecmp(optname, "eyefixrate") && optval) {
1156 pp->eyefixrate = atoi(optval);
1157 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
1158 pp->alwaysccaprate = atoi(optval);
1159 } else if (!strcasecmp(optname, "rate") && optval) {
1160 rate = atoi(optval);
1161 } else if (!strcasecmp(optname, "fillboardtries")) {
1162 pp->fillboardtries = atoi(optval);
1163 } else if (!strcasecmp(optname, "koage") && optval) {
1164 pp->koage = atoi(optval);
1165 } else if (!strcasecmp(optname, "pattern2")) {
1166 pp->pattern2 = optval && *optval == '0' ? false : true;
1167 } else if (!strcasecmp(optname, "selfatari_other")) {
1168 pp->selfatari_other = optval && *optval == '0' ? false : true;
1169 } else if (!strcasecmp(optname, "capcheckall")) {
1170 pp->capcheckall = optval && *optval == '0' ? false : true;
1171 } else if (!strcasecmp(optname, "cap_stone_min") && optval) {
1172 pp->cap_stone_min = atoi(optval);
1173 } else if (!strcasecmp(optname, "cap_stone_max") && optval) {
1174 pp->cap_stone_max = atoi(optval);
1175 } else if (!strcasecmp(optname, "cap_stone_denom") && optval) {
1176 pp->cap_stone_denom = atoi(optval);
1177 } else if (!strcasecmp(optname, "atari_miaisafe")) {
1178 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
1179 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
1180 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
1181 } else if (!strcasecmp(optname, "nlib_count") && optval) {
1182 pp->nlib_count = atoi(optval);
1183 } else if (!strcasecmp(optname, "middle_ladder")) {
1184 pp->middle_ladder = optval && *optval == '0' ? false : true;
1185 } else if (!strcasecmp(optname, "fullchoose")) {
1186 pp->fullchoose = true;
1187 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
1188 } else if (!strcasecmp(optname, "mqprob") && optval) {
1189 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
1190 for (int i = 0; *optval && i < MQ_MAX; i++) {
1191 pp->mq_prob[i] = atof(optval);
1192 optval += strcspn(optval, "%");
1193 if (*optval) optval++;
1195 } else if (!strcasecmp(optname, "pat3gammas") && optval) {
1196 /* PAT3_N %-separated floating point values */
1197 for (int i = 0; *optval && i < PAT3_N; i++) {
1198 pp->pat3_gammas[i] = atof(optval);
1199 optval += strcspn(optval, "%");
1200 if (*optval) optval++;
1202 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
1203 pp->tenuki_prob = atof(optval);
1204 } else {
1205 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
1206 exit(1);
1210 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
1211 if (pp->atarirate == -1U) pp->atarirate = rate;
1212 if (pp->nlibrate == -1U) pp->nlibrate = rate;
1213 if (pp->capturerate == -1U) pp->capturerate = rate;
1214 if (pp->patternrate == -1U) pp->patternrate = rate;
1215 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
1216 if (pp->eyefillrate == -1U) pp->eyefillrate = rate;
1217 if (pp->korate == -1U) pp->korate = rate;
1218 if (pp->josekirate == -1U) pp->josekirate = rate;
1219 if (pp->ladderrate == -1U) pp->ladderrate = rate;
1220 if (pp->nakaderate == -1U) pp->nakaderate = rate;
1221 if (pp->eyefixrate == -1U) pp->eyefixrate = rate;
1222 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
1224 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
1226 return p;