From 95e09d4d4ff4fa122ef3a83948ce0efd09697a82 Mon Sep 17 00:00:00 2001 From: Francois van Niekerk Date: Tue, 7 Dec 2010 02:53:37 +0100 Subject: [PATCH] With TD_GAMES, perform an exact number of playouts Patch by Francois, some enhancements by Pasky. --- uct/search.c | 6 ++++-- uct/search.h | 1 + uct/uct.c | 2 ++ uct/walk.c | 11 ++++++++--- uct/walk.h | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/uct/search.c b/uct/search.c index 0edbdc3..3e4e8e0 100644 --- a/uct/search.c +++ b/uct/search.c @@ -86,7 +86,7 @@ spawn_worker(void *ctx_) /* Setup */ fast_srandom(ctx->seed); /* Run */ - ctx->games = uct_playouts(ctx->u, ctx->b, ctx->color, ctx->t); + ctx->games = uct_playouts(ctx->u, ctx->b, ctx->color, ctx->t, ctx->ti); /* Finish */ pthread_mutex_lock(&finish_serializer); pthread_mutex_lock(&finish_mutex); @@ -130,6 +130,7 @@ spawn_thread_manager(void *ctx_) ctx->u = u; ctx->b = mctx->b; ctx->color = mctx->color; mctx->t = ctx->t = t; ctx->tid = ti; ctx->seed = fast_random(65536) + ti; + ctx->ti = mctx->ti; pthread_create(&threads[ti], NULL, spawn_worker, ctx); if (UDEBUGL(3)) fprintf(stderr, "Spawned worker %d\n", ti); @@ -193,7 +194,7 @@ uct_search_start(struct uct *u, struct board *b, enum stone color, assert(u->threads > 0); assert(!thread_manager_running); static struct uct_thread_ctx mctx; - mctx = (struct uct_thread_ctx) { .u = u, .b = b, .color = color, .t = t, .seed = fast_random(65536) }; + mctx = (struct uct_thread_ctx) { .u = u, .b = b, .color = color, .t = t, .seed = fast_random(65536), .ti = ti }; s->ctx = &mctx; pthread_mutex_lock(&finish_mutex); pthread_create(&thread_manager, NULL, spawn_thread_manager, s->ctx); @@ -384,6 +385,7 @@ uct_search_check_stop(struct uct *u, struct board *b, enum stone color, * time pressure but the tree is going to be just too messed * up otherwise - we might even play invalid suicides or pass * when we mustn't. */ + assert(!(ti->dim == TD_GAMES && ti->len.games < GJ_MINGAMES)); if (i < GJ_MINGAMES) return false; diff --git a/uct/search.h b/uct/search.h index 732d2ed..e9757af 100644 --- a/uct/search.h +++ b/uct/search.h @@ -40,6 +40,7 @@ struct uct_thread_ctx { struct tree *t; unsigned long seed; int games; + struct time_info *ti; }; diff --git a/uct/uct.c b/uct/uct.c index 09d5764..4088dbd 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -297,6 +297,8 @@ uct_search(struct uct *u, struct board *b, struct time_info *ti, enum stone colo * thread manager will swap the tree pointer asynchronously. */ /* Now, just periodically poll the search tree. */ + /* Note that in case of TD_GAMES, threads will terminate independently + * of the uct_search_check_stop() signalization. */ while (1) { time_sleep(TREE_BUSYWAIT_INTERVAL); /* TREE_BUSYWAIT_INTERVAL should never be less than desired time, or the diff --git a/uct/walk.c b/uct/walk.c index 93cf127..3d80bc7 100644 --- a/uct/walk.c +++ b/uct/walk.c @@ -503,10 +503,15 @@ end: } int -uct_playouts(struct uct *u, struct board *b, enum stone color, struct tree *t) +uct_playouts(struct uct *u, struct board *b, enum stone color, struct tree *t, struct time_info *ti) { int i; - for (i = 0; !uct_halt; i++) - uct_playout(u, b, color, t); + if (ti->dim == TD_GAMES) { + for (i = 0; t->root->u.playouts <= ti->len.games; i++) + uct_playout(u, b, color, t); + } else { + for (i = 0; !uct_halt; i++) + uct_playout(u, b, color, t); + } return i; } diff --git a/uct/walk.h b/uct/walk.h index a20e38b..fe7e38e 100644 --- a/uct/walk.h +++ b/uct/walk.h @@ -10,6 +10,6 @@ struct board; void uct_progress_status(struct uct *u, struct tree *t, enum stone color, int playouts); int uct_playout(struct uct *u, struct board *b, enum stone player_color, struct tree *t); -int uct_playouts(struct uct *u, struct board *b, enum stone color, struct tree *t); +int uct_playouts(struct uct *u, struct board *b, enum stone color, struct tree *t, struct time_info *ti); #endif -- 2.11.4.GIT