Always use at least half the desired time.
[pachi.git] / uct / uct.c
blob15ff24ee09c9facce745af830356ed827fec2070
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 "uct/dynkomi.h"
25 #include "uct/internal.h"
26 #include "uct/prior.h"
27 #include "uct/tree.h"
28 #include "uct/uct.h"
29 #include "uct/walk.h"
31 struct uct_policy *policy_ucb1_init(struct uct *u, char *arg);
32 struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg);
33 static void uct_pondering_stop(struct uct *u);
36 /* Default number of simulations to perform per move.
37 * Note that this is now in total over all threads! (Unless TM_ROOT.) */
38 #define MC_GAMES 80000
39 #define MC_GAMELEN MAX_GAMELEN
40 static const struct time_info default_ti = {
41 .period = TT_MOVE,
42 .dim = TD_GAMES,
43 .len = { .games = MC_GAMES },
46 /* How big proportion of ownermap counts must be of one color to consider
47 * the point sure. */
48 #define GJ_THRES 0.8
49 /* How many games to consider at minimum before judging groups. */
50 #define GJ_MINGAMES 500
52 /* How often to inspect the tree from the main thread to check for playout
53 * stop, progress reports, etc. (in seconds) */
54 #define TREE_BUSYWAIT_INTERVAL 0.1 /* 100ms */
56 /* Once per how many simulations (per thread) to show a progress report line. */
57 #define TREE_SIMPROGRESS_INTERVAL 10000
59 /* When terminating uct_search() early, the safety margin to add to the
60 * remaining playout number estimate when deciding whether the result can
61 * still change. */
62 #define PLAYOUT_DELTA_SAFEMARGIN 1000
65 static void
66 setup_state(struct uct *u, struct board *b, enum stone color)
68 u->t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0, u->local_tree_aging);
69 if (u->force_seed)
70 fast_srandom(u->force_seed);
71 if (UDEBUGL(0))
72 fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed());
73 //board_print(b, stderr);
74 if (!u->no_book && b->moves == 0) {
75 assert(color == S_BLACK);
76 tree_load(u->t, b);
80 static void
81 reset_state(struct uct *u)
83 assert(u->t);
84 tree_done(u->t); u->t = NULL;
87 static void
88 setup_dynkomi(struct uct *u, struct board *b, enum stone to_play)
90 if (u->t->use_extra_komi && u->dynkomi->permove)
91 u->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, u->t);
94 static void
95 prepare_move(struct engine *e, struct board *b, enum stone color)
97 struct uct *u = e->data;
99 if (u->t) {
100 /* Verify that we have sane state. */
101 assert(b->es == u);
102 assert(u->t && b->moves);
103 if (color != stone_other(u->t->root_color)) {
104 fprintf(stderr, "Fatal: Non-alternating play detected %d %d\n",
105 color, u->t->root_color);
106 exit(1);
109 } else {
110 /* We need fresh state. */
111 b->es = u;
112 setup_state(u, b, color);
115 u->ownermap.playouts = 0;
116 memset(u->ownermap.map, 0, board_size2(b) * sizeof(u->ownermap.map[0]));
119 static void
120 dead_group_list(struct uct *u, struct board *b, struct move_queue *mq)
122 struct group_judgement gj;
123 gj.thres = GJ_THRES;
124 gj.gs = alloca(board_size2(b) * sizeof(gj.gs[0]));
125 board_ownermap_judge_group(b, &u->ownermap, &gj);
126 groups_of_status(b, &gj, GS_DEAD, mq);
129 bool
130 uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive)
132 if (u->ownermap.playouts < GJ_MINGAMES)
133 return false;
135 struct move_queue mq = { .moves = 0 };
136 if (!pass_all_alive)
137 dead_group_list(u, b, &mq);
138 return pass_is_safe(b, color, &mq);
142 static void
143 uct_printhook_ownermap(struct board *board, coord_t c, FILE *f)
145 struct uct *u = board->es;
146 assert(u);
147 const char chr[] = ":XO,"; // dame, black, white, unclear
148 const char chm[] = ":xo,";
149 char ch = chr[board_ownermap_judge_point(&u->ownermap, c, GJ_THRES)];
150 if (ch == ',') { // less precise estimate then?
151 ch = chm[board_ownermap_judge_point(&u->ownermap, c, 0.67)];
153 fprintf(f, "%c ", ch);
156 static char *
157 uct_notify_play(struct engine *e, struct board *b, struct move *m)
159 struct uct *u = e->data;
160 if (!u->t) {
161 /* No state, create one - this is probably game beginning
162 * and we need to load the opening book right now. */
163 prepare_move(e, b, m->color);
164 assert(u->t);
167 /* Stop pondering. */
168 /* XXX: If we are about to receive multiple 'play' commands,
169 * e.g. in a rengo, we will not ponder during the rest of them. */
170 uct_pondering_stop(u);
172 if (is_resign(m->coord)) {
173 /* Reset state. */
174 reset_state(u);
175 return NULL;
178 /* Promote node of the appropriate move to the tree root. */
179 assert(u->t->root);
180 if (!tree_promote_at(u->t, b, m->coord)) {
181 if (UDEBUGL(0))
182 fprintf(stderr, "Warning: Cannot promote move node! Several play commands in row?\n");
183 reset_state(u);
184 return NULL;
186 return NULL;
189 static char *
190 uct_chat(struct engine *e, struct board *b, char *cmd)
192 struct uct *u = e->data;
193 static char reply[1024];
195 cmd += strspn(cmd, " \n\t");
196 if (!strncasecmp(cmd, "winrate", 7)) {
197 if (!u->t)
198 return "no game context (yet?)";
199 enum stone color = u->t->root_color;
200 struct tree_node *n = u->t->root;
201 snprintf(reply, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
202 n->u.playouts, u->threads, stone2str(color), coord2sstr(n->coord, b),
203 tree_node_get_value(u->t, -1, n->u.value) * 100);
204 if (u->t->use_extra_komi && abs(u->t->extra_komi) >= 0.5) {
205 sprintf(reply + strlen(reply), ", while self-imposing extra komi %.1f",
206 u->t->extra_komi);
208 strcat(reply, ".");
209 return reply;
211 return NULL;
214 static void
215 uct_dead_group_list(struct engine *e, struct board *b, struct move_queue *mq)
217 struct uct *u = e->data;
219 /* This means the game is probably over, no use pondering on. */
220 uct_pondering_stop(u);
222 if (u->pass_all_alive)
223 return; // no dead groups
225 bool mock_state = false;
227 if (!u->t) {
228 /* No state, but we cannot just back out - we might
229 * have passed earlier, only assuming some stones are
230 * dead, and then re-connected, only to lose counting
231 * when all stones are assumed alive. */
232 /* Mock up some state and seed the ownermap by few
233 * simulations. */
234 prepare_move(e, b, S_BLACK); assert(u->t);
235 for (int i = 0; i < GJ_MINGAMES; i++)
236 uct_playout(u, b, S_BLACK, u->t);
237 mock_state = true;
240 dead_group_list(u, b, mq);
242 if (mock_state) {
243 /* Clean up the mock state in case we will receive
244 * a genmove; we could get a non-alternating-move
245 * error from prepare_move() in that case otherwise. */
246 reset_state(u);
250 static void
251 playout_policy_done(struct playout_policy *p)
253 if (p->done) p->done(p);
254 if (p->data) free(p->data);
255 free(p);
258 static void
259 uct_done(struct engine *e)
261 /* This is called on engine reset, especially when clear_board
262 * is received and new game should begin. */
263 struct uct *u = e->data;
264 uct_pondering_stop(u);
265 if (u->t) reset_state(u);
266 free(u->ownermap.map);
268 free(u->policy);
269 free(u->random_policy);
270 playout_policy_done(u->playout);
271 uct_prior_done(u->prior);
275 /* Pachi threading structure (if uct_playouts_parallel() is used):
277 * main thread
278 * | main(), GTP communication, ...
279 * | starts and stops the search managed by thread_manager
281 * thread_manager
282 * | spawns and collects worker threads
284 * worker0
285 * worker1
286 * ...
287 * workerK
288 * uct_playouts() loop, doing descend-playout until uct_halt
290 * Another way to look at it is by functions (lines denote thread boundaries):
292 * | uct_genmove()
293 * | uct_search() (uct_search_start() .. uct_search_stop())
294 * | -----------------------
295 * | spawn_thread_manager()
296 * | -----------------------
297 * | spawn_worker()
298 * V uct_playouts() */
300 /* Set in thread manager in case the workers should stop. */
301 volatile sig_atomic_t uct_halt = 0;
302 /* ID of the running worker thread. */
303 __thread int thread_id = -1;
304 /* ID of the thread manager. */
305 static pthread_t thread_manager;
306 static bool thread_manager_running;
308 static pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER;
309 static pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER;
310 static volatile int finish_thread;
311 static pthread_mutex_t finish_serializer = PTHREAD_MUTEX_INITIALIZER;
313 struct spawn_ctx {
314 int tid;
315 struct uct *u;
316 struct board *b;
317 enum stone color;
318 struct tree *t;
319 unsigned long seed;
320 int games;
323 static void *
324 spawn_worker(void *ctx_)
326 struct spawn_ctx *ctx = ctx_;
327 /* Setup */
328 fast_srandom(ctx->seed);
329 thread_id = ctx->tid;
330 /* Run */
331 ctx->games = uct_playouts(ctx->u, ctx->b, ctx->color, ctx->t);
332 /* Finish */
333 pthread_mutex_lock(&finish_serializer);
334 pthread_mutex_lock(&finish_mutex);
335 finish_thread = ctx->tid;
336 pthread_cond_signal(&finish_cond);
337 pthread_mutex_unlock(&finish_mutex);
338 return ctx;
341 /* Thread manager, controlling worker threads. It must be called with
342 * finish_mutex lock held, but it will unlock it itself before exiting;
343 * this is necessary to be completely deadlock-free. */
344 /* The finish_cond can be signalled for it to stop; in that case,
345 * the caller should set finish_thread = -1. */
346 /* After it is started, it will update mctx->t to point at some tree
347 * used for the actual search (matters only for TM_ROOT), on return
348 * it will set mctx->games to the number of performed simulations. */
349 static void *
350 spawn_thread_manager(void *ctx_)
352 /* In thread_manager, we use only some of the ctx fields. */
353 struct spawn_ctx *mctx = ctx_;
354 struct uct *u = mctx->u;
355 struct tree *t = mctx->t;
356 bool shared_tree = u->parallel_tree;
357 fast_srandom(mctx->seed);
359 int played_games = 0;
360 pthread_t threads[u->threads];
361 int joined = 0;
363 uct_halt = 0;
365 /* Garbage collect the tree by preference when pondering. */
366 if (u->pondering && t->nodes && t->nodes_size > t->max_tree_size/2) {
367 unsigned long temp_size = (MIN_FREE_MEM_PERCENT * t->max_tree_size) / 100;
368 t->root = tree_garbage_collect(t, temp_size, t->root);
371 /* Spawn threads... */
372 for (int ti = 0; ti < u->threads; ti++) {
373 struct spawn_ctx *ctx = malloc(sizeof(*ctx));
374 ctx->u = u; ctx->b = mctx->b; ctx->color = mctx->color;
375 mctx->t = ctx->t = shared_tree ? t : tree_copy(t);
376 ctx->tid = ti; ctx->seed = fast_random(65536) + ti;
377 pthread_create(&threads[ti], NULL, spawn_worker, ctx);
378 if (UDEBUGL(3))
379 fprintf(stderr, "Spawned worker %d\n", ti);
382 /* ...and collect them back: */
383 while (joined < u->threads) {
384 /* Wait for some thread to finish... */
385 pthread_cond_wait(&finish_cond, &finish_mutex);
386 if (finish_thread < 0) {
387 /* Stop-by-caller. Tell the workers to wrap up. */
388 uct_halt = 1;
389 continue;
391 /* ...and gather its remnants. */
392 struct spawn_ctx *ctx;
393 pthread_join(threads[finish_thread], (void **) &ctx);
394 played_games += ctx->games;
395 joined++;
396 if (!shared_tree) {
397 if (ctx->t == mctx->t) mctx->t = t;
398 tree_merge(t, ctx->t);
399 tree_done(ctx->t);
401 free(ctx);
402 if (UDEBUGL(3))
403 fprintf(stderr, "Joined worker %d\n", finish_thread);
404 pthread_mutex_unlock(&finish_serializer);
407 pthread_mutex_unlock(&finish_mutex);
409 if (!shared_tree)
410 tree_normalize(mctx->t, u->threads);
412 mctx->games = played_games;
413 return mctx;
416 static struct spawn_ctx *
417 uct_search_start(struct uct *u, struct board *b, enum stone color, struct tree *t)
419 assert(u->threads > 0);
420 assert(!thread_manager_running);
422 struct spawn_ctx ctx = { .u = u, .b = b, .color = color, .t = t, .seed = fast_random(65536) };
423 static struct spawn_ctx mctx; mctx = ctx;
424 pthread_mutex_lock(&finish_mutex);
425 pthread_create(&thread_manager, NULL, spawn_thread_manager, &mctx);
426 thread_manager_running = true;
427 return &mctx;
430 static struct spawn_ctx *
431 uct_search_stop(void)
433 assert(thread_manager_running);
435 /* Signal thread manager to stop the workers. */
436 pthread_mutex_lock(&finish_mutex);
437 finish_thread = -1;
438 pthread_cond_signal(&finish_cond);
439 pthread_mutex_unlock(&finish_mutex);
441 /* Collect the thread manager. */
442 struct spawn_ctx *pctx;
443 thread_manager_running = false;
444 pthread_join(thread_manager, (void **) &pctx);
445 return pctx;
449 /* Determine whether we should terminate the search early. */
450 static bool
451 uct_search_stop_early(struct uct *u, struct tree *t, struct board *b,
452 struct time_info *ti, struct time_stop *stop,
453 struct tree_node *best, struct tree_node *best2,
454 int base_playouts, int i)
456 /* Always use at least half the desired time. It is silly
457 * to lose a won game because we played a bad move in 0.1s. */
458 double elapsed = 0;
459 if (ti->dim == TD_WALLTIME) {
460 elapsed = time_now() - ti->len.t.timer_start;
461 if (elapsed < 0.5 * stop->desired.time) return false;
464 /* Early break in won situation. */
465 if (best->u.playouts >= 2000 && tree_node_get_value(t, 1, best->u.value) >= u->loss_threshold)
466 return true;
467 /* Earlier break in super-won situation. */
468 if (best->u.playouts >= 500 && tree_node_get_value(t, 1, best->u.value) >= 0.95)
469 return true;
471 /* Break early if we estimate the second-best move cannot
472 * catch up in assigned time anymore. We use all our time
473 * if we are in byoyomi with single stone remaining in our
474 * period, however - it's better to pre-ponder. */
475 bool time_indulgent = (!ti->len.t.main_time && ti->len.t.byoyomi_stones == 1);
476 if (best2 && ti->dim == TD_WALLTIME && !time_indulgent) {
477 double remaining = stop->worst.time - elapsed;
478 double pps = ((double)i - base_playouts) / elapsed;
479 double estplayouts = remaining * pps + PLAYOUT_DELTA_SAFEMARGIN;
480 if (best->u.playouts > best2->u.playouts + estplayouts) {
481 if (UDEBUGL(2))
482 fprintf(stderr, "Early stop, result cannot change: "
483 "best %d, best2 %d, estimated %f simulations to go\n",
484 best->u.playouts, best2->u.playouts, estplayouts);
485 return true;
489 return false;
492 /* Determine whether we should terminate the search later. */
493 static bool
494 uct_search_keep_looking(struct uct *u, struct tree *t, struct board *b,
495 struct tree_node *best, struct tree_node *best2,
496 struct tree_node *bestr, struct tree_node *winner, int i)
498 if (!best) {
499 if (UDEBUGL(2))
500 fprintf(stderr, "Did not find best move, still trying...\n");
501 return true;
504 if (u->best2_ratio > 0) {
505 /* Check best/best2 simulations ratio. If the
506 * two best moves give very similar results,
507 * keep simulating. */
508 if (best2 && best2->u.playouts
509 && (double)best->u.playouts / best2->u.playouts < u->best2_ratio) {
510 if (UDEBUGL(2))
511 fprintf(stderr, "Best2 ratio %f < threshold %f\n",
512 (double)best->u.playouts / best2->u.playouts,
513 u->best2_ratio);
514 return true;
518 if (u->bestr_ratio > 0) {
519 /* Check best, best_best value difference. If the best move
520 * and its best child do not give similar enough results,
521 * keep simulating. */
522 if (bestr && bestr->u.playouts
523 && fabs((double)best->u.value - bestr->u.value) > u->bestr_ratio) {
524 if (UDEBUGL(2))
525 fprintf(stderr, "Bestr delta %f > threshold %f\n",
526 fabs((double)best->u.value - bestr->u.value),
527 u->bestr_ratio);
528 return true;
532 if (winner && winner != best) {
533 /* Keep simulating if best explored
534 * does not have also highest value. */
535 if (UDEBUGL(2))
536 fprintf(stderr, "[%d] best %3s [%d] %f != winner %3s [%d] %f\n", i,
537 coord2sstr(best->coord, t->board),
538 best->u.playouts, tree_node_get_value(t, 1, best->u.value),
539 coord2sstr(winner->coord, t->board),
540 winner->u.playouts, tree_node_get_value(t, 1, winner->u.value));
541 return true;
544 /* No reason to keep simulating, bye. */
545 return false;
548 /* Run time-limited MCTS search on foreground. */
549 static int
550 uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t)
552 int base_playouts = u->t->root->u.playouts;
553 if (UDEBUGL(2) && base_playouts > 0)
554 fprintf(stderr, "<pre-simulated %d games skipped>\n", base_playouts);
556 /* Set up time conditions. */
557 if (ti->period == TT_NULL) *ti = default_ti;
558 struct time_stop stop;
559 time_stop_conditions(ti, b, u->fuseki_end, u->yose_start, &stop);
561 /* Number of last dynkomi adjustment. */
562 int last_dynkomi = t->root->u.playouts;
563 /* Number of last game with progress print. */
564 int last_print = t->root->u.playouts;
565 /* Number of simulations to wait before next print. */
566 int print_interval = TREE_SIMPROGRESS_INTERVAL * (u->thread_model == TM_ROOT ? 1 : u->threads);
567 /* Printed notification about full memory? */
568 bool print_fullmem = false;
570 struct spawn_ctx *ctx = uct_search_start(u, b, color, t);
572 /* The search tree is ctx->t. This is normally == t, but in case of
573 * TM_ROOT, it is one of the trees belonging to the independent
574 * workers. It is important to reference ctx->t directly since the
575 * thread manager will swap the tree pointer asynchronously. */
576 /* XXX: This means TM_ROOT support is suboptimal since single stalled
577 * thread can stall the others in case of limiting the search by game
578 * count. However, TM_ROOT just does not deserve any more extra code
579 * right now. */
581 struct tree_node *best = NULL;
582 struct tree_node *best2 = NULL; // Second-best move.
583 struct tree_node *bestr = NULL; // best's best child.
584 struct tree_node *winner = NULL;
586 double busywait_interval = TREE_BUSYWAIT_INTERVAL;
588 /* Now, just periodically poll the search tree. */
589 while (1) {
590 time_sleep(busywait_interval);
591 /* busywait_interval should never be less than desired time, or the
592 * time control is broken. But if it happens to be less, we still search
593 * at least 100ms otherwise the move is completely random. */
595 int i = ctx->t->root->u.playouts;
597 /* Adjust dynkomi? */
598 if (ctx->t->use_extra_komi && u->dynkomi->permove
599 && u->dynkomi_interval
600 && i > last_dynkomi + u->dynkomi_interval) {
601 float old_dynkomi = ctx->t->extra_komi;
602 ctx->t->extra_komi = u->dynkomi->permove(u->dynkomi, b, ctx->t);
603 if (UDEBUGL(3) && old_dynkomi != ctx->t->extra_komi)
604 fprintf(stderr, "dynkomi adjusted (%f -> %f)\n", old_dynkomi, ctx->t->extra_komi);
607 /* Print progress? */
608 if (i - last_print > print_interval) {
609 last_print += print_interval; // keep the numbers tidy
610 uct_progress_status(u, ctx->t, color, last_print);
612 if (!print_fullmem && ctx->t->nodes_size > u->max_tree_size) {
613 if (UDEBUGL(2))
614 fprintf(stderr, "memory limit hit (%lu > %lu)\n", ctx->t->nodes_size, u->max_tree_size);
615 print_fullmem = true;
618 /* Never consider stopping if we played too few simulations.
619 * Maybe we risk losing on time when playing in super-extreme
620 * time pressure but the tree is going to be just too messed
621 * up otherwise - we might even play invalid suicides or pass
622 * when we mustn't. */
623 if (i < GJ_MINGAMES)
624 continue;
626 best = u->policy->choose(u->policy, ctx->t->root, b, color, resign);
627 if (best) best2 = u->policy->choose(u->policy, ctx->t->root, b, color, best->coord);
629 /* Possibly stop search early if it's no use to try on. */
630 if (best && uct_search_stop_early(u, ctx->t, b, ti, &stop, best, best2, base_playouts, i))
631 break;
633 /* Check against time settings. */
634 bool desired_done = false;
635 if (ti->dim == TD_WALLTIME) {
636 double elapsed = time_now() - ti->len.t.timer_start;
637 if (elapsed > stop.worst.time) break;
638 desired_done = elapsed > stop.desired.time;
640 } else { assert(ti->dim == TD_GAMES);
641 if (i > stop.worst.playouts) break;
642 desired_done = i > stop.desired.playouts;
645 /* We want to stop simulating, but are willing to keep trying
646 * if we aren't completely sure about the winner yet. */
647 if (desired_done) {
648 if (u->policy->winner && u->policy->evaluate) {
649 struct uct_descent descent = { .node = ctx->t->root };
650 u->policy->winner(u->policy, ctx->t, &descent);
651 winner = descent.node;
653 if (best)
654 bestr = u->policy->choose(u->policy, best, b, stone_other(color), resign);
655 if (!uct_search_keep_looking(u, ctx->t, b, best, best2, bestr, winner, i))
656 break;
659 /* TODO: Early break if best->variance goes under threshold and we already
660 * have enough playouts (possibly thanks to book or to pondering)? */
663 ctx = uct_search_stop();
665 if (UDEBUGL(2))
666 tree_dump(t, u->dumpthres);
667 if (UDEBUGL(0))
668 uct_progress_status(u, t, color, ctx->games);
670 return ctx->games;
674 /* Start pondering background with @color to play. */
675 static void
676 uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color)
678 if (UDEBUGL(1))
679 fprintf(stderr, "Starting to ponder with color %s\n", stone2str(stone_other(color)));
680 u->pondering = true;
682 /* We need a local board copy to ponder upon. */
683 struct board *b = malloc(sizeof(*b)); board_copy(b, b0);
685 /* *b0 did not have the genmove'd move played yet. */
686 struct move m = { t->root->coord, t->root_color };
687 int res = board_play(b, &m);
688 assert(res >= 0);
689 setup_dynkomi(u, b, stone_other(m.color));
691 /* Start MCTS manager thread "headless". */
692 uct_search_start(u, b, color, t);
695 /* uct_search_stop() frontend for the pondering (non-genmove) mode. */
696 static void
697 uct_pondering_stop(struct uct *u)
699 u->pondering = false;
700 if (!thread_manager_running)
701 return;
703 /* Stop the thread manager. */
704 struct spawn_ctx *ctx = uct_search_stop();
705 if (UDEBUGL(1)) {
706 fprintf(stderr, "(pondering) ");
707 uct_progress_status(u, ctx->t, ctx->color, ctx->games);
709 free(ctx->b);
713 static coord_t *
714 uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
716 double start_time = time_now();
717 struct uct *u = e->data;
719 if (b->superko_violation) {
720 fprintf(stderr, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
721 fprintf(stderr, "Maybe you play with situational instead of positional superko?\n");
722 fprintf(stderr, "I'm going to ignore the violation, but note that I may miss\n");
723 fprintf(stderr, "some moves valid under this ruleset because of this.\n");
724 b->superko_violation = false;
727 /* Seed the tree. */
728 uct_pondering_stop(u);
729 prepare_move(e, b, color);
730 assert(u->t);
732 /* How to decide whether to use dynkomi in this game? Since we use
733 * pondering, it's not simple "who-to-play" matter. Decide based on
734 * the last genmove issued. */
735 u->t->use_extra_komi = !!(u->dynkomi_mask & color);
736 setup_dynkomi(u, b, color);
738 /* Make pessimistic assumption about komi for Japanese rules to
739 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
740 * The rules usually give the same winner if the integer part of komi
741 * is odd so we adjust the komi only if it is even (for a board of
742 * odd size). We are not trying to get an exact evaluation for rare
743 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html
744 * TODO: Support the kgs-rules command once available. */
745 if (u->territory_scoring && (((int)floor(b->komi) + board_size(b)) & 1)) {
746 b->komi += (color == S_BLACK ? 1.0 : -1.0);
747 if (UDEBUGL(0))
748 fprintf(stderr, "Setting komi to %.1f assuming Japanese rules\n",
749 b->komi);
752 int base_playouts = u->t->root->u.playouts;
753 /* Perform the Monte Carlo Tree Search! */
754 int played_games = uct_search(u, b, ti, color, u->t);
756 /* Choose the best move from the tree. */
757 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color, resign);
758 if (!best) {
759 reset_state(u);
760 return coord_copy(pass);
762 if (UDEBUGL(1))
763 fprintf(stderr, "*** WINNER is %s (%d,%d) with score %1.4f (%d/%d:%d/%d games), extra komi %f\n",
764 coord2sstr(best->coord, b), coord_x(best->coord, b), coord_y(best->coord, b),
765 tree_node_get_value(u->t, 1, best->u.value), best->u.playouts,
766 u->t->root->u.playouts, u->t->root->u.playouts - base_playouts, played_games,
767 u->t->extra_komi);
769 /* Do not resign if we're so short of time that evaluation of best
770 * move is completely unreliable, we might be winning actually.
771 * In this case best is almost random but still better than resign.
772 * Also do not resign if we are getting bad results while actually
773 * giving away extra komi points (dynkomi). */
774 if (tree_node_get_value(u->t, 1, best->u.value) < u->resign_ratio
775 && !is_pass(best->coord) && best->u.playouts > GJ_MINGAMES
776 && u->t->extra_komi <= 1 /* XXX we assume dynamic komi == we are black */) {
777 reset_state(u);
778 return coord_copy(resign);
781 /* If the opponent just passed and we win counting, always
782 * pass as well. */
783 if (b->moves > 1 && is_pass(b->last_move.coord)) {
784 /* Make sure enough playouts are simulated. */
785 while (u->ownermap.playouts < GJ_MINGAMES)
786 uct_playout(u, b, color, u->t);
787 if (uct_pass_is_safe(u, b, color, u->pass_all_alive || pass_all_alive)) {
788 if (UDEBUGL(0))
789 fprintf(stderr, "<Will rather pass, looks safe enough.>\n");
790 best->coord = pass;
794 tree_promote_node(u->t, &best);
795 /* After a pass, pondering is harmful for two reasons:
796 * (i) We might keep pondering even when the game is over.
797 * Of course this is the case for opponent resign as well.
798 * (ii) More importantly, the ownermap will get skewed since
799 * the UCT will start cutting off any playouts. */
800 if (u->pondering_opt && !is_pass(best->coord)) {
801 uct_pondering_start(u, b, u->t, stone_other(color));
803 if (UDEBUGL(2)) {
804 double time = time_now() - start_time + 0.000001; /* avoid divide by zero */
805 fprintf(stderr, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
806 time, (int)(played_games/time), (int)(played_games/time/u->threads));
808 return coord_copy(best->coord);
812 bool
813 uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color)
815 struct uct *u = e->data;
816 if (!u->t) prepare_move(e, b, color);
817 assert(u->t);
819 if (ti->dim == TD_GAMES) {
820 /* Don't count in games that already went into the book. */
821 ti->len.games += u->t->root->u.playouts;
823 uct_search(u, b, ti, color, u->t);
825 assert(ti->dim == TD_GAMES);
826 tree_save(u->t, b, ti->len.games / 100);
828 return true;
831 void
832 uct_dumpbook(struct engine *e, struct board *b, enum stone color)
834 struct uct *u = e->data;
835 struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0, u->local_tree_aging);
836 tree_load(t, b);
837 tree_dump(t, 0);
838 tree_done(t);
842 struct uct *
843 uct_state_init(char *arg, struct board *b)
845 struct uct *u = calloc(1, sizeof(struct uct));
846 bool using_elo = false;
848 u->debug_level = debug_level;
849 u->gamelen = MC_GAMELEN;
850 u->mercymin = 0;
851 u->expand_p = 2;
852 u->dumpthres = 1000;
853 u->playout_amaf = true;
854 u->playout_amaf_nakade = false;
855 u->amaf_prior = false;
856 u->max_tree_size = 3072ULL * 1048576;
858 u->dynkomi_mask = S_BLACK;
860 u->threads = 1;
861 u->thread_model = TM_TREEVL;
862 u->parallel_tree = true;
863 u->virtual_loss = true;
865 u->fuseki_end = 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
866 u->yose_start = 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
867 u->bestr_ratio = 0.02;
868 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
869 // TODO: Further tuning and experiments with better time allocation schemes.
870 u->best2_ratio = 2.5;
872 u->val_scale = 0.04; u->val_points = 40;
874 u->tenuki_d = 4;
875 u->local_tree_aging = 2;
877 if (arg) {
878 char *optspec, *next = arg;
879 while (*next) {
880 optspec = next;
881 next += strcspn(next, ",");
882 if (*next) { *next++ = 0; } else { *next = 0; }
884 char *optname = optspec;
885 char *optval = strchr(optspec, '=');
886 if (optval) *optval++ = 0;
888 if (!strcasecmp(optname, "debug")) {
889 if (optval)
890 u->debug_level = atoi(optval);
891 else
892 u->debug_level++;
893 } else if (!strcasecmp(optname, "mercy") && optval) {
894 /* Minimal difference of black/white captures
895 * to stop playout - "Mercy Rule". Speeds up
896 * hopeless playouts at the expense of some
897 * accuracy. */
898 u->mercymin = atoi(optval);
899 } else if (!strcasecmp(optname, "gamelen") && optval) {
900 u->gamelen = atoi(optval);
901 } else if (!strcasecmp(optname, "expand_p") && optval) {
902 u->expand_p = atoi(optval);
903 } else if (!strcasecmp(optname, "dumpthres") && optval) {
904 u->dumpthres = atoi(optval);
905 } else if (!strcasecmp(optname, "best2_ratio") && optval) {
906 /* If set, prolong simulating while
907 * first_best/second_best playouts ratio
908 * is less than best2_ratio. */
909 u->best2_ratio = atof(optval);
910 } else if (!strcasecmp(optname, "bestr_ratio") && optval) {
911 /* If set, prolong simulating while
912 * best,best_best_child values delta
913 * is more than bestr_ratio. */
914 u->bestr_ratio = atof(optval);
915 } else if (!strcasecmp(optname, "playout_amaf")) {
916 /* Whether to include random playout moves in
917 * AMAF as well. (Otherwise, only tree moves
918 * are included in AMAF. Of course makes sense
919 * only in connection with an AMAF policy.) */
920 /* with-without: 55.5% (+-4.1) */
921 if (optval && *optval == '0')
922 u->playout_amaf = false;
923 else
924 u->playout_amaf = true;
925 } else if (!strcasecmp(optname, "playout_amaf_nakade")) {
926 /* Whether to include nakade moves from playouts
927 * in the AMAF statistics; this tends to nullify
928 * the playout_amaf effect by adding too much
929 * noise. */
930 if (optval && *optval == '0')
931 u->playout_amaf_nakade = false;
932 else
933 u->playout_amaf_nakade = true;
934 } else if (!strcasecmp(optname, "playout_amaf_cutoff") && optval) {
935 /* Keep only first N% of playout stage AMAF
936 * information. */
937 u->playout_amaf_cutoff = atoi(optval);
938 } else if ((!strcasecmp(optname, "policy") || !strcasecmp(optname, "random_policy")) && optval) {
939 char *policyarg = strchr(optval, ':');
940 struct uct_policy **p = !strcasecmp(optname, "policy") ? &u->policy : &u->random_policy;
941 if (policyarg)
942 *policyarg++ = 0;
943 if (!strcasecmp(optval, "ucb1")) {
944 *p = policy_ucb1_init(u, policyarg);
945 } else if (!strcasecmp(optval, "ucb1amaf")) {
946 *p = policy_ucb1amaf_init(u, policyarg);
947 } else {
948 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
949 exit(1);
951 } else if (!strcasecmp(optname, "playout") && optval) {
952 char *playoutarg = strchr(optval, ':');
953 if (playoutarg)
954 *playoutarg++ = 0;
955 if (!strcasecmp(optval, "moggy")) {
956 u->playout = playout_moggy_init(playoutarg, b);
957 } else if (!strcasecmp(optval, "light")) {
958 u->playout = playout_light_init(playoutarg, b);
959 } else if (!strcasecmp(optval, "elo")) {
960 u->playout = playout_elo_init(playoutarg, b);
961 using_elo = true;
962 } else {
963 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
964 exit(1);
966 } else if (!strcasecmp(optname, "prior") && optval) {
967 u->prior = uct_prior_init(optval, b);
968 } else if (!strcasecmp(optname, "amaf_prior") && optval) {
969 u->amaf_prior = atoi(optval);
970 } else if (!strcasecmp(optname, "threads") && optval) {
971 /* By default, Pachi will run with only single
972 * tree search thread! */
973 u->threads = atoi(optval);
974 } else if (!strcasecmp(optname, "thread_model") && optval) {
975 if (!strcasecmp(optval, "root")) {
976 /* Root parallelization - each thread
977 * does independent search, trees are
978 * merged at the end. */
979 u->thread_model = TM_ROOT;
980 u->parallel_tree = false;
981 u->virtual_loss = false;
982 } else if (!strcasecmp(optval, "tree")) {
983 /* Tree parallelization - all threads
984 * grind on the same tree. */
985 u->thread_model = TM_TREE;
986 u->parallel_tree = true;
987 u->virtual_loss = false;
988 } else if (!strcasecmp(optval, "treevl")) {
989 /* Tree parallelization, but also
990 * with virtual losses - this discou-
991 * rages most threads choosing the
992 * same tree branches to read. */
993 u->thread_model = TM_TREEVL;
994 u->parallel_tree = true;
995 u->virtual_loss = true;
996 } else {
997 fprintf(stderr, "UCT: Invalid thread model %s\n", optval);
998 exit(1);
1000 } else if (!strcasecmp(optname, "pondering")) {
1001 /* Keep searching even during opponent's turn. */
1002 u->pondering_opt = !optval || atoi(optval);
1003 } else if (!strcasecmp(optname, "fuseki_end") && optval) {
1004 /* At the very beginning it's not worth thinking
1005 * too long because the playout evaluations are
1006 * very noisy. So gradually increase the thinking
1007 * time up to maximum when fuseki_end percent
1008 * of the board has been played.
1009 * This only applies if we are not in byoyomi. */
1010 u->fuseki_end = atoi(optval);
1011 } else if (!strcasecmp(optname, "yose_start") && optval) {
1012 /* When yose_start percent of the board has been
1013 * played, or if we are in byoyomi, stop spending
1014 * more time and spread the remaining time
1015 * uniformly.
1016 * Between fuseki_end and yose_start, we spend
1017 * a constant proportion of the remaining time
1018 * on each move. (yose_start should actually
1019 * be much earlier than when real yose start,
1020 * but "yose" is a good short name to convey
1021 * the idea.) */
1022 u->yose_start = atoi(optval);
1023 } else if (!strcasecmp(optname, "force_seed") && optval) {
1024 u->force_seed = atoi(optval);
1025 } else if (!strcasecmp(optname, "no_book")) {
1026 u->no_book = true;
1027 } else if (!strcasecmp(optname, "dynkomi") && optval) {
1028 /* Dynamic komi approach; there are multiple
1029 * ways to adjust komi dynamically throughout
1030 * play. We currently support two: */
1031 char *dynkomiarg = strchr(optval, ':');
1032 if (dynkomiarg)
1033 *dynkomiarg++ = 0;
1034 if (!strcasecmp(optval, "none")) {
1035 u->dynkomi = uct_dynkomi_init_none(u, dynkomiarg, b);
1036 } else if (!strcasecmp(optval, "linear")) {
1037 u->dynkomi = uct_dynkomi_init_linear(u, dynkomiarg, b);
1038 } else if (!strcasecmp(optval, "adaptive")) {
1039 u->dynkomi = uct_dynkomi_init_adaptive(u, dynkomiarg, b);
1040 } else {
1041 fprintf(stderr, "UCT: Invalid dynkomi mode %s\n", optval);
1042 exit(1);
1044 } else if (!strcasecmp(optname, "dynkomi_mask") && optval) {
1045 /* Bitmask of colors the player must be
1046 * for dynkomi be applied; you may want
1047 * to use dynkomi_mask=3 to allow dynkomi
1048 * even in games where Pachi is white. */
1049 u->dynkomi_mask = atoi(optval);
1050 } else if (!strcasecmp(optname, "dynkomi_interval") && optval) {
1051 /* If non-zero, re-adjust dynamic komi
1052 * throughout a single genmove reading,
1053 * roughly every N simulations. */
1054 u->dynkomi_interval = atoi(optval);
1055 } else if (!strcasecmp(optname, "val_scale") && optval) {
1056 /* How much of the game result value should be
1057 * influenced by win size. Zero means it isn't. */
1058 u->val_scale = atof(optval);
1059 } else if (!strcasecmp(optname, "val_points") && optval) {
1060 /* Maximum size of win to be scaled into game
1061 * result value. Zero means boardsize^2. */
1062 u->val_points = atoi(optval) * 2; // result values are doubled
1063 } else if (!strcasecmp(optname, "val_extra")) {
1064 /* If false, the score coefficient will be simply
1065 * added to the value, instead of scaling the result
1066 * coefficient because of it. */
1067 u->val_extra = !optval || atoi(optval);
1068 } else if (!strcasecmp(optname, "local_tree") && optval) {
1069 /* Whether to bias exploration by local tree values
1070 * (must be supported by the used policy).
1071 * 0: Don't.
1072 * 1: Do, value = result.
1073 * Try to temper the result:
1074 * 2: Do, value = 0.5+(result-expected)/2.
1075 * 3: Do, value = 0.5+bzz((result-expected)^2).
1076 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
1077 u->local_tree = atoi(optval);
1078 } else if (!strcasecmp(optname, "tenuki_d") && optval) {
1079 /* Tenuki distance at which to break the local tree. */
1080 u->tenuki_d = atoi(optval);
1081 if (u->tenuki_d > TREE_NODE_D_MAX + 1) {
1082 fprintf(stderr, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX + 1);
1083 exit(1);
1085 } else if (!strcasecmp(optname, "local_tree_aging") && optval) {
1086 /* How much to reduce local tree values between moves. */
1087 u->local_tree_aging = atof(optval);
1088 } else if (!strcasecmp(optname, "local_tree_allseq")) {
1089 /* By default, only complete sequences are stored
1090 * in the local tree. If this is on, also
1091 * subsequences starting at each move are stored. */
1092 u->local_tree_allseq = !optval || atoi(optval);
1093 } else if (!strcasecmp(optname, "local_tree_playout")) {
1094 /* Whether to adjust ELO playout probability
1095 * distributions according to matched localtree
1096 * information. */
1097 u->local_tree_playout = !optval || atoi(optval);
1098 } else if (!strcasecmp(optname, "local_tree_pseqroot")) {
1099 /* By default, when we have no sequence move
1100 * to suggest in-playout, we give up. If this
1101 * is on, we make probability distribution from
1102 * sequences first moves instead. */
1103 u->local_tree_pseqroot = !optval || atoi(optval);
1104 } else if (!strcasecmp(optname, "pass_all_alive")) {
1105 /* Whether to consider all stones alive at the game
1106 * end instead of marking dead groupd. */
1107 u->pass_all_alive = !optval || atoi(optval);
1108 } else if (!strcasecmp(optname, "territory_scoring")) {
1109 /* Use territory scoring (default is area scoring).
1110 * An explicit kgs-rules command overrides this. */
1111 u->territory_scoring = !optval || atoi(optval);
1112 } else if (!strcasecmp(optname, "random_policy_chance") && optval) {
1113 /* If specified (N), with probability 1/N, random_policy policy
1114 * descend is used instead of main policy descend; useful
1115 * if specified policy (e.g. UCB1AMAF) can make unduly biased
1116 * choices sometimes, you can fall back to e.g.
1117 * random_policy=UCB1. */
1118 u->random_policy_chance = atoi(optval);
1119 } else if (!strcasecmp(optname, "max_tree_size") && optval) {
1120 /* Maximum amount of memory [MiB] consumed by the move tree.
1121 * For fast_alloc it includes the temp tree used for pruning.
1122 * Default is 3072 (3 GiB). Note that if you use TM_ROOT,
1123 * this limits size of only one of the trees, not all of them
1124 * together. */
1125 u->max_tree_size = atol(optval) * 1048576;
1126 } else if (!strcasecmp(optname, "fast_alloc")) {
1127 u->fast_alloc = !optval || atoi(optval);
1128 } else if (!strcasecmp(optname, "banner") && optval) {
1129 /* Additional banner string. This must come as the
1130 * last engine parameter. */
1131 if (*next) *--next = ',';
1132 u->banner = strdup(optval);
1133 break;
1134 } else {
1135 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
1136 exit(1);
1141 u->resign_ratio = 0.2; /* Resign when most games are lost. */
1142 u->loss_threshold = 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
1143 if (!u->policy)
1144 u->policy = policy_ucb1amaf_init(u, NULL);
1146 if (!!u->random_policy_chance ^ !!u->random_policy) {
1147 fprintf(stderr, "uct: Only one of random_policy and random_policy_chance is set\n");
1148 exit(1);
1151 if (!u->local_tree) {
1152 /* No ltree aging. */
1153 u->local_tree_aging = 1.0f;
1155 if (!using_elo)
1156 u->local_tree_playout = false;
1158 if (u->fast_alloc && !u->parallel_tree) {
1159 fprintf(stderr, "fast_alloc not supported with root parallelization.\n");
1160 exit(1);
1162 if (u->fast_alloc)
1163 u->max_tree_size = (100ULL * u->max_tree_size) / (100 + MIN_FREE_MEM_PERCENT);
1165 if (!u->prior)
1166 u->prior = uct_prior_init(NULL, b);
1168 if (!u->playout)
1169 u->playout = playout_moggy_init(NULL, b);
1170 u->playout->debug_level = u->debug_level;
1172 u->ownermap.map = malloc(board_size2(b) * sizeof(u->ownermap.map[0]));
1174 if (!u->dynkomi)
1175 u->dynkomi = uct_dynkomi_init_linear(u, NULL, b);
1177 /* Some things remain uninitialized for now - the opening book
1178 * is not loaded and the tree not set up. */
1179 /* This will be initialized in setup_state() at the first move
1180 * received/requested. This is because right now we are not aware
1181 * about any komi or handicap setup and such. */
1183 return u;
1186 struct engine *
1187 engine_uct_init(char *arg, struct board *b)
1189 struct uct *u = uct_state_init(arg, b);
1190 struct engine *e = calloc(1, sizeof(struct engine));
1191 e->name = "UCT Engine";
1192 e->printhook = uct_printhook_ownermap;
1193 e->notify_play = uct_notify_play;
1194 e->chat = uct_chat;
1195 e->genmove = uct_genmove;
1196 e->dead_group_list = uct_dead_group_list;
1197 e->done = uct_done;
1198 e->data = u;
1200 const char banner[] = "I'm playing UCT. When I'm losing, I will resign, "
1201 "if I think I win, I play until you pass. "
1202 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1203 if (!u->banner) u->banner = "";
1204 e->comment = malloc(sizeof(banner) + strlen(u->banner) + 1);
1205 sprintf(e->comment, "%s %s", banner, u->banner);
1207 return e;