UCT prior: Make pattern default at 10*eqex (200 for 19x19)
[pachi/t.git] / pachi.c
blob8f5f2bb6d0c2497d1da3e49f79efb25bb309e618
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 "timeinfo.h"
24 #include "random.h"
25 #include "version.h"
26 #include "network.h"
28 int debug_level = 3;
29 bool debug_boardprint = true;
30 long verbose_logs = 0;
31 int seed;
34 enum engine_id {
35 E_RANDOM,
36 E_REPLAY,
37 E_PATTERNSCAN,
38 E_PATTERNPLAY,
39 E_MONTECARLO,
40 E_UCT,
41 E_DISTRIBUTED,
42 E_JOSEKI,
43 E_MAX,
46 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
47 engine_random_init,
48 engine_replay_init,
49 engine_patternscan_init,
50 engine_patternplay_init,
51 engine_montecarlo_init,
52 engine_uct_init,
53 engine_distributed_init,
54 engine_joseki_init,
57 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
59 char *arg = e_arg? strdup(e_arg) : e_arg;
60 assert(engine < E_MAX);
61 struct engine *e = engine_init[engine](arg, b);
62 if (arg) free(arg);
63 return e;
66 static void done_engine(struct engine *e)
68 if (e->done) e->done(e);
69 if (e->data) free(e->data);
70 free(e);
73 static void usage(char *name)
75 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
76 fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
77 " [-d DEBUG_LEVEL] [-D] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
78 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name);
81 int main(int argc, char *argv[])
83 enum engine_id engine = E_UCT;
84 struct time_info ti_default = { .period = TT_NULL };
85 char *testfile = NULL;
86 char *gtp_port = NULL;
87 char *log_port = NULL;
88 int gtp_sock = -1;
89 char *fbookfile = NULL;
91 seed = time(NULL) ^ getpid();
93 int opt;
94 while ((opt = getopt(argc, argv, "e:d:Df:g:l:s:t:u:")) != -1) {
95 switch (opt) {
96 case 'e':
97 if (!strcasecmp(optarg, "random")) {
98 engine = E_RANDOM;
99 } else if (!strcasecmp(optarg, "replay")) {
100 engine = E_REPLAY;
101 } else if (!strcasecmp(optarg, "montecarlo")) {
102 engine = E_MONTECARLO;
103 } else if (!strcasecmp(optarg, "uct")) {
104 engine = E_UCT;
105 } else if (!strcasecmp(optarg, "distributed")) {
106 engine = E_DISTRIBUTED;
107 } else if (!strcasecmp(optarg, "patternscan")) {
108 engine = E_PATTERNSCAN;
109 } else if (!strcasecmp(optarg, "patternplay")) {
110 engine = E_PATTERNPLAY;
111 } else if (!strcasecmp(optarg, "joseki")) {
112 engine = E_JOSEKI;
113 } else {
114 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
115 exit(1);
117 break;
118 case 'd':
119 debug_level = atoi(optarg);
120 break;
121 case 'D':
122 debug_boardprint = false;
123 break;
124 case 'f':
125 fbookfile = strdup(optarg);
126 break;
127 case 'g':
128 gtp_port = strdup(optarg);
129 break;
130 case 'l':
131 log_port = strdup(optarg);
132 break;
133 case 's':
134 seed = atoi(optarg);
135 break;
136 case 't':
137 /* Time settings to follow; if specified,
138 * GTP time information is ignored. Useful
139 * e.g. when you want to force your bot to
140 * play weaker while giving the opponent
141 * reasonable time to play, or force play
142 * by number of simulations in timed games. */
143 /* Please see timeinfo.h:time_parse()
144 * description for syntax details. */
145 if (!time_parse(&ti_default, optarg)) {
146 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
147 exit(1);
149 ti_default.ignore_gtp = true;
150 assert(ti_default.period != TT_NULL);
151 break;
152 case 'u':
153 testfile = strdup(optarg);
154 break;
155 default: /* '?' */
156 usage(argv[0]);
157 exit(1);
161 if (log_port)
162 open_log_port(log_port);
164 fast_srandom(seed);
165 if (DEBUGL(0))
166 fprintf(stderr, "Random seed: %d\n", seed);
168 struct board *b = board_init(fbookfile);
169 struct time_info ti[S_MAX];
170 ti[S_BLACK] = ti_default;
171 ti[S_WHITE] = ti_default;
173 char *e_arg = NULL;
174 if (optind < argc)
175 e_arg = argv[optind];
176 struct engine *e = init_engine(engine, e_arg, b);
178 if (testfile) {
179 unittest(testfile);
180 return 0;
183 if (gtp_port) {
184 open_gtp_connection(&gtp_sock, gtp_port);
187 for (;;) {
188 char buf[4096];
189 while (fgets(buf, 4096, stdin)) {
190 if (DEBUGL(1))
191 fprintf(stderr, "IN: %s", buf);
193 enum parse_code c = gtp_parse(b, e, ti, buf);
194 if (c == P_ENGINE_RESET) {
195 ti[S_BLACK] = ti_default;
196 ti[S_WHITE] = ti_default;
197 if (!e->keep_on_clear) {
198 b->es = NULL;
199 done_engine(e);
200 e = init_engine(engine, e_arg, b);
202 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
203 /* The gtp command is a weak identity check,
204 * close the connection with a wrong peer. */
205 break;
208 if (!gtp_port) break;
209 open_gtp_connection(&gtp_sock, gtp_port);
211 done_engine(e);
212 return 0;