16 #include "joseki/base.h"
18 #include "playout/moggy.h"
19 #include "playout/light.h"
20 #include "tactics/util.h"
22 #include "uct/dynkomi.h"
23 #include "uct/internal.h"
24 #include "uct/plugins.h"
25 #include "uct/prior.h"
26 #include "uct/search.h"
27 #include "uct/slave.h"
33 struct uct_policy
*policy_ucb1_init(struct uct
*u
, char *arg
);
34 struct uct_policy
*policy_ucb1amaf_init(struct uct
*u
, char *arg
, struct board
*board
);
35 static void uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
);
37 /* Maximal simulation length. */
38 #define MC_GAMELEN MAX_GAMELEN
42 setup_state(struct uct
*u
, struct board
*b
, enum stone color
)
44 u
->t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0,
45 u
->max_pruned_size
, u
->pruning_threshold
, u
->local_tree_aging
, u
->stats_hbits
);
46 if (u
->initial_extra_komi
)
47 u
->t
->extra_komi
= u
->initial_extra_komi
;
49 fast_srandom(u
->force_seed
);
51 fprintf(stderr
, "Fresh board with random seed %lu\n", fast_getseed());
52 if (!u
->no_tbook
&& b
->moves
== 0) {
53 if (color
== S_BLACK
) {
55 } else if (DEBUGL(0)) {
56 fprintf(stderr
, "Warning: First move appears to be white\n");
62 reset_state(struct uct
*u
)
65 tree_done(u
->t
); u
->t
= NULL
;
69 setup_dynkomi(struct uct
*u
, struct board
*b
, enum stone to_play
)
71 if (u
->t
->use_extra_komi
&& !u
->pondering
&& u
->dynkomi
->permove
)
72 u
->t
->extra_komi
= u
->dynkomi
->permove(u
->dynkomi
, b
, u
->t
);
73 else if (!u
->t
->use_extra_komi
)
78 uct_prepare_move(struct uct
*u
, struct board
*b
, enum stone color
)
81 /* Verify that we have sane state. */
83 assert(u
->t
&& b
->moves
);
84 if (color
!= stone_other(u
->t
->root_color
)) {
85 fprintf(stderr
, "Fatal: Non-alternating play detected %d %d\n",
86 color
, u
->t
->root_color
);
89 uct_htable_reset(u
->t
);
92 /* We need fresh state. */
94 setup_state(u
, b
, color
);
97 u
->ownermap
.playouts
= 0;
98 memset(u
->ownermap
.map
, 0, board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
99 u
->played_own
= u
->played_all
= 0;
103 dead_group_list(struct uct
*u
, struct board
*b
, struct move_queue
*mq
, float thres
)
105 enum gj_state gs_array
[board_size2(b
)];
106 struct group_judgement gj
= { .thres
= thres
, .gs
= gs_array
};
107 board_ownermap_judge_groups(b
, &u
->ownermap
, &gj
);
108 groups_of_status(b
, &gj
, GS_DEAD
, mq
);
112 uct_pass_is_safe(struct uct
*u
, struct board
*b
, enum stone color
, bool pass_all_alive
)
114 /* Make sure enough playouts are simulated to get a reasonable dead group list. */
115 while (u
->ownermap
.playouts
< GJ_MINGAMES
)
116 uct_playout(u
, b
, color
, u
->t
);
118 struct move_queue mq
= { .moves
= 0 };
119 dead_group_list(u
, b
, &mq
, GJ_THRES
);
120 if (pass_all_alive
) {
121 for (unsigned int i
= 0; i
< mq
.moves
; i
++) {
122 if (board_at(b
, mq
.move
[i
]) == stone_other(color
)) {
123 return false; // We need to remove opponent dead groups first.
126 mq
.moves
= 0; // our dead stones are alive when pass_all_alive is true
128 if (u
->allow_losing_pass
) {
130 if (board_at(b
, c
) == S_OFFBOARD
)
132 if (board_ownermap_judge_point(&u
->ownermap
, c
, GJ_THRES
) == PJ_UNKNOWN
) {
134 fprintf(stderr
, "uct_pass_is_safe fails at %s[%d]\n", coord2sstr(c
, b
), c
);
135 return false; // Unclear point, clarify first.
140 return pass_is_safe(b
, color
, &mq
);
144 uct_board_print(struct engine
*e
, struct board
*b
, FILE *f
)
146 struct uct
*u
= b
->es
;
147 board_print_ownermap(b
, f
, (u
? &u
->ownermap
: NULL
));
151 uct_owner_map(struct engine
*e
, struct board
*b
, coord_t c
)
153 struct uct
*u
= b
->es
;
154 return board_ownermap_estimate_point(&u
->ownermap
, c
);
158 uct_notify_play(struct engine
*e
, struct board
*b
, struct move
*m
, char *enginearg
)
160 struct uct
*u
= e
->data
;
162 /* No state, create one - this is probably game beginning
163 * and we need to load the opening tbook right now. */
164 uct_prepare_move(u
, b
, m
->color
);
168 /* Stop pondering, required by tree_promote_at() */
169 uct_pondering_stop(u
);
170 if (UDEBUGL(2) && u
->slave
)
171 tree_dump(u
->t
, u
->dumpthres
);
173 if (is_resign(m
->coord
)) {
179 /* Promote node of the appropriate move to the tree root. */
181 if (u
->t
->untrustworthy_tree
| !tree_promote_at(u
->t
, b
, m
->coord
)) {
183 if (u
->t
->untrustworthy_tree
)
184 fprintf(stderr
, "Not promoting move node in untrustworthy tree.\n");
186 fprintf(stderr
, "Warning: Cannot promote move node! Several play commands in row?\n");
188 /* Preserve dynamic komi information, though, that is important. */
189 u
->initial_extra_komi
= u
->t
->extra_komi
;
194 /* If we are a slave in a distributed engine, start pondering once
195 * we know which move we actually played. See uct_genmove() about
196 * the check for pass. */
197 if (u
->pondering_opt
&& u
->slave
&& m
->color
== u
->my_color
&& !is_pass(m
->coord
))
198 uct_pondering_start(u
, b
, u
->t
, stone_other(m
->color
));
204 uct_undo(struct engine
*e
, struct board
*b
)
206 struct uct
*u
= e
->data
;
208 if (!u
->t
) return NULL
;
209 uct_pondering_stop(u
);
210 u
->initial_extra_komi
= u
->t
->extra_komi
;
216 uct_result(struct engine
*e
, struct board
*b
)
218 struct uct
*u
= e
->data
;
219 static char reply
[1024];
223 enum stone color
= u
->t
->root_color
;
224 struct tree_node
*n
= u
->t
->root
;
225 snprintf(reply
, 1024, "%s %s %d %.2f %.1f",
226 stone2str(color
), coord2sstr(node_coord(n
), b
),
227 n
->u
.playouts
, tree_node_get_value(u
->t
, -1, n
->u
.value
),
228 u
->t
->use_extra_komi
? u
->t
->extra_komi
: 0);
233 uct_chat(struct engine
*e
, struct board
*b
, bool opponent
, char *from
, char *cmd
)
235 struct uct
*u
= e
->data
;
238 return generic_chat(b
, opponent
, from
, cmd
, S_NONE
, pass
, 0, 1, u
->threads
, 0.0, 0.0);
240 struct tree_node
*n
= u
->t
->root
;
241 double winrate
= tree_node_get_value(u
->t
, -1, n
->u
.value
);
242 double extra_komi
= u
->t
->use_extra_komi
&& fabs(u
->t
->extra_komi
) >= 0.5 ? u
->t
->extra_komi
: 0;
244 return generic_chat(b
, opponent
, from
, cmd
, u
->t
->root_color
, node_coord(n
), n
->u
.playouts
, 1,
245 u
->threads
, winrate
, extra_komi
);
249 print_dead_groups(struct uct
*u
, struct board
*b
, struct move_queue
*mq
)
251 fprintf(stderr
, "dead groups (playing %s)\n", (u
->my_color
? stone2str(u
->my_color
) : "???"));
253 fprintf(stderr
, " none\n");
254 for (unsigned int i
= 0; i
< mq
->moves
; i
++) {
255 fprintf(stderr
, " ");
256 foreach_in_group(b
, mq
->move
[i
]) {
257 fprintf(stderr
, "%s ", coord2sstr(c
, b
));
258 } foreach_in_group_end
;
259 fprintf(stderr
, "\n");
264 print_extra_dead_group(struct board
*b
, group_t g
, int found
)
267 fprintf(stderr
, "also adding\n");
268 fprintf(stderr
, " ");
269 foreach_in_group(b
, g
) {
270 fprintf(stderr
, "%s ", coord2sstr(c
, b
));
271 } foreach_in_group_end
;
272 fprintf(stderr
, "\n");
276 uct_dead_group_list(struct engine
*e
, struct board
*b
, struct move_queue
*mq
)
278 struct uct
*u
= e
->data
;
279 bool unknown_color
= !u
->my_color
;
281 /* This means the game is probably over, no use pondering on. */
282 uct_pondering_stop(u
);
284 if (u
->pass_all_alive
)
285 return; // no dead groups
287 /* Create mock state */
288 if (u
->t
) reset_state(u
);
289 // We need S_BLACK here, but don't clobber u->my_color with uct_genmove_setup() !
290 uct_prepare_move(u
, b
, S_BLACK
);
292 /* Make sure the ownermap is well-seeded. */
293 while (u
->ownermap
.playouts
< GJ_MINGAMES
)
294 uct_playout(u
, b
, S_BLACK
, u
->t
);
295 /* Show the ownermap: */
297 board_print_ownermap(b
, stderr
, &u
->ownermap
);
299 struct move_queue relaxed
; relaxed
.moves
= 0;
300 dead_group_list(u
, b
, mq
, GJ_THRES
); // Strict
301 dead_group_list(u
, b
, &relaxed
, 0.55); // Relaxed
302 if (DEBUGL(2)) print_dead_groups(u
, b
, mq
);
304 /* Add own unclear dead groups if it doesn't change the outcome
305 * and spare opponent a genmove_cleanup phase... */
306 if (!unknown_color
) {
308 bool result
= pass_is_safe(b
, u
->my_color
, mq
);
309 for (unsigned int i
= 0; i
< relaxed
.moves
; i
++) {
310 group_t g
= relaxed
.move
[i
];
311 if (board_at(b
, g
) != u
->my_color
|| mq_has(mq
, g
))
314 struct move_queue tmp
; memcpy(&tmp
, mq
, sizeof(tmp
));
316 if (result
== pass_is_safe(b
, u
->my_color
, &tmp
)) {
318 if (DEBUGL(2)) print_extra_dead_group(b
, g
, found
++);
323 /* Clean up the mock state in case we will receive
324 * a genmove; we could get a non-alternating-move
325 * error from uct_prepare_move() in that case otherwise. */
330 uct_stop(struct engine
*e
)
332 /* This is called on game over notification. However, an undo
333 * and game resume can follow, so don't panic yet and just
334 * relax and stop thinking so that we don't waste CPU. */
335 struct uct
*u
= e
->data
;
336 uct_pondering_stop(u
);
340 uct_done(struct engine
*e
)
342 /* This is called on engine reset, especially when clear_board
343 * is received and new game should begin. */
346 struct uct
*u
= e
->data
;
347 uct_pondering_stop(u
);
348 if (u
->t
) reset_state(u
);
349 if (u
->dynkomi
) u
->dynkomi
->done(u
->dynkomi
);
350 free(u
->ownermap
.map
);
352 if (u
->policy
) u
->policy
->done(u
->policy
);
353 if (u
->random_policy
) u
->random_policy
->done(u
->random_policy
);
354 playout_policy_done(u
->playout
);
355 uct_prior_done(u
->prior
);
356 joseki_done(u
->jdict
);
357 pluginset_done(u
->plugins
);
362 /* Run time-limited MCTS search on foreground. */
364 uct_search(struct uct
*u
, struct board
*b
, struct time_info
*ti
, enum stone color
, struct tree
*t
, bool print_progress
)
366 struct uct_search_state s
;
367 uct_search_start(u
, b
, color
, t
, ti
, &s
);
368 if (UDEBUGL(2) && s
.base_playouts
> 0)
369 fprintf(stderr
, "<pre-simulated %d games>\n", s
.base_playouts
);
371 /* The search tree is ctx->t. This is currently == . It is important
372 * to reference ctx->t directly since the
373 * thread manager will swap the tree pointer asynchronously. */
375 /* Now, just periodically poll the search tree. */
376 /* Note that in case of TD_GAMES, threads will not wait for
377 * the uct_search_check_stop() signalization. */
379 time_sleep(TREE_BUSYWAIT_INTERVAL
);
380 /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
381 * time control is broken. But if it happens to be less, we still search
382 * at least 100ms otherwise the move is completely random. */
384 int i
= uct_search_games(&s
);
385 /* Print notifications etc. */
386 uct_search_progress(u
, b
, color
, t
, ti
, &s
, i
);
387 /* Check if we should stop the search. */
388 if (uct_search_check_stop(u
, b
, color
, t
, ti
, &s
, i
))
392 struct uct_thread_ctx
*ctx
= uct_search_stop();
393 if (UDEBUGL(2)) tree_dump(t
, u
->dumpthres
);
395 fprintf(stderr
, "(avg score %f/%d; dynkomi's %f/%d value %f/%d)\n",
396 t
->avg_score
.value
, t
->avg_score
.playouts
,
397 u
->dynkomi
->score
.value
, u
->dynkomi
->score
.playouts
,
398 u
->dynkomi
->value
.value
, u
->dynkomi
->value
.playouts
);
400 uct_progress_status(u
, t
, color
, ctx
->games
, NULL
);
402 if (u
->debug_after
.playouts
> 0) {
403 /* Now, start an additional run of playouts, single threaded. */
404 struct time_info debug_ti
= {
408 debug_ti
.len
.games
= t
->root
->u
.playouts
+ u
->debug_after
.playouts
;
410 board_print_ownermap(b
, stderr
, &u
->ownermap
);
411 fprintf(stderr
, "--8<-- UCT debug post-run begin (%d:%d) --8<--\n", u
->debug_after
.level
, u
->debug_after
.playouts
);
413 int debug_level_save
= debug_level
;
414 int u_debug_level_save
= u
->debug_level
;
415 int p_debug_level_save
= u
->playout
->debug_level
;
416 debug_level
= u
->debug_after
.level
;
417 u
->debug_level
= u
->debug_after
.level
;
418 u
->playout
->debug_level
= u
->debug_after
.level
;
421 uct_playouts(u
, b
, color
, t
, &debug_ti
);
422 tree_dump(t
, u
->dumpthres
);
425 debug_level
= debug_level_save
;
426 u
->debug_level
= u_debug_level_save
;
427 u
->playout
->debug_level
= p_debug_level_save
;
429 fprintf(stderr
, "--8<-- UCT debug post-run finished --8<--\n");
432 u
->played_own
+= ctx
->games
;
436 /* Start pondering background with @color to play. */
438 uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
)
441 fprintf(stderr
, "Starting to ponder with color %s\n", stone2str(stone_other(color
)));
444 /* We need a local board copy to ponder upon. */
445 struct board
*b
= malloc2(sizeof(*b
)); board_copy(b
, b0
);
447 /* *b0 did not have the genmove'd move played yet. */
448 struct move m
= { node_coord(t
->root
), t
->root_color
};
449 int res
= board_play(b
, &m
);
451 setup_dynkomi(u
, b
, stone_other(m
.color
));
453 /* Start MCTS manager thread "headless". */
454 static struct uct_search_state s
;
455 uct_search_start(u
, b
, color
, t
, NULL
, &s
);
458 /* uct_search_stop() frontend for the pondering (non-genmove) mode, and
459 * to stop the background search for a slave in the distributed engine. */
461 uct_pondering_stop(struct uct
*u
)
463 if (!thread_manager_running
)
466 /* Stop the thread manager. */
467 struct uct_thread_ctx
*ctx
= uct_search_stop();
469 if (u
->pondering
) fprintf(stderr
, "(pondering) ");
470 uct_progress_status(u
, ctx
->t
, ctx
->color
, ctx
->games
, NULL
);
474 u
->pondering
= false;
480 uct_genmove_setup(struct uct
*u
, struct board
*b
, enum stone color
)
482 if (b
->superko_violation
) {
483 fprintf(stderr
, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
484 fprintf(stderr
, "Maybe you play with situational instead of positional superko?\n");
485 fprintf(stderr
, "I'm going to ignore the violation, but note that I may miss\n");
486 fprintf(stderr
, "some moves valid under this ruleset because of this.\n");
487 b
->superko_violation
= false;
490 uct_prepare_move(u
, b
, color
);
495 /* How to decide whether to use dynkomi in this game? Since we use
496 * pondering, it's not simple "who-to-play" matter. Decide based on
497 * the last genmove issued. */
498 u
->t
->use_extra_komi
= !!(u
->dynkomi_mask
& color
);
499 setup_dynkomi(u
, b
, color
);
501 if (b
->rules
== RULES_JAPANESE
)
502 u
->territory_scoring
= true;
504 /* Make pessimistic assumption about komi for Japanese rules to
505 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
506 * The rules usually give the same winner if the integer part of komi
507 * is odd so we adjust the komi only if it is even (for a board of
508 * odd size). We are not trying to get an exact evaluation for rare
509 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
510 if (u
->territory_scoring
&& (((int)floor(b
->komi
) + board_size(b
)) & 1)) {
511 b
->komi
+= (color
== S_BLACK
? 1.0 : -1.0);
513 fprintf(stderr
, "Setting komi to %.1f assuming Japanese rules\n",
519 uct_live_gfx_hook(struct engine
*e
)
521 struct uct
*u
= e
->data
;
522 /* Hack: Override reportfreq to get decent update rates in GoGui */
523 u
->reportfreq
= 1000;
526 /* Kindof like uct_genmove() but just find the best candidates */
528 uct_best_moves(struct engine
*e
, struct board
*b
, enum stone color
)
530 struct time_info ti
= { .period
= TT_NULL
};
531 double start_time
= time_now();
532 struct uct
*u
= e
->data
;
533 uct_pondering_stop(u
);
536 uct_genmove_setup(u
, b
, color
);
538 /* Start the Monte Carlo Tree Search! */
539 int base_playouts
= u
->t
->root
->u
.playouts
;
540 int played_games
= uct_search(u
, b
, &ti
, color
, u
->t
, false);
543 uct_search_result(u
, b
, color
, u
->pass_all_alive
, played_games
, base_playouts
, &best_coord
);
546 double time
= time_now() - start_time
+ 0.000001; /* avoid divide by zero */
547 fprintf(stderr
, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
548 time
, (int)(played_games
/time
), (int)(played_games
/time
/u
->threads
));
551 uct_progress_status(u
, u
->t
, color
, played_games
, &best_coord
);
556 uct_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
558 double start_time
= time_now();
559 struct uct
*u
= e
->data
;
560 u
->pass_all_alive
|= pass_all_alive
;
561 uct_pondering_stop(u
);
564 // dcnn hack: reset state to make dcnn priors kick in.
565 // FIXME this makes pondering useless when using dcnn ...
567 u
->initial_extra_komi
= u
->t
->extra_komi
;
572 uct_genmove_setup(u
, b
, color
);
574 /* Start the Monte Carlo Tree Search! */
575 int base_playouts
= u
->t
->root
->u
.playouts
;
576 int played_games
= uct_search(u
, b
, ti
, color
, u
->t
, false);
579 struct tree_node
*best
;
580 best
= uct_search_result(u
, b
, color
, u
->pass_all_alive
, played_games
, base_playouts
, &best_coord
);
583 double time
= time_now() - start_time
+ 0.000001; /* avoid divide by zero */
584 fprintf(stderr
, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
585 time
, (int)(played_games
/time
), (int)(played_games
/time
/u
->threads
));
588 uct_progress_status(u
, u
->t
, color
, played_games
, &best_coord
);
591 /* Pass or resign. */
592 if (is_pass(best_coord
))
593 u
->initial_extra_komi
= u
->t
->extra_komi
;
595 return coord_copy(best_coord
);
598 if (!u
->t
->untrustworthy_tree
) {
599 tree_promote_node(u
->t
, &best
);
601 /* Throw away an untrustworthy tree. */
602 /* Preserve dynamic komi information, though, that is important. */
603 u
->initial_extra_komi
= u
->t
->extra_komi
;
607 /* After a pass, pondering is harmful for two reasons:
608 * (i) We might keep pondering even when the game is over.
609 * Of course this is the case for opponent resign as well.
610 * (ii) More importantly, the ownermap will get skewed since
611 * the UCT will start cutting off any playouts. */
612 if (u
->pondering_opt
&& u
->t
&& !is_pass(node_coord(best
))) {
613 uct_pondering_start(u
, b
, u
->t
, stone_other(color
));
615 return coord_copy(best_coord
);
620 uct_gentbook(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
)
622 struct uct
*u
= e
->data
;
623 if (!u
->t
) uct_prepare_move(u
, b
, color
);
626 if (ti
->dim
== TD_GAMES
) {
627 /* Don't count in games that already went into the tbook. */
628 ti
->len
.games
+= u
->t
->root
->u
.playouts
;
630 uct_search(u
, b
, ti
, color
, u
->t
, true);
632 assert(ti
->dim
== TD_GAMES
);
633 tree_save(u
->t
, b
, ti
->len
.games
/ 100);
639 uct_dumptbook(struct engine
*e
, struct board
*b
, enum stone color
)
641 struct uct
*u
= e
->data
;
642 struct tree
*t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0,
643 u
->max_pruned_size
, u
->pruning_threshold
, u
->local_tree_aging
, 0);
651 uct_evaluate_one(struct engine
*e
, struct board
*b
, struct time_info
*ti
, coord_t c
, enum stone color
)
653 struct uct
*u
= e
->data
;
657 struct move m
= { c
, color
};
658 int res
= board_play(&b2
, &m
);
661 color
= stone_other(color
);
663 if (u
->t
) reset_state(u
);
664 uct_prepare_move(u
, &b2
, color
);
668 uct_search(u
, &b2
, ti
, color
, u
->t
, true);
669 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, &b2
, color
, resign
);
671 bestval
= NAN
; // the opponent has no reply!
673 bestval
= tree_node_get_value(u
->t
, 1, best
->u
.value
);
676 reset_state(u
); // clean our junk
678 return isnan(bestval
) ? NAN
: 1.0f
- bestval
;
682 uct_evaluate(struct engine
*e
, struct board
*b
, struct time_info
*ti
, floating_t
*vals
, enum stone color
)
684 for (int i
= 0; i
< b
->flen
; i
++) {
685 if (is_pass(b
->f
[i
]))
688 vals
[i
] = uct_evaluate_one(e
, b
, ti
, b
->f
[i
], color
);
694 uct_state_init(char *arg
, struct board
*b
)
696 struct uct
*u
= calloc2(1, sizeof(struct uct
));
697 bool pat_setup
= false;
699 u
->debug_level
= debug_level
;
700 u
->reportfreq
= 10000;
701 u
->gamelen
= MC_GAMELEN
;
702 u
->resign_threshold
= 0.2;
703 u
->sure_win_threshold
= 0.95;
705 u
->significant_threshold
= 50;
708 u
->playout_amaf
= true;
709 u
->amaf_prior
= false;
710 u
->max_tree_size
= 1408ULL * 1048576;
711 u
->fast_alloc
= true;
712 u
->pruning_threshold
= 0;
715 u
->thread_model
= TM_TREEVL
;
718 u
->pondering_opt
= true;
720 u
->fuseki_end
= 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
721 u
->yose_start
= 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
722 u
->bestr_ratio
= 0.02;
723 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
724 // TODO: Further tuning and experiments with better time allocation schemes.
725 u
->best2_ratio
= 2.5;
726 // Higher values of max_maintime_ratio sometimes cause severe time trouble in tournaments
727 // It might be necessary to reduce it to 1.5 on large board, but more tuning is needed.
728 u
->max_maintime_ratio
= 2.0;
730 u
->val_scale
= 0; u
->val_points
= 40;
731 u
->dynkomi_interval
= 1000;
732 u
->dynkomi_mask
= S_BLACK
| S_WHITE
;
735 u
->local_tree_aging
= 80;
736 u
->local_tree_depth_decay
= 1.5;
737 u
->local_tree_eval
= LTE_ROOT
;
738 u
->local_tree_neival
= true;
742 u
->stats_delay
= 0.01; // 10 ms
743 u
->shared_levels
= 1;
745 u
->plugins
= pluginset_init(b
);
747 u
->jdict
= joseki_load(b
->size
);
750 char *optspec
, *next
= arg
;
753 next
+= strcspn(next
, ",");
754 if (*next
) { *next
++ = 0; } else { *next
= 0; }
756 char *optname
= optspec
;
757 char *optval
= strchr(optspec
, '=');
758 if (optval
) *optval
++ = 0;
762 if (!strcasecmp(optname
, "debug")) {
764 u
->debug_level
= atoi(optval
);
767 } else if (!strcasecmp(optname
, "reporting") && optval
) {
768 /* The format of output for detailed progress
769 * information (such as current best move and
770 * its value, etc.). */
771 if (!strcasecmp(optval
, "text")) {
772 /* Plaintext traditional output. */
773 u
->reporting
= UR_TEXT
;
774 } else if (!strcasecmp(optval
, "json")) {
775 /* JSON output. Implies debug=0. */
776 u
->reporting
= UR_JSON
;
778 } else if (!strcasecmp(optval
, "jsonbig")) {
779 /* JSON output, but much more detailed.
780 * Implies debug=0. */
781 u
->reporting
= UR_JSON_BIG
;
784 fprintf(stderr
, "UCT: Invalid reporting format %s\n", optval
);
787 } else if (!strcasecmp(optname
, "reportfreq") && optval
) {
788 /* The progress information line will be shown
789 * every <reportfreq> simulations. */
790 u
->reportfreq
= atoi(optval
);
791 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
792 /* When dumping the UCT tree on output, include
793 * nodes with at least this many playouts.
794 * (A fraction of the total # of playouts at the
796 /* Use 0 to list all nodes with at least one
797 * simulation, and -1 to list _all_ nodes. */
798 u
->dumpthres
= atof(optval
);
799 } else if (!strcasecmp(optname
, "resign_threshold") && optval
) {
800 /* Resign when this ratio of games is lost
801 * after GJ_MINGAMES sample is taken. */
802 u
->resign_threshold
= atof(optval
);
803 } else if (!strcasecmp(optname
, "sure_win_threshold") && optval
) {
804 /* Stop reading when this ratio of games is won
805 * after PLAYOUT_EARLY_BREAK_MIN sample is
806 * taken. (Prevents stupid time losses,
807 * friendly to human opponents.) */
808 u
->sure_win_threshold
= atof(optval
);
809 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
810 /* Set RNG seed at the tree setup. */
811 u
->force_seed
= atoi(optval
);
812 } else if (!strcasecmp(optname
, "no_tbook")) {
813 /* Disable UCT opening tbook. */
815 } else if (!strcasecmp(optname
, "pass_all_alive")) {
816 /* Whether to consider passing only after all
817 * dead groups were removed from the board;
818 * this is like all genmoves are in fact
819 * kgs-genmove_cleanup. */
820 u
->pass_all_alive
= !optval
|| atoi(optval
);
821 } else if (!strcasecmp(optname
, "allow_losing_pass")) {
822 /* Whether to consider passing in a clear
823 * but losing situation, to be scored as a loss
825 u
->allow_losing_pass
= !optval
|| atoi(optval
);
826 } else if (!strcasecmp(optname
, "territory_scoring")) {
827 /* Use territory scoring (default is area scoring).
828 * An explicit kgs-rules command overrides this. */
829 u
->territory_scoring
= !optval
|| atoi(optval
);
830 } else if (!strcasecmp(optname
, "stones_only")) {
831 /* Do not count eyes. Nice to teach go to kids.
832 * http://strasbourg.jeudego.org/regle_strasbourgeoise.htm */
833 b
->rules
= RULES_STONES_ONLY
;
834 u
->pass_all_alive
= true;
835 } else if (!strcasecmp(optname
, "debug_after")) {
836 /* debug_after=9:1000 will make Pachi think under
837 * the normal conditions, but at the point when
838 * a move is to be chosen, the tree is dumped and
839 * another 1000 simulations are run single-threaded
840 * with debug level 9, allowing inspection of Pachi's
841 * behavior after it has thought a lot. */
843 u
->debug_after
.level
= atoi(optval
);
844 char *playouts
= strchr(optval
, ':');
846 u
->debug_after
.playouts
= atoi(playouts
+1);
848 u
->debug_after
.playouts
= 1000;
850 u
->debug_after
.level
= 9;
851 u
->debug_after
.playouts
= 1000;
853 } else if (!strcasecmp(optname
, "banner") && optval
) {
854 /* Additional banner string. This must come as the
855 * last engine parameter. You can use '+' instead
856 * of ' ' if you are wrestling with kgsGtp. */
857 if (*next
) *--next
= ',';
858 u
->banner
= strdup(optval
);
859 for (char *b
= u
->banner
; *b
; b
++) {
860 if (*b
== '+') *b
= ' ';
863 } else if (!strcasecmp(optname
, "plugin") && optval
) {
864 /* Load an external plugin; filename goes before the colon,
865 * extra arguments after the colon. */
866 char *pluginarg
= strchr(optval
, ':');
869 plugin_load(u
->plugins
, optval
, pluginarg
);
871 /** UCT behavior and policies */
873 } else if ((!strcasecmp(optname
, "policy")
874 /* Node selection policy. ucb1amaf is the
875 * default policy implementing RAVE, while
876 * ucb1 is the simple exploration/exploitation
877 * policy. Policies can take further extra
879 || !strcasecmp(optname
, "random_policy")) && optval
) {
880 /* A policy to be used randomly with small
881 * chance instead of the default policy. */
882 char *policyarg
= strchr(optval
, ':');
883 struct uct_policy
**p
= !strcasecmp(optname
, "policy") ? &u
->policy
: &u
->random_policy
;
886 if (!strcasecmp(optval
, "ucb1")) {
887 *p
= policy_ucb1_init(u
, policyarg
);
888 } else if (!strcasecmp(optval
, "ucb1amaf")) {
889 *p
= policy_ucb1amaf_init(u
, policyarg
, b
);
891 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
894 } else if (!strcasecmp(optname
, "playout") && optval
) {
895 /* Random simulation (playout) policy.
896 * moggy is the default policy with large
897 * amount of domain-specific knowledge and
898 * heuristics. light is a simple uniformly
899 * random move selection policy. */
900 char *playoutarg
= strchr(optval
, ':');
903 if (!strcasecmp(optval
, "moggy")) {
904 u
->playout
= playout_moggy_init(playoutarg
, b
, u
->jdict
);
905 } else if (!strcasecmp(optval
, "light")) {
906 u
->playout
= playout_light_init(playoutarg
, b
);
908 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
911 } else if (!strcasecmp(optname
, "prior") && optval
) {
912 /* Node priors policy. When expanding a node,
913 * it will seed node values heuristically
914 * (most importantly, based on playout policy
915 * opinion, but also with regard to other
916 * things). See uct/prior.c for details.
917 * Use prior=eqex=0 to disable priors. */
918 u
->prior
= uct_prior_init(optval
, b
, u
);
919 } else if (!strcasecmp(optname
, "mercy") && optval
) {
920 /* Minimal difference of black/white captures
921 * to stop playout - "Mercy Rule". Speeds up
922 * hopeless playouts at the expense of some
924 u
->mercymin
= atoi(optval
);
925 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
926 /* Maximum length of single simulation
928 u
->gamelen
= atoi(optval
);
929 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
930 /* Expand UCT nodes after it has been
931 * visited this many times. */
932 u
->expand_p
= atoi(optval
);
933 } else if (!strcasecmp(optname
, "random_policy_chance") && optval
) {
934 /* If specified (N), with probability 1/N, random_policy policy
935 * descend is used instead of main policy descend; useful
936 * if specified policy (e.g. UCB1AMAF) can make unduly biased
937 * choices sometimes, you can fall back to e.g.
938 * random_policy=UCB1. */
939 u
->random_policy_chance
= atoi(optval
);
941 /** General AMAF behavior */
942 /* (Only relevant if the policy supports AMAF.
943 * More variables can be tuned as policy
946 } else if (!strcasecmp(optname
, "playout_amaf")) {
947 /* Whether to include random playout moves in
948 * AMAF as well. (Otherwise, only tree moves
949 * are included in AMAF. Of course makes sense
950 * only in connection with an AMAF policy.) */
951 /* with-without: 55.5% (+-4.1) */
952 if (optval
&& *optval
== '0')
953 u
->playout_amaf
= false;
955 u
->playout_amaf
= true;
956 } else if (!strcasecmp(optname
, "playout_amaf_cutoff") && optval
) {
957 /* Keep only first N% of playout stage AMAF
959 u
->playout_amaf_cutoff
= atoi(optval
);
960 } else if (!strcasecmp(optname
, "amaf_prior") && optval
) {
961 /* In node policy, consider prior values
962 * part of the real result term or part
963 * of the AMAF term? */
964 u
->amaf_prior
= atoi(optval
);
966 /** Performance and memory management */
968 } else if (!strcasecmp(optname
, "threads") && optval
) {
969 /* By default, Pachi will run with only single
970 * tree search thread! */
971 u
->threads
= atoi(optval
);
972 } else if (!strcasecmp(optname
, "thread_model") && optval
) {
973 if (!strcasecmp(optval
, "tree")) {
974 /* Tree parallelization - all threads
975 * grind on the same tree. */
976 u
->thread_model
= TM_TREE
;
978 } else if (!strcasecmp(optval
, "treevl")) {
979 /* Tree parallelization, but also
980 * with virtual losses - this discou-
981 * rages most threads choosing the
982 * same tree branches to read. */
983 u
->thread_model
= TM_TREEVL
;
985 fprintf(stderr
, "UCT: Invalid thread model %s\n", optval
);
988 } else if (!strcasecmp(optname
, "virtual_loss") && optval
) {
989 /* Number of virtual losses added before evaluating a node. */
990 u
->virtual_loss
= atoi(optval
);
991 } else if (!strcasecmp(optname
, "pondering")) {
992 /* Keep searching even during opponent's turn. */
993 u
->pondering_opt
= !optval
|| atoi(optval
);
994 } else if (!strcasecmp(optname
, "max_tree_size") && optval
) {
995 /* Maximum amount of memory [MiB] consumed by the move tree.
996 * For fast_alloc it includes the temp tree used for pruning.
997 * Default is 3072 (3 GiB). */
998 u
->max_tree_size
= atol(optval
) * 1048576;
999 } else if (!strcasecmp(optname
, "fast_alloc")) {
1000 u
->fast_alloc
= !optval
|| atoi(optval
);
1001 } else if (!strcasecmp(optname
, "pruning_threshold") && optval
) {
1002 /* Force pruning at beginning of a move if the tree consumes
1003 * more than this [MiB]. Default is 10% of max_tree_size.
1004 * Increase to reduce pruning time overhead if memory is plentiful.
1005 * This option is meaningful only for fast_alloc. */
1006 u
->pruning_threshold
= atol(optval
) * 1048576;
1010 } else if (!strcasecmp(optname
, "best2_ratio") && optval
) {
1011 /* If set, prolong simulating while
1012 * first_best/second_best playouts ratio
1013 * is less than best2_ratio. */
1014 u
->best2_ratio
= atof(optval
);
1015 } else if (!strcasecmp(optname
, "bestr_ratio") && optval
) {
1016 /* If set, prolong simulating while
1017 * best,best_best_child values delta
1018 * is more than bestr_ratio. */
1019 u
->bestr_ratio
= atof(optval
);
1020 } else if (!strcasecmp(optname
, "max_maintime_ratio") && optval
) {
1021 /* If set and while not in byoyomi, prolong simulating no more than
1022 * max_maintime_ratio times the normal desired thinking time. */
1023 u
->max_maintime_ratio
= atof(optval
);
1024 } else if (!strcasecmp(optname
, "fuseki_end") && optval
) {
1025 /* At the very beginning it's not worth thinking
1026 * too long because the playout evaluations are
1027 * very noisy. So gradually increase the thinking
1028 * time up to maximum when fuseki_end percent
1029 * of the board has been played.
1030 * This only applies if we are not in byoyomi. */
1031 u
->fuseki_end
= atoi(optval
);
1032 } else if (!strcasecmp(optname
, "yose_start") && optval
) {
1033 /* When yose_start percent of the board has been
1034 * played, or if we are in byoyomi, stop spending
1035 * more time and spread the remaining time
1037 * Between fuseki_end and yose_start, we spend
1038 * a constant proportion of the remaining time
1039 * on each move. (yose_start should actually
1040 * be much earlier than when real yose start,
1041 * but "yose" is a good short name to convey
1043 u
->yose_start
= atoi(optval
);
1047 } else if (!strcasecmp(optname
, "dynkomi") && optval
) {
1048 /* Dynamic komi approach; there are multiple
1049 * ways to adjust komi dynamically throughout
1050 * play. We currently support two: */
1051 char *dynkomiarg
= strchr(optval
, ':');
1054 if (!strcasecmp(optval
, "none")) {
1055 u
->dynkomi
= uct_dynkomi_init_none(u
, dynkomiarg
, b
);
1056 } else if (!strcasecmp(optval
, "linear")) {
1057 /* You should set dynkomi_mask=1 or a very low
1058 * handicap_value for white. */
1059 u
->dynkomi
= uct_dynkomi_init_linear(u
, dynkomiarg
, b
);
1060 } else if (!strcasecmp(optval
, "adaptive")) {
1061 /* There are many more knobs to
1062 * crank - see uct/dynkomi.c. */
1063 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomiarg
, b
);
1065 fprintf(stderr
, "UCT: Invalid dynkomi mode %s\n", optval
);
1068 } else if (!strcasecmp(optname
, "dynkomi_mask") && optval
) {
1069 /* Bitmask of colors the player must be
1070 * for dynkomi be applied; the default dynkomi_mask=3 allows
1071 * dynkomi even in games where Pachi is white. */
1072 u
->dynkomi_mask
= atoi(optval
);
1073 } else if (!strcasecmp(optname
, "dynkomi_interval") && optval
) {
1074 /* If non-zero, re-adjust dynamic komi
1075 * throughout a single genmove reading,
1076 * roughly every N simulations. */
1077 /* XXX: Does not work with tree
1078 * parallelization. */
1079 u
->dynkomi_interval
= atoi(optval
);
1080 } else if (!strcasecmp(optname
, "extra_komi") && optval
) {
1081 /* Initial dynamic komi settings. This
1082 * is useful for the adaptive dynkomi
1083 * policy as the value to start with
1084 * (this is NOT kept fixed) in case
1085 * there is not enough time in the search
1086 * to adjust the value properly (e.g. the
1087 * game was interrupted). */
1088 u
->initial_extra_komi
= atof(optval
);
1090 /** Node value result scaling */
1092 } else if (!strcasecmp(optname
, "val_scale") && optval
) {
1093 /* How much of the game result value should be
1094 * influenced by win size. Zero means it isn't. */
1095 u
->val_scale
= atof(optval
);
1096 } else if (!strcasecmp(optname
, "val_points") && optval
) {
1097 /* Maximum size of win to be scaled into game
1098 * result value. Zero means boardsize^2. */
1099 u
->val_points
= atoi(optval
) * 2; // result values are doubled
1100 } else if (!strcasecmp(optname
, "val_extra")) {
1101 /* If false, the score coefficient will be simply
1102 * added to the value, instead of scaling the result
1103 * coefficient because of it. */
1104 u
->val_extra
= !optval
|| atoi(optval
);
1105 } else if (!strcasecmp(optname
, "val_byavg")) {
1106 /* If true, the score included in the value will
1107 * be relative to average score in the current
1108 * search episode inst. of jigo. */
1109 u
->val_byavg
= !optval
|| atoi(optval
);
1110 } else if (!strcasecmp(optname
, "val_bytemp")) {
1111 /* If true, the value scaling coefficient
1112 * is different based on value extremity
1113 * (dist. from 0.5), linear between
1114 * val_bytemp_min, val_scale. */
1115 u
->val_bytemp
= !optval
|| atoi(optval
);
1116 } else if (!strcasecmp(optname
, "val_bytemp_min") && optval
) {
1117 /* Minimum val_scale in case of val_bytemp. */
1118 u
->val_bytemp_min
= atof(optval
);
1121 /* (Purely experimental. Does not work - yet!) */
1123 } else if (!strcasecmp(optname
, "local_tree")) {
1124 /* Whether to bias exploration by local tree values. */
1125 u
->local_tree
= !optval
|| atoi(optval
);
1126 } else if (!strcasecmp(optname
, "tenuki_d") && optval
) {
1127 /* Tenuki distance at which to break the local tree. */
1128 u
->tenuki_d
= atoi(optval
);
1129 if (u
->tenuki_d
> TREE_NODE_D_MAX
+ 1) {
1130 fprintf(stderr
, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX
+ 1);
1133 } else if (!strcasecmp(optname
, "local_tree_aging") && optval
) {
1134 /* How much to reduce local tree values between moves. */
1135 u
->local_tree_aging
= atof(optval
);
1136 } else if (!strcasecmp(optname
, "local_tree_depth_decay") && optval
) {
1137 /* With value x>0, during the descent the node
1138 * contributes 1/x^depth playouts in
1139 * the local tree. I.e., with x>1, nodes more
1140 * distant from local situation contribute more
1141 * than nodes near the root. */
1142 u
->local_tree_depth_decay
= atof(optval
);
1143 } else if (!strcasecmp(optname
, "local_tree_allseq")) {
1144 /* If disabled, only complete sequences are stored
1145 * in the local tree. If this is on, also
1146 * subsequences starting at each move are stored. */
1147 u
->local_tree_allseq
= !optval
|| atoi(optval
);
1148 } else if (!strcasecmp(optname
, "local_tree_neival")) {
1149 /* If disabled, local node value is not
1150 * computed just based on terminal status
1151 * of the coordinate, but also its neighbors. */
1152 u
->local_tree_neival
= !optval
|| atoi(optval
);
1153 } else if (!strcasecmp(optname
, "local_tree_eval")) {
1154 /* How is the value inserted in the local tree
1156 if (!strcasecmp(optval
, "root"))
1157 /* All moves within a tree branch are
1158 * considered wrt. their merit
1159 * reaching tachtical goal of making
1160 * the first move in the branch
1162 u
->local_tree_eval
= LTE_ROOT
;
1163 else if (!strcasecmp(optval
, "each"))
1164 /* Each move is considered wrt.
1165 * its own survival. */
1166 u
->local_tree_eval
= LTE_EACH
;
1167 else if (!strcasecmp(optval
, "total"))
1168 /* The tactical goal is the survival
1169 * of all the moves of my color and
1170 * non-survival of all the opponent
1171 * moves. Local values (and their
1172 * inverses) are averaged. */
1173 u
->local_tree_eval
= LTE_TOTAL
;
1175 fprintf(stderr
, "uct: unknown local_tree_eval %s\n", optval
);
1178 } else if (!strcasecmp(optname
, "local_tree_rootchoose")) {
1179 /* If disabled, only moves within the local
1180 * tree branch are considered; the values
1181 * of the branch roots (i.e. root children)
1182 * are ignored. This may make sense together
1183 * with eval!=each, we consider only moves
1184 * that influence the goal, not the "rating"
1185 * of the goal itself. (The real solution
1186 * will be probably using criticality to pick
1187 * local tree branches.) */
1188 u
->local_tree_rootchoose
= !optval
|| atoi(optval
);
1190 /** Other heuristics */
1191 } else if (!strcasecmp(optname
, "patterns")) {
1192 /* Load pattern database. Various modules
1193 * (priors, policies etc.) may make use
1194 * of this database. They will request
1195 * it automatically in that case, but you
1196 * can use this option to tweak the pattern
1198 patterns_init(&u
->pat
, optval
, false, true);
1199 u
->want_pat
= pat_setup
= true;
1200 } else if (!strcasecmp(optname
, "significant_threshold") && optval
) {
1201 /* Some heuristics (XXX: none in mainline) rely
1202 * on the knowledge of the last "significant"
1203 * node in the descent. Such a node is
1204 * considered reasonably trustworthy to carry
1205 * some meaningful information in the values
1206 * of the node and its children. */
1207 u
->significant_threshold
= atoi(optval
);
1209 /** Distributed engine slaves setup */
1211 } else if (!strcasecmp(optname
, "slave")) {
1212 /* Act as slave for the distributed engine. */
1213 u
->slave
= !optval
|| atoi(optval
);
1214 } else if (!strcasecmp(optname
, "slave_index") && optval
) {
1215 /* Optional index if per-slave behavior is desired.
1216 * Must be given as index/max */
1217 u
->slave_index
= atoi(optval
);
1218 char *p
= strchr(optval
, '/');
1219 if (p
) u
->max_slaves
= atoi(++p
);
1220 } else if (!strcasecmp(optname
, "shared_nodes") && optval
) {
1221 /* Share at most shared_nodes between master and slave at each genmoves.
1222 * Must use the same value in master and slaves. */
1223 u
->shared_nodes
= atoi(optval
);
1224 } else if (!strcasecmp(optname
, "shared_levels") && optval
) {
1225 /* Share only nodes of level <= shared_levels. */
1226 u
->shared_levels
= atoi(optval
);
1227 } else if (!strcasecmp(optname
, "stats_hbits") && optval
) {
1228 /* Set hash table size to 2^stats_hbits for the shared stats. */
1229 u
->stats_hbits
= atoi(optval
);
1230 } else if (!strcasecmp(optname
, "stats_delay") && optval
) {
1231 /* How long to wait in slave for initial stats to build up before
1232 * replying to the genmoves command (in ms) */
1233 u
->stats_delay
= 0.001 * atof(optval
);
1237 } else if (!strcasecmp(optname
, "maximize_score")) {
1238 /* A combination of settings that will make
1239 * Pachi try to maximize his points (instead
1240 * of playing slack yose) or minimize his loss
1241 * (and proceed to counting even when losing). */
1242 /* Please note that this preset might be
1243 * somewhat weaker than normal Pachi, and the
1244 * score maximization is approximate; point size
1245 * of win/loss still should not be used to judge
1246 * strength of Pachi or the opponent. */
1247 /* See README for some further notes. */
1248 if (!optval
|| atoi(optval
)) {
1249 /* Allow scoring a lost game. */
1250 u
->allow_losing_pass
= true;
1251 /* Make Pachi keep his calm when losing
1252 * and/or maintain winning marging. */
1253 /* Do not play games that are losing
1255 /* XXX: komi_ratchet_age=40000 is necessary
1256 * with losing_komi_ratchet, but 40000
1257 * is somewhat arbitrary value. */
1258 char dynkomi_args
[] = "losing_komi_ratchet:komi_ratchet_age=60000:no_komi_at_game_end=0:max_losing_komi=30";
1259 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomi_args
, b
);
1260 /* XXX: Values arbitrary so far. */
1261 /* XXX: Also, is bytemp sensible when
1262 * combined with dynamic komi?! */
1263 u
->val_scale
= 0.01;
1264 u
->val_bytemp
= true;
1265 u
->val_bytemp_min
= 0.001;
1266 u
->val_byavg
= true;
1270 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
1277 u
->policy
= policy_ucb1amaf_init(u
, NULL
, b
);
1279 if (!!u
->random_policy_chance
^ !!u
->random_policy
) {
1280 fprintf(stderr
, "uct: Only one of random_policy and random_policy_chance is set\n");
1284 if (!u
->local_tree
) {
1285 /* No ltree aging. */
1286 u
->local_tree_aging
= 1.0f
;
1289 if (u
->fast_alloc
) {
1290 if (u
->pruning_threshold
< u
->max_tree_size
/ 10)
1291 u
->pruning_threshold
= u
->max_tree_size
/ 10;
1292 if (u
->pruning_threshold
> u
->max_tree_size
/ 2)
1293 u
->pruning_threshold
= u
->max_tree_size
/ 2;
1295 /* Limit pruning temp space to 20% of memory. Beyond this we discard
1296 * the nodes and recompute them at the next move if necessary. */
1297 u
->max_pruned_size
= u
->max_tree_size
/ 5;
1298 u
->max_tree_size
-= u
->max_pruned_size
;
1300 /* Reserve 5% memory in case the background free() are slower
1301 * than the concurrent allocations. */
1302 u
->max_tree_size
-= u
->max_tree_size
/ 20;
1306 u
->prior
= uct_prior_init(NULL
, b
, u
);
1309 u
->playout
= playout_moggy_init(NULL
, b
, u
->jdict
);
1310 if (!u
->playout
->debug_level
)
1311 u
->playout
->debug_level
= u
->debug_level
;
1313 if (u
->want_pat
&& !pat_setup
)
1314 patterns_init(&u
->pat
, NULL
, false, true);
1317 u
->ownermap
.map
= malloc2(board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
1320 if (!u
->stats_hbits
) u
->stats_hbits
= DEFAULT_STATS_HBITS
;
1321 if (!u
->shared_nodes
) u
->shared_nodes
= DEFAULT_SHARED_NODES
;
1322 assert(u
->shared_levels
* board_bits2(b
) <= 8 * (int)sizeof(path_t
));
1326 u
->dynkomi
= board_small(b
) ? uct_dynkomi_init_none(u
, NULL
, b
)
1327 : uct_dynkomi_init_linear(u
, NULL
, b
);
1329 /* Some things remain uninitialized for now - the opening tbook
1330 * is not loaded and the tree not set up. */
1331 /* This will be initialized in setup_state() at the first move
1332 * received/requested. This is because right now we are not aware
1333 * about any komi or handicap setup and such. */
1339 engine_uct_init(char *arg
, struct board
*b
)
1341 struct uct
*u
= uct_state_init(arg
, b
);
1342 struct engine
*e
= calloc2(1, sizeof(struct engine
));
1344 e
->board_print
= uct_board_print
;
1345 e
->notify_play
= uct_notify_play
;
1348 e
->result
= uct_result
;
1349 e
->genmove
= uct_genmove
;
1350 e
->genmoves
= uct_genmoves
;
1351 e
->evaluate
= uct_evaluate
;
1352 e
->dead_group_list
= uct_dead_group_list
;
1355 e
->owner_map
= uct_owner_map
;
1356 e
->best_moves
= uct_best_moves
;
1357 e
->live_gfx_hook
= uct_live_gfx_hook
;
1360 e
->notify
= uct_notify
;
1362 const char banner
[] = "If you believe you have won but I am still playing, "
1363 "please help me understand by capturing all dead stones. "
1364 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1365 if (!u
->banner
) u
->banner
= "";
1366 e
->comment
= malloc2(sizeof(banner
) + strlen(u
->banner
) + 1);
1367 sprintf(e
->comment
, "%s %s", banner
, u
->banner
);