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/prior.h"
24 #include "uct/search.h"
25 #include "uct/slave.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_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
);
34 /* Maximal simulation length. */
35 #define MC_GAMELEN MAX_GAMELEN
39 setup_state(struct uct
*u
, struct board
*b
, enum stone color
)
41 u
->t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0,
42 u
->local_tree_aging
, u
->stats_hbits
);
44 fast_srandom(u
->force_seed
);
46 fprintf(stderr
, "Fresh board with random seed %lu\n", fast_getseed());
47 //board_print(b, stderr);
48 if (!u
->no_book
&& b
->moves
== 0) {
49 assert(color
== S_BLACK
);
55 reset_state(struct uct
*u
)
58 tree_done(u
->t
); u
->t
= NULL
;
62 setup_dynkomi(struct uct
*u
, struct board
*b
, enum stone to_play
)
64 if (u
->t
->use_extra_komi
&& !u
->pondering
&& u
->dynkomi
->permove
)
65 u
->t
->extra_komi
= u
->dynkomi
->permove(u
->dynkomi
, b
, u
->t
);
69 uct_prepare_move(struct uct
*u
, struct board
*b
, enum stone color
)
72 /* Verify that we have sane state. */
74 assert(u
->t
&& b
->moves
);
75 if (color
!= stone_other(u
->t
->root_color
)) {
76 fprintf(stderr
, "Fatal: Non-alternating play detected %d %d\n",
77 color
, u
->t
->root_color
);
80 uct_htable_reset(u
->t
);
83 /* We need fresh state. */
85 setup_state(u
, b
, color
);
88 u
->ownermap
.playouts
= 0;
89 memset(u
->ownermap
.map
, 0, board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
90 u
->played_own
= u
->played_all
= 0;
94 dead_group_list(struct uct
*u
, struct board
*b
, struct move_queue
*mq
)
96 struct group_judgement gj
;
98 gj
.gs
= alloca(board_size2(b
) * sizeof(gj
.gs
[0]));
99 board_ownermap_judge_group(b
, &u
->ownermap
, &gj
);
100 groups_of_status(b
, &gj
, GS_DEAD
, mq
);
104 uct_pass_is_safe(struct uct
*u
, struct board
*b
, enum stone color
, bool pass_all_alive
)
106 if (u
->ownermap
.playouts
< GJ_MINGAMES
)
109 struct move_queue mq
= { .moves
= 0 };
110 dead_group_list(u
, b
, &mq
);
111 if (pass_all_alive
&& mq
.moves
> 0)
112 return false; // We need to remove some dead groups first.
113 return pass_is_safe(b
, color
, &mq
);
117 uct_printhook_ownermap(struct board
*board
, coord_t c
, char *s
, char *end
)
119 struct uct
*u
= board
->es
;
121 const char chr
[] = ":XO,"; // dame, black, white, unclear
122 const char chm
[] = ":xo,";
123 char ch
= chr
[board_ownermap_judge_point(&u
->ownermap
, c
, GJ_THRES
)];
124 if (ch
== ',') { // less precise estimate then?
125 ch
= chm
[board_ownermap_judge_point(&u
->ownermap
, c
, 0.67)];
127 s
+= snprintf(s
, end
- s
, "%c ", ch
);
132 uct_notify_play(struct engine
*e
, struct board
*b
, struct move
*m
)
134 struct uct
*u
= e
->data
;
136 /* No state, create one - this is probably game beginning
137 * and we need to load the opening book right now. */
138 uct_prepare_move(u
, b
, m
->color
);
142 /* Stop pondering, required by tree_promote_at() */
143 uct_pondering_stop(u
);
144 if (UDEBUGL(2) && u
->slave
)
145 tree_dump(u
->t
, u
->dumpthres
);
147 if (is_resign(m
->coord
)) {
153 /* Promote node of the appropriate move to the tree root. */
155 if (!tree_promote_at(u
->t
, b
, m
->coord
)) {
157 fprintf(stderr
, "Warning: Cannot promote move node! Several play commands in row?\n");
162 /* If we are a slave in a distributed engine, start pondering once
163 * we know which move we actually played. See uct_genmove() about
164 * the check for pass. */
165 if (u
->pondering_opt
&& u
->slave
&& m
->color
== u
->my_color
&& !is_pass(m
->coord
))
166 uct_pondering_start(u
, b
, u
->t
, stone_other(m
->color
));
172 uct_chat(struct engine
*e
, struct board
*b
, char *cmd
)
174 struct uct
*u
= e
->data
;
175 static char reply
[1024];
177 cmd
+= strspn(cmd
, " \n\t");
178 if (!strncasecmp(cmd
, "winrate", 7)) {
180 return "no game context (yet?)";
181 enum stone color
= u
->t
->root_color
;
182 struct tree_node
*n
= u
->t
->root
;
183 snprintf(reply
, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
184 n
->u
.playouts
, u
->threads
, stone2str(color
), coord2sstr(n
->coord
, b
),
185 tree_node_get_value(u
->t
, -1, n
->u
.value
) * 100);
186 if (u
->t
->use_extra_komi
&& abs(u
->t
->extra_komi
) >= 0.5) {
187 sprintf(reply
+ strlen(reply
), ", while self-imposing extra komi %.1f",
197 uct_dead_group_list(struct engine
*e
, struct board
*b
, struct move_queue
*mq
)
199 struct uct
*u
= e
->data
;
201 /* This means the game is probably over, no use pondering on. */
202 uct_pondering_stop(u
);
204 if (u
->pass_all_alive
)
205 return; // no dead groups
207 bool mock_state
= false;
210 /* No state, but we cannot just back out - we might
211 * have passed earlier, only assuming some stones are
212 * dead, and then re-connected, only to lose counting
213 * when all stones are assumed alive. */
214 uct_prepare_move(u
, b
, S_BLACK
); assert(u
->t
);
217 /* Make sure the ownermap is well-seeded. */
218 while (u
->ownermap
.playouts
< GJ_MINGAMES
)
219 uct_playout(u
, b
, S_BLACK
, u
->t
);
220 /* Show the ownermap: */
222 board_print_custom(b
, stderr
, uct_printhook_ownermap
);
224 dead_group_list(u
, b
, mq
);
227 /* Clean up the mock state in case we will receive
228 * a genmove; we could get a non-alternating-move
229 * error from uct_prepare_move() in that case otherwise. */
235 playout_policy_done(struct playout_policy
*p
)
237 if (p
->done
) p
->done(p
);
238 if (p
->data
) free(p
->data
);
243 uct_done(struct engine
*e
)
245 /* This is called on engine reset, especially when clear_board
246 * is received and new game should begin. */
247 struct uct
*u
= e
->data
;
248 uct_pondering_stop(u
);
249 if (u
->t
) reset_state(u
);
250 free(u
->ownermap
.map
);
253 free(u
->random_policy
);
254 playout_policy_done(u
->playout
);
255 uct_prior_done(u
->prior
);
260 /* Run time-limited MCTS search on foreground. */
262 uct_search(struct uct
*u
, struct board
*b
, struct time_info
*ti
, enum stone color
, struct tree
*t
)
264 struct uct_search_state s
;
265 uct_search_start(u
, b
, color
, t
, ti
, &s
);
266 if (UDEBUGL(2) && s
.base_playouts
> 0)
267 fprintf(stderr
, "<pre-simulated %d games>\n", s
.base_playouts
);
269 /* The search tree is ctx->t. This is currently == . It is important
270 * to reference ctx->t directly since the
271 * thread manager will swap the tree pointer asynchronously. */
273 /* Now, just periodically poll the search tree. */
275 time_sleep(TREE_BUSYWAIT_INTERVAL
);
276 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
277 * time control is broken. But if it happens to be less, we still search
278 * at least 100ms otherwise the move is completely random. */
280 int i
= uct_search_games(&s
);
281 /* Print notifications etc. */
282 uct_search_progress(u
, b
, color
, t
, ti
, &s
, i
);
283 /* Check if we should stop the search. */
284 if (uct_search_check_stop(u
, b
, color
, t
, ti
, &s
, i
))
288 struct uct_thread_ctx
*ctx
= uct_search_stop();
289 if (UDEBUGL(2)) tree_dump(t
, u
->dumpthres
);
291 fprintf(stderr
, "(avg score %f/%d value %f/%d)\n",
292 u
->dynkomi
->score
.value
, u
->dynkomi
->score
.playouts
,
293 u
->dynkomi
->value
.value
, u
->dynkomi
->value
.playouts
);
295 uct_progress_status(u
, t
, color
, ctx
->games
);
297 u
->played_own
+= ctx
->games
;
301 /* Start pondering background with @color to play. */
303 uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
)
306 fprintf(stderr
, "Starting to ponder with color %s\n", stone2str(stone_other(color
)));
309 /* We need a local board copy to ponder upon. */
310 struct board
*b
= malloc2(sizeof(*b
)); board_copy(b
, b0
);
312 /* *b0 did not have the genmove'd move played yet. */
313 struct move m
= { t
->root
->coord
, t
->root_color
};
314 int res
= board_play(b
, &m
);
316 setup_dynkomi(u
, b
, stone_other(m
.color
));
318 /* Start MCTS manager thread "headless". */
319 static struct uct_search_state s
;
320 uct_search_start(u
, b
, color
, t
, NULL
, &s
);
323 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
324 * to stop the background search for a slave in the distributed engine. */
326 uct_pondering_stop(struct uct
*u
)
328 if (!thread_manager_running
)
331 /* Stop the thread manager. */
332 struct uct_thread_ctx
*ctx
= uct_search_stop();
334 if (u
->pondering
) fprintf(stderr
, "(pondering) ");
335 uct_progress_status(u
, ctx
->t
, ctx
->color
, ctx
->games
);
339 u
->pondering
= false;
345 uct_genmove_setup(struct uct
*u
, struct board
*b
, enum stone color
)
347 if (b
->superko_violation
) {
348 fprintf(stderr
, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
349 fprintf(stderr
, "Maybe you play with situational instead of positional superko?\n");
350 fprintf(stderr
, "I'm going to ignore the violation, but note that I may miss\n");
351 fprintf(stderr
, "some moves valid under this ruleset because of this.\n");
352 b
->superko_violation
= false;
355 uct_prepare_move(u
, b
, color
);
360 /* How to decide whether to use dynkomi in this game? Since we use
361 * pondering, it's not simple "who-to-play" matter. Decide based on
362 * the last genmove issued. */
363 u
->t
->use_extra_komi
= !!(u
->dynkomi_mask
& color
);
364 setup_dynkomi(u
, b
, color
);
366 if (b
->rules
== RULES_JAPANESE
)
367 u
->territory_scoring
= true;
369 /* Make pessimistic assumption about komi for Japanese rules to
370 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
371 * The rules usually give the same winner if the integer part of komi
372 * is odd so we adjust the komi only if it is even (for a board of
373 * odd size). We are not trying to get an exact evaluation for rare
374 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
375 if (u
->territory_scoring
&& (((int)floor(b
->komi
) + board_size(b
)) & 1)) {
376 b
->komi
+= (color
== S_BLACK
? 1.0 : -1.0);
378 fprintf(stderr
, "Setting komi to %.1f assuming Japanese rules\n",
384 uct_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
386 double start_time
= time_now();
387 struct uct
*u
= e
->data
;
388 uct_pondering_stop(u
);
389 uct_genmove_setup(u
, b
, color
);
391 /* Start the Monte Carlo Tree Search! */
392 int base_playouts
= u
->t
->root
->u
.playouts
;
393 int played_games
= uct_search(u
, b
, ti
, color
, u
->t
);
396 struct tree_node
*best
;
397 best
= uct_search_result(u
, b
, color
, pass_all_alive
, played_games
, base_playouts
, &best_coord
);
400 double time
= time_now() - start_time
+ 0.000001; /* avoid divide by zero */
401 fprintf(stderr
, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
402 time
, (int)(played_games
/time
), (int)(played_games
/time
/u
->threads
));
406 /* Pass or resign. */
408 return coord_copy(best_coord
);
410 tree_promote_node(u
->t
, &best
);
412 /* After a pass, pondering is harmful for two reasons:
413 * (i) We might keep pondering even when the game is over.
414 * Of course this is the case for opponent resign as well.
415 * (ii) More importantly, the ownermap will get skewed since
416 * the UCT will start cutting off any playouts. */
417 if (u
->pondering_opt
&& !is_pass(best
->coord
)) {
418 uct_pondering_start(u
, b
, u
->t
, stone_other(color
));
420 return coord_copy(best_coord
);
425 uct_genbook(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
)
427 struct uct
*u
= e
->data
;
428 if (!u
->t
) uct_prepare_move(u
, b
, color
);
431 if (ti
->dim
== TD_GAMES
) {
432 /* Don't count in games that already went into the book. */
433 ti
->len
.games
+= u
->t
->root
->u
.playouts
;
435 uct_search(u
, b
, ti
, color
, u
->t
);
437 assert(ti
->dim
== TD_GAMES
);
438 tree_save(u
->t
, b
, ti
->len
.games
/ 100);
444 uct_dumpbook(struct engine
*e
, struct board
*b
, enum stone color
)
446 struct uct
*u
= e
->data
;
447 struct tree
*t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0, u
->local_tree_aging
, 0);
455 uct_state_init(char *arg
, struct board
*b
)
457 struct uct
*u
= calloc2(1, sizeof(struct uct
));
458 bool using_elo
= false;
460 u
->debug_level
= debug_level
;
461 u
->gamelen
= MC_GAMELEN
;
465 u
->playout_amaf
= true;
466 u
->playout_amaf_nakade
= false;
467 u
->amaf_prior
= false;
468 u
->max_tree_size
= 3072ULL * 1048576;
470 u
->dynkomi_mask
= S_BLACK
;
473 u
->thread_model
= TM_TREEVL
;
474 u
->virtual_loss
= true;
476 u
->fuseki_end
= 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
477 u
->yose_start
= 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
478 u
->bestr_ratio
= 0.02;
479 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
480 // TODO: Further tuning and experiments with better time allocation schemes.
481 u
->best2_ratio
= 2.5;
483 u
->val_scale
= 0.04; u
->val_points
= 40;
484 /* TODO: Adjust this by number of simulations - it's more important how
485 * many times per move we do the adjustment. */
486 u
->dynkomi_interval
= 500;
489 u
->local_tree_aging
= 2;
492 char *optspec
, *next
= arg
;
495 next
+= strcspn(next
, ",");
496 if (*next
) { *next
++ = 0; } else { *next
= 0; }
498 char *optname
= optspec
;
499 char *optval
= strchr(optspec
, '=');
500 if (optval
) *optval
++ = 0;
502 if (!strcasecmp(optname
, "debug")) {
504 u
->debug_level
= atoi(optval
);
507 } else if (!strcasecmp(optname
, "mercy") && optval
) {
508 /* Minimal difference of black/white captures
509 * to stop playout - "Mercy Rule". Speeds up
510 * hopeless playouts at the expense of some
512 u
->mercymin
= atoi(optval
);
513 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
514 u
->gamelen
= atoi(optval
);
515 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
516 u
->expand_p
= atoi(optval
);
517 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
518 u
->dumpthres
= atoi(optval
);
519 } else if (!strcasecmp(optname
, "best2_ratio") && optval
) {
520 /* If set, prolong simulating while
521 * first_best/second_best playouts ratio
522 * is less than best2_ratio. */
523 u
->best2_ratio
= atof(optval
);
524 } else if (!strcasecmp(optname
, "bestr_ratio") && optval
) {
525 /* If set, prolong simulating while
526 * best,best_best_child values delta
527 * is more than bestr_ratio. */
528 u
->bestr_ratio
= atof(optval
);
529 } else if (!strcasecmp(optname
, "playout_amaf")) {
530 /* Whether to include random playout moves in
531 * AMAF as well. (Otherwise, only tree moves
532 * are included in AMAF. Of course makes sense
533 * only in connection with an AMAF policy.) */
534 /* with-without: 55.5% (+-4.1) */
535 if (optval
&& *optval
== '0')
536 u
->playout_amaf
= false;
538 u
->playout_amaf
= true;
539 } else if (!strcasecmp(optname
, "playout_amaf_nakade")) {
540 /* Whether to include nakade moves from playouts
541 * in the AMAF statistics; this tends to nullify
542 * the playout_amaf effect by adding too much
544 if (optval
&& *optval
== '0')
545 u
->playout_amaf_nakade
= false;
547 u
->playout_amaf_nakade
= true;
548 } else if (!strcasecmp(optname
, "playout_amaf_cutoff") && optval
) {
549 /* Keep only first N% of playout stage AMAF
551 u
->playout_amaf_cutoff
= atoi(optval
);
552 } else if ((!strcasecmp(optname
, "policy") || !strcasecmp(optname
, "random_policy")) && optval
) {
553 char *policyarg
= strchr(optval
, ':');
554 struct uct_policy
**p
= !strcasecmp(optname
, "policy") ? &u
->policy
: &u
->random_policy
;
557 if (!strcasecmp(optval
, "ucb1")) {
558 *p
= policy_ucb1_init(u
, policyarg
);
559 } else if (!strcasecmp(optval
, "ucb1amaf")) {
560 *p
= policy_ucb1amaf_init(u
, policyarg
);
562 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
565 } else if (!strcasecmp(optname
, "playout") && optval
) {
566 char *playoutarg
= strchr(optval
, ':');
569 if (!strcasecmp(optval
, "moggy")) {
570 u
->playout
= playout_moggy_init(playoutarg
, b
);
571 } else if (!strcasecmp(optval
, "light")) {
572 u
->playout
= playout_light_init(playoutarg
, b
);
573 } else if (!strcasecmp(optval
, "elo")) {
574 u
->playout
= playout_elo_init(playoutarg
, b
);
577 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
580 } else if (!strcasecmp(optname
, "prior") && optval
) {
581 u
->prior
= uct_prior_init(optval
, b
);
582 } else if (!strcasecmp(optname
, "amaf_prior") && optval
) {
583 u
->amaf_prior
= atoi(optval
);
584 } else if (!strcasecmp(optname
, "threads") && optval
) {
585 /* By default, Pachi will run with only single
586 * tree search thread! */
587 u
->threads
= atoi(optval
);
588 } else if (!strcasecmp(optname
, "thread_model") && optval
) {
589 if (!strcasecmp(optval
, "tree")) {
590 /* Tree parallelization - all threads
591 * grind on the same tree. */
592 u
->thread_model
= TM_TREE
;
593 u
->virtual_loss
= false;
594 } else if (!strcasecmp(optval
, "treevl")) {
595 /* Tree parallelization, but also
596 * with virtual losses - this discou-
597 * rages most threads choosing the
598 * same tree branches to read. */
599 u
->thread_model
= TM_TREEVL
;
600 u
->virtual_loss
= true;
602 fprintf(stderr
, "UCT: Invalid thread model %s\n", optval
);
605 } else if (!strcasecmp(optname
, "pondering")) {
606 /* Keep searching even during opponent's turn. */
607 u
->pondering_opt
= !optval
|| atoi(optval
);
608 } else if (!strcasecmp(optname
, "fuseki_end") && optval
) {
609 /* At the very beginning it's not worth thinking
610 * too long because the playout evaluations are
611 * very noisy. So gradually increase the thinking
612 * time up to maximum when fuseki_end percent
613 * of the board has been played.
614 * This only applies if we are not in byoyomi. */
615 u
->fuseki_end
= atoi(optval
);
616 } else if (!strcasecmp(optname
, "yose_start") && optval
) {
617 /* When yose_start percent of the board has been
618 * played, or if we are in byoyomi, stop spending
619 * more time and spread the remaining time
621 * Between fuseki_end and yose_start, we spend
622 * a constant proportion of the remaining time
623 * on each move. (yose_start should actually
624 * be much earlier than when real yose start,
625 * but "yose" is a good short name to convey
627 u
->yose_start
= atoi(optval
);
628 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
629 u
->force_seed
= atoi(optval
);
630 } else if (!strcasecmp(optname
, "no_book")) {
632 } else if (!strcasecmp(optname
, "dynkomi") && optval
) {
633 /* Dynamic komi approach; there are multiple
634 * ways to adjust komi dynamically throughout
635 * play. We currently support two: */
636 char *dynkomiarg
= strchr(optval
, ':');
639 if (!strcasecmp(optval
, "none")) {
640 u
->dynkomi
= uct_dynkomi_init_none(u
, dynkomiarg
, b
);
641 } else if (!strcasecmp(optval
, "linear")) {
642 u
->dynkomi
= uct_dynkomi_init_linear(u
, dynkomiarg
, b
);
643 } else if (!strcasecmp(optval
, "adaptive")) {
644 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomiarg
, b
);
646 fprintf(stderr
, "UCT: Invalid dynkomi mode %s\n", optval
);
649 } else if (!strcasecmp(optname
, "dynkomi_mask") && optval
) {
650 /* Bitmask of colors the player must be
651 * for dynkomi be applied; you may want
652 * to use dynkomi_mask=3 to allow dynkomi
653 * even in games where Pachi is white. */
654 u
->dynkomi_mask
= atoi(optval
);
655 } else if (!strcasecmp(optname
, "dynkomi_interval") && optval
) {
656 /* If non-zero, re-adjust dynamic komi
657 * throughout a single genmove reading,
658 * roughly every N simulations. */
659 /* XXX: Does not work with tree
660 * parallelization. */
661 u
->dynkomi_interval
= atoi(optval
);
662 } else if (!strcasecmp(optname
, "val_scale") && optval
) {
663 /* How much of the game result value should be
664 * influenced by win size. Zero means it isn't. */
665 u
->val_scale
= atof(optval
);
666 } else if (!strcasecmp(optname
, "val_points") && optval
) {
667 /* Maximum size of win to be scaled into game
668 * result value. Zero means boardsize^2. */
669 u
->val_points
= atoi(optval
) * 2; // result values are doubled
670 } else if (!strcasecmp(optname
, "val_extra")) {
671 /* If false, the score coefficient will be simply
672 * added to the value, instead of scaling the result
673 * coefficient because of it. */
674 u
->val_extra
= !optval
|| atoi(optval
);
675 } else if (!strcasecmp(optname
, "local_tree") && optval
) {
676 /* Whether to bias exploration by local tree values
677 * (must be supported by the used policy).
679 * 1: Do, value = result.
680 * Try to temper the result:
681 * 2: Do, value = 0.5+(result-expected)/2.
682 * 3: Do, value = 0.5+bzz((result-expected)^2).
683 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
684 u
->local_tree
= atoi(optval
);
685 } else if (!strcasecmp(optname
, "tenuki_d") && optval
) {
686 /* Tenuki distance at which to break the local tree. */
687 u
->tenuki_d
= atoi(optval
);
688 if (u
->tenuki_d
> TREE_NODE_D_MAX
+ 1) {
689 fprintf(stderr
, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX
+ 1);
692 } else if (!strcasecmp(optname
, "local_tree_aging") && optval
) {
693 /* How much to reduce local tree values between moves. */
694 u
->local_tree_aging
= atof(optval
);
695 } else if (!strcasecmp(optname
, "local_tree_allseq")) {
696 /* By default, only complete sequences are stored
697 * in the local tree. If this is on, also
698 * subsequences starting at each move are stored. */
699 u
->local_tree_allseq
= !optval
|| atoi(optval
);
700 } else if (!strcasecmp(optname
, "local_tree_playout")) {
701 /* Whether to adjust ELO playout probability
702 * distributions according to matched localtree
704 u
->local_tree_playout
= !optval
|| atoi(optval
);
705 } else if (!strcasecmp(optname
, "local_tree_pseqroot")) {
706 /* By default, when we have no sequence move
707 * to suggest in-playout, we give up. If this
708 * is on, we make probability distribution from
709 * sequences first moves instead. */
710 u
->local_tree_pseqroot
= !optval
|| atoi(optval
);
711 } else if (!strcasecmp(optname
, "pass_all_alive")) {
712 /* Whether to consider passing only after all
713 * dead groups were removed from the board;
714 * this is like all genmoves are in fact
715 * kgs-genmove_cleanup. */
716 u
->pass_all_alive
= !optval
|| atoi(optval
);
717 } else if (!strcasecmp(optname
, "territory_scoring")) {
718 /* Use territory scoring (default is area scoring).
719 * An explicit kgs-rules command overrides this. */
720 u
->territory_scoring
= !optval
|| atoi(optval
);
721 } else if (!strcasecmp(optname
, "random_policy_chance") && optval
) {
722 /* If specified (N), with probability 1/N, random_policy policy
723 * descend is used instead of main policy descend; useful
724 * if specified policy (e.g. UCB1AMAF) can make unduly biased
725 * choices sometimes, you can fall back to e.g.
726 * random_policy=UCB1. */
727 u
->random_policy_chance
= atoi(optval
);
728 } else if (!strcasecmp(optname
, "max_tree_size") && optval
) {
729 /* Maximum amount of memory [MiB] consumed by the move tree.
730 * For fast_alloc it includes the temp tree used for pruning.
731 * Default is 3072 (3 GiB). */
732 u
->max_tree_size
= atol(optval
) * 1048576;
733 } else if (!strcasecmp(optname
, "fast_alloc")) {
734 u
->fast_alloc
= !optval
|| atoi(optval
);
735 } else if (!strcasecmp(optname
, "slave")) {
736 /* Act as slave for the distributed engine. */
737 u
->slave
= !optval
|| atoi(optval
);
738 } else if (!strcasecmp(optname
, "shared_nodes") && optval
) {
739 /* Share at most shared_nodes between master and slave at each genmoves.
740 * Must use the same value in master and slaves. */
741 u
->shared_nodes
= atoi(optval
);
742 } else if (!strcasecmp(optname
, "shared_levels") && optval
) {
743 /* Share only nodes of level <= shared_levels. */
744 u
->shared_levels
= atoi(optval
);
745 } else if (!strcasecmp(optname
, "stats_hbits") && optval
) {
746 /* Set hash table size to 2^stats_hbits for the shared stats. */
747 u
->stats_hbits
= atoi(optval
);
748 } else if (!strcasecmp(optname
, "banner") && optval
) {
749 /* Additional banner string. This must come as the
750 * last engine parameter. */
751 if (*next
) *--next
= ',';
752 u
->banner
= strdup(optval
);
755 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
761 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
762 u
->loss_threshold
= 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
764 u
->policy
= policy_ucb1amaf_init(u
, NULL
);
766 if (!!u
->random_policy_chance
^ !!u
->random_policy
) {
767 fprintf(stderr
, "uct: Only one of random_policy and random_policy_chance is set\n");
771 if (!u
->local_tree
) {
772 /* No ltree aging. */
773 u
->local_tree_aging
= 1.0f
;
776 u
->local_tree_playout
= false;
779 u
->max_tree_size
= (100ULL * u
->max_tree_size
) / (100 + MIN_FREE_MEM_PERCENT
);
782 u
->prior
= uct_prior_init(NULL
, b
);
785 u
->playout
= playout_moggy_init(NULL
, b
);
786 u
->playout
->debug_level
= u
->debug_level
;
788 u
->ownermap
.map
= malloc2(board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
791 if (!u
->stats_hbits
) u
->stats_hbits
= DEFAULT_STATS_HBITS
;
792 if (!u
->shared_nodes
) u
->shared_nodes
= DEFAULT_SHARED_NODES
;
793 if (!u
->shared_levels
) u
->shared_levels
= 1;
794 assert(u
->shared_levels
* board_bits2(b
) <= 8 * (int)sizeof(path_t
));
798 u
->dynkomi
= uct_dynkomi_init_linear(u
, NULL
, b
);
800 /* Some things remain uninitialized for now - the opening book
801 * is not loaded and the tree not set up. */
802 /* This will be initialized in setup_state() at the first move
803 * received/requested. This is because right now we are not aware
804 * about any komi or handicap setup and such. */
810 engine_uct_init(char *arg
, struct board
*b
)
812 struct uct
*u
= uct_state_init(arg
, b
);
813 struct engine
*e
= calloc2(1, sizeof(struct engine
));
814 e
->name
= "UCT Engine";
815 e
->printhook
= uct_printhook_ownermap
;
816 e
->notify_play
= uct_notify_play
;
818 e
->genmove
= uct_genmove
;
819 e
->genmoves
= uct_genmoves
;
820 e
->dead_group_list
= uct_dead_group_list
;
824 e
->notify
= uct_notify
;
826 const char banner
[] = "I'm playing UCT. When I'm losing, I will resign, "
827 "if I think I win, I play until you pass. "
828 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
829 if (!u
->banner
) u
->banner
= "";
830 e
->comment
= malloc2(sizeof(banner
) + strlen(u
->banner
) + 1);
831 sprintf(e
->comment
, "%s %s", banner
, u
->banner
);