UCT: Reduce number of playouts if we already inherited many
[pachi.git] / uct / uct.c
blobf1dc30e36ad34fb59ef4b01f99c9b8a4f12505e0
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #define DEBUG
8 #include "debug.h"
9 #include "board.h"
10 #include "move.h"
11 #include "playout.h"
12 #include "playout/moggy.h"
13 #include "playout/old.h"
14 #include "playout/light.h"
15 #include "uct/internal.h"
16 #include "uct/tree.h"
17 #include "uct/uct.h"
19 struct uct_policy *policy_ucb1_init(struct uct *u, char *arg);
20 struct uct_policy *policy_ucb1tuned_init(struct uct *u, char *arg);
21 struct uct_policy *policy_ucb1amaf_init(struct uct *u, char *arg);
24 #define MC_GAMES 40000
25 #define MC_GAMELEN 400
28 static void
29 progress_status(struct uct *u, struct tree *t, enum stone color)
31 if (!UDEBUGL(0))
32 return;
34 /* Best move */
35 struct tree_node *best = u->policy->choose(u->policy, t->root, t->board, color);
36 if (!best) {
37 fprintf(stderr, "... No moves left\n");
38 return;
40 fprintf(stderr, "best %f ", best->u.value);
42 /* Max depth */
43 fprintf(stderr, "deepest % 2d ", t->max_depth - t->root->depth);
45 /* Best sequence */
46 fprintf(stderr, "| seq ");
47 for (int depth = 0; depth < 6; depth++) {
48 if (best && best->u.playouts >= 25) {
49 fprintf(stderr, "%3s ", coord2sstr(best->coord, t->board));
50 best = u->policy->choose(u->policy, best, t->board, color);
51 } else {
52 fprintf(stderr, " ");
56 /* Best candidates */
57 fprintf(stderr, "| can ");
58 int cans = 4;
59 struct tree_node *can[cans];
60 memset(can, 0, sizeof(can));
61 best = t->root->children;
62 while (best) {
63 int c = 0;
64 while ((!can[c] || best->u.playouts > can[c]->u.playouts) && ++c < cans);
65 for (int d = 0; d < c; d++) can[d] = can[d + 1];
66 if (c > 0) can[c - 1] = best;
67 best = best->sibling;
69 while (--cans >= 0) {
70 if (can[cans]) {
71 fprintf(stderr, "%3s(%.3f) ", coord2sstr(can[cans]->coord, t->board), can[cans]->u.value);
72 } else {
73 fprintf(stderr, " ");
77 fprintf(stderr, "\n");
81 static int
82 uct_playout(struct uct *u, struct board *b, enum stone color, struct tree *t)
84 struct board b2;
85 board_copy(&b2, b);
87 struct playout_amafmap *amaf = NULL;
88 if (u->policy->wants_amaf) {
89 amaf = calloc(1, sizeof(*amaf));
90 amaf->map = calloc(board_size2(&b2) + 1, sizeof(*amaf->map));
91 amaf->map++; // -1 is pass
94 /* Walk the tree until we find a leaf, then expand it and do
95 * a random playout. */
96 struct tree_node *n = t->root;
97 enum stone orig_color = color;
98 int result;
99 int pass_limit = (board_size(&b2) - 2) * (board_size(&b2) - 2) / 2;
100 int passes = is_pass(b->last_move.coord);
101 if (UDEBUGL(8))
102 fprintf(stderr, "--- UCT walk with color %d\n", color);
103 for (; pass; color = stone_other(color)) {
104 if (tree_leaf_node(n)) {
105 if (n->u.playouts >= u->expand_p)
106 tree_expand_node(t, n, &b2, color, u->radar_d, u->policy, (color == orig_color ? 1 : -1));
108 result = play_random_game(&b2, color, u->gamelen, u->playout_amaf ? amaf : NULL, u->playout);
109 if (orig_color != color && result >= 0)
110 result = !result;
111 if (UDEBUGL(7))
112 fprintf(stderr, "[%d..%d] %s random playout result %d\n", orig_color, color, coord2sstr(n->coord, t->board), result);
114 /* Reset color to the @n color. */
115 color = stone_other(color);
116 break;
119 n = u->policy->descend(u->policy, t, n, (color == orig_color ? 1 : -1), pass_limit);
120 assert(n == t->root || n->parent);
121 if (UDEBUGL(7))
122 fprintf(stderr, "-- UCT sent us to [%s] %f\n", coord2sstr(n->coord, t->board), n->u.value);
123 if (amaf && n->coord >= -1)
124 amaf->map[n->coord] = color;
125 struct move m = { n->coord, color };
126 int res = board_play(&b2, &m);
127 if (res < 0 || (!is_pass(m.coord) && !group_at(&b2, m.coord)) /* suicide */
128 || b2.superko_violation) {
129 if (UDEBUGL(6))
130 fprintf(stderr, "deleting invalid node %d,%d\n", coord_x(n->coord,b), coord_y(n->coord,b));
131 tree_delete_node(t, n);
132 result = -1;
133 goto end;
136 if (is_pass(n->coord)) {
137 passes++;
138 if (passes >= 2) {
139 float score = board_official_score(&b2);
140 result = (orig_color == S_BLACK) ? score < 0 : score > 0;
141 if (UDEBUGL(5))
142 fprintf(stderr, "[%d..%d] %s p-p scoring playout result %d (W %f)\n", orig_color, color, coord2sstr(n->coord, t->board), result, score);
143 if (UDEBUGL(6))
144 board_print(&b2, stderr);
145 break;
147 } else {
148 passes = 0;
152 assert(n == t->root || n->parent);
153 if (result >= 0)
154 u->policy->update(u->policy, t, n, color, amaf, result);
156 end:
157 if (amaf) {
158 free(amaf->map - 1);
159 free(amaf);
161 board_done_noalloc(&b2);
162 return result;
165 static coord_t *
166 uct_genmove(struct engine *e, struct board *b, enum stone color)
168 struct uct *u = e->data;
170 if (!u->t) {
171 tree_init:
172 u->t = tree_init(b, color);
173 //board_print(b, stderr);
175 if (!b->moves)
176 tree_load(u->t, b);
177 } else {
178 /* XXX: We hope that the opponent didn't suddenly play
179 * several moves in the row. */
180 for (struct tree_node *ni = u->t->root->children; ni; ni = ni->sibling)
181 if (ni->coord == b->last_move.coord) {
182 tree_promote_node(u->t, ni);
183 goto promoted;
185 fprintf(stderr, "CANNOT FIND NODE TO PROMOTE!\n");
186 tree_done(u->t);
187 goto tree_init;
188 promoted:;
191 int i;
192 for (i = 0; i < u->games - (u->t->root->u.playouts / 1.5); i++) {
193 int result = uct_playout(u, b, color, u->t);
194 if (result < 0) {
195 /* Tree descent has hit invalid move. */
196 continue;
199 if (i > 0 && !(i % 10000)) {
200 progress_status(u, u->t, color);
203 if (i > 0 && !(i % 500)) {
204 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color);
205 if (best && best->u.playouts >= 1000 && best->u.value >= u->loss_threshold)
206 break;
210 progress_status(u, u->t, color);
211 if (UDEBUGL(2))
212 tree_dump(u->t, u->dumpthres);
214 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color);
215 if (!best) {
216 tree_done(u->t); u->t = NULL;
217 return coord_copy(pass);
219 if (UDEBUGL(0))
220 fprintf(stderr, "*** WINNER is %s (%d,%d) with score %1.4f (%d/%d:%d games)\n", coord2sstr(best->coord, b), coord_x(best->coord, b), coord_y(best->coord, b), best->u.value, best->u.playouts, u->t->root->u.playouts, i);
221 if (best->u.value < u->resign_ratio && !is_pass(best->coord)) {
222 tree_done(u->t); u->t = NULL;
223 return coord_copy(resign);
225 tree_promote_node(u->t, best);
226 return coord_copy(best->coord);
229 bool
230 uct_genbook(struct engine *e, struct board *b, enum stone color)
232 struct uct *u = e->data;
233 u->t = tree_init(b, color);
235 int i;
236 for (i = 0; i < u->games; i++) {
237 int result = uct_playout(u, b, color, u->t);
238 if (result < 0) {
239 /* Tree descent has hit invalid move. */
240 continue;
243 if (i > 0 && !(i % 10000)) {
244 progress_status(u, u->t, color);
247 progress_status(u, u->t, color);
249 tree_save(u->t, b, u->games / 100);
251 tree_done(u->t);
253 return true;
256 void
257 uct_dumpbook(struct engine *e, struct board *b, enum stone color)
259 struct uct *u = e->data;
260 u->t = tree_init(b, color);
261 tree_load(u->t, b);
262 tree_dump(u->t, 0);
263 tree_done(u->t);
267 struct uct *
268 uct_state_init(char *arg)
270 struct uct *u = calloc(1, sizeof(struct uct));
272 u->debug_level = 1;
273 u->games = MC_GAMES;
274 u->gamelen = MC_GAMELEN;
275 u->expand_p = 2;
276 u->dumpthres = 500;
278 if (arg) {
279 char *optspec, *next = arg;
280 while (*next) {
281 optspec = next;
282 next += strcspn(next, ",");
283 if (*next) { *next++ = 0; } else { *next = 0; }
285 char *optname = optspec;
286 char *optval = strchr(optspec, '=');
287 if (optval) *optval++ = 0;
289 if (!strcasecmp(optname, "debug")) {
290 if (optval)
291 u->debug_level = atoi(optval);
292 else
293 u->debug_level++;
294 } else if (!strcasecmp(optname, "games") && optval) {
295 u->games = atoi(optval);
296 } else if (!strcasecmp(optname, "gamelen") && optval) {
297 u->gamelen = atoi(optval);
298 } else if (!strcasecmp(optname, "expand_p") && optval) {
299 u->expand_p = atoi(optval);
300 } else if (!strcasecmp(optname, "radar_d") && optval) {
301 /* For 19x19, it is good idea to set this to 3. */
302 u->radar_d = atoi(optval);
303 } else if (!strcasecmp(optname, "dumpthres") && optval) {
304 u->dumpthres = atoi(optval);
305 } else if (!strcasecmp(optname, "playout_amaf")) {
306 /* Whether to include random playout moves in
307 * AMAF as well. (Otherwise, only tree moves
308 * are included in AMAF. Of course makes sense
309 * only in connection with an AMAF policy.) */
310 u->playout_amaf = true;
311 } else if (!strcasecmp(optname, "policy") && optval) {
312 char *policyarg = strchr(optval, ':');
313 if (policyarg)
314 *policyarg++ = 0;
315 if (!strcasecmp(optval, "ucb1")) {
316 u->policy = policy_ucb1_init(u, policyarg);
317 } else if (!strcasecmp(optval, "ucb1tuned")) {
318 u->policy = policy_ucb1tuned_init(u, policyarg);
319 } else if (!strcasecmp(optval, "ucb1amaf")) {
320 u->policy = policy_ucb1amaf_init(u, policyarg);
321 } else {
322 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
324 } else if (!strcasecmp(optname, "playout") && optval) {
325 char *playoutarg = strchr(optval, ':');
326 if (playoutarg)
327 *playoutarg++ = 0;
328 if (!strcasecmp(optval, "old")) {
329 u->playout = playout_old_init(playoutarg);
330 } else if (!strcasecmp(optval, "moggy")) {
331 u->playout = playout_moggy_init(playoutarg);
332 } else if (!strcasecmp(optval, "light")) {
333 u->playout = playout_light_init(playoutarg);
334 } else {
335 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
337 } else {
338 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
343 u->resign_ratio = 0.2; /* Resign when most games are lost. */
344 u->loss_threshold = 0.95; /* Stop reading if after at least 500 playouts this is best value. */
345 if (!u->policy)
346 u->policy = policy_ucb1_init(u, NULL);
348 if (!u->playout)
349 u->playout = playout_moggy_init(NULL);
350 u->playout->debug_level = u->debug_level;
352 return u;
356 struct engine *
357 engine_uct_init(char *arg)
359 struct uct *u = uct_state_init(arg);
360 struct engine *e = calloc(1, sizeof(struct engine));
361 e->name = "UCT Engine";
362 e->comment = "I'm playing UCT. When we both pass, I will consider all the stones on the board alive. If you are reading this, write 'yes'. Please bear with me at the game end, I need to fill the whole board; if you help me, we will both be happier. Filling the board will not lose points (NZ rules).";
363 e->genmove = uct_genmove;
364 e->data = u;
366 return e;