UCT: Progressive Unpruning support (for all policies, tunable)
[pachi.git] / uct / uct.c
blob024dfba6cbf46c29087fffd284391fe838aa76bf
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 "move.h"
14 #include "mq.h"
15 #include "joseki/base.h"
16 #include "playout.h"
17 #include "playout/elo.h"
18 #include "playout/moggy.h"
19 #include "playout/light.h"
20 #include "tactics/util.h"
21 #include "timeinfo.h"
22 #include "uct/dynkomi.h"
23 #include "uct/internal.h"
24 #include "uct/plugins.h"
25 #include "uct/prior.h"
26 #include "uct/search.h"
27 #include "uct/slave.h"
28 #include "uct/tree.h"
29 #include "uct/uct.h"
30 #include "uct/walk.h"
32 struct uct_policy *policy_ucb1_init(struct uct *u, char *arg);
33 struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg);
34 static void uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color);
36 /* Maximal simulation length. */
37 #define MC_GAMELEN MAX_GAMELEN
40 static void
41 setup_state(struct uct *u, struct board *b, enum stone color)
43 u->t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0,
44 u->max_pruned_size, u->pruning_threshold, u->local_tree_aging, u->stats_hbits);
45 if (u->force_seed)
46 fast_srandom(u->force_seed);
47 if (UDEBUGL(0))
48 fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed());
49 //board_print(b, stderr);
50 if (!u->no_tbook && b->moves == 0) {
51 assert(color == S_BLACK);
52 tree_load(u->t, b);
56 static void
57 reset_state(struct uct *u)
59 assert(u->t);
60 tree_done(u->t); u->t = NULL;
63 static void
64 setup_dynkomi(struct uct *u, struct board *b, enum stone to_play)
66 if (u->t->use_extra_komi && !u->pondering && u->dynkomi->permove)
67 u->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, u->t);
68 else if (!u->t->use_extra_komi)
69 u->t->extra_komi = 0;
72 void
73 uct_prepare_move(struct uct *u, struct board *b, enum stone color)
75 if (u->t) {
76 /* Verify that we have sane state. */
77 assert(b->es == u);
78 assert(u->t && b->moves);
79 if (color != stone_other(u->t->root_color)) {
80 fprintf(stderr, "Fatal: Non-alternating play detected %d %d\n",
81 color, u->t->root_color);
82 exit(1);
84 uct_htable_reset(u->t);
86 } else {
87 /* We need fresh state. */
88 b->es = u;
89 setup_state(u, b, color);
92 u->ownermap.playouts = 0;
93 memset(u->ownermap.map, 0, board_size2(b) * sizeof(u->ownermap.map[0]));
94 u->played_own = u->played_all = 0;
97 static void
98 dead_group_list(struct uct *u, struct board *b, struct move_queue *mq)
100 struct group_judgement gj;
101 gj.thres = GJ_THRES;
102 gj.gs = alloca(board_size2(b) * sizeof(gj.gs[0]));
103 board_ownermap_judge_group(b, &u->ownermap, &gj);
104 groups_of_status(b, &gj, GS_DEAD, mq);
107 bool
108 uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive)
110 if (u->ownermap.playouts < GJ_MINGAMES)
111 return false;
113 struct move_queue mq = { .moves = 0 };
114 dead_group_list(u, b, &mq);
115 if (pass_all_alive && mq.moves > 0)
116 return false; // We need to remove some dead groups first.
117 return pass_is_safe(b, color, &mq);
120 static char *
121 uct_printhook_ownermap(struct board *board, coord_t c, char *s, char *end)
123 struct uct *u = board->es;
124 if (!u) {
125 strcat(s, ". ");
126 return s + 2;
128 const char chr[] = ":XO,"; // dame, black, white, unclear
129 const char chm[] = ":xo,";
130 char ch = chr[board_ownermap_judge_point(&u->ownermap, c, GJ_THRES)];
131 if (ch == ',') { // less precise estimate then?
132 ch = chm[board_ownermap_judge_point(&u->ownermap, c, 0.67)];
134 s += snprintf(s, end - s, "%c ", ch);
135 return s;
138 static char *
139 uct_notify_play(struct engine *e, struct board *b, struct move *m)
141 struct uct *u = e->data;
142 if (!u->t) {
143 /* No state, create one - this is probably game beginning
144 * and we need to load the opening tbook right now. */
145 uct_prepare_move(u, b, m->color);
146 assert(u->t);
149 /* Stop pondering, required by tree_promote_at() */
150 uct_pondering_stop(u);
151 if (UDEBUGL(2) && u->slave)
152 tree_dump(u->t, u->dumpthres);
154 if (is_resign(m->coord)) {
155 /* Reset state. */
156 reset_state(u);
157 return NULL;
160 /* Promote node of the appropriate move to the tree root. */
161 assert(u->t->root);
162 if (!tree_promote_at(u->t, b, m->coord)) {
163 if (UDEBUGL(0))
164 fprintf(stderr, "Warning: Cannot promote move node! Several play commands in row?\n");
165 reset_state(u);
166 return NULL;
169 /* If we are a slave in a distributed engine, start pondering once
170 * we know which move we actually played. See uct_genmove() about
171 * the check for pass. */
172 if (u->pondering_opt && u->slave && m->color == u->my_color && !is_pass(m->coord))
173 uct_pondering_start(u, b, u->t, stone_other(m->color));
175 return NULL;
178 static char *
179 uct_result(struct engine *e, struct board *b)
181 struct uct *u = e->data;
182 static char reply[1024];
184 if (!u->t)
185 return NULL;
186 enum stone color = u->t->root_color;
187 struct tree_node *n = u->t->root;
188 snprintf(reply, 1024, "%s %s %d %.2f %.1f",
189 stone2str(color), coord2sstr(n->coord, b),
190 n->u.playouts, tree_node_get_value(u->t, -1, n->u.value),
191 u->t->use_extra_komi ? u->t->extra_komi : 0);
192 return reply;
195 static char *
196 uct_chat(struct engine *e, struct board *b, char *cmd)
198 struct uct *u = e->data;
199 static char reply[1024];
201 cmd += strspn(cmd, " \n\t");
202 if (!strncasecmp(cmd, "winrate", 7)) {
203 if (!u->t)
204 return "no game context (yet?)";
205 enum stone color = u->t->root_color;
206 struct tree_node *n = u->t->root;
207 snprintf(reply, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
208 n->u.playouts, u->threads, stone2str(color), coord2sstr(n->coord, b),
209 tree_node_get_value(u->t, -1, n->u.value) * 100);
210 if (u->t->use_extra_komi && abs(u->t->extra_komi) >= 0.5) {
211 sprintf(reply + strlen(reply), ", while self-imposing extra komi %.1f",
212 u->t->extra_komi);
214 strcat(reply, ".");
215 return reply;
217 return NULL;
220 static void
221 uct_dead_group_list(struct engine *e, struct board *b, struct move_queue *mq)
223 struct uct *u = e->data;
225 /* This means the game is probably over, no use pondering on. */
226 uct_pondering_stop(u);
228 if (u->pass_all_alive)
229 return; // no dead groups
231 bool mock_state = false;
233 if (!u->t) {
234 /* No state, but we cannot just back out - we might
235 * have passed earlier, only assuming some stones are
236 * dead, and then re-connected, only to lose counting
237 * when all stones are assumed alive. */
238 uct_prepare_move(u, b, S_BLACK); assert(u->t);
239 mock_state = true;
241 /* Make sure the ownermap is well-seeded. */
242 while (u->ownermap.playouts < GJ_MINGAMES)
243 uct_playout(u, b, S_BLACK, u->t);
244 /* Show the ownermap: */
245 if (DEBUGL(2))
246 board_print_custom(b, stderr, uct_printhook_ownermap);
248 dead_group_list(u, b, mq);
250 if (mock_state) {
251 /* Clean up the mock state in case we will receive
252 * a genmove; we could get a non-alternating-move
253 * error from uct_prepare_move() in that case otherwise. */
254 reset_state(u);
258 static void
259 playout_policy_done(struct playout_policy *p)
261 if (p->done) p->done(p);
262 if (p->data) free(p->data);
263 free(p);
266 static void
267 uct_done(struct engine *e)
269 /* This is called on engine reset, especially when clear_board
270 * is received and new game should begin. */
271 struct uct *u = e->data;
272 uct_pondering_stop(u);
273 if (u->t) reset_state(u);
274 free(u->ownermap.map);
276 free(u->policy);
277 free(u->random_policy);
278 playout_policy_done(u->playout);
279 uct_prior_done(u->prior);
280 joseki_done(u->jdict);
281 pluginset_done(u->plugins);
286 /* Run time-limited MCTS search on foreground. */
287 static int
288 uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t)
290 struct uct_search_state s;
291 uct_search_start(u, b, color, t, ti, &s);
292 if (UDEBUGL(2) && s.base_playouts > 0)
293 fprintf(stderr, "<pre-simulated %d games>\n", s.base_playouts);
295 /* The search tree is ctx->t. This is currently == . It is important
296 * to reference ctx->t directly since the
297 * thread manager will swap the tree pointer asynchronously. */
299 /* Now, just periodically poll the search tree. */
300 /* Note that in case of TD_GAMES, threads will terminate independently
301 * of the uct_search_check_stop() signalization. */
302 while (1) {
303 time_sleep(TREE_BUSYWAIT_INTERVAL);
304 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
305 * time control is broken. But if it happens to be less, we still search
306 * at least 100ms otherwise the move is completely random. */
308 int i = uct_search_games(&s);
309 /* Print notifications etc. */
310 uct_search_progress(u, b, color, t, ti, &s, i);
311 /* Check if we should stop the search. */
312 if (uct_search_check_stop(u, b, color, t, ti, &s, i))
313 break;
316 struct uct_thread_ctx *ctx = uct_search_stop();
317 if (UDEBUGL(2)) tree_dump(t, u->dumpthres);
318 if (UDEBUGL(2))
319 fprintf(stderr, "(avg score %f/%d value %f/%d)\n",
320 u->dynkomi->score.value, u->dynkomi->score.playouts,
321 u->dynkomi->value.value, u->dynkomi->value.playouts);
322 if (UDEBUGL(0))
323 uct_progress_status(u, t, color, ctx->games);
325 u->played_own += ctx->games;
326 return ctx->games;
329 /* Start pondering background with @color to play. */
330 static void
331 uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color)
333 if (UDEBUGL(1))
334 fprintf(stderr, "Starting to ponder with color %s\n", stone2str(stone_other(color)));
335 u->pondering = true;
337 /* We need a local board copy to ponder upon. */
338 struct board *b = malloc2(sizeof(*b)); board_copy(b, b0);
340 /* *b0 did not have the genmove'd move played yet. */
341 struct move m = { t->root->coord, t->root_color };
342 int res = board_play(b, &m);
343 assert(res >= 0);
344 setup_dynkomi(u, b, stone_other(m.color));
346 /* Start MCTS manager thread "headless". */
347 static struct uct_search_state s;
348 uct_search_start(u, b, color, t, NULL, &s);
351 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
352 * to stop the background search for a slave in the distributed engine. */
353 void
354 uct_pondering_stop(struct uct *u)
356 if (!thread_manager_running)
357 return;
359 /* Stop the thread manager. */
360 struct uct_thread_ctx *ctx = uct_search_stop();
361 if (UDEBUGL(1)) {
362 if (u->pondering) fprintf(stderr, "(pondering) ");
363 uct_progress_status(u, ctx->t, ctx->color, ctx->games);
365 if (u->pondering) {
366 free(ctx->b);
367 u->pondering = false;
372 void
373 uct_genmove_setup(struct uct *u, struct board *b, enum stone color)
375 if (b->superko_violation) {
376 fprintf(stderr, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
377 fprintf(stderr, "Maybe you play with situational instead of positional superko?\n");
378 fprintf(stderr, "I'm going to ignore the violation, but note that I may miss\n");
379 fprintf(stderr, "some moves valid under this ruleset because of this.\n");
380 b->superko_violation = false;
383 uct_prepare_move(u, b, color);
385 assert(u->t);
386 u->my_color = color;
388 /* How to decide whether to use dynkomi in this game? Since we use
389 * pondering, it's not simple "who-to-play" matter. Decide based on
390 * the last genmove issued. */
391 u->t->use_extra_komi = !!(u->dynkomi_mask & color);
392 /* Moreover, we do not use extra komi at the game end - we are not
393 * to fool ourselves at this point. */
394 if (board_estimated_moves_left(b) <= MIN_MOVES_LEFT)
395 u->t->use_extra_komi = false;
396 setup_dynkomi(u, b, color);
398 if (b->rules == RULES_JAPANESE)
399 u->territory_scoring = true;
401 /* Make pessimistic assumption about komi for Japanese rules to
402 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
403 * The rules usually give the same winner if the integer part of komi
404 * is odd so we adjust the komi only if it is even (for a board of
405 * odd size). We are not trying to get an exact evaluation for rare
406 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
407 if (u->territory_scoring && (((int)floor(b->komi) + board_size(b)) & 1)) {
408 b->komi += (color == S_BLACK ? 1.0 : -1.0);
409 if (UDEBUGL(0))
410 fprintf(stderr, "Setting komi to %.1f assuming Japanese rules\n",
411 b->komi);
415 static coord_t *
416 uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
418 double start_time = time_now();
419 struct uct *u = e->data;
420 uct_pondering_stop(u);
421 uct_genmove_setup(u, b, color);
423 /* Start the Monte Carlo Tree Search! */
424 int base_playouts = u->t->root->u.playouts;
425 int played_games = uct_search(u, b, ti, color, u->t);
427 coord_t best_coord;
428 struct tree_node *best;
429 best = uct_search_result(u, b, color, pass_all_alive, played_games, base_playouts, &best_coord);
431 if (UDEBUGL(2)) {
432 double time = time_now() - start_time + 0.000001; /* avoid divide by zero */
433 fprintf(stderr, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
434 time, (int)(played_games/time), (int)(played_games/time/u->threads));
437 if (!best) {
438 /* Pass or resign. */
439 reset_state(u);
440 return coord_copy(best_coord);
442 tree_promote_node(u->t, &best);
444 /* After a pass, pondering is harmful for two reasons:
445 * (i) We might keep pondering even when the game is over.
446 * Of course this is the case for opponent resign as well.
447 * (ii) More importantly, the ownermap will get skewed since
448 * the UCT will start cutting off any playouts. */
449 if (u->pondering_opt && !is_pass(best->coord)) {
450 uct_pondering_start(u, b, u->t, stone_other(color));
452 return coord_copy(best_coord);
456 bool
457 uct_gentbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color)
459 struct uct *u = e->data;
460 if (!u->t) uct_prepare_move(u, b, color);
461 assert(u->t);
463 if (ti->dim == TD_GAMES) {
464 /* Don't count in games that already went into the tbook. */
465 ti->len.games += u->t->root->u.playouts;
467 uct_search(u, b, ti, color, u->t);
469 assert(ti->dim == TD_GAMES);
470 tree_save(u->t, b, ti->len.games / 100);
472 return true;
475 void
476 uct_dumptbook(struct engine *e, struct board *b, enum stone color)
478 struct uct *u = e->data;
479 struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0,
480 u->max_pruned_size, u->pruning_threshold, u->local_tree_aging, 0);
481 tree_load(t, b);
482 tree_dump(t, 0);
483 tree_done(t);
487 floating_t
488 uct_evaluate(struct engine *e, struct board *b, struct time_info *ti, coord_t c, enum stone color)
490 struct uct *u = e->data;
492 struct board b2;
493 board_copy(&b2, b);
494 struct move m = { c, color };
495 int res = board_play(&b2, &m);
496 if (res < 0)
497 return NAN;
498 color = stone_other(color);
500 if (u->t) reset_state(u);
501 uct_prepare_move(u, &b2, color);
502 assert(u->t);
504 floating_t bestval;
505 uct_search(u, &b2, ti, color, u->t);
506 struct tree_node *best = u->policy->choose(u->policy, u->t->root, &b2, color, resign);
507 if (!best) {
508 bestval = NAN; // the opponent has no reply!
509 } else {
510 bestval = tree_node_get_value(u->t, 1, best->u.value);
513 reset_state(u); // clean our junk
515 return isnan(bestval) ? NAN : 1.0f - bestval;
519 struct uct *
520 uct_state_init(char *arg, struct board *b)
522 struct uct *u = calloc2(1, sizeof(struct uct));
523 bool using_elo = false;
525 u->debug_level = debug_level;
526 u->gamelen = MC_GAMELEN;
527 u->resign_threshold = 0.2;
528 u->sure_win_threshold = 0.85;
529 u->mercymin = 0;
530 u->significant_threshold = 50;
531 u->expand_p = 2;
532 u->dumpthres = 1000;
533 u->playout_amaf = true;
534 u->playout_amaf_nakade = false;
535 u->amaf_prior = false;
536 u->max_tree_size = 3072ULL * 1048576;
537 u->pruning_threshold = 0;
539 u->threads = 1;
540 u->thread_model = TM_TREEVL;
541 u->virtual_loss = 1;
543 u->fuseki_end = 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
544 u->yose_start = 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
545 u->bestr_ratio = 0.02;
546 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
547 // TODO: Further tuning and experiments with better time allocation schemes.
548 u->best2_ratio = 2.5;
549 u->max_maintime_ratio = 3.0;
551 u->val_scale = 0.04; u->val_points = 40;
552 u->dynkomi_interval = 1000;
553 u->dynkomi_mask = S_BLACK | S_WHITE;
555 u->tenuki_d = 4;
556 u->local_tree_aging = 2;
558 u->prune_initial = 5;
559 u->prune_playouts = 40;
560 u->prune_speedup = log(1.3);
562 u->plugins = pluginset_init(b);
564 u->jdict = joseki_load(b->size);
566 if (arg) {
567 char *optspec, *next = arg;
568 while (*next) {
569 optspec = next;
570 next += strcspn(next, ",");
571 if (*next) { *next++ = 0; } else { *next = 0; }
573 char *optname = optspec;
574 char *optval = strchr(optspec, '=');
575 if (optval) *optval++ = 0;
577 /** Basic options */
579 if (!strcasecmp(optname, "debug")) {
580 if (optval)
581 u->debug_level = atoi(optval);
582 else
583 u->debug_level++;
584 } else if (!strcasecmp(optname, "dumpthres") && optval) {
585 /* When dumping the UCT tree on output, include
586 * nodes with at least this many playouts.
587 * (This value is re-scaled "intelligently"
588 * in case of very large trees.) */
589 u->dumpthres = atoi(optval);
590 } else if (!strcasecmp(optname, "resign_threshold") && optval) {
591 /* Resign when this ratio of games is lost
592 * after GJ_MINGAMES sample is taken. */
593 u->resign_threshold = atof(optval);
594 } else if (!strcasecmp(optname, "sure_win_threshold") && optval) {
595 /* Stop reading when this ratio of games is won
596 * after PLAYOUT_EARLY_BREAK_MIN sample is
597 * taken. (Prevents stupid time losses,
598 * friendly to human opponents.) */
599 u->sure_win_threshold = atof(optval);
600 } else if (!strcasecmp(optname, "force_seed") && optval) {
601 /* Set RNG seed at the tree setup. */
602 u->force_seed = atoi(optval);
603 } else if (!strcasecmp(optname, "no_tbook")) {
604 /* Disable UCT opening tbook. */
605 u->no_tbook = true;
606 } else if (!strcasecmp(optname, "pass_all_alive")) {
607 /* Whether to consider passing only after all
608 * dead groups were removed from the board;
609 * this is like all genmoves are in fact
610 * kgs-genmove_cleanup. */
611 u->pass_all_alive = !optval || atoi(optval);
612 } else if (!strcasecmp(optname, "territory_scoring")) {
613 /* Use territory scoring (default is area scoring).
614 * An explicit kgs-rules command overrides this. */
615 u->territory_scoring = !optval || atoi(optval);
616 } else if (!strcasecmp(optname, "banner") && optval) {
617 /* Additional banner string. This must come as the
618 * last engine parameter. */
619 if (*next) *--next = ',';
620 u->banner = strdup(optval);
621 break;
622 } else if (!strcasecmp(optname, "plugin") && optval) {
623 /* Load an external plugin; filename goes before the colon,
624 * extra arguments after the colon. */
625 char *pluginarg = strchr(optval, ':');
626 if (pluginarg)
627 *pluginarg++ = 0;
628 plugin_load(u->plugins, optval, pluginarg);
630 /** UCT behavior and policies */
632 } else if ((!strcasecmp(optname, "policy")
633 /* Node selection policy. ucb1amaf is the
634 * default policy implementing RAVE, while
635 * ucb1 is the simple exploration/exploitation
636 * policy. Policies can take further extra
637 * options. */
638 || !strcasecmp(optname, "random_policy")) && optval) {
639 /* A policy to be used randomly with small
640 * chance instead of the default policy. */
641 char *policyarg = strchr(optval, ':');
642 struct uct_policy **p = !strcasecmp(optname, "policy") ? &u->policy : &u->random_policy;
643 if (policyarg)
644 *policyarg++ = 0;
645 if (!strcasecmp(optval, "ucb1")) {
646 *p = policy_ucb1_init(u, policyarg);
647 } else if (!strcasecmp(optval, "ucb1amaf")) {
648 *p = policy_ucb1amaf_init(u, policyarg);
649 } else {
650 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
651 exit(1);
653 } else if (!strcasecmp(optname, "playout") && optval) {
654 /* Random simulation (playout) policy.
655 * moggy is the default policy with large
656 * amount of domain-specific knowledge and
657 * heuristics. light is a simple uniformly
658 * random move selection policy. */
659 char *playoutarg = strchr(optval, ':');
660 if (playoutarg)
661 *playoutarg++ = 0;
662 if (!strcasecmp(optval, "moggy")) {
663 u->playout = playout_moggy_init(playoutarg, b, u->jdict);
664 } else if (!strcasecmp(optval, "light")) {
665 u->playout = playout_light_init(playoutarg, b);
666 } else if (!strcasecmp(optval, "elo")) {
667 u->playout = playout_elo_init(playoutarg, b);
668 using_elo = true;
669 } else {
670 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
671 exit(1);
673 } else if (!strcasecmp(optname, "prior") && optval) {
674 /* Node priors policy. When expanding a node,
675 * it will seed node values heuristically
676 * (most importantly, based on playout policy
677 * opinion, but also with regard to other
678 * things). See uct/prior.c for details.
679 * Use prior=eqex=0 to disable priors. */
680 u->prior = uct_prior_init(optval, b);
681 } else if (!strcasecmp(optname, "mercy") && optval) {
682 /* Minimal difference of black/white captures
683 * to stop playout - "Mercy Rule". Speeds up
684 * hopeless playouts at the expense of some
685 * accuracy. */
686 u->mercymin = atoi(optval);
687 } else if (!strcasecmp(optname, "gamelen") && optval) {
688 /* Maximum length of single simulation
689 * in moves. */
690 u->gamelen = atoi(optval);
691 } else if (!strcasecmp(optname, "expand_p") && optval) {
692 /* Expand UCT nodes after it has been
693 * visited this many times. */
694 u->expand_p = atoi(optval);
695 } else if (!strcasecmp(optname, "random_policy_chance") && optval) {
696 /* If specified (N), with probability 1/N, random_policy policy
697 * descend is used instead of main policy descend; useful
698 * if specified policy (e.g. UCB1AMAF) can make unduly biased
699 * choices sometimes, you can fall back to e.g.
700 * random_policy=UCB1. */
701 u->random_policy_chance = atoi(optval);
703 /** General AMAF behavior */
704 /* (Only relevant if the policy supports AMAF.
705 * More variables can be tuned as policy
706 * parameters.) */
708 } else if (!strcasecmp(optname, "playout_amaf")) {
709 /* Whether to include random playout moves in
710 * AMAF as well. (Otherwise, only tree moves
711 * are included in AMAF. Of course makes sense
712 * only in connection with an AMAF policy.) */
713 /* with-without: 55.5% (+-4.1) */
714 if (optval && *optval == '0')
715 u->playout_amaf = false;
716 else
717 u->playout_amaf = true;
718 } else if (!strcasecmp(optname, "playout_amaf_nakade")) {
719 /* Whether to include nakade moves from playouts
720 * in the AMAF statistics; this tends to nullify
721 * the playout_amaf effect by adding too much
722 * noise. */
723 if (optval && *optval == '0')
724 u->playout_amaf_nakade = false;
725 else
726 u->playout_amaf_nakade = true;
727 } else if (!strcasecmp(optname, "playout_amaf_cutoff") && optval) {
728 /* Keep only first N% of playout stage AMAF
729 * information. */
730 u->playout_amaf_cutoff = atoi(optval);
731 } else if (!strcasecmp(optname, "amaf_prior") && optval) {
732 /* In node policy, consider prior values
733 * part of the real result term or part
734 * of the AMAF term? */
735 u->amaf_prior = atoi(optval);
737 /** Performance and memory management */
739 } else if (!strcasecmp(optname, "threads") && optval) {
740 /* By default, Pachi will run with only single
741 * tree search thread! */
742 u->threads = atoi(optval);
743 } else if (!strcasecmp(optname, "thread_model") && optval) {
744 if (!strcasecmp(optval, "tree")) {
745 /* Tree parallelization - all threads
746 * grind on the same tree. */
747 u->thread_model = TM_TREE;
748 u->virtual_loss = 0;
749 } else if (!strcasecmp(optval, "treevl")) {
750 /* Tree parallelization, but also
751 * with virtual losses - this discou-
752 * rages most threads choosing the
753 * same tree branches to read. */
754 u->thread_model = TM_TREEVL;
755 } else {
756 fprintf(stderr, "UCT: Invalid thread model %s\n", optval);
757 exit(1);
759 } else if (!strcasecmp(optname, "virtual_loss")) {
760 /* Number of virtual losses added before evaluating a node. */
761 u->virtual_loss = !optval || atoi(optval);
762 } else if (!strcasecmp(optname, "pondering")) {
763 /* Keep searching even during opponent's turn. */
764 u->pondering_opt = !optval || atoi(optval);
765 } else if (!strcasecmp(optname, "max_tree_size") && optval) {
766 /* Maximum amount of memory [MiB] consumed by the move tree.
767 * For fast_alloc it includes the temp tree used for pruning.
768 * Default is 3072 (3 GiB). */
769 u->max_tree_size = atol(optval) * 1048576;
770 } else if (!strcasecmp(optname, "fast_alloc")) {
771 u->fast_alloc = !optval || atoi(optval);
772 } else if (!strcasecmp(optname, "pruning_threshold") && optval) {
773 /* Force pruning at beginning of a move if the tree consumes
774 * more than this [MiB]. Default is 10% of max_tree_size.
775 * Increase to reduce pruning time overhead if memory is plentiful.
776 * This option is meaningful only for fast_alloc. */
777 u->pruning_threshold = atol(optval) * 1048576;
779 /** Time control */
781 } else if (!strcasecmp(optname, "best2_ratio") && optval) {
782 /* If set, prolong simulating while
783 * first_best/second_best playouts ratio
784 * is less than best2_ratio. */
785 u->best2_ratio = atof(optval);
786 } else if (!strcasecmp(optname, "bestr_ratio") && optval) {
787 /* If set, prolong simulating while
788 * best,best_best_child values delta
789 * is more than bestr_ratio. */
790 u->bestr_ratio = atof(optval);
791 } else if (!strcasecmp(optname, "max_maintime_ratio") && optval) {
792 /* If set and while not in byoyomi, prolong simulating no more than
793 * max_maintime_ratio times the normal desired thinking time. */
794 u->max_maintime_ratio = atof(optval);
795 } else if (!strcasecmp(optname, "fuseki_end") && optval) {
796 /* At the very beginning it's not worth thinking
797 * too long because the playout evaluations are
798 * very noisy. So gradually increase the thinking
799 * time up to maximum when fuseki_end percent
800 * of the board has been played.
801 * This only applies if we are not in byoyomi. */
802 u->fuseki_end = atoi(optval);
803 } else if (!strcasecmp(optname, "yose_start") && optval) {
804 /* When yose_start percent of the board has been
805 * played, or if we are in byoyomi, stop spending
806 * more time and spread the remaining time
807 * uniformly.
808 * Between fuseki_end and yose_start, we spend
809 * a constant proportion of the remaining time
810 * on each move. (yose_start should actually
811 * be much earlier than when real yose start,
812 * but "yose" is a good short name to convey
813 * the idea.) */
814 u->yose_start = atoi(optval);
816 /** Dynamic komi */
818 } else if (!strcasecmp(optname, "dynkomi") && optval) {
819 /* Dynamic komi approach; there are multiple
820 * ways to adjust komi dynamically throughout
821 * play. We currently support two: */
822 char *dynkomiarg = strchr(optval, ':');
823 if (dynkomiarg)
824 *dynkomiarg++ = 0;
825 if (!strcasecmp(optval, "none")) {
826 u->dynkomi = uct_dynkomi_init_none(u, dynkomiarg, b);
827 } else if (!strcasecmp(optval, "linear")) {
828 /* You should set dynkomi_mask=1
829 * since this doesn't work well
830 * for white handicaps! */
831 u->dynkomi = uct_dynkomi_init_linear(u, dynkomiarg, b);
832 } else if (!strcasecmp(optval, "adaptive")) {
833 /* There are many more knobs to
834 * crank - see uct/dynkomi.c. */
835 u->dynkomi = uct_dynkomi_init_adaptive(u, dynkomiarg, b);
836 } else {
837 fprintf(stderr, "UCT: Invalid dynkomi mode %s\n", optval);
838 exit(1);
840 } else if (!strcasecmp(optname, "dynkomi_mask") && optval) {
841 /* Bitmask of colors the player must be
842 * for dynkomi be applied; you may want
843 * to use dynkomi_mask=3 to allow dynkomi
844 * even in games where Pachi is white. */
845 u->dynkomi_mask = atoi(optval);
846 } else if (!strcasecmp(optname, "dynkomi_interval") && optval) {
847 /* If non-zero, re-adjust dynamic komi
848 * throughout a single genmove reading,
849 * roughly every N simulations. */
850 /* XXX: Does not work with tree
851 * parallelization. */
852 u->dynkomi_interval = atoi(optval);
854 /** Node value result scaling */
856 } else if (!strcasecmp(optname, "val_scale") && optval) {
857 /* How much of the game result value should be
858 * influenced by win size. Zero means it isn't. */
859 u->val_scale = atof(optval);
860 } else if (!strcasecmp(optname, "val_points") && optval) {
861 /* Maximum size of win to be scaled into game
862 * result value. Zero means boardsize^2. */
863 u->val_points = atoi(optval) * 2; // result values are doubled
864 } else if (!strcasecmp(optname, "val_extra")) {
865 /* If false, the score coefficient will be simply
866 * added to the value, instead of scaling the result
867 * coefficient because of it. */
868 u->val_extra = !optval || atoi(optval);
870 /* Progressive Unpruning */
872 } else if (!strcasecmp(optname, "prune_initial") && optval) {
873 /* Initial number of unpruned moves. */
874 u->prune_playouts = atoi(optval);
875 } else if (!strcasecmp(optname, "prune_playouts") && optval) {
876 /* A term in the progressive unpruning equation. */
877 u->prune_playouts = atoi(optval);
878 } else if (!strcasecmp(optname, "prune_speedup") && optval) {
879 /* B term in the progressive unpruning equation. */
880 u->prune_speedup = log(atof(optval));
882 /** Local trees */
883 /* (Purely experimental. Does not work - yet!) */
885 } else if (!strcasecmp(optname, "local_tree") && optval) {
886 /* Whether to bias exploration by local tree values
887 * (must be supported by the used policy).
888 * 0: Don't.
889 * 1: Do, value = result.
890 * Try to temper the result:
891 * 2: Do, value = 0.5+(result-expected)/2.
892 * 3: Do, value = 0.5+bzz((result-expected)^2).
893 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
894 u->local_tree = atoi(optval);
895 } else if (!strcasecmp(optname, "tenuki_d") && optval) {
896 /* Tenuki distance at which to break the local tree. */
897 u->tenuki_d = atoi(optval);
898 if (u->tenuki_d > TREE_NODE_D_MAX + 1) {
899 fprintf(stderr, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX + 1);
900 exit(1);
902 } else if (!strcasecmp(optname, "local_tree_aging") && optval) {
903 /* How much to reduce local tree values between moves. */
904 u->local_tree_aging = atof(optval);
905 } else if (!strcasecmp(optname, "local_tree_depth_decay") && optval) {
906 /* With value x>0, during the descent the node
907 * contributes 1/x^depth playouts in
908 * the local tree. I.e., with x>1, nodes more
909 * distant from local situation contribute more
910 * than nodes near the root. */
911 u->local_tree_depth_decay = atof(optval);
912 } else if (!strcasecmp(optname, "local_tree_allseq")) {
913 /* By default, only complete sequences are stored
914 * in the local tree. If this is on, also
915 * subsequences starting at each move are stored. */
916 u->local_tree_allseq = !optval || atoi(optval);
917 } else if (!strcasecmp(optname, "local_tree_playout")) {
918 /* Whether to adjust ELO playout probability
919 * distributions according to matched localtree
920 * information. */
921 u->local_tree_playout = !optval || atoi(optval);
922 } else if (!strcasecmp(optname, "local_tree_pseqroot")) {
923 /* By default, when we have no sequence move
924 * to suggest in-playout, we give up. If this
925 * is on, we make probability distribution from
926 * sequences first moves instead. */
927 u->local_tree_pseqroot = !optval || atoi(optval);
929 /** Other heuristics */
930 } else if (!strcasecmp(optname, "significant_threshold") && optval) {
931 /* Some heuristics (treepool) rely
932 * on the knowledge of the last "significant"
933 * node in the descent. Such a node is
934 * considered reasonably trustworthy to carry
935 * some meaningful information in the values
936 * of the node and its children. */
937 u->significant_threshold = atoi(optval);
938 } else if (!strcasecmp(optname, "treepool_chance") && optval) {
939 /* Chance of applying the treepool heuristic:
940 * one of the best N children of the last
941 * significant node is tried on each turn
942 * of the simulation. */
943 /* This is in form of two numbers:
944 * PREMOVE:POSTMOVE. Each is percentage
945 * value, one is the chance of the move
946 * tried before playout policy, one is the
947 * chance of it being applied if the policy
948 * has not picked anymove. */
949 char *optval2 = strchr(optval, ':');
950 if (!optval2) {
951 fprintf(stderr, "uct: treepool_chance takes two comma-separated numbers\n");
952 exit(1);
954 u->treepool_chance[0] = atoi(optval);
955 optval = ++optval2;
956 u->treepool_chance[1] = atoi(optval);
957 } else if (!strcasecmp(optname, "treepool_size") && optval) {
958 /* Number of top significant children
959 * to pick from. Too low means low effect,
960 * too high means even lousy moves are
961 * played. */
962 u->treepool_size = atoi(optval);
963 } else if (!strcasecmp(optname, "treepool_type") && optval) {
964 /* How to sort the children. */
965 if (!strcasecmp(optval, "rave_playouts"))
966 u->treepool_type = UTT_RAVE_PLAYOUTS;
967 else if (!strcasecmp(optval, "rave_value"))
968 u->treepool_type = UTT_RAVE_VALUE;
969 else if (!strcasecmp(optval, "uct_playouts"))
970 u->treepool_type = UTT_UCT_PLAYOUTS;
971 else if (!strcasecmp(optval, "uct_value"))
972 u->treepool_type = UTT_UCT_VALUE;
973 else if (!strcasecmp(optval, "evaluate"))
974 /* The proper combination of RAVE,
975 * UCT and prior, as used through
976 * descent. */
977 u->treepool_type = UTT_EVALUATE;
978 else {
979 fprintf(stderr, "uct: unknown treepool_type %s\n", optval);
980 exit(1);
982 } else if (!strcasecmp(optname, "treepool_pickfactor") && optval) {
983 /* Pick factor influencing children choice.
984 * By default (if this is 0 or 10), coords
985 * have uniform probability to be chosen;
986 * otherwise, children are tried from best
987 * to worst, each picked with probability
988 * (1/n * pickfactor/10).
989 * I.e., better children may be preferred
990 * if pickfactor > 10. */
991 u->treepool_pickfactor = atoi(optval);
993 /** Distributed engine slaves setup */
995 } else if (!strcasecmp(optname, "slave")) {
996 /* Act as slave for the distributed engine. */
997 u->slave = !optval || atoi(optval);
998 } else if (!strcasecmp(optname, "shared_nodes") && optval) {
999 /* Share at most shared_nodes between master and slave at each genmoves.
1000 * Must use the same value in master and slaves. */
1001 u->shared_nodes = atoi(optval);
1002 } else if (!strcasecmp(optname, "shared_levels") && optval) {
1003 /* Share only nodes of level <= shared_levels. */
1004 u->shared_levels = atoi(optval);
1005 } else if (!strcasecmp(optname, "stats_hbits") && optval) {
1006 /* Set hash table size to 2^stats_hbits for the shared stats. */
1007 u->stats_hbits = atoi(optval);
1009 } else {
1010 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
1011 exit(1);
1016 if (!u->policy)
1017 u->policy = policy_ucb1amaf_init(u, NULL);
1019 if (!!u->random_policy_chance ^ !!u->random_policy) {
1020 fprintf(stderr, "uct: Only one of random_policy and random_policy_chance is set\n");
1021 exit(1);
1024 if (!u->local_tree) {
1025 /* No ltree aging. */
1026 u->local_tree_aging = 1.0f;
1028 if (!using_elo)
1029 u->local_tree_playout = false;
1031 if (u->fast_alloc) {
1032 if (u->pruning_threshold < u->max_tree_size / 10)
1033 u->pruning_threshold = u->max_tree_size / 10;
1034 if (u->pruning_threshold > u->max_tree_size / 2)
1035 u->pruning_threshold = u->max_tree_size / 2;
1037 /* Limit pruning temp space to 20% of memory. Beyond this we discard
1038 * the nodes and recompute them at the next move if necessary. */
1039 u->max_pruned_size = u->max_tree_size / 5;
1040 u->max_tree_size -= u->max_pruned_size;
1041 } else {
1042 /* Reserve 5% memory in case the background free() are slower
1043 * than the concurrent allocations. */
1044 u->max_tree_size -= u->max_tree_size / 20;
1047 if (!u->prior)
1048 u->prior = uct_prior_init(NULL, b);
1050 if (!u->playout)
1051 u->playout = playout_moggy_init(NULL, b, u->jdict);
1052 if (!u->playout->debug_level)
1053 u->playout->debug_level = u->debug_level;
1055 u->ownermap.map = malloc2(board_size2(b) * sizeof(u->ownermap.map[0]));
1057 if (u->slave) {
1058 if (!u->stats_hbits) u->stats_hbits = DEFAULT_STATS_HBITS;
1059 if (!u->shared_nodes) u->shared_nodes = DEFAULT_SHARED_NODES;
1060 assert(u->shared_levels * board_bits2(b) <= 8 * (int)sizeof(path_t));
1063 if (!u->dynkomi)
1064 u->dynkomi = uct_dynkomi_init_adaptive(u, NULL, b);
1066 /* Some things remain uninitialized for now - the opening tbook
1067 * is not loaded and the tree not set up. */
1068 /* This will be initialized in setup_state() at the first move
1069 * received/requested. This is because right now we are not aware
1070 * about any komi or handicap setup and such. */
1072 return u;
1075 struct engine *
1076 engine_uct_init(char *arg, struct board *b)
1078 struct uct *u = uct_state_init(arg, b);
1079 struct engine *e = calloc2(1, sizeof(struct engine));
1080 e->name = "UCT Engine";
1081 e->printhook = uct_printhook_ownermap;
1082 e->notify_play = uct_notify_play;
1083 e->chat = uct_chat;
1084 e->result = uct_result;
1085 e->genmove = uct_genmove;
1086 e->genmoves = uct_genmoves;
1087 e->dead_group_list = uct_dead_group_list;
1088 e->done = uct_done;
1089 e->data = u;
1090 if (u->slave)
1091 e->notify = uct_notify;
1093 const char banner[] = "I'm playing UCT. When I'm losing, I will resign, "
1094 "if I think I win, I play until you pass. "
1095 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1096 if (!u->banner) u->banner = "";
1097 e->comment = malloc2(sizeof(banner) + strlen(u->banner) + 1);
1098 sprintf(e->comment, "%s %s", banner, u->banner);
1100 return e;