16 #include "playout/elo.h"
17 #include "playout/moggy.h"
18 #include "playout/light.h"
21 #include "distributed/distributed.h"
22 #include "uct/dynkomi.h"
23 #include "uct/internal.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, u
->local_tree_aging
);
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
);
82 /* We need fresh state. */
84 setup_state(u
, b
, color
);
87 u
->ownermap
.playouts
= 0;
88 memset(u
->ownermap
.map
, 0, board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
89 memset(u
->stats
, 0, board_size2(b
) * sizeof(u
->stats
[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
);
254 free(u
->random_policy
);
255 playout_policy_done(u
->playout
);
256 uct_prior_done(u
->prior
);
261 /* Run time-limited MCTS search on foreground. */
263 uct_search(struct uct
*u
, struct board
*b
, struct time_info
*ti
, enum stone color
, struct tree
*t
)
265 struct uct_search_state s
;
266 uct_search_start(u
, b
, color
, t
, ti
, &s
);
267 if (UDEBUGL(2) && s
.base_playouts
> 0)
268 fprintf(stderr
, "<pre-simulated %d games>\n", s
.base_playouts
);
270 /* The search tree is ctx->t. This is normally == t, but in case of
271 * TM_ROOT, it is one of the trees belonging to the independent
272 * workers. It is important to reference ctx->t directly since the
273 * thread manager will swap the tree pointer asynchronously. */
274 /* XXX: This means TM_ROOT support is suboptimal since single stalled
275 * thread can stall the others in case of limiting the search by game
276 * count. However, TM_ROOT just does not deserve any more extra code
279 /* Now, just periodically poll the search tree. */
281 time_sleep(TREE_BUSYWAIT_INTERVAL
);
282 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
283 * time control is broken. But if it happens to be less, we still search
284 * at least 100ms otherwise the move is completely random. */
286 int i
= uct_search_games(&s
);
287 /* Print notifications etc. */
288 uct_search_progress(u
, b
, color
, t
, ti
, &s
, i
);
289 /* Check if we should stop the search. */
290 if (uct_search_check_stop(u
, b
, color
, t
, ti
, &s
, i
))
294 struct uct_thread_ctx
*ctx
= uct_search_stop();
295 if (UDEBUGL(2)) tree_dump(t
, u
->dumpthres
);
297 fprintf(stderr
, "(avg score %f/%d value %f/%d)\n",
298 u
->dynkomi
->score
.value
, u
->dynkomi
->score
.playouts
,
299 u
->dynkomi
->value
.value
, u
->dynkomi
->value
.playouts
);
301 uct_progress_status(u
, t
, color
, ctx
->games
);
303 u
->played_own
+= ctx
->games
;
307 /* Start pondering background with @color to play. */
309 uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
)
312 fprintf(stderr
, "Starting to ponder with color %s\n", stone2str(stone_other(color
)));
315 /* We need a local board copy to ponder upon. */
316 struct board
*b
= malloc2(sizeof(*b
)); board_copy(b
, b0
);
318 /* *b0 did not have the genmove'd move played yet. */
319 struct move m
= { t
->root
->coord
, t
->root_color
};
320 int res
= board_play(b
, &m
);
322 setup_dynkomi(u
, b
, stone_other(m
.color
));
324 /* Start MCTS manager thread "headless". */
325 static struct uct_search_state s
;
326 uct_search_start(u
, b
, color
, t
, NULL
, &s
);
329 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
330 * to stop the background search for a slave in the distributed engine. */
332 uct_pondering_stop(struct uct
*u
)
334 if (!thread_manager_running
)
337 /* Stop the thread manager. */
338 struct uct_thread_ctx
*ctx
= uct_search_stop();
340 if (u
->pondering
) fprintf(stderr
, "(pondering) ");
341 uct_progress_status(u
, ctx
->t
, ctx
->color
, ctx
->games
);
345 u
->pondering
= false;
351 uct_genmove_setup(struct uct
*u
, struct board
*b
, enum stone color
)
353 if (b
->superko_violation
) {
354 fprintf(stderr
, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
355 fprintf(stderr
, "Maybe you play with situational instead of positional superko?\n");
356 fprintf(stderr
, "I'm going to ignore the violation, but note that I may miss\n");
357 fprintf(stderr
, "some moves valid under this ruleset because of this.\n");
358 b
->superko_violation
= false;
361 uct_prepare_move(u
, b
, color
);
366 /* How to decide whether to use dynkomi in this game? Since we use
367 * pondering, it's not simple "who-to-play" matter. Decide based on
368 * the last genmove issued. */
369 u
->t
->use_extra_komi
= !!(u
->dynkomi_mask
& color
);
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
);
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
->parallel_tree
= true;
481 u
->virtual_loss
= true;
483 u
->fuseki_end
= 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
484 u
->yose_start
= 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
485 u
->bestr_ratio
= 0.02;
486 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
487 // TODO: Further tuning and experiments with better time allocation schemes.
488 u
->best2_ratio
= 2.5;
490 u
->val_scale
= 0.04; u
->val_points
= 40;
493 u
->local_tree_aging
= 2;
496 char *optspec
, *next
= arg
;
499 next
+= strcspn(next
, ",");
500 if (*next
) { *next
++ = 0; } else { *next
= 0; }
502 char *optname
= optspec
;
503 char *optval
= strchr(optspec
, '=');
504 if (optval
) *optval
++ = 0;
506 if (!strcasecmp(optname
, "debug")) {
508 u
->debug_level
= atoi(optval
);
511 } else if (!strcasecmp(optname
, "mercy") && optval
) {
512 /* Minimal difference of black/white captures
513 * to stop playout - "Mercy Rule". Speeds up
514 * hopeless playouts at the expense of some
516 u
->mercymin
= atoi(optval
);
517 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
518 u
->gamelen
= atoi(optval
);
519 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
520 u
->expand_p
= atoi(optval
);
521 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
522 u
->dumpthres
= atoi(optval
);
523 } else if (!strcasecmp(optname
, "best2_ratio") && optval
) {
524 /* If set, prolong simulating while
525 * first_best/second_best playouts ratio
526 * is less than best2_ratio. */
527 u
->best2_ratio
= atof(optval
);
528 } else if (!strcasecmp(optname
, "bestr_ratio") && optval
) {
529 /* If set, prolong simulating while
530 * best,best_best_child values delta
531 * is more than bestr_ratio. */
532 u
->bestr_ratio
= atof(optval
);
533 } else if (!strcasecmp(optname
, "playout_amaf")) {
534 /* Whether to include random playout moves in
535 * AMAF as well. (Otherwise, only tree moves
536 * are included in AMAF. Of course makes sense
537 * only in connection with an AMAF policy.) */
538 /* with-without: 55.5% (+-4.1) */
539 if (optval
&& *optval
== '0')
540 u
->playout_amaf
= false;
542 u
->playout_amaf
= true;
543 } else if (!strcasecmp(optname
, "playout_amaf_nakade")) {
544 /* Whether to include nakade moves from playouts
545 * in the AMAF statistics; this tends to nullify
546 * the playout_amaf effect by adding too much
548 if (optval
&& *optval
== '0')
549 u
->playout_amaf_nakade
= false;
551 u
->playout_amaf_nakade
= true;
552 } else if (!strcasecmp(optname
, "playout_amaf_cutoff") && optval
) {
553 /* Keep only first N% of playout stage AMAF
555 u
->playout_amaf_cutoff
= atoi(optval
);
556 } else if ((!strcasecmp(optname
, "policy") || !strcasecmp(optname
, "random_policy")) && optval
) {
557 char *policyarg
= strchr(optval
, ':');
558 struct uct_policy
**p
= !strcasecmp(optname
, "policy") ? &u
->policy
: &u
->random_policy
;
561 if (!strcasecmp(optval
, "ucb1")) {
562 *p
= policy_ucb1_init(u
, policyarg
);
563 } else if (!strcasecmp(optval
, "ucb1amaf")) {
564 *p
= policy_ucb1amaf_init(u
, policyarg
);
566 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
569 } else if (!strcasecmp(optname
, "playout") && optval
) {
570 char *playoutarg
= strchr(optval
, ':');
573 if (!strcasecmp(optval
, "moggy")) {
574 u
->playout
= playout_moggy_init(playoutarg
, b
);
575 } else if (!strcasecmp(optval
, "light")) {
576 u
->playout
= playout_light_init(playoutarg
, b
);
577 } else if (!strcasecmp(optval
, "elo")) {
578 u
->playout
= playout_elo_init(playoutarg
, b
);
581 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
584 } else if (!strcasecmp(optname
, "prior") && optval
) {
585 u
->prior
= uct_prior_init(optval
, b
);
586 } else if (!strcasecmp(optname
, "amaf_prior") && optval
) {
587 u
->amaf_prior
= atoi(optval
);
588 } else if (!strcasecmp(optname
, "threads") && optval
) {
589 /* By default, Pachi will run with only single
590 * tree search thread! */
591 u
->threads
= atoi(optval
);
592 } else if (!strcasecmp(optname
, "thread_model") && optval
) {
593 if (!strcasecmp(optval
, "root")) {
594 /* Root parallelization - each thread
595 * does independent search, trees are
596 * merged at the end. */
597 u
->thread_model
= TM_ROOT
;
598 u
->parallel_tree
= false;
599 u
->virtual_loss
= false;
600 } else if (!strcasecmp(optval
, "tree")) {
601 /* Tree parallelization - all threads
602 * grind on the same tree. */
603 u
->thread_model
= TM_TREE
;
604 u
->parallel_tree
= true;
605 u
->virtual_loss
= false;
606 } else if (!strcasecmp(optval
, "treevl")) {
607 /* Tree parallelization, but also
608 * with virtual losses - this discou-
609 * rages most threads choosing the
610 * same tree branches to read. */
611 u
->thread_model
= TM_TREEVL
;
612 u
->parallel_tree
= true;
613 u
->virtual_loss
= true;
615 fprintf(stderr
, "UCT: Invalid thread model %s\n", optval
);
618 } else if (!strcasecmp(optname
, "pondering")) {
619 /* Keep searching even during opponent's turn. */
620 u
->pondering_opt
= !optval
|| atoi(optval
);
621 } else if (!strcasecmp(optname
, "fuseki_end") && optval
) {
622 /* At the very beginning it's not worth thinking
623 * too long because the playout evaluations are
624 * very noisy. So gradually increase the thinking
625 * time up to maximum when fuseki_end percent
626 * of the board has been played.
627 * This only applies if we are not in byoyomi. */
628 u
->fuseki_end
= atoi(optval
);
629 } else if (!strcasecmp(optname
, "yose_start") && optval
) {
630 /* When yose_start percent of the board has been
631 * played, or if we are in byoyomi, stop spending
632 * more time and spread the remaining time
634 * Between fuseki_end and yose_start, we spend
635 * a constant proportion of the remaining time
636 * on each move. (yose_start should actually
637 * be much earlier than when real yose start,
638 * but "yose" is a good short name to convey
640 u
->yose_start
= atoi(optval
);
641 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
642 u
->force_seed
= atoi(optval
);
643 } else if (!strcasecmp(optname
, "no_book")) {
645 } else if (!strcasecmp(optname
, "dynkomi") && optval
) {
646 /* Dynamic komi approach; there are multiple
647 * ways to adjust komi dynamically throughout
648 * play. We currently support two: */
649 char *dynkomiarg
= strchr(optval
, ':');
652 if (!strcasecmp(optval
, "none")) {
653 u
->dynkomi
= uct_dynkomi_init_none(u
, dynkomiarg
, b
);
654 } else if (!strcasecmp(optval
, "linear")) {
655 u
->dynkomi
= uct_dynkomi_init_linear(u
, dynkomiarg
, b
);
656 } else if (!strcasecmp(optval
, "adaptive")) {
657 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomiarg
, b
);
659 fprintf(stderr
, "UCT: Invalid dynkomi mode %s\n", optval
);
662 } else if (!strcasecmp(optname
, "dynkomi_mask") && optval
) {
663 /* Bitmask of colors the player must be
664 * for dynkomi be applied; you may want
665 * to use dynkomi_mask=3 to allow dynkomi
666 * even in games where Pachi is white. */
667 u
->dynkomi_mask
= atoi(optval
);
668 } else if (!strcasecmp(optname
, "dynkomi_interval") && optval
) {
669 /* If non-zero, re-adjust dynamic komi
670 * throughout a single genmove reading,
671 * roughly every N simulations. */
672 /* XXX: Does not work with tree
673 * parallelization. */
674 u
->dynkomi_interval
= atoi(optval
);
675 } else if (!strcasecmp(optname
, "val_scale") && optval
) {
676 /* How much of the game result value should be
677 * influenced by win size. Zero means it isn't. */
678 u
->val_scale
= atof(optval
);
679 } else if (!strcasecmp(optname
, "val_points") && optval
) {
680 /* Maximum size of win to be scaled into game
681 * result value. Zero means boardsize^2. */
682 u
->val_points
= atoi(optval
) * 2; // result values are doubled
683 } else if (!strcasecmp(optname
, "val_extra")) {
684 /* If false, the score coefficient will be simply
685 * added to the value, instead of scaling the result
686 * coefficient because of it. */
687 u
->val_extra
= !optval
|| atoi(optval
);
688 } else if (!strcasecmp(optname
, "local_tree") && optval
) {
689 /* Whether to bias exploration by local tree values
690 * (must be supported by the used policy).
692 * 1: Do, value = result.
693 * Try to temper the result:
694 * 2: Do, value = 0.5+(result-expected)/2.
695 * 3: Do, value = 0.5+bzz((result-expected)^2).
696 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
697 u
->local_tree
= atoi(optval
);
698 } else if (!strcasecmp(optname
, "tenuki_d") && optval
) {
699 /* Tenuki distance at which to break the local tree. */
700 u
->tenuki_d
= atoi(optval
);
701 if (u
->tenuki_d
> TREE_NODE_D_MAX
+ 1) {
702 fprintf(stderr
, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX
+ 1);
705 } else if (!strcasecmp(optname
, "local_tree_aging") && optval
) {
706 /* How much to reduce local tree values between moves. */
707 u
->local_tree_aging
= atof(optval
);
708 } else if (!strcasecmp(optname
, "local_tree_allseq")) {
709 /* By default, only complete sequences are stored
710 * in the local tree. If this is on, also
711 * subsequences starting at each move are stored. */
712 u
->local_tree_allseq
= !optval
|| atoi(optval
);
713 } else if (!strcasecmp(optname
, "local_tree_playout")) {
714 /* Whether to adjust ELO playout probability
715 * distributions according to matched localtree
717 u
->local_tree_playout
= !optval
|| atoi(optval
);
718 } else if (!strcasecmp(optname
, "local_tree_pseqroot")) {
719 /* By default, when we have no sequence move
720 * to suggest in-playout, we give up. If this
721 * is on, we make probability distribution from
722 * sequences first moves instead. */
723 u
->local_tree_pseqroot
= !optval
|| atoi(optval
);
724 } else if (!strcasecmp(optname
, "pass_all_alive")) {
725 /* Whether to consider passing only after all
726 * dead groups were removed from the board;
727 * this is like all genmoves are in fact
728 * kgs-genmove_cleanup. */
729 u
->pass_all_alive
= !optval
|| atoi(optval
);
730 } else if (!strcasecmp(optname
, "territory_scoring")) {
731 /* Use territory scoring (default is area scoring).
732 * An explicit kgs-rules command overrides this. */
733 u
->territory_scoring
= !optval
|| atoi(optval
);
734 } else if (!strcasecmp(optname
, "random_policy_chance") && optval
) {
735 /* If specified (N), with probability 1/N, random_policy policy
736 * descend is used instead of main policy descend; useful
737 * if specified policy (e.g. UCB1AMAF) can make unduly biased
738 * choices sometimes, you can fall back to e.g.
739 * random_policy=UCB1. */
740 u
->random_policy_chance
= atoi(optval
);
741 } else if (!strcasecmp(optname
, "max_tree_size") && optval
) {
742 /* Maximum amount of memory [MiB] consumed by the move tree.
743 * For fast_alloc it includes the temp tree used for pruning.
744 * Default is 3072 (3 GiB). Note that if you use TM_ROOT,
745 * this limits size of only one of the trees, not all of them
747 u
->max_tree_size
= atol(optval
) * 1048576;
748 } else if (!strcasecmp(optname
, "fast_alloc")) {
749 u
->fast_alloc
= !optval
|| atoi(optval
);
750 } else if (!strcasecmp(optname
, "slave")) {
751 /* Act as slave for the distributed engine. */
752 u
->slave
= !optval
|| atoi(optval
);
753 } else if (!strcasecmp(optname
, "banner") && optval
) {
754 /* Additional banner string. This must come as the
755 * last engine parameter. */
756 if (*next
) *--next
= ',';
757 u
->banner
= strdup(optval
);
760 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
766 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
767 u
->loss_threshold
= 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
769 u
->policy
= policy_ucb1amaf_init(u
, NULL
);
771 if (!!u
->random_policy_chance
^ !!u
->random_policy
) {
772 fprintf(stderr
, "uct: Only one of random_policy and random_policy_chance is set\n");
776 if (!u
->local_tree
) {
777 /* No ltree aging. */
778 u
->local_tree_aging
= 1.0f
;
781 u
->local_tree_playout
= false;
783 if (u
->fast_alloc
&& !u
->parallel_tree
) {
784 fprintf(stderr
, "fast_alloc not supported with root parallelization.\n");
788 u
->max_tree_size
= (100ULL * u
->max_tree_size
) / (100 + MIN_FREE_MEM_PERCENT
);
791 u
->prior
= uct_prior_init(NULL
, b
);
794 u
->playout
= playout_moggy_init(NULL
, b
);
795 u
->playout
->debug_level
= u
->debug_level
;
797 u
->ownermap
.map
= malloc2(board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
798 u
->stats
= malloc2(board_size2(b
) * sizeof(u
->stats
[0]));
801 u
->dynkomi
= uct_dynkomi_init_linear(u
, NULL
, b
);
803 /* Some things remain uninitialized for now - the opening book
804 * is not loaded and the tree not set up. */
805 /* This will be initialized in setup_state() at the first move
806 * received/requested. This is because right now we are not aware
807 * about any komi or handicap setup and such. */
813 engine_uct_init(char *arg
, struct board
*b
)
815 struct uct
*u
= uct_state_init(arg
, b
);
816 struct engine
*e
= calloc2(1, sizeof(struct engine
));
817 e
->name
= "UCT Engine";
818 e
->printhook
= uct_printhook_ownermap
;
819 e
->notify_play
= uct_notify_play
;
821 e
->genmove
= uct_genmove
;
822 e
->genmoves
= uct_genmoves
;
823 e
->dead_group_list
= uct_dead_group_list
;
827 e
->notify
= uct_notify
;
829 const char banner
[] = "I'm playing UCT. When I'm losing, I will resign, "
830 "if I think I win, I play until you pass. "
831 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
832 if (!u
->banner
) u
->banner
= "";
833 e
->comment
= malloc2(sizeof(banner
) + strlen(u
->banner
) + 1);
834 sprintf(e
->comment
, "%s %s", banner
, u
->banner
);