1 /* Heuristical playout (and tree prior) policy modelled primarily after
2 * the description of the Mogo engine. */
12 #include "joseki/base.h"
16 #include "playout/moggy.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. */
45 #define MQ_LADDER MQ_L2LIB /* XXX: We want to fit in char still! */
57 /* Note that the context can be shared by multiple threads! */
60 unsigned int lcapturerate
, atarirate
, nlibrate
, ladderrate
, capturerate
, patternrate
, korate
, josekirate
, nakaderate
, eyefixrate
;
61 unsigned int selfatarirate
, eyefillrate
, alwaysccaprate
;
62 unsigned int fillboardtries
;
64 /* Whether to look for patterns around second-to-last move. */
66 /* Whether, when self-atari attempt is detected, to play the other
67 * group's liberty if that is non-self-atari. */
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. */
75 /* Whether to always pick from moves capturing all groups in
76 * global_atari_check(). */
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
;
84 bool atari_def_no_hopeless
;
90 struct joseki_dict
*jdict
;
91 struct pattern3s patterns
;
93 double pat3_gammas
[PAT3_N
];
95 /* Gamma values for queue tags - correspond to probabilities. */
98 double mq_prob
[MQ_MAX
], tenuki_prob
;
102 static char moggy_patterns_src
[PAT3_N
][11] = {
103 /* hane pattern - enclosing hane */ /* 0.52 */
107 /* hane pattern - non-cutting hane */ /* 0.53 */
111 /* hane pattern - magari */ /* 0.32 */
115 /* hane pattern - thin hane */ /* 0.22 */
119 /* generic pattern - katatsuke or diagonal attachment; similar to magari */ /* 0.37 */
123 /* cut1 pattern (kiri) - unprotected cut */ /* 0.28 */
127 /* cut1 pattern (kiri) - peeped cut */ /* 0.21 */
131 /* cut2 pattern (de) */ /* 0.19 */
135 /* cut keima (not in Mogo) */ /* 0.82 */
138 "?o?", /* oo? has some pathological tsumego cases */
139 /* side pattern - chase */ /* 0.12 */
143 /* side pattern - block side cut */ /* 0.20 */
147 /* side pattern - block side connection */ /* 0.11 */
151 /* side pattern - sagari (SUSPICIOUS) */ /* 0.16 */
153 "x.x" /* Mogo has "x.?" */
154 "###" /* Mogo has "X" */,
156 /* side pattern - throw-in (SUSPICIOUS) */
161 /* side pattern - cut (SUSPICIOUS) */ /* 0.57 */
164 "###" /* Mogo has "X" */,
165 /* side pattern - eye piercing:
170 /* side pattern - make eye */ /* 0.44 */
180 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
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... */
188 if (!pattern3_move_here(&pp
->patterns
, b
, m
, &pi
))
190 /* ...and the move is not obviously stupid. */
191 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
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))
199 //fprintf(stderr, "%s: %d (%.3f)\n", coord2sstr(m->coord, b), (int) pi, pp->pat3_gammas[(int) pi]);
201 *gamma
= pp
->pat3_gammas
[(int) pi
];
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
};
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). */
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
)
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
))
232 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
, gammas
);
233 } foreach_8neighbor_end
;
237 mq_gamma_print(q
, gammas
, b
, "Pattern");
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
;
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
];
252 for (; !is_pass(*cc
); cc
++) {
253 if (coord_quadrant(*cc
, b
) != i
)
255 if (board_is_valid_play(b
, to_play
, *cc
))
257 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
261 if (q
->moves
> 0 && PLDEBUGL(5))
262 mq_print(q
, b
, "Joseki");
266 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
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
);
276 mq_print(q
, b
, "Global atari");
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
);
285 /* XXX: Try carrying on. */
287 mq_print(q
, b
, "Global atari");
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
);
295 /* XXX: Try carrying on. */
297 mq_print(q
, b
, "Global atari");
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)
318 group_atari_check(pp
->alwaysccaprate
, b
, g
, stone_other(m
->color
), q
, NULL
, pp
->middle_ladder
, 1<<MQ_LATARI
);
322 mq_print(q
, b
, "Local atari");
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)
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");
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
);
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
361 return q
->move
[fast_random(q
->moves
)];
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)
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
375 libmap_mq_print(q
, b
, "Local 2lib");
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:
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
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. */
413 foreach_8neighbor(b
, m
->coord
) {
414 group_t g
= group_at(b
, c
);
415 if (!g
|| group2
== g
|| board_at(b
, c
) != color
)
417 if (board_group_info(b
, g
).libs
< 3 || board_group_info(b
, g
).libs
> pp
->nlib_count
)
419 group_nlib_defense_check(b
, g
, color
, q
, 1<<MQ_LNLIB
);
420 group2
= g
; // prevent trivial repeated checks
421 } foreach_8neighbor_end
;
424 libmap_mq_print(q
, b
, "Local nlib");
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
)
434 if (is_pass(empty
)) {
438 if (!coord_is_8adjecent(c
, empty
, b
)) {
439 /* Seems like impossible nakade
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
));
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
490 && immediate_liberty_count(b
, c1
) > 0) {
491 foreach_neighbor(b
, c1
, {
492 if (c
== m
->coord
|| board_at(b
, c
) != S_NONE
)
494 /* ...and the neighbor must potentially falsify
496 coord_t falsifying
= c
;
497 foreach_diag_neighbor(b
, falsifying
) {
498 if (board_at(b
, c
) != S_NONE
)
500 if (!board_is_eyelike(b
, c
, to_play
))
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
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);
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
;
530 if (q
->moves
> 0 && PLDEBUGL(5))
531 mq_print(q
, b
, "Eye fix");
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)
546 foreach_diag_neighbor(b
, coord
) {
547 if (board_at(b
, c
) != S_NONE
)
549 } foreach_diag_neighbor_end
;
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
;
563 board_print(b
, stderr
);
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
;
575 if (!is_pass(b
->last_move
.coord
)) {
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
))
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
);
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
);
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
);
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
);
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
);
626 /* Check for patterns we know */
627 if (pp
->patternrate
> fast_random(100)) {
628 struct move_queue q
; q
.moves
= 0;
630 apply_pattern(p
, b
, &b
->last_move
,
631 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
634 return mq_gamma_pick(&q
, gammas
);
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
);
649 if (pp
->josekirate
> fast_random(100)) {
650 struct move_queue q
; q
.moves
= 0;
651 joseki_check(p
, b
, to_play
, &q
);
657 if (pp
->fillboardtries
> 0) {
658 coord_t c
= fillboard_check(p
, b
);
666 /* Pick a move from queue q, giving different likelihoods to moves
667 * based on their tags. */
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
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
])
680 q
->tag
[i
] |= q
->tag
[j
];
682 q
->tag
[j
] = q
->tag
[q
->moves
];
683 q
->move
[j
] = q
->move
[q
->moves
];
687 /* Now, construct a probdist. */
690 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
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
);
701 total
+= double_to_fixp(pp
->tenuki_prob
);
703 /* Finally, pick a move! */
704 fixp_t stab
= fast_irandom(total
);
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));
720 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
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;
731 board_print(b
, stderr
);
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
);
742 if (!is_pass(b
->last_move
.coord
)) {
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) {
777 apply_pattern(p
, b
, &b
->last_move
,
778 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
780 /* FIXME: Use the gammas. */
786 /* Any groups in atari? */
787 if (pp
->capturerate
> 0)
788 global_atari_check(p
, b
, to_play
, &q
);
791 if (pp
->josekirate
> 0)
792 joseki_check(p
, b
, to_play
, &q
);
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
));
803 return mq_tagged_choose(p
, b
, to_play
, &q
);
806 if (pp
->fillboardtries
> 0) {
807 coord_t c
= fillboard_check(p
, b
);
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
)
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) {
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
];
841 fprintf(stderr
, "1.0: nlib %s\n", coord2sstr(coord
, b
));
842 int assess
= games
/ 2;
843 add_prior_value(map
, coord
, 1, assess
);
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
);
862 return; // do not suggest the other lib at all
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
];
872 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
873 int assess
= games
/ 2;
874 add_prior_value(map
, coord
, 1, assess
);
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);
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
895 if (map
->to_play
!= board_at(b
, g
))
897 /* FIXME: We give the malus even if this move
898 * captures another group. */
900 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
901 add_prior_value(map
, coord
, 0, games
);
905 if (!pp
->capturerate
&& !pp
->lcapturerate
)
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
;
914 fprintf(stderr
, "1.0 (%d): atari %s\n", assess
, coord2sstr(coord
, b
));
915 add_prior_value(map
, coord
, 1, assess
);
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
;
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
)) {
934 fprintf(stderr
, "0.0: self-atari\n");
935 add_prior_value(map
, coord
, 0, games
);
936 if (!pp
->selfatari_other
)
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
);
944 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
945 add_prior_value(map
, coord
, 1.0, games
);
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
)) {
956 fprintf(stderr
, "1.0: pattern\n");
957 add_prior_value(map
, coord
, 1, games
);
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
)
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
;
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
) {
994 fprintf(stderr
, "skipping sar test\n");
997 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
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;
1007 fprintf(stderr
, "___ Redirecting to other lib %s\n",
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
) {
1022 fprintf(stderr
, "skipping eyefill test\n");
1025 bool eyefill
= board_is_eyelike(b
, m
->coord
, m
->color
);
1027 foreach_diag_neighbor(b
, m
->coord
) {
1028 if (board_at(b
, c
) != stone_other(m
->color
))
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];
1034 fprintf(stderr
, "___ Redirecting to capture %s\n",
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
)))
1043 if (is_bad_selfatari(b
, m
->color
, l
))
1050 } foreach_diag_neighbor_end
;
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
));
1064 p
->choose
= playout_moggy_seqchoose
;
1065 p
->assess
= playout_moggy_assess
;
1066 p
->permit
= playout_moggy_permit
;
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;
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;
1104 double mq_prob_default
[MQ_MAX
] = {
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
));
1125 char *optspec
, *next
= arg
;
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
);
1209 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
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
);