uct_search() result cannot change: Check against worst.time instead of desired.time
[pachi.git] / zzgo.c
blob1d46be72699177781d997387bdd0f5cba8f7195b
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 "gtp.h"
20 #include "timeinfo.h"
21 #include "random.h"
22 #include "version.h"
24 int debug_level = 3;
25 int seed;
28 enum engine_id {
29 E_RANDOM,
30 E_REPLAY,
31 E_PATTERNSCAN,
32 E_MONTECARLO,
33 E_UCT,
34 E_MAX,
37 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
38 engine_random_init,
39 engine_replay_init,
40 engine_patternscan_init,
41 engine_montecarlo_init,
42 engine_uct_init,
45 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
47 char *arg = e_arg? strdup(e_arg) : e_arg;
48 assert(engine < E_MAX);
49 struct engine *e = engine_init[engine](arg, b);
50 if (arg) free(arg);
51 return e;
54 static void done_engine(struct engine *e)
56 if (e->done) e->done(e);
57 if (e->data) free(e->data);
58 free(e);
61 bool engine_reset = false;
64 int main(int argc, char *argv[])
66 enum engine_id engine = E_UCT;
67 struct time_info ti_default = { .period = TT_NULL };
68 char *testfile = NULL;
70 seed = time(NULL) ^ getpid();
72 int opt;
73 while ((opt = getopt(argc, argv, "e:d:s:t:u:")) != -1) {
74 switch (opt) {
75 case 'e':
76 if (!strcasecmp(optarg, "random")) {
77 engine = E_RANDOM;
78 } else if (!strcasecmp(optarg, "patternscan")) {
79 engine = E_PATTERNSCAN;
80 } else if (!strcasecmp(optarg, "replay")) {
81 engine = E_REPLAY;
82 } else if (!strcasecmp(optarg, "montecarlo")) {
83 engine = E_MONTECARLO;
84 } else if (!strcasecmp(optarg, "uct")) {
85 engine = E_UCT;
86 } else {
87 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
88 exit(1);
90 break;
91 case 'd':
92 debug_level = atoi(optarg);
93 break;
94 case 's':
95 seed = atoi(optarg);
96 break;
97 case 't':
98 /* Time settings to follow; if specified,
99 * GTP time information is ignored. Useful
100 * e.g. when you want to force your bot to
101 * play weaker while giving the opponent
102 * reasonable time to play, or force play
103 * by number of simulations in timed games. */
104 /* Please see timeinfo.h:time_parse()
105 * description for syntax details. */
106 if (!time_parse(&ti_default, optarg)) {
107 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
108 exit(1);
110 ti_default.ignore_gtp = true;
111 assert(ti_default.period != TT_NULL);
112 break;
113 case 'u':
114 testfile = strdup(optarg);
115 break;
116 default: /* '?' */
117 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
118 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct] [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME] [ENGINE_ARGS]\n",
119 argv[0]);
120 exit(1);
124 fast_srandom(seed);
125 fprintf(stderr, "Random seed: %d\n", seed);
127 struct board *b = board_init();
128 struct time_info ti[S_MAX];
129 ti[S_BLACK] = ti_default;
130 ti[S_WHITE] = ti_default;
132 char *e_arg = NULL;
133 if (optind < argc)
134 e_arg = argv[optind];
135 struct engine *e = init_engine(engine, e_arg, b);
137 if (testfile) {
138 unittest(testfile);
139 return 0;
142 char buf[4096];
143 while (fgets(buf, 4096, stdin)) {
144 if (DEBUGL(1))
145 fprintf(stderr, "IN: %s", buf);
146 gtp_parse(b, e, ti, buf);
147 if (engine_reset) {
148 if (!e->keep_on_clear) {
149 b->es = NULL;
150 done_engine(e);
151 e = init_engine(engine, e_arg, b);
152 ti[S_BLACK] = ti_default;
153 ti[S_WHITE] = ti_default;
155 engine_reset = false;
158 done_engine(e);
159 return 0;