UCT: bestr_ratio 0.02 by default, best2_ratio 2.5 by default
[pachi.git] / uct / uct.c
blobe063c882a14b8767621611163291b70d61917246
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 "timeinfo.h"
23 #include "tactics.h"
24 #include "uct/internal.h"
25 #include "uct/prior.h"
26 #include "uct/tree.h"
27 #include "uct/uct.h"
28 #include "uct/walk.h"
30 struct uct_policy *policy_ucb1_init(struct uct *u, char *arg);
31 struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg);
32 static void uct_pondering_stop(struct uct *u);
35 /* Default number of simulations to perform per move.
36 * Note that this is now in total over all threads! (Unless TM_ROOT.) */
37 #define MC_GAMES 80000
38 #define MC_GAMELEN MAX_GAMELEN
39 static const struct time_info default_ti = {
40 .period = TT_MOVE,
41 .dim = TD_GAMES,
42 .len = { .games = MC_GAMES },
45 /* How big proportion of ownermap counts must be of one color to consider
46 * the point sure. */
47 #define GJ_THRES 0.8
48 /* How many games to consider at minimum before judging groups. */
49 #define GJ_MINGAMES 500
51 /* How often to inspect the tree from the main thread to check for playout
52 * stop, progress reports, etc. (in seconds) */
53 #define TREE_BUSYWAIT_INTERVAL 0.1 /* 100ms */
55 /* Once per how many simulations (per thread) to show a progress report line. */
56 #define TREE_SIMPROGRESS_INTERVAL 10000
58 /* When terminating uct_search() early, the safety margin to add to the
59 * remaining playout number estimate when deciding whether the result can
60 * still change. */
61 #define PLAYOUT_DELTA_SAFEMARGIN 1000
64 static void
65 setup_state(struct uct *u, struct board *b, enum stone color)
67 u->t = tree_init(b, color, u->fast_alloc ? u->max_tree_size: 0);
68 if (u->force_seed)
69 fast_srandom(u->force_seed);
70 if (UDEBUGL(0))
71 fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed());
72 //board_print(b, stderr);
73 if (!u->no_book && b->moves == 0) {
74 assert(color == S_BLACK);
75 tree_load(u->t, b);
79 static void
80 reset_state(struct uct *u)
82 assert(u->t);
83 tree_done(u->t); u->t = NULL;
86 static void
87 setup_dynkomi(struct uct *u, struct board *b, enum stone to_play)
89 if (u->dynkomi > b->moves && u->t->use_extra_komi)
90 u->t->extra_komi = uct_get_extra_komi(u, b);
91 else
92 u->t->extra_komi = 0;
95 static void
96 prepare_move(struct engine *e, struct board *b, enum stone color)
98 struct uct *u = e->data;
100 if (u->t) {
101 /* Verify that we have sane state. */
102 assert(b->es == u);
103 assert(u->t && b->moves);
104 if (color != stone_other(u->t->root_color)) {
105 fprintf(stderr, "Fatal: Non-alternating play detected %d %d\n",
106 color, u->t->root_color);
107 exit(1);
110 } else {
111 /* We need fresh state. */
112 b->es = u;
113 setup_state(u, b, color);
116 u->ownermap.playouts = 0;
117 memset(u->ownermap.map, 0, board_size2(b) * sizeof(u->ownermap.map[0]));
120 static void
121 dead_group_list(struct uct *u, struct board *b, struct move_queue *mq)
123 struct group_judgement gj;
124 gj.thres = GJ_THRES;
125 gj.gs = alloca(board_size2(b) * sizeof(gj.gs[0]));
126 board_ownermap_judge_group(b, &u->ownermap, &gj);
127 groups_of_status(b, &gj, GS_DEAD, mq);
130 bool
131 uct_pass_is_safe(struct uct *u, struct board *b, enum stone color, bool pass_all_alive)
133 if (u->ownermap.playouts < GJ_MINGAMES)
134 return false;
136 struct move_queue mq = { .moves = 0 };
137 if (!pass_all_alive)
138 dead_group_list(u, b, &mq);
139 return pass_is_safe(b, color, &mq);
143 static void
144 uct_printhook_ownermap(struct board *board, coord_t c, FILE *f)
146 struct uct *u = board->es;
147 assert(u);
148 const char chr[] = ":XO,"; // dame, black, white, unclear
149 const char chm[] = ":xo,";
150 char ch = chr[board_ownermap_judge_point(&u->ownermap, c, GJ_THRES)];
151 if (ch == ',') { // less precise estimate then?
152 ch = chm[board_ownermap_judge_point(&u->ownermap, c, 0.67)];
154 fprintf(f, "%c ", ch);
157 static char *
158 uct_notify_play(struct engine *e, struct board *b, struct move *m)
160 struct uct *u = e->data;
161 if (!u->t) {
162 /* No state, create one - this is probably game beginning
163 * and we need to load the opening book right now. */
164 prepare_move(e, b, m->color);
165 assert(u->t);
168 /* Stop pondering. */
169 /* XXX: If we are about to receive multiple 'play' commands,
170 * e.g. in a rengo, we will not ponder during the rest of them. */
171 uct_pondering_stop(u);
173 if (is_resign(m->coord)) {
174 /* Reset state. */
175 reset_state(u);
176 return NULL;
179 /* Promote node of the appropriate move to the tree root. */
180 assert(u->t->root);
181 if (!tree_promote_at(u->t, b, m->coord)) {
182 if (UDEBUGL(0))
183 fprintf(stderr, "Warning: Cannot promote move node! Several play commands in row?\n");
184 reset_state(u);
185 return NULL;
187 /* Setting up dynkomi is not necessary here, probably, but we
188 * better do it anyway for consistency reasons. */
189 setup_dynkomi(u, b, stone_other(m->color));
190 return NULL;
193 static char *
194 uct_chat(struct engine *e, struct board *b, char *cmd)
196 struct uct *u = e->data;
197 static char reply[1024];
199 cmd += strspn(cmd, " \n\t");
200 if (!strncasecmp(cmd, "winrate", 7)) {
201 if (!u->t)
202 return "no game context (yet?)";
203 enum stone color = u->t->root_color;
204 struct tree_node *n = u->t->root;
205 snprintf(reply, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
206 n->u.playouts, u->threads, stone2str(color), coord2sstr(n->coord, b),
207 tree_node_get_value(u->t, -1, n->u.value) * 100);
208 if (u->t->use_extra_komi && abs(u->t->extra_komi) >= 0.5) {
209 sprintf(reply + strlen(reply), ", while self-imposing extra komi %.1f",
210 u->t->extra_komi);
212 strcat(reply, ".");
213 return reply;
215 return NULL;
218 static void
219 uct_dead_group_list(struct engine *e, struct board *b, struct move_queue *mq)
221 struct uct *u = e->data;
223 /* This means the game is probably over, no use pondering on. */
224 uct_pondering_stop(u);
226 if (u->pass_all_alive)
227 return; // no dead groups
229 bool mock_state = false;
231 if (!u->t) {
232 /* No state, but we cannot just back out - we might
233 * have passed earlier, only assuming some stones are
234 * dead, and then re-connected, only to lose counting
235 * when all stones are assumed alive. */
236 /* Mock up some state and seed the ownermap by few
237 * simulations. */
238 prepare_move(e, b, S_BLACK); assert(u->t);
239 for (int i = 0; i < GJ_MINGAMES; i++)
240 uct_playout(u, b, S_BLACK, u->t);
241 mock_state = true;
244 dead_group_list(u, b, mq);
246 if (mock_state) {
247 /* Clean up the mock state in case we will receive
248 * a genmove; we could get a non-alternating-move
249 * error from prepare_move() in that case otherwise. */
250 reset_state(u);
254 static void
255 playout_policy_done(struct playout_policy *p)
257 if (p->done) p->done(p);
258 if (p->data) free(p->data);
259 free(p);
262 static void
263 uct_done(struct engine *e)
265 /* This is called on engine reset, especially when clear_board
266 * is received and new game should begin. */
267 struct uct *u = e->data;
268 uct_pondering_stop(u);
269 if (u->t) reset_state(u);
270 free(u->ownermap.map);
272 free(u->policy);
273 free(u->random_policy);
274 playout_policy_done(u->playout);
275 uct_prior_done(u->prior);
279 /* Pachi threading structure (if uct_playouts_parallel() is used):
281 * main thread
282 * | main(), GTP communication, ...
283 * | starts and stops the search managed by thread_manager
285 * thread_manager
286 * | spawns and collects worker threads
288 * worker0
289 * worker1
290 * ...
291 * workerK
292 * uct_playouts() loop, doing descend-playout until uct_halt
294 * Another way to look at it is by functions (lines denote thread boundaries):
296 * | uct_genmove()
297 * | uct_search() (uct_search_start() .. uct_search_stop())
298 * | -----------------------
299 * | spawn_thread_manager()
300 * | -----------------------
301 * | spawn_worker()
302 * V uct_playouts() */
304 /* Set in thread manager in case the workers should stop. */
305 volatile sig_atomic_t uct_halt = 0;
306 /* ID of the running worker thread. */
307 __thread int thread_id = -1;
308 /* ID of the thread manager. */
309 static pthread_t thread_manager;
310 static bool thread_manager_running;
312 static pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER;
313 static pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER;
314 static volatile int finish_thread;
315 static pthread_mutex_t finish_serializer = PTHREAD_MUTEX_INITIALIZER;
317 struct spawn_ctx {
318 int tid;
319 struct uct *u;
320 struct board *b;
321 enum stone color;
322 struct tree *t;
323 unsigned long seed;
324 int games;
327 static void *
328 spawn_worker(void *ctx_)
330 struct spawn_ctx *ctx = ctx_;
331 /* Setup */
332 fast_srandom(ctx->seed);
333 thread_id = ctx->tid;
334 /* Run */
335 ctx->games = uct_playouts(ctx->u, ctx->b, ctx->color, ctx->t);
336 /* Finish */
337 pthread_mutex_lock(&finish_serializer);
338 pthread_mutex_lock(&finish_mutex);
339 finish_thread = ctx->tid;
340 pthread_cond_signal(&finish_cond);
341 pthread_mutex_unlock(&finish_mutex);
342 return ctx;
345 /* Thread manager, controlling worker threads. It must be called with
346 * finish_mutex lock held, but it will unlock it itself before exiting;
347 * this is necessary to be completely deadlock-free. */
348 /* The finish_cond can be signalled for it to stop; in that case,
349 * the caller should set finish_thread = -1. */
350 /* After it is started, it will update mctx->t to point at some tree
351 * used for the actual search (matters only for TM_ROOT), on return
352 * it will set mctx->games to the number of performed simulations. */
353 static void *
354 spawn_thread_manager(void *ctx_)
356 /* In thread_manager, we use only some of the ctx fields. */
357 struct spawn_ctx *mctx = ctx_;
358 struct uct *u = mctx->u;
359 struct tree *t = mctx->t;
360 bool shared_tree = u->parallel_tree;
361 fast_srandom(mctx->seed);
363 int played_games = 0;
364 pthread_t threads[u->threads];
365 int joined = 0;
367 uct_halt = 0;
369 /* Spawn threads... */
370 for (int ti = 0; ti < u->threads; ti++) {
371 struct spawn_ctx *ctx = malloc(sizeof(*ctx));
372 ctx->u = u; ctx->b = mctx->b; ctx->color = mctx->color;
373 mctx->t = ctx->t = shared_tree ? t : tree_copy(t);
374 ctx->tid = ti; ctx->seed = fast_random(65536) + ti;
375 pthread_create(&threads[ti], NULL, spawn_worker, ctx);
376 if (UDEBUGL(2))
377 fprintf(stderr, "Spawned worker %d\n", ti);
380 /* ...and collect them back: */
381 while (joined < u->threads) {
382 /* Wait for some thread to finish... */
383 pthread_cond_wait(&finish_cond, &finish_mutex);
384 if (finish_thread < 0) {
385 /* Stop-by-caller. Tell the workers to wrap up. */
386 uct_halt = 1;
387 continue;
389 /* ...and gather its remnants. */
390 struct spawn_ctx *ctx;
391 pthread_join(threads[finish_thread], (void **) &ctx);
392 played_games += ctx->games;
393 joined++;
394 if (!shared_tree) {
395 if (ctx->t == mctx->t) mctx->t = t;
396 tree_merge(t, ctx->t);
397 tree_done(ctx->t);
399 free(ctx);
400 if (UDEBUGL(2))
401 fprintf(stderr, "Joined worker %d\n", finish_thread);
402 pthread_mutex_unlock(&finish_serializer);
405 pthread_mutex_unlock(&finish_mutex);
407 if (!shared_tree)
408 tree_normalize(mctx->t, u->threads);
410 mctx->games = played_games;
411 return mctx;
414 static struct spawn_ctx *
415 uct_search_start(struct uct *u, struct board *b, enum stone color, struct tree *t)
417 assert(u->threads > 0);
418 assert(!thread_manager_running);
420 struct spawn_ctx ctx = { .u = u, .b = b, .color = color, .t = t, .seed = fast_random(65536) };
421 static struct spawn_ctx mctx; mctx = ctx;
422 pthread_mutex_lock(&finish_mutex);
423 pthread_create(&thread_manager, NULL, spawn_thread_manager, &mctx);
424 thread_manager_running = true;
425 return &mctx;
428 static struct spawn_ctx *
429 uct_search_stop(void)
431 assert(thread_manager_running);
433 /* Signal thread manager to stop the workers. */
434 pthread_mutex_lock(&finish_mutex);
435 finish_thread = -1;
436 pthread_cond_signal(&finish_cond);
437 pthread_mutex_unlock(&finish_mutex);
439 /* Collect the thread manager. */
440 struct spawn_ctx *pctx;
441 thread_manager_running = false;
442 pthread_join(thread_manager, (void **) &pctx);
443 return pctx;
447 /* Determine whether we should terminate the search early. */
448 static bool
449 uct_search_stop_early(struct uct *u, struct tree *t, struct board *b,
450 struct time_info *ti, struct time_stop *stop,
451 struct tree_node *best, struct tree_node *best2,
452 int base_playouts, int i)
454 /* Early break in won situation. */
455 if (best->u.playouts >= 2000 && tree_node_get_value(t, 1, best->u.value) >= u->loss_threshold)
456 return true;
457 /* Earlier break in super-won situation. */
458 if (best->u.playouts >= 500 && tree_node_get_value(t, 1, best->u.value) >= 0.95)
459 return true;
461 /* Break early if we estimate the second-best move cannot
462 * catch up in assigned time anymore. We use all our time
463 * if we are in byoyomi with single stone remaining in our
464 * period, however - it's better to pre-ponder. */
465 bool time_indulgent = (!ti->len.t.main_time && ti->len.t.byoyomi_stones == 1);
466 if (best2 && ti->dim == TD_WALLTIME && !time_indulgent) {
467 double elapsed = time_now() - ti->len.t.timer_start;
468 double remaining = stop->worst.time - elapsed;
469 double pps = ((double)i - base_playouts) / elapsed;
470 double estplayouts = remaining * pps + PLAYOUT_DELTA_SAFEMARGIN;
471 if (best->u.playouts > best2->u.playouts + estplayouts) {
472 if (UDEBUGL(2))
473 fprintf(stderr, "Early stop, result cannot change: "
474 "best %d, best2 %d, estimated %f simulations to go\n",
475 best->u.playouts, best2->u.playouts, estplayouts);
476 return true;
480 return false;
483 /* Determine whether we should terminate the search later. */
484 static bool
485 uct_search_keep_looking(struct uct *u, struct tree *t, struct board *b,
486 struct tree_node *best, struct tree_node *best2,
487 struct tree_node *bestr, struct tree_node *winner, int i)
489 if (!best) {
490 if (UDEBUGL(2))
491 fprintf(stderr, "Did not find best move, still trying...\n");
492 return true;
495 if (u->best2_ratio > 0) {
496 /* Check best/best2 simulations ratio. If the
497 * two best moves give very similar results,
498 * keep simulating. */
499 if (best2 && best2->u.playouts
500 && (double)best->u.playouts / best2->u.playouts < u->best2_ratio) {
501 if (UDEBUGL(2))
502 fprintf(stderr, "Best2 ratio %f < threshold %f\n",
503 (double)best->u.playouts / best2->u.playouts,
504 u->best2_ratio);
505 return true;
509 if (u->bestr_ratio > 0) {
510 /* Check best, best_best value difference. If the best move
511 * and its best child do not give similar enough results,
512 * keep simulating. */
513 if (bestr && bestr->u.playouts
514 && fabs((double)best->u.value - bestr->u.value) > u->bestr_ratio) {
515 if (UDEBUGL(2))
516 fprintf(stderr, "Bestr delta %f > threshold %f\n",
517 fabs((double)best->u.value - bestr->u.value),
518 u->bestr_ratio);
519 return true;
523 if (winner && winner != best) {
524 /* Keep simulating if best explored
525 * does not have also highest value. */
526 if (UDEBUGL(2))
527 fprintf(stderr, "[%d] best %3s [%d] %f != winner %3s [%d] %f\n", i,
528 coord2sstr(best->coord, t->board),
529 best->u.playouts, tree_node_get_value(t, 1, best->u.value),
530 coord2sstr(winner->coord, t->board),
531 winner->u.playouts, tree_node_get_value(t, 1, winner->u.value));
532 return true;
535 /* No reason to keep simulating, bye. */
536 return false;
539 /* Run time-limited MCTS search on foreground. */
540 static int
541 uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t)
543 int base_playouts = u->t->root->u.playouts;
544 if (UDEBUGL(2) && base_playouts > 0)
545 fprintf(stderr, "<pre-simulated %d games skipped>\n", base_playouts);
547 /* Set up time conditions. */
548 if (ti->period == TT_NULL) *ti = default_ti;
549 struct time_stop stop;
550 time_stop_conditions(ti, b, u->fuseki_end, u->yose_start, &stop);
552 /* Number of last game with progress print. */
553 int last_print = t->root->u.playouts;
554 /* Number of simulations to wait before next print. */
555 int print_interval = TREE_SIMPROGRESS_INTERVAL * (u->thread_model == TM_ROOT ? 1 : u->threads);
556 /* Printed notification about full memory? */
557 bool print_fullmem = false;
559 struct spawn_ctx *ctx = uct_search_start(u, b, color, t);
561 /* The search tree is ctx->t. This is normally == t, but in case of
562 * TM_ROOT, it is one of the trees belonging to the independent
563 * workers. It is important to reference ctx->t directly since the
564 * thread manager will swap the tree pointer asynchronously. */
565 /* XXX: This means TM_ROOT support is suboptimal since single stalled
566 * thread can stall the others in case of limiting the search by game
567 * count. However, TM_ROOT just does not deserve any more extra code
568 * right now. */
570 struct tree_node *best = NULL;
571 struct tree_node *best2 = NULL; // Second-best move.
572 struct tree_node *bestr = NULL; // best's best child.
573 struct tree_node *winner = NULL;
575 double busywait_interval = TREE_BUSYWAIT_INTERVAL;
577 /* Now, just periodically poll the search tree. */
578 while (1) {
579 time_sleep(busywait_interval);
580 /* busywait_interval should never be less than desired time, or the
581 * time control is broken. But if it happens to be less, we still search
582 * at least 100ms otherwise the move is completely random. */
584 int i = ctx->t->root->u.playouts;
586 /* Print progress? */
587 if (i - last_print > print_interval) {
588 last_print += print_interval; // keep the numbers tidy
589 uct_progress_status(u, ctx->t, color, last_print);
591 if (!print_fullmem && ctx->t->nodes_size > u->max_tree_size) {
592 if (UDEBUGL(2))
593 fprintf(stderr, "memory limit hit (%ld > %lu)\n", ctx->t->nodes_size, u->max_tree_size);
594 print_fullmem = true;
597 best = u->policy->choose(u->policy, ctx->t->root, b, color, resign);
598 if (best) best2 = u->policy->choose(u->policy, ctx->t->root, b, color, best->coord);
600 /* Possibly stop search early if it's no use to try on. */
601 if (best && uct_search_stop_early(u, ctx->t, b, ti, &stop, best, best2, base_playouts, i))
602 break;
604 /* Check against time settings. */
605 bool desired_done = false;
606 if (ti->dim == TD_WALLTIME) {
607 double elapsed = time_now() - ti->len.t.timer_start;
608 if (elapsed > stop.worst.time) break;
609 desired_done = elapsed > stop.desired.time;
611 } else { assert(ti->dim == TD_GAMES);
612 if (i > stop.worst.playouts) break;
613 desired_done = i > stop.desired.playouts;
616 /* We want to stop simulating, but are willing to keep trying
617 * if we aren't completely sure about the winner yet. */
618 if (desired_done) {
619 if (u->policy->winner && u->policy->evaluate)
620 winner = u->policy->winner(u->policy, ctx->t, ctx->t->root);
621 if (best)
622 bestr = u->policy->choose(u->policy, best, b, stone_other(color), resign);
623 if (!uct_search_keep_looking(u, ctx->t, b, best, best2, bestr, winner, i))
624 break;
627 /* TODO: Early break if best->variance goes under threshold and we already
628 * have enough playouts (possibly thanks to book or to pondering)? */
631 ctx = uct_search_stop();
633 if (UDEBUGL(2))
634 tree_dump(t, u->dumpthres);
635 if (UDEBUGL(0))
636 uct_progress_status(u, t, color, ctx->games);
638 return ctx->games;
642 /* Start pondering background with @color to play. */
643 static void
644 uct_pondering_start(struct uct *u, struct board *b0, struct tree *t, enum stone color)
646 if (UDEBUGL(1))
647 fprintf(stderr, "Starting to ponder with color %s\n", stone2str(stone_other(color)));
648 u->pondering = true;
650 /* We need a local board copy to ponder upon. */
651 struct board *b = malloc(sizeof(*b)); board_copy(b, b0);
653 /* *b0 did not have the genmove'd move played yet. */
654 struct move m = { t->root->coord, t->root_color };
655 int res = board_play(b, &m);
656 assert(res >= 0);
657 setup_dynkomi(u, b, stone_other(m.color));
659 /* Start MCTS manager thread "headless". */
660 uct_search_start(u, b, color, t);
663 /* uct_search_stop() frontend for the pondering (non-genmove) mode. */
664 static void
665 uct_pondering_stop(struct uct *u)
667 u->pondering = false;
668 if (!thread_manager_running)
669 return;
671 /* Stop the thread manager. */
672 struct spawn_ctx *ctx = uct_search_stop();
673 if (UDEBUGL(1)) {
674 fprintf(stderr, "(pondering) ");
675 uct_progress_status(u, ctx->t, ctx->color, ctx->games);
677 free(ctx->b);
681 static coord_t *
682 uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive)
684 double start_time = time_now();
685 struct uct *u = e->data;
687 if (b->superko_violation) {
688 fprintf(stderr, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
689 fprintf(stderr, "Maybe you play with situational instead of positional superko?\n");
690 fprintf(stderr, "I'm going to ignore the violation, but note that I may miss\n");
691 fprintf(stderr, "some moves valid under this ruleset because of this.\n");
692 b->superko_violation = false;
695 /* Seed the tree. */
696 uct_pondering_stop(u);
697 prepare_move(e, b, color);
698 assert(u->t);
700 /* How to decide whether to use dynkomi in this game? Since we use
701 * pondering, it's not simple "who-to-play" matter. Decide based on
702 * the last genmove issued. */
703 u->t->use_extra_komi = !!(u->dynkomi_mask & color);
704 setup_dynkomi(u, b, color);
706 /* Make pessimistic assumption about komi for Japanese rules to
707 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
708 * The rules usually give the same winner if the integer part of komi
709 * is odd so we adjust the komi only if it is even (for a board of
710 * odd size). We are not trying to get an exact evaluation for rare
711 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html
712 * TODO: Support the kgs-rules command once available. */
713 if (u->territory_scoring && (((int)floor(b->komi) + b->size) & 1)) {
714 b->komi += (color == S_BLACK ? 1.0 : -1.0);
715 if (UDEBUGL(0))
716 fprintf(stderr, "Setting komi to %.1f assuming Japanese rules\n",
717 b->komi);
720 /* Perform the Monte Carlo Tree Search! */
721 int played_games = uct_search(u, b, ti, color, u->t);
723 /* Choose the best move from the tree. */
724 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color, resign);
725 if (!best) {
726 reset_state(u);
727 return coord_copy(pass);
729 if (UDEBUGL(1))
730 fprintf(stderr, "*** WINNER is %s (%d,%d) with score %1.4f (%d/%d:%d games)\n",
731 coord2sstr(best->coord, b), coord_x(best->coord, b), coord_y(best->coord, b),
732 tree_node_get_value(u->t, 1, best->u.value),
733 best->u.playouts, u->t->root->u.playouts, played_games);
735 /* Do not resign if we're so short of time that evaluation of best move is completely
736 * unreliable, we might be winning actually. In this case best is almost random but
737 * still better than resign. */
738 if (tree_node_get_value(u->t, 1, best->u.value) < u->resign_ratio && !is_pass(best->coord)
739 && best->u.playouts > GJ_MINGAMES) {
740 reset_state(u);
741 return coord_copy(resign);
744 /* If the opponent just passed and we win counting, always
745 * pass as well. */
746 if (b->moves > 1 && is_pass(b->last_move.coord)) {
747 /* Make sure enough playouts are simulated. */
748 while (u->ownermap.playouts < GJ_MINGAMES)
749 uct_playout(u, b, color, u->t);
750 if (uct_pass_is_safe(u, b, color, u->pass_all_alive || pass_all_alive)) {
751 if (UDEBUGL(0))
752 fprintf(stderr, "<Will rather pass, looks safe enough.>\n");
753 best->coord = pass;
757 tree_promote_node(u->t, &best);
758 /* After a pass, pondering is harmful for two reasons:
759 * (i) We might keep pondering even when the game is over.
760 * Of course this is the case for opponent resign as well.
761 * (ii) More importantly, the ownermap will get skewed since
762 * the UCT will start cutting off any playouts. */
763 if (u->pondering_opt && !is_pass(best->coord)) {
764 uct_pondering_start(u, b, u->t, stone_other(color));
766 if (UDEBUGL(2)) {
767 double time = time_now() - start_time + 0.000001; /* avoid divide by zero */
768 fprintf(stderr, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
769 time, (int)(played_games/time), (int)(played_games/time/u->threads));
771 return coord_copy(best->coord);
775 bool
776 uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color)
778 struct uct *u = e->data;
779 if (!u->t) prepare_move(e, b, color);
780 assert(u->t);
782 if (ti->dim == TD_GAMES) {
783 /* Don't count in games that already went into the book. */
784 ti->len.games += u->t->root->u.playouts;
786 uct_search(u, b, ti, color, u->t);
788 assert(ti->dim == TD_GAMES);
789 tree_save(u->t, b, ti->len.games / 100);
791 return true;
794 void
795 uct_dumpbook(struct engine *e, struct board *b, enum stone color)
797 struct uct *u = e->data;
798 struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size: 0);
799 tree_load(t, b);
800 tree_dump(t, 0);
801 tree_done(t);
805 struct uct *
806 uct_state_init(char *arg, struct board *b)
808 struct uct *u = calloc(1, sizeof(struct uct));
810 u->debug_level = 3;
811 u->gamelen = MC_GAMELEN;
812 u->mercymin = 0;
813 u->expand_p = 2;
814 u->dumpthres = 1000;
815 u->playout_amaf = true;
816 u->playout_amaf_nakade = false;
817 u->amaf_prior = false;
818 u->max_tree_size = 3072ULL * 1048576;
820 if (board_size(b) - 2 >= 19)
821 u->dynkomi = 200;
822 u->dynkomi_mask = S_BLACK;
823 u->handicap_value = 7;
825 u->threads = 1;
826 u->thread_model = TM_TREEVL;
827 u->parallel_tree = true;
828 u->virtual_loss = true;
830 u->fuseki_end = 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
831 u->yose_start = 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
832 u->bestr_ratio = 0.02;
833 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
834 // TODO: Further tuning and experiments with better time allocation schemes.
835 u->best2_ratio = 2.5;
837 u->val_scale = 0.04; u->val_points = 40;
839 if (arg) {
840 char *optspec, *next = arg;
841 while (*next) {
842 optspec = next;
843 next += strcspn(next, ",");
844 if (*next) { *next++ = 0; } else { *next = 0; }
846 char *optname = optspec;
847 char *optval = strchr(optspec, '=');
848 if (optval) *optval++ = 0;
850 if (!strcasecmp(optname, "debug")) {
851 if (optval)
852 u->debug_level = atoi(optval);
853 else
854 u->debug_level++;
855 } else if (!strcasecmp(optname, "mercy") && optval) {
856 /* Minimal difference of black/white captures
857 * to stop playout - "Mercy Rule". Speeds up
858 * hopeless playouts at the expense of some
859 * accuracy. */
860 u->mercymin = atoi(optval);
861 } else if (!strcasecmp(optname, "gamelen") && optval) {
862 u->gamelen = atoi(optval);
863 } else if (!strcasecmp(optname, "expand_p") && optval) {
864 u->expand_p = atoi(optval);
865 } else if (!strcasecmp(optname, "dumpthres") && optval) {
866 u->dumpthres = atoi(optval);
867 } else if (!strcasecmp(optname, "best2_ratio") && optval) {
868 /* If set, prolong simulating while
869 * first_best/second_best playouts ratio
870 * is less than best2_ratio. */
871 u->best2_ratio = atof(optval);
872 } else if (!strcasecmp(optname, "bestr_ratio") && optval) {
873 /* If set, prolong simulating while
874 * best,best_best_child values delta
875 * is more than bestr_ratio. */
876 u->bestr_ratio = atof(optval);
877 } else if (!strcasecmp(optname, "playout_amaf")) {
878 /* Whether to include random playout moves in
879 * AMAF as well. (Otherwise, only tree moves
880 * are included in AMAF. Of course makes sense
881 * only in connection with an AMAF policy.) */
882 /* with-without: 55.5% (+-4.1) */
883 if (optval && *optval == '0')
884 u->playout_amaf = false;
885 else
886 u->playout_amaf = true;
887 } else if (!strcasecmp(optname, "playout_amaf_nakade")) {
888 /* Whether to include nakade moves from playouts
889 * in the AMAF statistics; this tends to nullify
890 * the playout_amaf effect by adding too much
891 * noise. */
892 if (optval && *optval == '0')
893 u->playout_amaf_nakade = false;
894 else
895 u->playout_amaf_nakade = true;
896 } else if (!strcasecmp(optname, "playout_amaf_cutoff") && optval) {
897 /* Keep only first N% of playout stage AMAF
898 * information. */
899 u->playout_amaf_cutoff = atoi(optval);
900 } else if ((!strcasecmp(optname, "policy") || !strcasecmp(optname, "random_policy")) && optval) {
901 char *policyarg = strchr(optval, ':');
902 struct uct_policy **p = !strcasecmp(optname, "policy") ? &u->policy : &u->random_policy;
903 if (policyarg)
904 *policyarg++ = 0;
905 if (!strcasecmp(optval, "ucb1")) {
906 *p = policy_ucb1_init(u, policyarg);
907 } else if (!strcasecmp(optval, "ucb1amaf")) {
908 *p = policy_ucb1amaf_init(u, policyarg);
909 } else {
910 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
911 exit(1);
913 } else if (!strcasecmp(optname, "playout") && optval) {
914 char *playoutarg = strchr(optval, ':');
915 if (playoutarg)
916 *playoutarg++ = 0;
917 if (!strcasecmp(optval, "moggy")) {
918 u->playout = playout_moggy_init(playoutarg, b);
919 } else if (!strcasecmp(optval, "light")) {
920 u->playout = playout_light_init(playoutarg, b);
921 } else if (!strcasecmp(optval, "elo")) {
922 u->playout = playout_elo_init(playoutarg, b);
923 } else {
924 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
925 exit(1);
927 } else if (!strcasecmp(optname, "prior") && optval) {
928 u->prior = uct_prior_init(optval, b);
929 } else if (!strcasecmp(optname, "amaf_prior") && optval) {
930 u->amaf_prior = atoi(optval);
931 } else if (!strcasecmp(optname, "threads") && optval) {
932 /* By default, Pachi will run with only single
933 * tree search thread! */
934 u->threads = atoi(optval);
935 } else if (!strcasecmp(optname, "thread_model") && optval) {
936 if (!strcasecmp(optval, "root")) {
937 /* Root parallelization - each thread
938 * does independent search, trees are
939 * merged at the end. */
940 u->thread_model = TM_ROOT;
941 u->parallel_tree = false;
942 u->virtual_loss = false;
943 } else if (!strcasecmp(optval, "tree")) {
944 /* Tree parallelization - all threads
945 * grind on the same tree. */
946 u->thread_model = TM_TREE;
947 u->parallel_tree = true;
948 u->virtual_loss = false;
949 } else if (!strcasecmp(optval, "treevl")) {
950 /* Tree parallelization, but also
951 * with virtual losses - this discou-
952 * rages most threads choosing the
953 * same tree branches to read. */
954 u->thread_model = TM_TREEVL;
955 u->parallel_tree = true;
956 u->virtual_loss = true;
957 } else {
958 fprintf(stderr, "UCT: Invalid thread model %s\n", optval);
959 exit(1);
961 } else if (!strcasecmp(optname, "pondering")) {
962 /* Keep searching even during opponent's turn. */
963 u->pondering_opt = !optval || atoi(optval);
964 } else if (!strcasecmp(optname, "fuseki_end") && optval) {
965 /* At the very beginning it's not worth thinking
966 * too long because the playout evaluations are
967 * very noisy. So gradually increase the thinking
968 * time up to maximum when fuseki_end percent
969 * of the board has been played.
970 * This only applies if we are not in byoyomi. */
971 u->fuseki_end = atoi(optval);
972 } else if (!strcasecmp(optname, "yose_start") && optval) {
973 /* When yose_start percent of the board has been
974 * played, or if we are in byoyomi, stop spending
975 * more time and spread the remaining time
976 * uniformly.
977 * Between fuseki_end and yose_start, we spend
978 * a constant proportion of the remaining time
979 * on each move. (yose_start should actually
980 * be much earlier than when real yose start,
981 * but "yose" is a good short name to convey
982 * the idea.) */
983 u->yose_start = atoi(optval);
984 } else if (!strcasecmp(optname, "force_seed") && optval) {
985 u->force_seed = atoi(optval);
986 } else if (!strcasecmp(optname, "no_book")) {
987 u->no_book = true;
988 } else if (!strcasecmp(optname, "dynkomi")) {
989 /* Dynamic komi in handicap game; linearly
990 * decreases to basic settings until move
991 * #optval. */
992 u->dynkomi = optval ? atoi(optval) : 200;
993 } else if (!strcasecmp(optname, "dynkomi_mask") && optval) {
994 /* Bitmask of colors the player must be
995 * for dynkomi be applied; you may want
996 * to use dynkomi_mask=3 to allow dynkomi
997 * even in games where Pachi is white. */
998 u->dynkomi_mask = atoi(optval);
999 } else if (!strcasecmp(optname, "handicap_value") && optval) {
1000 /* Point value of single handicap stone,
1001 * for dynkomi computation. */
1002 u->handicap_value = atoi(optval);
1003 } else if (!strcasecmp(optname, "val_scale") && optval) {
1004 /* How much of the game result value should be
1005 * influenced by win size. Zero means it isn't. */
1006 u->val_scale = atof(optval);
1007 } else if (!strcasecmp(optname, "val_points") && optval) {
1008 /* Maximum size of win to be scaled into game
1009 * result value. Zero means boardsize^2. */
1010 u->val_points = atoi(optval) * 2; // result values are doubled
1011 } else if (!strcasecmp(optname, "val_extra")) {
1012 /* If false, the score coefficient will be simply
1013 * added to the value, instead of scaling the result
1014 * coefficient because of it. */
1015 u->val_extra = !optval || atoi(optval);
1016 } else if (!strcasecmp(optname, "root_heuristic") && optval) {
1017 /* Whether to bias exploration by root node values
1018 * (must be supported by the used policy).
1019 * 0: Don't.
1020 * 1: Do, value = result.
1021 * Try to temper the result:
1022 * 2: Do, value = 0.5+(result-expected)/2.
1023 * 3: Do, value = 0.5+bzz((result-expected)^2). */
1024 u->root_heuristic = atoi(optval);
1025 } else if (!strcasecmp(optname, "pass_all_alive")) {
1026 /* Whether to consider all stones alive at the game
1027 * end instead of marking dead groupd. */
1028 u->pass_all_alive = !optval || atoi(optval);
1029 } else if (!strcasecmp(optname, "territory_scoring")) {
1030 /* Use territory scoring (default is area scoring).
1031 * An explicit kgs-rules command overrides this. */
1032 u->territory_scoring = !optval || atoi(optval);
1033 } else if (!strcasecmp(optname, "random_policy_chance") && optval) {
1034 /* If specified (N), with probability 1/N, random_policy policy
1035 * descend is used instead of main policy descend; useful
1036 * if specified policy (e.g. UCB1AMAF) can make unduly biased
1037 * choices sometimes, you can fall back to e.g.
1038 * random_policy=UCB1. */
1039 u->random_policy_chance = atoi(optval);
1040 } else if (!strcasecmp(optname, "max_tree_size") && optval) {
1041 /* Maximum amount of memory [MiB] consumed by the move tree.
1042 * Default is 3072 (3 GiB). Note that if you use TM_ROOT,
1043 * this limits size of only one of the trees, not all of them
1044 * together. */
1045 u->max_tree_size = atol(optval) * 1048576;
1046 } else if (!strcasecmp(optname, "fast_alloc")) {
1047 u->fast_alloc = !optval || atoi(optval);
1048 } else if (!strcasecmp(optname, "banner") && optval) {
1049 /* Additional banner string. This must come as the
1050 * last engine parameter. */
1051 if (*next) *--next = ',';
1052 u->banner = strdup(optval);
1053 break;
1054 } else {
1055 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
1056 exit(1);
1061 u->resign_ratio = 0.2; /* Resign when most games are lost. */
1062 u->loss_threshold = 0.85; /* Stop reading if after at least 5000 playouts this is best value. */
1063 if (!u->policy)
1064 u->policy = policy_ucb1amaf_init(u, NULL);
1066 if (!!u->random_policy_chance ^ !!u->random_policy) {
1067 fprintf(stderr, "uct: Only one of random_policy and random_policy_chance is set\n");
1068 exit(1);
1071 if (u->fast_alloc && !u->parallel_tree) {
1072 fprintf(stderr, "fast_alloc not supported with root parallelization.\n");
1073 exit(1);
1076 if (!u->prior)
1077 u->prior = uct_prior_init(NULL, b);
1079 if (!u->playout)
1080 u->playout = playout_moggy_init(NULL, b);
1081 u->playout->debug_level = u->debug_level;
1083 u->ownermap.map = malloc(board_size2(b) * sizeof(u->ownermap.map[0]));
1085 /* Some things remain uninitialized for now - the opening book
1086 * is not loaded and the tree not set up. */
1087 /* This will be initialized in setup_state() at the first move
1088 * received/requested. This is because right now we are not aware
1089 * about any komi or handicap setup and such. */
1091 return u;
1094 struct engine *
1095 engine_uct_init(char *arg, struct board *b)
1097 struct uct *u = uct_state_init(arg, b);
1098 struct engine *e = calloc(1, sizeof(struct engine));
1099 e->name = "UCT Engine";
1100 e->printhook = uct_printhook_ownermap;
1101 e->notify_play = uct_notify_play;
1102 e->chat = uct_chat;
1103 e->genmove = uct_genmove;
1104 e->dead_group_list = uct_dead_group_list;
1105 e->done = uct_done;
1106 e->data = u;
1108 const char banner[] = "I'm playing UCT. When I'm losing, I will resign, "
1109 "if I think I win, I play until you pass. "
1110 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1111 if (!u->banner) u->banner = "";
1112 e->comment = malloc(sizeof(banner) + strlen(u->banner) + 1);
1113 sprintf(e->comment, "%s %s", banner, u->banner);
1115 return e;