UCT: Shortcut limit 500 -> 1000 playouts
[pachi.git] / uct / uct.c
blob2a024ea7da6d8c7bd8aaab8cfd6e91213444205e
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->value);
42 /* Max depth */
43 fprintf(stderr, "deepest %d ", t->max_depth);
45 /* Best sequence */
46 fprintf(stderr, "| seq ");
47 for (int depth = 0; depth < 6; depth++) {
48 if (best && best->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->playouts > can[c]->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]->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(b2.size2 + 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 = (b2.size - 2) * (b2.size - 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->playouts >= u->expand_p)
106 tree_expand_node(t, n, &b2, color, u->radar_d, u->policy);
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);
113 break;
116 n = u->policy->descend(u->policy, t, n, (color == orig_color ? 1 : -1), pass_limit);
117 assert(n == t->root || n->parent);
118 if (UDEBUGL(7))
119 fprintf(stderr, "-- UCT sent us to [%s] %f\n", coord2sstr(n->coord, t->board), n->value);
120 if (amaf && n->coord >= -1)
121 amaf->map[n->coord] = color;
122 struct move m = { n->coord, color };
123 int res = board_play(&b2, &m);
124 if (res < 0 || (!is_pass(m.coord) && !group_at(&b2, m.coord)) /* suicide */
125 || b2.superko_violation) {
126 if (UDEBUGL(6))
127 fprintf(stderr, "deleting invalid node %d,%d\n", coord_x(n->coord,b), coord_y(n->coord,b));
128 tree_delete_node(t, n);
129 result = -1;
130 goto end;
133 if (is_pass(n->coord)) {
134 passes++;
135 if (passes >= 2) {
136 float score = board_official_score(&b2);
137 result = (orig_color == S_BLACK) ? score < 0 : score > 0;
138 if (UDEBUGL(5))
139 fprintf(stderr, "[%d..%d] %s playout result %d (W %f)\n", orig_color, color, coord2sstr(n->coord, t->board), result, score);
140 if (UDEBUGL(6))
141 board_print(&b2, stderr);
142 break;
144 } else {
145 passes = 0;
149 assert(n == t->root || n->parent);
150 if (amaf)
151 amaf->color = stone_other(color);
152 if (result >= 0)
153 u->policy->update(u->policy, n, amaf, result);
155 end:
156 if (amaf) {
157 free(amaf->map - 1);
158 free(amaf);
160 board_done_noalloc(&b2);
161 return result;
164 static coord_t *
165 uct_genmove(struct engine *e, struct board *b, enum stone color)
167 struct uct *u = e->data;
169 if (!u->t) {
170 tree_init:
171 u->t = tree_init(b, color);
172 //board_print(b, stderr);
173 } else {
174 /* XXX: We hope that the opponent didn't suddenly play
175 * several moves in the row. */
176 for (struct tree_node *ni = u->t->root->children; ni; ni = ni->sibling)
177 if (ni->coord == b->last_move.coord) {
178 tree_promote_node(u->t, ni);
179 goto promoted;
181 fprintf(stderr, "CANNOT FIND NODE TO PROMOTE!\n");
182 tree_done(u->t);
183 goto tree_init;
184 promoted:;
187 int i;
188 for (i = 0; i < u->games; i++) {
189 int result = uct_playout(u, b, color, u->t);
190 if (result < 0) {
191 /* Tree descent has hit invalid move. */
192 continue;
195 if (i > 0 && !(i % 10000)) {
196 progress_status(u, u->t, color);
199 if (i > 0 && !(i % 500)) {
200 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color);
201 if (best && best->playouts >= 1000 && best->value >= u->loss_threshold)
202 break;
206 progress_status(u, u->t, color);
207 if (UDEBUGL(2))
208 tree_dump(u->t);
210 struct tree_node *best = u->policy->choose(u->policy, u->t->root, b, color);
211 if (!best) {
212 tree_done(u->t); u->t = NULL;
213 return coord_copy(pass);
215 if (UDEBUGL(1))
216 fprintf(stderr, "*** WINNER is %d,%d with score %1.4f (%d games)\n", coord_x(best->coord, b), coord_y(best->coord, b), best->value, u->t->root->playouts);
217 if (best->value < u->resign_ratio && !is_pass(best->coord)) {
218 tree_done(u->t); u->t = NULL;
219 return coord_copy(resign);
221 tree_promote_node(u->t, best);
222 return coord_copy(best->coord);
226 struct uct *
227 uct_state_init(char *arg)
229 struct uct *u = calloc(1, sizeof(struct uct));
231 u->debug_level = 1;
232 u->games = MC_GAMES;
233 u->gamelen = MC_GAMELEN;
234 u->expand_p = 2;
236 if (arg) {
237 char *optspec, *next = arg;
238 while (*next) {
239 optspec = next;
240 next += strcspn(next, ",");
241 if (*next) { *next++ = 0; } else { *next = 0; }
243 char *optname = optspec;
244 char *optval = strchr(optspec, '=');
245 if (optval) *optval++ = 0;
247 if (!strcasecmp(optname, "debug")) {
248 if (optval)
249 u->debug_level = atoi(optval);
250 else
251 u->debug_level++;
252 } else if (!strcasecmp(optname, "games") && optval) {
253 u->games = atoi(optval);
254 } else if (!strcasecmp(optname, "gamelen") && optval) {
255 u->gamelen = atoi(optval);
256 } else if (!strcasecmp(optname, "expand_p") && optval) {
257 u->expand_p = atoi(optval);
258 } else if (!strcasecmp(optname, "radar_d") && optval) {
259 /* For 19x19, it is good idea to set this to 3. */
260 u->radar_d = atoi(optval);
261 } else if (!strcasecmp(optname, "playout_amaf")) {
262 /* Whether to include random playout moves in
263 * AMAF as well. (Otherwise, only tree moves
264 * are included in AMAF. Of course makes sense
265 * only in connection with an AMAF policy.) */
266 u->playout_amaf = true;
267 } else if (!strcasecmp(optname, "policy") && optval) {
268 char *policyarg = strchr(optval, ':');
269 if (policyarg)
270 *policyarg++ = 0;
271 if (!strcasecmp(optval, "ucb1")) {
272 u->policy = policy_ucb1_init(u, policyarg);
273 } else if (!strcasecmp(optval, "ucb1tuned")) {
274 u->policy = policy_ucb1tuned_init(u, policyarg);
275 } else if (!strcasecmp(optval, "ucb1amaf")) {
276 u->policy = policy_ucb1amaf_init(u, policyarg);
277 } else {
278 fprintf(stderr, "UCT: Invalid tree policy %s\n", optval);
280 } else if (!strcasecmp(optname, "playout") && optval) {
281 char *playoutarg = strchr(optval, ':');
282 if (playoutarg)
283 *playoutarg++ = 0;
284 if (!strcasecmp(optval, "old")) {
285 u->playout = playout_old_init(playoutarg);
286 } else if (!strcasecmp(optval, "moggy")) {
287 u->playout = playout_moggy_init(playoutarg);
288 } else if (!strcasecmp(optval, "light")) {
289 u->playout = playout_light_init(playoutarg);
290 } else {
291 fprintf(stderr, "UCT: Invalid playout policy %s\n", optval);
293 } else {
294 fprintf(stderr, "uct: Invalid engine argument %s or missing value\n", optname);
299 u->resign_ratio = 0.2; /* Resign when most games are lost. */
300 u->loss_threshold = 0.95; /* Stop reading if after at least 500 playouts this is best value. */
301 if (!u->policy)
302 u->policy = policy_ucb1_init(u, NULL);
304 if (!u->playout)
305 u->playout = playout_light_init(NULL);
306 u->playout->debug_level = u->debug_level;
308 return u;
312 struct engine *
313 engine_uct_init(char *arg)
315 struct uct *u = uct_state_init(arg);
316 struct engine *e = calloc(1, sizeof(struct engine));
317 e->name = "UCT Engine";
318 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).";
319 e->genmove = uct_genmove;
320 e->data = u;
322 return e;