gochatai/pachi_py/pachi/uct/uct.c
/* Run time-limited MCTS search on foreground. */
Run time-limited MCTS search on foreground.
Run time - limited MCTS search on foreground .
static int
uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone color, struct tree *t, bool print_progress)
{
	struct uct_search_state s;
	uct_search_start(u, b, color, t, ti, &s);
	if (UDEBUGL(2) && s.base_playouts > 0)
		fprintf(stderr, "<pre-simulated %d games>\n", s.base_playouts);
	while (1) {
		time_sleep(TREE_BUSYWAIT_INTERVAL);
		int i = uct_search_games(&s);
		uct_search_progress(u, b, color, t, ti, &s, i);
		if (uct_search_check_stop(u, b, color, t, ti, &s, i))
			break;
	}
	struct uct_thread_ctx *ctx = uct_search_stop();
	if (UDEBUGL(2)) tree_dump(t, u->dumpthres);
	if (UDEBUGL(2))
		fprintf(stderr, "(avg score %f/%d; dynkomi's %f/%d value %f/%d)\n",
			t->avg_score.value, t->avg_score.playouts,
			u->dynkomi->score.value, u->dynkomi->score.playouts,
			u->dynkomi->value.value, u->dynkomi->value.playouts);
	if (print_progress)
		uct_progress_status(u, t, color, ctx->games, NULL);
	if (u->debug_after.playouts > 0) {
		struct time_info debug_ti = {
			.period = TT_MOVE,
			.dim = TD_GAMES,
		};
		debug_ti.len.games = t->root->u.playouts + u->debug_after.playouts;
		board_print_custom(b, stderr, uct_printhook_ownermap);
		fprintf(stderr, "--8<-- UCT debug post-run begin (%d:%d) --8<--\n", u->debug_after.level, u->debug_after.playouts);
		int debug_level_save = debug_level;
		int u_debug_level_save = u->debug_level;
		int p_debug_level_save = u->playout->debug_level;
		debug_level = u->debug_after.level;
		u->debug_level = u->debug_after.level;
		u->playout->debug_level = u->debug_after.level;
		uct_halt = false;
		uct_playouts(u, b, color, t, &debug_ti);
		tree_dump(t, u->dumpthres);
		uct_halt = true;
		debug_level = debug_level_save;
		u->debug_level = u_debug_level_save;
		u->playout->debug_level = p_debug_level_save;
		fprintf(stderr, "--8<-- UCT debug post-run finished --8<--\n");
	}
	u->played_own += ctx->games;
	return ctx->games;
}
static int uct_search ( struct uct * u , struct board * b , struct time_info * ti , enum stone color , struct tree * t , bool print_progress ) { struct uct_search_state s ; uct_search_start ( u , b , color , t , ti , & s ) ; if ( UDEBUGL ( 2 ) && s . base_playouts > 0 ) fprintf ( stderr , " \n " , s . base_playouts ) ; while ( 1 ) { time_sleep ( TREE_BUSYWAIT_INTERVAL ) ; int i = uct_search_games ( & s ) ; uct_search_progress ( u , b , color , t , ti , & s , i ) ; if ( uct_search_check_stop ( u , b , color , t , ti , & s , i ) ) break ; } struct uct_thread_ctx * ctx = uct_search_stop ( ) ; if ( UDEBUGL ( 2 ) ) tree_dump ( t , u -> dumpthres ) ; if ( UDEBUGL ( 2 ) ) fprintf ( stderr , " \n " , t -> avg_score . value , t -> avg_score . playouts , u -> dynkomi -> score . value , u -> dynkomi -> score . playouts , u -> dynkomi -> value . value , u -> dynkomi -> value . playouts ) ; if ( print_progress ) uct_progress_status ( u , t , color , ctx -> games , NULL ) ; if ( u -> debug_after . playouts > 0 ) { struct time_info debug_ti = { . period = TT_MOVE , . dim = TD_GAMES , } ; debug_ti . len . games = t -> root -> u . playouts + u -> debug_after . playouts ; board_print_custom ( b , stderr , uct_printhook_ownermap ) ; fprintf ( stderr , " \n " , u -> debug_after . level , u -> debug_after . playouts ) ; int debug_level_save = debug_level ; int u_debug_level_save = u -> debug_level ; int p_debug_level_save = u -> playout -> debug_level ; debug_level = u -> debug_after . level ; u -> debug_level = u -> debug_after . level ; u -> playout -> debug_level = u -> debug_after . level ; uct_halt = false ; uct_playouts ( u , b , color , t , & debug_ti ) ; tree_dump ( t , u -> dumpthres ) ; uct_halt = true ; debug_level = debug_level_save ; u -> debug_level = u_debug_level_save ; u -> playout -> debug_level = p_debug_level_save ; fprintf ( stderr , " \n " ) ; } u -> played_own += ctx -> games ; return ctx -> games ; }
Run time-limited MCTS search on foreground.
Run time - limited MCTS search on foreground .
/* The search tree is ctx->t. This is currently == . It is important
	 * to reference ctx->t directly since the
	 * thread manager will swap the tree pointer asynchronously. */ /* Now, just periodically poll the search tree. */ /* Note that in case of TD_GAMES, threads will not wait for
	 * the uct_search_check_stop() signalization. */ /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the
		 * time control is broken. But if it happens to be less, we still search
		 * at least 100ms otherwise the move is completely random. */ /* Print notifications etc. */ /* Check if we should stop the search. */ /* Now, start an additional run of playouts, single threaded. */