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. */
13 #include "joseki/base.h"
17 #include "playout/moggy.h"
19 #include "tactics/ladder.h"
20 #include "tactics/selfatari.h"
21 #include "uct/prior.h"
23 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
25 /* Whether to avoid capturing/atariing doomed groups (this is big
26 * performance hit and may reduce playouts balance; it does increase
27 * the strength, but not quite proportionally to the performance). */
28 //#define NO_DOOMED_GROUPS
31 /* Move queue tags: */
43 /* Note that the context can be shared by multiple threads! */
46 unsigned int lcapturerate
, atarirate
, capturerate
, patternrate
, korate
, josekirate
;
47 unsigned int selfatarirate
, alwaysccaprate
;
48 unsigned int fillboardtries
;
50 /* Whether to look for patterns around second-to-last move. */
52 /* Whether, when self-atari attempt is detected, to play the other
53 * group's liberty if that is non-self-atari. */
55 /* Whether to always pick from moves capturing all groups in
56 * global_atari_check(). */
59 struct joseki_dict
*jdict
;
60 struct pattern3s patterns
;
62 /* Gamma values for queue tags - correspond to probabilities. */
64 double mq_prob
[MQ_MAX
], tenuki_prob
;
68 static char moggy_patterns_src
[][11] = {
69 /* hane pattern - enclosing hane */
73 /* hane pattern - non-cutting hane */
77 /* hane pattern - magari */
81 /* hane pattern - thin hane */
85 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
89 /* cut1 pattern (kiri) - unprotected cut */
93 /* cut1 pattern (kiri) - peeped cut */
97 /* cut2 pattern (de) */
101 /* cut keima (not in Mogo) */
104 "???", /* o?? has some pathological tsumego cases */
105 /* side pattern - chase */
109 /* side pattern - block side cut */
113 /* side pattern - block side connection */
117 /* side pattern - sagari (SUSPICIOUS) */
119 "x.x" /* Mogo has "x.?" */
120 "###" /* Mogo has "X" */,
121 /* side pattern - throw-in (SUSPICIOUS) */
127 /* side pattern - cut (SUSPICIOUS) */
130 "###" /* Mogo has "X" */,
132 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
135 test_pattern3_here(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
137 struct moggy_policy
*pp
= p
->data
;
138 /* Check if 3x3 pattern is matched by given move... */
139 if (!pattern3_move_here(&pp
->patterns
, b
, m
))
141 /* ...and the move is not obviously stupid. */
142 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
144 /* Ladder moves are stupid. */
145 group_t atari_neighbor
= board_get_atari_neighbor(b
, m
->coord
, m
->color
);
146 if (atari_neighbor
&& is_ladder(b
, m
->coord
, atari_neighbor
))
152 apply_pattern_here(struct playout_policy
*p
, struct board
*b
, coord_t c
, enum stone color
, struct move_queue
*q
)
154 struct move m2
= { .coord
= c
, .color
= color
};
155 if (board_is_valid_move(b
, &m2
) && test_pattern3_here(p
, b
, &m2
))
156 mq_add(q
, c
, 1<<MQ_PAT3
);
159 /* Check if we match any pattern around given move (with the other color to play). */
161 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
, struct move_queue
*q
)
163 /* Suicides do not make any patterns and confuse us. */
164 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
167 foreach_8neighbor(b
, m
->coord
) {
168 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
169 } foreach_8neighbor_end
;
171 if (mm
) { /* Second move for pattern searching */
172 foreach_8neighbor(b
, mm
->coord
) {
173 if (coord_is_8adjecent(m
->coord
, c
, b
))
175 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
176 } foreach_8neighbor_end
;
180 mq_print(q
, b
, "Pattern");
185 can_play_on_lib(struct board
*b
, group_t g
, enum stone to_play
)
187 coord_t capture
= board_group_info(b
, g
).lib
[0];
189 fprintf(stderr
, "can capture group %d (%s)?\n",
190 g
, coord2sstr(capture
, b
));
191 /* Does playing on the liberty usefully capture the group? */
192 if (board_is_valid_play(b
, to_play
, capture
)
193 && !is_bad_selfatari(b
, to_play
, capture
))
199 /* For given position @c, decide if this is a group that is in danger from
200 * @capturer and @to_play can do anything about it (play at the last
201 * liberty to either capture or escape). */
202 /* Note that @to_play is important; e.g. consider snapback, it's good
203 * to play at the last liberty by attacker, but not defender. */
204 static __attribute__((always_inline
)) bool
205 capturable_group(struct board
*b
, enum stone capturer
, coord_t c
,
208 group_t g
= group_at(b
, c
);
209 if (likely(board_at(b
, c
) != stone_other(capturer
)
210 || board_group_info(b
, g
).libs
> 1))
213 return can_play_on_lib(b
, g
, to_play
);
216 /* For given atari group @group owned by @owner, decide if @to_play
217 * can save it / keep it in danger by dealing with one of the
218 * neighboring groups. */
220 can_countercapture(struct board
*b
, enum stone owner
, group_t g
,
221 enum stone to_play
, struct move_queue
*q
, enum mq_tag tag
)
226 unsigned int qmoves_prev
= q
? q
->moves
: 0;
228 foreach_in_group(b
, g
) {
229 foreach_neighbor(b
, c
, {
230 if (!capturable_group(b
, owner
, c
, to_play
))
236 mq_add(q
, board_group_info(b
, group_at(b
, c
)).lib
[0], 1<<tag
);
239 } foreach_in_group_end
;
241 bool can
= q
? q
->moves
> qmoves_prev
: false;
245 #ifdef NO_DOOMED_GROUPS
247 can_be_rescued(struct board
*b
, group_t group
, enum stone color
, enum mq_tag tag
)
249 /* Does playing on the liberty rescue the group? */
250 if (can_play_on_lib(b
, group
, color
))
253 /* Then, maybe we can capture one of our neighbors? */
254 return can_countercapture(b
, color
, group
, color
, NULL
, tag
);
258 /* ladder != NULL implies to always enqueue all relevant moves. */
260 group_atari_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
,
261 struct move_queue
*q
, coord_t
*ladder
, enum mq_tag tag
)
263 struct moggy_policy
*pp
= p
->data
;
264 int qmoves_prev
= q
->moves
;
266 /* We don't use @to_play almost anywhere since any moves here are good
267 * for both defender and attacker. */
269 enum stone color
= board_at(b
, group_base(group
));
270 coord_t lib
= board_group_info(b
, group
).lib
[0];
272 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
274 fprintf(stderr
, "[%s] atariiiiiiiii %s of color %d\n",
275 coord2sstr(group
, b
), coord2sstr(lib
, b
), color
);
276 assert(board_at(b
, lib
) == S_NONE
);
278 /* Can we capture some neighbor? */
279 bool ccap
= can_countercapture(b
, color
, group
, to_play
, q
, tag
);
280 if (ccap
&& !ladder
&& pp
->alwaysccaprate
> fast_random(100))
283 /* Otherwise, do not save kos. */
284 if (group_is_onestone(b
, group
)
285 && neighbor_count_at(b
, lib
, color
) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 4)
288 /* Do not suicide... */
289 if (!can_play_on_lib(b
, group
, to_play
))
291 #ifdef NO_DOOMED_GROUPS
292 /* Do not remove group that cannot be saved by the opponent. */
293 if (to_play
!= color
&& !can_be_rescued(b
, group
, color
, tag
))
297 fprintf(stderr
, "...escape route valid\n");
299 /* ...or play out ladders. */
300 if (is_ladder(b
, lib
, group
)) {
301 /* Sometimes we want to keep the ladder move in the
302 * queue in order to discourage it. */
309 fprintf(stderr
, "...no ladder\n");
311 if (to_play
!= color
) {
312 /* We are the attacker! In that case, throw away the moves
313 * that defend our groups, since we can capture the culprit. */
314 q
->moves
= qmoves_prev
;
317 mq_add(q
, lib
, 1<<tag
);
322 joseki_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
324 struct moggy_policy
*pp
= p
->data
;
328 for (int i
= 0; i
< 4; i
++) {
329 hash_t h
= b
->qhash
[i
] & joseki_hash_mask
;
330 coord_t
*cc
= pp
->jdict
->patterns
[h
].moves
[to_play
];
332 for (; !is_pass(*cc
); cc
++) {
333 if (coord_quadrant(*cc
, b
) != i
)
335 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
339 if (q
->moves
> 0 && PLDEBUGL(5))
340 mq_print(q
, b
, "Joseki");
344 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
349 struct moggy_policy
*pp
= p
->data
;
350 if (pp
->capcheckall
) {
351 for (int g
= 0; g
< b
->clen
; g
++)
352 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, MQ_GATARI
);
354 mq_print(q
, b
, "Global atari");
358 int g_base
= fast_random(b
->clen
);
359 for (int g
= g_base
; g
< b
->clen
; g
++) {
360 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, MQ_GATARI
);
362 /* XXX: Try carrying on. */
364 mq_print(q
, b
, "Global atari");
368 for (int g
= 0; g
< g_base
; g
++) {
369 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, MQ_GATARI
);
371 /* XXX: Try carrying on. */
373 mq_print(q
, b
, "Global atari");
381 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
383 /* Did the opponent play a self-atari? */
384 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
385 group_atari_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
, NULL
, MQ_LATARI
);
388 foreach_neighbor(b
, m
->coord
, {
389 group_t g
= group_at(b
, c
);
390 if (!g
|| board_group_info(b
, g
).libs
!= 1)
392 group_atari_check(p
, b
, g
, stone_other(m
->color
), q
, NULL
, MQ_LATARI
);
396 mq_print(q
, b
, "Local atari");
400 miai_2lib(struct board
*b
, group_t group
, enum stone color
)
402 bool can_connect
= false, can_pull_out
= false;
403 /* We have miai if we can either connect on both libs,
404 * or connect on one lib and escape on another. (Just
405 * having two escape routes can be risky.) We must make
406 * sure that we don't consider following as miai:
409 * O O X O - left dot would be pull-out, right dot connect */
410 foreach_neighbor(b
, board_group_info(b
, group
).lib
[0], {
411 enum stone cc
= board_at(b
, c
);
412 if (cc
== S_NONE
&& cc
!= board_at(b
, board_group_info(b
, group
).lib
[1])) {
414 } else if (cc
!= color
) {
418 group_t cg
= group_at(b
, c
);
419 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
422 foreach_neighbor(b
, board_group_info(b
, group
).lib
[1], {
423 enum stone cc
= board_at(b
, c
);
424 if (c
== board_group_info(b
, group
).lib
[0])
426 if (cc
== S_NONE
&& can_connect
) {
428 } else if (cc
!= color
) {
432 group_t cg
= group_at(b
, c
);
433 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
434 return (can_connect
|| can_pull_out
);
440 check_group_atari(struct board
*b
, group_t group
, enum stone owner
,
441 enum stone to_play
, struct move_queue
*q
)
443 for (int i
= 0; i
< 2; i
++) {
444 coord_t lib
= board_group_info(b
, group
).lib
[i
];
445 assert(board_at(b
, lib
) == S_NONE
);
446 if (!board_is_valid_play(b
, to_play
, lib
))
449 /* Don't play at the spot if it is extremely short
451 /* XXX: This looks harmful, could significantly
452 * prefer atari to throwin:
458 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + immediate_liberty_count(b
, lib
) < 2)
462 /* If the move is too "lumpy", do not play it:
465 * ..O.X.X <- always play the left one!
467 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 3)
470 #ifdef NO_DOOMED_GROUPS
471 /* If the owner can't play at the spot, we don't want
472 * to bother either. */
473 if (is_bad_selfatari(b
, owner
, lib
))
477 /* Of course we don't want to play bad selfatari
478 * ourselves, if we are the attacker... */
480 #ifdef NO_DOOMED_GROUPS
483 is_bad_selfatari(b
, to_play
, lib
))
486 /* Tasty! Crispy! Good! */
487 mq_add(q
, lib
, 1<<MQ_L2LIB
);
493 group_2lib_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
,
494 struct move_queue
*q
)
496 enum stone color
= board_at(b
, group_base(group
));
497 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
500 fprintf(stderr
, "[%s] 2lib check of color %d\n",
501 coord2sstr(group
, b
), color
);
503 /* Do not try to atari groups that cannot be harmed. */
504 if (miai_2lib(b
, group
, color
))
507 check_group_atari(b
, group
, color
, to_play
, q
);
509 /* Can we counter-atari another group, if we are the defender? */
510 if (to_play
!= color
)
512 foreach_in_group(b
, group
) {
513 foreach_neighbor(b
, c
, {
514 if (board_at(b
, c
) != stone_other(color
))
516 group_t g2
= group_at(b
, c
);
517 if (board_group_info(b
, g2
).libs
== 1) {
518 /* We can capture a neighbor. */
519 mq_add(q
, board_group_info(b
, g2
).lib
[0], 1<<MQ_L2LIB
);
523 if (board_group_info(b
, g2
).libs
!= 2)
525 check_group_atari(b
, g2
, color
, to_play
, q
);
527 } foreach_in_group_end
;
531 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
533 /* Does the opponent have just two liberties? */
534 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 2) {
535 group_2lib_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
);
537 /* We always prefer to take off an enemy chain liberty
538 * before pulling out ourselves. */
539 /* XXX: We aren't guaranteed to return to that group
542 return q
->move
[fast_random(q
->moves
)];
546 /* Then he took a third liberty from neighboring chain? */
547 foreach_neighbor(b
, m
->coord
, {
548 group_t g
= group_at(b
, c
);
549 if (!g
|| board_group_info(b
, g
).libs
!= 2)
551 group_2lib_check(p
, b
, g
, stone_other(m
->color
), q
);
555 mq_print(q
, b
, "Local 2lib");
559 fillboard_check(struct playout_policy
*p
, struct board
*b
)
561 struct moggy_policy
*pp
= p
->data
;
562 unsigned int fbtries
= b
->flen
/ 8;
563 if (pp
->fillboardtries
< fbtries
)
564 fbtries
= pp
->fillboardtries
;
566 for (unsigned int i
= 0; i
< fbtries
; i
++) {
567 coord_t coord
= b
->f
[fast_random(b
->flen
)];
568 if (immediate_liberty_count(b
, coord
) != 4)
570 foreach_diag_neighbor(b
, coord
) {
571 if (board_at(b
, c
) != S_NONE
)
573 } foreach_diag_neighbor_end
;
581 playout_moggy_partchoose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
583 struct moggy_policy
*pp
= p
->data
;
586 board_print(b
, stderr
);
589 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
590 && b
->moves
- b
->last_ko_age
< pp
->koage
591 && pp
->korate
> fast_random(100)) {
592 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
593 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
594 return b
->last_ko
.coord
;
598 if (!is_pass(b
->last_move
.coord
)) {
599 /* Local group in atari? */
600 if (pp
->lcapturerate
> fast_random(100)) {
601 struct move_queue q
; q
.moves
= 0;
602 local_atari_check(p
, b
, &b
->last_move
, &q
);
607 /* Local group can be PUT in atari? */
608 if (pp
->atarirate
> fast_random(100)) {
609 struct move_queue q
; q
.moves
= 0;
610 local_2lib_check(p
, b
, &b
->last_move
, &q
);
615 /* Check for patterns we know */
616 if (pp
->patternrate
> fast_random(100)) {
617 struct move_queue q
; q
.moves
= 0;
618 apply_pattern(p
, b
, &b
->last_move
,
619 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
628 /* Any groups in atari? */
629 if (pp
->capturerate
> fast_random(100)) {
630 struct move_queue q
; q
.moves
= 0;
631 global_atari_check(p
, b
, to_play
, &q
);
637 if (pp
->josekirate
> fast_random(100)) {
638 struct move_queue q
; q
.moves
= 0;
639 joseki_check(p
, b
, to_play
, &q
);
645 if (pp
->fillboardtries
> 0) {
646 coord_t c
= fillboard_check(p
, b
);
654 /* Pick a move from queue q, giving different likelihoods to moves
655 * based on their tags. */
657 mq_tagged_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
659 struct moggy_policy
*pp
= p
->data
;
661 /* First, merge all entries for a move. */
662 /* We use a naive O(N^2) since the average length of the queue
664 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
665 for (unsigned int j
= i
+ 1; j
< q
->moves
; j
++) {
666 if (q
->move
[i
] != q
->move
[j
])
668 q
->tag
[i
] |= q
->tag
[j
];
670 q
->tag
[j
] = q
->tag
[q
->moves
];
671 q
->move
[j
] = q
->move
[q
->moves
];
675 /* Now, construct a probdist. */
678 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
680 assert(q
->tag
[i
] != 0);
681 for (int j
= 1; j
< MQ_MAX
; j
++)
682 if (q
->tag
[i
] & (1<<j
)) {
683 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
684 val
*= pp
->mq_prob
[j
];
686 pd
[i
] = double_to_fixp(val
);
689 total
+= double_to_fixp(pp
->tenuki_prob
);
691 /* Finally, pick a move! */
692 fixp_t stab
= fast_irandom(total
);
693 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
694 //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));
701 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
706 playout_moggy_fullchoose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
708 struct moggy_policy
*pp
= p
->data
;
709 struct move_queue q
; q
.moves
= 0;
712 board_print(b
, stderr
);
715 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
716 && b
->moves
- b
->last_ko_age
< pp
->koage
717 && pp
->korate
> fast_random(100)) {
718 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
719 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
720 mq_add(&q
, b
->last_ko
.coord
, 1<<MQ_KO
);
724 if (!is_pass(b
->last_move
.coord
)) {
725 /* Local group in atari? */
726 if (pp
->lcapturerate
> fast_random(100)) {
727 local_atari_check(p
, b
, &b
->last_move
, &q
);
730 /* Local group can be PUT in atari? */
731 if (pp
->atarirate
> fast_random(100)) {
732 local_2lib_check(p
, b
, &b
->last_move
, &q
);
735 /* Check for patterns we know */
736 if (pp
->patternrate
> fast_random(100)) {
737 apply_pattern(p
, b
, &b
->last_move
,
738 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
745 /* Any groups in atari? */
746 if (pp
->capturerate
> fast_random(100)) {
747 global_atari_check(p
, b
, to_play
, &q
);
751 if (pp
->josekirate
> fast_random(100)) {
752 joseki_check(p
, b
, to_play
, &q
);
756 /* Average length of the queue is 1.4 move. */
757 printf("MQL %d ", q
.moves
);
758 for (unsigned int i
= 0; i
< q
.moves
; i
++)
759 printf("%s ", coord2sstr(q
.move
[i
], b
));
764 return mq_tagged_choose(p
, b
, to_play
, &q
);
767 if (pp
->fillboardtries
> 0) {
768 coord_t c
= fillboard_check(p
, b
);
778 selfatari_cousin(struct board
*b
, enum stone color
, coord_t coord
)
780 group_t groups
[4]; int groups_n
= 0;
781 foreach_neighbor(b
, coord
, {
782 enum stone s
= board_at(b
, c
);
783 if (s
!= color
) continue;
784 group_t g
= group_at(b
, c
);
785 if (board_group_info(b
, g
).libs
== 2)
786 groups
[groups_n
++] = g
;
791 group_t group
= groups
[fast_random(groups_n
)];
793 coord_t lib2
= board_group_other_lib(b
, group
, coord
);
794 if (is_bad_selfatari(b
, color
, lib2
))
800 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
802 struct moggy_policy
*pp
= p
->data
;
803 struct board
*b
= map
->b
;
804 struct move_queue q
; q
.moves
= 0;
806 if (board_group_info(b
, g
).libs
> 2)
810 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
811 board_print(b
, stderr
);
814 if (board_group_info(b
, g
).libs
== 2) {
817 group_2lib_check(p
, b
, g
, map
->to_play
, &q
);
819 coord_t coord
= q
.move
[q
.moves
];
821 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
822 int assess
= games
/ 2;
823 add_prior_value(map
, coord
, 1, assess
);
828 /* This group, sir, is in atari! */
830 coord_t ladder
= pass
;
831 group_atari_check(p
, b
, g
, map
->to_play
, &q
, &ladder
, 0);
833 coord_t coord
= q
.move
[q
.moves
];
835 /* _Never_ play here if this move plays out
836 * a caught ladder. */
837 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
838 /* Note that the opposite is not guarded against;
839 * we do not advise against capturing a laddered
840 * group (but we don't encourage it either). Such
841 * a move can simplify tactical situations if we
843 if (map
->to_play
!= board_at(b
, g
))
845 /* FIXME: We give the malus even if this move
846 * captures another group. */
848 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
849 add_prior_value(map
, coord
, 0, games
);
853 if (!pp
->capturerate
&& !pp
->lcapturerate
)
857 fprintf(stderr
, "1.0: atari %s\n", coord2sstr(coord
, b
));
858 int assess
= games
* 2;
859 add_prior_value(map
, coord
, 1, assess
);
864 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
866 struct moggy_policy
*pp
= p
->data
;
867 struct board
*b
= map
->b
;
870 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
871 board_print(b
, stderr
);
874 /* Is this move a self-atari? */
875 if (pp
->selfatarirate
) {
876 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
878 fprintf(stderr
, "0.0: self-atari\n");
879 add_prior_value(map
, coord
, 0, games
);
880 if (!pp
->selfatari_other
)
882 /* If we can play on the other liberty of the
883 * endangered group, do! */
884 coord
= selfatari_cousin(b
, map
->to_play
, coord
);
888 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
889 add_prior_value(map
, coord
, 1.0, games
);
895 if (pp
->patternrate
) {
896 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
897 if (test_pattern3_here(p
, b
, &m
)) {
899 fprintf(stderr
, "1.0: pattern\n");
900 add_prior_value(map
, coord
, 1, games
);
908 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
910 struct moggy_policy
*pp
= p
->data
;
912 /* First, go through all endangered groups. */
913 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
914 if (group_at(map
->b
, g
) == g
)
915 playout_moggy_assess_group(p
, map
, g
, games
);
917 /* Then, assess individual moves. */
918 if (!pp
->patternrate
&& !pp
->selfatarirate
)
920 foreach_free_point(map
->b
) {
921 if (map
->consider
[c
])
922 playout_moggy_assess_one(p
, map
, c
, games
);
923 } foreach_free_point_end
;
927 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
929 struct moggy_policy
*pp
= p
->data
;
931 /* The idea is simple for now - never allow self-atari moves.
932 * They suck in general, but this also permits us to actually
933 * handle seki in the playout stage. */
935 if (fast_random(100) >= pp
->selfatarirate
) {
937 fprintf(stderr
, "skipping sar test\n");
940 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
943 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
944 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
945 if (pp
->selfatari_other
) {
946 /* Ok, try the other liberty of the atari'd group. */
947 coord_t c
= selfatari_cousin(b
, m
->color
, m
->coord
);
948 if (is_pass(c
)) return false;
950 fprintf(stderr
, "___ Redirecting to other lib %s\n",
961 struct playout_policy
*
962 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
964 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
965 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
967 p
->choose
= playout_moggy_partchoose
;
968 p
->assess
= playout_moggy_assess
;
969 p
->permit
= playout_moggy_permit
;
975 pp
->lcapturerate
= pp
->atarirate
= pp
->capturerate
= pp
->patternrate
976 = pp
->selfatarirate
= pp
->josekirate
= -1U;
977 pp
->korate
= 0; pp
->koage
= 4;
978 pp
->alwaysccaprate
= 0;
979 pp
->selfatari_other
= true;
982 double mq_prob_default
[MQ_MAX
] = {
990 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
993 char *optspec
, *next
= arg
;
996 next
+= strcspn(next
, ":");
997 if (*next
) { *next
++ = 0; } else { *next
= 0; }
999 char *optname
= optspec
;
1000 char *optval
= strchr(optspec
, '=');
1001 if (optval
) *optval
++ = 0;
1003 if (!strcasecmp(optname
, "debug") && optval
) {
1004 p
->debug_level
= atoi(optval
);
1005 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
1006 pp
->lcapturerate
= atoi(optval
);
1007 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
1008 pp
->atarirate
= atoi(optval
);
1009 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
1010 pp
->capturerate
= atoi(optval
);
1011 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
1012 pp
->patternrate
= atoi(optval
);
1013 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
1014 pp
->selfatarirate
= atoi(optval
);
1015 } else if (!strcasecmp(optname
, "korate") && optval
) {
1016 pp
->korate
= atoi(optval
);
1017 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
1018 pp
->josekirate
= atoi(optval
);
1019 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
1020 pp
->alwaysccaprate
= atoi(optval
);
1021 } else if (!strcasecmp(optname
, "rate") && optval
) {
1022 rate
= atoi(optval
);
1023 } else if (!strcasecmp(optname
, "fillboardtries")) {
1024 pp
->fillboardtries
= atoi(optval
);
1025 } else if (!strcasecmp(optname
, "koage") && optval
) {
1026 pp
->koage
= atoi(optval
);
1027 } else if (!strcasecmp(optname
, "pattern2")) {
1028 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
1029 } else if (!strcasecmp(optname
, "selfatari_other")) {
1030 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
1031 } else if (!strcasecmp(optname
, "capcheckall")) {
1032 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
1033 } else if (!strcasecmp(optname
, "fullchoose")) {
1034 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_partchoose
: playout_moggy_fullchoose
;
1035 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
1036 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
1037 for (int i
= 1; *optval
&& i
< MQ_MAX
; i
++, optval
+= strcspn(optval
, "%")) {
1039 pp
->mq_prob
[i
] = atof(optval
);
1041 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
1042 pp
->tenuki_prob
= atof(optval
);
1044 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
1049 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
1050 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
1051 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
1052 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
1053 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
1054 if (pp
->korate
== -1U) pp
->korate
= rate
;
1055 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
1056 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
1058 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);