1 /* Playout policy by stochastically applying a fixed set of decision
2 * rules in given order - modelled after the intelligent playouts
3 * in the Mogo engine. */
16 #include "playout/moggy.h"
19 #include "uct/prior.h"
21 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
23 /* Whether to avoid capturing/atariing doomed groups (this is big
24 * performance hit and may reduce playouts balance; it does increase
25 * the strength, but not quite proportionally to the performance). */
26 //#define NO_DOOMED_GROUPS
29 /* Note that the context can be shared by multiple threads! */
32 bool ladders
, ladderassess
, borderladders
, assess_local
;
33 int lcapturerate
, atarirate
, capturerate
, patternrate
, korate
;
34 int selfatarirate
, alwaysccaprate
;
37 /* Whether to look for patterns around second-to-last move. */
40 struct pattern3s patterns
;
51 /* Below, we keep track of each trait for each |color_to_play */
52 int capturable_ready
:2; // is @capturable meaningful?
55 int can_countercapture_ready
:2;
56 int can_countercapture
:2;
59 /* Cache of evaluation of various board features. */
63 struct group_state
*groups
; /* [board_size2()], indexed by group_t */
64 unsigned char *groups_known
; /* Bitmap of known groups. */
67 /* Using board cache: this turns out to be actually a 10% slowdown,
68 * since we reuse data in the cache only very little within single
70 // #define CACHE_STATE
71 /* Reusing board cache across moves if they are successive on the
72 * board; only cache entries within cfg distance 2 of the last move
74 // #define PERSISTENT_STATE
77 static __thread
struct board_state
*ss
;
80 board_state_reuse(struct board_state
*s
, struct board
*b
)
82 /* Decide how much of the board state we can reuse. */
83 /* We do not cache ladder decisions, so we don't have
84 * to worry about this. */
85 coord_t c
= b
->last_move
.coord
;
87 if (unlikely(is_pass(c
))) {
88 /* Passes don't change anything. */
92 if (unlikely(board_at(b
, c
) == S_NONE
)) {
93 /* Suicide is hopeless. */
97 /* XXX: we can make some moves self-atari. */
99 if (neighbor_count_at(b
, c
, S_BLACK
) + neighbor_count_at(b
, c
, S_WHITE
) == 0) {
100 /* We are not taking off liberties of any other stones. */
107 static inline struct board_state
*
108 board_state_init(struct board
*b
)
111 if (ss
->bsize2
!= board_size2(b
)) {
113 free(ss
->groups_known
);
116 #ifdef PERSISTENT_STATE
117 /* Only one stone added to the board, nothing removed. */
118 else if (ss
->hash
== (b
->hash
^ hash_at(b
, b
->last_move
.coord
, b
->last_move
.color
))) {
120 if (likely(board_state_reuse(ss
, b
)))
126 ss
= malloc2(sizeof(*ss
));
127 ss
->bsize2
= board_size2(b
);
128 ss
->groups
= malloc2(board_size2(b
) * sizeof(*ss
->groups
));
129 ss
->groups_known
= malloc2(board_size2(b
) / 8 + 1);
132 memset(ss
->groups_known
, 0, board_size2(b
) / 8 + 1);
136 #define group_is_known(s, g) (s->groups_known[g >> 3] & (1 << (g & 7)))
137 #define group_set_known(s, g) (s->groups_known[g >> 3] |= (1 << (g & 7)))
138 #define group_trait_ready(s, g, color, gstat, trait) do { \
139 if (!group_is_known(s, g)) { \
140 memset(&s->groups[g], 0, sizeof(s->groups[g])); \
141 group_set_known(s, g); \
143 s->groups[g].status = gstat; \
144 s->groups[g].trait ## _ready |= color; \
146 #define group_trait_is_ready(s, g, color, trait) (s->groups[g].trait ## _ready & color)
147 #define group_trait_set(s, g, color, trait, val) s->groups[g].trait = (s->groups[g].trait & ~color) | (!!val * color)
148 #define group_trait_get(s, g, color, trait) (s->groups[g].trait & color)
152 #define board_state_init(b) NULL
153 #define group_is_known(s, g) false
154 #define group_set_known(s, g)
155 #define group_trait_ready(s, g, color, gstat, trait)
156 #define group_trait_is_ready(s, g, color, trait) false
157 #define group_trait_set(s, g, color, trait, val)
158 #define group_trait_get(s, g, color, trait) false
162 static char moggy_patterns_src
[][11] = {
163 /* hane pattern - enclosing hane */
167 /* hane pattern - non-cutting hane */
171 /* hane pattern - magari */
175 /* hane pattern - thin hane */
179 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
183 /* cut1 pattern (kiri) - unprotected cut */
187 /* cut1 pattern (kiri) - peeped cut */
191 /* cut2 pattern (de) */
195 /* cut keima (not in Mogo) */
198 "???", /* o?? has some pathological tsumego cases */
199 /* side pattern - chase */
203 /* side pattern - weirdness (SUSPICIOUS) */
207 /* side pattern - sagari (SUSPICIOUS) */
209 "x.x" /* Mogo has "x.?" */
210 "###" /* Mogo has "X" */,
211 /* side pattern - throw-in (SUSPICIOUS) */
217 /* side pattern - cut (SUSPICIOUS) */
220 "###" /* Mogo has "X" */,
222 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
225 test_pattern3_here(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
227 struct moggy_policy
*pp
= p
->data
;
228 /* Check if 3x3 pattern is matched by given move... */
229 if (!pattern3_move_here(&pp
->patterns
, b
, m
))
231 /* ...and the move is not obviously stupid. */
232 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
234 /* Ladder moves are stupid. */
235 group_t atari_neighbor
= board_get_atari_neighbor(b
, m
->coord
, m
->color
);
236 if (atari_neighbor
&& is_ladder(b
, m
->coord
, atari_neighbor
, pp
->borderladders
, pp
->ladders
))
242 apply_pattern_here(struct playout_policy
*p
, struct board
*b
, coord_t c
, enum stone color
, struct move_queue
*q
)
244 struct move m2
= { .coord
= c
, .color
= color
};
245 if (board_is_valid_move(b
, &m2
) && test_pattern3_here(p
, b
, &m2
))
249 /* Check if we match any pattern around given move (with the other color to play). */
251 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
)
256 /* Suicides do not make any patterns and confuse us. */
257 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
260 foreach_8neighbor(b
, m
->coord
) {
261 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), &q
);
262 } foreach_8neighbor_end
;
264 if (mm
) { /* Second move for pattern searching */
265 foreach_8neighbor(b
, mm
->coord
) {
266 if (coord_is_8adjecent(m
->coord
, c
, b
))
268 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), &q
);
269 } foreach_8neighbor_end
;
273 mq_print(&q
, b
, "Pattern");
280 can_play_on_lib(struct playout_policy
*p
, struct board_state
*s
,
281 struct board
*b
, group_t g
, enum stone to_play
)
283 if (group_is_known(s
, g
) && group_trait_is_ready(s
, g
, to_play
, capturable
)) {
284 /* We have already seen this group. */
285 assert(s
->groups
[g
].status
== G_ATARI
);
286 if (group_trait_get(s
, g
, to_play
, capturable
))
292 /* Cache miss. Set up cache entry, default at capturable = false. */
293 group_trait_ready(s
, g
, to_play
, G_ATARI
, capturable
);
295 coord_t capture
= board_group_info(b
, g
).lib
[0];
297 fprintf(stderr
, "can capture group %d (%s)?\n",
298 g
, coord2sstr(capture
, b
));
299 /* Does playing on the liberty usefully capture the group? */
300 if (board_is_valid_play(b
, to_play
, capture
)
301 && !is_bad_selfatari(b
, to_play
, capture
)) {
302 group_trait_set(s
, g
, to_play
, capturable
, true);
309 /* For given position @c, decide if this is a group that is in danger from
310 * @capturer and @to_play can do anything about it (play at the last
311 * liberty to either capture or escape). */
312 /* Note that @to_play is important; e.g. consider snapback, it's good
313 * to play at the last liberty by attacker, but not defender. */
314 static __attribute__((always_inline
)) bool
315 capturable_group(struct playout_policy
*p
, struct board_state
*s
,
316 struct board
*b
, enum stone capturer
, coord_t c
,
319 group_t g
= group_at(b
, c
);
320 if (likely(board_at(b
, c
) != stone_other(capturer
)
321 || board_group_info(b
, g
).libs
> 1))
324 return can_play_on_lib(p
, s
, b
, g
, to_play
);
327 /* For given atari group @group owned by @owner, decide if @to_play
328 * can save it / keep it in danger by dealing with one of the
329 * neighboring groups. */
331 can_countercapture(struct playout_policy
*p
, struct board_state
*s
,
332 struct board
*b
, enum stone owner
, group_t g
,
333 enum stone to_play
, struct move_queue
*q
)
337 if (group_is_known(s
, g
) && group_trait_is_ready(s
, g
, to_play
, can_countercapture
)) {
338 /* We have already seen this group. */
339 assert(s
->groups
[g
].status
== G_ATARI
);
340 if (group_trait_get(s
, g
, to_play
, can_countercapture
)) {
341 if (q
) { /* Scan for countercapture liberties. */
350 /* Cache miss. Set up cache entry, default at can_countercapture = true. */
351 group_trait_ready(s
, g
, to_play
, G_ATARI
, can_countercapture
);
352 group_trait_set(s
, g
, to_play
, can_countercapture
, true);
355 int qmoves_prev
= q
? q
->moves
: 0;
357 foreach_in_group(b
, g
) {
358 foreach_neighbor(b
, c
, {
359 if (!capturable_group(p
, s
, b
, owner
, c
, to_play
))
365 mq_add(q
, board_group_info(b
, group_at(b
, c
)).lib
[0]);
368 } foreach_in_group_end
;
370 bool can
= q
? q
->moves
> qmoves_prev
: false;
371 group_trait_set(s
, g
, to_play
, can_countercapture
, can
);
375 #ifdef NO_DOOMED_GROUPS
377 can_be_rescued(struct playout_policy
*p
, struct board_state
*s
,
378 struct board
*b
, group_t group
, enum stone color
)
380 /* Does playing on the liberty rescue the group? */
381 if (can_play_on_lib(p
, s
, b
, group
, color
))
384 /* Then, maybe we can capture one of our neighbors? */
385 return can_countercapture(p
, s
, b
, color
, group
, color
, NULL
);
389 /* ladder != NULL implies to always enqueue all relevant moves. */
391 group_atari_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
,
392 struct move_queue
*q
, coord_t
*ladder
, struct board_state
*s
)
394 struct moggy_policy
*pp
= p
->data
;
395 int qmoves_prev
= q
->moves
;
397 /* We don't use @to_play almost anywhere since any moves here are good
398 * for both defender and attacker. */
400 enum stone color
= board_at(b
, group_base(group
));
401 coord_t lib
= board_group_info(b
, group
).lib
[0];
403 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
405 fprintf(stderr
, "[%s] atariiiiiiiii %s of color %d\n",
406 coord2sstr(group
, b
), coord2sstr(lib
, b
), color
);
407 assert(board_at(b
, lib
) == S_NONE
);
409 /* Do not bother with kos. */
410 if (group_is_onestone(b
, group
)
411 && neighbor_count_at(b
, lib
, color
) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 4)
414 /* Can we capture some neighbor? */
415 bool ccap
= can_countercapture(p
, s
, b
, color
, group
, to_play
, q
);
416 if (ccap
&& !ladder
&& pp
->alwaysccaprate
> fast_random(100))
419 /* Do not suicide... */
420 if (!can_play_on_lib(p
, s
, b
, group
, to_play
))
422 #ifdef NO_DOOMED_GROUPS
423 /* Do not remove group that cannot be saved by the opponent. */
424 if (to_play
!= color
&& !can_be_rescued(p
, s
, b
, group
, color
))
428 fprintf(stderr
, "...escape route valid\n");
430 /* ...or play out ladders. */
431 if (is_ladder(b
, lib
, group
, pp
->borderladders
, pp
->ladders
)) {
432 /* Sometimes we want to keep the ladder move in the
433 * queue in order to discourage it. */
440 fprintf(stderr
, "...no ladder\n");
442 if (to_play
!= color
) {
443 /* We are the attacker! In that case, throw away the moves
444 * that defend our groups, since we can capture the culprit. */
445 q
->moves
= qmoves_prev
;
453 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct board_state
*s
)
461 int g_base
= fast_random(b
->clen
);
462 for (int g
= g_base
; g
< b
->clen
; g
++) {
463 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, &q
, NULL
, s
);
467 for (int g
= 0; g
< g_base
; g
++) {
468 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, &q
, NULL
, s
);
476 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct board_state
*s
)
481 /* Did the opponent play a self-atari? */
482 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
483 group_atari_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), &q
, NULL
, s
);
486 foreach_neighbor(b
, m
->coord
, {
487 group_t g
= group_at(b
, c
);
488 if (!g
|| board_group_info(b
, g
).libs
!= 1)
490 group_atari_check(p
, b
, g
, stone_other(m
->color
), &q
, NULL
, s
);
494 mq_print(&q
, b
, "Local atari");
500 miai_2lib(struct board
*b
, group_t group
, enum stone color
)
502 bool can_connect
= false, can_pull_out
= false;
503 /* We have miai if we can either connect on both libs,
504 * or connect on one lib and escape on another. (Just
505 * having two escape routes can be risky.) We must make
506 * sure that we don't consider following as miai:
509 * O O X O - left dot would be pull-out, right dot connect */
510 foreach_neighbor(b
, board_group_info(b
, group
).lib
[0], {
511 enum stone cc
= board_at(b
, c
);
512 if (cc
== S_NONE
&& cc
!= board_group_info(b
, group
).lib
[1]) {
514 } else if (cc
!= color
) {
518 group_t cg
= group_at(b
, c
);
519 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
522 foreach_neighbor(b
, board_group_info(b
, group
).lib
[1], {
523 enum stone cc
= board_at(b
, c
);
524 if (c
== board_group_info(b
, group
).lib
[0])
526 if (cc
== S_NONE
&& can_connect
) {
528 } else if (cc
!= color
) {
532 group_t cg
= group_at(b
, c
);
533 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
534 return (can_connect
|| can_pull_out
);
540 check_group_atari(struct board
*b
, group_t group
, enum stone owner
,
541 enum stone to_play
, struct move_queue
*q
)
543 for (int i
= 0; i
< 2; i
++) {
544 coord_t lib
= board_group_info(b
, group
).lib
[i
];
545 assert(board_at(b
, lib
) == S_NONE
);
546 if (!board_is_valid_play(b
, to_play
, lib
))
549 /* Don't play at the spot if it is extremely short
551 /* XXX: This looks harmful, could significantly
552 * prefer atari to throwin:
558 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + immediate_liberty_count(b
, lib
) < 2)
562 #ifdef NO_DOOMED_GROUPS
563 /* If the owner can't play at the spot, we don't want
564 * to bother either. */
565 if (is_bad_selfatari(b
, owner
, lib
))
569 /* Of course we don't want to play bad selfatari
570 * ourselves, if we are the attacker... */
572 #ifdef NO_DOOMED_GROUPS
575 is_bad_selfatari(b
, to_play
, lib
))
578 /* Tasty! Crispy! Good! */
585 group_2lib_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
,
586 struct move_queue
*q
, struct board_state
*s
)
588 enum stone color
= board_at(b
, group_base(group
));
589 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
592 fprintf(stderr
, "[%s] 2lib check of color %d\n",
593 coord2sstr(group
, b
), color
);
595 /* Do not try to atari groups that cannot be harmed. */
596 if (miai_2lib(b
, group
, color
))
599 check_group_atari(b
, group
, color
, to_play
, q
);
601 /* Can we counter-atari another group, if we are the defender? */
602 if (to_play
!= color
)
604 foreach_in_group(b
, group
) {
605 foreach_neighbor(b
, c
, {
606 if (board_at(b
, c
) != stone_other(color
))
608 group_t g2
= group_at(b
, c
);
609 if (board_group_info(b
, g2
).libs
!= 2)
611 check_group_atari(b
, g2
, color
, to_play
, q
);
613 } foreach_in_group_end
;
617 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct board_state
*s
)
622 /* Does the opponent have just two liberties? */
623 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 2) {
624 group_2lib_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), &q
, s
);
626 /* We always prefer to take off an enemy chain liberty
627 * before pulling out ourselves. */
628 /* XXX: We aren't guaranteed to return to that group
631 return q
.move
[fast_random(q
.moves
)];
635 /* Then he took a third liberty from neighboring chain? */
636 foreach_neighbor(b
, m
->coord
, {
637 group_t g
= group_at(b
, c
);
638 if (!g
|| board_group_info(b
, g
).libs
!= 2)
640 group_2lib_check(p
, b
, g
, stone_other(m
->color
), &q
, s
);
644 mq_print(&q
, b
, "Local 2lib");
650 playout_moggy_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
652 struct moggy_policy
*pp
= p
->data
;
655 struct board_state
*s
= board_state_init(b
);
658 board_print(b
, stderr
);
661 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
662 && b
->moves
- b
->last_ko_age
< pp
->koage
663 && pp
->korate
> fast_random(100)) {
664 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
665 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
666 return b
->last_ko
.coord
;
670 if (!is_pass(b
->last_move
.coord
)) {
671 /* Local group in atari? */
672 if (pp
->lcapturerate
> fast_random(100)) {
673 c
= local_atari_check(p
, b
, &b
->last_move
, s
);
678 /* Local group can be PUT in atari? */
679 if (pp
->atarirate
> fast_random(100)) {
680 c
= local_2lib_check(p
, b
, &b
->last_move
, s
);
685 /* Check for patterns we know */
686 if (pp
->patternrate
> fast_random(100)) {
687 c
= apply_pattern(p
, b
, &b
->last_move
,
688 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
);
696 /* Any groups in atari? */
697 if (pp
->capturerate
> fast_random(100)) {
698 c
= global_atari_check(p
, b
, to_play
, s
);
704 int fbtries
= b
->flen
/ 8;
705 for (int i
= 0; i
< (fbtries
< pp
->fillboardtries
? fbtries
: pp
->fillboardtries
); i
++) {
706 coord_t coord
= b
->f
[fast_random(b
->flen
)];
707 if (is_pass(coord
) || immediate_liberty_count(b
, coord
) != 4)
709 foreach_diag_neighbor(b
, coord
) {
710 if (board_at(b
, c
) != S_NONE
)
712 } foreach_diag_neighbor_end
;
722 assess_local_bonus(struct playout_policy
*p
, struct board
*board
, coord_t a
, coord_t b
, int games
)
724 struct moggy_policy
*pp
= p
->data
;
725 if (!pp
->assess_local
)
728 int dx
= abs(coord_x(a
, board
) - coord_x(b
, board
));
729 int dy
= abs(coord_y(a
, board
) - coord_y(b
, board
));
730 /* adjecent move, directly or diagonally? */
731 if (dx
+ dy
<= 1 + (dx
&& dy
))
738 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
,
739 struct board_state
*s
)
741 struct moggy_policy
*pp
= p
->data
;
742 struct board
*b
= map
->b
;
743 struct move_queue q
; q
.moves
= 0;
745 if (board_group_info(b
, g
).libs
> 2)
749 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
750 board_print(b
, stderr
);
753 if (board_group_info(b
, g
).libs
== 2) {
756 group_2lib_check(p
, b
, g
, map
->to_play
, &q
, s
);
758 coord_t coord
= q
.move
[q
.moves
];
760 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
761 int assess
= assess_local_bonus(p
, b
, b
->last_move
.coord
, coord
, games
) / 2;
762 add_prior_value(map
, coord
, 1, assess
);
767 /* This group, sir, is in atari! */
769 if (!pp
->capturerate
&& !pp
->lcapturerate
&& !pp
->ladderassess
)
772 coord_t ladder
= pass
;
773 group_atari_check(p
, b
, g
, map
->to_play
, &q
, &ladder
, s
);
775 coord_t coord
= q
.move
[q
.moves
];
777 /* _Never_ play here if this move plays out
778 * a caught ladder. */
779 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
780 /* Note that the opposite is not guarded against;
781 * we do not advise against capturing a laddered
782 * group (but we don't encourage it either). Such
783 * a move can simplify tactical situations if we
785 if (!pp
->ladderassess
|| map
->to_play
!= board_at(b
, g
))
787 /* FIXME: We give the malus even if this move
788 * captures another group. */
790 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
791 add_prior_value(map
, coord
, 0, games
);
795 if (!pp
->capturerate
&& !pp
->lcapturerate
)
799 fprintf(stderr
, "1.0: atari %s\n", coord2sstr(coord
, b
));
800 int assess
= assess_local_bonus(p
, b
, b
->last_move
.coord
, coord
, games
) * 2;
801 add_prior_value(map
, coord
, 1, assess
);
806 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
808 struct moggy_policy
*pp
= p
->data
;
809 struct board
*b
= map
->b
;
812 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
813 board_print(b
, stderr
);
816 /* Is this move a self-atari? */
817 if (pp
->selfatarirate
) {
818 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
820 fprintf(stderr
, "0.0: self-atari\n");
821 add_prior_value(map
, coord
, 0, games
);
827 if (pp
->patternrate
) {
828 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
829 if (test_pattern3_here(p
, b
, &m
)) {
831 fprintf(stderr
, "1.0: pattern\n");
832 int assess
= assess_local_bonus(p
, b
, b
->last_move
.coord
, coord
, games
);
833 add_prior_value(map
, coord
, 1, assess
);
841 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
843 struct moggy_policy
*pp
= p
->data
;
845 struct board_state
*s
= board_state_init(map
->b
);
847 /* First, go through all endangered groups. */
848 if (pp
->lcapturerate
|| pp
->capturerate
|| pp
->atarirate
|| pp
->ladderassess
)
849 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
850 if (group_at(map
->b
, g
) == g
)
851 playout_moggy_assess_group(p
, map
, g
, games
, s
);
853 /* Then, assess individual moves. */
854 if (!pp
->patternrate
&& !pp
->selfatarirate
)
856 foreach_point(map
->b
) {
857 if (map
->consider
[c
])
858 playout_moggy_assess_one(p
, map
, c
, games
);
863 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
865 struct moggy_policy
*pp
= p
->data
;
867 /* The idea is simple for now - never allow self-atari moves.
868 * They suck in general, but this also permits us to actually
869 * handle seki in the playout stage. */
871 if (fast_random(100) >= pp
->selfatarirate
) {
873 fprintf(stderr
, "skipping sar test\n");
876 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
877 if (PLDEBUGL(5) && selfatari
)
878 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
879 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
884 struct playout_policy
*
885 playout_moggy_init(char *arg
, struct board
*b
)
887 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
888 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
890 p
->choose
= playout_moggy_choose
;
891 p
->assess
= playout_moggy_assess
;
892 p
->permit
= playout_moggy_permit
;
896 pp
->lcapturerate
= pp
->atarirate
= pp
->capturerate
= pp
->patternrate
= pp
->selfatarirate
898 pp
->korate
= 0; pp
->koage
= 4;
899 pp
->alwaysccaprate
= 0;
900 pp
->ladders
= pp
->borderladders
= true;
901 pp
->ladderassess
= true;
904 char *optspec
, *next
= arg
;
907 next
+= strcspn(next
, ":");
908 if (*next
) { *next
++ = 0; } else { *next
= 0; }
910 char *optname
= optspec
;
911 char *optval
= strchr(optspec
, '=');
912 if (optval
) *optval
++ = 0;
914 if (!strcasecmp(optname
, "lcapturerate") && optval
) {
915 pp
->lcapturerate
= atoi(optval
);
916 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
917 pp
->atarirate
= atoi(optval
);
918 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
919 pp
->capturerate
= atoi(optval
);
920 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
921 pp
->patternrate
= atoi(optval
);
922 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
923 pp
->selfatarirate
= atoi(optval
);
924 } else if (!strcasecmp(optname
, "korate") && optval
) {
925 pp
->korate
= atoi(optval
);
926 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
927 pp
->alwaysccaprate
= atoi(optval
);
928 } else if (!strcasecmp(optname
, "rate") && optval
) {
930 } else if (!strcasecmp(optname
, "fillboardtries")) {
931 pp
->fillboardtries
= atoi(optval
);
932 } else if (!strcasecmp(optname
, "koage") && optval
) {
933 pp
->koage
= atoi(optval
);
934 } else if (!strcasecmp(optname
, "ladders")) {
935 pp
->ladders
= optval
&& *optval
== '0' ? false : true;
936 } else if (!strcasecmp(optname
, "borderladders")) {
937 pp
->borderladders
= optval
&& *optval
== '0' ? false : true;
938 } else if (!strcasecmp(optname
, "ladderassess")) {
939 pp
->ladderassess
= optval
&& *optval
== '0' ? false : true;
940 } else if (!strcasecmp(optname
, "assess_local")) {
941 pp
->assess_local
= optval
&& *optval
== '0' ? false : true;
942 } else if (!strcasecmp(optname
, "pattern2")) {
943 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
945 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
950 if (pp
->lcapturerate
== -1) pp
->lcapturerate
= rate
;
951 if (pp
->atarirate
== -1) pp
->atarirate
= rate
;
952 if (pp
->capturerate
== -1) pp
->capturerate
= rate
;
953 if (pp
->patternrate
== -1) pp
->patternrate
= rate
;
954 if (pp
->selfatarirate
== -1) pp
->selfatarirate
= rate
;
955 if (pp
->korate
== -1) pp
->korate
= rate
;
956 if (pp
->alwaysccaprate
== -1) pp
->alwaysccaprate
= rate
;
958 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);