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;
476 u
->dynkomi_mask
= S_BLACK
;
479 u
->thread_model
= TM_TREEVL
;
480 u
->virtual_loss
= true;
482 u
->fuseki_end
= 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
483 u
->yose_start
= 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
484 u
->bestr_ratio
= 0.02;
485 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
486 // TODO: Further tuning and experiments with better time allocation schemes.
487 u
->best2_ratio
= 2.5;
489 u
->val_scale
= 0.04; u
->val_points
= 40;
490 u
->dynkomi_interval
= 250;
493 u
->local_tree_aging
= 2;
495 u
->plugins
= pluginset_init(b
);
498 char *optspec
, *next
= arg
;
501 next
+= strcspn(next
, ",");
502 if (*next
) { *next
++ = 0; } else { *next
= 0; }
504 char *optname
= optspec
;
505 char *optval
= strchr(optspec
, '=');
506 if (optval
) *optval
++ = 0;
508 if (!strcasecmp(optname
, "debug")) {
510 u
->debug_level
= atoi(optval
);
513 } else if (!strcasecmp(optname
, "mercy") && optval
) {
514 /* Minimal difference of black/white captures
515 * to stop playout - "Mercy Rule". Speeds up
516 * hopeless playouts at the expense of some
518 u
->mercymin
= atoi(optval
);
519 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
520 u
->gamelen
= atoi(optval
);
521 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
522 u
->expand_p
= atoi(optval
);
523 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
524 u
->dumpthres
= atoi(optval
);
525 } else if (!strcasecmp(optname
, "best2_ratio") && optval
) {
526 /* If set, prolong simulating while
527 * first_best/second_best playouts ratio
528 * is less than best2_ratio. */
529 u
->best2_ratio
= atof(optval
);
530 } else if (!strcasecmp(optname
, "bestr_ratio") && optval
) {
531 /* If set, prolong simulating while
532 * best,best_best_child values delta
533 * is more than bestr_ratio. */
534 u
->bestr_ratio
= atof(optval
);
535 } else if (!strcasecmp(optname
, "playout_amaf")) {
536 /* Whether to include random playout moves in
537 * AMAF as well. (Otherwise, only tree moves
538 * are included in AMAF. Of course makes sense
539 * only in connection with an AMAF policy.) */
540 /* with-without: 55.5% (+-4.1) */
541 if (optval
&& *optval
== '0')
542 u
->playout_amaf
= false;
544 u
->playout_amaf
= true;
545 } else if (!strcasecmp(optname
, "playout_amaf_nakade")) {
546 /* Whether to include nakade moves from playouts
547 * in the AMAF statistics; this tends to nullify
548 * the playout_amaf effect by adding too much
550 if (optval
&& *optval
== '0')
551 u
->playout_amaf_nakade
= false;
553 u
->playout_amaf_nakade
= true;
554 } else if (!strcasecmp(optname
, "playout_amaf_cutoff") && optval
) {
555 /* Keep only first N% of playout stage AMAF
557 u
->playout_amaf_cutoff
= atoi(optval
);
558 } else if ((!strcasecmp(optname
, "policy") || !strcasecmp(optname
, "random_policy")) && optval
) {
559 char *policyarg
= strchr(optval
, ':');
560 struct uct_policy
**p
= !strcasecmp(optname
, "policy") ? &u
->policy
: &u
->random_policy
;
563 if (!strcasecmp(optval
, "ucb1")) {
564 *p
= policy_ucb1_init(u
, policyarg
);
565 } else if (!strcasecmp(optval
, "ucb1amaf")) {
566 *p
= policy_ucb1amaf_init(u
, policyarg
);
568 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
571 } else if (!strcasecmp(optname
, "playout") && optval
) {
572 char *playoutarg
= strchr(optval
, ':');
575 if (!strcasecmp(optval
, "moggy")) {
576 u
->playout
= playout_moggy_init(playoutarg
, b
);
577 } else if (!strcasecmp(optval
, "light")) {
578 u
->playout
= playout_light_init(playoutarg
, b
);
579 } else if (!strcasecmp(optval
, "elo")) {
580 u
->playout
= playout_elo_init(playoutarg
, b
);
583 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
586 } else if (!strcasecmp(optname
, "prior") && optval
) {
587 u
->prior
= uct_prior_init(optval
, b
);
588 } else if (!strcasecmp(optname
, "amaf_prior") && optval
) {
589 u
->amaf_prior
= atoi(optval
);
590 } else if (!strcasecmp(optname
, "threads") && optval
) {
591 /* By default, Pachi will run with only single
592 * tree search thread! */
593 u
->threads
= atoi(optval
);
594 } else if (!strcasecmp(optname
, "thread_model") && optval
) {
595 if (!strcasecmp(optval
, "tree")) {
596 /* Tree parallelization - all threads
597 * grind on the same tree. */
598 u
->thread_model
= TM_TREE
;
599 u
->virtual_loss
= false;
600 } else if (!strcasecmp(optval
, "treevl")) {
601 /* Tree parallelization, but also
602 * with virtual losses - this discou-
603 * rages most threads choosing the
604 * same tree branches to read. */
605 u
->thread_model
= TM_TREEVL
;
606 u
->virtual_loss
= true;
608 fprintf(stderr
, "UCT: Invalid thread model %s\n", optval
);
611 } else if (!strcasecmp(optname
, "pondering")) {
612 /* Keep searching even during opponent's turn. */
613 u
->pondering_opt
= !optval
|| atoi(optval
);
614 } else if (!strcasecmp(optname
, "fuseki_end") && optval
) {
615 /* At the very beginning it's not worth thinking
616 * too long because the playout evaluations are
617 * very noisy. So gradually increase the thinking
618 * time up to maximum when fuseki_end percent
619 * of the board has been played.
620 * This only applies if we are not in byoyomi. */
621 u
->fuseki_end
= atoi(optval
);
622 } else if (!strcasecmp(optname
, "yose_start") && optval
) {
623 /* When yose_start percent of the board has been
624 * played, or if we are in byoyomi, stop spending
625 * more time and spread the remaining time
627 * Between fuseki_end and yose_start, we spend
628 * a constant proportion of the remaining time
629 * on each move. (yose_start should actually
630 * be much earlier than when real yose start,
631 * but "yose" is a good short name to convey
633 u
->yose_start
= atoi(optval
);
634 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
635 u
->force_seed
= atoi(optval
);
636 } else if (!strcasecmp(optname
, "no_book")) {
638 } else if (!strcasecmp(optname
, "dynkomi") && optval
) {
639 /* Dynamic komi approach; there are multiple
640 * ways to adjust komi dynamically throughout
641 * play. We currently support two: */
642 char *dynkomiarg
= strchr(optval
, ':');
645 if (!strcasecmp(optval
, "none")) {
646 u
->dynkomi
= uct_dynkomi_init_none(u
, dynkomiarg
, b
);
647 } else if (!strcasecmp(optval
, "linear")) {
648 u
->dynkomi
= uct_dynkomi_init_linear(u
, dynkomiarg
, b
);
649 } else if (!strcasecmp(optval
, "adaptive")) {
650 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomiarg
, b
);
652 fprintf(stderr
, "UCT: Invalid dynkomi mode %s\n", optval
);
655 } else if (!strcasecmp(optname
, "dynkomi_mask") && optval
) {
656 /* Bitmask of colors the player must be
657 * for dynkomi be applied; you may want
658 * to use dynkomi_mask=3 to allow dynkomi
659 * even in games where Pachi is white. */
660 u
->dynkomi_mask
= atoi(optval
);
661 } else if (!strcasecmp(optname
, "dynkomi_interval") && optval
) {
662 /* If non-zero, re-adjust dynamic komi
663 * throughout a single genmove reading,
664 * roughly every N simulations. */
665 /* XXX: Does not work with tree
666 * parallelization. */
667 u
->dynkomi_interval
= atoi(optval
);
668 } else if (!strcasecmp(optname
, "val_scale") && optval
) {
669 /* How much of the game result value should be
670 * influenced by win size. Zero means it isn't. */
671 u
->val_scale
= atof(optval
);
672 } else if (!strcasecmp(optname
, "val_points") && optval
) {
673 /* Maximum size of win to be scaled into game
674 * result value. Zero means boardsize^2. */
675 u
->val_points
= atoi(optval
) * 2; // result values are doubled
676 } else if (!strcasecmp(optname
, "val_extra")) {
677 /* If false, the score coefficient will be simply
678 * added to the value, instead of scaling the result
679 * coefficient because of it. */
680 u
->val_extra
= !optval
|| atoi(optval
);
681 } else if (!strcasecmp(optname
, "local_tree") && optval
) {
682 /* Whether to bias exploration by local tree values
683 * (must be supported by the used policy).
685 * 1: Do, value = result.
686 * Try to temper the result:
687 * 2: Do, value = 0.5+(result-expected)/2.
688 * 3: Do, value = 0.5+bzz((result-expected)^2).
689 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
690 u
->local_tree
= atoi(optval
);
691 } else if (!strcasecmp(optname
, "tenuki_d") && optval
) {
692 /* Tenuki distance at which to break the local tree. */
693 u
->tenuki_d
= atoi(optval
);
694 if (u
->tenuki_d
> TREE_NODE_D_MAX
+ 1) {
695 fprintf(stderr
, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX
+ 1);
698 } else if (!strcasecmp(optname
, "local_tree_aging") && optval
) {
699 /* How much to reduce local tree values between moves. */
700 u
->local_tree_aging
= atof(optval
);
701 } else if (!strcasecmp(optname
, "local_tree_allseq")) {
702 /* By default, only complete sequences are stored
703 * in the local tree. If this is on, also
704 * subsequences starting at each move are stored. */
705 u
->local_tree_allseq
= !optval
|| atoi(optval
);
706 } else if (!strcasecmp(optname
, "local_tree_playout")) {
707 /* Whether to adjust ELO playout probability
708 * distributions according to matched localtree
710 u
->local_tree_playout
= !optval
|| atoi(optval
);
711 } else if (!strcasecmp(optname
, "local_tree_pseqroot")) {
712 /* By default, when we have no sequence move
713 * to suggest in-playout, we give up. If this
714 * is on, we make probability distribution from
715 * sequences first moves instead. */
716 u
->local_tree_pseqroot
= !optval
|| atoi(optval
);
717 } else if (!strcasecmp(optname
, "pass_all_alive")) {
718 /* Whether to consider passing only after all
719 * dead groups were removed from the board;
720 * this is like all genmoves are in fact
721 * kgs-genmove_cleanup. */
722 u
->pass_all_alive
= !optval
|| atoi(optval
);
723 } else if (!strcasecmp(optname
, "territory_scoring")) {
724 /* Use territory scoring (default is area scoring).
725 * An explicit kgs-rules command overrides this. */
726 u
->territory_scoring
= !optval
|| atoi(optval
);
727 } else if (!strcasecmp(optname
, "random_policy_chance") && optval
) {
728 /* If specified (N), with probability 1/N, random_policy policy
729 * descend is used instead of main policy descend; useful
730 * if specified policy (e.g. UCB1AMAF) can make unduly biased
731 * choices sometimes, you can fall back to e.g.
732 * random_policy=UCB1. */
733 u
->random_policy_chance
= atoi(optval
);
734 } else if (!strcasecmp(optname
, "max_tree_size") && optval
) {
735 /* Maximum amount of memory [MiB] consumed by the move tree.
736 * For fast_alloc it includes the temp tree used for pruning.
737 * Default is 3072 (3 GiB). */
738 u
->max_tree_size
= atol(optval
) * 1048576;
739 } else if (!strcasecmp(optname
, "fast_alloc")) {
740 u
->fast_alloc
= !optval
|| atoi(optval
);
741 } else if (!strcasecmp(optname
, "slave")) {
742 /* Act as slave for the distributed engine. */
743 u
->slave
= !optval
|| atoi(optval
);
744 } else if (!strcasecmp(optname
, "shared_nodes") && optval
) {
745 /* Share at most shared_nodes between master and slave at each genmoves.
746 * Must use the same value in master and slaves. */
747 u
->shared_nodes
= atoi(optval
);
748 } else if (!strcasecmp(optname
, "shared_levels") && optval
) {
749 /* Share only nodes of level <= shared_levels. */
750 u
->shared_levels
= atoi(optval
);
751 } else if (!strcasecmp(optname
, "stats_hbits") && optval
) {
752 /* Set hash table size to 2^stats_hbits for the shared stats. */
753 u
->stats_hbits
= atoi(optval
);
754 } else if (!strcasecmp(optname
, "banner") && optval
) {
755 /* Additional banner string. This must come as the
756 * last engine parameter. */
757 if (*next
) *--next
= ',';
758 u
->banner
= strdup(optval
);
760 } else if (!strcasecmp(optname
, "plugin") && optval
) {
761 /* Load an external plugin; filename goes before the colon,
762 * extra arguments after the colon. */
763 char *pluginarg
= strchr(optval
, ':');
766 plugin_load(u
->plugins
, optval
, pluginarg
);
768 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
774 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
775 u
->loss_threshold
= 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
777 u
->policy
= policy_ucb1amaf_init(u
, NULL
);
779 if (!!u
->random_policy_chance
^ !!u
->random_policy
) {
780 fprintf(stderr
, "uct: Only one of random_policy and random_policy_chance is set\n");
784 if (!u
->local_tree
) {
785 /* No ltree aging. */
786 u
->local_tree_aging
= 1.0f
;
789 u
->local_tree_playout
= false;
792 u
->max_tree_size
= (100ULL * u
->max_tree_size
) / (100 + MIN_FREE_MEM_PERCENT
);
795 u
->prior
= uct_prior_init(NULL
, b
);
798 u
->playout
= playout_moggy_init(NULL
, b
);
799 u
->playout
->debug_level
= u
->debug_level
;
801 u
->ownermap
.map
= malloc2(board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
804 if (!u
->stats_hbits
) u
->stats_hbits
= DEFAULT_STATS_HBITS
;
805 if (!u
->shared_nodes
) u
->shared_nodes
= DEFAULT_SHARED_NODES
;
806 if (!u
->shared_levels
) u
->shared_levels
= 1;
807 assert(u
->shared_levels
* board_bits2(b
) <= 8 * (int)sizeof(path_t
));
811 u
->dynkomi
= uct_dynkomi_init_linear(u
, NULL
, b
);
813 /* Some things remain uninitialized for now - the opening book
814 * is not loaded and the tree not set up. */
815 /* This will be initialized in setup_state() at the first move
816 * received/requested. This is because right now we are not aware
817 * about any komi or handicap setup and such. */
823 engine_uct_init(char *arg
, struct board
*b
)
825 struct uct
*u
= uct_state_init(arg
, b
);
826 struct engine
*e
= calloc2(1, sizeof(struct engine
));
827 e
->name
= "UCT Engine";
828 e
->printhook
= uct_printhook_ownermap
;
829 e
->notify_play
= uct_notify_play
;
831 e
->genmove
= uct_genmove
;
832 e
->genmoves
= uct_genmoves
;
833 e
->dead_group_list
= uct_dead_group_list
;
837 e
->notify
= uct_notify
;
839 const char banner
[] = "I'm playing UCT. When I'm losing, I will resign, "
840 "if I think I win, I play until you pass. "
841 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
842 if (!u
->banner
) u
->banner
= "";
843 e
->comment
= malloc2(sizeof(banner
) + strlen(u
->banner
) + 1);
844 sprintf(e
->comment
, "%s %s", banner
, u
->banner
);