group_wlib_check(): Drop unused playout_policy parameter
[pachi/pachi-r6144.git] / playout / moggy.c
blob787b34e45de75022057273366e13cc3b67f84697
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. */
5 #include <assert.h>
6 #include <math.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 #define DEBUG
11 #include "board.h"
12 #include "debug.h"
13 #include "joseki/base.h"
14 #include "mq.h"
15 #include "pattern3.h"
16 #include "playout.h"
17 #include "playout/moggy.h"
18 #include "random.h"
19 #include "tactics/1lib.h"
20 #include "tactics/ladder.h"
21 #include "tactics/selfatari.h"
22 #include "uct/prior.h"
24 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
26 /* Whether to avoid capturing/atariing doomed groups (this is big
27 * performance hit and may reduce playouts balance; it does increase
28 * the strength, but not quite proportionally to the performance). */
29 //#define NO_DOOMED_GROUPS
32 /* Move queue tags: */
33 enum mq_tag {
34 MQ_KO = 1,
35 MQ_LATARI,
36 MQ_L2LIB,
37 MQ_PAT3,
38 MQ_GATARI,
39 MQ_JOSEKI,
40 MQ_MAX
44 /* Note that the context can be shared by multiple threads! */
46 struct moggy_policy {
47 unsigned int lcapturerate, atarirate, capturerate, patternrate, korate, josekirate;
48 unsigned int selfatarirate, alwaysccaprate;
49 unsigned int fillboardtries;
50 int koage;
51 /* Whether to look for patterns around second-to-last move. */
52 bool pattern2;
53 /* Whether, when self-atari attempt is detected, to play the other
54 * group's liberty if that is non-self-atari. */
55 bool selfatari_other;
56 /* Whether to always pick from moves capturing all groups in
57 * global_atari_check(). */
58 bool capcheckall;
60 struct joseki_dict *jdict;
61 struct pattern3s patterns;
63 /* Gamma values for queue tags - correspond to probabilities. */
64 /* XXX: Tune. */
65 double mq_prob[MQ_MAX], tenuki_prob;
69 static char moggy_patterns_src[][11] = {
70 /* hane pattern - enclosing hane */
71 "XOX"
72 "..."
73 "???",
74 /* hane pattern - non-cutting hane */
75 "XO."
76 "..."
77 "?.?",
78 /* hane pattern - magari */
79 "XO?"
80 "X.."
81 "x.?",
82 /* hane pattern - thin hane */
83 "XOO"
84 "..."
85 "?.?" "X",
86 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
87 ".O."
88 "X.."
89 "...",
90 /* cut1 pattern (kiri) - unprotected cut */
91 "XO?"
92 "O.o"
93 "?o?",
94 /* cut1 pattern (kiri) - peeped cut */
95 "XO?"
96 "O.X"
97 "???",
98 /* cut2 pattern (de) */
99 "?X?"
100 "O.O"
101 "ooo",
102 /* cut keima (not in Mogo) */
103 "OX?"
104 "o.O"
105 "???", /* o?? has some pathological tsumego cases */
106 /* side pattern - chase */
107 "X.?"
108 "O.?"
109 "##?",
110 /* side pattern - block side cut */
111 "OX?"
112 "X.O"
113 "###",
114 /* side pattern - block side connection */
115 "?X?"
116 "x.O"
117 "###",
118 /* side pattern - sagari (SUSPICIOUS) */
119 "?XO"
120 "x.x" /* Mogo has "x.?" */
121 "###" /* Mogo has "X" */,
122 /* side pattern - throw-in (SUSPICIOUS) */
123 #if 0
124 "?OX"
125 "o.O"
126 "?##" "X",
127 #endif
128 /* side pattern - cut (SUSPICIOUS) */
129 "?OX"
130 "X.O"
131 "###" /* Mogo has "X" */,
133 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
135 static inline bool
136 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
138 struct moggy_policy *pp = p->data;
139 /* Check if 3x3 pattern is matched by given move... */
140 if (!pattern3_move_here(&pp->patterns, b, m))
141 return false;
142 /* ...and the move is not obviously stupid. */
143 if (is_bad_selfatari(b, m->color, m->coord))
144 return false;
145 /* Ladder moves are stupid. */
146 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
147 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
148 return false;
149 return true;
152 static void
153 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
155 struct move m2 = { .coord = c, .color = color };
156 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
157 mq_add(q, c, 1<<MQ_PAT3);
160 /* Check if we match any pattern around given move (with the other color to play). */
161 static void
162 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
164 /* Suicides do not make any patterns and confuse us. */
165 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
166 return;
168 foreach_8neighbor(b, m->coord) {
169 apply_pattern_here(p, b, c, stone_other(m->color), q);
170 } foreach_8neighbor_end;
172 if (mm) { /* Second move for pattern searching */
173 foreach_8neighbor(b, mm->coord) {
174 if (coord_is_8adjecent(m->coord, c, b))
175 continue;
176 apply_pattern_here(p, b, c, stone_other(m->color), q);
177 } foreach_8neighbor_end;
180 if (PLDEBUGL(5))
181 mq_print(q, b, "Pattern");
185 static void
186 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
188 struct moggy_policy *pp = p->data;
189 if (!pp->jdict)
190 return;
192 for (int i = 0; i < 4; i++) {
193 hash_t h = b->qhash[i] & joseki_hash_mask;
194 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
195 if (!cc) continue;
196 for (; !is_pass(*cc); cc++) {
197 if (coord_quadrant(*cc, b) != i)
198 continue;
199 mq_add(q, *cc, 1<<MQ_JOSEKI);
203 if (q->moves > 0 && PLDEBUGL(5))
204 mq_print(q, b, "Joseki");
207 static void
208 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
210 if (b->clen == 0)
211 return;
213 struct moggy_policy *pp = p->data;
214 if (pp->capcheckall) {
215 for (int g = 0; g < b->clen; g++)
216 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
217 if (PLDEBUGL(5))
218 mq_print(q, b, "Global atari");
219 return;
222 int g_base = fast_random(b->clen);
223 for (int g = g_base; g < b->clen; g++) {
224 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
225 if (q->moves > 0) {
226 /* XXX: Try carrying on. */
227 if (PLDEBUGL(5))
228 mq_print(q, b, "Global atari");
229 return;
232 for (int g = 0; g < g_base; g++) {
233 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
234 if (q->moves > 0) {
235 /* XXX: Try carrying on. */
236 if (PLDEBUGL(5))
237 mq_print(q, b, "Global atari");
238 return;
241 return;
244 static void
245 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
247 struct moggy_policy *pp = p->data;
249 /* Did the opponent play a self-atari? */
250 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
251 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
254 foreach_neighbor(b, m->coord, {
255 group_t g = group_at(b, c);
256 if (!g || board_group_info(b, g).libs != 1)
257 continue;
258 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
261 if (PLDEBUGL(5))
262 mq_print(q, b, "Local atari");
265 static bool
266 miai_2lib(struct board *b, group_t group, enum stone color)
268 bool can_connect = false, can_pull_out = false;
269 /* We have miai if we can either connect on both libs,
270 * or connect on one lib and escape on another. (Just
271 * having two escape routes can be risky.) We must make
272 * sure that we don't consider following as miai:
273 * X X X O
274 * X . . O
275 * O O X O - left dot would be pull-out, right dot connect */
276 foreach_neighbor(b, board_group_info(b, group).lib[0], {
277 enum stone cc = board_at(b, c);
278 if (cc == S_NONE && cc != board_at(b, board_group_info(b, group).lib[1])) {
279 can_pull_out = true;
280 } else if (cc != color) {
281 continue;
284 group_t cg = group_at(b, c);
285 if (cg && cg != group && board_group_info(b, cg).libs > 1)
286 can_connect = true;
288 foreach_neighbor(b, board_group_info(b, group).lib[1], {
289 enum stone cc = board_at(b, c);
290 if (c == board_group_info(b, group).lib[0])
291 continue;
292 if (cc == S_NONE && can_connect) {
293 return true;
294 } else if (cc != color) {
295 continue;
298 group_t cg = group_at(b, c);
299 if (cg && cg != group && board_group_info(b, cg).libs > 1)
300 return (can_connect || can_pull_out);
302 return false;
305 static void
306 check_group_atari(struct board *b, group_t group, enum stone owner,
307 enum stone to_play, struct move_queue *q)
309 for (int i = 0; i < 2; i++) {
310 coord_t lib = board_group_info(b, group).lib[i];
311 assert(board_at(b, lib) == S_NONE);
312 if (!board_is_valid_play(b, to_play, lib))
313 continue;
315 /* Don't play at the spot if it is extremely short
316 * of liberties... */
317 /* XXX: This looks harmful, could significantly
318 * prefer atari to throwin:
320 * XXXOOOOOXX
321 * .OO.....OX
322 * XXXOOOOOOX */
323 #if 0
324 if (neighbor_count_at(b, lib, stone_other(owner)) + immediate_liberty_count(b, lib) < 2)
325 continue;
326 #endif
328 /* If the move is too "lumpy", do not play it:
330 * #######
331 * ..O.X.X <- always play the left one!
332 * OXXXXXX */
333 if (neighbor_count_at(b, lib, stone_other(owner)) + neighbor_count_at(b, lib, S_OFFBOARD) == 3)
334 continue;
336 #ifdef NO_DOOMED_GROUPS
337 /* If the owner can't play at the spot, we don't want
338 * to bother either. */
339 if (is_bad_selfatari(b, owner, lib))
340 continue;
341 #endif
343 /* Of course we don't want to play bad selfatari
344 * ourselves, if we are the attacker... */
345 if (
346 #ifdef NO_DOOMED_GROUPS
347 to_play != owner &&
348 #endif
349 is_bad_selfatari(b, to_play, lib))
350 continue;
352 /* Tasty! Crispy! Good! */
353 mq_add(q, lib, 1<<MQ_L2LIB);
354 mq_nodup(q);
358 static void
359 group_2lib_check(struct board *b, group_t group, enum stone to_play, struct move_queue *q)
361 enum stone color = board_at(b, group_base(group));
362 assert(color != S_OFFBOARD && color != S_NONE);
364 if (DEBUGL(5))
365 fprintf(stderr, "[%s] 2lib check of color %d\n",
366 coord2sstr(group, b), color);
368 /* Do not try to atari groups that cannot be harmed. */
369 if (miai_2lib(b, group, color))
370 return;
372 check_group_atari(b, group, color, to_play, q);
374 /* Can we counter-atari another group, if we are the defender? */
375 if (to_play != color)
376 return;
377 foreach_in_group(b, group) {
378 foreach_neighbor(b, c, {
379 if (board_at(b, c) != stone_other(color))
380 continue;
381 group_t g2 = group_at(b, c);
382 if (board_group_info(b, g2).libs == 1) {
383 /* We can capture a neighbor. */
384 mq_add(q, board_group_info(b, g2).lib[0], 1<<MQ_L2LIB);
385 mq_nodup(q);
386 continue;
388 if (board_group_info(b, g2).libs != 2)
389 continue;
390 check_group_atari(b, g2, color, to_play, q);
392 } foreach_in_group_end;
395 static void
396 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
398 /* Does the opponent have just two liberties? */
399 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
400 group_2lib_check(b, group_at(b, m->coord), stone_other(m->color), q);
401 #if 0
402 /* We always prefer to take off an enemy chain liberty
403 * before pulling out ourselves. */
404 /* XXX: We aren't guaranteed to return to that group
405 * later. */
406 if (q->moves)
407 return q->move[fast_random(q->moves)];
408 #endif
411 /* Then he took a third liberty from neighboring chain? */
412 foreach_neighbor(b, m->coord, {
413 group_t g = group_at(b, c);
414 if (!g || board_group_info(b, g).libs != 2)
415 continue;
416 group_2lib_check(b, g, stone_other(m->color), q);
419 if (PLDEBUGL(5))
420 mq_print(q, b, "Local 2lib");
423 coord_t
424 fillboard_check(struct playout_policy *p, struct board *b)
426 struct moggy_policy *pp = p->data;
427 unsigned int fbtries = b->flen / 8;
428 if (pp->fillboardtries < fbtries)
429 fbtries = pp->fillboardtries;
431 for (unsigned int i = 0; i < fbtries; i++) {
432 coord_t coord = b->f[fast_random(b->flen)];
433 if (immediate_liberty_count(b, coord) != 4)
434 continue;
435 foreach_diag_neighbor(b, coord) {
436 if (board_at(b, c) != S_NONE)
437 goto next_try;
438 } foreach_diag_neighbor_end;
439 return coord;
440 next_try:;
442 return pass;
445 coord_t
446 playout_moggy_partchoose(struct playout_policy *p, struct board *b, enum stone to_play)
448 struct moggy_policy *pp = p->data;
450 if (PLDEBUGL(5))
451 board_print(b, stderr);
453 /* Ko fight check */
454 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
455 && b->moves - b->last_ko_age < pp->koage
456 && pp->korate > fast_random(100)) {
457 if (board_is_valid_play(b, to_play, b->last_ko.coord)
458 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
459 return b->last_ko.coord;
462 /* Local checks */
463 if (!is_pass(b->last_move.coord)) {
464 /* Local group in atari? */
465 if (pp->lcapturerate > fast_random(100)) {
466 struct move_queue q; q.moves = 0;
467 local_atari_check(p, b, &b->last_move, &q);
468 if (q.moves > 0)
469 return mq_pick(&q);
472 /* Local group can be PUT in atari? */
473 if (pp->atarirate > fast_random(100)) {
474 struct move_queue q; q.moves = 0;
475 local_2lib_check(p, b, &b->last_move, &q);
476 if (q.moves > 0)
477 return mq_pick(&q);
480 /* Check for patterns we know */
481 if (pp->patternrate > fast_random(100)) {
482 struct move_queue q; q.moves = 0;
483 apply_pattern(p, b, &b->last_move,
484 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
485 &q);
486 if (q.moves > 0)
487 return mq_pick(&q);
491 /* Global checks */
493 /* Any groups in atari? */
494 if (pp->capturerate > fast_random(100)) {
495 struct move_queue q; q.moves = 0;
496 global_atari_check(p, b, to_play, &q);
497 if (q.moves > 0)
498 return mq_pick(&q);
501 /* Joseki moves? */
502 if (pp->josekirate > fast_random(100)) {
503 struct move_queue q; q.moves = 0;
504 joseki_check(p, b, to_play, &q);
505 if (q.moves > 0)
506 return mq_pick(&q);
509 /* Fill board */
510 if (pp->fillboardtries > 0) {
511 coord_t c = fillboard_check(p, b);
512 if (!is_pass(c))
513 return c;
516 return pass;
519 /* Pick a move from queue q, giving different likelihoods to moves
520 * based on their tags. */
521 coord_t
522 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
524 struct moggy_policy *pp = p->data;
526 /* First, merge all entries for a move. */
527 /* We use a naive O(N^2) since the average length of the queue
528 * is about 1.4. */
529 for (unsigned int i = 0; i < q->moves; i++) {
530 for (unsigned int j = i + 1; j < q->moves; j++) {
531 if (q->move[i] != q->move[j])
532 continue;
533 q->tag[i] |= q->tag[j];
534 q->moves--;
535 q->tag[j] = q->tag[q->moves];
536 q->move[j] = q->move[q->moves];
540 /* Now, construct a probdist. */
541 fixp_t total = 0;
542 fixp_t pd[q->moves];
543 for (unsigned int i = 0; i < q->moves; i++) {
544 double val = 1.0;
545 assert(q->tag[i] != 0);
546 for (int j = 1; j < MQ_MAX; j++)
547 if (q->tag[i] & (1<<j)) {
548 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
549 val *= pp->mq_prob[j];
551 pd[i] = double_to_fixp(val);
552 total += pd[i];
554 total += double_to_fixp(pp->tenuki_prob);
556 /* Finally, pick a move! */
557 fixp_t stab = fast_irandom(total);
558 for (unsigned int i = 0; i < q->moves; i++) {
559 //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));
560 if (stab < pd[i])
561 return q->move[i];
562 stab -= pd[i];
565 /* Tenuki. */
566 assert(stab < double_to_fixp(pp->tenuki_prob));
567 return pass;
570 coord_t
571 playout_moggy_fullchoose(struct playout_policy *p, struct board *b, enum stone to_play)
573 struct moggy_policy *pp = p->data;
574 struct move_queue q; q.moves = 0;
576 if (PLDEBUGL(5))
577 board_print(b, stderr);
579 /* Ko fight check */
580 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
581 && b->moves - b->last_ko_age < pp->koage
582 && pp->korate > fast_random(100)) {
583 if (board_is_valid_play(b, to_play, b->last_ko.coord)
584 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
585 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
588 /* Local checks */
589 if (!is_pass(b->last_move.coord)) {
590 /* Local group in atari? */
591 if (pp->lcapturerate > fast_random(100)) {
592 local_atari_check(p, b, &b->last_move, &q);
595 /* Local group can be PUT in atari? */
596 if (pp->atarirate > fast_random(100)) {
597 local_2lib_check(p, b, &b->last_move, &q);
600 /* Check for patterns we know */
601 if (pp->patternrate > fast_random(100)) {
602 apply_pattern(p, b, &b->last_move,
603 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
604 &q);
608 /* Global checks */
610 /* Any groups in atari? */
611 if (pp->capturerate > fast_random(100)) {
612 global_atari_check(p, b, to_play, &q);
615 /* Joseki moves? */
616 if (pp->josekirate > fast_random(100)) {
617 joseki_check(p, b, to_play, &q);
620 #if 0
621 /* Average length of the queue is 1.4 move. */
622 printf("MQL %d ", q.moves);
623 for (unsigned int i = 0; i < q.moves; i++)
624 printf("%s ", coord2sstr(q.move[i], b));
625 printf("\n");
626 #endif
628 if (q.moves > 0)
629 return mq_tagged_choose(p, b, to_play, &q);
631 /* Fill board */
632 if (pp->fillboardtries > 0) {
633 coord_t c = fillboard_check(p, b);
634 if (!is_pass(c))
635 return c;
638 return pass;
642 static coord_t
643 selfatari_cousin(struct board *b, enum stone color, coord_t coord)
645 group_t groups[4]; int groups_n = 0;
646 foreach_neighbor(b, coord, {
647 enum stone s = board_at(b, c);
648 if (s != color) continue;
649 group_t g = group_at(b, c);
650 if (board_group_info(b, g).libs == 2)
651 groups[groups_n++] = g;
654 if (!groups_n)
655 return pass;
656 group_t group = groups[fast_random(groups_n)];
658 coord_t lib2 = board_group_other_lib(b, group, coord);
659 if (is_bad_selfatari(b, color, lib2))
660 return pass;
661 return lib2;
664 void
665 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
667 struct moggy_policy *pp = p->data;
668 struct board *b = map->b;
669 struct move_queue q; q.moves = 0;
671 if (board_group_info(b, g).libs > 2)
672 return;
674 if (PLDEBUGL(5)) {
675 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
676 board_print(b, stderr);
679 if (board_group_info(b, g).libs == 2) {
680 if (!pp->atarirate)
681 return;
682 group_2lib_check(b, g, map->to_play, &q);
683 while (q.moves--) {
684 coord_t coord = q.move[q.moves];
685 if (PLDEBUGL(5))
686 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
687 int assess = games / 2;
688 add_prior_value(map, coord, 1, assess);
690 return;
693 /* This group, sir, is in atari! */
695 coord_t ladder = pass;
696 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
697 while (q.moves--) {
698 coord_t coord = q.move[q.moves];
700 /* _Never_ play here if this move plays out
701 * a caught ladder. */
702 if (coord == ladder && !board_playing_ko_threat(b)) {
703 /* Note that the opposite is not guarded against;
704 * we do not advise against capturing a laddered
705 * group (but we don't encourage it either). Such
706 * a move can simplify tactical situations if we
707 * can afford it. */
708 if (map->to_play != board_at(b, g))
709 continue;
710 /* FIXME: We give the malus even if this move
711 * captures another group. */
712 if (PLDEBUGL(5))
713 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
714 add_prior_value(map, coord, 0, games);
715 continue;
718 if (!pp->capturerate && !pp->lcapturerate)
719 continue;
721 if (PLDEBUGL(5))
722 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
723 int assess = games * 2;
724 add_prior_value(map, coord, 1, assess);
728 void
729 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
731 struct moggy_policy *pp = p->data;
732 struct board *b = map->b;
734 if (PLDEBUGL(5)) {
735 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
736 board_print(b, stderr);
739 /* Is this move a self-atari? */
740 if (pp->selfatarirate) {
741 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
742 if (PLDEBUGL(5))
743 fprintf(stderr, "0.0: self-atari\n");
744 add_prior_value(map, coord, 0, games);
745 if (!pp->selfatari_other)
746 return;
747 /* If we can play on the other liberty of the
748 * endangered group, do! */
749 coord = selfatari_cousin(b, map->to_play, coord);
750 if (is_pass(coord))
751 return;
752 if (PLDEBUGL(5))
753 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
754 add_prior_value(map, coord, 1.0, games);
755 return;
759 /* Pattern check */
760 if (pp->patternrate) {
761 struct move m = { .color = map->to_play, .coord = coord };
762 if (test_pattern3_here(p, b, &m)) {
763 if (PLDEBUGL(5))
764 fprintf(stderr, "1.0: pattern\n");
765 add_prior_value(map, coord, 1, games);
769 return;
772 void
773 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
775 struct moggy_policy *pp = p->data;
777 /* First, go through all endangered groups. */
778 for (group_t g = 1; g < board_size2(map->b); g++)
779 if (group_at(map->b, g) == g)
780 playout_moggy_assess_group(p, map, g, games);
782 /* Then, assess individual moves. */
783 if (!pp->patternrate && !pp->selfatarirate)
784 return;
785 foreach_free_point(map->b) {
786 if (map->consider[c])
787 playout_moggy_assess_one(p, map, c, games);
788 } foreach_free_point_end;
791 bool
792 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
794 struct moggy_policy *pp = p->data;
796 /* The idea is simple for now - never allow self-atari moves.
797 * They suck in general, but this also permits us to actually
798 * handle seki in the playout stage. */
800 if (fast_random(100) >= pp->selfatarirate) {
801 if (PLDEBUGL(5))
802 fprintf(stderr, "skipping sar test\n");
803 return true;
805 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
806 if (selfatari) {
807 if (PLDEBUGL(5))
808 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
809 stone2str(m->color), coord2sstr(m->coord, b));
810 if (pp->selfatari_other) {
811 /* Ok, try the other liberty of the atari'd group. */
812 coord_t c = selfatari_cousin(b, m->color, m->coord);
813 if (is_pass(c)) return false;
814 if (PLDEBUGL(5))
815 fprintf(stderr, "___ Redirecting to other lib %s\n",
816 coord2sstr(c, b));
817 m->coord = c;
818 return true;
820 return false;
822 return true;
826 struct playout_policy *
827 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
829 struct playout_policy *p = calloc2(1, sizeof(*p));
830 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
831 p->data = pp;
832 p->choose = playout_moggy_partchoose;
833 p->assess = playout_moggy_assess;
834 p->permit = playout_moggy_permit;
836 pp->jdict = jdict;
838 int rate = 90;
840 pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate
841 = pp->selfatarirate = pp->josekirate = -1U;
842 pp->korate = 0; pp->koage = 4;
843 pp->alwaysccaprate = 0;
844 pp->selfatari_other = true;
846 /* C is stupid. */
847 double mq_prob_default[MQ_MAX] = {
848 [MQ_KO] = 6.0,
849 [MQ_LATARI] = 5.0,
850 [MQ_L2LIB] = 4.0,
851 [MQ_PAT3] = 3.0,
852 [MQ_GATARI] = 2.0,
853 [MQ_JOSEKI] = 1.0,
855 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
857 if (arg) {
858 char *optspec, *next = arg;
859 while (*next) {
860 optspec = next;
861 next += strcspn(next, ":");
862 if (*next) { *next++ = 0; } else { *next = 0; }
864 char *optname = optspec;
865 char *optval = strchr(optspec, '=');
866 if (optval) *optval++ = 0;
868 if (!strcasecmp(optname, "debug") && optval) {
869 p->debug_level = atoi(optval);
870 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
871 pp->lcapturerate = atoi(optval);
872 } else if (!strcasecmp(optname, "atarirate") && optval) {
873 pp->atarirate = atoi(optval);
874 } else if (!strcasecmp(optname, "capturerate") && optval) {
875 pp->capturerate = atoi(optval);
876 } else if (!strcasecmp(optname, "patternrate") && optval) {
877 pp->patternrate = atoi(optval);
878 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
879 pp->selfatarirate = atoi(optval);
880 } else if (!strcasecmp(optname, "korate") && optval) {
881 pp->korate = atoi(optval);
882 } else if (!strcasecmp(optname, "josekirate") && optval) {
883 pp->josekirate = atoi(optval);
884 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
885 pp->alwaysccaprate = atoi(optval);
886 } else if (!strcasecmp(optname, "rate") && optval) {
887 rate = atoi(optval);
888 } else if (!strcasecmp(optname, "fillboardtries")) {
889 pp->fillboardtries = atoi(optval);
890 } else if (!strcasecmp(optname, "koage") && optval) {
891 pp->koage = atoi(optval);
892 } else if (!strcasecmp(optname, "pattern2")) {
893 pp->pattern2 = optval && *optval == '0' ? false : true;
894 } else if (!strcasecmp(optname, "selfatari_other")) {
895 pp->selfatari_other = optval && *optval == '0' ? false : true;
896 } else if (!strcasecmp(optname, "capcheckall")) {
897 pp->capcheckall = optval && *optval == '0' ? false : true;
898 } else if (!strcasecmp(optname, "fullchoose")) {
899 p->choose = optval && *optval == '0' ? playout_moggy_partchoose : playout_moggy_fullchoose;
900 } else if (!strcasecmp(optname, "mqprob") && optval) {
901 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
902 for (int i = 1; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
903 optval++;
904 pp->mq_prob[i] = atof(optval);
906 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
907 pp->tenuki_prob = atof(optval);
908 } else {
909 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
910 exit(1);
914 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
915 if (pp->atarirate == -1U) pp->atarirate = rate;
916 if (pp->capturerate == -1U) pp->capturerate = rate;
917 if (pp->patternrate == -1U) pp->patternrate = rate;
918 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
919 if (pp->korate == -1U) pp->korate = rate;
920 if (pp->josekirate == -1U) pp->josekirate = rate;
921 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
923 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
925 return p;