UCT max_tree_size: Print a notification when memory gets full
[pachi.git] / zzgo.c
blob898c0bcec7ff625233afe07d84f82650bf1e95c2
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 "random.h"
21 #include "version.h"
23 int debug_level = 1;
24 int seed;
27 enum engine_id {
28 E_RANDOM,
29 E_REPLAY,
30 E_PATTERNSCAN,
31 E_MONTECARLO,
32 E_UCT,
33 E_MAX,
36 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
37 engine_random_init,
38 engine_replay_init,
39 engine_patternscan_init,
40 engine_montecarlo_init,
41 engine_uct_init,
44 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
46 char *arg = e_arg? strdup(e_arg) : e_arg;
47 assert(engine < E_MAX);
48 struct engine *e = engine_init[engine](arg, b);
49 if (arg) free(arg);
50 return e;
53 static void done_engine(struct engine *e)
55 if (e->done) e->done(e);
56 if (e->data) free(e->data);
57 free(e);
60 bool engine_reset = false;
63 int main(int argc, char *argv[])
65 enum engine_id engine = E_UCT;
66 char *testfile = NULL;
68 seed = time(NULL) ^ getpid();
70 int opt;
71 while ((opt = getopt(argc, argv, "e:d:s:t:")) != -1) {
72 switch (opt) {
73 case 'e':
74 if (!strcasecmp(optarg, "random")) {
75 engine = E_RANDOM;
76 } else if (!strcasecmp(optarg, "patternscan")) {
77 engine = E_PATTERNSCAN;
78 } else if (!strcasecmp(optarg, "replay")) {
79 engine = E_REPLAY;
80 } else if (!strcasecmp(optarg, "montecarlo")) {
81 engine = E_MONTECARLO;
82 } else if (!strcasecmp(optarg, "uct")) {
83 engine = E_UCT;
84 } else {
85 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
86 exit(1);
88 break;
89 case 'd':
90 debug_level = atoi(optarg);
91 break;
92 case 's':
93 seed = atoi(optarg);
94 break;
95 case 't':
96 testfile = strdup(optarg);
97 break;
98 default: /* '?' */
99 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
100 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct] [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t FILENAME] [ENGINE_ARGS]\n",
101 argv[0]);
102 exit(1);
106 fast_srandom(seed);
107 fprintf(stderr, "Random seed: %d\n", seed);
109 struct board *b = board_init();
111 char *e_arg = NULL;
112 if (optind < argc)
113 e_arg = argv[optind];
114 struct engine *e = init_engine(engine, e_arg, b);
116 if (testfile) {
117 unittest(testfile);
118 return 0;
121 char buf[4096];
122 while (fgets(buf, 4096, stdin)) {
123 if (DEBUGL(1))
124 fprintf(stderr, "IN: %s", buf);
125 gtp_parse(b, e, buf);
126 if (engine_reset) {
127 if (!e->keep_on_clear) {
128 b->es = NULL;
129 done_engine(e);
130 e = init_engine(engine, e_arg, b);
132 engine_reset = false;
135 done_engine(e);
136 return 0;