Moggy: Better nlibrate default for 19x19
[pachi/derm.git] / playout / moggy.c
blob0b0ad59f10c2c2bda4c3c4863a7f54827727ac4d
1 /* Heuristical playout (and tree prior) policy modelled primarily after
2 * the description of the Mogo engine. */
4 #include <assert.h>
5 #include <math.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #define DEBUG
10 #include "board.h"
11 #include "debug.h"
12 #include "joseki/base.h"
13 #include "mq.h"
14 #include "pattern3.h"
15 #include "playout.h"
16 #include "playout/moggy.h"
17 #include "random.h"
18 #include "tactics/1lib.h"
19 #include "tactics/2lib.h"
20 #include "tactics/nlib.h"
21 #include "tactics/ladder.h"
22 #include "tactics/selfatari.h"
23 #include "uct/prior.h"
25 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
28 /* In case "seqchoose" move picker is enabled (i.e. no "fullchoose"
29 * parameter passed), we stochastically apply fixed set of decision
30 * rules in given order.
32 * In "fullchoose" mode, we instead build a move queue of variously
33 * tagged candidates, then consider a probability distribution over
34 * them and pick a move from that. */
36 /* Move queue tags. Some may be even undesirable - these moves then
37 * receive a penalty; penalty tags should be used only when it is
38 * certain the move would be considered anyway. */
39 enum mq_tag {
40 MQ_KO = 0,
41 MQ_LATARI,
42 MQ_L2LIB,
43 MQ_LNLIB,
44 MQ_PAT3,
45 MQ_GATARI,
46 MQ_JOSEKI,
47 MQ_MAX
51 /* Note that the context can be shared by multiple threads! */
53 struct moggy_policy {
54 unsigned int lcapturerate, atarirate, nlibrate, capturerate, patternrate, korate, josekirate;
55 unsigned int selfatarirate, alwaysccaprate;
56 unsigned int fillboardtries;
57 int koage;
58 /* Whether to look for patterns around second-to-last move. */
59 bool pattern2;
60 /* Whether, when self-atari attempt is detected, to play the other
61 * group's liberty if that is non-self-atari. */
62 bool selfatari_other;
63 /* Whether to always pick from moves capturing all groups in
64 * global_atari_check(). */
65 bool capcheckall;
66 /* 2lib settings: */
67 bool atari_def_no_hopeless;
68 bool atari_miaisafe;
69 /* nlib settings: */
70 int nlib_count;
72 struct joseki_dict *jdict;
73 struct pattern3s patterns;
75 /* Gamma values for queue tags - correspond to probabilities. */
76 /* XXX: Tune. */
77 double mq_prob[MQ_MAX], tenuki_prob;
81 static char moggy_patterns_src[][11] = {
82 /* hane pattern - enclosing hane */
83 "XOX"
84 "..."
85 "???",
86 /* hane pattern - non-cutting hane */
87 "YO."
88 "..."
89 "?.?",
90 /* hane pattern - magari */
91 "XO?"
92 "X.."
93 "x.?",
94 /* hane pattern - thin hane */
95 "XOO"
96 "..."
97 "?.?" "X",
98 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
99 ".Q."
100 "Y.."
101 "...",
102 /* cut1 pattern (kiri) - unprotected cut */
103 "XO?"
104 "O.o"
105 "?o?",
106 /* cut1 pattern (kiri) - peeped cut */
107 "XO?"
108 "O.X"
109 "???",
110 /* cut2 pattern (de) */
111 "?X?"
112 "O.O"
113 "ooo",
114 /* cut keima (not in Mogo) */
115 "OX?"
116 "o.O"
117 "???", /* o?? has some pathological tsumego cases */
118 /* side pattern - chase */
119 "X.?"
120 "O.?"
121 "##?",
122 /* side pattern - block side cut */
123 "OX?"
124 "X.O"
125 "###",
126 /* side pattern - block side connection */
127 "?X?"
128 "x.O"
129 "###",
130 /* side pattern - sagari (SUSPICIOUS) */
131 "?XQ"
132 "x.x" /* Mogo has "x.?" */
133 "###" /* Mogo has "X" */,
134 /* side pattern - throw-in (SUSPICIOUS) */
135 #if 0
136 "?OX"
137 "o.O"
138 "?##" "X",
139 #endif
140 /* side pattern - cut (SUSPICIOUS) */
141 "?OY"
142 "Y.O"
143 "###" /* Mogo has "X" */,
144 /* side pattern - eye piercing:
145 * # O O O .
146 * # O . O .
147 * # . . . .
148 * # # # # # */
149 #if 0
150 "Oxx"
151 "..."
152 "###",
153 #endif
155 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
157 static inline bool
158 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
160 struct moggy_policy *pp = p->data;
161 /* Check if 3x3 pattern is matched by given move... */
162 if (!pattern3_move_here(&pp->patterns, b, m))
163 return false;
164 /* ...and the move is not obviously stupid. */
165 if (is_bad_selfatari(b, m->color, m->coord))
166 return false;
167 /* Ladder moves are stupid. */
168 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
169 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
170 return false;
171 return true;
174 static void
175 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
177 struct move m2 = { .coord = c, .color = color };
178 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
179 mq_add(q, c, 1<<MQ_PAT3);
182 /* Check if we match any pattern around given move (with the other color to play). */
183 static void
184 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
186 /* Suicides do not make any patterns and confuse us. */
187 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
188 return;
190 foreach_8neighbor(b, m->coord) {
191 apply_pattern_here(p, b, c, stone_other(m->color), q);
192 } foreach_8neighbor_end;
194 if (mm) { /* Second move for pattern searching */
195 foreach_8neighbor(b, mm->coord) {
196 if (coord_is_8adjecent(m->coord, c, b))
197 continue;
198 apply_pattern_here(p, b, c, stone_other(m->color), q);
199 } foreach_8neighbor_end;
202 if (PLDEBUGL(5))
203 mq_print(q, b, "Pattern");
207 static void
208 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
210 struct moggy_policy *pp = p->data;
211 if (!pp->jdict)
212 return;
214 for (int i = 0; i < 4; i++) {
215 hash_t h = b->qhash[i] & joseki_hash_mask;
216 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
217 if (!cc) continue;
218 for (; !is_pass(*cc); cc++) {
219 if (coord_quadrant(*cc, b) != i)
220 continue;
221 mq_add(q, *cc, 1<<MQ_JOSEKI);
225 if (q->moves > 0 && PLDEBUGL(5))
226 mq_print(q, b, "Joseki");
229 static void
230 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
232 if (b->clen == 0)
233 return;
235 struct moggy_policy *pp = p->data;
236 if (pp->capcheckall) {
237 for (int g = 0; g < b->clen; g++)
238 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
239 if (PLDEBUGL(5))
240 mq_print(q, b, "Global atari");
241 return;
244 int g_base = fast_random(b->clen);
245 for (int g = g_base; g < b->clen; g++) {
246 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
247 if (q->moves > 0) {
248 /* XXX: Try carrying on. */
249 if (PLDEBUGL(5))
250 mq_print(q, b, "Global atari");
251 return;
254 for (int g = 0; g < g_base; g++) {
255 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
256 if (q->moves > 0) {
257 /* XXX: Try carrying on. */
258 if (PLDEBUGL(5))
259 mq_print(q, b, "Global atari");
260 return;
263 return;
266 static void
267 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
269 struct moggy_policy *pp = p->data;
271 /* Did the opponent play a self-atari? */
272 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
273 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
276 foreach_neighbor(b, m->coord, {
277 group_t g = group_at(b, c);
278 if (!g || board_group_info(b, g).libs != 1)
279 continue;
280 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
283 if (PLDEBUGL(5))
284 mq_print(q, b, "Local atari");
288 static void
289 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
291 struct moggy_policy *pp = p->data;
293 /* Does the opponent have just two liberties? */
294 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
295 group_2lib_check(b, group_at(b, m->coord), stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
296 #if 0
297 /* We always prefer to take off an enemy chain liberty
298 * before pulling out ourselves. */
299 /* XXX: We aren't guaranteed to return to that group
300 * later. */
301 if (q->moves)
302 return q->move[fast_random(q->moves)];
303 #endif
306 /* Then he took a third liberty from neighboring chain? */
307 foreach_neighbor(b, m->coord, {
308 group_t g = group_at(b, c);
309 if (!g || board_group_info(b, g).libs != 2)
310 continue;
311 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
314 if (PLDEBUGL(5))
315 mq_print(q, b, "Local 2lib");
318 static void
319 local_nlib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
321 struct moggy_policy *pp = p->data;
322 enum stone color = stone_other(m->color);
324 /* Attacking N-liberty groups in general is probably
325 * not feasible. What we are primarily concerned about is
326 * counter-attacking groups that have two physical liberties,
327 * but three effective liberties:
329 * . O . . . . #
330 * O O X X X X #
331 * . X O O X . #
332 * . X O . O X #
333 * . X O O . X #
334 * # # # # # # #
336 * The time for this to come is when the opponent took a liberty
337 * of ours, making a few-liberty group. Therefore, we focus
338 * purely on defense.
340 * There is a tradeoff - down to how many liberties we need to
341 * be to start looking? nlib_count=3 will work for the left black
342 * group (2lib-solver will suggest connecting the false eye), but
343 * not for top black group (it is too late to start playing 3-3
344 * capturing race). Also, we cannot prevent stupidly taking an
345 * outside liberty ourselves; the higher nlib_count, the higher
346 * the chance we withstand this.
348 * However, higher nlib_count means that we will waste more time
349 * checking non-urgent or alive groups, and we will play silly
350 * or wasted moves around alive groups. */
352 group_t group2 = 0;
353 foreach_neighbor(b, m->coord, {
354 group_t g = group_at(b, c);
355 if (!g || group2 == g || board_at(b, c) != color)
356 continue;
357 if (board_group_info(b, g).libs < 3 || board_group_info(b, g).libs > pp->nlib_count)
358 continue;
359 group_nlib_defense_check(b, g, color, q, 1<<MQ_LNLIB);
360 group2 = g; // prevent trivial repeated checks
363 if (PLDEBUGL(5))
364 mq_print(q, b, "Local nlib");
367 coord_t
368 fillboard_check(struct playout_policy *p, struct board *b)
370 struct moggy_policy *pp = p->data;
371 unsigned int fbtries = b->flen / 8;
372 if (pp->fillboardtries < fbtries)
373 fbtries = pp->fillboardtries;
375 for (unsigned int i = 0; i < fbtries; i++) {
376 coord_t coord = b->f[fast_random(b->flen)];
377 if (immediate_liberty_count(b, coord) != 4)
378 continue;
379 foreach_diag_neighbor(b, coord) {
380 if (board_at(b, c) != S_NONE)
381 goto next_try;
382 } foreach_diag_neighbor_end;
383 return coord;
384 next_try:;
386 return pass;
389 coord_t
390 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
392 struct moggy_policy *pp = p->data;
394 if (PLDEBUGL(5))
395 board_print(b, stderr);
397 /* Ko fight check */
398 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
399 && b->moves - b->last_ko_age < pp->koage
400 && pp->korate > fast_random(100)) {
401 if (board_is_valid_play(b, to_play, b->last_ko.coord)
402 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
403 return b->last_ko.coord;
406 /* Local checks */
407 if (!is_pass(b->last_move.coord)) {
408 /* Local group in atari? */
409 if (pp->lcapturerate > fast_random(100)) {
410 struct move_queue q; q.moves = 0;
411 local_atari_check(p, b, &b->last_move, &q);
412 if (q.moves > 0)
413 return mq_pick(&q);
416 /* Local group can be PUT in atari? */
417 if (pp->atarirate > fast_random(100)) {
418 struct move_queue q; q.moves = 0;
419 local_2lib_check(p, b, &b->last_move, &q);
420 if (q.moves > 0)
421 return mq_pick(&q);
424 /* Local group reduced some of our groups to 3 libs? */
425 if (pp->nlibrate > fast_random(100)) {
426 struct move_queue q; q.moves = 0;
427 local_nlib_check(p, b, &b->last_move, &q);
428 if (q.moves > 0)
429 return mq_pick(&q);
432 /* Check for patterns we know */
433 if (pp->patternrate > fast_random(100)) {
434 struct move_queue q; q.moves = 0;
435 apply_pattern(p, b, &b->last_move,
436 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
437 &q);
438 if (q.moves > 0)
439 return mq_pick(&q);
443 /* Global checks */
445 /* Any groups in atari? */
446 if (pp->capturerate > fast_random(100)) {
447 struct move_queue q; q.moves = 0;
448 global_atari_check(p, b, to_play, &q);
449 if (q.moves > 0)
450 return mq_pick(&q);
453 /* Joseki moves? */
454 if (pp->josekirate > fast_random(100)) {
455 struct move_queue q; q.moves = 0;
456 joseki_check(p, b, to_play, &q);
457 if (q.moves > 0)
458 return mq_pick(&q);
461 /* Fill board */
462 if (pp->fillboardtries > 0) {
463 coord_t c = fillboard_check(p, b);
464 if (!is_pass(c))
465 return c;
468 return pass;
471 /* Pick a move from queue q, giving different likelihoods to moves
472 * based on their tags. */
473 coord_t
474 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
476 struct moggy_policy *pp = p->data;
478 /* First, merge all entries for a move. */
479 /* We use a naive O(N^2) since the average length of the queue
480 * is about 1.4. */
481 for (unsigned int i = 0; i < q->moves; i++) {
482 for (unsigned int j = i + 1; j < q->moves; j++) {
483 if (q->move[i] != q->move[j])
484 continue;
485 q->tag[i] |= q->tag[j];
486 q->moves--;
487 q->tag[j] = q->tag[q->moves];
488 q->move[j] = q->move[q->moves];
492 /* Now, construct a probdist. */
493 fixp_t total = 0;
494 fixp_t pd[q->moves];
495 for (unsigned int i = 0; i < q->moves; i++) {
496 double val = 1.0;
497 assert(q->tag[i] != 0);
498 for (int j = 0; j < MQ_MAX; j++)
499 if (q->tag[i] & (1<<j)) {
500 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
501 val *= pp->mq_prob[j];
503 pd[i] = double_to_fixp(val);
504 total += pd[i];
506 total += double_to_fixp(pp->tenuki_prob);
508 /* Finally, pick a move! */
509 fixp_t stab = fast_irandom(total);
510 for (unsigned int i = 0; i < q->moves; i++) {
511 //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));
512 if (stab < pd[i])
513 return q->move[i];
514 stab -= pd[i];
517 /* Tenuki. */
518 assert(stab < double_to_fixp(pp->tenuki_prob));
519 return pass;
522 coord_t
523 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
525 struct moggy_policy *pp = p->data;
526 struct move_queue q; q.moves = 0;
528 if (PLDEBUGL(5))
529 board_print(b, stderr);
531 /* Ko fight check */
532 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
533 && b->moves - b->last_ko_age < pp->koage) {
534 if (board_is_valid_play(b, to_play, b->last_ko.coord)
535 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
536 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
539 /* Local checks */
540 if (!is_pass(b->last_move.coord)) {
541 /* Local group in atari? */
542 local_atari_check(p, b, &b->last_move, &q);
544 /* Local group can be PUT in atari? */
545 local_2lib_check(p, b, &b->last_move, &q);
547 /* Local group reduced some of our groups to 3 libs? */
548 local_nlib_check(p, b, &b->last_move, &q);
550 /* Check for patterns we know */
551 apply_pattern(p, b, &b->last_move,
552 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
553 &q);
556 /* Global checks */
558 /* Any groups in atari? */
559 global_atari_check(p, b, to_play, &q);
561 /* Joseki moves? */
562 joseki_check(p, b, to_play, &q);
564 #if 0
565 /* Average length of the queue is 1.4 move. */
566 printf("MQL %d ", q.moves);
567 for (unsigned int i = 0; i < q.moves; i++)
568 printf("%s ", coord2sstr(q.move[i], b));
569 printf("\n");
570 #endif
572 if (q.moves > 0)
573 return mq_tagged_choose(p, b, to_play, &q);
575 /* Fill board */
576 if (pp->fillboardtries > 0) {
577 coord_t c = fillboard_check(p, b);
578 if (!is_pass(c))
579 return c;
582 return pass;
586 void
587 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
589 struct moggy_policy *pp = p->data;
590 struct board *b = map->b;
591 struct move_queue q; q.moves = 0;
593 if (board_group_info(b, g).libs > pp->nlib_count)
594 return;
596 if (PLDEBUGL(5)) {
597 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
598 board_print(b, stderr);
601 if (board_group_info(b, g).libs > 2) {
602 if (!pp->nlibrate)
603 return;
604 if (board_at(b, g) != map->to_play)
605 return; // we do only defense
606 group_nlib_defense_check(b, g, map->to_play, &q, 0);
607 while (q.moves--) {
608 coord_t coord = q.move[q.moves];
609 if (PLDEBUGL(5))
610 fprintf(stderr, "1.0: nlib %s\n", coord2sstr(coord, b));
611 int assess = games / 2;
612 add_prior_value(map, coord, 1, assess);
614 return;
617 if (board_group_info(b, g).libs == 2) {
618 if (!pp->atarirate)
619 return;
620 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
621 while (q.moves--) {
622 coord_t coord = q.move[q.moves];
623 if (PLDEBUGL(5))
624 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
625 int assess = games / 2;
626 add_prior_value(map, coord, 1, assess);
628 return;
631 /* This group, sir, is in atari! */
633 coord_t ladder = pass;
634 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
635 while (q.moves--) {
636 coord_t coord = q.move[q.moves];
638 /* _Never_ play here if this move plays out
639 * a caught ladder. */
640 if (coord == ladder && !board_playing_ko_threat(b)) {
641 /* Note that the opposite is not guarded against;
642 * we do not advise against capturing a laddered
643 * group (but we don't encourage it either). Such
644 * a move can simplify tactical situations if we
645 * can afford it. */
646 if (map->to_play != board_at(b, g))
647 continue;
648 /* FIXME: We give the malus even if this move
649 * captures another group. */
650 if (PLDEBUGL(5))
651 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
652 add_prior_value(map, coord, 0, games);
653 continue;
656 if (!pp->capturerate && !pp->lcapturerate)
657 continue;
659 if (PLDEBUGL(5))
660 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
661 int assess = games * 2;
662 add_prior_value(map, coord, 1, assess);
666 void
667 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
669 struct moggy_policy *pp = p->data;
670 struct board *b = map->b;
672 if (PLDEBUGL(5)) {
673 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
674 board_print(b, stderr);
677 /* Is this move a self-atari? */
678 if (pp->selfatarirate) {
679 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
680 if (PLDEBUGL(5))
681 fprintf(stderr, "0.0: self-atari\n");
682 add_prior_value(map, coord, 0, games);
683 if (!pp->selfatari_other)
684 return;
685 /* If we can play on the other liberty of the
686 * endangered group, do! */
687 coord = selfatari_cousin(b, map->to_play, coord);
688 if (is_pass(coord))
689 return;
690 if (PLDEBUGL(5))
691 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
692 add_prior_value(map, coord, 1.0, games);
693 return;
697 /* Pattern check */
698 if (pp->patternrate) {
699 struct move m = { .color = map->to_play, .coord = coord };
700 if (test_pattern3_here(p, b, &m)) {
701 if (PLDEBUGL(5))
702 fprintf(stderr, "1.0: pattern\n");
703 add_prior_value(map, coord, 1, games);
707 return;
710 void
711 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
713 struct moggy_policy *pp = p->data;
715 /* First, go through all endangered groups. */
716 for (group_t g = 1; g < board_size2(map->b); g++)
717 if (group_at(map->b, g) == g)
718 playout_moggy_assess_group(p, map, g, games);
720 /* Then, assess individual moves. */
721 if (!pp->patternrate && !pp->selfatarirate)
722 return;
723 foreach_free_point(map->b) {
724 if (map->consider[c])
725 playout_moggy_assess_one(p, map, c, games);
726 } foreach_free_point_end;
729 bool
730 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
732 struct moggy_policy *pp = p->data;
734 /* The idea is simple for now - never allow self-atari moves.
735 * They suck in general, but this also permits us to actually
736 * handle seki in the playout stage. */
738 if (fast_random(100) >= pp->selfatarirate) {
739 if (PLDEBUGL(5))
740 fprintf(stderr, "skipping sar test\n");
741 return true;
743 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
744 if (selfatari) {
745 if (PLDEBUGL(5))
746 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
747 stone2str(m->color), coord2sstr(m->coord, b));
748 if (pp->selfatari_other) {
749 /* Ok, try the other liberty of the atari'd group. */
750 coord_t c = selfatari_cousin(b, m->color, m->coord);
751 if (is_pass(c)) return false;
752 if (PLDEBUGL(5))
753 fprintf(stderr, "___ Redirecting to other lib %s\n",
754 coord2sstr(c, b));
755 m->coord = c;
756 return true;
758 return false;
760 return true;
764 struct playout_policy *
765 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
767 struct playout_policy *p = calloc2(1, sizeof(*p));
768 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
769 p->data = pp;
770 p->choose = playout_moggy_seqchoose;
771 p->assess = playout_moggy_assess;
772 p->permit = playout_moggy_permit;
774 pp->jdict = jdict;
776 /* These settings are tuned for 19x19 play with several threads
777 * on reasonable time limits (i.e., rather large number of playouts).
778 * XXX: no 9x9 tuning has been done recently. */
779 int rate = board_large(b) ? 80 : 90;
781 pp->lcapturerate = pp->atarirate = pp->nlibrate = pp->patternrate
782 = pp->selfatarirate = pp->josekirate = -1U;
783 if (board_large(b)) {
784 pp->lcapturerate = pp->patternrate = 100;
785 pp->nlibrate = 20;
786 pp->pattern2 = true;
788 pp->korate = 20; pp->koage = 4;
789 pp->alwaysccaprate = 20;
790 pp->selfatari_other = true;
791 pp->atari_def_no_hopeless = !board_large(b);
792 pp->atari_miaisafe = true;
793 pp->nlib_count = 4;
795 /* C is stupid. */
796 double mq_prob_default[MQ_MAX] = {
797 [MQ_KO] = 6.0,
798 [MQ_LATARI] = 5.0,
799 [MQ_L2LIB] = 4.0,
800 [MQ_LNLIB] = 3.5,
801 [MQ_PAT3] = 3.0,
802 [MQ_GATARI] = 2.0,
803 [MQ_JOSEKI] = 1.0,
805 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
807 if (arg) {
808 char *optspec, *next = arg;
809 while (*next) {
810 optspec = next;
811 next += strcspn(next, ":");
812 if (*next) { *next++ = 0; } else { *next = 0; }
814 char *optname = optspec;
815 char *optval = strchr(optspec, '=');
816 if (optval) *optval++ = 0;
818 if (!strcasecmp(optname, "debug") && optval) {
819 p->debug_level = atoi(optval);
820 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
821 pp->lcapturerate = atoi(optval);
822 } else if (!strcasecmp(optname, "atarirate") && optval) {
823 pp->atarirate = atoi(optval);
824 } else if (!strcasecmp(optname, "nlibrate") && optval) {
825 pp->nlibrate = atoi(optval);
826 } else if (!strcasecmp(optname, "capturerate") && optval) {
827 pp->capturerate = atoi(optval);
828 } else if (!strcasecmp(optname, "patternrate") && optval) {
829 pp->patternrate = atoi(optval);
830 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
831 pp->selfatarirate = atoi(optval);
832 } else if (!strcasecmp(optname, "korate") && optval) {
833 pp->korate = atoi(optval);
834 } else if (!strcasecmp(optname, "josekirate") && optval) {
835 pp->josekirate = atoi(optval);
836 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
837 pp->alwaysccaprate = atoi(optval);
838 } else if (!strcasecmp(optname, "rate") && optval) {
839 rate = atoi(optval);
840 } else if (!strcasecmp(optname, "fillboardtries")) {
841 pp->fillboardtries = atoi(optval);
842 } else if (!strcasecmp(optname, "koage") && optval) {
843 pp->koage = atoi(optval);
844 } else if (!strcasecmp(optname, "pattern2")) {
845 pp->pattern2 = optval && *optval == '0' ? false : true;
846 } else if (!strcasecmp(optname, "selfatari_other")) {
847 pp->selfatari_other = optval && *optval == '0' ? false : true;
848 } else if (!strcasecmp(optname, "capcheckall")) {
849 pp->capcheckall = optval && *optval == '0' ? false : true;
850 } else if (!strcasecmp(optname, "atari_miaisafe")) {
851 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
852 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
853 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
854 } else if (!strcasecmp(optname, "nlib_count") && optval) {
855 pp->nlib_count = atoi(optval);
856 } else if (!strcasecmp(optname, "fullchoose")) {
857 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
858 } else if (!strcasecmp(optname, "mqprob") && optval) {
859 /* KO%LATARI%L2LIB%LNLIB%PAT3%GATARI%JOSEKI */
860 for (int i = 0; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
861 optval++;
862 pp->mq_prob[i] = atof(optval);
864 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
865 pp->tenuki_prob = atof(optval);
866 } else {
867 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
868 exit(1);
872 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
873 if (pp->atarirate == -1U) pp->atarirate = rate;
874 if (pp->capturerate == -1U) pp->capturerate = rate;
875 if (pp->patternrate == -1U) pp->patternrate = rate;
876 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
877 if (pp->korate == -1U) pp->korate = rate;
878 if (pp->josekirate == -1U) pp->josekirate = rate;
879 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
881 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
883 return p;