18 #include "playout/elo.h"
19 #include "playout/moggy.h"
20 #include "playout/light.h"
24 #include "distributed/distributed.h"
25 #include "uct/dynkomi.h"
26 #include "uct/internal.h"
27 #include "uct/prior.h"
32 struct uct_policy
*policy_ucb1_init(struct uct
*u
, char *arg
);
33 struct uct_policy
*policy_ucb1amaf_init(struct uct
*u
, char *arg
);
34 static void uct_pondering_stop(struct uct
*u
);
35 static void uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
);
36 static char *uct_getstats(struct uct
*u
, struct board
*b
, coord_t
*c
);
38 /* Default number of simulations to perform per move.
39 * Note that this is now in total over all threads! (Unless TM_ROOT.) */
40 #define MC_GAMES 80000
41 #define MC_GAMELEN MAX_GAMELEN
42 static const struct time_info default_ti
= {
45 .len
= { .games
= MC_GAMES
},
48 /* How big proportion of ownermap counts must be of one color to consider
51 /* How many games to consider at minimum before judging groups. */
52 #define GJ_MINGAMES 500
54 /* How often to inspect the tree from the main thread to check for playout
55 * stop, progress reports, etc. (in seconds) */
56 #define TREE_BUSYWAIT_INTERVAL 0.1 /* 100ms */
58 /* Once per how many simulations (per thread) to show a progress report line. */
59 #define TREE_SIMPROGRESS_INTERVAL 10000
61 /* How often to send stats updates for the distributed engine (in seconds). */
62 #define STATS_SEND_INTERVAL 0.5
64 /* When terminating uct_search() early, the safety margin to add to the
65 * remaining playout number estimate when deciding whether the result can
67 #define PLAYOUT_DELTA_SAFEMARGIN 1000
71 setup_state(struct uct
*u
, struct board
*b
, enum stone color
)
73 u
->t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0, u
->local_tree_aging
);
75 fast_srandom(u
->force_seed
);
77 fprintf(stderr
, "Fresh board with random seed %lu\n", fast_getseed());
78 //board_print(b, stderr);
79 if (!u
->no_book
&& b
->moves
== 0) {
80 assert(color
== S_BLACK
);
86 reset_state(struct uct
*u
)
89 tree_done(u
->t
); u
->t
= NULL
;
93 setup_dynkomi(struct uct
*u
, struct board
*b
, enum stone to_play
)
95 if (u
->t
->use_extra_komi
&& u
->dynkomi
->permove
)
96 u
->t
->extra_komi
= u
->dynkomi
->permove(u
->dynkomi
, b
, u
->t
);
100 prepare_move(struct engine
*e
, struct board
*b
, enum stone color
)
102 struct uct
*u
= e
->data
;
105 /* Verify that we have sane state. */
107 assert(u
->t
&& b
->moves
);
108 if (color
!= stone_other(u
->t
->root_color
)) {
109 fprintf(stderr
, "Fatal: Non-alternating play detected %d %d\n",
110 color
, u
->t
->root_color
);
115 /* We need fresh state. */
117 setup_state(u
, b
, color
);
120 u
->ownermap
.playouts
= 0;
121 memset(u
->ownermap
.map
, 0, board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
125 dead_group_list(struct uct
*u
, struct board
*b
, struct move_queue
*mq
)
127 struct group_judgement gj
;
129 gj
.gs
= alloca(board_size2(b
) * sizeof(gj
.gs
[0]));
130 board_ownermap_judge_group(b
, &u
->ownermap
, &gj
);
131 groups_of_status(b
, &gj
, GS_DEAD
, mq
);
135 uct_pass_is_safe(struct uct
*u
, struct board
*b
, enum stone color
, bool pass_all_alive
)
137 if (u
->ownermap
.playouts
< GJ_MINGAMES
)
140 struct move_queue mq
= { .moves
= 0 };
142 dead_group_list(u
, b
, &mq
);
143 return pass_is_safe(b
, color
, &mq
);
146 /* This function is called only when running as slave in the distributed version. */
147 static enum parse_code
148 uct_notify(struct engine
*e
, struct board
*b
, int id
, char *cmd
, char *args
, char **reply
)
150 struct uct
*u
= e
->data
;
152 static bool board_resized
= false;
153 board_resized
|= is_gamestart(cmd
);
155 /* Force resending the whole command history if we are out of sync
156 * but do it only once, not if already getting the history. */
157 if ((move_number(id
) != b
->moves
|| !board_resized
) && !reply_disabled(id
)) {
159 fprintf(stderr
, "Out of sync, id %d, move %d\n", id
, b
->moves
);
160 static char buf
[128];
161 snprintf(buf
, sizeof(buf
), "out of sync, move %d expected", b
->moves
);
166 return reply_disabled(id
) ? P_NOREPLY
: P_OK
;
170 uct_printhook_ownermap(struct board
*board
, coord_t c
, char *s
, char *end
)
172 struct uct
*u
= board
->es
;
174 const char chr
[] = ":XO,"; // dame, black, white, unclear
175 const char chm
[] = ":xo,";
176 char ch
= chr
[board_ownermap_judge_point(&u
->ownermap
, c
, GJ_THRES
)];
177 if (ch
== ',') { // less precise estimate then?
178 ch
= chm
[board_ownermap_judge_point(&u
->ownermap
, c
, 0.67)];
180 s
+= snprintf(s
, end
- s
, "%c ", ch
);
185 uct_notify_play(struct engine
*e
, struct board
*b
, struct move
*m
)
187 struct uct
*u
= e
->data
;
189 /* No state, create one - this is probably game beginning
190 * and we need to load the opening book right now. */
191 prepare_move(e
, b
, m
->color
);
195 /* Stop pondering, required by tree_promote_at() */
196 uct_pondering_stop(u
);
198 if (is_resign(m
->coord
)) {
204 /* Promote node of the appropriate move to the tree root. */
206 if (!tree_promote_at(u
->t
, b
, m
->coord
)) {
208 fprintf(stderr
, "Warning: Cannot promote move node! Several play commands in row?\n");
213 /* If we are a slave in a distributed engine, start pondering once
214 * we know which move we actually played. See uct_genmove() about
215 * the check for pass. */
216 if (u
->pondering_opt
&& u
->slave
&& m
->color
== u
->my_color
&& !is_pass(m
->coord
))
217 uct_pondering_start(u
, b
, u
->t
, stone_other(m
->color
));
223 uct_chat(struct engine
*e
, struct board
*b
, char *cmd
)
225 struct uct
*u
= e
->data
;
226 static char reply
[1024];
228 cmd
+= strspn(cmd
, " \n\t");
229 if (!strncasecmp(cmd
, "winrate", 7)) {
231 return "no game context (yet?)";
232 enum stone color
= u
->t
->root_color
;
233 struct tree_node
*n
= u
->t
->root
;
234 snprintf(reply
, 1024, "In %d playouts at %d threads, %s %s can win with %.2f%% probability",
235 n
->u
.playouts
, u
->threads
, stone2str(color
), coord2sstr(n
->coord
, b
),
236 tree_node_get_value(u
->t
, -1, n
->u
.value
) * 100);
237 if (u
->t
->use_extra_komi
&& abs(u
->t
->extra_komi
) >= 0.5) {
238 sprintf(reply
+ strlen(reply
), ", while self-imposing extra komi %.1f",
248 uct_dead_group_list(struct engine
*e
, struct board
*b
, struct move_queue
*mq
)
250 struct uct
*u
= e
->data
;
252 /* This means the game is probably over, no use pondering on. */
253 uct_pondering_stop(u
);
255 if (u
->pass_all_alive
)
256 return; // no dead groups
258 bool mock_state
= false;
261 /* No state, but we cannot just back out - we might
262 * have passed earlier, only assuming some stones are
263 * dead, and then re-connected, only to lose counting
264 * when all stones are assumed alive. */
265 /* Mock up some state and seed the ownermap by few
267 prepare_move(e
, b
, S_BLACK
); assert(u
->t
);
268 for (int i
= 0; i
< GJ_MINGAMES
; i
++)
269 uct_playout(u
, b
, S_BLACK
, u
->t
);
273 dead_group_list(u
, b
, mq
);
276 /* Clean up the mock state in case we will receive
277 * a genmove; we could get a non-alternating-move
278 * error from prepare_move() in that case otherwise. */
284 playout_policy_done(struct playout_policy
*p
)
286 if (p
->done
) p
->done(p
);
287 if (p
->data
) free(p
->data
);
292 uct_done(struct engine
*e
)
294 /* This is called on engine reset, especially when clear_board
295 * is received and new game should begin. */
296 struct uct
*u
= e
->data
;
297 uct_pondering_stop(u
);
298 if (u
->t
) reset_state(u
);
299 free(u
->ownermap
.map
);
302 free(u
->random_policy
);
303 playout_policy_done(u
->playout
);
304 uct_prior_done(u
->prior
);
308 /* Pachi threading structure (if uct_playouts_parallel() is used):
311 * | main(), GTP communication, ...
312 * | starts and stops the search managed by thread_manager
315 * | spawns and collects worker threads
321 * uct_playouts() loop, doing descend-playout until uct_halt
323 * Another way to look at it is by functions (lines denote thread boundaries):
326 * | uct_search() (uct_search_start() .. uct_search_stop())
327 * | -----------------------
328 * | spawn_thread_manager()
329 * | -----------------------
331 * V uct_playouts() */
333 /* Set in thread manager in case the workers should stop. */
334 volatile sig_atomic_t uct_halt
= 0;
335 /* ID of the running worker thread. */
336 __thread
int thread_id
= -1;
337 /* ID of the thread manager. */
338 static pthread_t thread_manager
;
339 static bool thread_manager_running
;
341 static pthread_mutex_t finish_mutex
= PTHREAD_MUTEX_INITIALIZER
;
342 static pthread_cond_t finish_cond
= PTHREAD_COND_INITIALIZER
;
343 static volatile int finish_thread
;
344 static pthread_mutex_t finish_serializer
= PTHREAD_MUTEX_INITIALIZER
;
357 spawn_worker(void *ctx_
)
359 struct spawn_ctx
*ctx
= ctx_
;
361 fast_srandom(ctx
->seed
);
362 thread_id
= ctx
->tid
;
364 ctx
->games
= uct_playouts(ctx
->u
, ctx
->b
, ctx
->color
, ctx
->t
);
366 pthread_mutex_lock(&finish_serializer
);
367 pthread_mutex_lock(&finish_mutex
);
368 finish_thread
= ctx
->tid
;
369 pthread_cond_signal(&finish_cond
);
370 pthread_mutex_unlock(&finish_mutex
);
374 /* Thread manager, controlling worker threads. It must be called with
375 * finish_mutex lock held, but it will unlock it itself before exiting;
376 * this is necessary to be completely deadlock-free. */
377 /* The finish_cond can be signalled for it to stop; in that case,
378 * the caller should set finish_thread = -1. */
379 /* After it is started, it will update mctx->t to point at some tree
380 * used for the actual search (matters only for TM_ROOT), on return
381 * it will set mctx->games to the number of performed simulations. */
383 spawn_thread_manager(void *ctx_
)
385 /* In thread_manager, we use only some of the ctx fields. */
386 struct spawn_ctx
*mctx
= ctx_
;
387 struct uct
*u
= mctx
->u
;
388 struct tree
*t
= mctx
->t
;
389 bool shared_tree
= u
->parallel_tree
;
390 fast_srandom(mctx
->seed
);
392 int played_games
= 0;
393 pthread_t threads
[u
->threads
];
398 /* Garbage collect the tree by preference when pondering. */
399 if (u
->pondering
&& t
->nodes
&& t
->nodes_size
> t
->max_tree_size
/2) {
400 unsigned long temp_size
= (MIN_FREE_MEM_PERCENT
* t
->max_tree_size
) / 100;
401 t
->root
= tree_garbage_collect(t
, temp_size
, t
->root
);
404 /* Spawn threads... */
405 for (int ti
= 0; ti
< u
->threads
; ti
++) {
406 struct spawn_ctx
*ctx
= malloc(sizeof(*ctx
));
407 ctx
->u
= u
; ctx
->b
= mctx
->b
; ctx
->color
= mctx
->color
;
408 mctx
->t
= ctx
->t
= shared_tree
? t
: tree_copy(t
);
409 ctx
->tid
= ti
; ctx
->seed
= fast_random(65536) + ti
;
410 pthread_create(&threads
[ti
], NULL
, spawn_worker
, ctx
);
412 fprintf(stderr
, "Spawned worker %d\n", ti
);
415 /* ...and collect them back: */
416 while (joined
< u
->threads
) {
417 /* Wait for some thread to finish... */
418 pthread_cond_wait(&finish_cond
, &finish_mutex
);
419 if (finish_thread
< 0) {
420 /* Stop-by-caller. Tell the workers to wrap up. */
424 /* ...and gather its remnants. */
425 struct spawn_ctx
*ctx
;
426 pthread_join(threads
[finish_thread
], (void **) &ctx
);
427 played_games
+= ctx
->games
;
430 if (ctx
->t
== mctx
->t
) mctx
->t
= t
;
431 tree_merge(t
, ctx
->t
);
436 fprintf(stderr
, "Joined worker %d\n", finish_thread
);
437 pthread_mutex_unlock(&finish_serializer
);
440 pthread_mutex_unlock(&finish_mutex
);
443 tree_normalize(mctx
->t
, u
->threads
);
445 mctx
->games
= played_games
;
449 static struct spawn_ctx
*
450 uct_search_start(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
)
452 assert(u
->threads
> 0);
453 assert(!thread_manager_running
);
455 struct spawn_ctx ctx
= { .u
= u
, .b
= b
, .color
= color
, .t
= t
, .seed
= fast_random(65536) };
456 static struct spawn_ctx mctx
; mctx
= ctx
;
457 pthread_mutex_lock(&finish_mutex
);
458 pthread_create(&thread_manager
, NULL
, spawn_thread_manager
, &mctx
);
459 thread_manager_running
= true;
463 static struct spawn_ctx
*
464 uct_search_stop(void)
466 assert(thread_manager_running
);
468 /* Signal thread manager to stop the workers. */
469 pthread_mutex_lock(&finish_mutex
);
471 pthread_cond_signal(&finish_cond
);
472 pthread_mutex_unlock(&finish_mutex
);
474 /* Collect the thread manager. */
475 struct spawn_ctx
*pctx
;
476 thread_manager_running
= false;
477 pthread_join(thread_manager
, (void **) &pctx
);
482 /* Determine whether we should terminate the search early. */
484 uct_search_stop_early(struct uct
*u
, struct tree
*t
, struct board
*b
,
485 struct time_info
*ti
, struct time_stop
*stop
,
486 struct tree_node
*best
, struct tree_node
*best2
,
487 int base_playouts
, int i
)
489 /* Always use at least half the desired time. It is silly
490 * to lose a won game because we played a bad move in 0.1s. */
492 if (ti
->dim
== TD_WALLTIME
) {
493 elapsed
= time_now() - ti
->len
.t
.timer_start
;
494 if (elapsed
< 0.5 * stop
->desired
.time
) return false;
497 /* Early break in won situation. */
498 if (best
->u
.playouts
>= 2000 && tree_node_get_value(t
, 1, best
->u
.value
) >= u
->loss_threshold
)
500 /* Earlier break in super-won situation. */
501 if (best
->u
.playouts
>= 500 && tree_node_get_value(t
, 1, best
->u
.value
) >= 0.95)
504 /* Break early if we estimate the second-best move cannot
505 * catch up in assigned time anymore. We use all our time
506 * if we are in byoyomi with single stone remaining in our
507 * period, however - it's better to pre-ponder. */
508 bool time_indulgent
= (!ti
->len
.t
.main_time
&& ti
->len
.t
.byoyomi_stones
== 1);
509 if (best2
&& ti
->dim
== TD_WALLTIME
&& !time_indulgent
) {
510 double remaining
= stop
->worst
.time
- elapsed
;
511 double pps
= ((double)i
- base_playouts
) / elapsed
;
512 double estplayouts
= remaining
* pps
+ PLAYOUT_DELTA_SAFEMARGIN
;
513 if (best
->u
.playouts
> best2
->u
.playouts
+ estplayouts
) {
515 fprintf(stderr
, "Early stop, result cannot change: "
516 "best %d, best2 %d, estimated %f simulations to go\n",
517 best
->u
.playouts
, best2
->u
.playouts
, estplayouts
);
525 /* Determine whether we should terminate the search later. */
527 uct_search_keep_looking(struct uct
*u
, struct tree
*t
, struct board
*b
,
528 struct time_info
*ti
, struct time_stop
*stop
,
529 struct tree_node
*best
, struct tree_node
*best2
,
530 struct tree_node
*bestr
, struct tree_node
*winner
, int i
)
534 fprintf(stderr
, "Did not find best move, still trying...\n");
538 /* Do not waste time if we are winning. Spend up to worst time if
539 * we are unsure, but only desired time if we are sure of winning. */
540 float beta
= 2 * (tree_node_get_value(t
, 1, best
->u
.value
) - 0.5);
541 if (ti
->dim
== TD_WALLTIME
&& beta
> 0) {
542 double good_enough
= stop
->desired
.time
* beta
+ stop
->worst
.time
* (1 - beta
);
543 double elapsed
= time_now() - ti
->len
.t
.timer_start
;
544 if (elapsed
> good_enough
) return false;
547 if (u
->best2_ratio
> 0) {
548 /* Check best/best2 simulations ratio. If the
549 * two best moves give very similar results,
550 * keep simulating. */
551 if (best2
&& best2
->u
.playouts
552 && (double)best
->u
.playouts
/ best2
->u
.playouts
< u
->best2_ratio
) {
554 fprintf(stderr
, "Best2 ratio %f < threshold %f\n",
555 (double)best
->u
.playouts
/ best2
->u
.playouts
,
561 if (u
->bestr_ratio
> 0) {
562 /* Check best, best_best value difference. If the best move
563 * and its best child do not give similar enough results,
564 * keep simulating. */
565 if (bestr
&& bestr
->u
.playouts
566 && fabs((double)best
->u
.value
- bestr
->u
.value
) > u
->bestr_ratio
) {
568 fprintf(stderr
, "Bestr delta %f > threshold %f\n",
569 fabs((double)best
->u
.value
- bestr
->u
.value
),
575 if (winner
&& winner
!= best
) {
576 /* Keep simulating if best explored
577 * does not have also highest value. */
579 fprintf(stderr
, "[%d] best %3s [%d] %f != winner %3s [%d] %f\n", i
,
580 coord2sstr(best
->coord
, t
->board
),
581 best
->u
.playouts
, tree_node_get_value(t
, 1, best
->u
.value
),
582 coord2sstr(winner
->coord
, t
->board
),
583 winner
->u
.playouts
, tree_node_get_value(t
, 1, winner
->u
.value
));
587 /* No reason to keep simulating, bye. */
591 /* Run time-limited MCTS search on foreground. */
593 uct_search(struct uct
*u
, struct board
*b
, struct time_info
*ti
, enum stone color
, struct tree
*t
)
595 int base_playouts
= u
->t
->root
->u
.playouts
;
596 if (UDEBUGL(2) && base_playouts
> 0)
597 fprintf(stderr
, "<pre-simulated %d games skipped>\n", base_playouts
);
599 /* Set up time conditions. */
600 if (ti
->period
== TT_NULL
) *ti
= default_ti
;
601 struct time_stop stop
;
602 time_stop_conditions(ti
, b
, u
->fuseki_end
, u
->yose_start
, &stop
);
604 /* Number of last dynkomi adjustment. */
605 int last_dynkomi
= t
->root
->u
.playouts
;
606 /* Number of last game with progress print. */
607 int last_print
= t
->root
->u
.playouts
;
608 /* Number of simulations to wait before next print. */
609 int print_interval
= TREE_SIMPROGRESS_INTERVAL
* (u
->thread_model
== TM_ROOT
? 1 : u
->threads
);
610 /* Printed notification about full memory? */
611 bool print_fullmem
= false;
612 /* Absolute time of last distributed stats update. */
613 double last_stats_sent
= time_now();
614 /* Interval between distributed stats updates. */
615 double stats_interval
= STATS_SEND_INTERVAL
;
617 struct spawn_ctx
*ctx
= uct_search_start(u
, b
, color
, t
);
619 /* The search tree is ctx->t. This is normally == t, but in case of
620 * TM_ROOT, it is one of the trees belonging to the independent
621 * workers. It is important to reference ctx->t directly since the
622 * thread manager will swap the tree pointer asynchronously. */
623 /* XXX: This means TM_ROOT support is suboptimal since single stalled
624 * thread can stall the others in case of limiting the search by game
625 * count. However, TM_ROOT just does not deserve any more extra code
628 struct tree_node
*best
= NULL
;
629 struct tree_node
*best2
= NULL
; // Second-best move.
630 struct tree_node
*bestr
= NULL
; // best's best child.
631 struct tree_node
*winner
= NULL
;
633 double busywait_interval
= TREE_BUSYWAIT_INTERVAL
;
635 /* Now, just periodically poll the search tree. */
637 time_sleep(busywait_interval
);
638 /* busywait_interval should never be less than desired time, or the
639 * time control is broken. But if it happens to be less, we still search
640 * at least 100ms otherwise the move is completely random. */
642 int i
= ctx
->t
->root
->u
.playouts
;
644 /* Adjust dynkomi? */
645 if (ctx
->t
->use_extra_komi
&& u
->dynkomi
->permove
646 && u
->dynkomi_interval
647 && i
> last_dynkomi
+ u
->dynkomi_interval
) {
648 float old_dynkomi
= ctx
->t
->extra_komi
;
649 ctx
->t
->extra_komi
= u
->dynkomi
->permove(u
->dynkomi
, b
, ctx
->t
);
650 if (UDEBUGL(3) && old_dynkomi
!= ctx
->t
->extra_komi
)
651 fprintf(stderr
, "dynkomi adjusted (%f -> %f)\n", old_dynkomi
, ctx
->t
->extra_komi
);
654 /* Print progress? */
655 if (i
- last_print
> print_interval
) {
656 last_print
+= print_interval
; // keep the numbers tidy
657 uct_progress_status(u
, ctx
->t
, color
, last_print
);
659 if (!print_fullmem
&& ctx
->t
->nodes_size
> u
->max_tree_size
) {
661 fprintf(stderr
, "memory limit hit (%lu > %lu)\n", ctx
->t
->nodes_size
, u
->max_tree_size
);
662 print_fullmem
= true;
665 /* Never consider stopping if we played too few simulations.
666 * Maybe we risk losing on time when playing in super-extreme
667 * time pressure but the tree is going to be just too messed
668 * up otherwise - we might even play invalid suicides or pass
669 * when we mustn't. */
673 best
= u
->policy
->choose(u
->policy
, ctx
->t
->root
, b
, color
, resign
);
674 if (best
) best2
= u
->policy
->choose(u
->policy
, ctx
->t
->root
, b
, color
, best
->coord
);
676 /* Possibly stop search early if it's no use to try on. */
677 if (best
&& uct_search_stop_early(u
, ctx
->t
, b
, ti
, &stop
, best
, best2
, base_playouts
, i
))
680 /* Check against time settings. */
681 bool desired_done
= false;
682 double now
= time_now();
683 if (ti
->dim
== TD_WALLTIME
) {
684 double elapsed
= now
- ti
->len
.t
.timer_start
;
685 if (elapsed
> stop
.worst
.time
) break;
686 desired_done
= elapsed
> stop
.desired
.time
;
687 if (stats_interval
< 0.1 * stop
.desired
.time
)
688 stats_interval
= 0.1 * stop
.desired
.time
;
690 } else { assert(ti
->dim
== TD_GAMES
);
691 if (i
> stop
.worst
.playouts
) break;
692 desired_done
= i
> stop
.desired
.playouts
;
695 /* We want to stop simulating, but are willing to keep trying
696 * if we aren't completely sure about the winner yet. */
698 if (u
->policy
->winner
&& u
->policy
->evaluate
) {
699 struct uct_descent descent
= { .node
= ctx
->t
->root
};
700 u
->policy
->winner(u
->policy
, ctx
->t
, &descent
);
701 winner
= descent
.node
;
704 bestr
= u
->policy
->choose(u
->policy
, best
, b
, stone_other(color
), resign
);
705 if (!uct_search_keep_looking(u
, ctx
->t
, b
, ti
, &stop
, best
, best2
, bestr
, winner
, i
))
709 /* TODO: Early break if best->variance goes under threshold and we already
710 * have enough playouts (possibly thanks to book or to pondering)? */
712 /* Send new stats for the distributed engine.
713 * End with #\n (not \n\n) to indicate a temporary result. */
714 if (u
->slave
&& now
- last_stats_sent
> stats_interval
) {
715 printf("=%d %s\n#\n", u
->gtp_id
, uct_getstats(u
, b
, NULL
));
717 last_stats_sent
= now
;
721 ctx
= uct_search_stop();
724 tree_dump(t
, u
->dumpthres
);
726 uct_progress_status(u
, t
, color
, ctx
->games
);
732 /* Start pondering background with @color to play. */
734 uct_pondering_start(struct uct
*u
, struct board
*b0
, struct tree
*t
, enum stone color
)
737 fprintf(stderr
, "Starting to ponder with color %s\n", stone2str(stone_other(color
)));
740 /* We need a local board copy to ponder upon. */
741 struct board
*b
= malloc(sizeof(*b
)); board_copy(b
, b0
);
743 /* *b0 did not have the genmove'd move played yet. */
744 struct move m
= { t
->root
->coord
, t
->root_color
};
745 int res
= board_play(b
, &m
);
747 setup_dynkomi(u
, b
, stone_other(m
.color
));
749 /* Start MCTS manager thread "headless". */
750 uct_search_start(u
, b
, color
, t
);
753 /* uct_search_stop() frontend for the pondering (non-genmove) mode. */
755 uct_pondering_stop(struct uct
*u
)
757 u
->pondering
= false;
758 if (!thread_manager_running
)
761 /* Stop the thread manager. */
762 struct spawn_ctx
*ctx
= uct_search_stop();
764 fprintf(stderr
, "(pondering) ");
765 uct_progress_status(u
, ctx
->t
, ctx
->color
, ctx
->games
);
772 uct_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
774 double start_time
= time_now();
775 struct uct
*u
= e
->data
;
777 if (b
->superko_violation
) {
778 fprintf(stderr
, "!!! WARNING: SUPERKO VIOLATION OCCURED BEFORE THIS MOVE\n");
779 fprintf(stderr
, "Maybe you play with situational instead of positional superko?\n");
780 fprintf(stderr
, "I'm going to ignore the violation, but note that I may miss\n");
781 fprintf(stderr
, "some moves valid under this ruleset because of this.\n");
782 b
->superko_violation
= false;
786 uct_pondering_stop(u
);
787 prepare_move(e
, b
, color
);
791 /* How to decide whether to use dynkomi in this game? Since we use
792 * pondering, it's not simple "who-to-play" matter. Decide based on
793 * the last genmove issued. */
794 u
->t
->use_extra_komi
= !!(u
->dynkomi_mask
& color
);
795 setup_dynkomi(u
, b
, color
);
797 if (b
->rules
== RULES_JAPANESE
)
798 u
->territory_scoring
= true;
800 /* Make pessimistic assumption about komi for Japanese rules to
801 * avoid losing by 0.5 when winning by 0.5 with Chinese rules.
802 * The rules usually give the same winner if the integer part of komi
803 * is odd so we adjust the komi only if it is even (for a board of
804 * odd size). We are not trying to get an exact evaluation for rare
805 * cases of seki. For details see http://home.snafu.de/jasiek/parity.html */
806 if (u
->territory_scoring
&& (((int)floor(b
->komi
) + board_size(b
)) & 1)) {
807 b
->komi
+= (color
== S_BLACK
? 1.0 : -1.0);
809 fprintf(stderr
, "Setting komi to %.1f assuming Japanese rules\n",
813 int base_playouts
= u
->t
->root
->u
.playouts
;
814 /* Perform the Monte Carlo Tree Search! */
815 int played_games
= uct_search(u
, b
, ti
, color
, u
->t
);
817 /* Choose the best move from the tree. */
818 struct tree_node
*best
= u
->policy
->choose(u
->policy
, u
->t
->root
, b
, color
, resign
);
820 if (!u
->slave
) reset_state(u
);
821 return coord_copy(pass
);
824 fprintf(stderr
, "*** WINNER is %s (%d,%d) with score %1.4f (%d/%d:%d/%d games), extra komi %f\n",
825 coord2sstr(best
->coord
, b
), coord_x(best
->coord
, b
), coord_y(best
->coord
, b
),
826 tree_node_get_value(u
->t
, 1, best
->u
.value
), best
->u
.playouts
,
827 u
->t
->root
->u
.playouts
, u
->t
->root
->u
.playouts
- base_playouts
, played_games
,
830 /* Do not resign if we're so short of time that evaluation of best
831 * move is completely unreliable, we might be winning actually.
832 * In this case best is almost random but still better than resign.
833 * Also do not resign if we are getting bad results while actually
834 * giving away extra komi points (dynkomi). */
835 if (tree_node_get_value(u
->t
, 1, best
->u
.value
) < u
->resign_ratio
836 && !is_pass(best
->coord
) && best
->u
.playouts
> GJ_MINGAMES
837 && u
->t
->extra_komi
<= 1 /* XXX we assume dynamic komi == we are black */) {
838 if (!u
->slave
) reset_state(u
);
839 return coord_copy(resign
);
842 /* If the opponent just passed and we win counting, always
844 if (b
->moves
> 1 && is_pass(b
->last_move
.coord
)) {
845 /* Make sure enough playouts are simulated. */
846 while (u
->ownermap
.playouts
< GJ_MINGAMES
)
847 uct_playout(u
, b
, color
, u
->t
);
848 if (uct_pass_is_safe(u
, b
, color
, u
->pass_all_alive
|| pass_all_alive
)) {
850 fprintf(stderr
, "<Will rather pass, looks safe enough.>\n");
855 /* If we are a slave in the distributed engine, we'll soon get
856 * a "play" command later telling us which move was chosen,
857 * and pondering now will not gain much. */
859 tree_promote_node(u
->t
, &best
);
861 /* After a pass, pondering is harmful for two reasons:
862 * (i) We might keep pondering even when the game is over.
863 * Of course this is the case for opponent resign as well.
864 * (ii) More importantly, the ownermap will get skewed since
865 * the UCT will start cutting off any playouts. */
866 if (u
->pondering_opt
&& !is_pass(best
->coord
)) {
867 uct_pondering_start(u
, b
, u
->t
, stone_other(color
));
871 double time
= time_now() - start_time
+ 0.000001; /* avoid divide by zero */
872 fprintf(stderr
, "genmove in %0.2fs (%d games/s, %d games/s/thread)\n",
873 time
, (int)(played_games
/time
), (int)(played_games
/time
/u
->threads
));
875 return coord_copy(best
->coord
);
878 /* Get stats updates for the distributed engine. Return a buffer
879 * with one line "total_playouts threads" then a list of lines
880 * "coord playouts value". The last line must not end with \n.
881 * If c is not null, add this move with root->playouts weight.
882 * This function is called only by the main thread, but may be
883 * called while the tree is updated by the worker threads.
884 * Keep this code in sync with select_best_move(). */
886 uct_getstats(struct uct
*u
, struct board
*b
, coord_t
*c
)
888 static char reply
[10240];
890 char *end
= reply
+ sizeof(reply
);
891 struct tree_node
*root
= u
->t
->root
;
892 r
+= snprintf(r
, end
- r
, "%d %d", root
->u
.playouts
, u
->threads
);
893 int min_playouts
= root
->u
.playouts
/ 100;
895 // Give a large weight to pass or resign, but still allow other moves.
897 r
+= snprintf(r
, end
- r
, "\n%s %d %.1f", coord2sstr(*c
, b
), root
->u
.playouts
,
900 /* We rely on the fact that root->children is set only
901 * after all children are created. */
902 for (struct tree_node
*ni
= root
->children
; ni
; ni
= ni
->sibling
) {
903 if (ni
->u
.playouts
<= min_playouts
904 || ni
->hints
& TREE_HINT_INVALID
905 || is_pass(ni
->coord
))
907 char *coord
= coord2sstr(ni
->coord
, b
);
908 // We return the values as stored in the tree, so from black's view.
909 r
+= snprintf(r
, end
- r
, "\n%s %d %.7f", coord
, ni
->u
.playouts
, ni
->u
.value
);
915 uct_genmoves(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
917 struct uct
*u
= e
->data
;
920 coord_t
*c
= uct_genmove(e
, b
, ti
, color
, pass_all_alive
);
922 char *reply
= uct_getstats(u
, b
, is_pass(*c
) || is_resign(*c
) ? c
: NULL
);
929 uct_genbook(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
)
931 struct uct
*u
= e
->data
;
932 if (!u
->t
) prepare_move(e
, b
, color
);
935 if (ti
->dim
== TD_GAMES
) {
936 /* Don't count in games that already went into the book. */
937 ti
->len
.games
+= u
->t
->root
->u
.playouts
;
939 uct_search(u
, b
, ti
, color
, u
->t
);
941 assert(ti
->dim
== TD_GAMES
);
942 tree_save(u
->t
, b
, ti
->len
.games
/ 100);
948 uct_dumpbook(struct engine
*e
, struct board
*b
, enum stone color
)
950 struct uct
*u
= e
->data
;
951 struct tree
*t
= tree_init(b
, color
, u
->fast_alloc
? u
->max_tree_size
: 0, u
->local_tree_aging
);
959 uct_state_init(char *arg
, struct board
*b
)
961 struct uct
*u
= calloc(1, sizeof(struct uct
));
962 bool using_elo
= false;
964 u
->debug_level
= debug_level
;
965 u
->gamelen
= MC_GAMELEN
;
969 u
->playout_amaf
= true;
970 u
->playout_amaf_nakade
= false;
971 u
->amaf_prior
= false;
972 u
->max_tree_size
= 3072ULL * 1048576;
974 u
->dynkomi_mask
= S_BLACK
;
977 u
->thread_model
= TM_TREEVL
;
978 u
->parallel_tree
= true;
979 u
->virtual_loss
= true;
981 u
->fuseki_end
= 20; // max time at 361*20% = 72 moves (our 36th move, still 99 to play)
982 u
->yose_start
= 40; // (100-40-25)*361/100/2 = 63 moves still to play by us then
983 u
->bestr_ratio
= 0.02;
984 // 2.5 is clearly too much, but seems to compensate well for overly stern time allocations.
985 // TODO: Further tuning and experiments with better time allocation schemes.
986 u
->best2_ratio
= 2.5;
988 u
->val_scale
= 0.04; u
->val_points
= 40;
991 u
->local_tree_aging
= 2;
994 char *optspec
, *next
= arg
;
997 next
+= strcspn(next
, ",");
998 if (*next
) { *next
++ = 0; } else { *next
= 0; }
1000 char *optname
= optspec
;
1001 char *optval
= strchr(optspec
, '=');
1002 if (optval
) *optval
++ = 0;
1004 if (!strcasecmp(optname
, "debug")) {
1006 u
->debug_level
= atoi(optval
);
1009 } else if (!strcasecmp(optname
, "mercy") && optval
) {
1010 /* Minimal difference of black/white captures
1011 * to stop playout - "Mercy Rule". Speeds up
1012 * hopeless playouts at the expense of some
1014 u
->mercymin
= atoi(optval
);
1015 } else if (!strcasecmp(optname
, "gamelen") && optval
) {
1016 u
->gamelen
= atoi(optval
);
1017 } else if (!strcasecmp(optname
, "expand_p") && optval
) {
1018 u
->expand_p
= atoi(optval
);
1019 } else if (!strcasecmp(optname
, "dumpthres") && optval
) {
1020 u
->dumpthres
= atoi(optval
);
1021 } else if (!strcasecmp(optname
, "best2_ratio") && optval
) {
1022 /* If set, prolong simulating while
1023 * first_best/second_best playouts ratio
1024 * is less than best2_ratio. */
1025 u
->best2_ratio
= atof(optval
);
1026 } else if (!strcasecmp(optname
, "bestr_ratio") && optval
) {
1027 /* If set, prolong simulating while
1028 * best,best_best_child values delta
1029 * is more than bestr_ratio. */
1030 u
->bestr_ratio
= atof(optval
);
1031 } else if (!strcasecmp(optname
, "playout_amaf")) {
1032 /* Whether to include random playout moves in
1033 * AMAF as well. (Otherwise, only tree moves
1034 * are included in AMAF. Of course makes sense
1035 * only in connection with an AMAF policy.) */
1036 /* with-without: 55.5% (+-4.1) */
1037 if (optval
&& *optval
== '0')
1038 u
->playout_amaf
= false;
1040 u
->playout_amaf
= true;
1041 } else if (!strcasecmp(optname
, "playout_amaf_nakade")) {
1042 /* Whether to include nakade moves from playouts
1043 * in the AMAF statistics; this tends to nullify
1044 * the playout_amaf effect by adding too much
1046 if (optval
&& *optval
== '0')
1047 u
->playout_amaf_nakade
= false;
1049 u
->playout_amaf_nakade
= true;
1050 } else if (!strcasecmp(optname
, "playout_amaf_cutoff") && optval
) {
1051 /* Keep only first N% of playout stage AMAF
1053 u
->playout_amaf_cutoff
= atoi(optval
);
1054 } else if ((!strcasecmp(optname
, "policy") || !strcasecmp(optname
, "random_policy")) && optval
) {
1055 char *policyarg
= strchr(optval
, ':');
1056 struct uct_policy
**p
= !strcasecmp(optname
, "policy") ? &u
->policy
: &u
->random_policy
;
1059 if (!strcasecmp(optval
, "ucb1")) {
1060 *p
= policy_ucb1_init(u
, policyarg
);
1061 } else if (!strcasecmp(optval
, "ucb1amaf")) {
1062 *p
= policy_ucb1amaf_init(u
, policyarg
);
1064 fprintf(stderr
, "UCT: Invalid tree policy %s\n", optval
);
1067 } else if (!strcasecmp(optname
, "playout") && optval
) {
1068 char *playoutarg
= strchr(optval
, ':');
1071 if (!strcasecmp(optval
, "moggy")) {
1072 u
->playout
= playout_moggy_init(playoutarg
, b
);
1073 } else if (!strcasecmp(optval
, "light")) {
1074 u
->playout
= playout_light_init(playoutarg
, b
);
1075 } else if (!strcasecmp(optval
, "elo")) {
1076 u
->playout
= playout_elo_init(playoutarg
, b
);
1079 fprintf(stderr
, "UCT: Invalid playout policy %s\n", optval
);
1082 } else if (!strcasecmp(optname
, "prior") && optval
) {
1083 u
->prior
= uct_prior_init(optval
, b
);
1084 } else if (!strcasecmp(optname
, "amaf_prior") && optval
) {
1085 u
->amaf_prior
= atoi(optval
);
1086 } else if (!strcasecmp(optname
, "threads") && optval
) {
1087 /* By default, Pachi will run with only single
1088 * tree search thread! */
1089 u
->threads
= atoi(optval
);
1090 } else if (!strcasecmp(optname
, "thread_model") && optval
) {
1091 if (!strcasecmp(optval
, "root")) {
1092 /* Root parallelization - each thread
1093 * does independent search, trees are
1094 * merged at the end. */
1095 u
->thread_model
= TM_ROOT
;
1096 u
->parallel_tree
= false;
1097 u
->virtual_loss
= false;
1098 } else if (!strcasecmp(optval
, "tree")) {
1099 /* Tree parallelization - all threads
1100 * grind on the same tree. */
1101 u
->thread_model
= TM_TREE
;
1102 u
->parallel_tree
= true;
1103 u
->virtual_loss
= false;
1104 } else if (!strcasecmp(optval
, "treevl")) {
1105 /* Tree parallelization, but also
1106 * with virtual losses - this discou-
1107 * rages most threads choosing the
1108 * same tree branches to read. */
1109 u
->thread_model
= TM_TREEVL
;
1110 u
->parallel_tree
= true;
1111 u
->virtual_loss
= true;
1113 fprintf(stderr
, "UCT: Invalid thread model %s\n", optval
);
1116 } else if (!strcasecmp(optname
, "pondering")) {
1117 /* Keep searching even during opponent's turn. */
1118 u
->pondering_opt
= !optval
|| atoi(optval
);
1119 } else if (!strcasecmp(optname
, "fuseki_end") && optval
) {
1120 /* At the very beginning it's not worth thinking
1121 * too long because the playout evaluations are
1122 * very noisy. So gradually increase the thinking
1123 * time up to maximum when fuseki_end percent
1124 * of the board has been played.
1125 * This only applies if we are not in byoyomi. */
1126 u
->fuseki_end
= atoi(optval
);
1127 } else if (!strcasecmp(optname
, "yose_start") && optval
) {
1128 /* When yose_start percent of the board has been
1129 * played, or if we are in byoyomi, stop spending
1130 * more time and spread the remaining time
1132 * Between fuseki_end and yose_start, we spend
1133 * a constant proportion of the remaining time
1134 * on each move. (yose_start should actually
1135 * be much earlier than when real yose start,
1136 * but "yose" is a good short name to convey
1138 u
->yose_start
= atoi(optval
);
1139 } else if (!strcasecmp(optname
, "force_seed") && optval
) {
1140 u
->force_seed
= atoi(optval
);
1141 } else if (!strcasecmp(optname
, "no_book")) {
1143 } else if (!strcasecmp(optname
, "dynkomi") && optval
) {
1144 /* Dynamic komi approach; there are multiple
1145 * ways to adjust komi dynamically throughout
1146 * play. We currently support two: */
1147 char *dynkomiarg
= strchr(optval
, ':');
1150 if (!strcasecmp(optval
, "none")) {
1151 u
->dynkomi
= uct_dynkomi_init_none(u
, dynkomiarg
, b
);
1152 } else if (!strcasecmp(optval
, "linear")) {
1153 u
->dynkomi
= uct_dynkomi_init_linear(u
, dynkomiarg
, b
);
1154 } else if (!strcasecmp(optval
, "adaptive")) {
1155 u
->dynkomi
= uct_dynkomi_init_adaptive(u
, dynkomiarg
, b
);
1157 fprintf(stderr
, "UCT: Invalid dynkomi mode %s\n", optval
);
1160 } else if (!strcasecmp(optname
, "dynkomi_mask") && optval
) {
1161 /* Bitmask of colors the player must be
1162 * for dynkomi be applied; you may want
1163 * to use dynkomi_mask=3 to allow dynkomi
1164 * even in games where Pachi is white. */
1165 u
->dynkomi_mask
= atoi(optval
);
1166 } else if (!strcasecmp(optname
, "dynkomi_interval") && optval
) {
1167 /* If non-zero, re-adjust dynamic komi
1168 * throughout a single genmove reading,
1169 * roughly every N simulations. */
1170 u
->dynkomi_interval
= atoi(optval
);
1171 } else if (!strcasecmp(optname
, "val_scale") && optval
) {
1172 /* How much of the game result value should be
1173 * influenced by win size. Zero means it isn't. */
1174 u
->val_scale
= atof(optval
);
1175 } else if (!strcasecmp(optname
, "val_points") && optval
) {
1176 /* Maximum size of win to be scaled into game
1177 * result value. Zero means boardsize^2. */
1178 u
->val_points
= atoi(optval
) * 2; // result values are doubled
1179 } else if (!strcasecmp(optname
, "val_extra")) {
1180 /* If false, the score coefficient will be simply
1181 * added to the value, instead of scaling the result
1182 * coefficient because of it. */
1183 u
->val_extra
= !optval
|| atoi(optval
);
1184 } else if (!strcasecmp(optname
, "local_tree") && optval
) {
1185 /* Whether to bias exploration by local tree values
1186 * (must be supported by the used policy).
1188 * 1: Do, value = result.
1189 * Try to temper the result:
1190 * 2: Do, value = 0.5+(result-expected)/2.
1191 * 3: Do, value = 0.5+bzz((result-expected)^2).
1192 * 4: Do, value = 0.5+sqrt(result-expected)/2. */
1193 u
->local_tree
= atoi(optval
);
1194 } else if (!strcasecmp(optname
, "tenuki_d") && optval
) {
1195 /* Tenuki distance at which to break the local tree. */
1196 u
->tenuki_d
= atoi(optval
);
1197 if (u
->tenuki_d
> TREE_NODE_D_MAX
+ 1) {
1198 fprintf(stderr
, "uct: tenuki_d must not be larger than TREE_NODE_D_MAX+1 %d\n", TREE_NODE_D_MAX
+ 1);
1201 } else if (!strcasecmp(optname
, "local_tree_aging") && optval
) {
1202 /* How much to reduce local tree values between moves. */
1203 u
->local_tree_aging
= atof(optval
);
1204 } else if (!strcasecmp(optname
, "local_tree_allseq")) {
1205 /* By default, only complete sequences are stored
1206 * in the local tree. If this is on, also
1207 * subsequences starting at each move are stored. */
1208 u
->local_tree_allseq
= !optval
|| atoi(optval
);
1209 } else if (!strcasecmp(optname
, "local_tree_playout")) {
1210 /* Whether to adjust ELO playout probability
1211 * distributions according to matched localtree
1213 u
->local_tree_playout
= !optval
|| atoi(optval
);
1214 } else if (!strcasecmp(optname
, "local_tree_pseqroot")) {
1215 /* By default, when we have no sequence move
1216 * to suggest in-playout, we give up. If this
1217 * is on, we make probability distribution from
1218 * sequences first moves instead. */
1219 u
->local_tree_pseqroot
= !optval
|| atoi(optval
);
1220 } else if (!strcasecmp(optname
, "pass_all_alive")) {
1221 /* Whether to consider all stones alive at the game
1222 * end instead of marking dead groupd. */
1223 u
->pass_all_alive
= !optval
|| atoi(optval
);
1224 } else if (!strcasecmp(optname
, "territory_scoring")) {
1225 /* Use territory scoring (default is area scoring).
1226 * An explicit kgs-rules command overrides this. */
1227 u
->territory_scoring
= !optval
|| atoi(optval
);
1228 } else if (!strcasecmp(optname
, "random_policy_chance") && optval
) {
1229 /* If specified (N), with probability 1/N, random_policy policy
1230 * descend is used instead of main policy descend; useful
1231 * if specified policy (e.g. UCB1AMAF) can make unduly biased
1232 * choices sometimes, you can fall back to e.g.
1233 * random_policy=UCB1. */
1234 u
->random_policy_chance
= atoi(optval
);
1235 } else if (!strcasecmp(optname
, "max_tree_size") && optval
) {
1236 /* Maximum amount of memory [MiB] consumed by the move tree.
1237 * For fast_alloc it includes the temp tree used for pruning.
1238 * Default is 3072 (3 GiB). Note that if you use TM_ROOT,
1239 * this limits size of only one of the trees, not all of them
1241 u
->max_tree_size
= atol(optval
) * 1048576;
1242 } else if (!strcasecmp(optname
, "fast_alloc")) {
1243 u
->fast_alloc
= !optval
|| atoi(optval
);
1244 } else if (!strcasecmp(optname
, "slave")) {
1245 /* Act as slave for the distributed engine. */
1246 u
->slave
= !optval
|| atoi(optval
);
1247 } else if (!strcasecmp(optname
, "banner") && optval
) {
1248 /* Additional banner string. This must come as the
1249 * last engine parameter. */
1250 if (*next
) *--next
= ',';
1251 u
->banner
= strdup(optval
);
1254 fprintf(stderr
, "uct: Invalid engine argument %s or missing value\n", optname
);
1260 u
->resign_ratio
= 0.2; /* Resign when most games are lost. */
1261 u
->loss_threshold
= 0.85; /* Stop reading if after at least 2000 playouts this is best value. */
1263 u
->policy
= policy_ucb1amaf_init(u
, NULL
);
1265 if (!!u
->random_policy_chance
^ !!u
->random_policy
) {
1266 fprintf(stderr
, "uct: Only one of random_policy and random_policy_chance is set\n");
1270 if (!u
->local_tree
) {
1271 /* No ltree aging. */
1272 u
->local_tree_aging
= 1.0f
;
1275 u
->local_tree_playout
= false;
1277 if (u
->fast_alloc
&& !u
->parallel_tree
) {
1278 fprintf(stderr
, "fast_alloc not supported with root parallelization.\n");
1282 u
->max_tree_size
= (100ULL * u
->max_tree_size
) / (100 + MIN_FREE_MEM_PERCENT
);
1285 u
->prior
= uct_prior_init(NULL
, b
);
1288 u
->playout
= playout_moggy_init(NULL
, b
);
1289 u
->playout
->debug_level
= u
->debug_level
;
1291 u
->ownermap
.map
= malloc(board_size2(b
) * sizeof(u
->ownermap
.map
[0]));
1294 u
->dynkomi
= uct_dynkomi_init_linear(u
, NULL
, b
);
1296 /* Some things remain uninitialized for now - the opening book
1297 * is not loaded and the tree not set up. */
1298 /* This will be initialized in setup_state() at the first move
1299 * received/requested. This is because right now we are not aware
1300 * about any komi or handicap setup and such. */
1306 engine_uct_init(char *arg
, struct board
*b
)
1308 struct uct
*u
= uct_state_init(arg
, b
);
1309 struct engine
*e
= calloc(1, sizeof(struct engine
));
1310 e
->name
= "UCT Engine";
1311 e
->printhook
= uct_printhook_ownermap
;
1312 e
->notify_play
= uct_notify_play
;
1314 e
->genmove
= uct_genmove
;
1315 e
->genmoves
= uct_genmoves
;
1316 e
->dead_group_list
= uct_dead_group_list
;
1320 e
->notify
= uct_notify
;
1322 const char banner
[] = "I'm playing UCT. When I'm losing, I will resign, "
1323 "if I think I win, I play until you pass. "
1324 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
1325 if (!u
->banner
) u
->banner
= "";
1326 e
->comment
= malloc(sizeof(banner
) + strlen(u
->banner
) + 1);
1327 sprintf(e
->comment
, "%s %s", banner
, u
->banner
);