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 "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. */
44 #define MQ_LADDER MQ_L2LIB /* XXX: We want to fit in char still! */
56 /* Note that the context can be shared by multiple threads! */
59 unsigned int lcapturerate
, atarirate
, nlibrate
, ladderrate
, capturerate
, patternrate
, korate
, josekirate
, nakaderate
, eyefixrate
;
60 unsigned int selfatarirate
, eyefillrate
, alwaysccaprate
;
61 unsigned int fillboardtries
;
63 /* Whether to look for patterns around second-to-last move. */
65 /* Whether, when self-atari attempt is detected, to play the other
66 * group's liberty if that is non-self-atari. */
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. */
74 /* Whether to always pick from moves capturing all groups in
75 * global_atari_check(). */
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
;
83 bool atari_def_no_hopeless
;
89 struct joseki_dict
*jdict
;
90 struct pattern3s patterns
;
92 double pat3_gammas
[PAT3_N
];
94 /* Gamma values for queue tags - correspond to probabilities. */
97 double mq_prob
[MQ_MAX
], tenuki_prob
;
101 static char moggy_patterns_src
[PAT3_N
][11] = {
102 /* hane pattern - enclosing hane */ /* 0.52 */
106 /* hane pattern - non-cutting hane */ /* 0.53 */
110 /* hane pattern - magari */ /* 0.32 */
114 /* hane pattern - thin hane */ /* 0.22 */
118 /* generic pattern - katatsuke or diagonal attachment; similar to magari */ /* 0.37 */
122 /* cut1 pattern (kiri) - unprotected cut */ /* 0.28 */
126 /* cut1 pattern (kiri) - peeped cut */ /* 0.21 */
130 /* cut2 pattern (de) */ /* 0.19 */
134 /* cut keima (not in Mogo) */ /* 0.82 */
137 "?o?", /* oo? has some pathological tsumego cases */
138 /* side pattern - chase */ /* 0.12 */
142 /* side pattern - block side cut */ /* 0.20 */
146 /* side pattern - block side connection */ /* 0.11 */
150 /* side pattern - sagari (SUSPICIOUS) */ /* 0.16 */
152 "x.x" /* Mogo has "x.?" */
153 "###" /* Mogo has "X" */,
155 /* side pattern - throw-in (SUSPICIOUS) */
160 /* side pattern - cut (SUSPICIOUS) */ /* 0.57 */
163 "###" /* Mogo has "X" */,
164 /* side pattern - eye piercing:
169 /* side pattern - make eye */ /* 0.44 */
179 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
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... */
187 if (!pattern3_move_here(&pp
->patterns
, b
, m
, &pi
))
189 /* ...and the move is not obviously stupid. */
190 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
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))
198 //fprintf(stderr, "%s: %d (%.3f)\n", coord2sstr(m->coord, b), (int) pi, pp->pat3_gammas[(int) pi]);
200 *gamma
= pp
->pat3_gammas
[(int) pi
];
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
};
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). */
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
)
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
))
231 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
, gammas
);
232 } foreach_8neighbor_end
;
236 mq_gamma_print(q
, gammas
, b
, "Pattern");
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
;
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
];
251 for (; !is_pass(*cc
); cc
++) {
252 if (coord_quadrant(*cc
, b
) != i
)
254 if (board_is_valid_play(b
, to_play
, *cc
))
256 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
260 if (q
->moves
> 0 && PLDEBUGL(5))
261 mq_print(q
, b
, "Joseki");
265 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
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
);
275 mq_print(q
, b
, "Global atari");
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
);
284 /* XXX: Try carrying on. */
286 mq_print(q
, b
, "Global atari");
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
);
294 /* XXX: Try carrying on. */
296 mq_print(q
, b
, "Global atari");
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)
317 group_atari_check(pp
->alwaysccaprate
, b
, g
, stone_other(m
->color
), q
, NULL
, pp
->middle_ladder
, 1<<MQ_LATARI
);
321 mq_print(q
, b
, "Local atari");
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)
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");
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
);
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
360 return q
->move
[fast_random(q
->moves
)];
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)
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
374 mq_print(q
, b
, "Local 2lib");
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:
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
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. */
412 foreach_8neighbor(b
, m
->coord
) {
413 group_t g
= group_at(b
, c
);
414 if (!g
|| group2
== g
|| board_at(b
, c
) != color
)
416 if (board_group_info(b
, g
).libs
< 3 || board_group_info(b
, g
).libs
> pp
->nlib_count
)
418 group_nlib_defense_check(b
, g
, color
, q
, 1<<MQ_LNLIB
);
419 group2
= g
; // prevent trivial repeated checks
420 } foreach_8neighbor_end
;
423 mq_print(q
, b
, "Local nlib");
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
)
433 if (is_pass(empty
)) {
437 if (!coord_is_8adjecent(c
, empty
, b
)) {
438 /* Seems like impossible nakade
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
));
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
489 && immediate_liberty_count(b
, c1
) > 0) {
490 foreach_neighbor(b
, c1
, {
491 if (c
== m
->coord
|| board_at(b
, c
) != S_NONE
)
493 /* ...and the neighbor must potentially falsify
495 coord_t falsifying
= c
;
496 foreach_diag_neighbor(b
, falsifying
) {
497 if (board_at(b
, c
) != S_NONE
)
499 if (!board_is_eyelike(b
, c
, to_play
))
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
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);
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
;
529 if (q
->moves
> 0 && PLDEBUGL(5))
530 mq_print(q
, b
, "Eye fix");
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)
545 foreach_diag_neighbor(b
, coord
) {
546 if (board_at(b
, c
) != S_NONE
)
548 } foreach_diag_neighbor_end
;
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
;
562 board_print(b
, stderr
);
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
;
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
);
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
);
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
);
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
);
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
);
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
))
623 /* Check for patterns we know */
624 if (pp
->patternrate
> fast_random(100)) {
625 struct move_queue q
; q
.moves
= 0;
627 apply_pattern(p
, b
, &b
->last_move
,
628 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
631 return mq_gamma_pick(&q
, gammas
);
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
);
646 if (pp
->josekirate
> fast_random(100)) {
647 struct move_queue q
; q
.moves
= 0;
648 joseki_check(p
, b
, to_play
, &q
);
654 if (pp
->fillboardtries
> 0) {
655 coord_t c
= fillboard_check(p
, b
);
663 /* Pick a move from queue q, giving different likelihoods to moves
664 * based on their tags. */
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
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
])
677 q
->tag
[i
] |= q
->tag
[j
];
679 q
->tag
[j
] = q
->tag
[q
->moves
];
680 q
->move
[j
] = q
->move
[q
->moves
];
684 /* Now, construct a probdist. */
687 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
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
);
698 total
+= double_to_fixp(pp
->tenuki_prob
);
700 /* Finally, pick a move! */
701 fixp_t stab
= fast_irandom(total
);
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));
717 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
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;
728 board_print(b
, stderr
);
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
);
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
);
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) {
770 apply_pattern(p
, b
, &b
->last_move
,
771 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
773 /* FIXME: Use the gammas. */
779 /* Any groups in atari? */
780 if (pp
->capturerate
> 0)
781 global_atari_check(p
, b
, to_play
, &q
);
784 if (pp
->josekirate
> 0)
785 joseki_check(p
, b
, to_play
, &q
);
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
));
796 return mq_tagged_choose(p
, b
, to_play
, &q
);
799 if (pp
->fillboardtries
> 0) {
800 coord_t c
= fillboard_check(p
, b
);
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
)
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) {
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);
831 coord_t coord
= q
.move
[q
.moves
];
833 fprintf(stderr
, "1.0: nlib %s\n", coord2sstr(coord
, b
));
834 int assess
= games
/ 2;
835 add_prior_value(map
, coord
, 1, assess
);
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
);
854 return; // do not suggest the other lib at all
859 group_2lib_check(b
, g
, map
->to_play
, &q
, 0, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
861 coord_t coord
= q
.move
[q
.moves
];
863 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
864 int assess
= games
/ 2;
865 add_prior_value(map
, coord
, 1, assess
);
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);
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
885 if (map
->to_play
!= board_at(b
, g
))
887 /* FIXME: We give the malus even if this move
888 * captures another group. */
890 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
891 add_prior_value(map
, coord
, 0, games
);
895 if (!pp
->capturerate
&& !pp
->lcapturerate
)
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
;
904 fprintf(stderr
, "1.0 (%d): atari %s\n", assess
, coord2sstr(coord
, b
));
905 add_prior_value(map
, coord
, 1, assess
);
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
;
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
)) {
924 fprintf(stderr
, "0.0: self-atari\n");
925 add_prior_value(map
, coord
, 0, games
);
926 if (!pp
->selfatari_other
)
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
);
934 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
935 add_prior_value(map
, coord
, 1.0, games
);
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
)) {
946 fprintf(stderr
, "1.0: pattern\n");
947 add_prior_value(map
, coord
, 1, games
);
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
)
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
;
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
) {
984 fprintf(stderr
, "skipping sar test\n");
987 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
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;
997 fprintf(stderr
, "___ Redirecting to other lib %s\n",
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
) {
1012 fprintf(stderr
, "skipping eyefill test\n");
1015 bool eyefill
= board_is_eyelike(b
, m
->coord
, m
->color
);
1017 foreach_diag_neighbor(b
, m
->coord
) {
1018 if (board_at(b
, c
) != stone_other(m
->color
))
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];
1024 fprintf(stderr
, "___ Redirecting to capture %s\n",
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
)))
1033 if (is_bad_selfatari(b
, m
->color
, l
))
1040 } foreach_diag_neighbor_end
;
1048 struct playout_policy
*
1049 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
1051 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
1052 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
1054 p
->choose
= playout_moggy_seqchoose
;
1055 p
->assess
= playout_moggy_assess
;
1056 p
->permit
= playout_moggy_permit
;
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
= pp
->nakaderate
= -1U;
1068 pp
->nakaderate
= 60;
1069 pp
->korate
= 40; pp
->koage
= 4;
1070 pp
->alwaysccaprate
= 40;
1071 pp
->eyefillrate
= 40;
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 pp
->selfatarirate
= 95;
1081 pp
->selfatari_other
= true;
1083 pp
->pattern2
= true;
1085 pp
->cap_stone_min
= 2;
1086 pp
->cap_stone_max
= 15;
1087 pp
->cap_stone_denom
= 200;
1089 pp
->atari_def_no_hopeless
= !board_large(b
);
1090 pp
->atari_miaisafe
= true;
1094 double mq_prob_default
[MQ_MAX
] = {
1104 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
1106 /* Default 3x3 pattern gammas tuned on 15x15 with 500s/game on
1107 * i7-3770 single thread using 40000 CLOP games. */
1108 double pat3_gammas_default
[PAT3_N
] = {
1109 0.52, 0.53, 0.32, 0.22, 0.37, 0.28, 0.21, 0.19, 0.82,
1110 0.12, 0.20, 0.11, 0.16, 0.57, 0.44
1112 memcpy(pp
->pat3_gammas
, pat3_gammas_default
, sizeof(pp
->pat3_gammas
));
1115 char *optspec
, *next
= arg
;
1118 next
+= strcspn(next
, ":");
1119 if (*next
) { *next
++ = 0; } else { *next
= 0; }
1121 char *optname
= optspec
;
1122 char *optval
= strchr(optspec
, '=');
1123 if (optval
) *optval
++ = 0;
1125 if (!strcasecmp(optname
, "debug") && optval
) {
1126 p
->debug_level
= atoi(optval
);
1127 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
1128 pp
->lcapturerate
= atoi(optval
);
1129 } else if (!strcasecmp(optname
, "ladderrate") && optval
) {
1130 /* Note that ladderrate is considered obsolete;
1131 * it is ineffective and superseded by the
1132 * prune_ladders prior. */
1133 pp
->ladderrate
= atoi(optval
);
1134 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
1135 pp
->atarirate
= atoi(optval
);
1136 } else if (!strcasecmp(optname
, "nlibrate") && optval
) {
1137 pp
->nlibrate
= atoi(optval
);
1138 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
1139 pp
->capturerate
= atoi(optval
);
1140 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
1141 pp
->patternrate
= atoi(optval
);
1142 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
1143 pp
->selfatarirate
= atoi(optval
);
1144 } else if (!strcasecmp(optname
, "eyefillrate") && optval
) {
1145 pp
->eyefillrate
= atoi(optval
);
1146 } else if (!strcasecmp(optname
, "korate") && optval
) {
1147 pp
->korate
= atoi(optval
);
1148 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
1149 pp
->josekirate
= atoi(optval
);
1150 } else if (!strcasecmp(optname
, "nakaderate") && optval
) {
1151 pp
->nakaderate
= atoi(optval
);
1152 } else if (!strcasecmp(optname
, "eyefixrate") && optval
) {
1153 pp
->eyefixrate
= atoi(optval
);
1154 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
1155 pp
->alwaysccaprate
= atoi(optval
);
1156 } else if (!strcasecmp(optname
, "rate") && optval
) {
1157 rate
= atoi(optval
);
1158 } else if (!strcasecmp(optname
, "fillboardtries")) {
1159 pp
->fillboardtries
= atoi(optval
);
1160 } else if (!strcasecmp(optname
, "koage") && optval
) {
1161 pp
->koage
= atoi(optval
);
1162 } else if (!strcasecmp(optname
, "pattern2")) {
1163 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
1164 } else if (!strcasecmp(optname
, "selfatari_other")) {
1165 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
1166 } else if (!strcasecmp(optname
, "capcheckall")) {
1167 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
1168 } else if (!strcasecmp(optname
, "cap_stone_min") && optval
) {
1169 pp
->cap_stone_min
= atoi(optval
);
1170 } else if (!strcasecmp(optname
, "cap_stone_max") && optval
) {
1171 pp
->cap_stone_max
= atoi(optval
);
1172 } else if (!strcasecmp(optname
, "cap_stone_denom") && optval
) {
1173 pp
->cap_stone_denom
= atoi(optval
);
1174 } else if (!strcasecmp(optname
, "atari_miaisafe")) {
1175 pp
->atari_miaisafe
= optval
&& *optval
== '0' ? false : true;
1176 } else if (!strcasecmp(optname
, "atari_def_no_hopeless")) {
1177 pp
->atari_def_no_hopeless
= optval
&& *optval
== '0' ? false : true;
1178 } else if (!strcasecmp(optname
, "nlib_count") && optval
) {
1179 pp
->nlib_count
= atoi(optval
);
1180 } else if (!strcasecmp(optname
, "middle_ladder")) {
1181 pp
->middle_ladder
= optval
&& *optval
== '0' ? false : true;
1182 } else if (!strcasecmp(optname
, "fullchoose")) {
1183 pp
->fullchoose
= true;
1184 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_seqchoose
: playout_moggy_fullchoose
;
1185 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
1186 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
1187 for (int i
= 0; *optval
&& i
< MQ_MAX
; i
++) {
1188 pp
->mq_prob
[i
] = atof(optval
);
1189 optval
+= strcspn(optval
, "%");
1190 if (*optval
) optval
++;
1192 } else if (!strcasecmp(optname
, "pat3gammas") && optval
) {
1193 /* PAT3_N %-separated floating point values */
1194 for (int i
= 0; *optval
&& i
< PAT3_N
; i
++) {
1195 pp
->pat3_gammas
[i
] = atof(optval
);
1196 optval
+= strcspn(optval
, "%");
1197 if (*optval
) optval
++;
1199 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
1200 pp
->tenuki_prob
= atof(optval
);
1202 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
1207 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
1208 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
1209 if (pp
->nlibrate
== -1U) pp
->nlibrate
= rate
;
1210 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
1211 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
1212 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
1213 if (pp
->eyefillrate
== -1U) pp
->eyefillrate
= rate
;
1214 if (pp
->korate
== -1U) pp
->korate
= rate
;
1215 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
1216 if (pp
->ladderrate
== -1U) pp
->ladderrate
= rate
;
1217 if (pp
->nakaderate
== -1U) pp
->nakaderate
= rate
;
1218 if (pp
->eyefixrate
== -1U) pp
->eyefixrate
= rate
;
1219 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
1221 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);