From f6b10cd32a378633a4fd6a6a5494bca68746abd9 Mon Sep 17 00:00:00 2001 From: lemonsqueeze Date: Fri, 29 Apr 2016 23:50:29 +0200 Subject: [PATCH] dcnn engine: plays dcnn best move --- dcnn.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dcnn.h | 5 +++++ pachi.c | 13 ++++++++++- uct/prior.c | 49 +++--------------------------------------- uct/uct.c | 2 +- 5 files changed, 92 insertions(+), 48 deletions(-) diff --git a/dcnn.cpp b/dcnn.cpp index 8919f82..1febf57 100644 --- a/dcnn.cpp +++ b/dcnn.cpp @@ -17,6 +17,8 @@ extern "C" { #include "debug.h" #include "board.h" #include "dcnn.h" +#include "engine.h" +#include "uct/tree.h" static shared_ptr > net; @@ -116,7 +118,76 @@ dcnn_get_moves(struct board *b, enum stone color, float result[]) delete[] data; delete blob; } + +void +find_dcnn_best_moves(struct board *b, float *r, coord_t *best, float *best_r) +{ + for (int i = 0; i < DCNN_BEST_N; i++) + best[i] = pass; + + foreach_free_point(b) { + int k = (coord_x(c, b) - 1) * 19 + (coord_y(c, b) - 1); + for (int i = 0; i < DCNN_BEST_N; i++) + if (r[k] > best_r[i]) { + for (int j = DCNN_BEST_N - 1; j > i; j--) { // shift + best_r[j] = best_r[j - 1]; + best[j] = best[j - 1]; + } + best_r[i] = r[k]; + best[i] = c; + break; + } + } foreach_free_point_end; +} + +void +print_dcnn_best_moves(struct tree_node *node, struct board *b, + coord_t *best, float *best_r) +{ + fprintf(stderr, "%.*sprior_dcnn(%s) = [ ", + node->depth * 4, " ", + coord2sstr(node_coord(node), b)); + for (int i = 0; i < DCNN_BEST_N; i++) + fprintf(stderr, "%s ", coord2sstr(best[i], b)); + fprintf(stderr, "] "); + + fprintf(stderr, "[ "); + for (int i = 0; i < DCNN_BEST_N; i++) + fprintf(stderr, "%.2f ", best_r[i]); + fprintf(stderr, "]\n"); +} + +static coord_t * +dcnn_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive) +{ + float r[19 * 19]; + float best_r[DCNN_BEST_N] = { 0.0, }; + coord_t best_moves[DCNN_BEST_N]; + dcnn_get_moves(b, color, r); + find_dcnn_best_moves(b, r, best_moves, best_r); + return coord_copy(best_moves[0]); +} +struct engine * +engine_dcnn_init(char *arg, struct board *b) +{ + dcnn_init(); + if (!net) { + fprintf(stderr, "Couldn't initialize dcnn, aborting.\n"); + abort(); + } + //struct patternplay *pp = patternplay_state_init(arg); + struct engine *e = (engine*)calloc2(1, sizeof(struct engine)); + e->name = (char*)"DCNN Engine"; + e->comment = (char*)"I just select dcnn's best move."; + e->genmove = dcnn_genmove; + //e->evaluate = dcnn_evaluate; + //e->data = pp; + + return e; +} + + } /* extern "C" */ diff --git a/dcnn.h b/dcnn.h index 7a1170c..887bc8f 100644 --- a/dcnn.h +++ b/dcnn.h @@ -9,10 +9,15 @@ extern "C" { #ifdef DCNN +#define DCNN_BEST_N 5 + void dcnn_get_moves(struct board *b, enum stone color, float result[]); bool using_dcnn(struct board *b); void dcnn_quiet_caffe(int argc, char *argv[]); void dcnn_init(); +void find_dcnn_best_moves(struct board *b, float *r, coord_t *best, float *best_r); +void print_dcnn_best_moves(struct tree_node *node, struct board *b, coord_t *best, float *best_r); +struct engine *engine_dcnn_init(char *arg, struct board *b); #else diff --git a/pachi.c b/pachi.c index 334aaee..8a5c21b 100644 --- a/pachi.c +++ b/pachi.c @@ -25,6 +25,7 @@ #include "random.h" #include "version.h" #include "network.h" +#include "uct/tree.h" #include "dcnn.h" int debug_level = 3; @@ -42,6 +43,9 @@ enum engine_id { E_UCT, E_DISTRIBUTED, E_JOSEKI, +#ifdef DCNN + E_DCNN, +#endif E_MAX, }; @@ -54,6 +58,9 @@ static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = { engine_uct_init, engine_distributed_init, engine_joseki_init, +#ifdef DCNN + engine_dcnn_init, +#endif }; static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b) @@ -75,7 +82,7 @@ static void done_engine(struct engine *e) static void usage(char *name) { fprintf(stderr, "Pachi version %s\n", PACHI_VERSION); - fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n" + fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed|dcnn]\n" " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n" " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name); } @@ -117,6 +124,10 @@ int main(int argc, char *argv[]) engine = E_PATTERNPLAY; } else if (!strcasecmp(optarg, "joseki")) { engine = E_JOSEKI; +#ifdef DCNN + } else if (!strcasecmp(optarg, "dcnn")) { + engine = E_DCNN; +#endif } else { fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg); exit(1); diff --git a/uct/prior.c b/uct/prior.c index 7261d38..b1a0a42 100644 --- a/uct/prior.c +++ b/uct/prior.c @@ -10,13 +10,13 @@ #include "joseki/base.h" #include "move.h" #include "random.h" -#include "dcnn.h" #include "tactics/ladder.h" #include "tactics/util.h" #include "uct/internal.h" #include "uct/plugins.h" #include "uct/prior.h" #include "uct/tree.h" +#include "dcnn.h" /* Applying heuristic values to the tree nodes, skewing the reading in * most interesting directions. */ @@ -70,49 +70,6 @@ uct_prior_eye(struct uct *u, struct tree_node *node, struct prior_map *map) #ifdef DCNN -#define DCNN_BEST_N 5 - -static void -find_dcnn_best_moves(struct prior_map *map, float *r, coord_t *best, float *best_r) -{ - struct board *b = map->b; - - for (int i = 0; i < DCNN_BEST_N; i++) - best[i] = pass; - - foreach_free_point(b) { - if (!map->consider[c]) - continue; - - int k = (coord_x(c, b) - 1) * 19 + (coord_y(c, b) - 1); - for (int i = 0; i < DCNN_BEST_N; i++) - if (r[k] > best_r[i]) { - for (int j = DCNN_BEST_N - 1; j > i; j--) { // shift - best_r[j] = best_r[j - 1]; - best[j] = best[j - 1]; - } - best_r[i] = r[k]; - best[i] = c; - break; - } - } foreach_free_point_end; -} - -static void -print_dcnn_best_moves(struct tree_node *node, struct prior_map *map, - coord_t *best, float *best_r) -{ - fprintf(stderr, "dcnn best: [ "); - for (int i = 0; i < DCNN_BEST_N; i++) - fprintf(stderr, "%s ", coord2sstr(best[i], map->b)); - fprintf(stderr, "] "); - - fprintf(stderr, "[ "); - for (int i = 0; i < DCNN_BEST_N; i++) - fprintf(stderr, "%.2f ", best_r[i]); - fprintf(stderr, "]\n"); -} - static void uct_prior_dcnn(struct uct *u, struct tree_node *node, struct prior_map *map) { @@ -120,8 +77,8 @@ uct_prior_dcnn(struct uct *u, struct tree_node *node, struct prior_map *map) float best_r[DCNN_BEST_N] = { 0.0, }; coord_t best_moves[DCNN_BEST_N]; dcnn_get_moves(map->b, map->to_play, r); - find_dcnn_best_moves(map, r, best_moves, best_r); - print_dcnn_best_moves(node, map, best_moves, best_r); + find_dcnn_best_moves(map->b, r, best_moves, best_r); + print_dcnn_best_moves(node, map->b, best_moves, best_r); foreach_free_point(map->b) { if (!map->consider[c]) diff --git a/uct/uct.c b/uct/uct.c index 8999f46..4a7f761 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -13,7 +13,6 @@ #include "chat.h" #include "move.h" #include "mq.h" -#include "dcnn.h" #include "joseki/base.h" #include "playout.h" #include "playout/moggy.h" @@ -29,6 +28,7 @@ #include "uct/tree.h" #include "uct/uct.h" #include "uct/walk.h" +#include "dcnn.h" struct uct_policy *policy_ucb1_init(struct uct *u, char *arg); struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg, struct board *board); -- 2.11.4.GIT