expand_node(): Set prior even on pass moves
[pachi.git] / uct / tree.c
blob5018818631d6e763f507e702b3f94087d4aaeae0
1 #include <assert.h>
2 #include <math.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include "board.h"
10 #include "debug.h"
11 #include "engine.h"
12 #include "move.h"
13 #include "playout.h"
14 #include "uct/internal.h"
15 #include "uct/tree.h"
18 static struct tree_node *
19 tree_init_node(struct tree *t, coord_t coord, int depth)
21 struct tree_node *n = calloc(1, sizeof(*n));
22 n->coord = coord;
23 n->depth = depth;
24 if (depth > t->max_depth)
25 t->max_depth = depth;
26 return n;
29 struct tree *
30 tree_init(struct board *board, enum stone color)
32 struct tree *t = calloc(1, sizeof(*t));
33 t->board = board;
34 /* The root PASS move is only virtual, we never play it. */
35 t->root = tree_init_node(t, pass, 0);
36 t->root_symmetry = board->symmetry;
37 return t;
41 static void
42 tree_done_node(struct tree *t, struct tree_node *n)
44 struct tree_node *ni = n->children;
45 while (ni) {
46 struct tree_node *nj = ni->sibling;
47 tree_done_node(t, ni);
48 ni = nj;
50 free(n);
53 void
54 tree_done(struct tree *t)
56 tree_done_node(t, t->root);
57 free(t);
61 static void
62 tree_node_dump(struct tree *tree, struct tree_node *node, int l, int thres)
64 for (int i = 0; i < l; i++) fputc(' ', stderr);
65 int children = 0;
66 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
67 children++;
68 fprintf(stderr, "[%s] %f (%d/%d playouts [prior %d/%d amaf %d/%d]; hints %x; %d children)\n", coord2sstr(node->coord, tree->board), node->u.value, node->u.wins, node->u.playouts, node->prior.wins, node->prior.playouts, node->amaf.wins, node->amaf.playouts, node->hints, children);
70 /* Print nodes sorted by #playouts. */
72 struct tree_node *nbox[1000]; int nboxl = 0;
73 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
74 if (ni->u.playouts > thres)
75 nbox[nboxl++] = ni;
77 while (true) {
78 int best = -1;
79 for (int i = 0; i < nboxl; i++)
80 if (nbox[i] && (best < 0 || nbox[i]->u.playouts > nbox[best]->u.playouts))
81 best = i;
82 if (best < 0)
83 break;
84 tree_node_dump(tree, nbox[best], l + 1, /* node->u.value < 0.1 ? 0 : */ thres);
85 nbox[best] = NULL;
89 void
90 tree_dump(struct tree *tree, int thres)
92 if (thres && tree->root->u.playouts / thres > 100) {
93 /* Be a bit sensible about this; the opening book can create
94 * huge dumps at first. */
95 thres = tree->root->u.playouts / 100 * (thres < 1000 ? 1 : thres / 1000);
97 tree_node_dump(tree, tree->root, 0, thres);
101 static char *
102 tree_book_name(struct board *b)
104 static char buf[256];
105 if (b->handicap > 0) {
106 sprintf(buf, "uctbook-%d-%02.01f-h%d.pachitree", b->size - 2, b->komi, b->handicap);
107 } else {
108 sprintf(buf, "uctbook-%d-%02.01f.pachitree", b->size - 2, b->komi);
110 return buf;
113 static void
114 tree_node_save(FILE *f, struct tree_node *node, int thres)
116 fputc(1, f);
117 fwrite(((void *) node) + offsetof(struct tree_node, depth),
118 sizeof(struct tree_node) - offsetof(struct tree_node, depth),
119 1, f);
121 if (node->u.playouts >= thres)
122 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
123 tree_node_save(f, ni, thres);
125 fputc(0, f);
128 void
129 tree_save(struct tree *tree, struct board *b, int thres)
131 char *filename = tree_book_name(b);
132 FILE *f = fopen(filename, "wb");
133 if (!f) {
134 perror("fopen");
135 return;
137 tree_node_save(f, tree->root, thres);
138 fputc(0, f);
139 fclose(f);
143 void
144 tree_node_load(FILE *f, struct tree_node *node, int *num, bool invert)
146 (*num)++;
148 fread(((void *) node) + offsetof(struct tree_node, depth),
149 sizeof(struct tree_node) - offsetof(struct tree_node, depth),
150 1, f);
152 /* Keep values in sane scale, otherwise we start overflowing.
153 * We may go slow here but we must be careful about not getting
154 * too huge integers.*/
155 #define MAX_PLAYOUTS 10000000
156 if (node->u.playouts > MAX_PLAYOUTS) {
157 int over = node->u.playouts - MAX_PLAYOUTS;
158 node->u.wins -= ((double) node->u.wins / node->u.playouts) * over;
159 node->u.playouts = MAX_PLAYOUTS;
161 if (node->amaf.playouts > MAX_PLAYOUTS) {
162 int over = node->amaf.playouts - MAX_PLAYOUTS;
163 node->amaf.wins -= ((double) node->amaf.wins / node->amaf.playouts) * over;
164 node->amaf.playouts = MAX_PLAYOUTS;
167 if (invert) {
168 node->u.wins = node->u.playouts - node->u.wins;
169 node->u.value = 1 - node->u.value;
170 node->amaf.wins = node->amaf.playouts - node->amaf.wins;
171 node->amaf.value = 1 - node->amaf.value;
172 node->prior.wins = node->prior.playouts - node->prior.wins;
173 node->prior.value = 1 - node->prior.value;
176 struct tree_node *ni = NULL, *ni_prev = NULL;
177 while (fgetc(f)) {
178 ni_prev = ni; ni = calloc(1, sizeof(*ni));
179 if (!node->children)
180 node->children = ni;
181 else
182 ni_prev->sibling = ni;
183 ni->parent = node;
184 tree_node_load(f, ni, num, invert);
188 void
189 tree_load(struct tree *tree, struct board *b, enum stone color)
191 char *filename = tree_book_name(b);
192 FILE *f = fopen(filename, "rb");
193 if (!f)
194 return;
196 fprintf(stderr, "Loading opening book %s...\n", filename);
198 int num = 0;
199 if (fgetc(f))
200 tree_node_load(f, tree->root, &num, color != S_BLACK);
201 fprintf(stderr, "Loaded %d nodes.\n", num);
203 fclose(f);
207 static struct tree_node *
208 tree_node_copy(struct tree_node *node)
210 struct tree_node *n2 = malloc(sizeof(*n2));
211 *n2 = *node;
212 if (!node->children)
213 return n2;
214 struct tree_node *ni = node->children;
215 struct tree_node *ni2 = tree_node_copy(ni);
216 n2->children = ni2; ni2->parent = n2;
217 while ((ni = ni->sibling)) {
218 ni2->sibling = tree_node_copy(ni);
219 ni2 = ni2->sibling; ni2->parent = n2;
221 return n2;
224 struct tree *
225 tree_copy(struct tree *tree)
227 struct tree *t2 = malloc(sizeof(*t2));
228 *t2 = *tree;
229 t2->root = tree_node_copy(tree->root);
230 return t2;
234 static void
235 tree_node_merge(struct tree_node *dest, struct tree_node *src)
237 dest->hints |= src->hints;
239 /* Merge the children, both are coord-sorted lists. */
240 struct tree_node *di = dest->children, *dip = NULL;
241 struct tree_node *si = src->children, *sip = NULL;
242 while (di && si) {
243 if (di->coord != si->coord) {
244 /* src has some extra items or misses di */
245 struct tree_node *si2 = si->sibling;
246 while (si2 && di->coord != si2->coord) {
247 si2 = si2->sibling;
249 if (!si2)
250 goto next_di; /* src misses di, move on */
251 /* chain the extra [si,si2) items before di */
252 if (dip)
253 dip->sibling = si;
254 else
255 dest->children = si;
256 while (si->sibling != si2) {
257 si->parent = dest;
258 si = si->sibling;
260 si->sibling = di;
261 si = si2;
262 if (sip)
263 sip->sibling = si;
264 else
265 src->children = si;
267 /* Matching nodes - recurse... */
268 tree_node_merge(di, si);
269 /* ...and move on. */
270 sip = si; si = si->sibling;
271 next_di:
272 dip = di; di = di->sibling;
274 if (si) {
275 if (dip)
276 dip->sibling = si;
277 else
278 dest->children = si;
279 while (si) {
280 si->parent = dest;
281 si = si->sibling;
283 if (sip)
284 sip->sibling = NULL;
285 else
286 src->children = NULL;
289 /* In case of prior playouts, we do not want to accumulate them
290 * over merges - they remain static after setup. However, different
291 * trees may have different priors non-deterministically. We just
292 * take the average. */
293 if (dest->prior.playouts != src->prior.playouts
294 || dest->prior.wins != src->prior.wins) {
295 dest->prior.playouts = (dest->prior.playouts + src->prior.playouts) / 2;
296 dest->prior.wins = (dest->prior.wins + src->prior.wins) / 2;
297 if (dest->prior.playouts)
298 dest->prior.value = dest->prior.wins / dest->prior.playouts;
301 dest->amaf.playouts += src->amaf.playouts;
302 dest->amaf.wins += src->amaf.wins;
303 if (dest->amaf.playouts)
304 dest->amaf.value = dest->amaf.wins / dest->amaf.playouts;
306 dest->u.playouts += src->u.playouts;
307 dest->u.wins += src->u.wins;
308 if (dest->prior.playouts + dest->amaf.playouts + dest->u.playouts)
309 tree_update_node_value(dest);
312 /* Merge two trees built upon the same board. Note that the operation is
313 * destructive on src. */
314 void
315 tree_merge(struct tree *dest, struct tree *src)
317 if (src->max_depth > dest->max_depth)
318 dest->max_depth = src->max_depth;
319 tree_node_merge(dest->root, src->root);
323 /* Tree symmetry: When possible, we will localize the tree to a single part
324 * of the board in tree_expand_node() and possibly flip along symmetry axes
325 * to another part of the board in tree_promote_at(). We follow b->symmetry
326 * guidelines here. */
329 void
330 tree_expand_node(struct tree *t, struct tree_node *node, struct board *b, enum stone color, int radar, struct uct_policy *policy, int parity)
332 struct tree_node *ni = tree_init_node(t, pass, node->depth + 1);
333 ni->parent = node; node->children = ni;
334 if (policy->prior)
335 policy->prior(policy, t, ni, b, color, parity);
337 /* The loop considers only the symmetry playground. */
338 if (UDEBUGL(6)) {
339 fprintf(stderr, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
340 coord2sstr(node->coord, b),
341 b->symmetry.x1, b->symmetry.y1,
342 b->symmetry.x2, b->symmetry.y2,
343 b->symmetry.type, b->symmetry.d);
345 for (int i = b->symmetry.x1; i <= b->symmetry.x2; i++) {
346 for (int j = b->symmetry.y1; j <= b->symmetry.y2; j++) {
347 if (b->symmetry.d) {
348 int x = b->symmetry.type == SYM_DIAG_DOWN ? board_size(b) - 1 - i : i;
349 if (x > j) {
350 if (UDEBUGL(7))
351 fprintf(stderr, "drop %d,%d\n", i, j);
352 continue;
356 coord_t c = coord_xy_otf(i, j, t->board);
357 if (board_at(b, c) != S_NONE)
358 continue;
359 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
360 if (b->hash /* not empty board */ && radar && !board_stone_radar(b, c, radar))
361 continue;
363 struct tree_node *nj = tree_init_node(t, c, node->depth + 1);
364 nj->parent = node; ni->sibling = nj; ni = nj;
366 if (policy->prior)
367 policy->prior(policy, t, ni, b, color, parity);
373 static coord_t
374 flip_coord(struct board *b, coord_t c,
375 bool flip_horiz, bool flip_vert, int flip_diag)
377 int x = coord_x(c, b), y = coord_y(c, b);
378 if (flip_diag) {
379 int z = x; x = y; y = z;
381 if (flip_horiz) {
382 x = board_size(b) - 1 - x;
384 if (flip_vert) {
385 y = board_size(b) - 1 - y;
387 return coord_xy_otf(x, y, b);
390 static void
391 tree_fix_node_symmetry(struct board *b, struct tree_node *node,
392 bool flip_horiz, bool flip_vert, int flip_diag)
394 node->coord = flip_coord(b, node->coord, flip_horiz, flip_vert, flip_diag);
396 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
397 tree_fix_node_symmetry(b, ni, flip_horiz, flip_vert, flip_diag);
400 static void
401 tree_fix_symmetry(struct tree *tree, struct board *b, coord_t c)
403 struct board_symmetry *s = &tree->root_symmetry;
404 int cx = coord_x(c, b), cy = coord_y(c, b);
406 /* playground X->h->v->d normalization
407 * :::.. .d...
408 * .::.. v....
409 * ..:.. .....
410 * ..... h...X
411 * ..... ..... */
412 bool flip_horiz = cx < s->x1 || cx > s->x2;
413 bool flip_vert = cy < s->y1 || cy > s->y2;
415 bool flip_diag = 0;
416 if (s->d) {
417 bool dir = (s->type == SYM_DIAG_DOWN);
418 int x = dir ^ flip_horiz ^ flip_vert ? board_size(b) - 1 - cx : cx;
419 if (flip_vert ? x < cy : x > cy) {
420 flip_diag = 1;
424 if (UDEBUGL(4)) {
425 fprintf(stderr, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
426 coord2sstr(c, b), flip_horiz, flip_vert, flip_diag,
427 coord2sstr(flip_coord(b, c, flip_horiz, flip_vert, flip_diag), b),
428 s->type, s->d, b->symmetry.type, b->symmetry.d);
430 tree_fix_node_symmetry(b, tree->root, flip_horiz, flip_vert, flip_diag);
434 static void
435 tree_unlink_node(struct tree_node *node)
437 struct tree_node *ni = node->parent;
438 if (ni->children == node) {
439 ni->children = node->sibling;
440 } else {
441 ni = ni->children;
442 while (ni->sibling != node)
443 ni = ni->sibling;
444 ni->sibling = node->sibling;
448 void
449 tree_delete_node(struct tree *tree, struct tree_node *node)
451 tree_unlink_node(node);
452 tree_done_node(tree, node);
455 void
456 tree_promote_node(struct tree *tree, struct tree_node *node)
458 assert(node->parent == tree->root);
459 tree_unlink_node(node);
460 tree_done_node(tree, tree->root);
461 tree->root = node;
462 board_symmetry_update(tree->board, &tree->root_symmetry, node->coord);
463 node->parent = NULL;
466 bool
467 tree_promote_at(struct tree *tree, struct board *b, coord_t c)
469 tree_fix_symmetry(tree, b, c);
471 for (struct tree_node *ni = tree->root->children; ni; ni = ni->sibling) {
472 if (ni->coord == c) {
473 tree_promote_node(tree, ni);
474 return true;
477 return false;
480 bool
481 tree_leaf_node(struct tree_node *node)
483 return !(node->children);
486 void
487 tree_update_node_value(struct tree_node *node)
489 bool noamaf = node->hints & NODE_HINT_NOAMAF;
490 node->u.value = (float)(node->u.wins + node->prior.wins + (!noamaf ? node->amaf.wins : 0))
491 / (node->u.playouts + node->prior.playouts + (!noamaf ? node->amaf.playouts : 0));
492 #if 0
493 { struct board b2; board_size(&b2) = 9+2;
494 fprintf(stderr, "%s->%s %d/%d %d/%d %f\n", node->parent ? coord2sstr(node->parent->coord, &b2) : NULL, coord2sstr(node->coord, &b2), node->u.wins, node->u.playouts, node->prior.wins, node->prior.playouts, node->u.value); }
495 #endif