12 #include "playout/moggy.h"
15 #include "uct/prior.h"
17 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
20 /* Note that the context can be shared by multiple threads! */
23 bool ladders
, ladderassess
, borderladders
, assess_local
;
24 int lcapturerate
, atarirate
, capturerate
, patternrate
;
27 /* Whether to look for patterns around second-to-last move. */
30 struct pattern3s patterns
;
34 static char moggy_patterns_src
[][11] = {
35 /* hane pattern - enclosing hane */
39 /* hane pattern - non-cutting hane */
43 /* hane pattern - magari */
47 /* hane pattern - thin hane */
51 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
55 /* cut1 pattern (kiri) - unprotected cut */
59 /* cut1 pattern (kiri) - peeped cut */
63 /* cut2 pattern (de) */
67 /* cut keima (not in Mogo) */
70 "???", /* o?? has some pathological tsumego cases */
71 /* side pattern - chase */
75 /* side pattern - weirdness (SUSPICIOUS) */
79 /* side pattern - sagari (SUSPICIOUS) */
81 "x.x" /* Mogo has "x.?" */
82 "###" /* Mogo has "X" */,
83 /* side pattern - throw-in (SUSPICIOUS) */
89 /* side pattern - cut (SUSPICIOUS) */
92 "###" /* Mogo has "X" */,
94 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
98 apply_pattern_here(struct playout_policy
*p
,
99 struct board
*b
, struct move
*m
, struct move_queue
*q
)
101 struct moggy_policy
*pp
= p
->data
;
102 if (test_pattern3_here(&pp
->patterns
, b
, m
))
106 /* Check if we match any pattern around given move (with the other color to play). */
108 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
)
113 /* Suicides do not make any patterns and confuse us. */
114 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
117 foreach_neighbor(b
, m
->coord
, {
118 struct move m2
; m2
.coord
= c
; m2
.color
= stone_other(m
->color
);
119 if (board_is_valid_move(b
, &m2
))
120 apply_pattern_here(p
, b
, &m2
, &q
);
122 foreach_diag_neighbor(b
, m
->coord
) {
123 struct move m2
; m2
.coord
= c
; m2
.color
= stone_other(m
->color
);
124 if (board_is_valid_move(b
, &m2
))
125 apply_pattern_here(p
, b
, &m2
, &q
);
126 } foreach_diag_neighbor_end
;
128 if (mm
) { /* Second move for pattern searching */
129 foreach_neighbor(b
, mm
->coord
, {
130 if (coord_is_8adjecent(m
->coord
, c
, b
))
132 struct move m2
; m2
.coord
= c
; m2
.color
= stone_other(m
->color
);
133 if (board_is_valid_move(b
, &m2
))
134 apply_pattern_here(p
, b
, &m2
, &q
);
136 foreach_diag_neighbor(b
, mm
->coord
) {
137 if (coord_is_8adjecent(m
->coord
, c
, b
))
139 struct move m2
; m2
.coord
= c
; m2
.color
= stone_other(m
->color
);
140 if (board_is_valid_move(b
, &m2
))
141 apply_pattern_here(p
, b
, &m2
, &q
);
142 } foreach_diag_neighbor_end
;
146 fprintf(stderr
, "Pattern candidate moves: ");
147 for (int i
= 0; i
< q
.moves
; i
++) {
148 fprintf(stderr
, "%s ", coord2sstr(q
.move
[i
], b
));
150 fprintf(stderr
, "\n");
153 int i
= fast_random(q
.moves
);
154 return q
.moves
? q
.move
[i
] : pass
;
159 /* Is this ladder breaker friendly for the one who catches ladder. */
161 ladder_catcher(struct board
*b
, int x
, int y
, enum stone laddered
)
163 enum stone breaker
= board_atxy(b
, x
, y
);
164 return breaker
== stone_other(laddered
) || breaker
== S_OFFBOARD
;
168 ladder_catches(struct playout_policy
*p
, struct board
*b
, coord_t coord
, group_t laddered
)
170 struct moggy_policy
*pp
= p
->data
;
172 /* This is very trivial and gets a lot of corner cases wrong.
173 * We need this to be just very fast. One important point is
174 * that we sometimes might not notice a ladder but if we do,
175 * it should always work; thus we can use this for strong
176 * negative hinting safely. */
178 enum stone lcolor
= board_at(b
, group_base(laddered
));
179 int x
= coord_x(coord
, b
), y
= coord_y(coord
, b
);
182 fprintf(stderr
, "ladder check - does %s play out %s's laddered group %s?\n",
183 coord2sstr(coord
, b
), stone2str(lcolor
), coord2sstr(laddered
, b
));
185 /* First, special-case first-line "ladders". This is a huge chunk
186 * of ladders we actually meet and want to play. */
187 if (pp
->borderladders
188 && neighbor_count_at(b
, coord
, S_OFFBOARD
) == 1
189 && neighbor_count_at(b
, coord
, lcolor
) == 1) {
191 fprintf(stderr
, "border ladder\n");
192 /* Direction along border; xd is horiz. border, yd vertical. */
194 if (board_atxy(b
, x
+ 1, y
) == S_OFFBOARD
|| board_atxy(b
, x
- 1, y
) == S_OFFBOARD
)
198 /* Direction from the border; -1 is above/left, 1 is below/right. */
199 int dd
= (board_atxy(b
, x
+ yd
, y
+ xd
) == S_OFFBOARD
) ? 1 : -1;
201 fprintf(stderr
, "xd %d yd %d dd %d\n", xd
, yd
, dd
);
207 /* This is normally caught, unless we have friends both above
209 if (board_atxy(b
, x
+ xd
* 2, y
+ yd
* 2) == lcolor
210 && board_atxy(b
, x
- xd
* 2, y
- yd
* 2) == lcolor
)
212 /* ...or can't block where we need because of shortage
214 int libs1
= board_group_info(b
, group_atxy(b
, x
+ xd
- yd
* dd
, y
+ yd
- xd
* dd
)).libs
;
215 int libs2
= board_group_info(b
, group_atxy(b
, x
- xd
- yd
* dd
, y
- yd
- xd
* dd
)).libs
;
217 fprintf(stderr
, "libs1 %d libs2 %d\n", libs1
, libs2
);
218 if (libs1
< 2 && libs2
< 2)
220 if (board_atxy(b
, x
+ xd
* 2, y
+ yd
* 2) == lcolor
&& libs1
< 3)
222 if (board_atxy(b
, x
- xd
* 2, y
- yd
* 2) == lcolor
&& libs2
< 3)
230 /* Figure out the ladder direction */
232 xd
= board_atxy(b
, x
+ 1, y
) == S_NONE
? 1 : board_atxy(b
, x
- 1, y
) == S_NONE
? -1 : 0;
233 yd
= board_atxy(b
, x
, y
+ 1) == S_NONE
? 1 : board_atxy(b
, x
, y
- 1) == S_NONE
? -1 : 0;
237 fprintf(stderr
, "no ladder, too little space; self-atari?\n");
241 /* For given (xd,yd), we have two possibilities where to move
242 * next. Consider (-1,-1):
247 bool horiz_first
= ladder_catcher(b
, x
, y
- yd
, lcolor
); // left case
248 bool vert_first
= ladder_catcher(b
, x
- xd
, y
, lcolor
); // right case
250 /* We don't have to look at the other 'X' in the position - if it
251 * wouldn't be there, the group wouldn't be in atari. */
253 /* We do only tight ladders, not loose ladders. Furthermore,
254 * the ladders need to be simple:
256 * c O X supported . c O unsupported
259 assert(!(horiz_first
&& vert_first
));
260 if (!horiz_first
&& !vert_first
) {
261 /* TODO: In case of basic non-simple ladder, play out both variants. */
263 fprintf(stderr
, "non-simple ladder\n");
267 /* We do that below for further moves, but now initially - check
268 * that at 'c', we aren't putting any of the catching stones
270 #if 1 // this might be broken?
271 #define check_catcher_danger(b, x_, y_) do { \
272 if (board_atxy(b, (x_), (y_)) != S_OFFBOARD \
273 && board_group_info(b, group_atxy(b, (x_), (y_))).libs <= 2) { \
275 fprintf(stderr, "ladder failed - atari at the beginning\n"); \
280 check_catcher_danger(b
, x
, y
- yd
);
281 check_catcher_danger(b
, x
- xd
, y
+ yd
);
283 check_catcher_danger(b
, x
- xd
, y
);
284 check_catcher_danger(b
, x
+ xd
, y
- yd
);
286 #undef check_catcher_danger
289 #define ladder_check(xd1_, yd1_, xd2_, yd2_, xd3_, yd3_) \
290 if (board_atxy(b, x, y) != S_NONE) { \
291 /* Did we hit a stone when playing out ladder? */ \
292 if (ladder_catcher(b, x, y, lcolor)) \
293 return true; /* ladder works */ \
294 if (board_group_info(b, group_atxy(b, x, y)).lib[0] > 0) \
295 return false; /* friend that's not in atari himself */ \
297 /* No. So we are at new position. \
298 * We need to check indirect ladder breakers. */ \
300 * . x o O 1 <- only at O we can check for o at 2 \
301 * x o o x . otherwise x at O would be still deadly \
303 * We check for o and x at 1, these are vital. \
304 * We check only for o at 2; x at 2 would mean we \
305 * need to fork (one step earlier). */ \
306 coord_t c1 = coord_xy(b, x + (xd1_), y + (yd1_)); \
307 enum stone s1 = board_at(b, c1); \
308 if (s1 == lcolor) return false; \
309 if (s1 == stone_other(lcolor)) { \
310 /* One more thing - if the position at 3 is \
311 * friendly and safe, we escaped anyway! */ \
312 coord_t c3 = coord_xy(b, x + (xd3_), y + (yd3_)); \
313 return board_at(b, c3) != lcolor \
314 || board_group_info(b, group_at(b, c3)).libs < 2; \
316 enum stone s2 = board_atxy(b, x + (xd2_), y + (yd2_)); \
317 if (s2 == lcolor) return false; \
318 /* Then, can X actually "play" 1 in the ladder? */ \
319 if (neighbor_count_at(b, c1, lcolor) + neighbor_count_at(b, c1, S_OFFBOARD) >= 2) \
320 return false; /* It would be self-atari! */ \
322 #define ladder_horiz do { if (PLDEBUGL(6)) fprintf(stderr, "%d,%d horiz step (%d,%d)\n", x, y, xd, yd); x += xd; ladder_check(xd, 0, -2 * xd, yd, 0, yd); } while (0)
323 #define ladder_vert do { if (PLDEBUGL(6)) fprintf(stderr, "%d,%d vert step of (%d,%d)\n", x, y, xd, yd); y += yd; ladder_check(0, yd, xd, -2 * yd, xd, 0); } while (0)
325 if (ladder_catcher(b
, x
- xd
, y
, lcolor
))
335 can_be_captured(struct playout_policy
*p
, struct board
*b
, enum stone capturer
, coord_t c
, enum stone to_play
)
337 if (board_at(b
, c
) != stone_other(capturer
)
338 || board_group_info(b
, group_at(b
, c
)).libs
> 1)
341 coord_t capture
= board_group_info(b
, group_at(b
, c
)).lib
[0];
343 fprintf(stderr
, "can capture group %d (%s)?\n",
344 group_at(b
, c
), coord2sstr(capture
, b
));
345 struct move m
; m
.color
= to_play
; m
.coord
= capture
;
346 /* Does that move even make sense? */
347 if (!board_is_valid_move(b
, &m
))
349 /* Make sure capturing the group will actually
351 else if (is_bad_selfatari(b
, to_play
, capture
))
358 can_be_rescued(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone color
, coord_t lib
)
360 /* Does playing on the liberty rescue the group? */
361 if (!is_bad_selfatari(b
, color
, lib
))
364 /* Then, maybe we can capture one of our neighbors? */
365 foreach_in_group(b
, group
) {
366 foreach_neighbor(b
, c
, {
367 if (!is_pass(can_be_captured(p
, b
, color
, c
, color
)))
370 } foreach_in_group_end
;
375 group_atari_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
, struct move_queue
*q
, coord_t
*ladder
)
377 int qmoves_prev
= q
->moves
;
379 /* We don't use @to_play almost anywhere since any moves here are good
380 * for both defender and attacker. */
382 enum stone color
= board_at(b
, group_base(group
));
383 coord_t lib
= board_group_info(b
, group
).lib
[0];
385 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
387 fprintf(stderr
, "[%s] atariiiiiiiii %s of color %d\n", coord2sstr(group
, b
), coord2sstr(lib
, b
), color
);
388 assert(board_at(b
, lib
) == S_NONE
);
390 /* Do not bother with kos. */
391 if (group_is_onestone(b
, group
)
392 && neighbor_count_at(b
, lib
, color
) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 4)
395 /* Can we capture some neighbor? */
396 foreach_in_group(b
, group
) {
397 foreach_neighbor(b
, c
, {
398 coord_t capture
= can_be_captured(p
, b
, color
, c
, to_play
);
399 if (is_pass(capture
))
405 } foreach_in_group_end
;
407 struct move m
; m
.color
= to_play
; m
.coord
= lib
;
408 if (!board_is_valid_move(b
, &m
))
411 /* Do not suicide... */
412 if (is_bad_selfatari(b
, to_play
, lib
))
414 /* Do not remove group that cannot be saved by the opponent. */
415 if (to_play
!= color
&& !can_be_rescued(p
, b
, group
, color
, lib
))
418 fprintf(stderr
, "...escape route valid\n");
420 /* ...or play out ladders. */
421 if (ladder_catches(p
, b
, lib
, group
)) {
422 /* Sometimes we want to keep the ladder move in the
423 * queue in order to discourage it. */
430 fprintf(stderr
, "...no ladder\n");
432 if (to_play
!= color
) {
433 /* We are the attacker! In that case, throw away the moves
434 * that defend our groups, since we can capture the culprit. */
435 q
->moves
= qmoves_prev
;
443 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
451 int g_base
= fast_random(b
->clen
);
452 for (int g
= g_base
; g
< b
->clen
; g
++) {
453 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, &q
, NULL
);
455 return q
.move
[fast_random(q
.moves
)];
457 for (int g
= 0; g
< g_base
; g
++) {
458 group_atari_check(p
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, &q
, NULL
);
460 return q
.move
[fast_random(q
.moves
)];
466 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
471 /* Did the opponent play a self-atari? */
472 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
473 group_atari_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), &q
, NULL
);
476 foreach_neighbor(b
, m
->coord
, {
477 group_t g
= group_at(b
, c
);
478 if (!g
|| board_group_info(b
, g
).libs
!= 1)
480 group_atari_check(p
, b
, g
, stone_other(m
->color
), &q
, NULL
);
484 fprintf(stderr
, "Local atari candidate moves: ");
485 for (int i
= 0; i
< q
.moves
; i
++) {
486 fprintf(stderr
, "%s ", coord2sstr(q
.move
[i
], b
));
488 fprintf(stderr
, "\n");
491 int i
= fast_random(q
.moves
);
492 return q
.moves
? q
.move
[i
] : pass
;
496 miai_2lib(struct board
*b
, group_t group
, enum stone color
)
498 bool can_connect
= false, can_pull_out
= false;
499 /* We have miai if we can either connect on both libs,
500 * or connect on one lib and escape on another. (Just
501 * having two escape routes can be risky.) */
502 foreach_neighbor(b
, board_group_info(b
, group
).lib
[0], {
503 enum stone cc
= board_at(b
, c
);
504 if (cc
== S_NONE
&& cc
!= board_group_info(b
, group
).lib
[1]) {
506 } else if (cc
!= color
) {
510 group_t cg
= group_at(b
, c
);
511 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
514 foreach_neighbor(b
, board_group_info(b
, group
).lib
[1], {
515 enum stone cc
= board_at(b
, c
);
516 if (cc
== S_NONE
&& cc
!= board_group_info(b
, group
).lib
[0] && can_connect
) {
518 } else if (cc
!= color
) {
522 group_t cg
= group_at(b
, c
);
523 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
524 return (can_connect
|| can_pull_out
);
530 group_2lib_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
, struct move_queue
*q
)
532 enum stone color
= board_at(b
, group_base(group
));
533 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
536 fprintf(stderr
, "[%s] 2lib check of color %d\n",
537 coord2sstr(group
, b
), color
);
539 /* Do not try to atari groups that cannot be harmed. */
540 if (miai_2lib(b
, group
, color
))
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 struct move m
; m
.color
= to_play
; m
.coord
= lib
;
547 if (!board_is_valid_move(b
, &m
))
550 /* Don't play at the spot if it is extremely short
552 /* XXX: This looks harmful, could significantly
553 * prefer atari to throwin:
559 if (neighbor_count_at(b
, lib
, stone_other(color
)) + immediate_liberty_count(b
, lib
) < 2)
563 /* If the owner can't play at the spot, we don't want
564 * to bother either. */
565 if (is_bad_selfatari(b
, color
, lib
))
568 /* Of course we don't want to play bad selfatari
569 * ourselves, if we are the attacker... */
570 if (to_play
!= color
&& is_bad_selfatari(b
, to_play
, lib
))
573 /* Tasty! Crispy! Good! */
579 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
584 /* Does the opponent have just two liberties? */
585 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 2) {
586 group_2lib_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), &q
);
588 /* We always prefer to take off an enemy chain liberty
589 * before pulling out ourselves. */
590 /* XXX: We aren't guaranteed to return to that group
593 return q
.move
[fast_random(q
.moves
)];
597 /* Then he took a third liberty from neighboring chain? */
598 foreach_neighbor(b
, m
->coord
, {
599 group_t g
= group_at(b
, c
);
600 if (!g
|| board_group_info(b
, g
).libs
!= 2)
602 group_2lib_check(p
, b
, g
, stone_other(m
->color
), &q
);
606 fprintf(stderr
, "Local 2lib candidate moves: ");
607 for (int i
= 0; i
< q
.moves
; i
++) {
608 fprintf(stderr
, "%s ", coord2sstr(q
.move
[i
], b
));
610 fprintf(stderr
, "\n");
613 int i
= fast_random(q
.moves
);
614 return q
.moves
? q
.move
[i
] : pass
;
618 playout_moggy_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
620 struct moggy_policy
*pp
= p
->data
;
624 board_print(b
, stderr
);
627 if (!is_pass(b
->last_move
.coord
)) {
628 /* Local group in atari? */
629 if (pp
->lcapturerate
> fast_random(100)) {
630 c
= local_atari_check(p
, b
, &b
->last_move
);
635 /* Local group can be PUT in atari? */
636 if (pp
->atarirate
> fast_random(100)) {
637 c
= local_2lib_check(p
, b
, &b
->last_move
);
642 /* Check for patterns we know */
643 if (pp
->patternrate
> fast_random(100)) {
644 c
= apply_pattern(p
, b
, &b
->last_move
,
645 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
);
653 /* Any groups in atari? */
654 if (pp
->capturerate
> fast_random(100)) {
655 c
= global_atari_check(p
, b
, to_play
);
661 int fbtries
= b
->flen
/ 8;
662 for (int i
= 0; i
< (fbtries
< pp
->fillboardtries
? fbtries
: pp
->fillboardtries
); i
++) {
663 coord_t coord
= b
->f
[fast_random(b
->flen
)];
664 if (is_pass(coord
) || immediate_liberty_count(b
, coord
) != 4)
666 foreach_diag_neighbor(b
, coord
) {
667 if (board_at(b
, c
) != S_NONE
)
669 } foreach_diag_neighbor_end
;
679 assess_local_bonus(struct playout_policy
*p
, struct board
*board
, coord_t a
, coord_t b
, int games
)
681 struct moggy_policy
*pp
= p
->data
;
682 if (!pp
->assess_local
)
685 int dx
= abs(coord_x(a
, board
) - coord_x(b
, board
));
686 int dy
= abs(coord_y(a
, board
) - coord_y(b
, board
));
687 /* adjecent move, directly or diagonally? */
688 if (dx
+ dy
<= 1 + (dx
&& dy
))
695 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
697 struct moggy_policy
*pp
= p
->data
;
698 struct board
*b
= map
->b
;
699 struct move_queue q
; q
.moves
= 0;
701 if (board_group_info(b
, g
).libs
> 2)
705 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
706 board_print(b
, stderr
);
709 if (board_group_info(b
, g
).libs
== 2) {
712 group_2lib_check(p
, b
, g
, map
->to_play
, &q
);
714 coord_t coord
= q
.move
[q
.moves
];
716 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
717 int assess
= assess_local_bonus(p
, b
, b
->last_move
.coord
, coord
, games
) / 2;
718 add_prior_value(map
, coord
, assess
, assess
);
723 /* This group, sir, is in atari! */
725 if (!pp
->capturerate
&& !pp
->lcapturerate
&& !pp
->ladderassess
)
728 coord_t ladder
= pass
;
729 group_atari_check(p
, b
, g
, map
->to_play
, &q
, &ladder
);
731 coord_t coord
= q
.move
[q
.moves
];
733 /* _Never_ play here if this move plays out
734 * a caught ladder. */
735 if (coord
== ladder
) {
736 /* Note that the opposite is not guarded against;
737 * we do not advise against capturing a laddered
738 * group (but we don't encourage it either). Such
739 * a move can simplify tactical situations if we
741 if (!pp
->ladderassess
|| map
->to_play
!= board_at(b
, g
))
743 /* FIXME: We give the malus even if this move
744 * captures another group. */
746 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
747 add_prior_value(map
, coord
, -games
, games
);
751 if (!pp
->capturerate
&& !pp
->lcapturerate
)
755 fprintf(stderr
, "1.0: atari %s\n", coord2sstr(coord
, b
));
756 int assess
= assess_local_bonus(p
, b
, b
->last_move
.coord
, coord
, games
) * 2;
757 add_prior_value(map
, coord
, assess
, assess
);
762 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
764 struct moggy_policy
*pp
= p
->data
;
765 struct board
*b
= map
->b
;
768 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
769 board_print(b
, stderr
);
772 /* Is this move a self-atari? */
773 if (pp
->selfatarirate
) {
774 if (is_bad_selfatari(b
, map
->to_play
, coord
)) {
776 fprintf(stderr
, "0.0: self-atari\n");
782 if (pp
->patternrate
) {
783 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
784 if (test_pattern3_here(&pp
->patterns
, b
, &m
)) {
786 fprintf(stderr
, "1.0: pattern\n");
787 return assess_local_bonus(p
, b
, b
->last_move
.coord
, coord
, games
);
795 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
797 struct moggy_policy
*pp
= p
->data
;
799 /* First, go through all endangered groups. */
800 if (pp
->lcapturerate
|| pp
->capturerate
|| pp
->atarirate
|| pp
->ladderassess
)
801 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
802 if (group_at(map
->b
, g
) == g
)
803 playout_moggy_assess_group(p
, map
, g
, games
);
805 /* Then, assess individual moves. */
806 if (!pp
->patternrate
&& !pp
->selfatarirate
)
808 foreach_point(map
->b
) {
809 if (!map
->consider
[c
])
811 int assess
= playout_moggy_assess_one(p
, map
, c
, games
);
814 add_prior_value(map
, c
, assess
, abs(assess
));
819 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
821 struct moggy_policy
*pp
= p
->data
;
823 /* The idea is simple for now - never allow self-atari moves.
824 * They suck in general, but this also permits us to actually
825 * handle seki in the playout stage. */
827 if (fast_random(100) >= pp
->selfatarirate
) {
829 fprintf(stderr
, "skipping sar test\n");
832 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
833 if (PLDEBUGL(5) && selfatari
)
834 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
835 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
840 struct playout_policy
*
841 playout_moggy_init(char *arg
)
843 struct playout_policy
*p
= calloc(1, sizeof(*p
));
844 struct moggy_policy
*pp
= calloc(1, sizeof(*pp
));
846 p
->choose
= playout_moggy_choose
;
847 p
->assess
= playout_moggy_assess
;
848 p
->permit
= playout_moggy_permit
;
852 pp
->lcapturerate
= pp
->atarirate
= pp
->capturerate
= pp
->patternrate
= pp
->selfatarirate
= -1;
853 pp
->ladders
= pp
->borderladders
= true;
854 pp
->ladderassess
= true;
857 char *optspec
, *next
= arg
;
860 next
+= strcspn(next
, ":");
861 if (*next
) { *next
++ = 0; } else { *next
= 0; }
863 char *optname
= optspec
;
864 char *optval
= strchr(optspec
, '=');
865 if (optval
) *optval
++ = 0;
867 if (!strcasecmp(optname
, "lcapturerate") && optval
) {
868 pp
->lcapturerate
= atoi(optval
);
869 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
870 pp
->atarirate
= atoi(optval
);
871 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
872 pp
->capturerate
= atoi(optval
);
873 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
874 pp
->patternrate
= atoi(optval
);
875 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
876 pp
->selfatarirate
= atoi(optval
);
877 } else if (!strcasecmp(optname
, "rate") && optval
) {
879 } else if (!strcasecmp(optname
, "fillboardtries")) {
880 pp
->fillboardtries
= atoi(optval
);
881 } else if (!strcasecmp(optname
, "ladders")) {
882 pp
->ladders
= optval
&& *optval
== '0' ? false : true;
883 } else if (!strcasecmp(optname
, "borderladders")) {
884 pp
->borderladders
= optval
&& *optval
== '0' ? false : true;
885 } else if (!strcasecmp(optname
, "ladderassess")) {
886 pp
->ladderassess
= optval
&& *optval
== '0' ? false : true;
887 } else if (!strcasecmp(optname
, "assess_local")) {
888 pp
->assess_local
= optval
&& *optval
== '0' ? false : true;
889 } else if (!strcasecmp(optname
, "pattern2")) {
890 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
892 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
897 if (pp
->lcapturerate
== -1) pp
->lcapturerate
= rate
;
898 if (pp
->atarirate
== -1) pp
->atarirate
= rate
;
899 if (pp
->capturerate
== -1) pp
->capturerate
= rate
;
900 if (pp
->patternrate
== -1) pp
->patternrate
= rate
;
901 if (pp
->selfatarirate
== -1) pp
->selfatarirate
= rate
;
903 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);