Merge branch 'master' into libmap
[pachi.git] / uct / uct.c
blob96ed386e5a13175d2c45f00b7c570f8a8389c9d6
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
8 #define DEBUG
10 #include "debug.h"
11 #include "board.h"
12 #include "gtp.h"
13 #include "chat.h"
14 #include "move.h"
15 #include "mq.h"
16 #include "joseki/base.h"
17 #include "playout.h"
18 #include "playout/moggy.h"
19 #include "playout/light.h"
20 #include "tactics/goals.h"
21 #include "tactics/util.h"
22 #include "timeinfo.h"
23 #include "uct/dynkomi.h"
24 #include "uct/internal.h"
25 #include "uct/plugins.h"
26 #include "uct/prior.h"
27 #include "uct/search.h"
28 #include "uct/slave.h"
29 #include "uct/tree.h"
30 #include "uct/uct.h"
31 #include "uct/walk.h"
33 struct uct_policy *policy_ucb1_init(struct uct *u, char *arg);
34 struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg, struct board *board);
35 static void uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color);
37 /* Maximal simulation length. */
38 #define MC_GAMELEN MAX_GAMELEN
41 static void
42 setup_state(struct uct *u, struct board *b, enum stone color)
44 u->t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0,
45 u->max_pruned_size, u->pruning_threshold, u->local_tree_aging, u->stats_hbits);
46 if (u->initial_extra_komi)
47 u->t->extra_komi = u->initial_extra_komi;
48 if (u->force_seed)
49 fast_srandom(u->force_seed);
50 if (UDEBUGL(3))
51 fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed());
52 if (!u->no_tbook && b->moves == 0) {
53 if (color == S_BLACK) {
54 tree_load(u->t, b);
55 } else if (DEBUGL(0)) {
56 fprintf(stderr, "Warning: First move appears to be white\n");
61 static void
62 reset_state(struct uct *u)
64 assert(u->t);
65 tree_done(u->t); u->t = NULL;
68 static void
69 setup_dynkomi(struct uct *u, struct board *b, enum stone to_play)
71 if (u->t->use_extra_komi && !u->pondering && u->dynkomi->permove)
72 u->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, u->t);
73 else if (!u->t->use_extra_komi)
74 u->t->extra_komi = 0;
77 void
78 uct_prepare_move(struct uct *u, struct board *b, enum stone color)
80 if (u->t) {
81 /* Verify that we have sane state. */
82 assert(b->es == u);
83 assert(u->t && b->moves);
84 if (color != stone_other(u->t->root_color)) {
85 fprintf(stderr, "Fatal: Non-alternating play detected %d %d\n",
86 color, u->t->root_color);
87 exit(1);
89 uct_htable_reset(u->t);
91 } else {
92 /* We need fresh state. */
93 b->es = u;
94 setup_state(u, b, color);
97 u->ownermap.playouts = 0;
98 memset(u->ownermap.map, 0, board_size2(b) * sizeof(u->ownermap.map[0]));
99 u->played_own = u->played_all = 0;
102 static void
103 dead_group_list(struct uct *u, struct board *b, struct move_queue *mq)
105 enum gj_state gs_array[board_size2(b)];
106 struct group_judgement gj = { .thres = GJ_THRES, .gs = gs_array };
107 board_ownermap_judge_groups(b, &u->ownermap, &gj);
108 groups_of_status(b, &gj, GS_DEAD, mq);
111 bool
112 uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive)
114 /* Make sure enough playouts are simulated to get a reasonable dead group list. */
115 while (u->ownermap.playouts < GJ_MINGAMES)
116 uct_playout(u, b, color, u->t);
118 struct move_queue mq = { .moves = 0 };
119 dead_group_list(u, b, &mq);
120 if (pass_all_alive) {
121 for (unsigned int i = 0; i < mq.moves; i++) {
122 if (board_at(b, mq.move[i]) == stone_other(color)) {
123 return false; // We need to remove opponent dead groups first.
126 mq.moves = 0; // our dead stones are alive when pass_all_alive is true
128 if (u->allow_losing_pass) {
129 foreach_point(b) {
130 if (board_at(b, c) == S_OFFBOARD)
131 continue;
132 if (board_ownermap_judge_point(&u->ownermap, c, GJ_THRES) == PJ_UNKNOWN) {
133 if (UDEBUGL(3))
134 fprintf(stderr, "uct_pass_is_safe fails at %s[%d]\n", coord2sstr(c, b), c);
135 return false; // Unclear point, clarify first.
137 } foreach_point_end;
138 return true;
140 return pass_is_safe(b, color, &mq);
143 static char *
144 uct_printhook_ownermap(struct board *board, coord_t c, char *s, char *end)
146 struct uct *u = board->es;
147 if (!u) {
148 strcat(s, ". ");
149 return s + 2;
151 const char chr[] = ":XO,"; // dame, black, white, unclear
152 const char chm[] = ":xo,";
153 char ch = chr[board_ownermap_judge_point(&u->ownermap, c, GJ_THRES)];
154 if (ch == ',') { // less precise estimate then?
155 ch = chm[board_ownermap_judge_point(&u->ownermap, c, 0.67)];
157 s += snprintf(s, end - s, "%c ", ch);
158 return s;
161 static char *
162 uct_notify_play(struct engine *e, struct board *b, struct move *m, char *enginearg)
164 struct uct *u = e->data;
165 if (!u->t) {
166 /* No state, create one - this is probably game beginning
167 * and we need to load the opening tbook right now. */
168 uct_prepare_move(u, b, m->color);
169 assert(u->t);
172 /* Stop pondering, required by tree_promote_at() */
173 uct_pondering_stop(u);
174 if (UDEBUGL(2) && u->slave)
175 tree_dump(u->t, u->dumpthres);
177 if (is_resign(m->coord)) {
178 /* Reset state. */
179 reset_state(u);
180 return NULL;
183 /* Promote node of the appropriate move to the tree root. */
184 assert(u->t->root);
185 if (!tree_promote_at(u->t, b, m->coord)) {
186 if (UDEBUGL(3))
187 fprintf(stderr, "Warning: Cannot promote move node! Several play commands in row?\n");
188 /* Preserve dynamic komi information, though, that is important. */
189 u->initial_extra_komi = u->t->extra_komi;
190 reset_state(u);
191 return NULL;
194 /* If we are a slave in a distributed engine, start pondering once
195 * we know which move we actually played. See uct_genmove() about
196 * the check for pass. */
197 if (u->pondering_opt && u->slave && m->color == u->my_color && !is_pass(m->coord))
198 uct_pondering_start(u, b, u->t, stone_other(m->color));
200 return NULL;
203 static char *
204 uct_undo(struct engine *e, struct board *b)
206 struct uct *u = e->data;
208 if (!u->t) return NULL;
209 uct_pondering_stop(u);
210 u->initial_extra_komi = u->t->extra_komi;
211 reset_state(u);
212 return NULL;
215 static char *
216 uct_result(struct engine *e, struct board *b)
218 struct uct *u = e->data;
219 static char reply[1024];
221 if (!u->t)
222 return NULL;
223 enum stone color = u->t->root_color;
224 struct tree_node *n = u->t->root;
225 snprintf(reply, 1024, "%s %s %d %.2f %.1f",
226 stone2str(color), coord2sstr(node_coord(n), b),
227 n->u.playouts, tree_node_get_value(u->t, -1, n->u.value),
228 u->t->use_extra_komi ? u->t->extra_komi : 0);
229 return reply;
232 static char *
233 uct_chat(struct engine *e, struct board *b, bool opponent, char *from, char *cmd)
235 struct uct *u = e->data;
237 if (!u->t)
238 return generic_chat(b, opponent, from, cmd, S_NONE, pass, 0, 1, u->threads, 0.0, 0.0);
240 struct tree_node *n = u->t->root;
241 double winrate = tree_node_get_value(u->t, -1, n->u.value);
242 double extra_komi = u->t->use_extra_komi && abs(u->t->extra_komi) >= 0.5 ? u->t->extra_komi : 0;
244 return generic_chat(b, opponent, from, cmd, u->t->root_color, node_coord(n), n->u.playouts, 1,
245 u->threads, winrate, extra_komi);
248 static void
249 uct_dead_group_list(struct engine *e, struct board *b, struct move_queue *mq)
251 struct uct *u = e->data;
253 /* This means the game is probably over, no use pondering on. */
254 uct_pondering_stop(u);
256 if (u->pass_all_alive)
257 return; // no dead groups
259 bool mock_state = false;
261 if (!u->t) {
262 /* No state, but we cannot just back out - we might
263 * have passed earlier, only assuming some stones are
264 * dead, and then re-connected, only to lose counting
265 * when all stones are assumed alive. */
266 uct_prepare_move(u, b, S_BLACK); assert(u->t);
267 mock_state = true;
269 /* Make sure the ownermap is well-seeded. */
270 while (u->ownermap.playouts < GJ_MINGAMES)
271 uct_playout(u, b, S_BLACK, u->t);
272 /* Show the ownermap: */
273 if (DEBUGL(2))
274 board_print_custom(b, stderr, uct_printhook_ownermap);
276 dead_group_list(u, b, mq);
278 if (mock_state) {
279 /* Clean up the mock state in case we will receive
280 * a genmove; we could get a non-alternating-move
281 * error from uct_prepare_move() in that case otherwise. */
282 reset_state(u);
286 static void
287 playout_policy_done(struct playout_policy *p)
289 if (p->done) p->done(p);
290 if (p->data) free(p->data);
291 free(p);
294 static void
295 uct_stop(struct engine *e)
297 /* This is called on game over notification. However, an undo
298 * and game resume can follow, so don't panic yet and just
299 * relax and stop thinking so that we don't waste CPU. */
300 struct uct *u = e->data;
301 uct_pondering_stop(u);
304 static void
305 uct_done(struct engine *e)
307 /* This is called on engine reset, especially when clear_board
308 * is received and new game should begin. */
309 struct uct *u = e->data;
310 uct_pondering_stop(u);
311 if (u->t) reset_state(u);
312 free(u->ownermap.map);
314 free(u->policy);
315 free(u->random_policy);
316 playout_policy_done(u->playout);
317 uct_prior_done(u->prior);
318 joseki_done(u->jdict);
319 pluginset_done(u->plugins);
324 /* Run time-limited MCTS search on foreground. */
325 static int
326 uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t, bool print_progress)
328 struct uct_search_state s;
329 uct_search_start(u, b, color, t, ti, &s);
330 if (UDEBUGL(2) && s.base_playouts > 0)
331 fprintf(stderr, "<pre-simulated %d games>\n", s.base_playouts);
333 /* The search tree is ctx->t. This is currently == . It is important
334 * to reference ctx->t directly since the
335 * thread manager will swap the tree pointer asynchronously. */
337 /* Now, just periodically poll the search tree. */
338 /* Note that in case of TD_GAMES, threads will not wait for
339 * the uct_search_check_stop() signalization. */
340 while (1) {
341 time_sleep(TREE_BUSYWAIT_INTERVAL);
342 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
343 * time control is broken. But if it happens to be less, we still search
344 * at least 100ms otherwise the move is completely random. */
346 int i = uct_search_games(&s);
347 /* Print notifications etc. */
348 uct_search_progress(u, b, color, t, ti, &s, i);
349 /* Check if we should stop the search. */
350 if (uct_search_check_stop(u, b, color, t, ti, &s, i))
351 break;
354 struct uct_thread_ctx *ctx = uct_search_stop();
355 if (UDEBUGL(2)) tree_dump(t, u->dumpthres);
356 if (UDEBUGL(2))
357 fprintf(stderr, "(avg score %f/%d; dynkomi's %f/%d value %f/%d)\n",
358 t->avg_score.value, t->avg_score.playouts,
359 u->dynkomi->score.value, u->dynkomi->score.playouts,
360 u->dynkomi->value.value, u->dynkomi->value.playouts);
361 if (print_progress)
362 uct_progress_status(u, t, color, ctx->games, NULL);
364 u->played_own += ctx->games;
365 return ctx->games;
368 /* Start pondering background with @color to play. */
369 static void
370 uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color)
372 if (UDEBUGL(1))
373 fprintf(stderr, "Starting to ponder with color %s\n", stone2str(stone_other(color)));
374 u->pondering = true;
376 /* We need a local board copy to ponder upon. */
377 struct board *b = malloc2(sizeof(*b)); board_copy(b, b0);
379 /* *b0 did not have the genmove'd move played yet. */
380 struct move m = { node_coord(t->root), t->root_color };
381 int res = board_play(b, &m);
382 assert(res >= 0);
383 setup_dynkomi(u, b, stone_other(m.color));
385 /* Start MCTS manager thread "headless". */
386 static struct uct_search_state s;
387 uct_search_start(u, b, color, t, NULL, &s);
390 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
391 * to stop the background search for a slave in the distributed engine. */
392 void
393 uct_pondering_stop(struct uct *u)
395 if (!thread_manager_running)
396 return;
398 /* Stop the thread manager. */
399 struct uct_thread_ctx *ctx = uct_search_stop();
400 if (UDEBUGL(1)) {
401 if (u->pondering) fprintf(stderr, "(pondering) ");
402 uct_progress_status(u, ctx->t, ctx->color, ctx->games, NULL);
404 if (u->pondering) {
405 free(ctx->b);
406 u->pondering = false;
411 void
412 uct_genmove_setup(struct uct *u, struct board *b, enum stone color)
414 if (b->superko_violation) {
415 fprintf(stderr, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
416 fprintf(stderr, "Maybe you play with situational instead of positional superko?\n");
417 fprintf(stderr, "I'm going to ignore the violation, but note that I may miss\n");
418 fprintf(stderr, "some moves valid under this ruleset because of this.\n");
419 b->superko_violation = false;
422 uct_prepare_move(u, b, color);
424 assert(u->t);
425 u->my_color = color;
427 /* How to decide whether to use dynkomi in this game? Since we use
428 * pondering, it's not simple "who-to-play" matter. Decide based on
429 * the last genmove issued. */
430 u->t->use_extra_komi = !!(u->dynkomi_mask & color);
431 setup_dynkomi(u, b, color);
433 if (b->rules == RULES_JAPANESE)
434 u->territory_scoring = true;
436 /* Make pessimistic assumption about komi for Japanese rules to
437 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
438 * The rules usually give the same winner if the integer part of komi
439 * is odd so we adjust the komi only if it is even (for a board of
440 * odd size). We are not trying to get an exact evaluation for rare
441 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
442 if (u->territory_scoring && (((int)floor(b->komi) + board_size(b)) & 1)) {
443 b->komi += (color == S_BLACK ? 1.0 : -1.0);
444 if (UDEBUGL(0))
445 fprintf(stderr, "Setting komi to %.1f assuming Japanese rules\n",
446 b->komi);
450 static coord_t *
451 uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
453 double start_time = time_now();
454 struct uct *u = e->data;
455 u->pass_all_alive |= pass_all_alive;
456 uct_pondering_stop(u);
457 uct_genmove_setup(u, b, color);
459 /* Start the Monte Carlo Tree Search! */
460 int base_playouts = u->t->root->u.playouts;
461 int played_games = uct_search(u, b, ti, color, u->t, false);
463 coord_t best_coord;
464 struct tree_node *best;
465 best = uct_search_result(u, b, color, u->pass_all_alive, played_games, base_playouts, &best_coord);
467 if (UDEBUGL(2)) {
468 double time = time_now() - start_time + 0.000001; /* avoid divide by zero */
469 fprintf(stderr, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
470 time, (int)(played_games/time), (int)(played_games/time/u->threads));
473 uct_progress_status(u, u->t, color, played_games, &best_coord);
475 if (!best) {
476 /* Pass or resign. */
477 if (is_pass(best_coord))
478 u->initial_extra_komi = u->t->extra_komi;
479 reset_state(u);
480 return coord_copy(best_coord);
482 tree_promote_node(u->t, &best);
484 /* After a pass, pondering is harmful for two reasons:
485 * (i) We might keep pondering even when the game is over.
486 * Of course this is the case for opponent resign as well.
487 * (ii) More importantly, the ownermap will get skewed since
488 * the UCT will start cutting off any playouts. */
489 if (u->pondering_opt && !is_pass(node_coord(best))) {
490 uct_pondering_start(u, b, u->t, stone_other(color));
492 return coord_copy(best_coord);
496 bool
497 uct_gentbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color)
499 struct uct *u = e->data;
500 if (!u->t) uct_prepare_move(u, b, color);
501 assert(u->t);
503 if (ti->dim == TD_GAMES) {
504 /* Don't count in games that already went into the tbook. */
505 ti->len.games += u->t->root->u.playouts;
507 uct_search(u, b, ti, color, u->t, true);
509 assert(ti->dim == TD_GAMES);
510 tree_save(u->t, b, ti->len.games / 100);
512 return true;
515 void
516 uct_dumptbook(struct engine *e, struct board *b, enum stone color)
518 struct uct *u = e->data;
519 struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0,
520 u->max_pruned_size, u->pruning_threshold, u->local_tree_aging, 0);
521 tree_load(t, b);
522 tree_dump(t, 0);
523 tree_done(t);
527 floating_t
528 uct_evaluate_one(struct engine *e, struct board *b, struct time_info *ti, coord_t c, enum stone color)
530 struct uct *u = e->data;
532 struct board b2;
533 board_copy(&b2, b);
534 struct move m = { c, color };
535 int res = board_play(&b2, &m);
536 if (res < 0)
537 return NAN;
538 color = stone_other(color);
540 if (u->t) reset_state(u);
541 uct_prepare_move(u, &b2, color);
542 assert(u->t);
544 floating_t bestval;
545 uct_search(u, &b2, ti, color, u->t, true);
546 struct tree_node *best = u->policy->choose(u->policy, u->t->root, &b2, color, resign);
547 if (!best) {
548 bestval = NAN; // the opponent has no reply!
549 } else {
550 bestval = tree_node_get_value(u->t, 1, best->u.value);
553 reset_state(u); // clean our junk
555 return isnan(bestval) ? NAN : 1.0f - bestval;
558 void
559 uct_evaluate(struct engine *e, struct board *b, struct time_info *ti, floating_t *vals, enum stone color)
561 for (int i = 0; i < b->flen; i++) {
562 if (is_pass(b->f[i]))
563 vals[i] = NAN;
564 else
565 vals[i] = uct_evaluate_one(e, b, ti, b->f[i], color);
570 struct uct *
571 uct_state_init(char *arg, struct board *b)
573 struct uct *u = calloc2(1, sizeof(struct uct));
574 bool pat_setup = false;
576 u->debug_level = debug_level;
577 u->reportfreq = 10000;
578 u->gamelen = MC_GAMELEN;
579 u->resign_threshold = 0.2;
580 u->sure_win_threshold = 0.95;
581 u->mercymin = 0;
582 u->significant_threshold = 50;
583 u->expand_p = 8;
584 u->dumpthres = 1000;
585 u->playout_amaf = true;
586 u->amaf_prior = false;
587 u->max_tree_size = 1408ULL * 1048576;
588 u->fast_alloc = true;
589 u->pruning_threshold = 0;
591 u->threads = 1;
592 u->thread_model = TM_TREEVL;
593 u->virtual_loss = 1;
595 u->pondering_opt = true;
597 u->fuseki_end = 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
598 u->yose_start = 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
599 u->bestr_ratio = 0.02;
600 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
601 // TODO: Further tuning and experiments with better time allocation schemes.
602 u->best2_ratio = 2.5;
603 // Higher values of max_maintime_ratio sometimes cause severe time trouble in tournaments
604 // It might be necessary to reduce it to 1.5 on large board, but more tuning is needed.
605 u->max_maintime_ratio = 2.0;
607 u->val_scale = 0; u->val_points = 40;
608 u->dynkomi_interval = 1000;
609 u->dynkomi_mask = S_BLACK | S_WHITE;
611 u->tenuki_d = 4;
612 u->local_tree_aging = 80;
613 u->local_tree_depth_decay = 1.5;
614 u->local_tree_eval = LTE_ROOT;
615 u->local_tree_neival = true;
617 u->max_slaves = -1;
618 u->slave_index = -1;
619 u->stats_delay = 0.01; // 10 ms
620 u->shared_levels = 1;
622 u->plugins = pluginset_init(b);
624 u->jdict = joseki_load(b->size);
626 if (arg) {
627 char *optspec, *next = arg;
628 while (*next) {
629 optspec = next;
630 next += strcspn(next, ",");
631 if (*next) { *next++ = 0; } else { *next = 0; }
633 char *optname = optspec;
634 char *optval = strchr(optspec, '=');
635 if (optval) *optval++ = 0;
637 /** Basic options */
639 if (!strcasecmp(optname, "debug")) {
640 if (optval)
641 u->debug_level = atoi(optval);
642 else
643 u->debug_level++;
644 } else if (!strcasecmp(optname, "reporting") && optval) {
645 /* The format of output for detailed progress
646 * information (such as current best move and
647 * its value, etc.). */
648 if (!strcasecmp(optval, "text")) {
649 /* Plaintext traditional output. */
650 u->reporting = UR_TEXT;
651 } else if (!strcasecmp(optval, "json")) {
652 /* JSON output. Implies debug=0. */
653 u->reporting = UR_JSON;
654 u->debug_level = 0;
655 } else if (!strcasecmp(optval, "jsonbig")) {
656 /* JSON output, but much more detailed.
657 * Implies debug=0. */
658 u->reporting = UR_JSON_BIG;
659 u->debug_level = 0;
660 } else {
661 fprintf(stderr, "UCT: Invalid reporting format %s\n", optval);
662 exit(1);
664 } else if (!strcasecmp(optname, "reportfreq") && optval) {
665 /* The progress information line will be shown
666 * every <reportfreq> simulations. */
667 u->reportfreq = atoi(optval);
668 } else if (!strcasecmp(optname, "dumpthres") && optval) {
669 /* When dumping the UCT tree on output, include
670 * nodes with at least this many playouts.
671 * (This value is re-scaled "intelligently"
672 * in case of very large trees.) */
673 u->dumpthres = atoi(optval);
674 } else if (!strcasecmp(optname, "resign_threshold") && optval) {
675 /* Resign when this ratio of games is lost
676 * after GJ_MINGAMES sample is taken. */
677 u->resign_threshold = atof(optval);
678 } else if (!strcasecmp(optname, "sure_win_threshold") && optval) {
679 /* Stop reading when this ratio of games is won
680 * after PLAYOUT_EARLY_BREAK_MIN sample is
681 * taken. (Prevents stupid time losses,
682 * friendly to human opponents.) */
683 u->sure_win_threshold = atof(optval);
684 } else if (!strcasecmp(optname, "force_seed") && optval) {
685 /* Set RNG seed at the tree setup. */
686 u->force_seed = atoi(optval);
687 } else if (!strcasecmp(optname, "no_tbook")) {
688 /* Disable UCT opening tbook. */
689 u->no_tbook = true;
690 } else if (!strcasecmp(optname, "pass_all_alive")) {
691 /* Whether to consider passing only after all
692 * dead groups were removed from the board;
693 * this is like all genmoves are in fact
694 * kgs-genmove_cleanup. */
695 u->pass_all_alive = !optval || atoi(optval);
696 } else if (!strcasecmp(optname, "allow_losing_pass")) {
697 /* Whether to consider passing in a clear
698 * but losing situation, to be scored as a loss
699 * for us. */
700 u->allow_losing_pass = !optval || atoi(optval);
701 } else if (!strcasecmp(optname, "territory_scoring")) {
702 /* Use territory scoring (default is area scoring).
703 * An explicit kgs-rules command overrides this. */
704 u->territory_scoring = !optval || atoi(optval);
705 } else if (!strcasecmp(optname, "stones_only")) {
706 /* Do not count eyes. Nice to teach go to kids.
707 * http://strasbourg.jeudego.org/regle_strasbourgeoise.htm */
708 b->rules = RULES_STONES_ONLY;
709 u->pass_all_alive = true;
710 } else if (!strcasecmp(optname, "banner") && optval) {
711 /* Additional banner string. This must come as the
712 * last engine parameter. You can use '+' instead
713 * of ' ' if you are wrestling with kgsGtp. */
714 if (*next) *--next = ',';
715 u->banner = strdup(optval);
716 for (char *b = u->banner; *b; b++) {
717 if (*b == '+') *b = ' ';
719 break;
720 } else if (!strcasecmp(optname, "plugin") && optval) {
721 /* Load an external plugin; filename goes before the colon,
722 * extra arguments after the colon. */
723 char *pluginarg = strchr(optval, ':');
724 if (pluginarg)
725 *pluginarg++ = 0;
726 plugin_load(u->plugins, optval, pluginarg);
728 /** UCT behavior and policies */
730 } else if ((!strcasecmp(optname, "policy")
731 /* Node selection policy. ucb1amaf is the
732 * default policy implementing RAVE, while
733 * ucb1 is the simple exploration/exploitation
734 * policy. Policies can take further extra
735 * options. */
736 || !strcasecmp(optname, "random_policy")) && optval) {
737 /* A policy to be used randomly with small
738 * chance instead of the default policy. */
739 char *policyarg = strchr(optval, ':');
740 struct uct_policy **p = !strcasecmp(optname, "policy") ? &u->policy : &u->random_policy;
741 if (policyarg)
742 *policyarg++ = 0;
743 if (!strcasecmp(optval, "ucb1")) {
744 *p = policy_ucb1_init(u, policyarg);
745 } else if (!strcasecmp(optval, "ucb1amaf")) {
746 *p = policy_ucb1amaf_init(u, policyarg, b);
747 } else {
748 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
749 exit(1);
751 } else if (!strcasecmp(optname, "playout") && optval) {
752 /* Random simulation (playout) policy.
753 * moggy is the default policy with large
754 * amount of domain-specific knowledge and
755 * heuristics. light is a simple uniformly
756 * random move selection policy. */
757 char *playoutarg = strchr(optval, ':');
758 if (playoutarg)
759 *playoutarg++ = 0;
760 if (!strcasecmp(optval, "moggy")) {
761 u->playout = playout_moggy_init(playoutarg, b, u->jdict);
762 } else if (!strcasecmp(optval, "light")) {
763 u->playout = playout_light_init(playoutarg, b);
764 } else {
765 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
766 exit(1);
768 } else if (!strcasecmp(optname, "prior") && optval) {
769 /* Node priors policy. When expanding a node,
770 * it will seed node values heuristically
771 * (most importantly, based on playout policy
772 * opinion, but also with regard to other
773 * things). See uct/prior.c for details.
774 * Use prior=eqex=0 to disable priors. */
775 u->prior = uct_prior_init(optval, b, u);
776 } else if (!strcasecmp(optname, "mercy") && optval) {
777 /* Minimal difference of black/white captures
778 * to stop playout - "Mercy Rule". Speeds up
779 * hopeless playouts at the expense of some
780 * accuracy. */
781 u->mercymin = atoi(optval);
782 } else if (!strcasecmp(optname, "gamelen") && optval) {
783 /* Maximum length of single simulation
784 * in moves. */
785 u->gamelen = atoi(optval);
786 } else if (!strcasecmp(optname, "expand_p") && optval) {
787 /* Expand UCT nodes after it has been
788 * visited this many times. */
789 u->expand_p = atoi(optval);
790 } else if (!strcasecmp(optname, "random_policy_chance") && optval) {
791 /* If specified (N), with probability 1/N, random_policy policy
792 * descend is used instead of main policy descend; useful
793 * if specified policy (e.g. UCB1AMAF) can make unduly biased
794 * choices sometimes, you can fall back to e.g.
795 * random_policy=UCB1. */
796 u->random_policy_chance = atoi(optval);
798 /** General AMAF behavior */
799 /* (Only relevant if the policy supports AMAF.
800 * More variables can be tuned as policy
801 * parameters.) */
803 } else if (!strcasecmp(optname, "playout_amaf")) {
804 /* Whether to include random playout moves in
805 * AMAF as well. (Otherwise, only tree moves
806 * are included in AMAF. Of course makes sense
807 * only in connection with an AMAF policy.) */
808 /* with-without: 55.5% (+-4.1) */
809 if (optval && *optval == '0')
810 u->playout_amaf = false;
811 else
812 u->playout_amaf = true;
813 } else if (!strcasecmp(optname, "playout_amaf_cutoff") && optval) {
814 /* Keep only first N% of playout stage AMAF
815 * information. */
816 u->playout_amaf_cutoff = atoi(optval);
817 } else if (!strcasecmp(optname, "amaf_prior") && optval) {
818 /* In node policy, consider prior values
819 * part of the real result term or part
820 * of the AMAF term? */
821 u->amaf_prior = atoi(optval);
823 /** Performance and memory management */
825 } else if (!strcasecmp(optname, "threads") && optval) {
826 /* By default, Pachi will run with only single
827 * tree search thread! */
828 u->threads = atoi(optval);
829 } else if (!strcasecmp(optname, "thread_model") && optval) {
830 if (!strcasecmp(optval, "tree")) {
831 /* Tree parallelization - all threads
832 * grind on the same tree. */
833 u->thread_model = TM_TREE;
834 u->virtual_loss = 0;
835 } else if (!strcasecmp(optval, "treevl")) {
836 /* Tree parallelization, but also
837 * with virtual losses - this discou-
838 * rages most threads choosing the
839 * same tree branches to read. */
840 u->thread_model = TM_TREEVL;
841 } else {
842 fprintf(stderr, "UCT: Invalid thread model %s\n", optval);
843 exit(1);
845 } else if (!strcasecmp(optname, "virtual_loss") && optval) {
846 /* Number of virtual losses added before evaluating a node. */
847 u->virtual_loss = atoi(optval);
848 } else if (!strcasecmp(optname, "pondering")) {
849 /* Keep searching even during opponent's turn. */
850 u->pondering_opt = !optval || atoi(optval);
851 } else if (!strcasecmp(optname, "max_tree_size") && optval) {
852 /* Maximum amount of memory [MiB] consumed by the move tree.
853 * For fast_alloc it includes the temp tree used for pruning.
854 * Default is 3072 (3 GiB). */
855 u->max_tree_size = atol(optval) * 1048576;
856 } else if (!strcasecmp(optname, "fast_alloc")) {
857 u->fast_alloc = !optval || atoi(optval);
858 } else if (!strcasecmp(optname, "pruning_threshold") && optval) {
859 /* Force pruning at beginning of a move if the tree consumes
860 * more than this [MiB]. Default is 10% of max_tree_size.
861 * Increase to reduce pruning time overhead if memory is plentiful.
862 * This option is meaningful only for fast_alloc. */
863 u->pruning_threshold = atol(optval) * 1048576;
865 /** Time control */
867 } else if (!strcasecmp(optname, "best2_ratio") && optval) {
868 /* If set, prolong simulating while
869 * first_best/second_best playouts ratio
870 * is less than best2_ratio. */
871 u->best2_ratio = atof(optval);
872 } else if (!strcasecmp(optname, "bestr_ratio") && optval) {
873 /* If set, prolong simulating while
874 * best,best_best_child values delta
875 * is more than bestr_ratio. */
876 u->bestr_ratio = atof(optval);
877 } else if (!strcasecmp(optname, "max_maintime_ratio") && optval) {
878 /* If set and while not in byoyomi, prolong simulating no more than
879 * max_maintime_ratio times the normal desired thinking time. */
880 u->max_maintime_ratio = atof(optval);
881 } else if (!strcasecmp(optname, "fuseki_end") && optval) {
882 /* At the very beginning it's not worth thinking
883 * too long because the playout evaluations are
884 * very noisy. So gradually increase the thinking
885 * time up to maximum when fuseki_end percent
886 * of the board has been played.
887 * This only applies if we are not in byoyomi. */
888 u->fuseki_end = atoi(optval);
889 } else if (!strcasecmp(optname, "yose_start") && optval) {
890 /* When yose_start percent of the board has been
891 * played, or if we are in byoyomi, stop spending
892 * more time and spread the remaining time
893 * uniformly.
894 * Between fuseki_end and yose_start, we spend
895 * a constant proportion of the remaining time
896 * on each move. (yose_start should actually
897 * be much earlier than when real yose start,
898 * but "yose" is a good short name to convey
899 * the idea.) */
900 u->yose_start = atoi(optval);
902 /** Dynamic komi */
904 } else if (!strcasecmp(optname, "dynkomi") && optval) {
905 /* Dynamic komi approach; there are multiple
906 * ways to adjust komi dynamically throughout
907 * play. We currently support two: */
908 char *dynkomiarg = strchr(optval, ':');
909 if (dynkomiarg)
910 *dynkomiarg++ = 0;
911 if (!strcasecmp(optval, "none")) {
912 u->dynkomi = uct_dynkomi_init_none(u, dynkomiarg, b);
913 } else if (!strcasecmp(optval, "linear")) {
914 /* You should set dynkomi_mask=1 or a very low
915 * handicap_value for white. */
916 u->dynkomi = uct_dynkomi_init_linear(u, dynkomiarg, b);
917 } else if (!strcasecmp(optval, "adaptive")) {
918 /* There are many more knobs to
919 * crank - see uct/dynkomi.c. */
920 u->dynkomi = uct_dynkomi_init_adaptive(u, dynkomiarg, b);
921 } else {
922 fprintf(stderr, "UCT: Invalid dynkomi mode %s\n", optval);
923 exit(1);
925 } else if (!strcasecmp(optname, "dynkomi_mask") && optval) {
926 /* Bitmask of colors the player must be
927 * for dynkomi be applied; the default dynkomi_mask=3 allows
928 * dynkomi even in games where Pachi is white. */
929 u->dynkomi_mask = atoi(optval);
930 } else if (!strcasecmp(optname, "dynkomi_interval") && optval) {
931 /* If non-zero, re-adjust dynamic komi
932 * throughout a single genmove reading,
933 * roughly every N simulations. */
934 /* XXX: Does not work with tree
935 * parallelization. */
936 u->dynkomi_interval = atoi(optval);
937 } else if (!strcasecmp(optname, "extra_komi") && optval) {
938 /* Initial dynamic komi settings. This
939 * is useful for the adaptive dynkomi
940 * policy as the value to start with
941 * (this is NOT kept fixed) in case
942 * there is not enough time in the search
943 * to adjust the value properly (e.g. the
944 * game was interrupted). */
945 u->initial_extra_komi = atof(optval);
947 /** Node value result scaling */
949 } else if (!strcasecmp(optname, "val_scale") && optval) {
950 /* How much of the game result value should be
951 * influenced by win size. Zero means it isn't. */
952 u->val_scale = atof(optval);
953 } else if (!strcasecmp(optname, "val_points") && optval) {
954 /* Maximum size of win to be scaled into game
955 * result value. Zero means boardsize^2. */
956 u->val_points = atoi(optval) * 2; // result values are doubled
957 } else if (!strcasecmp(optname, "val_extra")) {
958 /* If false, the score coefficient will be simply
959 * added to the value, instead of scaling the result
960 * coefficient because of it. */
961 u->val_extra = !optval || atoi(optval);
962 } else if (!strcasecmp(optname, "val_byavg")) {
963 /* If true, the score included in the value will
964 * be relative to average score in the current
965 * search episode inst. of jigo. */
966 u->val_byavg = !optval || atoi(optval);
967 } else if (!strcasecmp(optname, "val_bytemp")) {
968 /* If true, the value scaling coefficient
969 * is different based on value extremity
970 * (dist. from 0.5), linear between
971 * val_bytemp_min, val_scale. */
972 u->val_bytemp = !optval || atoi(optval);
973 } else if (!strcasecmp(optname, "val_bytemp_min") && optval) {
974 /* Minimum val_scale in case of val_bytemp. */
975 u->val_bytemp_min = atof(optval);
977 /** Local trees */
978 /* (Purely experimental. Does not work - yet!) */
980 } else if (!strcasecmp(optname, "local_tree")) {
981 /* Whether to bias exploration by local tree values. */
982 u->local_tree = !optval || atoi(optval);
983 } else if (!strcasecmp(optname, "tenuki_d") && optval) {
984 /* Tenuki distance at which to break the local tree. */
985 u->tenuki_d = atoi(optval);
986 if (u->tenuki_d > TREE_NODE_D_MAX + 1) {
987 fprintf(stderr, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX + 1);
988 exit(1);
990 } else if (!strcasecmp(optname, "local_tree_aging") && optval) {
991 /* How much to reduce local tree values between moves. */
992 u->local_tree_aging = atof(optval);
993 } else if (!strcasecmp(optname, "local_tree_depth_decay") && optval) {
994 /* With value x>0, during the descent the node
995 * contributes 1/x^depth playouts in
996 * the local tree. I.e., with x>1, nodes more
997 * distant from local situation contribute more
998 * than nodes near the root. */
999 u->local_tree_depth_decay = atof(optval);
1000 } else if (!strcasecmp(optname, "local_tree_allseq")) {
1001 /* If disabled, only complete sequences are stored
1002 * in the local tree. If this is on, also
1003 * subsequences starting at each move are stored. */
1004 u->local_tree_allseq = !optval || atoi(optval);
1005 } else if (!strcasecmp(optname, "local_tree_neival")) {
1006 /* If disabled, local node value is not
1007 * computed just based on terminal status
1008 * of the coordinate, but also its neighbors. */
1009 u->local_tree_neival = !optval || atoi(optval);
1010 } else if (!strcasecmp(optname, "local_tree_eval")) {
1011 /* How is the value inserted in the local tree
1012 * determined. */
1013 if (!strcasecmp(optval, "root"))
1014 /* All moves within a tree branch are
1015 * considered wrt. their merit
1016 * reaching tachtical goal of making
1017 * the first move in the branch
1018 * survive. */
1019 u->local_tree_eval = LTE_ROOT;
1020 else if (!strcasecmp(optval, "each"))
1021 /* Each move is considered wrt.
1022 * its own survival. */
1023 u->local_tree_eval = LTE_EACH;
1024 else if (!strcasecmp(optval, "total"))
1025 /* The tactical goal is the survival
1026 * of all the moves of my color and
1027 * non-survival of all the opponent
1028 * moves. Local values (and their
1029 * inverses) are averaged. */
1030 u->local_tree_eval = LTE_TOTAL;
1031 else {
1032 fprintf(stderr, "uct: unknown local_tree_eval %s\n", optval);
1033 exit(1);
1035 } else if (!strcasecmp(optname, "local_tree_rootchoose")) {
1036 /* If disabled, only moves within the local
1037 * tree branch are considered; the values
1038 * of the branch roots (i.e. root children)
1039 * are ignored. This may make sense together
1040 * with eval!=each, we consider only moves
1041 * that influence the goal, not the "rating"
1042 * of the goal itself. (The real solution
1043 * will be probably using criticality to pick
1044 * local tree branches.) */
1045 u->local_tree_rootchoose = !optval || atoi(optval);
1047 /** Other heuristics */
1048 } else if (!strcasecmp(optname, "patterns")) {
1049 /* Load pattern database. Various modules
1050 * (priors, policies etc.) may make use
1051 * of this database. They will request
1052 * it automatically in that case, but you
1053 * can use this option to tweak the pattern
1054 * parameters. */
1055 patterns_init(&u->pat, optval, false, true);
1056 u->want_pat = pat_setup = true;
1057 } else if (!strcasecmp(optname, "significant_threshold") && optval) {
1058 /* Some heuristics (XXX: none in mainline) rely
1059 * on the knowledge of the last "significant"
1060 * node in the descent. Such a node is
1061 * considered reasonably trustworthy to carry
1062 * some meaningful information in the values
1063 * of the node and its children. */
1064 u->significant_threshold = atoi(optval);
1065 } else if (!strcasecmp(optname, "libmap")) {
1066 /* Online learning of move tactical ratings by
1067 * liberty maps. */
1068 libmap_setup(optval);
1069 libmap_init(b);
1071 /** Distributed engine slaves setup */
1073 } else if (!strcasecmp(optname, "slave")) {
1074 /* Act as slave for the distributed engine. */
1075 u->slave = !optval || atoi(optval);
1076 } else if (!strcasecmp(optname, "slave_index") && optval) {
1077 /* Optional index if per-slave behavior is desired.
1078 * Must be given as index/max */
1079 u->slave_index = atoi(optval);
1080 char *p = strchr(optval, '/');
1081 if (p) u->max_slaves = atoi(++p);
1082 } else if (!strcasecmp(optname, "shared_nodes") && optval) {
1083 /* Share at most shared_nodes between master and slave at each genmoves.
1084 * Must use the same value in master and slaves. */
1085 u->shared_nodes = atoi(optval);
1086 } else if (!strcasecmp(optname, "shared_levels") && optval) {
1087 /* Share only nodes of level <= shared_levels. */
1088 u->shared_levels = atoi(optval);
1089 } else if (!strcasecmp(optname, "stats_hbits") && optval) {
1090 /* Set hash table size to 2^stats_hbits for the shared stats. */
1091 u->stats_hbits = atoi(optval);
1092 } else if (!strcasecmp(optname, "stats_delay") && optval) {
1093 /* How long to wait in slave for initial stats to build up before
1094 * replying to the genmoves command (in ms) */
1095 u->stats_delay = 0.001 * atof(optval);
1097 /** Presets */
1099 } else if (!strcasecmp(optname, "maximize_score")) {
1100 /* A combination of settings that will make
1101 * Pachi try to maximize his points (instead
1102 * of playing slack yose) or minimize his loss
1103 * (and proceed to counting even when losing). */
1104 /* Please note that this preset might be
1105 * somewhat weaker than normal Pachi, and the
1106 * score maximization is approximate; point size
1107 * of win/loss still should not be used to judge
1108 * strength of Pachi or the opponent. */
1109 /* See README for some further notes. */
1110 if (!optval || atoi(optval)) {
1111 /* Allow scoring a lost game. */
1112 u->allow_losing_pass = true;
1113 /* Make Pachi keep his calm when losing
1114 * and/or maintain winning marging. */
1115 /* Do not play games that are losing
1116 * by too much. */
1117 /* XXX: komi_ratchet_age=40000 is necessary
1118 * with losing_komi_ratchet, but 40000
1119 * is somewhat arbitrary value. */
1120 char dynkomi_args[] = "losing_komi_ratchet:komi_ratchet_age=60000:no_komi_at_game_end=0:max_losing_komi=30";
1121 u->dynkomi = uct_dynkomi_init_adaptive(u, dynkomi_args, b);
1122 /* XXX: Values arbitrary so far. */
1123 /* XXX: Also, is bytemp sensible when
1124 * combined with dynamic komi?! */
1125 u->val_scale = 0.01;
1126 u->val_bytemp = true;
1127 u->val_bytemp_min = 0.001;
1128 u->val_byavg = true;
1131 } else {
1132 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
1133 exit(1);
1138 if (!u->policy)
1139 u->policy = policy_ucb1amaf_init(u, NULL, b);
1141 if (!!u->random_policy_chance ^ !!u->random_policy) {
1142 fprintf(stderr, "uct: Only one of random_policy and random_policy_chance is set\n");
1143 exit(1);
1146 if (!u->local_tree) {
1147 /* No ltree aging. */
1148 u->local_tree_aging = 1.0f;
1151 if (u->fast_alloc) {
1152 if (u->pruning_threshold < u->max_tree_size / 10)
1153 u->pruning_threshold = u->max_tree_size / 10;
1154 if (u->pruning_threshold > u->max_tree_size / 2)
1155 u->pruning_threshold = u->max_tree_size / 2;
1157 /* Limit pruning temp space to 20% of memory. Beyond this we discard
1158 * the nodes and recompute them at the next move if necessary. */
1159 u->max_pruned_size = u->max_tree_size / 5;
1160 u->max_tree_size -= u->max_pruned_size;
1161 } else {
1162 /* Reserve 5% memory in case the background free() are slower
1163 * than the concurrent allocations. */
1164 u->max_tree_size -= u->max_tree_size / 20;
1167 if (!u->prior)
1168 u->prior = uct_prior_init(NULL, b, u);
1170 if (!u->playout)
1171 u->playout = playout_moggy_init(NULL, b, u->jdict);
1172 if (!u->playout->debug_level)
1173 u->playout->debug_level = u->debug_level;
1175 if (u->want_pat && !pat_setup)
1176 patterns_init(&u->pat, NULL, false, true);
1178 u->ownermap.map = malloc2(board_size2(b) * sizeof(u->ownermap.map[0]));
1180 if (u->slave) {
1181 if (!u->stats_hbits) u->stats_hbits = DEFAULT_STATS_HBITS;
1182 if (!u->shared_nodes) u->shared_nodes = DEFAULT_SHARED_NODES;
1183 assert(u->shared_levels * board_bits2(b) <= 8 * (int)sizeof(path_t));
1186 if (!u->dynkomi)
1187 u->dynkomi = board_small(b) ? uct_dynkomi_init_none(u, NULL, b)
1188 : uct_dynkomi_init_linear(u, NULL, b);
1190 /* Some things remain uninitialized for now - the opening tbook
1191 * is not loaded and the tree not set up. */
1192 /* This will be initialized in setup_state() at the first move
1193 * received/requested. This is because right now we are not aware
1194 * about any komi or handicap setup and such. */
1196 return u;
1199 struct engine *
1200 engine_uct_init(char *arg, struct board *b)
1202 struct uct *u = uct_state_init(arg, b);
1203 struct engine *e = calloc2(1, sizeof(struct engine));
1204 e->name = "UCT";
1205 e->printhook = uct_printhook_ownermap;
1206 e->notify_play = uct_notify_play;
1207 e->chat = uct_chat;
1208 e->undo = uct_undo;
1209 e->result = uct_result;
1210 e->genmove = uct_genmove;
1211 e->genmoves = uct_genmoves;
1212 e->evaluate = uct_evaluate;
1213 e->dead_group_list = uct_dead_group_list;
1214 e->stop = uct_stop;
1215 e->done = uct_done;
1216 e->data = u;
1217 if (u->slave)
1218 e->notify = uct_notify;
1220 const char banner[] = "If you believe you have won but I am still playing, "
1221 "please help me understand by capturing all dead stones. "
1222 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1223 if (!u->banner) u->banner = "";
1224 e->comment = malloc2(sizeof(banner) + strlen(u->banner) + 1);
1225 sprintf(e->comment, "%s %s", banner, u->banner);
1227 return e;