caffe dcnn support
[pachi.git] / pachi.c
blob334aaeea5e4266c35c8abf061e84bca4fcc37cbc
1 #define DEBUG
2 #include <assert.h>
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <unistd.h>
10 #include "board.h"
11 #include "debug.h"
12 #include "engine.h"
13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "patternscan/patternscan.h"
17 #include "patternplay/patternplay.h"
18 #include "joseki/joseki.h"
19 #include "t-unit/test.h"
20 #include "uct/uct.h"
21 #include "distributed/distributed.h"
22 #include "gtp.h"
23 #include "chat.h"
24 #include "timeinfo.h"
25 #include "random.h"
26 #include "version.h"
27 #include "network.h"
28 #include "dcnn.h"
30 int debug_level = 3;
31 bool debug_boardprint = true;
32 long verbose_logs = 0;
33 int seed;
36 enum engine_id {
37 E_RANDOM,
38 E_REPLAY,
39 E_PATTERNSCAN,
40 E_PATTERNPLAY,
41 E_MONTECARLO,
42 E_UCT,
43 E_DISTRIBUTED,
44 E_JOSEKI,
45 E_MAX,
48 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
49 engine_random_init,
50 engine_replay_init,
51 engine_patternscan_init,
52 engine_patternplay_init,
53 engine_montecarlo_init,
54 engine_uct_init,
55 engine_distributed_init,
56 engine_joseki_init,
59 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
61 char *arg = e_arg? strdup(e_arg) : e_arg;
62 assert(engine < E_MAX);
63 struct engine *e = engine_init[engine](arg, b);
64 if (arg) free(arg);
65 return e;
68 static void done_engine(struct engine *e)
70 if (e->done) e->done(e);
71 if (e->data) free(e->data);
72 free(e);
75 static void usage(char *name)
77 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
78 fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
79 " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
80 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name);
83 int main(int argc, char *argv[])
85 enum engine_id engine = E_UCT;
86 struct time_info ti_default = { .period = TT_NULL };
87 char *testfile = NULL;
88 char *gtp_port = NULL;
89 char *log_port = NULL;
90 int gtp_sock = -1;
91 char *chatfile = NULL;
92 char *fbookfile = NULL;
93 char *ruleset = NULL;
95 seed = time(NULL) ^ getpid();
97 int opt;
98 while ((opt = getopt(argc, argv, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
99 switch (opt) {
100 case 'c':
101 chatfile = strdup(optarg);
102 break;
103 case 'e':
104 if (!strcasecmp(optarg, "random")) {
105 engine = E_RANDOM;
106 } else if (!strcasecmp(optarg, "replay")) {
107 engine = E_REPLAY;
108 } else if (!strcasecmp(optarg, "montecarlo")) {
109 engine = E_MONTECARLO;
110 } else if (!strcasecmp(optarg, "uct")) {
111 engine = E_UCT;
112 } else if (!strcasecmp(optarg, "distributed")) {
113 engine = E_DISTRIBUTED;
114 } else if (!strcasecmp(optarg, "patternscan")) {
115 engine = E_PATTERNSCAN;
116 } else if (!strcasecmp(optarg, "patternplay")) {
117 engine = E_PATTERNPLAY;
118 } else if (!strcasecmp(optarg, "joseki")) {
119 engine = E_JOSEKI;
120 } else {
121 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
122 exit(1);
124 break;
125 case 'd':
126 debug_level = atoi(optarg);
127 break;
128 case 'D':
129 debug_boardprint = false;
130 break;
131 case 'f':
132 fbookfile = strdup(optarg);
133 break;
134 case 'g':
135 gtp_port = strdup(optarg);
136 break;
137 case 'l':
138 log_port = strdup(optarg);
139 break;
140 case 'r':
141 ruleset = strdup(optarg);
142 break;
143 case 's':
144 seed = atoi(optarg);
145 break;
146 case 't':
147 /* Time settings to follow; if specified,
148 * GTP time information is ignored. Useful
149 * e.g. when you want to force your bot to
150 * play weaker while giving the opponent
151 * reasonable time to play, or force play
152 * by number of simulations in timed games. */
153 /* Please see timeinfo.h:time_parse()
154 * description for syntax details. */
155 if (!time_parse(&ti_default, optarg)) {
156 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
157 exit(1);
159 ti_default.ignore_gtp = true;
160 assert(ti_default.period != TT_NULL);
161 break;
162 case 'u':
163 testfile = strdup(optarg);
164 break;
165 default: /* '?' */
166 usage(argv[0]);
167 exit(1);
171 dcnn_quiet_caffe(argc, argv);
172 if (log_port)
173 open_log_port(log_port);
175 fast_srandom(seed);
176 if (DEBUGL(0))
177 fprintf(stderr, "Random seed: %d\n", seed);
179 struct board *b = board_init(fbookfile);
180 if (ruleset) {
181 if (!board_set_rules(b, ruleset)) {
182 fprintf(stderr, "Unknown ruleset: %s\n", ruleset);
183 exit(1);
187 struct time_info ti[S_MAX];
188 ti[S_BLACK] = ti_default;
189 ti[S_WHITE] = ti_default;
191 chat_init(chatfile);
193 char *e_arg = NULL;
194 if (optind < argc)
195 e_arg = argv[optind];
196 struct engine *e = init_engine(engine, e_arg, b);
198 if (testfile) {
199 unittest(testfile);
200 return 0;
203 if (gtp_port) {
204 open_gtp_connection(&gtp_sock, gtp_port);
207 for (;;) {
208 char buf[4096];
209 while (fgets(buf, 4096, stdin)) {
210 if (DEBUGL(1))
211 fprintf(stderr, "IN: %s", buf);
213 enum parse_code c = gtp_parse(b, e, ti, buf);
214 if (c == P_ENGINE_RESET) {
215 ti[S_BLACK] = ti_default;
216 ti[S_WHITE] = ti_default;
217 if (!e->keep_on_clear) {
218 b->es = NULL;
219 done_engine(e);
220 e = init_engine(engine, e_arg, b);
222 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
223 /* The gtp command is a weak identity check,
224 * close the connection with a wrong peer. */
225 break;
228 if (!gtp_port) break;
229 open_gtp_connection(&gtp_sock, gtp_port);
231 done_engine(e);
232 chat_done();
233 free(testfile);
234 free(gtp_port);
235 free(log_port);
236 free(chatfile);
237 free(fbookfile);
238 free(ruleset);
239 return 0;