16 #include "playout/elo.h"
17 #include "playout/moggy.h"
18 #include "playout/light.h"
21 #include "uct/dynkomi.h"
22 #include "uct/internal.h"
23 #include "uct/plugins.h"
24 #include "uct/prior.h"
25 #include "uct/search.h"
26 #include "uct/slave.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_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
);
35 /* Maximal simulation length. */
36 #define MC_GAMELEN MAX_GAMELEN
40 setup_state(struct uct
*u
, struct board
*b
, enum stone color
)
42 u
->t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0,
43 u
->local_tree_aging
, u
->stats_hbits
);
45 fast_srandom(u
->force_seed
);
47 fprintf(stderr
, "Fresh board with random seed %lu\n", fast_getseed());
48 //board_print(b, stderr);
49 if (!u
->no_book
&& b
->moves
== 0) {
50 assert(color
== S_BLACK
);
56 reset_state(struct uct
*u
)
59 tree_done(u
->t
); u
->t
= NULL
;
63 setup_dynkomi(struct uct
*u
, struct board
*b
, enum stone to_play
)
65 if (u
->t
->use_extra_komi
&& !u
->pondering
&& u
->dynkomi
->permove
)
66 u
->t
->extra_komi
= u
->dynkomi
->permove(u
->dynkomi
, b
, u
->t
);
70 uct_prepare_move(struct uct
*u
, struct board
*b
, enum stone color
)
73 /* Verify that we have sane state. */
75 assert(u
->t
&& b
->moves
);
76 if (color
!= stone_other(u
->t
->root_color
)) {
77 fprintf(stderr
, "Fatal: Non-alternating play detected %d %d\n",
78 color
, u
->t
->root_color
);
81 uct_htable_reset(u
->t
);
84 /* We need fresh state. */
86 setup_state(u
, b
, color
);
89 u
->ownermap
.playouts
= 0;
90 memset(u
->ownermap
.map
, 0, board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
91 u
->played_own
= u
->played_all
= 0;
95 dead_group_list(struct uct
*u
, struct board
*b
, struct move_queue
*mq
)
97 struct group_judgement gj
;
99 gj
.gs
= alloca(board_size2(b
) * sizeof(gj
.gs
[0]));
100 board_ownermap_judge_group(b
, &u
->ownermap
, &gj
);
101 groups_of_status(b
, &gj
, GS_DEAD
, mq
);
105 uct_pass_is_safe(struct uct
*u
, struct board
*b
, enum stone color
, bool pass_all_alive
)
107 if (u
->ownermap
.playouts
< GJ_MINGAMES
)
110 struct move_queue mq
= { .moves
= 0 };
111 dead_group_list(u
, b
, &mq
);
112 if (pass_all_alive
&& mq
.moves
> 0)
113 return false; // We need to remove some dead groups first.
114 return pass_is_safe(b
, color
, &mq
);
118 uct_printhook_ownermap(struct board
*board
, coord_t c
, char *s
, char *end
)
120 struct uct
*u
= board
->es
;
122 const char chr
[] = ":XO,"; // dame, black, white, unclear
123 const char chm
[] = ":xo,";
124 char ch
= chr
[board_ownermap_judge_point(&u
->ownermap
, c
, GJ_THRES
)];
125 if (ch
== ',') { // less precise estimate then?
126 ch
= chm
[board_ownermap_judge_point(&u
->ownermap
, c
, 0.67)];
128 s
+= snprintf(s
, end
- s
, "%c ", ch
);
133 uct_notify_play(struct engine
*e
, struct board
*b
, struct move
*m
)
135 struct uct
*u
= e
->data
;
137 /* No state, create one - this is probably game beginning
138 * and we need to load the opening book right now. */
139 uct_prepare_move(u
, b
, m
->color
);
143 /* Stop pondering, required by tree_promote_at() */
144 uct_pondering_stop(u
);
145 if (UDEBUGL(2) && u
->slave
)
146 tree_dump(u
->t
, u
->dumpthres
);
148 if (is_resign(m
->coord
)) {
154 /* Promote node of the appropriate move to the tree root. */
156 if (!tree_promote_at(u
->t
, b
, m
->coord
)) {
158 fprintf(stderr
, "Warning: Cannot promote move node! Several play commands in row?\n");
163 /* If we are a slave in a distributed engine, start pondering once
164 * we know which move we actually played. See uct_genmove() about
165 * the check for pass. */
166 if (u
->pondering_opt
&& u
->slave
&& m
->color
== u
->my_color
&& !is_pass(m
->coord
))
167 uct_pondering_start(u
, b
, u
->t
, stone_other(m
->color
));
173 uct_chat(struct engine
*e
, struct board
*b
, char *cmd
)
175 struct uct
*u
= e
->data
;
176 static char reply
[1024];
178 cmd
+= strspn(cmd
, " \n\t");
179 if (!strncasecmp(cmd
, "winrate", 7)) {
181 return "no game context (yet?)";
182 enum stone color
= u
->t
->root_color
;
183 struct tree_node
*n
= u
->t
->root
;
184 snprintf(reply
, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
185 n
->u
.playouts
, u
->threads
, stone2str(color
), coord2sstr(n
->coord
, b
),
186 tree_node_get_value(u
->t
, -1, n
->u
.value
) * 100);
187 if (u
->t
->use_extra_komi
&& abs(u
->t
->extra_komi
) >= 0.5) {
188 sprintf(reply
+ strlen(reply
), ", while self-imposing extra komi %.1f",
198 uct_dead_group_list(struct engine
*e
, struct board
*b
, struct move_queue
*mq
)
200 struct uct
*u
= e
->data
;
202 /* This means the game is probably over, no use pondering on. */
203 uct_pondering_stop(u
);
205 if (u
->pass_all_alive
)
206 return; // no dead groups
208 bool mock_state
= false;
211 /* No state, but we cannot just back out - we might
212 * have passed earlier, only assuming some stones are
213 * dead, and then re-connected, only to lose counting
214 * when all stones are assumed alive. */
215 uct_prepare_move(u
, b
, S_BLACK
); assert(u
->t
);
218 /* Make sure the ownermap is well-seeded. */
219 while (u
->ownermap
.playouts
< GJ_MINGAMES
)
220 uct_playout(u
, b
, S_BLACK
, u
->t
);
221 /* Show the ownermap: */
223 board_print_custom(b
, stderr
, uct_printhook_ownermap
);
225 dead_group_list(u
, b
, mq
);
228 /* Clean up the mock state in case we will receive
229 * a genmove; we could get a non-alternating-move
230 * error from uct_prepare_move() in that case otherwise. */
236 playout_policy_done(struct playout_policy
*p
)
238 if (p
->done
) p
->done(p
);
239 if (p
->data
) free(p
->data
);
244 uct_done(struct engine
*e
)
246 /* This is called on engine reset, especially when clear_board
247 * is received and new game should begin. */
248 struct uct
*u
= e
->data
;
249 uct_pondering_stop(u
);
250 if (u
->t
) reset_state(u
);
251 free(u
->ownermap
.map
);
254 free(u
->random_policy
);
255 playout_policy_done(u
->playout
);
256 uct_prior_done(u
->prior
);
257 pluginset_done(u
->plugins
);
262 /* Run time-limited MCTS search on foreground. */
264 uct_search(struct uct
*u
, struct board
*b
, struct time_info
*ti
, enum stone color
, struct tree
*t
)
266 struct uct_search_state s
;
267 uct_search_start(u
, b
, color
, t
, ti
, &s
);
268 if (UDEBUGL(2) && s
.base_playouts
> 0)
269 fprintf(stderr
, "<pre-simulated %d games>\n", s
.base_playouts
);
271 /* The search tree is ctx->t. This is currently == . It is important
272 * to reference ctx->t directly since the
273 * thread manager will swap the tree pointer asynchronously. */
275 /* Now, just periodically poll the search tree. */
277 time_sleep(TREE_BUSYWAIT_INTERVAL
);
278 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
279 * time control is broken. But if it happens to be less, we still search
280 * at least 100ms otherwise the move is completely random. */
282 int i
= uct_search_games(&s
);
283 /* Print notifications etc. */
284 uct_search_progress(u
, b
, color
, t
, ti
, &s
, i
);
285 /* Check if we should stop the search. */
286 if (uct_search_check_stop(u
, b
, color
, t
, ti
, &s
, i
))
290 struct uct_thread_ctx
*ctx
= uct_search_stop();
291 if (UDEBUGL(2)) tree_dump(t
, u
->dumpthres
);
293 fprintf(stderr
, "(avg score %f/%d value %f/%d)\n",
294 u
->dynkomi
->score
.value
, u
->dynkomi
->score
.playouts
,
295 u
->dynkomi
->value
.value
, u
->dynkomi
->value
.playouts
);
297 uct_progress_status(u
, t
, color
, ctx
->games
);
299 u
->played_own
+= ctx
->games
;
303 /* Start pondering background with @color to play. */
305 uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
)
308 fprintf(stderr
, "Starting to ponder with color %s\n", stone2str(stone_other(color
)));
311 /* We need a local board copy to ponder upon. */
312 struct board
*b
= malloc2(sizeof(*b
)); board_copy(b
, b0
);
314 /* *b0 did not have the genmove'd move played yet. */
315 struct move m
= { t
->root
->coord
, t
->root_color
};
316 int res
= board_play(b
, &m
);
318 setup_dynkomi(u
, b
, stone_other(m
.color
));
320 /* Start MCTS manager thread "headless". */
321 static struct uct_search_state s
;
322 uct_search_start(u
, b
, color
, t
, NULL
, &s
);
325 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
326 * to stop the background search for a slave in the distributed engine. */
328 uct_pondering_stop(struct uct
*u
)
330 if (!thread_manager_running
)
333 /* Stop the thread manager. */
334 struct uct_thread_ctx
*ctx
= uct_search_stop();
336 if (u
->pondering
) fprintf(stderr
, "(pondering) ");
337 uct_progress_status(u
, ctx
->t
, ctx
->color
, ctx
->games
);
341 u
->pondering
= false;
347 uct_genmove_setup(struct uct
*u
, struct board
*b
, enum stone color
)
349 if (b
->superko_violation
) {
350 fprintf(stderr
, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
351 fprintf(stderr
, "Maybe you play with situational instead of positional superko?\n");
352 fprintf(stderr
, "I'm going to ignore the violation, but note that I may miss\n");
353 fprintf(stderr
, "some moves valid under this ruleset because of this.\n");
354 b
->superko_violation
= false;
357 uct_prepare_move(u
, b
, color
);
362 /* How to decide whether to use dynkomi in this game? Since we use
363 * pondering, it's not simple "who-to-play" matter. Decide based on
364 * the last genmove issued. */
365 u
->t
->use_extra_komi
= !!(u
->dynkomi_mask
& color
);
366 /* Moreover, we do not use extra komi at the game end - we are not
367 * to fool ourselves at this point. */
368 if (board_estimated_moves_left(b
) <= MIN_MOVES_LEFT
)
369 u
->t
->use_extra_komi
= false;
370 setup_dynkomi(u
, b
, color
);
372 if (b
->rules
== RULES_JAPANESE
)
373 u
->territory_scoring
= true;
375 /* Make pessimistic assumption about komi for Japanese rules to
376 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
377 * The rules usually give the same winner if the integer part of komi
378 * is odd so we adjust the komi only if it is even (for a board of
379 * odd size). We are not trying to get an exact evaluation for rare
380 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
381 if (u
->territory_scoring
&& (((int)floor(b
->komi
) + board_size(b
)) & 1)) {
382 b
->komi
+= (color
== S_BLACK
? 1.0 : -1.0);
384 fprintf(stderr
, "Setting komi to %.1f assuming Japanese rules\n",
390 uct_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
392 double start_time
= time_now();
393 struct uct
*u
= e
->data
;
394 uct_pondering_stop(u
);
395 uct_genmove_setup(u
, b
, color
);
397 /* Start the Monte Carlo Tree Search! */
398 int base_playouts
= u
->t
->root
->u
.playouts
;
399 int played_games
= uct_search(u
, b
, ti
, color
, u
->t
);
402 struct tree_node
*best
;
403 best
= uct_search_result(u
, b
, color
, pass_all_alive
, played_games
, base_playouts
, &best_coord
);
406 double time
= time_now() - start_time
+ 0.000001; /* avoid divide by zero */
407 fprintf(stderr
, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
408 time
, (int)(played_games
/time
), (int)(played_games
/time
/u
->threads
));
412 /* Pass or resign. */
414 return coord_copy(best_coord
);
416 tree_promote_node(u
->t
, &best
);
418 /* After a pass, pondering is harmful for two reasons:
419 * (i) We might keep pondering even when the game is over.
420 * Of course this is the case for opponent resign as well.
421 * (ii) More importantly, the ownermap will get skewed since
422 * the UCT will start cutting off any playouts. */
423 if (u
->pondering_opt
&& !is_pass(best
->coord
)) {
424 uct_pondering_start(u
, b
, u
->t
, stone_other(color
));
426 return coord_copy(best_coord
);
431 uct_genbook(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
)
433 struct uct
*u
= e
->data
;
434 if (!u
->t
) uct_prepare_move(u
, b
, color
);
437 if (ti
->dim
== TD_GAMES
) {
438 /* Don't count in games that already went into the book. */
439 ti
->len
.games
+= u
->t
->root
->u
.playouts
;
441 uct_search(u
, b
, ti
, color
, u
->t
);
443 assert(ti
->dim
== TD_GAMES
);
444 tree_save(u
->t
, b
, ti
->len
.games
/ 100);
450 uct_dumpbook(struct engine
*e
, struct board
*b
, enum stone color
)
452 struct uct
*u
= e
->data
;
453 struct tree
*t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0, u
->local_tree_aging
, 0);
461 uct_state_init(char *arg
, struct board
*b
)
463 struct uct
*u
= calloc2(1, sizeof(struct uct
));
464 bool using_elo
= false;
466 u
->debug_level
= debug_level
;
467 u
->gamelen
= MC_GAMELEN
;
471 u
->playout_amaf
= true;
472 u
->playout_amaf_nakade
= false;
473 u
->amaf_prior
= false;
474 u
->max_tree_size
= 3072ULL * 1048576;
477 u
->thread_model
= TM_TREEVL
;
478 u
->virtual_loss
= true;
480 u
->fuseki_end
= 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
481 u
->yose_start
= 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
482 u
->bestr_ratio
= 0.02;
483 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
484 // TODO: Further tuning and experiments with better time allocation schemes.
485 u
->best2_ratio
= 2.5;
487 u
->val_scale
= 0.04; u
->val_points
= 40;
488 u
->dynkomi_interval
= 1000;
489 u
->dynkomi_mask
= S_BLACK
| S_WHITE
;
492 u
->local_tree_aging
= 2;
494 u
->plugins
= pluginset_init(b
);
497 char *optspec
, *next
= arg
;
500 next
+= strcspn(next
, ",");
501 if (*next
) { *next
++ = 0; } else { *next
= 0; }
503 char *optname
= optspec
;
504 char *optval
= strchr(optspec
, '=');
505 if (optval
) *optval
++ = 0;
507 if (!strcasecmp(optname
, "debug")) {
509 u
->debug_level
= atoi(optval
);
512 } else if (!strcasecmp(optname
, "mercy") && optval
) {
513 /* Minimal difference of black/white captures
514 * to stop playout - "Mercy Rule". Speeds up
515 * hopeless playouts at the expense of some
517 u
->mercymin
= atoi(optval
);
518 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
519 u
->gamelen
= atoi(optval
);
520 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
521 u
->expand_p
= atoi(optval
);
522 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
523 u
->dumpthres
= atoi(optval
);
524 } else if (!strcasecmp(optname
, "best2_ratio") && optval
) {
525 /* If set, prolong simulating while
526 * first_best/second_best playouts ratio
527 * is less than best2_ratio. */
528 u
->best2_ratio
= atof(optval
);
529 } else if (!strcasecmp(optname
, "bestr_ratio") && optval
) {
530 /* If set, prolong simulating while
531 * best,best_best_child values delta
532 * is more than bestr_ratio. */
533 u
->bestr_ratio
= atof(optval
);
534 } else if (!strcasecmp(optname
, "playout_amaf")) {
535 /* Whether to include random playout moves in
536 * AMAF as well. (Otherwise, only tree moves
537 * are included in AMAF. Of course makes sense
538 * only in connection with an AMAF policy.) */
539 /* with-without: 55.5% (+-4.1) */
540 if (optval
&& *optval
== '0')
541 u
->playout_amaf
= false;
543 u
->playout_amaf
= true;
544 } else if (!strcasecmp(optname
, "playout_amaf_nakade")) {
545 /* Whether to include nakade moves from playouts
546 * in the AMAF statistics; this tends to nullify
547 * the playout_amaf effect by adding too much
549 if (optval
&& *optval
== '0')
550 u
->playout_amaf_nakade
= false;
552 u
->playout_amaf_nakade
= true;
553 } else if (!strcasecmp(optname
, "playout_amaf_cutoff") && optval
) {
554 /* Keep only first N% of playout stage AMAF
556 u
->playout_amaf_cutoff
= atoi(optval
);
557 } else if ((!strcasecmp(optname
, "policy") || !strcasecmp(optname
, "random_policy")) && optval
) {
558 char *policyarg
= strchr(optval
, ':');
559 struct uct_policy
**p
= !strcasecmp(optname
, "policy") ? &u
->policy
: &u
->random_policy
;
562 if (!strcasecmp(optval
, "ucb1")) {
563 *p
= policy_ucb1_init(u
, policyarg
);
564 } else if (!strcasecmp(optval
, "ucb1amaf")) {
565 *p
= policy_ucb1amaf_init(u
, policyarg
);
567 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
570 } else if (!strcasecmp(optname
, "playout") && optval
) {
571 char *playoutarg
= strchr(optval
, ':');
574 if (!strcasecmp(optval
, "moggy")) {
575 u
->playout
= playout_moggy_init(playoutarg
, b
);
576 } else if (!strcasecmp(optval
, "light")) {
577 u
->playout
= playout_light_init(playoutarg
, b
);
578 } else if (!strcasecmp(optval
, "elo")) {
579 u
->playout
= playout_elo_init(playoutarg
, b
);
582 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
585 } else if (!strcasecmp(optname
, "prior") && optval
) {
586 u
->prior
= uct_prior_init(optval
, b
);
587 } else if (!strcasecmp(optname
, "amaf_prior") && optval
) {
588 u
->amaf_prior
= atoi(optval
);
589 } else if (!strcasecmp(optname
, "threads") && optval
) {
590 /* By default, Pachi will run with only single
591 * tree search thread! */
592 u
->threads
= atoi(optval
);
593 } else if (!strcasecmp(optname
, "thread_model") && optval
) {
594 if (!strcasecmp(optval
, "tree")) {
595 /* Tree parallelization - all threads
596 * grind on the same tree. */
597 u
->thread_model
= TM_TREE
;
598 u
->virtual_loss
= false;
599 } else if (!strcasecmp(optval
, "treevl")) {
600 /* Tree parallelization, but also
601 * with virtual losses - this discou-
602 * rages most threads choosing the
603 * same tree branches to read. */
604 u
->thread_model
= TM_TREEVL
;
605 u
->virtual_loss
= true;
607 fprintf(stderr
, "UCT: Invalid thread model %s\n", optval
);
610 } else if (!strcasecmp(optname
, "pondering")) {
611 /* Keep searching even during opponent's turn. */
612 u
->pondering_opt
= !optval
|| atoi(optval
);
613 } else if (!strcasecmp(optname
, "fuseki_end") && optval
) {
614 /* At the very beginning it's not worth thinking
615 * too long because the playout evaluations are
616 * very noisy. So gradually increase the thinking
617 * time up to maximum when fuseki_end percent
618 * of the board has been played.
619 * This only applies if we are not in byoyomi. */
620 u
->fuseki_end
= atoi(optval
);
621 } else if (!strcasecmp(optname
, "yose_start") && optval
) {
622 /* When yose_start percent of the board has been
623 * played, or if we are in byoyomi, stop spending
624 * more time and spread the remaining time
626 * Between fuseki_end and yose_start, we spend
627 * a constant proportion of the remaining time
628 * on each move. (yose_start should actually
629 * be much earlier than when real yose start,
630 * but "yose" is a good short name to convey
632 u
->yose_start
= atoi(optval
);
633 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
634 u
->force_seed
= atoi(optval
);
635 } else if (!strcasecmp(optname
, "no_book")) {
637 } else if (!strcasecmp(optname
, "dynkomi") && optval
) {
638 /* Dynamic komi approach; there are multiple
639 * ways to adjust komi dynamically throughout
640 * play. We currently support two: */
641 char *dynkomiarg
= strchr(optval
, ':');
644 if (!strcasecmp(optval
, "none")) {
645 u
->dynkomi
= uct_dynkomi_init_none(u
, dynkomiarg
, b
);
646 } else if (!strcasecmp(optval
, "linear")) {
647 /* You should set dynkomi_mask=1
648 * since this doesn't work well
649 * for white handicaps! */
650 u
->dynkomi
= uct_dynkomi_init_linear(u
, dynkomiarg
, b
);
651 } else if (!strcasecmp(optval
, "adaptive")) {
652 /* There are many more knobs to
653 * crank - see uct/dynkomi.c. */
654 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomiarg
, b
);
656 fprintf(stderr
, "UCT: Invalid dynkomi mode %s\n", optval
);
659 } else if (!strcasecmp(optname
, "dynkomi_mask") && optval
) {
660 /* Bitmask of colors the player must be
661 * for dynkomi be applied; you may want
662 * to use dynkomi_mask=3 to allow dynkomi
663 * even in games where Pachi is white. */
664 u
->dynkomi_mask
= atoi(optval
);
665 } else if (!strcasecmp(optname
, "dynkomi_interval") && optval
) {
666 /* If non-zero, re-adjust dynamic komi
667 * throughout a single genmove reading,
668 * roughly every N simulations. */
669 /* XXX: Does not work with tree
670 * parallelization. */
671 u
->dynkomi_interval
= atoi(optval
);
672 } else if (!strcasecmp(optname
, "val_scale") && optval
) {
673 /* How much of the game result value should be
674 * influenced by win size. Zero means it isn't. */
675 u
->val_scale
= atof(optval
);
676 } else if (!strcasecmp(optname
, "val_points") && optval
) {
677 /* Maximum size of win to be scaled into game
678 * result value. Zero means boardsize^2. */
679 u
->val_points
= atoi(optval
) * 2; // result values are doubled
680 } else if (!strcasecmp(optname
, "val_extra")) {
681 /* If false, the score coefficient will be simply
682 * added to the value, instead of scaling the result
683 * coefficient because of it. */
684 u
->val_extra
= !optval
|| atoi(optval
);
685 } else if (!strcasecmp(optname
, "local_tree") && optval
) {
686 /* Whether to bias exploration by local tree values
687 * (must be supported by the used policy).
689 * 1: Do, value = result.
690 * Try to temper the result:
691 * 2: Do, value = 0.5+(result-expected)/2.
692 * 3: Do, value = 0.5+bzz((result-expected)^2).
693 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
694 u
->local_tree
= atoi(optval
);
695 } else if (!strcasecmp(optname
, "tenuki_d") && optval
) {
696 /* Tenuki distance at which to break the local tree. */
697 u
->tenuki_d
= atoi(optval
);
698 if (u
->tenuki_d
> TREE_NODE_D_MAX
+ 1) {
699 fprintf(stderr
, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX
+ 1);
702 } else if (!strcasecmp(optname
, "local_tree_aging") && optval
) {
703 /* How much to reduce local tree values between moves. */
704 u
->local_tree_aging
= atof(optval
);
705 } else if (!strcasecmp(optname
, "local_tree_allseq")) {
706 /* By default, only complete sequences are stored
707 * in the local tree. If this is on, also
708 * subsequences starting at each move are stored. */
709 u
->local_tree_allseq
= !optval
|| atoi(optval
);
710 } else if (!strcasecmp(optname
, "local_tree_playout")) {
711 /* Whether to adjust ELO playout probability
712 * distributions according to matched localtree
714 u
->local_tree_playout
= !optval
|| atoi(optval
);
715 } else if (!strcasecmp(optname
, "local_tree_pseqroot")) {
716 /* By default, when we have no sequence move
717 * to suggest in-playout, we give up. If this
718 * is on, we make probability distribution from
719 * sequences first moves instead. */
720 u
->local_tree_pseqroot
= !optval
|| atoi(optval
);
721 } else if (!strcasecmp(optname
, "pass_all_alive")) {
722 /* Whether to consider passing only after all
723 * dead groups were removed from the board;
724 * this is like all genmoves are in fact
725 * kgs-genmove_cleanup. */
726 u
->pass_all_alive
= !optval
|| atoi(optval
);
727 } else if (!strcasecmp(optname
, "territory_scoring")) {
728 /* Use territory scoring (default is area scoring).
729 * An explicit kgs-rules command overrides this. */
730 u
->territory_scoring
= !optval
|| atoi(optval
);
731 } else if (!strcasecmp(optname
, "random_policy_chance") && optval
) {
732 /* If specified (N), with probability 1/N, random_policy policy
733 * descend is used instead of main policy descend; useful
734 * if specified policy (e.g. UCB1AMAF) can make unduly biased
735 * choices sometimes, you can fall back to e.g.
736 * random_policy=UCB1. */
737 u
->random_policy_chance
= atoi(optval
);
738 } else if (!strcasecmp(optname
, "max_tree_size") && optval
) {
739 /* Maximum amount of memory [MiB] consumed by the move tree.
740 * For fast_alloc it includes the temp tree used for pruning.
741 * Default is 3072 (3 GiB). */
742 u
->max_tree_size
= atol(optval
) * 1048576;
743 } else if (!strcasecmp(optname
, "fast_alloc")) {
744 u
->fast_alloc
= !optval
|| atoi(optval
);
745 } else if (!strcasecmp(optname
, "slave")) {
746 /* Act as slave for the distributed engine. */
747 u
->slave
= !optval
|| atoi(optval
);
748 } else if (!strcasecmp(optname
, "shared_nodes") && optval
) {
749 /* Share at most shared_nodes between master and slave at each genmoves.
750 * Must use the same value in master and slaves. */
751 u
->shared_nodes
= atoi(optval
);
752 } else if (!strcasecmp(optname
, "shared_levels") && optval
) {
753 /* Share only nodes of level <= shared_levels. */
754 u
->shared_levels
= atoi(optval
);
755 } else if (!strcasecmp(optname
, "stats_hbits") && optval
) {
756 /* Set hash table size to 2^stats_hbits for the shared stats. */
757 u
->stats_hbits
= atoi(optval
);
758 } else if (!strcasecmp(optname
, "banner") && optval
) {
759 /* Additional banner string. This must come as the
760 * last engine parameter. */
761 if (*next
) *--next
= ',';
762 u
->banner
= strdup(optval
);
764 } else if (!strcasecmp(optname
, "plugin") && optval
) {
765 /* Load an external plugin; filename goes before the colon,
766 * extra arguments after the colon. */
767 char *pluginarg
= strchr(optval
, ':');
770 plugin_load(u
->plugins
, optval
, pluginarg
);
772 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
778 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
779 u
->loss_threshold
= 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
781 u
->policy
= policy_ucb1amaf_init(u
, NULL
);
783 if (!!u
->random_policy_chance
^ !!u
->random_policy
) {
784 fprintf(stderr
, "uct: Only one of random_policy and random_policy_chance is set\n");
788 if (!u
->local_tree
) {
789 /* No ltree aging. */
790 u
->local_tree_aging
= 1.0f
;
793 u
->local_tree_playout
= false;
796 u
->max_tree_size
= (100ULL * u
->max_tree_size
) / (100 + MIN_FREE_MEM_PERCENT
);
799 u
->prior
= uct_prior_init(NULL
, b
);
802 u
->playout
= playout_moggy_init(NULL
, b
);
803 u
->playout
->debug_level
= u
->debug_level
;
805 u
->ownermap
.map
= malloc2(board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
808 if (!u
->stats_hbits
) u
->stats_hbits
= DEFAULT_STATS_HBITS
;
809 if (!u
->shared_nodes
) u
->shared_nodes
= DEFAULT_SHARED_NODES
;
810 if (!u
->shared_levels
) u
->shared_levels
= 1;
811 assert(u
->shared_levels
* board_bits2(b
) <= 8 * (int)sizeof(path_t
));
815 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, NULL
, b
);
817 /* Some things remain uninitialized for now - the opening book
818 * is not loaded and the tree not set up. */
819 /* This will be initialized in setup_state() at the first move
820 * received/requested. This is because right now we are not aware
821 * about any komi or handicap setup and such. */
827 engine_uct_init(char *arg
, struct board
*b
)
829 struct uct
*u
= uct_state_init(arg
, b
);
830 struct engine
*e
= calloc2(1, sizeof(struct engine
));
831 e
->name
= "UCT Engine";
832 e
->printhook
= uct_printhook_ownermap
;
833 e
->notify_play
= uct_notify_play
;
835 e
->genmove
= uct_genmove
;
836 e
->genmoves
= uct_genmoves
;
837 e
->dead_group_list
= uct_dead_group_list
;
841 e
->notify
= uct_notify
;
843 const char banner
[] = "I'm playing UCT. When I'm losing, I will resign, "
844 "if I think I win, I play until you pass. "
845 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
846 if (!u
->banner
) u
->banner
= "";
847 e
->comment
= malloc2(sizeof(banner
) + strlen(u
->banner
) + 1);
848 sprintf(e
->comment
, "%s %s", banner
, u
->banner
);