Distributed engine: the master reads and writes binary arguments
[pachi/nmclean.git] / uct / uct.c
blob174325666e0d95a479ea2cdced3cef448fb15601
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 "playout.h"
16 #include "playout/elo.h"
17 #include "playout/moggy.h"
18 #include "playout/light.h"
19 #include "tactics.h"
20 #include "timeinfo.h"
21 #include "uct/dynkomi.h"
22 #include "uct/internal.h"
23 #include "uct/prior.h"
24 #include "uct/search.h"
25 #include "uct/slave.h"
26 #include "uct/tree.h"
27 #include "uct/uct.h"
28 #include "uct/walk.h"
30 struct uct_policy *policy_ucb1_init(struct uct *u, char *arg);
31 struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg);
32 static void uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color);
34 /* Maximal simulation length. */
35 #define MC_GAMELEN MAX_GAMELEN
38 static void
39 setup_state(struct uct *u, struct board *b, enum stone color)
41 u->t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0,
42 u->local_tree_aging, u->stats_hbits);
43 if (u->force_seed)
44 fast_srandom(u->force_seed);
45 if (UDEBUGL(0))
46 fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed());
47 //board_print(b, stderr);
48 if (!u->no_book && b->moves == 0) {
49 assert(color == S_BLACK);
50 tree_load(u->t, b);
54 static void
55 reset_state(struct uct *u)
57 assert(u->t);
58 tree_done(u->t); u->t = NULL;
61 static void
62 setup_dynkomi(struct uct *u, struct board *b, enum stone to_play)
64 if (u->t->use_extra_komi && !u->pondering && u->dynkomi->permove)
65 u->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, u->t);
68 void
69 uct_prepare_move(struct uct *u, struct board *b, enum stone color)
71 if (u->t) {
72 /* Verify that we have sane state. */
73 assert(b->es == u);
74 assert(u->t && b->moves);
75 if (color != stone_other(u->t->root_color)) {
76 fprintf(stderr, "Fatal: Non-alternating play detected %d %d\n",
77 color, u->t->root_color);
78 exit(1);
80 uct_htable_reset(u->t);
82 } else {
83 /* We need fresh state. */
84 b->es = u;
85 setup_state(u, b, color);
88 u->ownermap.playouts = 0;
89 memset(u->ownermap.map, 0, board_size2(b) * sizeof(u->ownermap.map[0]));
90 u->played_own = u->played_all = 0;
93 static void
94 dead_group_list(struct uct *u, struct board *b, struct move_queue *mq)
96 struct group_judgement gj;
97 gj.thres = GJ_THRES;
98 gj.gs = alloca(board_size2(b) * sizeof(gj.gs[0]));
99 board_ownermap_judge_group(b, &u->ownermap, &gj);
100 groups_of_status(b, &gj, GS_DEAD, mq);
103 bool
104 uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive)
106 if (u->ownermap.playouts < GJ_MINGAMES)
107 return false;
109 struct move_queue mq = { .moves = 0 };
110 dead_group_list(u, b, &mq);
111 if (pass_all_alive && mq.moves > 0)
112 return false; // We need to remove some dead groups first.
113 return pass_is_safe(b, color, &mq);
116 static char *
117 uct_printhook_ownermap(struct board *board, coord_t c, char *s, char *end)
119 struct uct *u = board->es;
120 assert(u);
121 const char chr[] = ":XO,"; // dame, black, white, unclear
122 const char chm[] = ":xo,";
123 char ch = chr[board_ownermap_judge_point(&u->ownermap, c, GJ_THRES)];
124 if (ch == ',') { // less precise estimate then?
125 ch = chm[board_ownermap_judge_point(&u->ownermap, c, 0.67)];
127 s += snprintf(s, end - s, "%c ", ch);
128 return s;
131 static char *
132 uct_notify_play(struct engine *e, struct board *b, struct move *m)
134 struct uct *u = e->data;
135 if (!u->t) {
136 /* No state, create one - this is probably game beginning
137 * and we need to load the opening book right now. */
138 uct_prepare_move(u, b, m->color);
139 assert(u->t);
142 /* Stop pondering, required by tree_promote_at() */
143 uct_pondering_stop(u);
144 if (UDEBUGL(2) && u->slave)
145 tree_dump(u->t, u->dumpthres);
147 if (is_resign(m->coord)) {
148 /* Reset state. */
149 reset_state(u);
150 return NULL;
153 /* Promote node of the appropriate move to the tree root. */
154 assert(u->t->root);
155 if (!tree_promote_at(u->t, b, m->coord)) {
156 if (UDEBUGL(0))
157 fprintf(stderr, "Warning: Cannot promote move node! Several play commands in row?\n");
158 reset_state(u);
159 return NULL;
162 /* If we are a slave in a distributed engine, start pondering once
163 * we know which move we actually played. See uct_genmove() about
164 * the check for pass. */
165 if (u->pondering_opt && u->slave && m->color == u->my_color && !is_pass(m->coord))
166 uct_pondering_start(u, b, u->t, stone_other(m->color));
168 return NULL;
171 static char *
172 uct_chat(struct engine *e, struct board *b, char *cmd)
174 struct uct *u = e->data;
175 static char reply[1024];
177 cmd += strspn(cmd, " \n\t");
178 if (!strncasecmp(cmd, "winrate", 7)) {
179 if (!u->t)
180 return "no game context (yet?)";
181 enum stone color = u->t->root_color;
182 struct tree_node *n = u->t->root;
183 snprintf(reply, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
184 n->u.playouts, u->threads, stone2str(color), coord2sstr(n->coord, b),
185 tree_node_get_value(u->t, -1, n->u.value) * 100);
186 if (u->t->use_extra_komi && abs(u->t->extra_komi) >= 0.5) {
187 sprintf(reply + strlen(reply), ", while self-imposing extra komi %.1f",
188 u->t->extra_komi);
190 strcat(reply, ".");
191 return reply;
193 return NULL;
196 static void
197 uct_dead_group_list(struct engine *e, struct board *b, struct move_queue *mq)
199 struct uct *u = e->data;
201 /* This means the game is probably over, no use pondering on. */
202 uct_pondering_stop(u);
204 if (u->pass_all_alive)
205 return; // no dead groups
207 bool mock_state = false;
209 if (!u->t) {
210 /* No state, but we cannot just back out - we might
211 * have passed earlier, only assuming some stones are
212 * dead, and then re-connected, only to lose counting
213 * when all stones are assumed alive. */
214 uct_prepare_move(u, b, S_BLACK); assert(u->t);
215 mock_state = true;
217 /* Make sure the ownermap is well-seeded. */
218 while (u->ownermap.playouts < GJ_MINGAMES)
219 uct_playout(u, b, S_BLACK, u->t);
220 /* Show the ownermap: */
221 if (DEBUGL(2))
222 board_print_custom(b, stderr, uct_printhook_ownermap);
224 dead_group_list(u, b, mq);
226 if (mock_state) {
227 /* Clean up the mock state in case we will receive
228 * a genmove; we could get a non-alternating-move
229 * error from uct_prepare_move() in that case otherwise. */
230 reset_state(u);
234 static void
235 playout_policy_done(struct playout_policy *p)
237 if (p->done) p->done(p);
238 if (p->data) free(p->data);
239 free(p);
242 static void
243 uct_done(struct engine *e)
245 /* This is called on engine reset, especially when clear_board
246 * is received and new game should begin. */
247 struct uct *u = e->data;
248 uct_pondering_stop(u);
249 if (u->t) reset_state(u);
250 free(u->ownermap.map);
252 free(u->policy);
253 free(u->random_policy);
254 playout_policy_done(u->playout);
255 uct_prior_done(u->prior);
260 /* Run time-limited MCTS search on foreground. */
261 static int
262 uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t)
264 struct uct_search_state s;
265 uct_search_start(u, b, color, t, ti, &s);
266 if (UDEBUGL(2) && s.base_playouts > 0)
267 fprintf(stderr, "<pre-simulated %d games>\n", s.base_playouts);
269 /* The search tree is ctx->t. This is normally == t, but in case of
270 * TM_ROOT, it is one of the trees belonging to the independent
271 * workers. It is important to reference ctx->t directly since the
272 * thread manager will swap the tree pointer asynchronously. */
273 /* XXX: This means TM_ROOT support is suboptimal since single stalled
274 * thread can stall the others in case of limiting the search by game
275 * count. However, TM_ROOT just does not deserve any more extra code
276 * right now. */
278 /* Now, just periodically poll the search tree. */
279 while (1) {
280 time_sleep(TREE_BUSYWAIT_INTERVAL);
281 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
282 * time control is broken. But if it happens to be less, we still search
283 * at least 100ms otherwise the move is completely random. */
285 int i = uct_search_games(&s);
286 /* Print notifications etc. */
287 uct_search_progress(u, b, color, t, ti, &s, i);
288 /* Check if we should stop the search. */
289 if (uct_search_check_stop(u, b, color, t, ti, &s, i))
290 break;
293 struct uct_thread_ctx *ctx = uct_search_stop();
294 if (UDEBUGL(2)) tree_dump(t, u->dumpthres);
295 if (UDEBUGL(2))
296 fprintf(stderr, "(avg score %f/%d value %f/%d)\n",
297 u->dynkomi->score.value, u->dynkomi->score.playouts,
298 u->dynkomi->value.value, u->dynkomi->value.playouts);
299 if (UDEBUGL(0))
300 uct_progress_status(u, t, color, ctx->games);
302 u->played_own += ctx->games;
303 return ctx->games;
306 /* Start pondering background with @color to play. */
307 static void
308 uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color)
310 if (UDEBUGL(1))
311 fprintf(stderr, "Starting to ponder with color %s\n", stone2str(stone_other(color)));
312 u->pondering = true;
314 /* We need a local board copy to ponder upon. */
315 struct board *b = malloc2(sizeof(*b)); board_copy(b, b0);
317 /* *b0 did not have the genmove'd move played yet. */
318 struct move m = { t->root->coord, t->root_color };
319 int res = board_play(b, &m);
320 assert(res >= 0);
321 setup_dynkomi(u, b, stone_other(m.color));
323 /* Start MCTS manager thread "headless". */
324 static struct uct_search_state s;
325 uct_search_start(u, b, color, t, NULL, &s);
328 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
329 * to stop the background search for a slave in the distributed engine. */
330 void
331 uct_pondering_stop(struct uct *u)
333 if (!thread_manager_running)
334 return;
336 /* Stop the thread manager. */
337 struct uct_thread_ctx *ctx = uct_search_stop();
338 if (UDEBUGL(1)) {
339 if (u->pondering) fprintf(stderr, "(pondering) ");
340 uct_progress_status(u, ctx->t, ctx->color, ctx->games);
342 if (u->pondering) {
343 free(ctx->b);
344 u->pondering = false;
349 void
350 uct_genmove_setup(struct uct *u, struct board *b, enum stone color)
352 if (b->superko_violation) {
353 fprintf(stderr, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
354 fprintf(stderr, "Maybe you play with situational instead of positional superko?\n");
355 fprintf(stderr, "I'm going to ignore the violation, but note that I may miss\n");
356 fprintf(stderr, "some moves valid under this ruleset because of this.\n");
357 b->superko_violation = false;
360 uct_prepare_move(u, b, color);
362 assert(u->t);
363 u->my_color = color;
365 /* How to decide whether to use dynkomi in this game? Since we use
366 * pondering, it's not simple "who-to-play" matter. Decide based on
367 * the last genmove issued. */
368 u->t->use_extra_komi = !!(u->dynkomi_mask & color);
369 setup_dynkomi(u, b, color);
371 if (b->rules == RULES_JAPANESE)
372 u->territory_scoring = true;
374 /* Make pessimistic assumption about komi for Japanese rules to
375 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
376 * The rules usually give the same winner if the integer part of komi
377 * is odd so we adjust the komi only if it is even (for a board of
378 * odd size). We are not trying to get an exact evaluation for rare
379 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
380 if (u->territory_scoring && (((int)floor(b->komi) + board_size(b)) & 1)) {
381 b->komi += (color == S_BLACK ? 1.0 : -1.0);
382 if (UDEBUGL(0))
383 fprintf(stderr, "Setting komi to %.1f assuming Japanese rules\n",
384 b->komi);
388 static coord_t *
389 uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
391 double start_time = time_now();
392 struct uct *u = e->data;
393 uct_pondering_stop(u);
394 uct_genmove_setup(u, b, color);
396 /* Start the Monte Carlo Tree Search! */
397 int base_playouts = u->t->root->u.playouts;
398 int played_games = uct_search(u, b, ti, color, u->t);
400 coord_t best_coord;
401 struct tree_node *best;
402 best = uct_search_result(u, b, color, pass_all_alive, played_games, base_playouts, &best_coord);
404 if (UDEBUGL(2)) {
405 double time = time_now() - start_time + 0.000001; /* avoid divide by zero */
406 fprintf(stderr, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
407 time, (int)(played_games/time), (int)(played_games/time/u->threads));
410 if (!best) {
411 /* Pass or resign. */
412 reset_state(u);
413 return coord_copy(best_coord);
415 tree_promote_node(u->t, &best);
417 /* After a pass, pondering is harmful for two reasons:
418 * (i) We might keep pondering even when the game is over.
419 * Of course this is the case for opponent resign as well.
420 * (ii) More importantly, the ownermap will get skewed since
421 * the UCT will start cutting off any playouts. */
422 if (u->pondering_opt && !is_pass(best->coord)) {
423 uct_pondering_start(u, b, u->t, stone_other(color));
425 return coord_copy(best_coord);
429 bool
430 uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color)
432 struct uct *u = e->data;
433 if (!u->t) uct_prepare_move(u, b, color);
434 assert(u->t);
436 if (ti->dim == TD_GAMES) {
437 /* Don't count in games that already went into the book. */
438 ti->len.games += u->t->root->u.playouts;
440 uct_search(u, b, ti, color, u->t);
442 assert(ti->dim == TD_GAMES);
443 tree_save(u->t, b, ti->len.games / 100);
445 return true;
448 void
449 uct_dumpbook(struct engine *e, struct board *b, enum stone color)
451 struct uct *u = e->data;
452 struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0, u->local_tree_aging, 0);
453 tree_load(t, b);
454 tree_dump(t, 0);
455 tree_done(t);
459 struct uct *
460 uct_state_init(char *arg, struct board *b)
462 struct uct *u = calloc2(1, sizeof(struct uct));
463 bool using_elo = false;
465 u->debug_level = debug_level;
466 u->gamelen = MC_GAMELEN;
467 u->mercymin = 0;
468 u->expand_p = 2;
469 u->dumpthres = 1000;
470 u->playout_amaf = true;
471 u->playout_amaf_nakade = false;
472 u->amaf_prior = false;
473 u->max_tree_size = 3072ULL * 1048576;
475 u->dynkomi_mask = S_BLACK;
477 u->threads = 1;
478 u->thread_model = TM_TREEVL;
479 u->parallel_tree = true;
480 u->virtual_loss = true;
482 u->fuseki_end = 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
483 u->yose_start = 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
484 u->bestr_ratio = 0.02;
485 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
486 // TODO: Further tuning and experiments with better time allocation schemes.
487 u->best2_ratio = 2.5;
489 u->val_scale = 0.04; u->val_points = 40;
490 /* TODO: Adjust this by number of simulations - it's more important how
491 * many times per move we do the adjustment. */
492 u->dynkomi_interval = 500;
494 u->tenuki_d = 4;
495 u->local_tree_aging = 2;
497 if (arg) {
498 char *optspec, *next = arg;
499 while (*next) {
500 optspec = next;
501 next += strcspn(next, ",");
502 if (*next) { *next++ = 0; } else { *next = 0; }
504 char *optname = optspec;
505 char *optval = strchr(optspec, '=');
506 if (optval) *optval++ = 0;
508 if (!strcasecmp(optname, "debug")) {
509 if (optval)
510 u->debug_level = atoi(optval);
511 else
512 u->debug_level++;
513 } else if (!strcasecmp(optname, "mercy") && optval) {
514 /* Minimal difference of black/white captures
515 * to stop playout - "Mercy Rule". Speeds up
516 * hopeless playouts at the expense of some
517 * accuracy. */
518 u->mercymin = atoi(optval);
519 } else if (!strcasecmp(optname, "gamelen") && optval) {
520 u->gamelen = atoi(optval);
521 } else if (!strcasecmp(optname, "expand_p") && optval) {
522 u->expand_p = atoi(optval);
523 } else if (!strcasecmp(optname, "dumpthres") && optval) {
524 u->dumpthres = atoi(optval);
525 } else if (!strcasecmp(optname, "best2_ratio") && optval) {
526 /* If set, prolong simulating while
527 * first_best/second_best playouts ratio
528 * is less than best2_ratio. */
529 u->best2_ratio = atof(optval);
530 } else if (!strcasecmp(optname, "bestr_ratio") && optval) {
531 /* If set, prolong simulating while
532 * best,best_best_child values delta
533 * is more than bestr_ratio. */
534 u->bestr_ratio = atof(optval);
535 } else if (!strcasecmp(optname, "playout_amaf")) {
536 /* Whether to include random playout moves in
537 * AMAF as well. (Otherwise, only tree moves
538 * are included in AMAF. Of course makes sense
539 * only in connection with an AMAF policy.) */
540 /* with-without: 55.5% (+-4.1) */
541 if (optval && *optval == '0')
542 u->playout_amaf = false;
543 else
544 u->playout_amaf = true;
545 } else if (!strcasecmp(optname, "playout_amaf_nakade")) {
546 /* Whether to include nakade moves from playouts
547 * in the AMAF statistics; this tends to nullify
548 * the playout_amaf effect by adding too much
549 * noise. */
550 if (optval && *optval == '0')
551 u->playout_amaf_nakade = false;
552 else
553 u->playout_amaf_nakade = true;
554 } else if (!strcasecmp(optname, "playout_amaf_cutoff") && optval) {
555 /* Keep only first N% of playout stage AMAF
556 * information. */
557 u->playout_amaf_cutoff = atoi(optval);
558 } else if ((!strcasecmp(optname, "policy") || !strcasecmp(optname, "random_policy")) && optval) {
559 char *policyarg = strchr(optval, ':');
560 struct uct_policy **p = !strcasecmp(optname, "policy") ? &u->policy : &u->random_policy;
561 if (policyarg)
562 *policyarg++ = 0;
563 if (!strcasecmp(optval, "ucb1")) {
564 *p = policy_ucb1_init(u, policyarg);
565 } else if (!strcasecmp(optval, "ucb1amaf")) {
566 *p = policy_ucb1amaf_init(u, policyarg);
567 } else {
568 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
569 exit(1);
571 } else if (!strcasecmp(optname, "playout") && optval) {
572 char *playoutarg = strchr(optval, ':');
573 if (playoutarg)
574 *playoutarg++ = 0;
575 if (!strcasecmp(optval, "moggy")) {
576 u->playout = playout_moggy_init(playoutarg, b);
577 } else if (!strcasecmp(optval, "light")) {
578 u->playout = playout_light_init(playoutarg, b);
579 } else if (!strcasecmp(optval, "elo")) {
580 u->playout = playout_elo_init(playoutarg, b);
581 using_elo = true;
582 } else {
583 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
584 exit(1);
586 } else if (!strcasecmp(optname, "prior") && optval) {
587 u->prior = uct_prior_init(optval, b);
588 } else if (!strcasecmp(optname, "amaf_prior") && optval) {
589 u->amaf_prior = atoi(optval);
590 } else if (!strcasecmp(optname, "threads") && optval) {
591 /* By default, Pachi will run with only single
592 * tree search thread! */
593 u->threads = atoi(optval);
594 } else if (!strcasecmp(optname, "thread_model") && optval) {
595 if (!strcasecmp(optval, "root")) {
596 /* Root parallelization - each thread
597 * does independent search, trees are
598 * merged at the end. */
599 u->thread_model = TM_ROOT;
600 u->parallel_tree = false;
601 u->virtual_loss = false;
602 } else if (!strcasecmp(optval, "tree")) {
603 /* Tree parallelization - all threads
604 * grind on the same tree. */
605 u->thread_model = TM_TREE;
606 u->parallel_tree = true;
607 u->virtual_loss = false;
608 } else if (!strcasecmp(optval, "treevl")) {
609 /* Tree parallelization, but also
610 * with virtual losses - this discou-
611 * rages most threads choosing the
612 * same tree branches to read. */
613 u->thread_model = TM_TREEVL;
614 u->parallel_tree = true;
615 u->virtual_loss = true;
616 } else {
617 fprintf(stderr, "UCT: Invalid thread model %s\n", optval);
618 exit(1);
620 } else if (!strcasecmp(optname, "pondering")) {
621 /* Keep searching even during opponent's turn. */
622 u->pondering_opt = !optval || atoi(optval);
623 } else if (!strcasecmp(optname, "fuseki_end") && optval) {
624 /* At the very beginning it's not worth thinking
625 * too long because the playout evaluations are
626 * very noisy. So gradually increase the thinking
627 * time up to maximum when fuseki_end percent
628 * of the board has been played.
629 * This only applies if we are not in byoyomi. */
630 u->fuseki_end = atoi(optval);
631 } else if (!strcasecmp(optname, "yose_start") && optval) {
632 /* When yose_start percent of the board has been
633 * played, or if we are in byoyomi, stop spending
634 * more time and spread the remaining time
635 * uniformly.
636 * Between fuseki_end and yose_start, we spend
637 * a constant proportion of the remaining time
638 * on each move. (yose_start should actually
639 * be much earlier than when real yose start,
640 * but "yose" is a good short name to convey
641 * the idea.) */
642 u->yose_start = atoi(optval);
643 } else if (!strcasecmp(optname, "force_seed") && optval) {
644 u->force_seed = atoi(optval);
645 } else if (!strcasecmp(optname, "no_book")) {
646 u->no_book = true;
647 } else if (!strcasecmp(optname, "dynkomi") && optval) {
648 /* Dynamic komi approach; there are multiple
649 * ways to adjust komi dynamically throughout
650 * play. We currently support two: */
651 char *dynkomiarg = strchr(optval, ':');
652 if (dynkomiarg)
653 *dynkomiarg++ = 0;
654 if (!strcasecmp(optval, "none")) {
655 u->dynkomi = uct_dynkomi_init_none(u, dynkomiarg, b);
656 } else if (!strcasecmp(optval, "linear")) {
657 u->dynkomi = uct_dynkomi_init_linear(u, dynkomiarg, b);
658 } else if (!strcasecmp(optval, "adaptive")) {
659 u->dynkomi = uct_dynkomi_init_adaptive(u, dynkomiarg, b);
660 } else {
661 fprintf(stderr, "UCT: Invalid dynkomi mode %s\n", optval);
662 exit(1);
664 } else if (!strcasecmp(optname, "dynkomi_mask") && optval) {
665 /* Bitmask of colors the player must be
666 * for dynkomi be applied; you may want
667 * to use dynkomi_mask=3 to allow dynkomi
668 * even in games where Pachi is white. */
669 u->dynkomi_mask = atoi(optval);
670 } else if (!strcasecmp(optname, "dynkomi_interval") && optval) {
671 /* If non-zero, re-adjust dynamic komi
672 * throughout a single genmove reading,
673 * roughly every N simulations. */
674 /* XXX: Does not work with tree
675 * parallelization. */
676 u->dynkomi_interval = atoi(optval);
677 } else if (!strcasecmp(optname, "val_scale") && optval) {
678 /* How much of the game result value should be
679 * influenced by win size. Zero means it isn't. */
680 u->val_scale = atof(optval);
681 } else if (!strcasecmp(optname, "val_points") && optval) {
682 /* Maximum size of win to be scaled into game
683 * result value. Zero means boardsize^2. */
684 u->val_points = atoi(optval) * 2; // result values are doubled
685 } else if (!strcasecmp(optname, "val_extra")) {
686 /* If false, the score coefficient will be simply
687 * added to the value, instead of scaling the result
688 * coefficient because of it. */
689 u->val_extra = !optval || atoi(optval);
690 } else if (!strcasecmp(optname, "local_tree") && optval) {
691 /* Whether to bias exploration by local tree values
692 * (must be supported by the used policy).
693 * 0: Don't.
694 * 1: Do, value = result.
695 * Try to temper the result:
696 * 2: Do, value = 0.5+(result-expected)/2.
697 * 3: Do, value = 0.5+bzz((result-expected)^2).
698 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
699 u->local_tree = atoi(optval);
700 } else if (!strcasecmp(optname, "tenuki_d") && optval) {
701 /* Tenuki distance at which to break the local tree. */
702 u->tenuki_d = atoi(optval);
703 if (u->tenuki_d > TREE_NODE_D_MAX + 1) {
704 fprintf(stderr, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX + 1);
705 exit(1);
707 } else if (!strcasecmp(optname, "local_tree_aging") && optval) {
708 /* How much to reduce local tree values between moves. */
709 u->local_tree_aging = atof(optval);
710 } else if (!strcasecmp(optname, "local_tree_allseq")) {
711 /* By default, only complete sequences are stored
712 * in the local tree. If this is on, also
713 * subsequences starting at each move are stored. */
714 u->local_tree_allseq = !optval || atoi(optval);
715 } else if (!strcasecmp(optname, "local_tree_playout")) {
716 /* Whether to adjust ELO playout probability
717 * distributions according to matched localtree
718 * information. */
719 u->local_tree_playout = !optval || atoi(optval);
720 } else if (!strcasecmp(optname, "local_tree_pseqroot")) {
721 /* By default, when we have no sequence move
722 * to suggest in-playout, we give up. If this
723 * is on, we make probability distribution from
724 * sequences first moves instead. */
725 u->local_tree_pseqroot = !optval || atoi(optval);
726 } else if (!strcasecmp(optname, "pass_all_alive")) {
727 /* Whether to consider passing only after all
728 * dead groups were removed from the board;
729 * this is like all genmoves are in fact
730 * kgs-genmove_cleanup. */
731 u->pass_all_alive = !optval || atoi(optval);
732 } else if (!strcasecmp(optname, "territory_scoring")) {
733 /* Use territory scoring (default is area scoring).
734 * An explicit kgs-rules command overrides this. */
735 u->territory_scoring = !optval || atoi(optval);
736 } else if (!strcasecmp(optname, "random_policy_chance") && optval) {
737 /* If specified (N), with probability 1/N, random_policy policy
738 * descend is used instead of main policy descend; useful
739 * if specified policy (e.g. UCB1AMAF) can make unduly biased
740 * choices sometimes, you can fall back to e.g.
741 * random_policy=UCB1. */
742 u->random_policy_chance = atoi(optval);
743 } else if (!strcasecmp(optname, "max_tree_size") && optval) {
744 /* Maximum amount of memory [MiB] consumed by the move tree.
745 * For fast_alloc it includes the temp tree used for pruning.
746 * Default is 3072 (3 GiB). Note that if you use TM_ROOT,
747 * this limits size of only one of the trees, not all of them
748 * together. */
749 u->max_tree_size = atol(optval) * 1048576;
750 } else if (!strcasecmp(optname, "fast_alloc")) {
751 u->fast_alloc = !optval || atoi(optval);
752 } else if (!strcasecmp(optname, "slave")) {
753 /* Act as slave for the distributed engine. */
754 u->slave = !optval || atoi(optval);
755 } else if (!strcasecmp(optname, "stats_hbits") && optval) {
756 /* Set hash table size to 2^stats_hbits for the shared stats. */
757 u->stats_hbits = atoi(optval);
758 } else if (!strcasecmp(optname, "banner") && optval) {
759 /* Additional banner string. This must come as the
760 * last engine parameter. */
761 if (*next) *--next = ',';
762 u->banner = strdup(optval);
763 break;
764 } else {
765 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
766 exit(1);
771 u->resign_ratio = 0.2; /* Resign when most games are lost. */
772 u->loss_threshold = 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
773 if (!u->policy)
774 u->policy = policy_ucb1amaf_init(u, NULL);
776 if (!!u->random_policy_chance ^ !!u->random_policy) {
777 fprintf(stderr, "uct: Only one of random_policy and random_policy_chance is set\n");
778 exit(1);
781 if (!u->local_tree) {
782 /* No ltree aging. */
783 u->local_tree_aging = 1.0f;
785 if (!using_elo)
786 u->local_tree_playout = false;
788 if (u->fast_alloc && !u->parallel_tree) {
789 fprintf(stderr, "fast_alloc not supported with root parallelization.\n");
790 exit(1);
792 if (u->slave && !u->parallel_tree) {
793 /* node->pu used by slave. */
794 fprintf(stderr, "slave not supported with root parallelization.\n");
795 exit(1);
797 if (u->fast_alloc)
798 u->max_tree_size = (100ULL * u->max_tree_size) / (100 + MIN_FREE_MEM_PERCENT);
800 if (!u->prior)
801 u->prior = uct_prior_init(NULL, b);
803 if (!u->playout)
804 u->playout = playout_moggy_init(NULL, b);
805 u->playout->debug_level = u->debug_level;
807 u->ownermap.map = malloc2(board_size2(b) * sizeof(u->ownermap.map[0]));
809 if (u->slave) {
810 if (!u->stats_hbits) u->stats_hbits = DEFAULT_STATS_HBITS;
813 if (!u->dynkomi)
814 u->dynkomi = uct_dynkomi_init_linear(u, NULL, b);
816 /* Some things remain uninitialized for now - the opening book
817 * is not loaded and the tree not set up. */
818 /* This will be initialized in setup_state() at the first move
819 * received/requested. This is because right now we are not aware
820 * about any komi or handicap setup and such. */
822 return u;
825 struct engine *
826 engine_uct_init(char *arg, struct board *b)
828 struct uct *u = uct_state_init(arg, b);
829 struct engine *e = calloc2(1, sizeof(struct engine));
830 e->name = "UCT Engine";
831 e->printhook = uct_printhook_ownermap;
832 e->notify_play = uct_notify_play;
833 e->chat = uct_chat;
834 e->genmove = uct_genmove;
835 e->genmoves = uct_genmoves;
836 e->dead_group_list = uct_dead_group_list;
837 e->done = uct_done;
838 e->data = u;
839 if (u->slave)
840 e->notify = uct_notify;
842 const char banner[] = "I'm playing UCT. When I'm losing, I will resign, "
843 "if I think I win, I play until you pass. "
844 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
845 if (!u->banner) u->banner = "";
846 e->comment = malloc2(sizeof(banner) + strlen(u->banner) + 1);
847 sprintf(e->comment, "%s %s", banner, u->banner);
849 return e;