tree_node_dump(): Honor thres even for value < 0.1
[pachi.git] / uct / tree.c
blob7853fbecea958703a91c5160bf34dbfa8c15b364
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 sprintf(buf, "uctbook-%d-%02.01f.pachitree", b->size - 2, b->komi);
106 return buf;
109 static void
110 tree_node_save(FILE *f, struct tree_node *node, int thres)
112 fputc(1, f);
113 fwrite(((void *) node) + offsetof(struct tree_node, depth),
114 sizeof(struct tree_node) - offsetof(struct tree_node, depth),
115 1, f);
117 if (node->u.playouts >= thres)
118 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
119 tree_node_save(f, ni, thres);
121 fputc(0, f);
124 void
125 tree_save(struct tree *tree, struct board *b, int thres)
127 char *filename = tree_book_name(b);
128 FILE *f = fopen(filename, "wb");
129 if (!f) {
130 perror("fopen");
131 return;
133 tree_node_save(f, tree->root, thres);
134 fputc(0, f);
135 fclose(f);
139 void
140 tree_node_load(FILE *f, struct tree_node *node, int *num, bool invert)
142 (*num)++;
144 fread(((void *) node) + offsetof(struct tree_node, depth),
145 sizeof(struct tree_node) - offsetof(struct tree_node, depth),
146 1, f);
148 /* Keep values in sane scale, otherwise we start overflowing.
149 * We may go slow here but we must be careful about not getting
150 * too huge integers.*/
151 #define MAX_PLAYOUTS 10000000
152 if (node->u.playouts > MAX_PLAYOUTS) {
153 int over = node->u.playouts - MAX_PLAYOUTS;
154 node->u.wins -= ((double) node->u.wins / node->u.playouts) * over;
155 node->u.playouts = MAX_PLAYOUTS;
157 if (node->amaf.playouts > MAX_PLAYOUTS) {
158 int over = node->amaf.playouts - MAX_PLAYOUTS;
159 node->amaf.wins -= ((double) node->amaf.wins / node->amaf.playouts) * over;
160 node->amaf.playouts = MAX_PLAYOUTS;
163 if (invert) {
164 node->u.wins = node->u.playouts - node->u.wins;
165 node->u.value = 1 - node->u.value;
166 node->amaf.wins = node->amaf.playouts - node->amaf.wins;
167 node->amaf.value = 1 - node->amaf.value;
168 node->prior.wins = node->prior.playouts - node->prior.wins;
169 node->prior.value = 1 - node->prior.value;
172 struct tree_node *ni = NULL, *ni_prev = NULL;
173 while (fgetc(f)) {
174 ni_prev = ni; ni = calloc(1, sizeof(*ni));
175 if (!node->children)
176 node->children = ni;
177 else
178 ni_prev->sibling = ni;
179 ni->parent = node;
180 tree_node_load(f, ni, num, invert);
184 void
185 tree_load(struct tree *tree, struct board *b, enum stone color)
187 char *filename = tree_book_name(b);
188 FILE *f = fopen(filename, "rb");
189 if (!f)
190 return;
192 fprintf(stderr, "Loading opening book %s...\n", filename);
194 int num = 0;
195 if (fgetc(f))
196 tree_node_load(f, tree->root, &num, color != S_BLACK);
197 fprintf(stderr, "Loaded %d nodes.\n", num);
199 fclose(f);
203 static struct tree_node *
204 tree_node_copy(struct tree_node *node)
206 struct tree_node *n2 = malloc(sizeof(*n2));
207 *n2 = *node;
208 if (!node->children)
209 return n2;
210 struct tree_node *ni = node->children;
211 struct tree_node *ni2 = tree_node_copy(ni);
212 n2->children = ni2; ni2->parent = n2;
213 while ((ni = ni->sibling)) {
214 ni2->sibling = tree_node_copy(ni);
215 ni2 = ni2->sibling; ni2->parent = n2;
217 return n2;
220 struct tree *
221 tree_copy(struct tree *tree)
223 struct tree *t2 = malloc(sizeof(*t2));
224 *t2 = *tree;
225 t2->root = tree_node_copy(tree->root);
226 return t2;
230 static void
231 tree_node_merge(struct tree_node *dest, struct tree_node *src)
233 dest->hints |= src->hints;
235 /* Merge the children, both are coord-sorted lists. */
236 struct tree_node *di = dest->children, *dip = NULL;
237 struct tree_node *si = src->children, *sip = NULL;
238 while (di && si) {
239 if (di->coord != si->coord) {
240 /* src has some extra items or misses di */
241 struct tree_node *si2 = si->sibling;
242 while (si2 && di->coord != si2->coord) {
243 si2 = si2->sibling;
245 if (!si2)
246 goto next_di; /* src misses di, move on */
247 /* chain the extra [si,si2) items before di */
248 if (dip)
249 dip->sibling = si;
250 else
251 dest->children = si;
252 while (si->sibling != si2) {
253 si->parent = dest;
254 si = si->sibling;
256 si->sibling = di;
257 si = si2;
258 if (sip)
259 sip->sibling = si;
260 else
261 src->children = si;
263 /* Matching nodes - recurse... */
264 tree_node_merge(di, si);
265 /* ...and move on. */
266 sip = si; si = si->sibling;
267 next_di:
268 dip = di; di = di->sibling;
270 if (si) {
271 if (dip)
272 dip->sibling = si;
273 else
274 dest->children = si;
275 while (si) {
276 si->parent = dest;
277 si = si->sibling;
279 if (sip)
280 sip->sibling = NULL;
281 else
282 src->children = NULL;
285 dest->prior.playouts += src->prior.playouts;
286 dest->prior.wins += src->prior.wins;
287 if (dest->prior.playouts)
288 dest->prior.value = dest->prior.wins / dest->prior.playouts;
289 dest->amaf.playouts += src->amaf.playouts;
290 dest->amaf.wins += src->amaf.wins;
291 if (dest->amaf.playouts)
292 dest->amaf.value = dest->amaf.wins / dest->amaf.playouts;
293 dest->u.playouts += src->u.playouts;
294 dest->u.wins += src->u.wins;
295 if (dest->prior.playouts + dest->amaf.playouts + dest->u.playouts)
296 tree_update_node_value(dest);
299 /* Merge two trees built upon the same board. Note that the operation is
300 * destructive on src. */
301 void
302 tree_merge(struct tree *dest, struct tree *src)
304 if (src->max_depth > dest->max_depth)
305 dest->max_depth = src->max_depth;
306 tree_node_merge(dest->root, src->root);
310 /* Tree symmetry: When possible, we will localize the tree to a single part
311 * of the board in tree_expand_node() and possibly flip along symmetry axes
312 * to another part of the board in tree_promote_at(). We follow b->symmetry
313 * guidelines here. */
316 void
317 tree_expand_node(struct tree *t, struct tree_node *node, struct board *b, enum stone color, int radar, struct uct_policy *policy, int parity)
319 struct tree_node *ni = tree_init_node(t, pass, node->depth + 1);
320 ni->parent = node; node->children = ni;
322 /* The loop considers only the symmetry playground. */
323 if (UDEBUGL(6)) {
324 fprintf(stderr, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
325 coord2sstr(node->coord, b),
326 b->symmetry.x1, b->symmetry.y1,
327 b->symmetry.x2, b->symmetry.y2,
328 b->symmetry.type, b->symmetry.d);
330 for (int i = b->symmetry.x1; i <= b->symmetry.x2; i++) {
331 for (int j = b->symmetry.y1; j <= b->symmetry.y2; j++) {
332 if (b->symmetry.d) {
333 int x = b->symmetry.type == SYM_DIAG_DOWN ? board_size(b) - 1 - i : i;
334 if (x > j) {
335 if (UDEBUGL(7))
336 fprintf(stderr, "drop %d,%d\n", i, j);
337 continue;
341 coord_t c = coord_xy_otf(i, j, t->board);
342 if (board_at(b, c) != S_NONE)
343 continue;
344 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
345 if (b->hash /* not empty board */ && radar && !board_stone_radar(b, c, radar))
346 continue;
348 struct tree_node *nj = tree_init_node(t, c, node->depth + 1);
349 nj->parent = node; ni->sibling = nj; ni = nj;
351 if (policy->prior)
352 policy->prior(policy, t, ni, b, color, parity);
358 static coord_t
359 flip_coord(struct board *b, coord_t c,
360 bool flip_horiz, bool flip_vert, int flip_diag)
362 int x = coord_x(c, b), y = coord_y(c, b);
363 if (flip_diag) {
364 int z = x; x = y; y = z;
366 if (flip_horiz) {
367 x = board_size(b) - 1 - x;
369 if (flip_vert) {
370 y = board_size(b) - 1 - y;
372 return coord_xy_otf(x, y, b);
375 static void
376 tree_fix_node_symmetry(struct board *b, struct tree_node *node,
377 bool flip_horiz, bool flip_vert, int flip_diag)
379 node->coord = flip_coord(b, node->coord, flip_horiz, flip_vert, flip_diag);
381 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
382 tree_fix_node_symmetry(b, ni, flip_horiz, flip_vert, flip_diag);
385 static void
386 tree_fix_symmetry(struct tree *tree, struct board *b, coord_t c)
388 struct board_symmetry *s = &tree->root_symmetry;
389 int cx = coord_x(c, b), cy = coord_y(c, b);
391 /* playground X->h->v->d normalization
392 * :::.. .d...
393 * .::.. v....
394 * ..:.. .....
395 * ..... h...X
396 * ..... ..... */
397 bool flip_horiz = cx < s->x1 || cx > s->x2;
398 bool flip_vert = cy < s->y1 || cy > s->y2;
400 bool flip_diag = 0;
401 if (s->d) {
402 bool dir = (s->type == SYM_DIAG_DOWN);
403 int x = dir ^ flip_horiz ^ flip_vert ? board_size(b) - 1 - cx : cx;
404 if (flip_vert ? x < cy : x > cy) {
405 flip_diag = 1;
409 if (UDEBUGL(4)) {
410 fprintf(stderr, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
411 coord2sstr(c, b), flip_horiz, flip_vert, flip_diag,
412 coord2sstr(flip_coord(b, c, flip_horiz, flip_vert, flip_diag), b),
413 s->type, s->d, b->symmetry.type, b->symmetry.d);
415 tree_fix_node_symmetry(b, tree->root, flip_horiz, flip_vert, flip_diag);
419 static void
420 tree_unlink_node(struct tree_node *node)
422 struct tree_node *ni = node->parent;
423 if (ni->children == node) {
424 ni->children = node->sibling;
425 } else {
426 ni = ni->children;
427 while (ni->sibling != node)
428 ni = ni->sibling;
429 ni->sibling = node->sibling;
433 void
434 tree_delete_node(struct tree *tree, struct tree_node *node)
436 tree_unlink_node(node);
437 tree_done_node(tree, node);
440 void
441 tree_promote_node(struct tree *tree, struct tree_node *node)
443 assert(node->parent == tree->root);
444 tree_unlink_node(node);
445 tree_done_node(tree, tree->root);
446 tree->root = node;
447 board_symmetry_update(tree->board, &tree->root_symmetry, node->coord);
448 node->parent = NULL;
451 bool
452 tree_promote_at(struct tree *tree, struct board *b, coord_t c)
454 tree_fix_symmetry(tree, b, c);
456 for (struct tree_node *ni = tree->root->children; ni; ni = ni->sibling) {
457 if (ni->coord == c) {
458 tree_promote_node(tree, ni);
459 return true;
462 return false;
465 bool
466 tree_leaf_node(struct tree_node *node)
468 return !(node->children);
471 void
472 tree_update_node_value(struct tree_node *node)
474 bool noamaf = node->hints & NODE_HINT_NOAMAF;
475 node->u.value = (float)(node->u.wins + node->prior.wins + (!noamaf ? node->amaf.wins : 0))
476 / (node->u.playouts + node->prior.playouts + (!noamaf ? node->amaf.playouts : 0));
477 #if 0
478 { struct board b2; board_size(&b2) = 9+2;
479 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); }
480 #endif