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! */
54 /* Note that the context can be shared by multiple threads! */
57 unsigned int lcapturerate
, atarirate
, nlibrate
, ladderrate
, capturerate
, patternrate
, korate
, josekirate
, nakaderate
;
58 unsigned int selfatarirate
, eyefillrate
, alwaysccaprate
;
59 unsigned int fillboardtries
;
61 /* Whether to look for patterns around second-to-last move. */
63 /* Whether, when self-atari attempt is detected, to play the other
64 * group's liberty if that is non-self-atari. */
66 /* Whether to read out ladders elsewhere than near the board
71 /* Whether to always pick from moves capturing all groups in
72 * global_atari_check(). */
74 /* Prior stone weighting. Weight of each stone between
75 * cap_stone_min and cap_stone_max is (assess*100)/cap_stone_denom. */
76 int cap_stone_min
, cap_stone_max
;
80 bool atari_def_no_hopeless
;
86 struct joseki_dict
*jdict
;
87 struct pattern3s patterns
;
89 /* Gamma values for queue tags - correspond to probabilities. */
91 double mq_prob
[MQ_MAX
], tenuki_prob
;
95 static char moggy_patterns_src
[][11] = {
96 /* hane pattern - enclosing hane */
100 /* hane pattern - non-cutting hane */
104 /* hane pattern - magari */
108 /* hane pattern - thin hane */
112 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
116 /* cut1 pattern (kiri) - unprotected cut */
120 /* cut1 pattern (kiri) - peeped cut */
124 /* cut2 pattern (de) */
128 /* cut keima (not in Mogo) */
131 "???", /* o?? has some pathological tsumego cases */
132 /* side pattern - chase */
136 /* side pattern - block side cut */
140 /* side pattern - block side connection */
144 /* side pattern - sagari (SUSPICIOUS) */
146 "x.x" /* Mogo has "x.?" */
147 "###" /* Mogo has "X" */,
148 /* side pattern - throw-in (SUSPICIOUS) */
154 /* side pattern - cut (SUSPICIOUS) */
157 "###" /* Mogo has "X" */,
158 /* side pattern - eye piercing:
169 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
172 test_pattern3_here(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
174 struct moggy_policy
*pp
= p
->data
;
175 /* Check if 3x3 pattern is matched by given move... */
176 if (!pattern3_move_here(&pp
->patterns
, b
, m
))
178 /* ...and the move is not obviously stupid. */
179 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
181 /* Ladder moves are stupid. */
182 group_t atari_neighbor
= board_get_atari_neighbor(b
, m
->coord
, m
->color
);
183 if (atari_neighbor
&& is_ladder(b
, m
->coord
, atari_neighbor
, pp
->middle_ladder
)
184 && !can_countercapture(b
, board_at(b
, group_base(atari_neighbor
)),
185 atari_neighbor
, m
->color
, NULL
, 0))
191 apply_pattern_here(struct playout_policy
*p
, struct board
*b
, coord_t c
, enum stone color
, struct move_queue
*q
)
193 struct move m2
= { .coord
= c
, .color
= color
};
194 if (board_is_valid_move(b
, &m2
) && test_pattern3_here(p
, b
, &m2
))
195 mq_add(q
, c
, 1<<MQ_PAT3
);
198 /* Check if we match any pattern around given move (with the other color to play). */
200 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
, struct move_queue
*q
)
202 /* Suicides do not make any patterns and confuse us. */
203 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
206 foreach_8neighbor(b
, m
->coord
) {
207 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
208 } foreach_8neighbor_end
;
210 if (mm
) { /* Second move for pattern searching */
211 foreach_8neighbor(b
, mm
->coord
) {
212 if (coord_is_8adjecent(m
->coord
, c
, b
))
214 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
215 } foreach_8neighbor_end
;
219 mq_print(q
, b
, "Pattern");
224 joseki_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
226 struct moggy_policy
*pp
= p
->data
;
230 for (int i
= 0; i
< 4; i
++) {
231 hash_t h
= b
->qhash
[i
] & joseki_hash_mask
;
232 coord_t
*cc
= pp
->jdict
->patterns
[h
].moves
[to_play
];
234 for (; !is_pass(*cc
); cc
++) {
235 if (coord_quadrant(*cc
, b
) != i
)
237 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
241 if (q
->moves
> 0 && PLDEBUGL(5))
242 mq_print(q
, b
, "Joseki");
246 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
251 struct moggy_policy
*pp
= p
->data
;
252 if (pp
->capcheckall
) {
253 for (int g
= 0; g
< b
->clen
; g
++)
254 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, pp
->middle_ladder
, 1<<MQ_GATARI
);
256 mq_print(q
, b
, "Global atari");
260 int g_base
= fast_random(b
->clen
);
261 for (int g
= g_base
; g
< b
->clen
; g
++) {
262 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, pp
->middle_ladder
, 1<<MQ_GATARI
);
264 /* XXX: Try carrying on. */
266 mq_print(q
, b
, "Global atari");
270 for (int g
= 0; g
< g_base
; g
++) {
271 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, pp
->middle_ladder
, 1<<MQ_GATARI
);
273 /* XXX: Try carrying on. */
275 mq_print(q
, b
, "Global atari");
283 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
285 struct moggy_policy
*pp
= p
->data
;
287 /* Did the opponent play a self-atari? */
288 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
289 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
, NULL
, pp
->middle_ladder
, 1<<MQ_LATARI
);
292 foreach_neighbor(b
, m
->coord
, {
293 group_t g
= group_at(b
, c
);
294 if (!g
|| board_group_info(b
, g
).libs
!= 1)
296 group_atari_check(pp
->alwaysccaprate
, b
, g
, stone_other(m
->color
), q
, NULL
, pp
->middle_ladder
, 1<<MQ_LATARI
);
300 mq_print(q
, b
, "Local atari");
305 local_ladder_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
307 group_t group
= group_at(b
, m
->coord
);
309 if (board_group_info(b
, group
).libs
!= 2)
312 for (int i
= 0; i
< 2; i
++) {
313 coord_t chase
= board_group_info(b
, group
).lib
[i
];
314 coord_t escape
= board_group_info(b
, group
).lib
[1 - i
];
315 if (wouldbe_ladder(b
, escape
, chase
, board_at(b
, group
)))
316 mq_add(q
, chase
, 1<<MQ_LADDER
);
319 if (q
->moves
> 0 && PLDEBUGL(5))
320 mq_print(q
, b
, "Ladder");
325 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
327 struct moggy_policy
*pp
= p
->data
;
328 group_t group
= group_at(b
, m
->coord
), group2
= 0;
330 /* Does the opponent have just two liberties? */
331 if (board_group_info(b
, group
).libs
== 2) {
332 group_2lib_check(b
, group
, stone_other(m
->color
), q
, 1<<MQ_L2LIB
, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
334 /* We always prefer to take off an enemy chain liberty
335 * before pulling out ourselves. */
336 /* XXX: We aren't guaranteed to return to that group
339 return q
->move
[fast_random(q
->moves
)];
343 /* Then he took a third liberty from neighboring chain? */
344 foreach_neighbor(b
, m
->coord
, {
345 group_t g
= group_at(b
, c
);
346 if (!g
|| g
== group
|| g
== group2
|| board_group_info(b
, g
).libs
!= 2)
348 group_2lib_check(b
, g
, stone_other(m
->color
), q
, 1<<MQ_L2LIB
, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
349 group2
= g
; // prevent trivial repeated checks
353 mq_print(q
, b
, "Local 2lib");
357 local_nlib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
359 struct moggy_policy
*pp
= p
->data
;
360 enum stone color
= stone_other(m
->color
);
362 /* Attacking N-liberty groups in general is probably
363 * not feasible. What we are primarily concerned about is
364 * counter-attacking groups that have two physical liberties,
365 * but three effective liberties:
374 * The time for this to come is when the opponent took a liberty
375 * of ours, making a few-liberty group. Therefore, we focus
378 * There is a tradeoff - down to how many liberties we need to
379 * be to start looking? nlib_count=3 will work for the left black
380 * group (2lib-solver will suggest connecting the false eye), but
381 * not for top black group (it is too late to start playing 3-3
382 * capturing race). Also, we cannot prevent stupidly taking an
383 * outside liberty ourselves; the higher nlib_count, the higher
384 * the chance we withstand this.
386 * However, higher nlib_count means that we will waste more time
387 * checking non-urgent or alive groups, and we will play silly
388 * or wasted moves around alive groups. */
391 foreach_8neighbor(b
, m
->coord
) {
392 group_t g
= group_at(b
, c
);
393 if (!g
|| group2
== g
|| board_at(b
, c
) != color
)
395 if (board_group_info(b
, g
).libs
< 3 || board_group_info(b
, g
).libs
> pp
->nlib_count
)
397 group_nlib_defense_check(b
, g
, color
, q
, 1<<MQ_LNLIB
);
398 group2
= g
; // prevent trivial repeated checks
399 } foreach_8neighbor_end
;
402 mq_print(q
, b
, "Local nlib");
406 nakade_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, enum stone to_play
)
408 coord_t empty
= pass
;
409 foreach_neighbor(b
, m
->coord
, {
410 if (board_at(b
, c
) != S_NONE
)
412 if (is_pass(empty
)) {
416 if (!coord_is_8adjecent(c
, empty
, b
)) {
417 /* Seems like impossible nakade
422 assert(!is_pass(empty
));
424 coord_t nakade
= nakade_point(b
, empty
, stone_other(to_play
));
425 if (PLDEBUGL(5) && !is_pass(nakade
))
426 fprintf(stderr
, "Nakade: %s\n", coord2sstr(nakade
, b
));
431 fillboard_check(struct playout_policy
*p
, struct board
*b
)
433 struct moggy_policy
*pp
= p
->data
;
434 unsigned int fbtries
= b
->flen
/ 8;
435 if (pp
->fillboardtries
< fbtries
)
436 fbtries
= pp
->fillboardtries
;
438 for (unsigned int i
= 0; i
< fbtries
; i
++) {
439 coord_t coord
= b
->f
[fast_random(b
->flen
)];
440 if (immediate_liberty_count(b
, coord
) != 4)
442 foreach_diag_neighbor(b
, coord
) {
443 if (board_at(b
, c
) != S_NONE
)
445 } foreach_diag_neighbor_end
;
454 playout_moggy_seqchoose(struct playout_policy
*p
, struct playout_setup
*s
, struct board
*b
, enum stone to_play
)
456 struct moggy_policy
*pp
= p
->data
;
459 board_print(b
, stderr
);
462 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
463 && b
->moves
- b
->last_ko_age
< pp
->koage
464 && pp
->korate
> fast_random(100)) {
465 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
466 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
467 return b
->last_ko
.coord
;
471 if (!is_pass(b
->last_move
.coord
)) {
473 if (pp
->nakaderate
> fast_random(100)
474 && immediate_liberty_count(b
, b
->last_move
.coord
) > 0) {
475 coord_t nakade
= nakade_check(p
, b
, &b
->last_move
, to_play
);
476 if (!is_pass(nakade
))
480 /* Local group in atari? */
481 if (pp
->lcapturerate
> fast_random(100)) {
482 struct move_queue q
; q
.moves
= 0;
483 local_atari_check(p
, b
, &b
->last_move
, &q
);
488 /* Local group trying to escape ladder? */
489 if (pp
->ladderrate
> fast_random(100)) {
490 struct move_queue q
; q
.moves
= 0;
491 local_ladder_check(p
, b
, &b
->last_move
, &q
);
496 /* Local group can be PUT in atari? */
497 if (pp
->atarirate
> fast_random(100)) {
498 struct move_queue q
; q
.moves
= 0;
499 local_2lib_check(p
, b
, &b
->last_move
, &q
);
504 /* Local group reduced some of our groups to 3 libs? */
505 if (pp
->nlibrate
> fast_random(100)) {
506 struct move_queue q
; q
.moves
= 0;
507 local_nlib_check(p
, b
, &b
->last_move
, &q
);
512 /* Check for patterns we know */
513 if (pp
->patternrate
> fast_random(100)) {
514 struct move_queue q
; q
.moves
= 0;
515 apply_pattern(p
, b
, &b
->last_move
,
516 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
525 /* Any groups in atari? */
526 if (pp
->capturerate
> fast_random(100)) {
527 struct move_queue q
; q
.moves
= 0;
528 global_atari_check(p
, b
, to_play
, &q
);
534 if (pp
->josekirate
> fast_random(100)) {
535 struct move_queue q
; q
.moves
= 0;
536 joseki_check(p
, b
, to_play
, &q
);
542 if (pp
->fillboardtries
> 0) {
543 coord_t c
= fillboard_check(p
, b
);
551 /* Pick a move from queue q, giving different likelihoods to moves
552 * based on their tags. */
554 mq_tagged_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
556 struct moggy_policy
*pp
= p
->data
;
558 /* First, merge all entries for a move. */
559 /* We use a naive O(N^2) since the average length of the queue
561 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
562 for (unsigned int j
= i
+ 1; j
< q
->moves
; j
++) {
563 if (q
->move
[i
] != q
->move
[j
])
565 q
->tag
[i
] |= q
->tag
[j
];
567 q
->tag
[j
] = q
->tag
[q
->moves
];
568 q
->move
[j
] = q
->move
[q
->moves
];
572 /* Now, construct a probdist. */
575 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
577 assert(q
->tag
[i
] != 0);
578 for (int j
= 0; j
< MQ_MAX
; j
++)
579 if (q
->tag
[i
] & (1<<j
)) {
580 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
581 val
*= pp
->mq_prob
[j
];
583 pd
[i
] = double_to_fixp(val
);
586 total
+= double_to_fixp(pp
->tenuki_prob
);
588 /* Finally, pick a move! */
589 fixp_t stab
= fast_irandom(total
);
590 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
591 //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));
598 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
603 playout_moggy_fullchoose(struct playout_policy
*p
, struct playout_setup
*s
, struct board
*b
, enum stone to_play
)
605 struct moggy_policy
*pp
= p
->data
;
606 struct move_queue q
; q
.moves
= 0;
609 board_print(b
, stderr
);
612 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
613 && b
->moves
- b
->last_ko_age
< pp
->koage
) {
614 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
615 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
616 mq_add(&q
, b
->last_ko
.coord
, 1<<MQ_KO
);
620 if (!is_pass(b
->last_move
.coord
)) {
622 if (immediate_liberty_count(b
, b
->last_move
.coord
) > 0) {
623 coord_t nakade
= nakade_check(p
, b
, &b
->last_move
, to_play
);
624 if (!is_pass(nakade
))
625 mq_add(&q
, nakade
, 1<<MQ_NAKADE
);
628 /* Local group in atari? */
629 local_atari_check(p
, b
, &b
->last_move
, &q
);
631 /* Local group trying to escape ladder? */
632 local_ladder_check(p
, b
, &b
->last_move
, &q
);
634 /* Local group can be PUT in atari? */
635 local_2lib_check(p
, b
, &b
->last_move
, &q
);
637 /* Local group reduced some of our groups to 3 libs? */
638 local_nlib_check(p
, b
, &b
->last_move
, &q
);
640 /* Check for patterns we know */
641 apply_pattern(p
, b
, &b
->last_move
,
642 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
648 /* Any groups in atari? */
649 global_atari_check(p
, b
, to_play
, &q
);
652 joseki_check(p
, b
, to_play
, &q
);
655 /* Average length of the queue is 1.4 move. */
656 printf("MQL %d ", q
.moves
);
657 for (unsigned int i
= 0; i
< q
.moves
; i
++)
658 printf("%s ", coord2sstr(q
.move
[i
], b
));
663 return mq_tagged_choose(p
, b
, to_play
, &q
);
666 if (pp
->fillboardtries
> 0) {
667 coord_t c
= fillboard_check(p
, b
);
677 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
679 struct moggy_policy
*pp
= p
->data
;
680 struct board
*b
= map
->b
;
681 struct move_queue q
; q
.moves
= 0;
683 if (board_group_info(b
, g
).libs
> pp
->nlib_count
)
687 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
688 board_print(b
, stderr
);
691 if (board_group_info(b
, g
).libs
> 2) {
694 if (board_at(b
, g
) != map
->to_play
)
695 return; // we do only defense
696 group_nlib_defense_check(b
, g
, map
->to_play
, &q
, 0);
698 coord_t coord
= q
.move
[q
.moves
];
700 fprintf(stderr
, "1.0: nlib %s\n", coord2sstr(coord
, b
));
701 int assess
= games
/ 2;
702 add_prior_value(map
, coord
, 1, assess
);
707 if (board_group_info(b
, g
).libs
== 2) {
708 if (pp
->ladderrate
) {
709 /* Make sure to play the correct liberty in case
710 * this is a group that can be caught in a ladder. */
711 bool ladderable
= false;
712 for (int i
= 0; i
< 2; i
++) {
713 coord_t chase
= board_group_info(b
, g
).lib
[i
];
714 coord_t escape
= board_group_info(b
, g
).lib
[1 - i
];
715 if (wouldbe_ladder(b
, escape
, chase
, board_at(b
, g
))) {
716 add_prior_value(map
, chase
, 1, games
);
721 return; // do not suggest the other lib at all
726 group_2lib_check(b
, g
, map
->to_play
, &q
, 0, pp
->atari_miaisafe
, pp
->atari_def_no_hopeless
);
728 coord_t coord
= q
.move
[q
.moves
];
730 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
731 int assess
= games
/ 2;
732 add_prior_value(map
, coord
, 1, assess
);
737 /* This group, sir, is in atari! */
739 coord_t ladder
= pass
;
740 group_atari_check(pp
->alwaysccaprate
, b
, g
, map
->to_play
, &q
, &ladder
, true, 0);
742 coord_t coord
= q
.move
[q
.moves
];
744 /* _Never_ play here if this move plays out
745 * a caught ladder. */
746 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
747 /* Note that the opposite is not guarded against;
748 * we do not advise against capturing a laddered
749 * group (but we don't encourage it either). Such
750 * a move can simplify tactical situations if we
752 if (map
->to_play
!= board_at(b
, g
))
754 /* FIXME: We give the malus even if this move
755 * captures another group. */
757 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
758 add_prior_value(map
, coord
, 0, games
);
762 if (!pp
->capturerate
&& !pp
->lcapturerate
)
765 int assess
= games
* 2;
766 if (pp
->cap_stone_denom
> 0) {
767 int stones
= group_stone_count(b
, g
, pp
->cap_stone_max
) - (pp
->cap_stone_min
-1);
768 assess
+= (stones
> 0 ? stones
: 0) * games
* 100 / pp
->cap_stone_denom
;
771 fprintf(stderr
, "1.0 (%d): atari %s\n", assess
, coord2sstr(coord
, b
));
772 add_prior_value(map
, coord
, 1, assess
);
777 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
779 struct moggy_policy
*pp
= p
->data
;
780 struct board
*b
= map
->b
;
783 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
784 board_print(b
, stderr
);
787 /* Is this move a self-atari? */
788 if (pp
->selfatarirate
) {
789 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
791 fprintf(stderr
, "0.0: self-atari\n");
792 add_prior_value(map
, coord
, 0, games
);
793 if (!pp
->selfatari_other
)
795 /* If we can play on the other liberty of the
796 * endangered group, do! */
797 coord
= selfatari_cousin(b
, map
->to_play
, coord
, NULL
);
801 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
802 add_prior_value(map
, coord
, 1.0, games
);
808 if (pp
->patternrate
) {
809 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
810 if (test_pattern3_here(p
, b
, &m
)) {
812 fprintf(stderr
, "1.0: pattern\n");
813 add_prior_value(map
, coord
, 1, games
);
821 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
823 struct moggy_policy
*pp
= p
->data
;
825 /* First, go through all endangered groups. */
826 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
827 if (group_at(map
->b
, g
) == g
)
828 playout_moggy_assess_group(p
, map
, g
, games
);
830 /* Then, assess individual moves. */
831 if (!pp
->patternrate
&& !pp
->selfatarirate
)
833 foreach_free_point(map
->b
) {
834 if (map
->consider
[c
])
835 playout_moggy_assess_one(p
, map
, c
, games
);
836 } foreach_free_point_end
;
840 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
842 struct moggy_policy
*pp
= p
->data
;
844 /* The idea is simple for now - never allow self-atari moves.
845 * They suck in general, but this also permits us to actually
846 * handle seki in the playout stage. */
848 if (fast_random(100) >= pp
->selfatarirate
) {
850 fprintf(stderr
, "skipping sar test\n");
853 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
856 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
857 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
858 if (pp
->selfatari_other
) {
859 /* Ok, try the other liberty of the atari'd group. */
860 coord_t c
= selfatari_cousin(b
, m
->color
, m
->coord
, NULL
);
861 if (is_pass(c
)) return false;
863 fprintf(stderr
, "___ Redirecting to other lib %s\n",
872 /* Check if we don't seem to be filling our eye. This should
873 * happen only for false eyes, but some of them are in fact
874 * real eyes with diagonal filled by a dead stone. Prefer
875 * to counter-capture in that case. */
876 if (fast_random(100) >= pp
->eyefillrate
) {
878 fprintf(stderr
, "skipping eyefill test\n");
881 bool eyefill
= board_is_eyelike(b
, m
->coord
, m
->color
);
883 foreach_diag_neighbor(b
, m
->coord
) {
884 if (board_at(b
, c
) != stone_other(m
->color
))
886 switch (board_group_info(b
, group_at(b
, c
)).libs
) {
887 case 1: /* Capture! */
888 c
= board_group_info(b
, group_at(b
, c
)).lib
[0];
890 fprintf(stderr
, "___ Redirecting to capture %s\n",
894 case 2: /* Try to switch to some 2-lib neighbor. */
895 for (int i
= 0; i
< 2; i
++) {
896 coord_t l
= board_group_info(b
, group_at(b
, c
)).lib
[i
];
897 if (board_is_one_point_eye(b
, l
, board_at(b
, c
)))
899 if (is_bad_selfatari(b
, m
->color
, l
))
906 } foreach_diag_neighbor_end
;
914 struct playout_policy
*
915 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
917 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
918 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
920 p
->choose
= playout_moggy_seqchoose
;
921 p
->assess
= playout_moggy_assess
;
922 p
->permit
= playout_moggy_permit
;
926 /* These settings are tuned for 19x19 play with several threads
927 * on reasonable time limits (i.e., rather large number of playouts).
928 * XXX: no 9x9 tuning has been done recently. */
929 int rate
= board_large(b
) ? 80 : 90;
931 pp
->lcapturerate
= pp
->atarirate
= pp
->nlibrate
= pp
->patternrate
932 = pp
->selfatarirate
= pp
->eyefillrate
= pp
->josekirate
= pp
->ladderrate
= -1U;
933 if (board_large(b
)) {
934 pp
->lcapturerate
= 90;
935 pp
->patternrate
= 100;
940 pp
->korate
= 20; pp
->koage
= 4;
941 pp
->alwaysccaprate
= 20;
942 pp
->selfatari_other
= true;
943 pp
->middle_ladder
= true;
945 pp
->cap_stone_min
= 2;
946 pp
->cap_stone_max
= 15;
947 pp
->cap_stone_denom
= 200;
949 pp
->atari_def_no_hopeless
= !board_large(b
);
950 pp
->atari_miaisafe
= true;
954 double mq_prob_default
[MQ_MAX
] = {
964 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
967 char *optspec
, *next
= arg
;
970 next
+= strcspn(next
, ":");
971 if (*next
) { *next
++ = 0; } else { *next
= 0; }
973 char *optname
= optspec
;
974 char *optval
= strchr(optspec
, '=');
975 if (optval
) *optval
++ = 0;
977 if (!strcasecmp(optname
, "debug") && optval
) {
978 p
->debug_level
= atoi(optval
);
979 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
980 pp
->lcapturerate
= atoi(optval
);
981 } else if (!strcasecmp(optname
, "ladderrate") && optval
) {
982 pp
->ladderrate
= atoi(optval
);
983 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
984 pp
->atarirate
= atoi(optval
);
985 } else if (!strcasecmp(optname
, "nlibrate") && optval
) {
986 pp
->nlibrate
= atoi(optval
);
987 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
988 pp
->capturerate
= atoi(optval
);
989 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
990 pp
->patternrate
= atoi(optval
);
991 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
992 pp
->selfatarirate
= atoi(optval
);
993 } else if (!strcasecmp(optname
, "eyefillrate") && optval
) {
994 pp
->eyefillrate
= atoi(optval
);
995 } else if (!strcasecmp(optname
, "korate") && optval
) {
996 pp
->korate
= atoi(optval
);
997 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
998 pp
->josekirate
= atoi(optval
);
999 } else if (!strcasecmp(optname
, "nakaderate") && optval
) {
1000 pp
->nakaderate
= atoi(optval
);
1001 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
1002 pp
->alwaysccaprate
= atoi(optval
);
1003 } else if (!strcasecmp(optname
, "rate") && optval
) {
1004 rate
= atoi(optval
);
1005 } else if (!strcasecmp(optname
, "fillboardtries")) {
1006 pp
->fillboardtries
= atoi(optval
);
1007 } else if (!strcasecmp(optname
, "koage") && optval
) {
1008 pp
->koage
= atoi(optval
);
1009 } else if (!strcasecmp(optname
, "pattern2")) {
1010 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
1011 } else if (!strcasecmp(optname
, "selfatari_other")) {
1012 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
1013 } else if (!strcasecmp(optname
, "capcheckall")) {
1014 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
1015 } else if (!strcasecmp(optname
, "cap_stone_min") && optval
) {
1016 pp
->cap_stone_min
= atoi(optval
);
1017 } else if (!strcasecmp(optname
, "cap_stone_max") && optval
) {
1018 pp
->cap_stone_max
= atoi(optval
);
1019 } else if (!strcasecmp(optname
, "cap_stone_denom") && optval
) {
1020 pp
->cap_stone_denom
= atoi(optval
);
1021 } else if (!strcasecmp(optname
, "atari_miaisafe")) {
1022 pp
->atari_miaisafe
= optval
&& *optval
== '0' ? false : true;
1023 } else if (!strcasecmp(optname
, "atari_def_no_hopeless")) {
1024 pp
->atari_def_no_hopeless
= optval
&& *optval
== '0' ? false : true;
1025 } else if (!strcasecmp(optname
, "nlib_count") && optval
) {
1026 pp
->nlib_count
= atoi(optval
);
1027 } else if (!strcasecmp(optname
, "middle_ladder")) {
1028 pp
->middle_ladder
= optval
&& *optval
== '0' ? false : true;
1029 } else if (!strcasecmp(optname
, "fullchoose")) {
1030 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_seqchoose
: playout_moggy_fullchoose
;
1031 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
1032 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI%NAKADE */
1033 for (int i
= 0; *optval
&& i
< MQ_MAX
; i
++) {
1034 pp
->mq_prob
[i
] = atof(optval
);
1035 optval
+= strcspn(optval
, "%");
1036 if (*optval
) optval
++;
1038 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
1039 pp
->tenuki_prob
= atof(optval
);
1041 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
1046 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
1047 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
1048 if (pp
->nlibrate
== -1U) pp
->nlibrate
= rate
;
1049 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
1050 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
1051 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
1052 if (pp
->eyefillrate
== -1U) pp
->eyefillrate
= rate
;
1053 if (pp
->korate
== -1U) pp
->korate
= rate
;
1054 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
1055 if (pp
->ladderrate
== -1U) pp
->ladderrate
= rate
;
1056 if (pp
->nakaderate
== -1U) pp
->nakaderate
= rate
;
1057 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
1059 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);