From a62690661158ea67e291a9abf8827f860535a580 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Mon, 29 Nov 2010 19:26:39 +0100 Subject: [PATCH] UCT book -> tbook (tree book), added short explanation to HACKING --- HACKING | 12 ++++++++++++ Makefile | 2 +- distributed/distributed.c | 4 ++-- genbook.sh => gentbook.sh | 26 +++++++++++++------------- gtp.c | 14 +++++++------- uct/internal.h | 2 +- uct/search.c | 2 +- uct/tree.c | 8 ++++---- uct/tree.h | 2 +- uct/uct.c | 18 +++++++++--------- uct/uct.h | 4 ++-- 11 files changed, 53 insertions(+), 41 deletions(-) rename genbook.sh => gentbook.sh (60%) diff --git a/HACKING b/HACKING index 59947d5..241d64c 100644 --- a/HACKING +++ b/HACKING @@ -255,3 +255,15 @@ are matched per-quadrant, no other move must be within the quadrant. See joseki/README for details on usage of the auxiliary engine and a full recipe for making Pachi play according to joseki sequences extracted from the Kogo dictionary. + + +Opening Book +============ + +So far, there is no explicit support for opening book. + +However, the UCT engine can "pre-read" the starting board position and +dump the core of the built tree to a file, loading it later. This is +called a 'tbook' (as in "tree book") and can be generated using the +./gentbook.sh script. The newly generated file is automatically used +by the UCT engine when found. diff --git a/Makefile b/Makefile index 2665f3c..1326083 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ zzgo: $(OBJS) zzgo.o $(LOCALLIBS) .PHONY: zzgo-profiled zzgo-profiled: @make clean all XLDFLAGS=-fprofile-generate XCFLAGS="-fprofile-generate -fomit-frame-pointer -frename-registers" - ./zzgo -t =5000 no_book slaves_quit) - || !strcasecmp(cmd, "uct_genbook") - || !strcasecmp(cmd, "uct_dumpbook") + || !strcasecmp(cmd, "uct_gentbook") + || !strcasecmp(cmd, "uct_dumptbook") || !strcasecmp(cmd, "kgs-chat") || !strcasecmp(cmd, "time_left") diff --git a/genbook.sh b/gentbook.sh similarity index 60% rename from genbook.sh rename to gentbook.sh index c1680fe..831b44f 100755 --- a/genbook.sh +++ b/gentbook.sh @@ -12,22 +12,22 @@ else games=200000 fi -rm uctbook-$size-7.5.pachitree +rm ucttbook-$size-7.5.pachitree n=0 -genbook1() +gentbook1() { echo "[#$n:$1]" n=$((n+1)) - echo -e 'boardsize '$size'\nclear_board\nkomi 7.5\nuct_genbook b' | + echo -e 'boardsize '$size'\nclear_board\nkomi 7.5\nuct_gentbook b' | ./zzgo -t =$games "policy=ucb1amaf:explore_p=$1$popts$opts" } -genbook1 0.0 -genbook1 0.1 -genbook1 0.2 -genbook1 2.0 -genbook1 0.2 -genbook1 2.0 -genbook1 0.1 -genbook1 0.0 -genbook1 0.0 -genbook1 0.0 +gentbook1 0.0 +gentbook1 0.1 +gentbook1 0.2 +gentbook1 2.0 +gentbook1 0.2 +gentbook1 2.0 +gentbook1 0.1 +gentbook1 0.0 +gentbook1 0.0 +gentbook1 0.0 diff --git a/gtp.c b/gtp.c index 658c246..a4e8dba 100644 --- a/gtp.c +++ b/gtp.c @@ -425,23 +425,23 @@ next_group:; gtp_error(id, "illegal status specifier", NULL); } - /* Custom commands for handling UCT opening book */ - } else if (!strcasecmp(cmd, "uct_genbook")) { + /* Custom commands for handling UCT opening tbook */ + } else if (!strcasecmp(cmd, "uct_gentbook")) { /* Board must be initialized properly, as if for genmove; - * makes sense only as 'uct_genbook b'. */ + * makes sense only as 'uct_gentbook b'. */ char *arg; next_tok(arg); enum stone color = str2stone(arg); - if (uct_genbook(engine, board, &ti[color], color)) + if (uct_gentbook(engine, board, &ti[color], color)) gtp_reply(id, NULL); else - gtp_error(id, "error generating book", NULL); + gtp_error(id, "error generating tbook", NULL); - } else if (!strcasecmp(cmd, "uct_dumpbook")) { + } else if (!strcasecmp(cmd, "uct_dumptbook")) { char *arg; next_tok(arg); enum stone color = str2stone(arg); - uct_dumpbook(engine, board, color); + uct_dumptbook(engine, board, color); gtp_reply(id, NULL); } else if (!strcasecmp(cmd, "uct_evaluate")) { diff --git a/uct/internal.h b/uct/internal.h index 6f8c3ee..884e814 100644 --- a/uct/internal.h +++ b/uct/internal.h @@ -37,7 +37,7 @@ struct uct { int playout_amaf_cutoff; int dumpthres; int force_seed; - bool no_book; + bool no_tbook; bool fast_alloc; unsigned long max_tree_size; unsigned long max_pruned_size; diff --git a/uct/search.c b/uct/search.c index cbd6f8b..0edbdc3 100644 --- a/uct/search.c +++ b/uct/search.c @@ -427,7 +427,7 @@ uct_search_check_stop(struct uct *u, struct board *b, enum stone color, } /* TODO: Early break if best->variance goes under threshold - * and we already have enough playouts (possibly thanks to book + * and we already have enough playouts (possibly thanks to tbook * or to pondering)? */ return false; } diff --git a/uct/tree.c b/uct/tree.c index 6ce79d7..ac25fcf 100644 --- a/uct/tree.c +++ b/uct/tree.c @@ -225,7 +225,7 @@ void tree_dump(struct tree *tree, int thres) { if (thres && tree->root->u.playouts / thres > 100) { - /* Be a bit sensible about this; the opening book can create + /* Be a bit sensible about this; the opening tbook can create * huge dumps at first. */ thres = tree->root->u.playouts / 100 * (thres < 1000 ? 1 : thres / 1000); } @@ -248,9 +248,9 @@ tree_book_name(struct board *b) { static char buf[256]; if (b->handicap > 0) { - sprintf(buf, "uctbook-%d-%02.01f-h%d.pachitree", b->size - 2, b->komi, b->handicap); + sprintf(buf, "ucttbook-%d-%02.01f-h%d.pachitree", b->size - 2, b->komi, b->handicap); } else { - sprintf(buf, "uctbook-%d-%02.01f.pachitree", b->size - 2, b->komi); + sprintf(buf, "ucttbook-%d-%02.01f.pachitree", b->size - 2, b->komi); } return buf; } @@ -333,7 +333,7 @@ tree_load(struct tree *tree, struct board *b) if (!f) return; - fprintf(stderr, "Loading opening book %s...\n", filename); + fprintf(stderr, "Loading opening tbook %s...\n", filename); int num = 0; if (fgetc(f)) diff --git a/uct/tree.h b/uct/tree.h index 57a619a..3b92b03 100644 --- a/uct/tree.h +++ b/uct/tree.h @@ -58,7 +58,7 @@ struct tree_node { hash_t hash; struct tree_node *parent, *sibling, *children; - /*** From here on, struct is saved/loaded from opening book */ + /*** From here on, struct is saved/loaded from opening tbook */ unsigned short depth; // just for statistics diff --git a/uct/uct.c b/uct/uct.c index 46823c5..4631ebd 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -47,7 +47,7 @@ setup_state(struct uct *u, struct board *b, enum stone color) if (UDEBUGL(0)) fprintf(stderr, "Fresh board with random seed %lu\n", fast_getseed()); //board_print(b, stderr); - if (!u->no_book && b->moves == 0) { + if (!u->no_tbook && b->moves == 0) { assert(color == S_BLACK); tree_load(u->t, b); } @@ -138,7 +138,7 @@ uct_notify_play(struct engine *e, struct board *b, struct move *m) struct uct *u = e->data; if (!u->t) { /* No state, create one - this is probably game beginning - * and we need to load the opening book right now. */ + * and we need to load the opening tbook right now. */ uct_prepare_move(u, b, m->color); assert(u->t); } @@ -449,14 +449,14 @@ uct_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone bool -uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color) +uct_gentbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color) { struct uct *u = e->data; if (!u->t) uct_prepare_move(u, b, color); assert(u->t); if (ti->dim == TD_GAMES) { - /* Don't count in games that already went into the book. */ + /* Don't count in games that already went into the tbook. */ ti->len.games += u->t->root->u.playouts; } uct_search(u, b, ti, color, u->t); @@ -468,7 +468,7 @@ uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone } void -uct_dumpbook(struct engine *e, struct board *b, enum stone color) +uct_dumptbook(struct engine *e, struct board *b, enum stone color) { struct uct *u = e->data; struct tree *t = tree_init(b, color, u->fast_alloc ? u->max_tree_size : 0, @@ -589,9 +589,9 @@ uct_state_init(char *arg, struct board *b) } else if (!strcasecmp(optname, "force_seed") && optval) { /* Set RNG seed at the tree setup. */ u->force_seed = atoi(optval); - } else if (!strcasecmp(optname, "no_book")) { - /* Disable UCT opening book. */ - u->no_book = true; + } else if (!strcasecmp(optname, "no_tbook")) { + /* Disable UCT opening tbook. */ + u->no_tbook = true; } else if (!strcasecmp(optname, "pass_all_alive")) { /* Whether to consider passing only after all * dead groups were removed from the board; @@ -963,7 +963,7 @@ uct_state_init(char *arg, struct board *b) if (!u->dynkomi) u->dynkomi = uct_dynkomi_init_adaptive(u, NULL, b); - /* Some things remain uninitialized for now - the opening book + /* Some things remain uninitialized for now - the opening tbook * is not loaded and the tree not set up. */ /* This will be initialized in setup_state() at the first move * received/requested. This is because right now we are not aware diff --git a/uct/uct.h b/uct/uct.h index 625e10a..4a66276 100644 --- a/uct/uct.h +++ b/uct/uct.h @@ -8,8 +8,8 @@ struct engine *engine_uct_init(char *arg, struct board *b); struct board; struct time_info; -bool uct_genbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color); -void uct_dumpbook(struct engine *e, struct board *b, enum stone color); +bool uct_gentbook(struct engine *e, struct board *b, struct time_info *ti, enum stone color); +void uct_dumptbook(struct engine *e, struct board *b, enum stone color); /* Evaluate feasibility of player @color playing at @c. Will simulate * this move for time @ti, then return 1-max(opponent_win_likelihood). */ -- 2.11.4.GIT