Distributed engine: support the kgs-rules command.
[pachi.git] / uct / uct.c
blobe8febefe0a3620bf7e349fcf3020f34f4fcf99d2
1 #include <assert.h>
2 #include <math.h>
3 #include <pthread.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <time.h>
10 #define DEBUG
12 #include "debug.h"
13 #include "board.h"
14 #include "gtp.h"
15 #include "move.h"
16 #include "mq.h"
17 #include "playout.h"
18 #include "playout/elo.h"
19 #include "playout/moggy.h"
20 #include "playout/light.h"
21 #include "random.h"
22 #include "tactics.h"
23 #include "timeinfo.h"
24 #include "distributed/distributed.h"
25 #include "uct/dynkomi.h"
26 #include "uct/internal.h"
27 #include "uct/prior.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_stop(struct uct *u);
35 static void uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color);
37 /* Default number of simulations to perform per move.
38 * Note that this is now in total over all threads! (Unless TM_ROOT.) */
39 #define MC_GAMES 80000
40 #define MC_GAMELEN MAX_GAMELEN
41 static const struct time_info default_ti = {
42 .period = TT_MOVE,
43 .dim = TD_GAMES,
44 .len = { .games = MC_GAMES },
47 /* How big proportion of ownermap counts must be of one color to consider
48 * the point sure. */
49 #define GJ_THRES 0.8
50 /* How many games to consider at minimum before judging groups. */
51 #define GJ_MINGAMES 500
53 /* How often to inspect the tree from the main thread to check for playout
54 * stop, progress reports, etc. (in seconds) */
55 #define TREE_BUSYWAIT_INTERVAL 0.1 /* 100ms */
57 /* Once per how many simulations (per thread) to show a progress report line. */
58 #define TREE_SIMPROGRESS_INTERVAL 10000
60 /* How often to send stats updates for the distributed engine (in seconds). */
61 #define STATS_SEND_INTERVAL 0.5
63 /* When terminating uct_search() early, the safety margin to add to the
64 * remaining playout number estimate when deciding whether the result can
65 * still change. */
66 #define PLAYOUT_DELTA_SAFEMARGIN 1000
69 static void
70 setup_state(struct uct *u, struct board *b, enum stone color)
72 u->t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0, u->local_tree_aging);
73 if (u->force_seed)
74 fast_srandom(u->force_seed);
75 if (UDEBUGL(0))
76 fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed());
77 //board_print(b, stderr);
78 if (!u->no_book && b->moves == 0) {
79 assert(color == S_BLACK);
80 tree_load(u->t, b);
84 static void
85 reset_state(struct uct *u)
87 assert(u->t);
88 tree_done(u->t); u->t = NULL;
91 static void
92 setup_dynkomi(struct uct *u, struct board *b, enum stone to_play)
94 if (u->t->use_extra_komi && u->dynkomi->permove)
95 u->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, u->t);
98 static void
99 prepare_move(struct engine *e, struct board *b, enum stone color)
101 struct uct *u = e->data;
103 if (u->t) {
104 /* Verify that we have sane state. */
105 assert(b->es == u);
106 assert(u->t && b->moves);
107 if (color != stone_other(u->t->root_color)) {
108 fprintf(stderr, "Fatal: Non-alternating play detected %d %d\n",
109 color, u->t->root_color);
110 exit(1);
113 } else {
114 /* We need fresh state. */
115 b->es = u;
116 setup_state(u, b, color);
119 u->ownermap.playouts = 0;
120 memset(u->ownermap.map, 0, board_size2(b) * sizeof(u->ownermap.map[0]));
121 memset(u->stats, 0, board_size2(b) * sizeof(u->stats[0]));
122 u->played_own = u->played_all = 0;
125 static void
126 dead_group_list(struct uct *u, struct board *b, struct move_queue *mq)
128 struct group_judgement gj;
129 gj.thres = GJ_THRES;
130 gj.gs = alloca(board_size2(b) * sizeof(gj.gs[0]));
131 board_ownermap_judge_group(b, &u->ownermap, &gj);
132 groups_of_status(b, &gj, GS_DEAD, mq);
135 bool
136 uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive)
138 if (u->ownermap.playouts < GJ_MINGAMES)
139 return false;
141 struct move_queue mq = { .moves = 0 };
142 if (!pass_all_alive)
143 dead_group_list(u, b, &mq);
144 return pass_is_safe(b, color, &mq);
147 /* This function is called only when running as slave in the distributed version. */
148 static enum parse_code
149 uct_notify(struct engine *e, struct board *b, int id, char *cmd, char *args, char **reply)
151 struct uct *u = e->data;
153 static bool board_resized = false;
154 board_resized |= is_gamestart(cmd);
156 /* Force resending the whole command history if we are out of sync
157 * but do it only once, not if already getting the history. */
158 if ((move_number(id) != b->moves || !board_resized)
159 && !reply_disabled(id) && !is_reset(cmd)) {
160 if (UDEBUGL(0))
161 fprintf(stderr, "Out of sync, id %d, move %d\n", id, b->moves);
162 static char buf[128];
163 snprintf(buf, sizeof(buf), "out of sync, move %d expected", b->moves);
164 *reply = buf;
165 return P_DONE_ERROR;
167 return reply_disabled(id) ? P_NOREPLY : P_OK;
170 static char *
171 uct_printhook_ownermap(struct board *board, coord_t c, char *s, char *end)
173 struct uct *u = board->es;
174 assert(u);
175 const char chr[] = ":XO,"; // dame, black, white, unclear
176 const char chm[] = ":xo,";
177 char ch = chr[board_ownermap_judge_point(&u->ownermap, c, GJ_THRES)];
178 if (ch == ',') { // less precise estimate then?
179 ch = chm[board_ownermap_judge_point(&u->ownermap, c, 0.67)];
181 s += snprintf(s, end - s, "%c ", ch);
182 return s;
185 static char *
186 uct_notify_play(struct engine *e, struct board *b, struct move *m)
188 struct uct *u = e->data;
189 if (!u->t) {
190 /* No state, create one - this is probably game beginning
191 * and we need to load the opening book right now. */
192 prepare_move(e, b, m->color);
193 assert(u->t);
196 /* Stop pondering, required by tree_promote_at() */
197 uct_pondering_stop(u);
198 if (UDEBUGL(2) && u->slave)
199 tree_dump(u->t, u->dumpthres);
201 if (is_resign(m->coord)) {
202 /* Reset state. */
203 reset_state(u);
204 return NULL;
207 /* Promote node of the appropriate move to the tree root. */
208 assert(u->t->root);
209 if (!tree_promote_at(u->t, b, m->coord)) {
210 if (UDEBUGL(0))
211 fprintf(stderr, "Warning: Cannot promote move node! Several play commands in row?\n");
212 reset_state(u);
213 return NULL;
216 /* If we are a slave in a distributed engine, start pondering once
217 * we know which move we actually played. See uct_genmove() about
218 * the check for pass. */
219 if (u->pondering_opt && u->slave && m->color == u->my_color && !is_pass(m->coord))
220 uct_pondering_start(u, b, u->t, stone_other(m->color));
222 return NULL;
225 static char *
226 uct_chat(struct engine *e, struct board *b, char *cmd)
228 struct uct *u = e->data;
229 static char reply[1024];
231 cmd += strspn(cmd, " \n\t");
232 if (!strncasecmp(cmd, "winrate", 7)) {
233 if (!u->t)
234 return "no game context (yet?)";
235 enum stone color = u->t->root_color;
236 struct tree_node *n = u->t->root;
237 snprintf(reply, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
238 n->u.playouts, u->threads, stone2str(color), coord2sstr(n->coord, b),
239 tree_node_get_value(u->t, -1, n->u.value) * 100);
240 if (u->t->use_extra_komi && abs(u->t->extra_komi) >= 0.5) {
241 sprintf(reply + strlen(reply), ", while self-imposing extra komi %.1f",
242 u->t->extra_komi);
244 strcat(reply, ".");
245 return reply;
247 return NULL;
250 static void
251 uct_dead_group_list(struct engine *e, struct board *b, struct move_queue *mq)
253 struct uct *u = e->data;
255 /* This means the game is probably over, no use pondering on. */
256 uct_pondering_stop(u);
258 if (u->pass_all_alive)
259 return; // no dead groups
261 bool mock_state = false;
263 if (!u->t) {
264 /* No state, but we cannot just back out - we might
265 * have passed earlier, only assuming some stones are
266 * dead, and then re-connected, only to lose counting
267 * when all stones are assumed alive. */
268 /* Mock up some state and seed the ownermap by few
269 * simulations. */
270 prepare_move(e, b, S_BLACK); assert(u->t);
271 for (int i = 0; i < GJ_MINGAMES; i++)
272 uct_playout(u, b, S_BLACK, u->t);
273 mock_state = true;
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 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_done(struct engine *e)
297 /* This is called on engine reset, especially when clear_board
298 * is received and new game should begin. */
299 struct uct *u = e->data;
300 uct_pondering_stop(u);
301 if (u->t) reset_state(u);
302 free(u->ownermap.map);
303 free(u->stats);
305 free(u->policy);
306 free(u->random_policy);
307 playout_policy_done(u->playout);
308 uct_prior_done(u->prior);
312 /* Pachi threading structure (if uct_playouts_parallel() is used):
314 * main thread
315 * | main(), GTP communication, ...
316 * | starts and stops the search managed by thread_manager
318 * thread_manager
319 * | spawns and collects worker threads
321 * worker0
322 * worker1
323 * ...
324 * workerK
325 * uct_playouts() loop, doing descend-playout until uct_halt
327 * Another way to look at it is by functions (lines denote thread boundaries):
329 * | uct_genmove()
330 * | uct_search() (uct_search_start() .. uct_search_stop())
331 * | -----------------------
332 * | spawn_thread_manager()
333 * | -----------------------
334 * | spawn_worker()
335 * V uct_playouts() */
337 /* Set in thread manager in case the workers should stop. */
338 volatile sig_atomic_t uct_halt = 0;
339 /* ID of the running worker thread. */
340 __thread int thread_id = -1;
341 /* ID of the thread manager. */
342 static pthread_t thread_manager;
343 static bool thread_manager_running;
345 static pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER;
346 static pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER;
347 static volatile int finish_thread;
348 static pthread_mutex_t finish_serializer = PTHREAD_MUTEX_INITIALIZER;
350 struct spawn_ctx {
351 int tid;
352 struct uct *u;
353 struct board *b;
354 enum stone color;
355 struct tree *t;
356 unsigned long seed;
357 int games;
360 static void *
361 spawn_worker(void *ctx_)
363 struct spawn_ctx *ctx = ctx_;
364 /* Setup */
365 fast_srandom(ctx->seed);
366 thread_id = ctx->tid;
367 /* Run */
368 ctx->games = uct_playouts(ctx->u, ctx->b, ctx->color, ctx->t);
369 /* Finish */
370 pthread_mutex_lock(&finish_serializer);
371 pthread_mutex_lock(&finish_mutex);
372 finish_thread = ctx->tid;
373 pthread_cond_signal(&finish_cond);
374 pthread_mutex_unlock(&finish_mutex);
375 return ctx;
378 /* Thread manager, controlling worker threads. It must be called with
379 * finish_mutex lock held, but it will unlock it itself before exiting;
380 * this is necessary to be completely deadlock-free. */
381 /* The finish_cond can be signalled for it to stop; in that case,
382 * the caller should set finish_thread = -1. */
383 /* After it is started, it will update mctx->t to point at some tree
384 * used for the actual search (matters only for TM_ROOT), on return
385 * it will set mctx->games to the number of performed simulations. */
386 static void *
387 spawn_thread_manager(void *ctx_)
389 /* In thread_manager, we use only some of the ctx fields. */
390 struct spawn_ctx *mctx = ctx_;
391 struct uct *u = mctx->u;
392 struct tree *t = mctx->t;
393 bool shared_tree = u->parallel_tree;
394 fast_srandom(mctx->seed);
396 int played_games = 0;
397 pthread_t threads[u->threads];
398 int joined = 0;
400 uct_halt = 0;
402 /* Garbage collect the tree by preference when pondering. */
403 if (u->pondering && t->nodes && t->nodes_size > t->max_tree_size/2) {
404 unsigned long temp_size = (MIN_FREE_MEM_PERCENT * t->max_tree_size) / 100;
405 t->root = tree_garbage_collect(t, temp_size, t->root);
408 /* Spawn threads... */
409 for (int ti = 0; ti < u->threads; ti++) {
410 struct spawn_ctx *ctx = malloc2(sizeof(*ctx));
411 ctx->u = u; ctx->b = mctx->b; ctx->color = mctx->color;
412 mctx->t = ctx->t = shared_tree ? t : tree_copy(t);
413 ctx->tid = ti; ctx->seed = fast_random(65536) + ti;
414 pthread_create(&threads[ti], NULL, spawn_worker, ctx);
415 if (UDEBUGL(3))
416 fprintf(stderr, "Spawned worker %d\n", ti);
419 /* ...and collect them back: */
420 while (joined < u->threads) {
421 /* Wait for some thread to finish... */
422 pthread_cond_wait(&finish_cond, &finish_mutex);
423 if (finish_thread < 0) {
424 /* Stop-by-caller. Tell the workers to wrap up. */
425 uct_halt = 1;
426 continue;
428 /* ...and gather its remnants. */
429 struct spawn_ctx *ctx;
430 pthread_join(threads[finish_thread], (void **) &ctx);
431 played_games += ctx->games;
432 joined++;
433 if (!shared_tree) {
434 if (ctx->t == mctx->t) mctx->t = t;
435 tree_merge(t, ctx->t);
436 tree_done(ctx->t);
438 free(ctx);
439 if (UDEBUGL(3))
440 fprintf(stderr, "Joined worker %d\n", finish_thread);
441 pthread_mutex_unlock(&finish_serializer);
444 pthread_mutex_unlock(&finish_mutex);
446 if (!shared_tree)
447 tree_normalize(mctx->t, u->threads);
449 mctx->games = played_games;
450 return mctx;
453 static struct spawn_ctx *
454 uct_search_start(struct uct *u, struct board *b, enum stone color, struct tree *t)
456 assert(u->threads > 0);
457 assert(!thread_manager_running);
459 struct spawn_ctx ctx = { .u = u, .b = b, .color = color, .t = t, .seed = fast_random(65536) };
460 static struct spawn_ctx mctx; mctx = ctx;
461 pthread_mutex_lock(&finish_mutex);
462 pthread_create(&thread_manager, NULL, spawn_thread_manager, &mctx);
463 thread_manager_running = true;
464 return &mctx;
467 static struct spawn_ctx *
468 uct_search_stop(void)
470 assert(thread_manager_running);
472 /* Signal thread manager to stop the workers. */
473 pthread_mutex_lock(&finish_mutex);
474 finish_thread = -1;
475 pthread_cond_signal(&finish_cond);
476 pthread_mutex_unlock(&finish_mutex);
478 /* Collect the thread manager. */
479 struct spawn_ctx *pctx;
480 thread_manager_running = false;
481 pthread_join(thread_manager, (void **) &pctx);
482 return pctx;
486 /* Determine whether we should terminate the search early. */
487 static bool
488 uct_search_stop_early(struct uct *u, struct tree *t, struct board *b,
489 struct time_info *ti, struct time_stop *stop,
490 struct tree_node *best, struct tree_node *best2,
491 int played)
493 /* Always use at least half the desired time. It is silly
494 * to lose a won game because we played a bad move in 0.1s. */
495 double elapsed = 0;
496 if (ti->dim == TD_WALLTIME) {
497 elapsed = time_now() - ti->len.t.timer_start;
498 if (elapsed < 0.5 * stop->desired.time) return false;
501 /* Early break in won situation. */
502 if (best->u.playouts >= 2000 && tree_node_get_value(t, 1, best->u.value) >= u->loss_threshold)
503 return true;
504 /* Earlier break in super-won situation. */
505 if (best->u.playouts >= 500 && tree_node_get_value(t, 1, best->u.value) >= 0.95)
506 return true;
508 /* Break early if we estimate the second-best move cannot
509 * catch up in assigned time anymore. We use all our time
510 * if we are in byoyomi with single stone remaining in our
511 * period, however - it's better to pre-ponder. */
512 bool time_indulgent = (!ti->len.t.main_time && ti->len.t.byoyomi_stones == 1);
513 if (best2 && ti->dim == TD_WALLTIME && !time_indulgent) {
514 double remaining = stop->worst.time - elapsed;
515 double pps = ((double)played) / elapsed;
516 double estplayouts = remaining * pps + PLAYOUT_DELTA_SAFEMARGIN;
517 if (best->u.playouts > best2->u.playouts + estplayouts) {
518 if (UDEBUGL(2))
519 fprintf(stderr, "Early stop, result cannot change: "
520 "best %d, best2 %d, estimated %f simulations to go\n",
521 best->u.playouts, best2->u.playouts, estplayouts);
522 return true;
526 return false;
529 /* Determine whether we should terminate the search later than expected. */
530 static bool
531 uct_search_keep_looking(struct uct *u, struct tree *t, struct board *b,
532 struct time_info *ti, struct time_stop *stop,
533 struct tree_node *best, struct tree_node *best2,
534 struct tree_node *bestr, struct tree_node *winner, int i)
536 if (!best) {
537 if (UDEBUGL(2))
538 fprintf(stderr, "Did not find best move, still trying...\n");
539 return true;
542 /* Do not waste time if we are winning. Spend up to worst time if
543 * we are unsure, but only desired time if we are sure of winning. */
544 float beta = 2 * (tree_node_get_value(t, 1, best->u.value) - 0.5);
545 if (ti->dim == TD_WALLTIME && beta > 0) {
546 double good_enough = stop->desired.time * beta + stop->worst.time * (1 - beta);
547 double elapsed = time_now() - ti->len.t.timer_start;
548 if (elapsed > good_enough) return false;
551 if (u->best2_ratio > 0) {
552 /* Check best/best2 simulations ratio. If the
553 * two best moves give very similar results,
554 * keep simulating. */
555 if (best2 && best2->u.playouts
556 && (double)best->u.playouts / best2->u.playouts < u->best2_ratio) {
557 if (UDEBUGL(2))
558 fprintf(stderr, "Best2 ratio %f < threshold %f\n",
559 (double)best->u.playouts / best2->u.playouts,
560 u->best2_ratio);
561 return true;
565 if (u->bestr_ratio > 0) {
566 /* Check best, best_best value difference. If the best move
567 * and its best child do not give similar enough results,
568 * keep simulating. */
569 if (bestr && bestr->u.playouts
570 && fabs((double)best->u.value - bestr->u.value) > u->bestr_ratio) {
571 if (UDEBUGL(2))
572 fprintf(stderr, "Bestr delta %f > threshold %f\n",
573 fabs((double)best->u.value - bestr->u.value),
574 u->bestr_ratio);
575 return true;
579 if (winner && winner != best) {
580 /* Keep simulating if best explored
581 * does not have also highest value. */
582 if (UDEBUGL(2))
583 fprintf(stderr, "[%d] best %3s [%d] %f != winner %3s [%d] %f\n", i,
584 coord2sstr(best->coord, t->board),
585 best->u.playouts, tree_node_get_value(t, 1, best->u.value),
586 coord2sstr(winner->coord, t->board),
587 winner->u.playouts, tree_node_get_value(t, 1, winner->u.value));
588 return true;
591 /* No reason to keep simulating, bye. */
592 return false;
595 /* Run time-limited MCTS search. For a slave in the distributed
596 * engine, the search is done in background and will be stopped at
597 * the next uct_notify_play(); keep_looking is advice for the master. */
598 static int
599 uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color,
600 struct tree *t, bool *keep_looking)
602 int base_playouts = u->t->root->u.playouts;
603 if (UDEBUGL(2) && base_playouts > 0)
604 fprintf(stderr, "<pre-simulated %d games skipped>\n", base_playouts);
606 *keep_looking = false;
608 /* Number of last dynkomi adjustment. */
609 int last_dynkomi = t->root->u.playouts;
610 /* Number of last game with progress print. */
611 int last_print = t->root->u.playouts;
612 /* Number of simulations to wait before next print. */
613 int print_interval = TREE_SIMPROGRESS_INTERVAL * (u->thread_model == TM_ROOT ? 1 : u->threads);
614 /* Printed notification about full memory? */
615 bool print_fullmem = false;
617 static struct time_stop stop;
618 static struct spawn_ctx *ctx;
619 if (!thread_manager_running) {
620 if (ti->period == TT_NULL) *ti = default_ti;
621 time_stop_conditions(ti, b, u->fuseki_end, u->yose_start, &stop);
623 ctx = uct_search_start(u, b, color, t);
624 } else {
625 /* Keep the search running. */
626 assert(u->slave);
629 /* The search tree is ctx->t. This is normally == t, but in case of
630 * TM_ROOT, it is one of the trees belonging to the independent
631 * workers. It is important to reference ctx->t directly since the
632 * thread manager will swap the tree pointer asynchronously. */
633 /* XXX: This means TM_ROOT support is suboptimal since single stalled
634 * thread can stall the others in case of limiting the search by game
635 * count. However, TM_ROOT just does not deserve any more extra code
636 * right now. */
638 struct tree_node *best = NULL;
639 struct tree_node *best2 = NULL; // Second-best move.
640 struct tree_node *bestr = NULL; // best's best child.
641 struct tree_node *winner = NULL;
643 double busywait_interval = TREE_BUSYWAIT_INTERVAL;
645 /* Now, just periodically poll the search tree. */
646 while (1) {
647 time_sleep(busywait_interval);
648 /* busywait_interval should never be less than desired time, or the
649 * time control is broken. But if it happens to be less, we still search
650 * at least 100ms otherwise the move is completely random. */
652 int i = ctx->t->root->u.playouts;
654 /* Adjust dynkomi? */
655 if (ctx->t->use_extra_komi && u->dynkomi->permove
656 && u->dynkomi_interval
657 && i > last_dynkomi + u->dynkomi_interval) {
658 last_dynkomi += u->dynkomi_interval;
659 float old_dynkomi = ctx->t->extra_komi;
660 ctx->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, ctx->t);
661 if (UDEBUGL(3) && old_dynkomi != ctx->t->extra_komi)
662 fprintf(stderr, "dynkomi adjusted (%f -> %f)\n", old_dynkomi, ctx->t->extra_komi);
665 /* Print progress? */
666 if (i - last_print > print_interval) {
667 last_print += print_interval; // keep the numbers tidy
668 uct_progress_status(u, ctx->t, color, last_print);
670 if (!print_fullmem && ctx->t->nodes_size > u->max_tree_size) {
671 if (UDEBUGL(2))
672 fprintf(stderr, "memory limit hit (%lu > %lu)\n", ctx->t->nodes_size, u->max_tree_size);
673 print_fullmem = true;
676 /* Never consider stopping if we played too few simulations.
677 * Maybe we risk losing on time when playing in super-extreme
678 * time pressure but the tree is going to be just too messed
679 * up otherwise - we might even play invalid suicides or pass
680 * when we mustn't. */
681 if (i < GJ_MINGAMES)
682 continue;
684 best = u->policy->choose(u->policy, ctx->t->root, b, color, resign);
685 if (best) best2 = u->policy->choose(u->policy, ctx->t->root, b, color, best->coord);
687 /* Possibly stop search early if it's no use to try on. */
688 int played = u->played_all + i - base_playouts;
689 if (best && uct_search_stop_early(u, ctx->t, b, ti, &stop, best, best2, played))
690 break;
692 /* Check against time settings. */
693 bool desired_done;
694 if (ti->dim == TD_WALLTIME) {
695 double elapsed = time_now() - ti->len.t.timer_start;
696 if (elapsed > stop.worst.time) break;
697 desired_done = elapsed > stop.desired.time;
699 } else { assert(ti->dim == TD_GAMES);
700 if (i > stop.worst.playouts) break;
701 desired_done = i > stop.desired.playouts;
704 /* We want to stop simulating, but are willing to keep trying
705 * if we aren't completely sure about the winner yet. */
706 if (desired_done) {
707 if (u->policy->winner && u->policy->evaluate) {
708 struct uct_descent descent = { .node = ctx->t->root };
709 u->policy->winner(u->policy, ctx->t, &descent);
710 winner = descent.node;
712 if (best)
713 bestr = u->policy->choose(u->policy, best, b, stone_other(color), resign);
714 if (!uct_search_keep_looking(u, ctx->t, b, ti, &stop, best, best2, bestr, winner, i))
715 break;
718 /* TODO: Early break if best->variance goes under threshold and we already
719 * have enough playouts (possibly thanks to book or to pondering)? */
721 /* If running as slave in the distributed engine,
722 * let the search continue in background. */
723 if (u->slave) {
724 *keep_looking = true;
725 break;
729 int games;
730 if (!u->slave) {
731 ctx = uct_search_stop();
732 games = ctx->games;
733 if (UDEBUGL(2)) tree_dump(t, u->dumpthres);
734 } else {
735 /* We can only return an estimate here. */
736 games = ctx->t->root->u.playouts - base_playouts;
738 if (UDEBUGL(2))
739 fprintf(stderr, "(avg score %f/%d value %f/%d)\n",
740 u->dynkomi->score.value, u->dynkomi->score.playouts,
741 u->dynkomi->value.value, u->dynkomi->value.playouts);
742 if (UDEBUGL(0))
743 uct_progress_status(u, t, color, games);
745 return games;
749 /* Start pondering background with @color to play. */
750 static void
751 uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color)
753 if (UDEBUGL(1))
754 fprintf(stderr, "Starting to ponder with color %s\n", stone2str(stone_other(color)));
755 u->pondering = true;
757 /* We need a local board copy to ponder upon. */
758 struct board *b = malloc2(sizeof(*b)); board_copy(b, b0);
760 /* *b0 did not have the genmove'd move played yet. */
761 struct move m = { t->root->coord, t->root_color };
762 int res = board_play(b, &m);
763 assert(res >= 0);
764 setup_dynkomi(u, b, stone_other(m.color));
766 /* Start MCTS manager thread "headless". */
767 uct_search_start(u, b, color, t);
770 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
771 * to stop the background search for a slave in the distributed engine. */
772 static void
773 uct_pondering_stop(struct uct *u)
775 if (!thread_manager_running)
776 return;
778 /* Stop the thread manager. */
779 struct spawn_ctx *ctx = uct_search_stop();
780 if (UDEBUGL(1)) {
781 if (u->pondering) fprintf(stderr, "(pondering) ");
782 uct_progress_status(u, ctx->t, ctx->color, ctx->games);
784 if (u->pondering) {
785 free(ctx->b);
786 u->pondering = false;
790 /* Common part of uct_genmove() and uct_genmoves().
791 * Returns the best node, or NULL if *best_coord is pass or resign. */
792 static struct tree_node *
793 uct_bestmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color,
794 bool pass_all_alive, bool *keep_looking, coord_t *best_coord)
796 double start_time = time_now();
797 struct uct *u = e->data;
799 if (b->superko_violation) {
800 fprintf(stderr, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
801 fprintf(stderr, "Maybe you play with situational instead of positional superko?\n");
802 fprintf(stderr, "I'm going to ignore the violation, but note that I may miss\n");
803 fprintf(stderr, "some moves valid under this ruleset because of this.\n");
804 b->superko_violation = false;
807 assert(u->t);
808 u->my_color = color;
810 /* How to decide whether to use dynkomi in this game? Since we use
811 * pondering, it's not simple "who-to-play" matter. Decide based on
812 * the last genmove issued. */
813 u->t->use_extra_komi = !!(u->dynkomi_mask & color);
814 setup_dynkomi(u, b, color);
816 if (b->rules == RULES_JAPANESE)
817 u->territory_scoring = true;
819 /* Make pessimistic assumption about komi for Japanese rules to
820 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
821 * The rules usually give the same winner if the integer part of komi
822 * is odd so we adjust the komi only if it is even (for a board of
823 * odd size). We are not trying to get an exact evaluation for rare
824 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
825 if (u->territory_scoring && (((int)floor(b->komi) + board_size(b)) & 1)) {
826 b->komi += (color == S_BLACK ? 1.0 : -1.0);
827 if (UDEBUGL(0))
828 fprintf(stderr, "Setting komi to %.1f assuming Japanese rules\n",
829 b->komi);
832 int base_playouts = u->t->root->u.playouts;
833 /* Start or continue the Monte Carlo Tree Search! */
834 int played_games = uct_search(u, b, ti, color, u->t, keep_looking);
835 u->played_own += played_games;
837 /* Choose the best move from the tree. */
838 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color, resign);
839 if (!best) {
840 *best_coord = pass;
841 return NULL;
843 *best_coord = best->coord;
844 if (UDEBUGL(1))
845 fprintf(stderr, "*** WINNER is %s (%d,%d) with score %1.4f (%d/%d:%d/%d games), extra komi %f\n",
846 coord2sstr(best->coord, b), coord_x(best->coord, b), coord_y(best->coord, b),
847 tree_node_get_value(u->t, 1, best->u.value), best->u.playouts,
848 u->t->root->u.playouts, u->t->root->u.playouts - base_playouts, played_games,
849 u->t->extra_komi);
851 /* Do not resign if we're so short of time that evaluation of best
852 * move is completely unreliable, we might be winning actually.
853 * In this case best is almost random but still better than resign.
854 * Also do not resign if we are getting bad results while actually
855 * giving away extra komi points (dynkomi). */
856 if (tree_node_get_value(u->t, 1, best->u.value) < u->resign_ratio
857 && !is_pass(best->coord) && best->u.playouts > GJ_MINGAMES
858 && u->t->extra_komi < 0.5 /* XXX we assume dynamic komi == we are black */) {
859 *best_coord = resign;
860 return NULL;
863 /* If the opponent just passed and we win counting, always
864 * pass as well. */
865 if (b->moves > 1 && is_pass(b->last_move.coord)) {
866 /* Make sure enough playouts are simulated. */
867 while (u->ownermap.playouts < GJ_MINGAMES)
868 uct_playout(u, b, color, u->t);
869 if (uct_pass_is_safe(u, b, color, u->pass_all_alive || pass_all_alive)) {
870 if (UDEBUGL(0))
871 fprintf(stderr, "<Will rather pass, looks safe enough.>\n");
872 *best_coord = pass;
873 best = NULL;
877 if (UDEBUGL(2)) {
878 double time = time_now() - start_time + 0.000001; /* avoid divide by zero */
879 fprintf(stderr, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
880 time, (int)(played_games/time), (int)(played_games/time/u->threads));
882 return best;
885 static coord_t *
886 uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
888 struct uct *u = e->data;
889 uct_pondering_stop(u);
890 prepare_move(e, b, color);
892 bool keep_looking;
893 coord_t best_coord;
894 struct tree_node *best;
895 best = uct_bestmove(e, b, ti, color, pass_all_alive, &keep_looking, &best_coord);
896 if (!best) {
897 reset_state(u);
898 return coord_copy(best_coord);
900 tree_promote_node(u->t, &best);
902 /* After a pass, pondering is harmful for two reasons:
903 * (i) We might keep pondering even when the game is over.
904 * Of course this is the case for opponent resign as well.
905 * (ii) More importantly, the ownermap will get skewed since
906 * the UCT will start cutting off any playouts. */
907 if (u->pondering_opt && !is_pass(best->coord)) {
908 uct_pondering_start(u, b, u->t, stone_other(color));
910 return coord_copy(best_coord);
913 /* Get stats updates for the distributed engine. Return a buffer with
914 * one line "played_own root_playouts threads keep_looking" then a list
915 * of lines "coord playouts value amaf_playouts amaf_value".
916 * The last line must not end with \n.
917 * If c is pass or resign, add this move with root->playouts weight.
918 * This function is called only by the main thread, but may be
919 * called while the tree is updated by the worker threads.
920 * Keep this code in sync with select_best_move(). */
921 static char *
922 uct_getstats(struct uct *u, struct board *b, coord_t c, bool keep_looking)
924 static char reply[10240];
925 char *r = reply;
926 char *end = reply + sizeof(reply);
927 struct tree_node *root = u->t->root;
928 r += snprintf(r, end - r, "%d %d %d %d", u->played_own, root->u.playouts, u->threads, keep_looking);
929 int min_playouts = root->u.playouts / 100;
931 /* Give a large weight to pass or resign, but still allow other moves.
932 * Only root->u.playouts will be used (majority vote) but send amaf
933 * stats too for consistency. */
934 if (is_pass(c) || is_resign(c))
935 r += snprintf(r, end - r, "\n%s %d %.1f %d %.1f", coord2sstr(c, b),
936 root->u.playouts, 0.0, root->amaf.playouts, 0.0);
938 /* We rely on the fact that root->children is set only
939 * after all children are created. */
940 for (struct tree_node *ni = root->children; ni; ni = ni->sibling) {
942 if (is_pass(ni->coord)) continue;
943 struct node_stats *ns = &u->stats[ni->coord];
944 ns->last_sent_own.u.playouts = ns->last_sent_own.amaf.playouts = 0;
945 ns->node = ni;
946 if (ni->u.playouts <= min_playouts || ni->hints & TREE_HINT_INVALID)
947 continue;
949 char *coord = coord2sstr(ni->coord, b);
950 /* We return the values as stored in the tree, so from black's view.
951 * own = total_in_tree - added_from_others */
952 struct move_stats2 s = { .u = ni->u, .amaf = ni->amaf };
953 struct move_stats2 others = ns->added_from_others;
954 if (s.u.playouts - others.u.playouts <= min_playouts)
955 continue;
956 if (others.u.playouts)
957 stats_rm_result(&s.u, others.u.value, others.u.playouts);
958 if (others.amaf.playouts)
959 stats_rm_result(&s.amaf, others.amaf.value, others.amaf.playouts);
961 r += snprintf(r, end - r, "\n%s %d %.7f %d %.7f", coord,
962 s.u.playouts, s.u.value, s.amaf.playouts, s.amaf.value);
963 ns->last_sent_own = s;
964 /* If the master discards these values because this slave
965 * is out of sync, u->stats will be reset anyway. */
967 return reply;
970 /* Set mapping from coordinates to children of the root node. */
971 static void
972 find_top_nodes(struct uct *u)
974 if (!u->t || !u->t->root) return;
976 for (struct tree_node *ni = u->t->root->children; ni; ni = ni->sibling) {
977 if (!is_pass(ni->coord))
978 u->stats[ni->coord].node = ni;
982 /* genmoves gets in the args parameter
983 * "played_games main_time byoyomi_time byoyomi_periods byoyomi_stones"
984 * and reads a list of lines "coord playouts value amaf_playouts amaf_value"
985 * to get stats of other slaves, except for the first call at a given move number.
986 * See uct_getstats() for the description of the return value. */
987 static char *
988 uct_genmoves(struct engine *e, struct board *b, struct time_info *ti, enum stone color,
989 char *args, bool pass_all_alive)
991 struct uct *u = e->data;
992 assert(u->slave);
994 /* Seed the tree if the search is not already running. */
995 if (!thread_manager_running) prepare_move(e, b, color);
997 /* Get playouts and time information from master.
998 * Keep this code in sync with distributed_genmove(). */
999 if ((ti->dim == TD_WALLTIME
1000 && sscanf(args, "%d %lf %lf %d %d", &u->played_all, &ti->len.t.main_time,
1001 &ti->len.t.byoyomi_time, &ti->len.t.byoyomi_periods,
1002 &ti->len.t.byoyomi_stones) != 5)
1004 || (ti->dim == TD_GAMES && sscanf(args, "%d", &u->played_all) != 1)) {
1005 return NULL;
1008 /* Get the move stats if they are present. They are
1009 * coord-sorted but the code here doesn't depend on this.
1010 * Keep this code in sync with select_best_move(). */
1012 char line[128];
1013 while (fgets(line, sizeof(line), stdin) && *line != '\n') {
1014 char move[64];
1015 struct move_stats2 s;
1016 if (sscanf(line, "%63s %d %f %d %f", move,
1017 &s.u.playouts, &s.u.value,
1018 &s.amaf.playouts, &s.amaf.value) != 5)
1019 return NULL;
1020 coord_t *c_ = str2coord(move, board_size(b));
1021 coord_t c = *c_;
1022 coord_done(c_);
1023 assert(!is_pass(c) && !is_resign(c));
1025 struct node_stats *ns = &u->stats[c];
1026 if (!ns->node) find_top_nodes(u);
1027 /* The node may not exist if this slave was behind
1028 * but this should be rare so it is not worth creating
1029 * the node here. */
1030 if (!ns->node) {
1031 if (DEBUGL(2))
1032 fprintf(stderr, "can't find node %s %d\n", move, c);
1033 continue;
1036 /* The master may not send moves below a certain threshold,
1037 * but if it sends one it includes the contributions from
1038 * all slaves including ours (last_sent_own):
1039 * received_others = received_total - last_sent_own */
1040 if (ns->last_sent_own.u.playouts)
1041 stats_rm_result(&s.u, ns->last_sent_own.u.value,
1042 ns->last_sent_own.u.playouts);
1043 if (ns->last_sent_own.amaf.playouts)
1044 stats_rm_result(&s.amaf, ns->last_sent_own.amaf.value,
1045 ns->last_sent_own.amaf.playouts);
1047 /* others_delta = received_others - added_from_others */
1048 struct move_stats2 delta = s;
1049 if (ns->added_from_others.u.playouts)
1050 stats_rm_result(&delta.u, ns->added_from_others.u.value,
1051 ns->added_from_others.u.playouts);
1052 /* delta may be <= 0 if some slaves stopped sending this move
1053 * because it became below a playouts threshold. In this case
1054 * we just keep the old stats in our tree. */
1055 if (delta.u.playouts <= 0) continue;
1057 if (ns->added_from_others.amaf.playouts)
1058 stats_rm_result(&delta.amaf, ns->added_from_others.amaf.value,
1059 ns->added_from_others.amaf.playouts);
1061 stats_add_result(&ns->node->u, delta.u.value, delta.u.playouts);
1062 stats_add_result(&ns->node->amaf, delta.amaf.value, delta.amaf.playouts);
1063 ns->added_from_others = s;
1066 bool keep_looking;
1067 coord_t best_coord;
1068 uct_bestmove(e, b, ti, color, pass_all_alive, &keep_looking, &best_coord);
1070 char *reply = uct_getstats(u, b, best_coord, keep_looking);
1071 return reply;
1075 bool
1076 uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color)
1078 struct uct *u = e->data;
1079 if (!u->t) prepare_move(e, b, color);
1080 assert(u->t);
1082 if (ti->dim == TD_GAMES) {
1083 /* Don't count in games that already went into the book. */
1084 ti->len.games += u->t->root->u.playouts;
1086 bool keep_looking;
1087 uct_search(u, b, ti, color, u->t, &keep_looking);
1089 assert(ti->dim == TD_GAMES);
1090 tree_save(u->t, b, ti->len.games / 100);
1092 return true;
1095 void
1096 uct_dumpbook(struct engine *e, struct board *b, enum stone color)
1098 struct uct *u = e->data;
1099 struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0, u->local_tree_aging);
1100 tree_load(t, b);
1101 tree_dump(t, 0);
1102 tree_done(t);
1106 struct uct *
1107 uct_state_init(char *arg, struct board *b)
1109 struct uct *u = calloc2(1, sizeof(struct uct));
1110 bool using_elo = false;
1112 u->debug_level = debug_level;
1113 u->gamelen = MC_GAMELEN;
1114 u->mercymin = 0;
1115 u->expand_p = 2;
1116 u->dumpthres = 1000;
1117 u->playout_amaf = true;
1118 u->playout_amaf_nakade = false;
1119 u->amaf_prior = false;
1120 u->max_tree_size = 3072ULL * 1048576;
1122 u->dynkomi_mask = S_BLACK;
1124 u->threads = 1;
1125 u->thread_model = TM_TREEVL;
1126 u->parallel_tree = true;
1127 u->virtual_loss = true;
1129 u->fuseki_end = 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
1130 u->yose_start = 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
1131 u->bestr_ratio = 0.02;
1132 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
1133 // TODO: Further tuning and experiments with better time allocation schemes.
1134 u->best2_ratio = 2.5;
1136 u->val_scale = 0.04; u->val_points = 40;
1138 u->tenuki_d = 4;
1139 u->local_tree_aging = 2;
1141 if (arg) {
1142 char *optspec, *next = arg;
1143 while (*next) {
1144 optspec = next;
1145 next += strcspn(next, ",");
1146 if (*next) { *next++ = 0; } else { *next = 0; }
1148 char *optname = optspec;
1149 char *optval = strchr(optspec, '=');
1150 if (optval) *optval++ = 0;
1152 if (!strcasecmp(optname, "debug")) {
1153 if (optval)
1154 u->debug_level = atoi(optval);
1155 else
1156 u->debug_level++;
1157 } else if (!strcasecmp(optname, "mercy") && optval) {
1158 /* Minimal difference of black/white captures
1159 * to stop playout - "Mercy Rule". Speeds up
1160 * hopeless playouts at the expense of some
1161 * accuracy. */
1162 u->mercymin = atoi(optval);
1163 } else if (!strcasecmp(optname, "gamelen") && optval) {
1164 u->gamelen = atoi(optval);
1165 } else if (!strcasecmp(optname, "expand_p") && optval) {
1166 u->expand_p = atoi(optval);
1167 } else if (!strcasecmp(optname, "dumpthres") && optval) {
1168 u->dumpthres = atoi(optval);
1169 } else if (!strcasecmp(optname, "best2_ratio") && optval) {
1170 /* If set, prolong simulating while
1171 * first_best/second_best playouts ratio
1172 * is less than best2_ratio. */
1173 u->best2_ratio = atof(optval);
1174 } else if (!strcasecmp(optname, "bestr_ratio") && optval) {
1175 /* If set, prolong simulating while
1176 * best,best_best_child values delta
1177 * is more than bestr_ratio. */
1178 u->bestr_ratio = atof(optval);
1179 } else if (!strcasecmp(optname, "playout_amaf")) {
1180 /* Whether to include random playout moves in
1181 * AMAF as well. (Otherwise, only tree moves
1182 * are included in AMAF. Of course makes sense
1183 * only in connection with an AMAF policy.) */
1184 /* with-without: 55.5% (+-4.1) */
1185 if (optval && *optval == '0')
1186 u->playout_amaf = false;
1187 else
1188 u->playout_amaf = true;
1189 } else if (!strcasecmp(optname, "playout_amaf_nakade")) {
1190 /* Whether to include nakade moves from playouts
1191 * in the AMAF statistics; this tends to nullify
1192 * the playout_amaf effect by adding too much
1193 * noise. */
1194 if (optval && *optval == '0')
1195 u->playout_amaf_nakade = false;
1196 else
1197 u->playout_amaf_nakade = true;
1198 } else if (!strcasecmp(optname, "playout_amaf_cutoff") && optval) {
1199 /* Keep only first N% of playout stage AMAF
1200 * information. */
1201 u->playout_amaf_cutoff = atoi(optval);
1202 } else if ((!strcasecmp(optname, "policy") || !strcasecmp(optname, "random_policy")) && optval) {
1203 char *policyarg = strchr(optval, ':');
1204 struct uct_policy **p = !strcasecmp(optname, "policy") ? &u->policy : &u->random_policy;
1205 if (policyarg)
1206 *policyarg++ = 0;
1207 if (!strcasecmp(optval, "ucb1")) {
1208 *p = policy_ucb1_init(u, policyarg);
1209 } else if (!strcasecmp(optval, "ucb1amaf")) {
1210 *p = policy_ucb1amaf_init(u, policyarg);
1211 } else {
1212 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
1213 exit(1);
1215 } else if (!strcasecmp(optname, "playout") && optval) {
1216 char *playoutarg = strchr(optval, ':');
1217 if (playoutarg)
1218 *playoutarg++ = 0;
1219 if (!strcasecmp(optval, "moggy")) {
1220 u->playout = playout_moggy_init(playoutarg, b);
1221 } else if (!strcasecmp(optval, "light")) {
1222 u->playout = playout_light_init(playoutarg, b);
1223 } else if (!strcasecmp(optval, "elo")) {
1224 u->playout = playout_elo_init(playoutarg, b);
1225 using_elo = true;
1226 } else {
1227 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
1228 exit(1);
1230 } else if (!strcasecmp(optname, "prior") && optval) {
1231 u->prior = uct_prior_init(optval, b);
1232 } else if (!strcasecmp(optname, "amaf_prior") && optval) {
1233 u->amaf_prior = atoi(optval);
1234 } else if (!strcasecmp(optname, "threads") && optval) {
1235 /* By default, Pachi will run with only single
1236 * tree search thread! */
1237 u->threads = atoi(optval);
1238 } else if (!strcasecmp(optname, "thread_model") && optval) {
1239 if (!strcasecmp(optval, "root")) {
1240 /* Root parallelization - each thread
1241 * does independent search, trees are
1242 * merged at the end. */
1243 u->thread_model = TM_ROOT;
1244 u->parallel_tree = false;
1245 u->virtual_loss = false;
1246 } else if (!strcasecmp(optval, "tree")) {
1247 /* Tree parallelization - all threads
1248 * grind on the same tree. */
1249 u->thread_model = TM_TREE;
1250 u->parallel_tree = true;
1251 u->virtual_loss = false;
1252 } else if (!strcasecmp(optval, "treevl")) {
1253 /* Tree parallelization, but also
1254 * with virtual losses - this discou-
1255 * rages most threads choosing the
1256 * same tree branches to read. */
1257 u->thread_model = TM_TREEVL;
1258 u->parallel_tree = true;
1259 u->virtual_loss = true;
1260 } else {
1261 fprintf(stderr, "UCT: Invalid thread model %s\n", optval);
1262 exit(1);
1264 } else if (!strcasecmp(optname, "pondering")) {
1265 /* Keep searching even during opponent's turn. */
1266 u->pondering_opt = !optval || atoi(optval);
1267 } else if (!strcasecmp(optname, "fuseki_end") && optval) {
1268 /* At the very beginning it's not worth thinking
1269 * too long because the playout evaluations are
1270 * very noisy. So gradually increase the thinking
1271 * time up to maximum when fuseki_end percent
1272 * of the board has been played.
1273 * This only applies if we are not in byoyomi. */
1274 u->fuseki_end = atoi(optval);
1275 } else if (!strcasecmp(optname, "yose_start") && optval) {
1276 /* When yose_start percent of the board has been
1277 * played, or if we are in byoyomi, stop spending
1278 * more time and spread the remaining time
1279 * uniformly.
1280 * Between fuseki_end and yose_start, we spend
1281 * a constant proportion of the remaining time
1282 * on each move. (yose_start should actually
1283 * be much earlier than when real yose start,
1284 * but "yose" is a good short name to convey
1285 * the idea.) */
1286 u->yose_start = atoi(optval);
1287 } else if (!strcasecmp(optname, "force_seed") && optval) {
1288 u->force_seed = atoi(optval);
1289 } else if (!strcasecmp(optname, "no_book")) {
1290 u->no_book = true;
1291 } else if (!strcasecmp(optname, "dynkomi") && optval) {
1292 /* Dynamic komi approach; there are multiple
1293 * ways to adjust komi dynamically throughout
1294 * play. We currently support two: */
1295 char *dynkomiarg = strchr(optval, ':');
1296 if (dynkomiarg)
1297 *dynkomiarg++ = 0;
1298 if (!strcasecmp(optval, "none")) {
1299 u->dynkomi = uct_dynkomi_init_none(u, dynkomiarg, b);
1300 } else if (!strcasecmp(optval, "linear")) {
1301 u->dynkomi = uct_dynkomi_init_linear(u, dynkomiarg, b);
1302 } else if (!strcasecmp(optval, "adaptive")) {
1303 u->dynkomi = uct_dynkomi_init_adaptive(u, dynkomiarg, b);
1304 } else {
1305 fprintf(stderr, "UCT: Invalid dynkomi mode %s\n", optval);
1306 exit(1);
1308 } else if (!strcasecmp(optname, "dynkomi_mask") && optval) {
1309 /* Bitmask of colors the player must be
1310 * for dynkomi be applied; you may want
1311 * to use dynkomi_mask=3 to allow dynkomi
1312 * even in games where Pachi is white. */
1313 u->dynkomi_mask = atoi(optval);
1314 } else if (!strcasecmp(optname, "dynkomi_interval") && optval) {
1315 /* If non-zero, re-adjust dynamic komi
1316 * throughout a single genmove reading,
1317 * roughly every N simulations. */
1318 /* XXX: Does not work with tree
1319 * parallelization. */
1320 u->dynkomi_interval = atoi(optval);
1321 } else if (!strcasecmp(optname, "val_scale") && optval) {
1322 /* How much of the game result value should be
1323 * influenced by win size. Zero means it isn't. */
1324 u->val_scale = atof(optval);
1325 } else if (!strcasecmp(optname, "val_points") && optval) {
1326 /* Maximum size of win to be scaled into game
1327 * result value. Zero means boardsize^2. */
1328 u->val_points = atoi(optval) * 2; // result values are doubled
1329 } else if (!strcasecmp(optname, "val_extra")) {
1330 /* If false, the score coefficient will be simply
1331 * added to the value, instead of scaling the result
1332 * coefficient because of it. */
1333 u->val_extra = !optval || atoi(optval);
1334 } else if (!strcasecmp(optname, "local_tree") && optval) {
1335 /* Whether to bias exploration by local tree values
1336 * (must be supported by the used policy).
1337 * 0: Don't.
1338 * 1: Do, value = result.
1339 * Try to temper the result:
1340 * 2: Do, value = 0.5+(result-expected)/2.
1341 * 3: Do, value = 0.5+bzz((result-expected)^2).
1342 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
1343 u->local_tree = atoi(optval);
1344 } else if (!strcasecmp(optname, "tenuki_d") && optval) {
1345 /* Tenuki distance at which to break the local tree. */
1346 u->tenuki_d = atoi(optval);
1347 if (u->tenuki_d > TREE_NODE_D_MAX + 1) {
1348 fprintf(stderr, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX + 1);
1349 exit(1);
1351 } else if (!strcasecmp(optname, "local_tree_aging") && optval) {
1352 /* How much to reduce local tree values between moves. */
1353 u->local_tree_aging = atof(optval);
1354 } else if (!strcasecmp(optname, "local_tree_allseq")) {
1355 /* By default, only complete sequences are stored
1356 * in the local tree. If this is on, also
1357 * subsequences starting at each move are stored. */
1358 u->local_tree_allseq = !optval || atoi(optval);
1359 } else if (!strcasecmp(optname, "local_tree_playout")) {
1360 /* Whether to adjust ELO playout probability
1361 * distributions according to matched localtree
1362 * information. */
1363 u->local_tree_playout = !optval || atoi(optval);
1364 } else if (!strcasecmp(optname, "local_tree_pseqroot")) {
1365 /* By default, when we have no sequence move
1366 * to suggest in-playout, we give up. If this
1367 * is on, we make probability distribution from
1368 * sequences first moves instead. */
1369 u->local_tree_pseqroot = !optval || atoi(optval);
1370 } else if (!strcasecmp(optname, "pass_all_alive")) {
1371 /* Whether to consider all stones alive at the game
1372 * end instead of marking dead groupd. */
1373 u->pass_all_alive = !optval || atoi(optval);
1374 } else if (!strcasecmp(optname, "territory_scoring")) {
1375 /* Use territory scoring (default is area scoring).
1376 * An explicit kgs-rules command overrides this. */
1377 u->territory_scoring = !optval || atoi(optval);
1378 } else if (!strcasecmp(optname, "random_policy_chance") && optval) {
1379 /* If specified (N), with probability 1/N, random_policy policy
1380 * descend is used instead of main policy descend; useful
1381 * if specified policy (e.g. UCB1AMAF) can make unduly biased
1382 * choices sometimes, you can fall back to e.g.
1383 * random_policy=UCB1. */
1384 u->random_policy_chance = atoi(optval);
1385 } else if (!strcasecmp(optname, "max_tree_size") && optval) {
1386 /* Maximum amount of memory [MiB] consumed by the move tree.
1387 * For fast_alloc it includes the temp tree used for pruning.
1388 * Default is 3072 (3 GiB). Note that if you use TM_ROOT,
1389 * this limits size of only one of the trees, not all of them
1390 * together. */
1391 u->max_tree_size = atol(optval) * 1048576;
1392 } else if (!strcasecmp(optname, "fast_alloc")) {
1393 u->fast_alloc = !optval || atoi(optval);
1394 } else if (!strcasecmp(optname, "slave")) {
1395 /* Act as slave for the distributed engine. */
1396 u->slave = !optval || atoi(optval);
1397 } else if (!strcasecmp(optname, "banner") && optval) {
1398 /* Additional banner string. This must come as the
1399 * last engine parameter. */
1400 if (*next) *--next = ',';
1401 u->banner = strdup(optval);
1402 break;
1403 } else {
1404 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
1405 exit(1);
1410 u->resign_ratio = 0.2; /* Resign when most games are lost. */
1411 u->loss_threshold = 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
1412 if (!u->policy)
1413 u->policy = policy_ucb1amaf_init(u, NULL);
1415 if (!!u->random_policy_chance ^ !!u->random_policy) {
1416 fprintf(stderr, "uct: Only one of random_policy and random_policy_chance is set\n");
1417 exit(1);
1420 if (!u->local_tree) {
1421 /* No ltree aging. */
1422 u->local_tree_aging = 1.0f;
1424 if (!using_elo)
1425 u->local_tree_playout = false;
1427 if (u->fast_alloc && !u->parallel_tree) {
1428 fprintf(stderr, "fast_alloc not supported with root parallelization.\n");
1429 exit(1);
1431 if (u->fast_alloc)
1432 u->max_tree_size = (100ULL * u->max_tree_size) / (100 + MIN_FREE_MEM_PERCENT);
1434 if (!u->prior)
1435 u->prior = uct_prior_init(NULL, b);
1437 if (!u->playout)
1438 u->playout = playout_moggy_init(NULL, b);
1439 u->playout->debug_level = u->debug_level;
1441 u->ownermap.map = malloc2(board_size2(b) * sizeof(u->ownermap.map[0]));
1442 u->stats = malloc2(board_size2(b) * sizeof(u->stats[0]));
1444 if (!u->dynkomi)
1445 u->dynkomi = uct_dynkomi_init_linear(u, NULL, b);
1447 /* Some things remain uninitialized for now - the opening book
1448 * is not loaded and the tree not set up. */
1449 /* This will be initialized in setup_state() at the first move
1450 * received/requested. This is because right now we are not aware
1451 * about any komi or handicap setup and such. */
1453 return u;
1456 struct engine *
1457 engine_uct_init(char *arg, struct board *b)
1459 struct uct *u = uct_state_init(arg, b);
1460 struct engine *e = calloc2(1, sizeof(struct engine));
1461 e->name = "UCT Engine";
1462 e->printhook = uct_printhook_ownermap;
1463 e->notify_play = uct_notify_play;
1464 e->chat = uct_chat;
1465 e->genmove = uct_genmove;
1466 e->genmoves = uct_genmoves;
1467 e->dead_group_list = uct_dead_group_list;
1468 e->done = uct_done;
1469 e->data = u;
1470 if (u->slave)
1471 e->notify = uct_notify;
1473 const char banner[] = "I'm playing UCT. When I'm losing, I will resign, "
1474 "if I think I win, I play until you pass. "
1475 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1476 if (!u->banner) u->banner = "";
1477 e->comment = malloc2(sizeof(banner) + strlen(u->banner) + 1);
1478 sprintf(e->comment, "%s %s", banner, u->banner);
1480 return e;