From 6b6c69d24bc70bbbce2f9a083ecee2532500a692 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 11 Aug 2010 09:52:09 +0200 Subject: [PATCH] Joseki: Remove global jdict instance Each engine now has its own distance, possibly passing its reference to the playout policy. --- joseki/base.c | 11 ++++++++--- joseki/base.h | 2 -- joseki/joseki.c | 25 ++++++++++++++----------- montecarlo/montecarlo.c | 3 ++- playout/moggy.c | 8 ++++++-- playout/moggy.h | 3 ++- replay/replay.c | 3 ++- uct/internal.h | 2 ++ uct/prior.c | 2 +- uct/uct.c | 8 ++++++-- zzgo.c | 3 --- 11 files changed, 43 insertions(+), 27 deletions(-) diff --git a/joseki/base.c b/joseki/base.c index a7f9c97..e84ad0a 100644 --- a/joseki/base.c +++ b/joseki/base.c @@ -2,14 +2,13 @@ #include #include +#define DEBUG #include "board.h" #include "debug.h" #include "move.h" #include "joseki/base.h" -struct joseki_dict *jdict; - struct joseki_dict * joseki_init(int bsize) { @@ -23,7 +22,11 @@ struct joseki_dict * joseki_load(int bsize) { FILE *f = fopen("pachijoseki.dat", "r"); // XXX: size-dependent - if (!f) return NULL; + if (!f) { + if (DEBUGL(3)) + perror("pachijoseki.dat"); + return NULL; + } struct joseki_dict *jd = joseki_init(bsize); char linebuf[1024]; @@ -56,6 +59,8 @@ joseki_load(int bsize) } fclose(f); + if (DEBUGL(2)) + fprintf(stderr, "Joseki dictionary for board size %d loaded.\n", bsize - 2); return jd; } diff --git a/joseki/base.h b/joseki/base.h index 2eee3eb..b5b1cfb 100644 --- a/joseki/base.h +++ b/joseki/base.h @@ -18,8 +18,6 @@ struct joseki_dict { struct joseki_pattern *patterns; }; -extern struct joseki_dict *jdict; - struct joseki_dict *joseki_init(int bsize); struct joseki_dict *joseki_load(int bsize); void joseki_done(struct joseki_dict *); diff --git a/joseki/joseki.c b/joseki/joseki.c index 103713d..930c2e4 100644 --- a/joseki/joseki.c +++ b/joseki/joseki.c @@ -13,9 +13,11 @@ /* Internal engine state. */ struct joseki_engine { int debug_level; - int size; bool discard; + int size; + struct joseki_dict *jdict; + struct board *b[16]; // boards with reversed color, mirrored and rotated }; @@ -31,7 +33,10 @@ joseki_play(struct engine *e, struct board *b, struct move *m) if (!b->moves) { /* New game, reset state. */ j->size = board_size(b); - assert(j->size == jdict->bsize); + if (j->jdict) + assert(j->size == j->jdict->bsize); + else + j->jdict = joseki_init(j->size); j->discard = false; for (int i = 0; i < 16; i++) { board_resize(j->b[i], j->size - 2); @@ -88,7 +93,7 @@ joseki_play(struct engine *e, struct board *b, struct move *m) if (i & HASH_OCOLOR) color = stone_other(color); - coord_t **ccp = &jdict->patterns[j->b[i]->qhash[quadrant] & joseki_hash_mask].moves[color - 1]; + coord_t **ccp = &j->jdict->patterns[j->b[i]->qhash[quadrant] & joseki_hash_mask].moves[color - 1]; int count = 1; if (*ccp) { @@ -131,12 +136,12 @@ engine_joseki_done(struct engine *e) board_clear(b); for (hash_t i = 0; i < 1 << joseki_hash_bits; i++) { - for (int j = 0; j < 2; j++) { + for (int s = 0; s < 2; s++) { static const char cs[] = "bw"; - if (!jdict->patterns[i].moves[j]) + if (!j->jdict->patterns[i].moves[s]) continue; - printf("%" PRIhash " %c", i, cs[j]); - coord_t *cc = jdict->patterns[i].moves[j]; + printf("%" PRIhash " %c", i, cs[s]); + coord_t *cc = j->jdict->patterns[i].moves[s]; int count = 0; while (!is_pass(*cc)) { printf(" %s", coord2sstr(*cc, b)); @@ -147,6 +152,8 @@ engine_joseki_done(struct engine *e) } board_done(b); + + joseki_done(j->jdict); } @@ -184,10 +191,6 @@ joseki_state_init(char *arg) } } - if (jdict) - joseki_done(jdict); - jdict = joseki_init(19 + 2); // XXX - return j; } diff --git a/montecarlo/montecarlo.c b/montecarlo/montecarlo.c index 751438a..53b3249 100644 --- a/montecarlo/montecarlo.c +++ b/montecarlo/montecarlo.c @@ -5,6 +5,7 @@ #include "board.h" #include "engine.h" +#include "joseki/base.h" #include "move.h" #include "playout/elo.h" #include "playout/moggy.h" @@ -236,7 +237,7 @@ montecarlo_state_init(char *arg, struct board *b) if (playoutarg) *playoutarg++ = 0; if (!strcasecmp(optval, "moggy")) { - mc->playout = playout_moggy_init(playoutarg, b); + mc->playout = playout_moggy_init(playoutarg, b, joseki_load(b->size)); } else if (!strcasecmp(optval, "light")) { mc->playout = playout_light_init(playoutarg, b); } else if (!strcasecmp(optval, "elo")) { diff --git a/playout/moggy.c b/playout/moggy.c index f45e179..f28aa3c 100644 --- a/playout/moggy.c +++ b/playout/moggy.c @@ -41,6 +41,7 @@ struct moggy_policy { * group's liberty if that is non-self-atari. */ bool selfatari_other; + struct joseki_dict *jdict; struct pattern3s patterns; }; @@ -460,12 +461,13 @@ group_atari_check(struct playout_policy *p, struct board *b, group_t group, enum static coord_t joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct board_state *s) { + struct moggy_policy *pp = p->data; struct move_queue q; q.moves = 0; for (int i = 0; i < 4; i++) { hash_t h = b->qhash[i] & joseki_hash_mask; - coord_t *cc = jdict->patterns[h].moves[to_play]; + coord_t *cc = pp->jdict->patterns[h].moves[to_play]; if (!cc) continue; for (; !is_pass(*cc); cc++) { if (coord_quadrant(*cc, b) != i) @@ -981,7 +983,7 @@ playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m) struct playout_policy * -playout_moggy_init(char *arg, struct board *b) +playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict) { struct playout_policy *p = calloc2(1, sizeof(*p)); struct moggy_policy *pp = calloc2(1, sizeof(*pp)); @@ -990,6 +992,8 @@ playout_moggy_init(char *arg, struct board *b) p->assess = playout_moggy_assess; p->permit = playout_moggy_permit; + pp->jdict = jdict; + int rate = 90; pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate = pp->selfatarirate diff --git a/playout/moggy.h b/playout/moggy.h index 6467597..0f6c10c 100644 --- a/playout/moggy.h +++ b/playout/moggy.h @@ -3,7 +3,8 @@ struct board; struct playout_policy; +struct joseki_dict; -struct playout_policy *playout_moggy_init(char *arg, struct board *b); +struct playout_policy *playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict); #endif diff --git a/replay/replay.c b/replay/replay.c index dd7c72d..4efc515 100644 --- a/replay/replay.c +++ b/replay/replay.c @@ -4,6 +4,7 @@ #include "board.h" #include "debug.h" #include "engine.h" +#include "joseki/base.h" #include "move.h" #include "playout.h" #include "playout/elo.h" @@ -81,7 +82,7 @@ replay_state_init(char *arg, struct board *b) if (playoutarg) *playoutarg++ = 0; if (!strcasecmp(optval, "moggy")) { - r->playout = playout_moggy_init(playoutarg, b); + r->playout = playout_moggy_init(playoutarg, b, joseki_load(b->size)); } else if (!strcasecmp(optval, "light")) { r->playout = playout_light_init(playoutarg, b); } else if (!strcasecmp(optval, "elo")) { diff --git a/uct/internal.h b/uct/internal.h index b23f62d..2058965 100644 --- a/uct/internal.h +++ b/uct/internal.h @@ -15,6 +15,7 @@ struct uct_policy; struct uct_prior; struct uct_dynkomi; struct uct_pluginset; +struct joseki_dict; /* How big proportion of ownermap counts must be of one color to consider * the point sure. */ @@ -80,6 +81,7 @@ struct uct { struct playout_policy *playout; struct uct_prior *prior; struct uct_pluginset *plugins; + struct joseki_dict *jdict; /* Used within frame of single genmove. */ struct board_ownermap ownermap; diff --git a/uct/prior.c b/uct/prior.c index 2ff1f8a..033d405 100644 --- a/uct/prior.c +++ b/uct/prior.c @@ -129,7 +129,7 @@ uct_prior_joseki(struct uct *u, struct tree_node *node, struct prior_map *map) /* Q_{joseki} */ for (int i = 0; i < 4; i++) { hash_t h = map->b->qhash[i] & joseki_hash_mask; - coord_t *cc = jdict->patterns[h].moves[map->to_play - 1]; + coord_t *cc = u->jdict->patterns[h].moves[map->to_play - 1]; if (!cc) continue; for (; !is_pass(*cc); cc++) { if (coord_quadrant(*cc, map->b) != i) diff --git a/uct/uct.c b/uct/uct.c index 0fccf7f..c1c52f5 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -12,6 +12,7 @@ #include "gtp.h" #include "move.h" #include "mq.h" +#include "joseki/base.h" #include "playout.h" #include "playout/elo.h" #include "playout/moggy.h" @@ -271,6 +272,7 @@ uct_done(struct engine *e) free(u->random_policy); playout_policy_done(u->playout); uct_prior_done(u->prior); + joseki_done(u->jdict); pluginset_done(u->plugins); } @@ -510,6 +512,8 @@ uct_state_init(char *arg, struct board *b) u->plugins = pluginset_init(b); + u->jdict = joseki_load(b->size); + if (arg) { char *optspec, *next = arg; while (*next) { @@ -589,7 +593,7 @@ uct_state_init(char *arg, struct board *b) if (playoutarg) *playoutarg++ = 0; if (!strcasecmp(optval, "moggy")) { - u->playout = playout_moggy_init(playoutarg, b); + u->playout = playout_moggy_init(playoutarg, b, u->jdict); } else if (!strcasecmp(optval, "light")) { u->playout = playout_light_init(playoutarg, b); } else if (!strcasecmp(optval, "elo")) { @@ -816,7 +820,7 @@ uct_state_init(char *arg, struct board *b) u->prior = uct_prior_init(NULL, b); if (!u->playout) - u->playout = playout_moggy_init(NULL, b); + u->playout = playout_moggy_init(NULL, b, u->jdict); u->playout->debug_level = u->debug_level; u->ownermap.map = malloc2(board_size2(b) * sizeof(u->ownermap.map[0])); diff --git a/zzgo.c b/zzgo.c index 3eaf3b3..b8ce54f 100644 --- a/zzgo.c +++ b/zzgo.c @@ -15,7 +15,6 @@ #include "random/random.h" #include "patternscan/patternscan.h" #include "joseki/joseki.h" -#include "joseki/base.h" #include "t-unit/test.h" #include "uct/uct.h" #include "distributed/distributed.h" @@ -153,8 +152,6 @@ int main(int argc, char *argv[]) if (DEBUGL(0)) fprintf(stderr, "Random seed: %d\n", seed); - jdict = joseki_load(19 + 2); // XXX - struct board *b = board_init(); struct time_info ti[S_MAX]; ti[S_BLACK] = ti_default; -- 2.11.4.GIT