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
;
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 */
106 /* hane pattern - non-cutting hane */
110 /* hane pattern - magari */
114 /* hane pattern - thin hane */
118 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
122 /* cut1 pattern (kiri) - unprotected cut */
126 /* cut1 pattern (kiri) - peeped cut */
130 /* cut2 pattern (de) */
134 /* cut keima (not in Mogo) */
137 "???", /* o?? has some pathological tsumego cases */
138 /* side pattern - chase */
142 /* side pattern - block side cut */
146 /* side pattern - block side connection */
150 /* side pattern - sagari (SUSPICIOUS) */
152 "x.x" /* Mogo has "x.?" */
153 "###" /* Mogo has "X" */,
154 /* side pattern - throw-in (SUSPICIOUS) */
160 /* side pattern - cut (SUSPICIOUS) */
163 "###" /* Mogo has "X" */,
164 /* side pattern - eye piercing:
169 /* side pattern - make eye */
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 fillboard_check(struct playout_policy
*p
, struct board
*b
)
454 struct moggy_policy
*pp
= p
->data
;
455 unsigned int fbtries
= b
->flen
/ 8;
456 if (pp
->fillboardtries
< fbtries
)
457 fbtries
= pp
->fillboardtries
;
459 for (unsigned int i
= 0; i
< fbtries
; i
++) {
460 coord_t coord
= b
->f
[fast_random(b
->flen
)];
461 if (immediate_liberty_count(b
, coord
) != 4)
463 foreach_diag_neighbor(b
, coord
) {
464 if (board_at(b
, c
) != S_NONE
)
466 } foreach_diag_neighbor_end
;
475 playout_moggy_seqchoose(struct playout_policy
*p
, struct playout_setup
*s
, struct board
*b
, enum stone to_play
)
477 struct moggy_policy
*pp
= p
->data
;
480 board_print(b
, stderr
);
483 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
484 && b
->moves
- b
->last_ko_age
< pp
->koage
485 && pp
->korate
> fast_random(100)) {
486 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
487 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
488 return b
->last_ko
.coord
;
492 if (!is_pass(b
->last_move
.coord
)) {
494 if (pp
->nakaderate
> fast_random(100)
495 && immediate_liberty_count(b
, b
->last_move
.coord
) > 0) {
496 coord_t nakade
= nakade_check(p
, b
, &b
->last_move
, to_play
);
497 if (!is_pass(nakade
))
501 /* Local group in atari? */
502 if (pp
->lcapturerate
> fast_random(100)) {
503 struct move_queue q
; q
.moves
= 0;
504 local_atari_check(p
, b
, &b
->last_move
, &q
);
509 /* Local group trying to escape ladder? */
510 if (pp
->ladderrate
> fast_random(100)) {
511 struct move_queue q
; q
.moves
= 0;
512 local_ladder_check(p
, b
, &b
->last_move
, &q
);
517 /* Local group can be PUT in atari? */
518 if (pp
->atarirate
> fast_random(100)) {
519 struct move_queue q
; q
.moves
= 0;
520 local_2lib_check(p
, b
, &b
->last_move
, &q
);
525 /* Local group reduced some of our groups to 3 libs? */
526 if (pp
->nlibrate
> fast_random(100)) {
527 struct move_queue q
; q
.moves
= 0;
528 local_nlib_check(p
, b
, &b
->last_move
, &q
);
533 /* Check for patterns we know */
534 if (pp
->patternrate
> fast_random(100)) {
535 struct move_queue q
; q
.moves
= 0;
537 apply_pattern(p
, b
, &b
->last_move
,
538 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
541 return mq_gamma_pick(&q
, gammas
);
547 /* Any groups in atari? */
548 if (pp
->capturerate
> fast_random(100)) {
549 struct move_queue q
; q
.moves
= 0;
550 global_atari_check(p
, b
, to_play
, &q
);
556 if (pp
->josekirate
> fast_random(100)) {
557 struct move_queue q
; q
.moves
= 0;
558 joseki_check(p
, b
, to_play
, &q
);
564 if (pp
->fillboardtries
> 0) {
565 coord_t c
= fillboard_check(p
, b
);
573 /* Pick a move from queue q, giving different likelihoods to moves
574 * based on their tags. */
576 mq_tagged_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
578 struct moggy_policy
*pp
= p
->data
;
580 /* First, merge all entries for a move. */
581 /* We use a naive O(N^2) since the average length of the queue
583 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
584 for (unsigned int j
= i
+ 1; j
< q
->moves
; j
++) {
585 if (q
->move
[i
] != q
->move
[j
])
587 q
->tag
[i
] |= q
->tag
[j
];
589 q
->tag
[j
] = q
->tag
[q
->moves
];
590 q
->move
[j
] = q
->move
[q
->moves
];
594 /* Now, construct a probdist. */
597 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
599 assert(q
->tag
[i
] != 0);
600 for (int j
= 0; j
< MQ_MAX
; j
++)
601 if (q
->tag
[i
] & (1<<j
)) {
602 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
603 val
*= pp
->mq_prob
[j
];
605 pd
[i
] = double_to_fixp(val
);
608 total
+= double_to_fixp(pp
->tenuki_prob
);
610 /* Finally, pick a move! */
611 fixp_t stab
= fast_irandom(total
);
613 fprintf(stderr
, "Pick (total %.3f stab %.3f): ", fixp_to_double(total
), fixp_to_double(stab
));
614 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
615 fprintf(stderr
, "%s(%x:%.3f) ", coord2sstr(q
->move
[i
], b
), q
->tag
[i
], fixp_to_double(pd
[i
]));
617 fprintf(stderr
, "\n");
619 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
620 //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));
627 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
632 playout_moggy_fullchoose(struct playout_policy
*p
, struct playout_setup
*s
, struct board
*b
, enum stone to_play
)
634 struct moggy_policy
*pp
= p
->data
;
635 struct move_queue q
; q
.moves
= 0;
638 board_print(b
, stderr
);
641 if (pp
->korate
> 0 && !is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
642 && b
->moves
- b
->last_ko_age
< pp
->koage
) {
643 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
644 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
645 mq_add(&q
, b
->last_ko
.coord
, 1<<MQ_KO
);
649 if (!is_pass(b
->last_move
.coord
)) {
651 if (pp
->nakaderate
> 0 && immediate_liberty_count(b
, b
->last_move
.coord
) > 0) {
652 coord_t nakade
= nakade_check(p
, b
, &b
->last_move
, to_play
);
653 if (!is_pass(nakade
))
654 mq_add(&q
, nakade
, 1<<MQ_NAKADE
);
657 /* Local group in atari? */
658 if (pp
->lcapturerate
> 0)
659 local_atari_check(p
, b
, &b
->last_move
, &q
);
661 /* Local group trying to escape ladder? */
662 if (pp
->ladderrate
> 0)
663 local_ladder_check(p
, b
, &b
->last_move
, &q
);
665 /* Local group can be PUT in atari? */
666 if (pp
->atarirate
> 0)
667 local_2lib_check(p
, b
, &b
->last_move
, &q
);
669 /* Local group reduced some of our groups to 3 libs? */
670 if (pp
->nlibrate
> 0)
671 local_nlib_check(p
, b
, &b
->last_move
, &q
);
673 /* Check for patterns we know */
674 if (pp
->patternrate
> 0) {
676 apply_pattern(p
, b
, &b
->last_move
,
677 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
679 /* FIXME: Use the gammas. */
685 /* Any groups in atari? */
686 if (pp
->capturerate
> 0)
687 global_atari_check(p
, b
, to_play
, &q
);
690 if (pp
->josekirate
> 0)
691 joseki_check(p
, b
, to_play
, &q
);
694 /* Average length of the queue is 1.4 move. */
695 printf("MQL %d ", q
.moves
);
696 for (unsigned int i
= 0; i
< q
.moves
; i
++)
697 printf("%s ", coord2sstr(q
.move
[i
], b
));
702 return mq_tagged_choose(p
, b
, to_play
, &q
);
705 if (pp
->fillboardtries
> 0) {
706 coord_t c
= fillboard_check(p
, b
);
716 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
718 struct moggy_policy
*pp
= p
->data
;
719 struct board
*b
= map
->b
;
720 struct move_queue q
; q
.moves
= 0;
722 if (board_group_info(b
, g
).libs
> pp
->nlib_count
)
726 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
727 board_print(b
, stderr
);
730 if (board_group_info(b
, g
).libs
> 2) {
733 if (board_at(b
, g
) != map
->to_play
)
734 return; // we do only defense
735 group_nlib_defense_check(b
, g
, map
->to_play
, &q
, 0);
737 coord_t coord
= q
.move
[q
.moves
];
739 fprintf(stderr
, "1.0: nlib %s\n", coord2sstr(coord
, b
));
740 int assess
= games
/ 2;
741 add_prior_value(map
, coord
, 1, assess
);
746 if (board_group_info(b
, g
).libs
== 2) {
747 if (pp
->ladderrate
) {
748 /* Make sure to play the correct liberty in case
749 * this is a group that can be caught in a ladder. */
750 bool ladderable
= false;
751 for (int i
= 0; i
< 2; i
++) {
752 coord_t chase
= board_group_info(b
, g
).lib
[i
];
753 coord_t escape
= board_group_info(b
, g
).lib
[1 - i
];
754 if (wouldbe_ladder(b
, g
, escape
, chase
, board_at(b
, g
))) {
755 add_prior_value(map
, chase
, 1, games
);
760 return; // do not suggest the other lib at all
765 group_2lib_check(b
, g
, map
->to_play
, &q
, 0, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
767 coord_t coord
= q
.move
[q
.moves
];
769 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
770 int assess
= games
/ 2;
771 add_prior_value(map
, coord
, 1, assess
);
776 /* This group, sir, is in atari! */
778 coord_t ladder
= pass
;
779 group_atari_check(pp
->alwaysccaprate
, b
, g
, map
->to_play
, &q
, &ladder
, true, 0);
781 coord_t coord
= q
.move
[q
.moves
];
783 /* _Never_ play here if this move plays out
784 * a caught ladder. */
785 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
786 /* Note that the opposite is not guarded against;
787 * we do not advise against capturing a laddered
788 * group (but we don't encourage it either). Such
789 * a move can simplify tactical situations if we
791 if (map
->to_play
!= board_at(b
, g
))
793 /* FIXME: We give the malus even if this move
794 * captures another group. */
796 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
797 add_prior_value(map
, coord
, 0, games
);
801 if (!pp
->capturerate
&& !pp
->lcapturerate
)
804 int assess
= games
* 2;
805 if (pp
->cap_stone_denom
> 0) {
806 int stones
= group_stone_count(b
, g
, pp
->cap_stone_max
) - (pp
->cap_stone_min
-1);
807 assess
+= (stones
> 0 ? stones
: 0) * games
* 100 / pp
->cap_stone_denom
;
810 fprintf(stderr
, "1.0 (%d): atari %s\n", assess
, coord2sstr(coord
, b
));
811 add_prior_value(map
, coord
, 1, assess
);
816 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
818 struct moggy_policy
*pp
= p
->data
;
819 struct board
*b
= map
->b
;
822 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
823 board_print(b
, stderr
);
826 /* Is this move a self-atari? */
827 if (pp
->selfatarirate
) {
828 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
830 fprintf(stderr
, "0.0: self-atari\n");
831 add_prior_value(map
, coord
, 0, games
);
832 if (!pp
->selfatari_other
)
834 /* If we can play on the other liberty of the
835 * endangered group, do! */
836 coord
= selfatari_cousin(b
, map
->to_play
, coord
, NULL
);
840 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
841 add_prior_value(map
, coord
, 1.0, games
);
847 if (pp
->patternrate
) {
848 // XXX: Use gamma value?
849 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
850 if (test_pattern3_here(p
, b
, &m
, true, NULL
)) {
852 fprintf(stderr
, "1.0: pattern\n");
853 add_prior_value(map
, coord
, 1, games
);
861 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
863 struct moggy_policy
*pp
= p
->data
;
865 /* First, go through all endangered groups. */
866 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
867 if (group_at(map
->b
, g
) == g
)
868 playout_moggy_assess_group(p
, map
, g
, games
);
870 /* Then, assess individual moves. */
871 if (!pp
->patternrate
&& !pp
->selfatarirate
)
873 foreach_free_point(map
->b
) {
874 if (map
->consider
[c
])
875 playout_moggy_assess_one(p
, map
, c
, games
);
876 } foreach_free_point_end
;
880 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
882 struct moggy_policy
*pp
= p
->data
;
884 /* The idea is simple for now - never allow self-atari moves.
885 * They suck in general, but this also permits us to actually
886 * handle seki in the playout stage. */
888 if (fast_random(100) >= pp
->selfatarirate
) {
890 fprintf(stderr
, "skipping sar test\n");
893 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
896 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
897 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
898 if (pp
->selfatari_other
) {
899 /* Ok, try the other liberty of the atari'd group. */
900 coord_t c
= selfatari_cousin(b
, m
->color
, m
->coord
, NULL
);
901 if (is_pass(c
)) return false;
903 fprintf(stderr
, "___ Redirecting to other lib %s\n",
912 /* Check if we don't seem to be filling our eye. This should
913 * happen only for false eyes, but some of them are in fact
914 * real eyes with diagonal filled by a dead stone. Prefer
915 * to counter-capture in that case. */
916 if (fast_random(100) >= pp
->eyefillrate
) {
918 fprintf(stderr
, "skipping eyefill test\n");
921 bool eyefill
= board_is_eyelike(b
, m
->coord
, m
->color
);
923 foreach_diag_neighbor(b
, m
->coord
) {
924 if (board_at(b
, c
) != stone_other(m
->color
))
926 switch (board_group_info(b
, group_at(b
, c
)).libs
) {
927 case 1: /* Capture! */
928 c
= board_group_info(b
, group_at(b
, c
)).lib
[0];
930 fprintf(stderr
, "___ Redirecting to capture %s\n",
934 case 2: /* Try to switch to some 2-lib neighbor. */
935 for (int i
= 0; i
< 2; i
++) {
936 coord_t l
= board_group_info(b
, group_at(b
, c
)).lib
[i
];
937 if (board_is_one_point_eye(b
, l
, board_at(b
, c
)))
939 if (is_bad_selfatari(b
, m
->color
, l
))
946 } foreach_diag_neighbor_end
;
954 struct playout_policy
*
955 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
957 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
958 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
960 p
->choose
= playout_moggy_seqchoose
;
961 p
->assess
= playout_moggy_assess
;
962 p
->permit
= playout_moggy_permit
;
966 /* These settings are tuned for 19x19 play with several threads
967 * on reasonable time limits (i.e., rather large number of playouts).
968 * XXX: no 9x9 tuning has been done recently. */
969 int rate
= board_large(b
) ? 80 : 90;
971 pp
->lcapturerate
= pp
->atarirate
= pp
->nlibrate
972 = pp
->selfatarirate
= pp
->josekirate
= -1U;
973 pp
->patternrate
= 100;
977 pp
->lcapturerate
= 90;
978 pp
->korate
= 20; pp
->koage
= 4;
979 pp
->alwaysccaprate
= 40;
980 pp
->eyefillrate
= 60;
981 pp
->selfatari_other
= true;
983 pp
->cap_stone_min
= 2;
984 pp
->cap_stone_max
= 15;
985 pp
->cap_stone_denom
= 200;
987 pp
->atari_def_no_hopeless
= !board_large(b
);
988 pp
->atari_miaisafe
= true;
992 double mq_prob_default
[MQ_MAX
] = {
1002 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
1004 /* By default, 3x3 pattern gammas are all equal. */
1005 for (int i
= 0; i
< PAT3_N
; i
++)
1006 pp
->pat3_gammas
[i
] = 1.0;
1009 char *optspec
, *next
= arg
;
1012 next
+= strcspn(next
, ":");
1013 if (*next
) { *next
++ = 0; } else { *next
= 0; }
1015 char *optname
= optspec
;
1016 char *optval
= strchr(optspec
, '=');
1017 if (optval
) *optval
++ = 0;
1019 if (!strcasecmp(optname
, "debug") && optval
) {
1020 p
->debug_level
= atoi(optval
);
1021 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
1022 pp
->lcapturerate
= atoi(optval
);
1023 } else if (!strcasecmp(optname
, "ladderrate") && optval
) {
1024 pp
->ladderrate
= atoi(optval
);
1025 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
1026 pp
->atarirate
= atoi(optval
);
1027 } else if (!strcasecmp(optname
, "nlibrate") && optval
) {
1028 pp
->nlibrate
= atoi(optval
);
1029 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
1030 pp
->capturerate
= atoi(optval
);
1031 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
1032 pp
->patternrate
= atoi(optval
);
1033 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
1034 pp
->selfatarirate
= atoi(optval
);
1035 } else if (!strcasecmp(optname
, "eyefillrate") && optval
) {
1036 pp
->eyefillrate
= atoi(optval
);
1037 } else if (!strcasecmp(optname
, "korate") && optval
) {
1038 pp
->korate
= atoi(optval
);
1039 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
1040 pp
->josekirate
= atoi(optval
);
1041 } else if (!strcasecmp(optname
, "nakaderate") && optval
) {
1042 pp
->nakaderate
= atoi(optval
);
1043 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
1044 pp
->alwaysccaprate
= atoi(optval
);
1045 } else if (!strcasecmp(optname
, "rate") && optval
) {
1046 rate
= atoi(optval
);
1047 } else if (!strcasecmp(optname
, "fillboardtries")) {
1048 pp
->fillboardtries
= atoi(optval
);
1049 } else if (!strcasecmp(optname
, "koage") && optval
) {
1050 pp
->koage
= atoi(optval
);
1051 } else if (!strcasecmp(optname
, "pattern2")) {
1052 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
1053 } else if (!strcasecmp(optname
, "selfatari_other")) {
1054 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
1055 } else if (!strcasecmp(optname
, "capcheckall")) {
1056 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
1057 } else if (!strcasecmp(optname
, "cap_stone_min") && optval
) {
1058 pp
->cap_stone_min
= atoi(optval
);
1059 } else if (!strcasecmp(optname
, "cap_stone_max") && optval
) {
1060 pp
->cap_stone_max
= atoi(optval
);
1061 } else if (!strcasecmp(optname
, "cap_stone_denom") && optval
) {
1062 pp
->cap_stone_denom
= atoi(optval
);
1063 } else if (!strcasecmp(optname
, "atari_miaisafe")) {
1064 pp
->atari_miaisafe
= optval
&& *optval
== '0' ? false : true;
1065 } else if (!strcasecmp(optname
, "atari_def_no_hopeless")) {
1066 pp
->atari_def_no_hopeless
= optval
&& *optval
== '0' ? false : true;
1067 } else if (!strcasecmp(optname
, "nlib_count") && optval
) {
1068 pp
->nlib_count
= atoi(optval
);
1069 } else if (!strcasecmp(optname
, "middle_ladder")) {
1070 pp
->middle_ladder
= optval
&& *optval
== '0' ? false : true;
1071 } else if (!strcasecmp(optname
, "fullchoose")) {
1072 pp
->fullchoose
= true;
1073 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_seqchoose
: playout_moggy_fullchoose
;
1074 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
1075 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
1076 for (int i
= 0; *optval
&& i
< MQ_MAX
; i
++) {
1077 pp
->mq_prob
[i
] = atof(optval
);
1078 optval
+= strcspn(optval
, "%");
1079 if (*optval
) optval
++;
1081 } else if (!strcasecmp(optname
, "pat3gammas") && optval
) {
1082 /* PAT3_N %-separated floating point values */
1083 for (int i
= 0; *optval
&& i
< PAT3_N
; i
++) {
1084 pp
->pat3_gammas
[i
] = atof(optval
);
1085 optval
+= strcspn(optval
, "%");
1086 if (*optval
) optval
++;
1088 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
1089 pp
->tenuki_prob
= atof(optval
);
1091 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
1096 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
1097 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
1098 if (pp
->nlibrate
== -1U) pp
->nlibrate
= rate
;
1099 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
1100 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
1101 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
1102 if (pp
->eyefillrate
== -1U) pp
->eyefillrate
= rate
;
1103 if (pp
->korate
== -1U) pp
->korate
= rate
;
1104 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
1105 if (pp
->ladderrate
== -1U) pp
->ladderrate
= rate
;
1106 if (pp
->nakaderate
== -1U) pp
->nakaderate
= rate
;
1107 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
1109 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);