Merge branch 'master' into libmap
[pachi.git] / uct / tree.c
blob19f3ac9a9d6374b00c586348b3f30fcdea9e29df
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 #define DEBUG
10 #include "board.h"
11 #include "debug.h"
12 #include "engine.h"
13 #include "move.h"
14 #include "playout.h"
15 #include "tactics/goals.h"
16 #include "tactics/util.h"
17 #include "timeinfo.h"
18 #include "uct/internal.h"
19 #include "uct/prior.h"
20 #include "uct/tree.h"
21 #include "uct/slave.h"
24 /* Allocate tree node(s). The returned nodes are initialized with zeroes.
25 * Returns NULL if not enough memory.
26 * This function may be called by multiple threads in parallel. */
27 static struct tree_node *
28 tree_alloc_node(struct tree *t, int count, bool fast_alloc)
30 struct tree_node *n = NULL;
31 size_t nsize = count * sizeof(*n);
32 unsigned long old_size = __sync_fetch_and_add(&t->nodes_size, nsize);
34 if (fast_alloc) {
35 if (old_size + nsize > t->max_tree_size)
36 return NULL;
37 assert(t->nodes != NULL);
38 n = (struct tree_node *)(t->nodes + old_size);
39 memset(n, 0, nsize);
40 } else {
41 n = calloc2(count, sizeof(*n));
43 return n;
46 /* Initialize a node at a given place in memory.
47 * This function may be called by multiple threads in parallel. */
48 static void
49 tree_setup_node(struct tree *t, struct tree_node *n, coord_t coord, int depth)
51 static volatile unsigned int hash = 0;
52 n->coord = coord;
53 n->depth = depth;
54 /* n->hash is used only for debugging. It is very likely (but not
55 * guaranteed) to be unique. */
56 hash_t h = n - (struct tree_node *)0;
57 n->hash = (h << 32) + (hash++ & 0xffffffff);
58 if (depth > t->max_depth)
59 t->max_depth = depth;
62 /* Allocate and initialize a node. Returns NULL (fast_alloc mode)
63 * or exits the main program if not enough memory.
64 * This function may be called by multiple threads in parallel. */
65 static struct tree_node *
66 tree_init_node(struct tree *t, coord_t coord, int depth, bool fast_alloc)
68 struct tree_node *n;
69 n = tree_alloc_node(t, 1, fast_alloc);
70 if (!n) return NULL;
71 tree_setup_node(t, n, coord, depth);
72 return n;
75 /* Create a tree structure. Pre-allocate all nodes if max_tree_size is > 0. */
76 struct tree *
77 tree_init(struct board *board, enum stone color, unsigned long max_tree_size,
78 unsigned long max_pruned_size, unsigned long pruning_threshold, floating_t ltree_aging, int hbits)
80 struct tree *t = calloc2(1, sizeof(*t));
81 t->board = board;
82 t->max_tree_size = max_tree_size;
83 t->max_pruned_size = max_pruned_size;
84 t->pruning_threshold = pruning_threshold;
85 if (max_tree_size != 0) {
86 t->nodes = malloc2(max_tree_size);
87 /* The nodes buffer doesn't need initialization. This is currently
88 * done by tree_init_node to spread the load. Doing a memset for the
89 * entire buffer here would be too slow for large trees (>10 GB). */
91 /* The root PASS move is only virtual, we never play it. */
92 t->root = tree_init_node(t, pass, 0, t->nodes);
93 t->root_symmetry = board->symmetry;
94 t->root_color = stone_other(color); // to research black moves, root will be white
96 t->ltree_black = tree_init_node(t, pass, 0, false);
97 t->ltree_white = tree_init_node(t, pass, 0, false);
98 t->ltree_aging = ltree_aging;
100 t->hbits = hbits;
101 if (hbits) t->htable = uct_htable_alloc(hbits);
102 return t;
106 /* This function may be called by multiple threads in parallel on the
107 * same tree, but not on node n. n may be detached from the tree but
108 * must have been created in this tree originally.
109 * It returns the remaining size of the tree after n has been freed. */
110 static unsigned long
111 tree_done_node(struct tree *t, struct tree_node *n)
113 struct tree_node *ni = n->children;
114 while (ni) {
115 struct tree_node *nj = ni->sibling;
116 tree_done_node(t, ni);
117 ni = nj;
119 free(n);
120 unsigned long old_size = __sync_fetch_and_sub(&t->nodes_size, sizeof(*n));
121 return old_size - sizeof(*n);
124 struct subtree_ctx {
125 struct tree *t;
126 struct tree_node *n;
129 /* Worker thread for tree_done_node_detached(). Only for fast_alloc=false. */
130 static void *
131 tree_done_node_worker(void *ctx_)
133 struct subtree_ctx *ctx = ctx_;
134 char *str = coord2str(node_coord(ctx->n), ctx->t->board);
136 unsigned long tree_size = tree_done_node(ctx->t, ctx->n);
137 if (!tree_size)
138 free(ctx->t);
139 if (DEBUGL(2))
140 fprintf(stderr, "done freeing node at %s, tree size %lu\n", str, tree_size);
141 free(str);
142 free(ctx);
143 return NULL;
146 /* Asynchronously free the subtree of nodes rooted at n. If the tree becomes
147 * empty free the tree also. Only for fast_alloc=false. */
148 static void
149 tree_done_node_detached(struct tree *t, struct tree_node *n)
151 if (n->u.playouts < 1000) { // no thread for small tree
152 if (!tree_done_node(t, n))
153 free(t);
154 return;
156 pthread_attr_t attr;
157 pthread_attr_init(&attr);
158 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
160 pthread_t thread;
161 struct subtree_ctx *ctx = malloc2(sizeof(struct subtree_ctx));
162 ctx->t = t;
163 ctx->n = n;
164 pthread_create(&thread, &attr, tree_done_node_worker, ctx);
165 pthread_attr_destroy(&attr);
168 void
169 tree_done(struct tree *t)
171 tree_done_node(t, t->ltree_black);
172 tree_done_node(t, t->ltree_white);
174 if (t->htable) free(t->htable);
175 if (t->nodes) {
176 free(t->nodes);
177 free(t);
178 } else if (!tree_done_node(t, t->root)) {
179 free(t);
180 /* A tree_done_node_worker might still be running on this tree but
181 * it will free the tree later. It is also freeing nodes faster than
182 * we will create new ones. */
187 static void
188 tree_node_dump(struct tree *tree, struct tree_node *node, int treeparity, int l, int thres)
190 for (int i = 0; i < l; i++) fputc(' ', stderr);
191 int children = 0;
192 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
193 children++;
195 /* FIXME: libmap info is correct only at root node!!! */
196 enum stone color = tree_node_color(tree, node);
197 struct move m = { .coord = node->coord, .color = color };
198 struct move_stats lm = { .playouts = -1, .value = 0 };
199 if (tree->board->libmap)
200 lm = libmap_board_move_stats(tree->board->libmap, tree->board, m);
202 /* We use 1 as parity, since for all nodes we want to know the
203 * win probability of _us_, not the node color. */
204 fprintf(stderr, "[%s] %.3f/%d [prior %.3f/%d amaf %.3f/%d crit %.3f vloss %d libmap %.3f/%d] h=%x c#=%d <%"PRIhash">\n",
205 coord2sstr(node_coord(node), tree->board),
206 tree_node_get_value(tree, treeparity, node->u.value), node->u.playouts,
207 tree_node_get_value(tree, treeparity, node->prior.value), node->prior.playouts,
208 tree_node_get_value(tree, treeparity, node->amaf.value), node->amaf.playouts,
209 tree_node_criticality(tree, node), node->descents,
210 tree_node_get_value(tree, 1, lm.value), lm.playouts,
211 node->hints, children, node->hash);
213 /* Print nodes sorted by #playouts. */
215 struct tree_node *nbox[1000]; int nboxl = 0;
216 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
217 if (ni->u.playouts > thres)
218 nbox[nboxl++] = ni;
220 while (true) {
221 int best = -1;
222 for (int i = 0; i < nboxl; i++)
223 if (nbox[i] && (best < 0 || nbox[i]->u.playouts > nbox[best]->u.playouts))
224 best = i;
225 if (best < 0)
226 break;
227 tree_node_dump(tree, nbox[best], treeparity, l + 1, /* node->u.value < 0.1 ? 0 : */ thres);
228 nbox[best] = NULL;
232 void
233 tree_dump(struct tree *tree, int thres)
235 if (thres && tree->root->u.playouts / thres > 100) {
236 /* Be a bit sensible about this; the opening tbook can create
237 * huge dumps at first. */
238 thres = tree->root->u.playouts / 100 * (thres < 1000 ? 1 : thres / 1000);
240 fprintf(stderr, "(UCT tree; root %s; extra komi %f; max depth %d)\n",
241 stone2str(tree->root_color), tree->extra_komi,
242 tree->max_depth - tree->root->depth);
243 tree_node_dump(tree, tree->root, 1, 0, thres);
245 if (DEBUGL(3) && tree->ltree_black) {
246 fprintf(stderr, "B local tree:\n");
247 tree_node_dump(tree, tree->ltree_black, tree->root_color == S_WHITE ? 1 : -1, 0, thres);
248 fprintf(stderr, "W local tree:\n");
249 tree_node_dump(tree, tree->ltree_white, tree->root_color == S_BLACK ? 1 : -1, 0, thres);
254 static char *
255 tree_book_name(struct board *b)
257 static char buf[256];
258 if (b->handicap > 0) {
259 sprintf(buf, "ucttbook-%d-%02.01f-h%d.pachitree", b->size - 2, b->komi, b->handicap);
260 } else {
261 sprintf(buf, "ucttbook-%d-%02.01f.pachitree", b->size - 2, b->komi);
263 return buf;
266 static void
267 tree_node_save(FILE *f, struct tree_node *node, int thres)
269 bool save_children = node->u.playouts >= thres;
271 if (!save_children)
272 node->is_expanded = 0;
274 fputc(1, f);
275 fwrite(((void *) node) + offsetof(struct tree_node, depth),
276 sizeof(struct tree_node) - offsetof(struct tree_node, depth),
277 1, f);
279 if (save_children) {
280 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
281 tree_node_save(f, ni, thres);
282 } else {
283 if (node->children)
284 node->is_expanded = 1;
287 fputc(0, f);
290 void
291 tree_save(struct tree *tree, struct board *b, int thres)
293 char *filename = tree_book_name(b);
294 FILE *f = fopen(filename, "wb");
295 if (!f) {
296 perror("fopen");
297 return;
299 tree_node_save(f, tree->root, thres);
300 fputc(0, f);
301 fclose(f);
305 void
306 tree_node_load(FILE *f, struct tree_node *node, int *num)
308 (*num)++;
310 fread(((void *) node) + offsetof(struct tree_node, depth),
311 sizeof(struct tree_node) - offsetof(struct tree_node, depth),
312 1, f);
314 /* Keep values in sane scale, otherwise we start overflowing. */
315 #define MAX_PLAYOUTS 10000000
316 if (node->u.playouts > MAX_PLAYOUTS) {
317 node->u.playouts = MAX_PLAYOUTS;
319 if (node->amaf.playouts > MAX_PLAYOUTS) {
320 node->amaf.playouts = MAX_PLAYOUTS;
322 memcpy(&node->pu, &node->u, sizeof(node->u));
324 struct tree_node *ni = NULL, *ni_prev = NULL;
325 while (fgetc(f)) {
326 ni_prev = ni; ni = calloc2(1, sizeof(*ni));
327 if (!node->children)
328 node->children = ni;
329 else
330 ni_prev->sibling = ni;
331 ni->parent = node;
332 tree_node_load(f, ni, num);
336 void
337 tree_load(struct tree *tree, struct board *b)
339 char *filename = tree_book_name(b);
340 FILE *f = fopen(filename, "rb");
341 if (!f)
342 return;
344 fprintf(stderr, "Loading opening tbook %s...\n", filename);
346 int num = 0;
347 if (fgetc(f))
348 tree_node_load(f, tree->root, &num);
349 fprintf(stderr, "Loaded %d nodes.\n", num);
351 fclose(f);
355 /* Copy the subtree rooted at node: all nodes at or below depth
356 * or with at least threshold playouts. Only for fast_alloc.
357 * The code is destructive on src. The relative order of children of
358 * a given node is preserved (assumed by tree_get_node in particular).
359 * Returns the copy of node in the destination tree, or NULL
360 * if we could not copy it. */
361 static struct tree_node *
362 tree_prune(struct tree *dest, struct tree *src, struct tree_node *node,
363 int threshold, int depth)
365 assert(dest->nodes && node);
366 struct tree_node *n2 = tree_alloc_node(dest, 1, true);
367 if (!n2)
368 return NULL;
369 *n2 = *node;
370 if (n2->depth > dest->max_depth)
371 dest->max_depth = n2->depth;
372 n2->children = NULL;
373 n2->is_expanded = false;
375 if (node->depth >= depth && node->u.playouts < threshold)
376 return n2;
377 /* For deep nodes with many playouts, we must copy all children,
378 * even those with zero playouts, because partially expanded
379 * nodes are not supported. Considering them as fully expanded
380 * would degrade the playing strength. The only exception is
381 * when dest becomes full, but this should never happen in practice
382 * if threshold is chosen to limit the number of nodes traversed. */
383 struct tree_node *ni = node->children;
384 if (!ni)
385 return n2;
386 struct tree_node **prev2 = &(n2->children);
387 while (ni) {
388 struct tree_node *ni2 = tree_prune(dest, src, ni, threshold, depth);
389 if (!ni2) break;
390 *prev2 = ni2;
391 prev2 = &(ni2->sibling);
392 ni2->parent = n2;
393 ni = ni->sibling;
395 if (!ni) {
396 n2->is_expanded = true;
397 } else {
398 n2->children = NULL; // avoid partially expanded nodes
400 return n2;
403 /* The following constants are used for garbage collection of nodes.
404 * A tree is considered large if the top node has >= 40K playouts.
405 * For such trees, we copy deep nodes only if they have enough
406 * playouts, with a gradually increasing threshold up to 40.
407 * These constants define how much time we're willing to spend
408 * scanning the source tree when promoting a move. The chosen values
409 * make worst case pruning in about 3s for 20 GB ram, and this
410 * is only for long thinking time (>1M playouts). For fast games the
411 * trees don't grow large. For small ram or fast game we copy the
412 * entire tree. These values do not degrade playing strength and are
413 * necessary to avoid losing on time; increasing DEEP_PLAYOUTS_THRESHOLD
414 * or decreasing LARGE_TREE_PLAYOUTS will make the program faster but
415 * playing worse. */
416 #define LARGE_TREE_PLAYOUTS 40000LL
417 #define DEEP_PLAYOUTS_THRESHOLD 40
419 /* Garbage collect the tree early if the top node has < 5K playouts,
420 * to avoid having to do it later on a large subtree.
421 * This guarantees garbage collection in < 1s. */
422 #define SMALL_TREE_PLAYOUTS 5000
424 /* Free all the tree, keeping only the subtree rooted at node.
425 * Prune the subtree if necessary to fit in memory or
426 * to save time scanning the tree.
427 * Returns the moved node. Only for fast_alloc. */
428 struct tree_node *
429 tree_garbage_collect(struct tree *tree, struct tree_node *node)
431 assert(tree->nodes && !node->parent && !node->sibling);
432 double start_time = time_now();
433 unsigned long orig_size = tree->nodes_size;
435 struct tree *temp_tree = tree_init(tree->board, tree->root_color,
436 tree->max_pruned_size, 0, 0, tree->ltree_aging, 0);
437 temp_tree->nodes_size = 0; // We do not want the dummy pass node
438 struct tree_node *temp_node;
440 /* Find the maximum depth at which we can copy all nodes. */
441 int max_nodes = 1;
442 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
443 max_nodes++;
444 unsigned long nodes_size = max_nodes * sizeof(*node);
445 int max_depth = node->depth;
446 while (nodes_size < tree->max_pruned_size && max_nodes > 1) {
447 max_nodes--;
448 nodes_size += max_nodes * nodes_size;
449 max_depth++;
452 /* Copy all nodes for small trees. For large trees, copy all nodes
453 * with depth <= max_depth, and all nodes with enough playouts.
454 * Avoiding going too deep (except for nodes with many playouts) is mostly
455 * to save time scanning the source tree. It can take over 20s to traverse
456 * completely a large source tree (20 GB) even without copying because
457 * the traversal is not friendly at all with the memory cache. */
458 int threshold = (node->u.playouts - LARGE_TREE_PLAYOUTS) * DEEP_PLAYOUTS_THRESHOLD / LARGE_TREE_PLAYOUTS;
459 if (threshold < 0) threshold = 0;
460 if (threshold > DEEP_PLAYOUTS_THRESHOLD) threshold = DEEP_PLAYOUTS_THRESHOLD;
461 temp_node = tree_prune(temp_tree, tree, node, threshold, max_depth);
462 assert(temp_node);
464 /* Now copy back to original tree. */
465 tree->nodes_size = 0;
466 tree->max_depth = 0;
467 struct tree_node *new_node = tree_prune(tree, temp_tree, temp_node, 0, temp_tree->max_depth);
469 if (DEBUGL(1)) {
470 double now = time_now();
471 static double prev_time;
472 if (!prev_time) prev_time = start_time;
473 fprintf(stderr,
474 "tree pruned in %0.6g s, prev %0.3g s ago, dest depth %d wanted %d,"
475 " size %lu->%lu/%lu, playouts %d\n",
476 now - start_time, start_time - prev_time, temp_tree->max_depth, max_depth,
477 orig_size, temp_tree->nodes_size, tree->max_pruned_size, new_node->u.playouts);
478 prev_time = start_time;
480 if (temp_tree->nodes_size >= temp_tree->max_tree_size) {
481 fprintf(stderr, "temp tree overflow, max_tree_size %lu, pruning_threshold %lu\n",
482 tree->max_tree_size, tree->pruning_threshold);
483 /* This is not a serious problem, we will simply recompute the discarded nodes
484 * at the next move if necessary. This is better than frequently wasting memory. */
485 } else {
486 assert(tree->nodes_size == temp_tree->nodes_size);
487 assert(tree->max_depth == temp_tree->max_depth);
489 tree_done(temp_tree);
490 return new_node;
494 /* Get a node of given coordinate from within parent, possibly creating it
495 * if necessary - in a very raw form (no .d, priors, ...). */
496 /* FIXME: Adjust for board symmetry. */
497 struct tree_node *
498 tree_get_node(struct tree *t, struct tree_node *parent, coord_t c, bool create)
500 if (!parent->children || node_coord(parent->children) >= c) {
501 /* Special case: Insertion at the beginning. */
502 if (parent->children && node_coord(parent->children) == c)
503 return parent->children;
504 if (!create)
505 return NULL;
507 struct tree_node *nn = tree_init_node(t, c, parent->depth + 1, false);
508 nn->parent = parent; nn->sibling = parent->children;
509 parent->children = nn;
510 return nn;
513 /* No candidate at the beginning, look through all the children. */
515 struct tree_node *ni;
516 for (ni = parent->children; ni->sibling; ni = ni->sibling)
517 if (node_coord(ni->sibling) >= c)
518 break;
520 if (ni->sibling && node_coord(ni->sibling) == c)
521 return ni->sibling;
522 assert(node_coord(ni) < c);
523 if (!create)
524 return NULL;
526 struct tree_node *nn = tree_init_node(t, c, parent->depth + 1, false);
527 nn->parent = parent; nn->sibling = ni->sibling; ni->sibling = nn;
528 return nn;
531 /* Get local tree node corresponding to given node, given local node child
532 * iterator @lni (which points either at the corresponding node, or at the
533 * nearest local tree node after @ni). */
534 struct tree_node *
535 tree_lnode_for_node(struct tree *tree, struct tree_node *ni, struct tree_node *lni, int tenuki_d)
537 /* Now set up lnode, which is the actual local node
538 * corresponding to ni - either lni if it is an
539 * exact match and ni is not tenuki, <pass> local
540 * node if ni is tenuki, or NULL if there is no
541 * corresponding node available. */
543 if (is_pass(node_coord(ni))) {
544 /* Also, for sanity reasons we never use local
545 * tree for passes. (Maybe we could, but it's
546 * too hard to think about.) */
547 return NULL;
550 if (node_coord(lni) == node_coord(ni)) {
551 /* We don't consider tenuki a sequence play
552 * that we have in local tree even though
553 * ni->d is too high; this can happen if this
554 * occured in different board topology. */
555 return lni;
558 if (ni->d >= tenuki_d) {
559 /* Tenuki, pick a pass lsibling if available. */
560 assert(lni->parent && lni->parent->children);
561 if (is_pass(node_coord(lni->parent->children))) {
562 return lni->parent->children;
563 } else {
564 return NULL;
568 /* No corresponding local node, lnode stays NULL. */
569 return NULL;
573 /* Tree symmetry: When possible, we will localize the tree to a single part
574 * of the board in tree_expand_node() and possibly flip along symmetry axes
575 * to another part of the board in tree_promote_at(). We follow b->symmetry
576 * guidelines here. */
579 /* This function must be thread safe, given that board b is only modified by the calling thread. */
580 void
581 tree_expand_node(struct tree *t, struct tree_node *node, struct board *b, enum stone color, struct uct *u, int parity)
583 /* Get a Common Fate Graph distance map from parent node. */
584 int distances[board_size2(b)];
585 if (!is_pass(b->last_move.coord) && !is_resign(b->last_move.coord)) {
586 cfg_distances(b, node_coord(node), distances, TREE_NODE_D_MAX);
587 } else {
588 // Pass or resign - everything is too far.
589 foreach_point(b) { distances[c] = TREE_NODE_D_MAX + 1; } foreach_point_end;
592 /* Get a map of prior values to initialize the new nodes with. */
593 struct prior_map map = {
594 .b = b,
595 .to_play = color,
596 .parity = tree_parity(t, parity),
597 .distances = distances,
599 // Include pass in the prior map.
600 struct move_stats map_prior[board_size2(b) + 1]; map.prior = &map_prior[1];
601 bool map_consider[board_size2(b) + 1]; map.consider = &map_consider[1];
602 memset(map_prior, 0, sizeof(map_prior));
603 memset(map_consider, 0, sizeof(map_consider));
604 map.consider[pass] = true;
605 int child_count = 1; // for pass
606 foreach_free_point(b) {
607 assert(board_at(b, c) == S_NONE);
608 if (!board_is_valid_play(b, color, c))
609 continue;
610 map.consider[c] = true;
611 child_count++;
612 } foreach_free_point_end;
613 uct_prior(u, node, &map);
615 /* Now, create the nodes (all at once if fast_alloc) */
616 struct tree_node *ni = t->nodes ? tree_alloc_node(t, child_count, true) : tree_alloc_node(t, 1, false);
617 /* In fast_alloc mode we might temporarily run out of nodes but this should be rare. */
618 if (!ni) {
619 node->is_expanded = false;
620 return;
622 tree_setup_node(t, ni, pass, node->depth + 1);
624 struct tree_node *first_child = ni;
625 ni->parent = node;
626 ni->prior = map.prior[pass]; ni->d = TREE_NODE_D_MAX + 1;
628 /* The loop considers only the symmetry playground. */
629 if (UDEBUGL(6)) {
630 fprintf(stderr, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
631 coord2sstr(node_coord(node), b),
632 b->symmetry.x1, b->symmetry.y1,
633 b->symmetry.x2, b->symmetry.y2,
634 b->symmetry.type, b->symmetry.d);
636 int child = 1;
637 for (int j = b->symmetry.y1; j <= b->symmetry.y2; j++) {
638 for (int i = b->symmetry.x1; i <= b->symmetry.x2; i++) {
639 if (b->symmetry.d) {
640 int x = b->symmetry.type == SYM_DIAG_DOWN ? board_size(b) - 1 - i : i;
641 if (x > j) {
642 if (UDEBUGL(7))
643 fprintf(stderr, "drop %d,%d\n", i, j);
644 continue;
648 coord_t c = coord_xy(t->board, i, j);
649 if (!map.consider[c]) // Filter out invalid moves
650 continue;
651 assert(c != node_coord(node)); // I have spotted "C3 C3" in some sequence...
653 struct tree_node *nj = t->nodes ? first_child + child++ : tree_alloc_node(t, 1, false);
654 tree_setup_node(t, nj, c, node->depth + 1);
655 nj->parent = node; ni->sibling = nj; ni = nj;
657 ni->prior = map.prior[c];
658 ni->d = distances[c];
661 node->children = first_child; // must be done at the end to avoid race
665 static coord_t
666 flip_coord(struct board *b, coord_t c,
667 bool flip_horiz, bool flip_vert, int flip_diag)
669 int x = coord_x(c, b), y = coord_y(c, b);
670 if (flip_diag) {
671 int z = x; x = y; y = z;
673 if (flip_horiz) {
674 x = board_size(b) - 1 - x;
676 if (flip_vert) {
677 y = board_size(b) - 1 - y;
679 return coord_xy(b, x, y);
682 static void
683 tree_fix_node_symmetry(struct board *b, struct tree_node *node,
684 bool flip_horiz, bool flip_vert, int flip_diag)
686 if (!is_pass(node_coord(node)))
687 node->coord = flip_coord(b, node_coord(node), flip_horiz, flip_vert, flip_diag);
689 for (struct tree_node *ni = node->children; ni; ni = ni->sibling)
690 tree_fix_node_symmetry(b, ni, flip_horiz, flip_vert, flip_diag);
693 static void
694 tree_fix_symmetry(struct tree *tree, struct board *b, coord_t c)
696 if (is_pass(c))
697 return;
699 struct board_symmetry *s = &tree->root_symmetry;
700 int cx = coord_x(c, b), cy = coord_y(c, b);
702 /* playground X->h->v->d normalization
703 * :::.. .d...
704 * .::.. v....
705 * ..:.. .....
706 * ..... h...X
707 * ..... ..... */
708 bool flip_horiz = cx < s->x1 || cx > s->x2;
709 bool flip_vert = cy < s->y1 || cy > s->y2;
711 bool flip_diag = 0;
712 if (s->d) {
713 bool dir = (s->type == SYM_DIAG_DOWN);
714 int x = dir ^ flip_horiz ^ flip_vert ? board_size(b) - 1 - cx : cx;
715 if (flip_vert ? x < cy : x > cy) {
716 flip_diag = 1;
720 if (DEBUGL(4)) {
721 fprintf(stderr, "%s [%d,%d -> %d,%d;%d,%d] will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
722 coord2sstr(c, b),
723 cx, cy, s->x1, s->y1, s->x2, s->y2,
724 flip_horiz, flip_vert, flip_diag,
725 coord2sstr(flip_coord(b, c, flip_horiz, flip_vert, flip_diag), b),
726 s->type, s->d, b->symmetry.type, b->symmetry.d);
728 if (flip_horiz || flip_vert || flip_diag)
729 tree_fix_node_symmetry(b, tree->root, flip_horiz, flip_vert, flip_diag);
733 static void
734 tree_unlink_node(struct tree_node *node)
736 struct tree_node *ni = node->parent;
737 if (ni->children == node) {
738 ni->children = node->sibling;
739 } else {
740 ni = ni->children;
741 while (ni->sibling != node)
742 ni = ni->sibling;
743 ni->sibling = node->sibling;
745 node->sibling = NULL;
746 node->parent = NULL;
749 /* Reduce weight of statistics on promotion. Remove nodes that
750 * get reduced to zero playouts; returns next node to consider
751 * in the children list (@node may get deleted). */
752 static struct tree_node *
753 tree_age_node(struct tree *tree, struct tree_node *node)
755 node->u.playouts /= tree->ltree_aging;
756 if (node->parent && !node->u.playouts) {
757 struct tree_node *sibling = node->sibling;
758 /* Delete node, no playouts. */
759 tree_unlink_node(node);
760 tree_done_node(tree, node);
761 return sibling;
764 struct tree_node *ni = node->children;
765 while (ni) ni = tree_age_node(tree, ni);
766 return node->sibling;
769 /* Promotes the given node as the root of the tree. In the fast_alloc
770 * mode, the node may be moved and some of its subtree may be pruned. */
771 void
772 tree_promote_node(struct tree *tree, struct tree_node **node)
774 assert((*node)->parent == tree->root);
775 tree_unlink_node(*node);
776 if (!tree->nodes) {
777 /* Freeing the rest of the tree can take several seconds on large
778 * trees, so we must do it asynchronously: */
779 tree_done_node_detached(tree, tree->root);
780 } else {
781 /* Garbage collect if we run out of memory, or it is cheap to do so now: */
782 if (tree->nodes_size >= tree->pruning_threshold
783 || (tree->nodes_size >= tree->max_tree_size / 10 && (*node)->u.playouts < SMALL_TREE_PLAYOUTS))
784 *node = tree_garbage_collect(tree, *node);
786 tree->root = *node;
787 tree->root_color = stone_other(tree->root_color);
789 board_symmetry_update(tree->board, &tree->root_symmetry, node_coord(*node));
790 tree->avg_score.playouts = 0;
792 /* If the tree deepest node was under node, or if we called tree_garbage_collect,
793 * tree->max_depth is correct. Otherwise we could traverse the tree
794 * to recompute max_depth but it's not worth it: it's just for debugging
795 * and soon the tree will grow and max_depth will become correct again. */
797 if (tree->ltree_aging != 1.0f) { // XXX: != should work here even with the floating_t
798 tree_age_node(tree, tree->ltree_black);
799 tree_age_node(tree, tree->ltree_white);
803 bool
804 tree_promote_at(struct tree *tree, struct board *b, coord_t c)
806 tree_fix_symmetry(tree, b, c);
808 for (struct tree_node *ni = tree->root->children; ni; ni = ni->sibling) {
809 if (node_coord(ni) == c) {
810 tree_promote_node(tree, &ni);
811 return true;
814 return false;