UCT: Fix dynamic playouts number scaling
[pachi.git] / uct / uct.c
blob42e32d7d843f0c349fe95571b766ea587ac755e1
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, int playouts)
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, "[%d] ", playouts);
41 fprintf(stderr, "best %f ", best->u.value);
43 /* Max depth */
44 fprintf(stderr, "deepest % 2d ", t->max_depth - t->root->depth);
46 /* Best sequence */
47 fprintf(stderr, "| seq ");
48 for (int depth = 0; depth < 6; depth++) {
49 if (best && best->u.playouts >= 25) {
50 fprintf(stderr, "%3s ", coord2sstr(best->coord, t->board));
51 best = u->policy->choose(u->policy, best, t->board, color);
52 } else {
53 fprintf(stderr, " ");
57 /* Best candidates */
58 fprintf(stderr, "| can ");
59 int cans = 4;
60 struct tree_node *can[cans];
61 memset(can, 0, sizeof(can));
62 best = t->root->children;
63 while (best) {
64 int c = 0;
65 while ((!can[c] || best->u.playouts > can[c]->u.playouts) && ++c < cans);
66 for (int d = 0; d < c; d++) can[d] = can[d + 1];
67 if (c > 0) can[c - 1] = best;
68 best = best->sibling;
70 while (--cans >= 0) {
71 if (can[cans]) {
72 fprintf(stderr, "%3s(%.3f) ", coord2sstr(can[cans]->coord, t->board), can[cans]->u.value);
73 } else {
74 fprintf(stderr, " ");
78 fprintf(stderr, "\n");
82 static int
83 uct_playout(struct uct *u, struct board *b, enum stone color, struct tree *t)
85 struct board b2;
86 board_copy(&b2, b);
88 struct playout_amafmap *amaf = NULL;
89 if (u->policy->wants_amaf) {
90 amaf = calloc(1, sizeof(*amaf));
91 amaf->map = calloc(board_size2(&b2) + 1, sizeof(*amaf->map));
92 amaf->map++; // -1 is pass
95 /* Walk the tree until we find a leaf, then expand it and do
96 * a random playout. */
97 struct tree_node *n = t->root;
98 enum stone orig_color = color;
99 int result;
100 int pass_limit = (board_size(&b2) - 2) * (board_size(&b2) - 2) / 2;
101 int passes = is_pass(b->last_move.coord);
102 if (UDEBUGL(8))
103 fprintf(stderr, "--- UCT walk with color %d\n", color);
104 for (; pass; color = stone_other(color)) {
105 if (tree_leaf_node(n)) {
106 if (n->u.playouts >= u->expand_p)
107 tree_expand_node(t, n, &b2, color, u->radar_d, u->policy, (color == orig_color ? 1 : -1));
109 result = play_random_game(&b2, color, u->gamelen, u->playout_amaf ? amaf : NULL, u->playout);
110 if (orig_color != color && result >= 0)
111 result = !result;
112 if (UDEBUGL(7))
113 fprintf(stderr, "[%d..%d] %s random playout result %d\n", orig_color, color, coord2sstr(n->coord, t->board), result);
115 /* Reset color to the @n color. */
116 color = stone_other(color);
117 break;
120 n = u->policy->descend(u->policy, t, n, (color == orig_color ? 1 : -1), pass_limit);
121 assert(n == t->root || n->parent);
122 if (UDEBUGL(7))
123 fprintf(stderr, "-- UCT sent us to [%s] %f\n", coord2sstr(n->coord, t->board), n->u.value);
124 if (amaf && n->coord >= -1)
125 amaf->map[n->coord] = color;
126 struct move m = { n->coord, color };
127 int res = board_play(&b2, &m);
128 if (res < 0 || (!is_pass(m.coord) && !group_at(&b2, m.coord)) /* suicide */
129 || b2.superko_violation) {
130 if (UDEBUGL(6))
131 fprintf(stderr, "deleting invalid node %d,%d\n", coord_x(n->coord,b), coord_y(n->coord,b));
132 tree_delete_node(t, n);
133 result = -1;
134 goto end;
137 if (is_pass(n->coord)) {
138 passes++;
139 if (passes >= 2) {
140 float score = board_official_score(&b2);
141 result = (orig_color == S_BLACK) ? score < 0 : score > 0;
142 if (UDEBUGL(5))
143 fprintf(stderr, "[%d..%d] %s p-p scoring playout result %d (W %f)\n", orig_color, color, coord2sstr(n->coord, t->board), result, score);
144 if (UDEBUGL(6))
145 board_print(&b2, stderr);
146 break;
148 } else {
149 passes = 0;
153 assert(n == t->root || n->parent);
154 if (result >= 0)
155 u->policy->update(u->policy, t, n, color, amaf, result);
157 end:
158 if (amaf) {
159 free(amaf->map - 1);
160 free(amaf);
162 board_done_noalloc(&b2);
163 return result;
166 static coord_t *
167 uct_genmove(struct engine *e, struct board *b, enum stone color)
169 struct uct *u = e->data;
171 if (!u->t) {
172 tree_init:
173 u->t = tree_init(b, color);
174 //board_print(b, stderr);
176 if (!b->moves)
177 tree_load(u->t, b);
178 } else {
179 /* XXX: We hope that the opponent didn't suddenly play
180 * several moves in the row. */
181 for (struct tree_node *ni = u->t->root->children; ni; ni = ni->sibling)
182 if (ni->coord == b->last_move.coord) {
183 tree_promote_node(u->t, ni);
184 goto promoted;
186 fprintf(stderr, "CANNOT FIND NODE TO PROMOTE!\n");
187 tree_done(u->t);
188 goto tree_init;
189 promoted:;
192 int i, games = u->games - (u->t->root->u.playouts / 1.5);
193 for (i = 0; i < games; i++) {
194 int result = uct_playout(u, b, color, u->t);
195 if (result < 0) {
196 /* Tree descent has hit invalid move. */
197 continue;
200 if (i > 0 && !(i % 10000)) {
201 progress_status(u, u->t, color, i);
204 if (i > 0 && !(i % 500)) {
205 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color);
206 if (best && best->u.playouts >= 1000 && best->u.value >= u->loss_threshold)
207 break;
211 progress_status(u, u->t, color, i);
212 if (UDEBUGL(2))
213 tree_dump(u->t, u->dumpthres);
215 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color);
216 if (!best) {
217 tree_done(u->t); u->t = NULL;
218 return coord_copy(pass);
220 if (UDEBUGL(0))
221 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);
222 if (best->u.value < u->resign_ratio && !is_pass(best->coord)) {
223 tree_done(u->t); u->t = NULL;
224 return coord_copy(resign);
226 tree_promote_node(u->t, best);
227 return coord_copy(best->coord);
230 bool
231 uct_genbook(struct engine *e, struct board *b, enum stone color)
233 struct uct *u = e->data;
234 u->t = tree_init(b, color);
236 int i;
237 for (i = 0; i < u->games; i++) {
238 int result = uct_playout(u, b, color, u->t);
239 if (result < 0) {
240 /* Tree descent has hit invalid move. */
241 continue;
244 if (i > 0 && !(i % 10000)) {
245 progress_status(u, u->t, color, i);
248 progress_status(u, u->t, color, i);
250 tree_save(u->t, b, u->games / 1000);
252 tree_done(u->t);
254 return true;
257 void
258 uct_dumpbook(struct engine *e, struct board *b, enum stone color)
260 struct uct *u = e->data;
261 u->t = tree_init(b, color);
262 tree_load(u->t, b);
263 tree_dump(u->t, 0);
264 tree_done(u->t);
268 struct uct *
269 uct_state_init(char *arg)
271 struct uct *u = calloc(1, sizeof(struct uct));
273 u->debug_level = 1;
274 u->games = MC_GAMES;
275 u->gamelen = MC_GAMELEN;
276 u->expand_p = 2;
277 u->dumpthres = 500;
279 if (arg) {
280 char *optspec, *next = arg;
281 while (*next) {
282 optspec = next;
283 next += strcspn(next, ",");
284 if (*next) { *next++ = 0; } else { *next = 0; }
286 char *optname = optspec;
287 char *optval = strchr(optspec, '=');
288 if (optval) *optval++ = 0;
290 if (!strcasecmp(optname, "debug")) {
291 if (optval)
292 u->debug_level = atoi(optval);
293 else
294 u->debug_level++;
295 } else if (!strcasecmp(optname, "games") && optval) {
296 u->games = atoi(optval);
297 } else if (!strcasecmp(optname, "gamelen") && optval) {
298 u->gamelen = atoi(optval);
299 } else if (!strcasecmp(optname, "expand_p") && optval) {
300 u->expand_p = atoi(optval);
301 } else if (!strcasecmp(optname, "radar_d") && optval) {
302 /* For 19x19, it is good idea to set this to 3. */
303 u->radar_d = atoi(optval);
304 } else if (!strcasecmp(optname, "dumpthres") && optval) {
305 u->dumpthres = atoi(optval);
306 } else if (!strcasecmp(optname, "playout_amaf")) {
307 /* Whether to include random playout moves in
308 * AMAF as well. (Otherwise, only tree moves
309 * are included in AMAF. Of course makes sense
310 * only in connection with an AMAF policy.) */
311 u->playout_amaf = true;
312 } else if (!strcasecmp(optname, "policy") && optval) {
313 char *policyarg = strchr(optval, ':');
314 if (policyarg)
315 *policyarg++ = 0;
316 if (!strcasecmp(optval, "ucb1")) {
317 u->policy = policy_ucb1_init(u, policyarg);
318 } else if (!strcasecmp(optval, "ucb1tuned")) {
319 u->policy = policy_ucb1tuned_init(u, policyarg);
320 } else if (!strcasecmp(optval, "ucb1amaf")) {
321 u->policy = policy_ucb1amaf_init(u, policyarg);
322 } else {
323 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
325 } else if (!strcasecmp(optname, "playout") && optval) {
326 char *playoutarg = strchr(optval, ':');
327 if (playoutarg)
328 *playoutarg++ = 0;
329 if (!strcasecmp(optval, "old")) {
330 u->playout = playout_old_init(playoutarg);
331 } else if (!strcasecmp(optval, "moggy")) {
332 u->playout = playout_moggy_init(playoutarg);
333 } else if (!strcasecmp(optval, "light")) {
334 u->playout = playout_light_init(playoutarg);
335 } else {
336 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
338 } else {
339 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
344 u->resign_ratio = 0.2; /* Resign when most games are lost. */
345 u->loss_threshold = 0.95; /* Stop reading if after at least 500 playouts this is best value. */
346 if (!u->policy)
347 u->policy = policy_ucb1_init(u, NULL);
349 if (!u->playout)
350 u->playout = playout_moggy_init(NULL);
351 u->playout->debug_level = u->debug_level;
353 return u;
357 struct engine *
358 engine_uct_init(char *arg)
360 struct uct *u = uct_state_init(arg);
361 struct engine *e = calloc(1, sizeof(struct engine));
362 e->name = "UCT Engine";
363 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).";
364 e->genmove = uct_genmove;
365 e->data = u;
367 return e;