Distributed engine: fix keep_looking in slave to avoid premature search stop.
[pachi.git] / zzgo.c
blob2a27bcb035af183611347a1b41e8233f84e7e1ce
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 "t-unit/test.h"
18 #include "uct/uct.h"
19 #include "distributed/distributed.h"
20 #include "gtp.h"
21 #include "timeinfo.h"
22 #include "random.h"
23 #include "version.h"
24 #include "network.h"
26 int debug_level = 3;
27 int seed;
30 enum engine_id {
31 E_RANDOM,
32 E_REPLAY,
33 E_PATTERNSCAN,
34 E_MONTECARLO,
35 E_UCT,
36 E_DISTRIBUTED,
37 E_MAX,
40 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
41 engine_random_init,
42 engine_replay_init,
43 engine_patternscan_init,
44 engine_montecarlo_init,
45 engine_uct_init,
46 engine_distributed_init,
49 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
51 char *arg = e_arg? strdup(e_arg) : e_arg;
52 assert(engine < E_MAX);
53 struct engine *e = engine_init[engine](arg, b);
54 if (arg) free(arg);
55 return e;
58 static void done_engine(struct engine *e)
60 if (e->done) e->done(e);
61 if (e->data) free(e->data);
62 free(e);
65 static void usage(char *name)
67 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
68 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct|distributed]\n"
69 " [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
70 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [ENGINE_ARGS]\n", name);
73 int main(int argc, char *argv[])
75 enum engine_id engine = E_UCT;
76 struct time_info ti_default = { .period = TT_NULL };
77 char *testfile = NULL;
78 char *gtp_port = NULL;
79 char *log_port = NULL;
80 int gtp_sock = -1;
82 seed = time(NULL) ^ getpid();
84 int opt;
85 while ((opt = getopt(argc, argv, "e:d:g:l:s:t:u:")) != -1) {
86 switch (opt) {
87 case 'e':
88 if (!strcasecmp(optarg, "random")) {
89 engine = E_RANDOM;
90 } else if (!strcasecmp(optarg, "patternscan")) {
91 engine = E_PATTERNSCAN;
92 } else if (!strcasecmp(optarg, "replay")) {
93 engine = E_REPLAY;
94 } else if (!strcasecmp(optarg, "montecarlo")) {
95 engine = E_MONTECARLO;
96 } else if (!strcasecmp(optarg, "uct")) {
97 engine = E_UCT;
98 } else if (!strcasecmp(optarg, "distributed")) {
99 engine = E_DISTRIBUTED;
100 } else {
101 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
102 exit(1);
104 break;
105 case 'd':
106 debug_level = atoi(optarg);
107 break;
108 case 'g':
109 gtp_port = strdup(optarg);
110 break;
111 case 'l':
112 log_port = strdup(optarg);
113 break;
114 case 's':
115 seed = atoi(optarg);
116 break;
117 case 't':
118 /* Time settings to follow; if specified,
119 * GTP time information is ignored. Useful
120 * e.g. when you want to force your bot to
121 * play weaker while giving the opponent
122 * reasonable time to play, or force play
123 * by number of simulations in timed games. */
124 /* Please see timeinfo.h:time_parse()
125 * description for syntax details. */
126 if (!time_parse(&ti_default, optarg)) {
127 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
128 exit(1);
130 ti_default.ignore_gtp = true;
131 assert(ti_default.period != TT_NULL);
132 break;
133 case 'u':
134 testfile = strdup(optarg);
135 break;
136 default: /* '?' */
137 usage(argv[0]);
138 exit(1);
142 if (log_port)
143 open_log_port(log_port);
145 fast_srandom(seed);
146 if (DEBUGL(0))
147 fprintf(stderr, "Random seed: %d\n", seed);
149 struct board *b = board_init();
150 struct time_info ti[S_MAX];
151 ti[S_BLACK] = ti_default;
152 ti[S_WHITE] = ti_default;
154 char *e_arg = NULL;
155 if (optind < argc)
156 e_arg = argv[optind];
157 struct engine *e = init_engine(engine, e_arg, b);
159 if (testfile) {
160 unittest(testfile);
161 return 0;
164 if (gtp_port) {
165 open_gtp_connection(&gtp_sock, gtp_port);
168 for (;;) {
169 char buf[4096];
170 while (fgets(buf, 4096, stdin)) {
171 if (DEBUGL(1))
172 fprintf(stderr, "IN: %s", buf);
174 enum parse_code c = gtp_parse(b, e, ti, buf);
175 if (c == P_ENGINE_RESET) {
176 ti[S_BLACK] = ti_default;
177 ti[S_WHITE] = ti_default;
178 if (!e->keep_on_clear) {
179 b->es = NULL;
180 done_engine(e);
181 e = init_engine(engine, e_arg, b);
183 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
184 /* The gtp command is a weak identity check,
185 * close the connection with a wrong peer. */
186 break;
189 if (!gtp_port) break;
190 open_gtp_connection(&gtp_sock, gtp_port);
192 done_engine(e);
193 return 0;