16 #include "uct/internal.h"
17 #include "uct/prior.h"
21 /* This function may be called by multiple threads in parallel */
22 static struct tree_node
*
23 tree_init_node(struct tree
*t
, coord_t coord
, int depth
)
25 struct tree_node
*n
= calloc(1, sizeof(*n
));
27 fprintf(stderr
, "tree_init_node(): OUT OF MEMORY\n");
30 __sync_fetch_and_add(&t
->nodes_size
, sizeof(*n
));
33 volatile static long c
= 1000000;
34 n
->hash
= __sync_fetch_and_add(&c
, 1);
35 if (depth
> t
->max_depth
)
41 tree_init(struct board
*board
, enum stone color
)
43 struct tree
*t
= calloc(1, sizeof(*t
));
45 /* The root PASS move is only virtual, we never play it. */
46 t
->root
= tree_init_node(t
, pass
, 0);
47 t
->root_symmetry
= board
->symmetry
;
48 t
->root_color
= stone_other(color
); // to research black moves, root will be white
54 tree_done_node(struct tree
*t
, struct tree_node
*n
)
56 struct tree_node
*ni
= n
->children
;
58 struct tree_node
*nj
= ni
->sibling
;
59 tree_done_node(t
, ni
);
62 t
->nodes_size
-= sizeof(*n
); // atomic operation not needed here
67 tree_done(struct tree
*t
)
69 tree_done_node(t
, t
->root
);
70 if (t
->chchvals
) free(t
->chchvals
);
71 if (t
->chvals
) free(t
->chvals
);
77 tree_node_dump(struct tree
*tree
, struct tree_node
*node
, int l
, int thres
)
79 for (int i
= 0; i
< l
; i
++) fputc(' ', stderr
);
81 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
83 /* We use 1 as parity, since for all nodes we want to know the
84 * win probability of _us_, not the node color. */
85 fprintf(stderr
, "[%s] %f %% %d [prior %f %% %d amaf %f %% %d]; hints %x; %d children <%"PRIhash
">\n",
86 coord2sstr(node
->coord
, tree
->board
),
87 tree_node_get_value(tree
, 1, node
->u
.value
), node
->u
.playouts
,
88 tree_node_get_value(tree
, 1, node
->prior
.value
), node
->prior
.playouts
,
89 tree_node_get_value(tree
, 1, node
->amaf
.value
), node
->amaf
.playouts
,
90 node
->hints
, children
, node
->hash
);
92 /* Print nodes sorted by #playouts. */
94 struct tree_node
*nbox
[1000]; int nboxl
= 0;
95 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
96 if (ni
->u
.playouts
> thres
)
101 for (int i
= 0; i
< nboxl
; i
++)
102 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
106 tree_node_dump(tree
, nbox
[best
], l
+ 1, /* node->u.value < 0.1 ? 0 : */ thres
);
112 tree_dump_chval(struct tree
*tree
, struct move_stats
*v
)
114 for (int y
= board_size(tree
->board
) - 2; y
> 1; y
--) {
115 for (int x
= 1; x
< board_size(tree
->board
) - 1; x
++) {
116 coord_t c
= coord_xy(tree
->board
, x
, y
);
117 fprintf(stderr
, "%.2f%%%05d ", v
[c
].value
, v
[c
].playouts
);
119 fprintf(stderr
, "\n");
124 tree_dump(struct tree
*tree
, int thres
)
126 if (thres
&& tree
->root
->u
.playouts
/ thres
> 100) {
127 /* Be a bit sensible about this; the opening book can create
128 * huge dumps at first. */
129 thres
= tree
->root
->u
.playouts
/ 100 * (thres
< 1000 ? 1 : thres
/ 1000);
131 fprintf(stderr
, "(UCT tree; root %s; extra komi %f)\n",
132 stone2str(tree
->root_color
), tree
->extra_komi
);
133 tree_node_dump(tree
, tree
->root
, 0, thres
);
135 if (DEBUGL(3) && tree
->chvals
) {
136 fprintf(stderr
, "children stats:\n");
137 tree_dump_chval(tree
, tree
->chvals
);
138 fprintf(stderr
, "grandchildren stats:\n");
139 tree_dump_chval(tree
, tree
->chchvals
);
145 tree_book_name(struct board
*b
)
147 static char buf
[256];
148 if (b
->handicap
> 0) {
149 sprintf(buf
, "uctbook-%d-%02.01f-h%d.pachitree", b
->size
- 2, b
->komi
, b
->handicap
);
151 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
157 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
160 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
161 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
164 if (node
->u
.playouts
>= thres
)
165 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
166 tree_node_save(f
, ni
, thres
);
172 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
174 char *filename
= tree_book_name(b
);
175 FILE *f
= fopen(filename
, "wb");
180 tree_node_save(f
, tree
->root
, thres
);
187 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
)
191 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
192 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
195 /* Keep values in sane scale, otherwise we start overflowing. */
196 #define MAX_PLAYOUTS 10000000
197 if (node
->u
.playouts
> MAX_PLAYOUTS
) {
198 node
->u
.playouts
= MAX_PLAYOUTS
;
200 if (node
->amaf
.playouts
> MAX_PLAYOUTS
) {
201 node
->amaf
.playouts
= MAX_PLAYOUTS
;
204 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
205 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
207 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
209 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
213 ni_prev
->sibling
= ni
;
215 tree_node_load(f
, ni
, num
);
220 tree_load(struct tree
*tree
, struct board
*b
)
222 char *filename
= tree_book_name(b
);
223 FILE *f
= fopen(filename
, "rb");
227 fprintf(stderr
, "Loading opening book %s...\n", filename
);
231 tree_node_load(f
, tree
->root
, &num
);
232 fprintf(stderr
, "Loaded %d nodes.\n", num
);
238 static struct tree_node
*
239 tree_node_copy(struct tree_node
*node
)
241 struct tree_node
*n2
= malloc(sizeof(*n2
));
245 struct tree_node
*ni
= node
->children
;
246 struct tree_node
*ni2
= tree_node_copy(ni
);
247 n2
->children
= ni2
; ni2
->parent
= n2
;
248 while ((ni
= ni
->sibling
)) {
249 ni2
->sibling
= tree_node_copy(ni
);
250 ni2
= ni2
->sibling
; ni2
->parent
= n2
;
256 tree_copy(struct tree
*tree
)
258 struct tree
*t2
= malloc(sizeof(*t2
));
260 t2
->root
= tree_node_copy(tree
->root
);
266 tree_node_merge(struct tree_node
*dest
, struct tree_node
*src
)
268 /* Do not merge nodes that weren't touched at all. */
269 assert(dest
->pamaf
.playouts
== src
->pamaf
.playouts
);
270 assert(dest
->pu
.playouts
== src
->pu
.playouts
);
271 if (src
->amaf
.playouts
- src
->pamaf
.playouts
== 0
272 && src
->u
.playouts
- src
->pu
.playouts
== 0) {
276 dest
->hints
|= src
->hints
;
278 /* Merge the children, both are coord-sorted lists. */
279 struct tree_node
*di
= dest
->children
, **dref
= &dest
->children
;
280 struct tree_node
*si
= src
->children
, **sref
= &src
->children
;
282 if (di
->coord
!= si
->coord
) {
283 /* src has some extra items or misses di */
284 struct tree_node
*si2
= si
->sibling
;
285 while (si2
&& di
->coord
!= si2
->coord
) {
289 goto next_di
; /* src misses di, move on */
290 /* chain the extra [si,si2) items before di */
292 while (si
->sibling
!= si2
) {
301 /* Matching nodes - recurse... */
302 tree_node_merge(di
, si
);
303 /* ...and move on. */
304 sref
= &si
->sibling
; si
= si
->sibling
;
306 dref
= &di
->sibling
; di
= di
->sibling
;
309 /* Some outstanding nodes are left on src side, rechain
319 /* Priors should be constant. */
320 assert(dest
->prior
.playouts
== src
->prior
.playouts
&& dest
->prior
.value
== src
->prior
.value
);
322 stats_merge(&dest
->amaf
, &src
->amaf
);
323 stats_merge(&dest
->u
, &src
->u
);
326 /* Merge two trees built upon the same board. Note that the operation is
327 * destructive on src. */
329 tree_merge(struct tree
*dest
, struct tree
*src
)
331 if (src
->max_depth
> dest
->max_depth
)
332 dest
->max_depth
= src
->max_depth
;
333 tree_node_merge(dest
->root
, src
->root
);
338 tree_node_normalize(struct tree_node
*node
, int factor
)
340 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
341 tree_node_normalize(ni
, factor
);
343 #define normalize(s1, s2, t) node->s2.t = node->s1.t + (node->s2.t - node->s1.t) / factor;
344 normalize(pamaf
, amaf
, playouts
);
345 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
347 normalize(pu
, u
, playouts
);
348 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
352 /* Normalize a tree, dividing the amaf and u values by given
353 * factor; otherwise, simulations run in independent threads
354 * two trees built upon the same board. To correctly handle
355 * results taken from previous simulation run, they are backed
358 tree_normalize(struct tree
*tree
, int factor
)
360 tree_node_normalize(tree
->root
, factor
);
364 /* Get a node of given coordinate from within parent, possibly creating it
365 * if necessary - in a very raw form (no .d, priors, ...). */
366 /* FIXME: Adjust for board symmetry. */
368 tree_get_node(struct tree
*t
, struct tree_node
*parent
, coord_t c
, bool create
)
370 if (!parent
->children
|| parent
->children
->coord
>= c
) {
371 /* Special case: Insertion at the beginning. */
372 if (parent
->children
&& parent
->children
->coord
== c
)
373 return parent
->children
;
377 struct tree_node
*nn
= tree_init_node(t
, c
, parent
->depth
+ 1);
378 nn
->parent
= parent
; nn
->sibling
= parent
->children
;
379 parent
->children
= nn
;
383 /* No candidate at the beginning, look through all the children. */
385 struct tree_node
*ni
;
386 for (ni
= parent
->children
; ni
->sibling
; ni
= ni
->sibling
)
387 if (ni
->sibling
->coord
>= c
)
390 if (ni
->sibling
&& ni
->sibling
->coord
== c
)
392 assert(ni
->coord
< c
);
396 struct tree_node
*nn
= tree_init_node(t
, c
, parent
->depth
+ 1);
397 nn
->parent
= parent
; nn
->sibling
= ni
->sibling
; ni
->sibling
= nn
;
402 /* Tree symmetry: When possible, we will localize the tree to a single part
403 * of the board in tree_expand_node() and possibly flip along symmetry axes
404 * to another part of the board in tree_promote_at(). We follow b->symmetry
405 * guidelines here. */
409 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, struct uct
*u
, int parity
)
411 /* Get a Common Fate Graph distance map from parent node. */
412 int distances
[board_size2(b
)];
413 if (!is_pass(b
->last_move
.coord
) && !is_resign(b
->last_move
.coord
)) {
414 cfg_distances(b
, node
->coord
, distances
, TREE_NODE_D_MAX
);
416 // Pass or resign - everything is too far.
417 foreach_point(b
) { distances
[c
] = TREE_NODE_D_MAX
+ 1; } foreach_point_end
;
420 /* Get a map of prior values to initialize the new nodes with. */
421 struct prior_map map
= {
424 .parity
= tree_parity(t
, parity
),
425 .distances
= distances
,
427 // Include pass in the prior map.
428 struct move_stats map_prior
[board_size2(b
) + 1]; map
.prior
= &map_prior
[1];
429 bool map_consider
[board_size2(b
) + 1]; map
.consider
= &map_consider
[1];
430 memset(map_prior
, 0, sizeof(map_prior
));
431 memset(map_consider
, 0, sizeof(map_consider
));
432 struct move pm
= { .color
= color
};
433 map
.consider
[pass
] = true;
435 if (board_at(b
, c
) != S_NONE
)
438 if (!board_is_valid_move(b
, &pm
))
440 map
.consider
[c
] = true;
442 uct_prior(u
, node
, &map
);
444 /* Now, create the nodes. */
445 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
446 struct tree_node
*first_child
= ni
;
448 ni
->prior
= map
.prior
[pass
]; ni
->d
= TREE_NODE_D_MAX
+ 1;
450 /* The loop considers only the symmetry playground. */
452 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
453 coord2sstr(node
->coord
, b
),
454 b
->symmetry
.x1
, b
->symmetry
.y1
,
455 b
->symmetry
.x2
, b
->symmetry
.y2
,
456 b
->symmetry
.type
, b
->symmetry
.d
);
458 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
459 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
461 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
464 fprintf(stderr
, "drop %d,%d\n", i
, j
);
469 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
470 if (!map
.consider
[c
]) // Filter out invalid moves
472 assert(c
!= node
->coord
); // I have spotted "C3 C3" in some sequence...
474 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
475 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
477 ni
->prior
= map
.prior
[c
];
478 ni
->d
= distances
[c
];
481 node
->children
= first_child
; // must be done at the end to avoid race
486 flip_coord(struct board
*b
, coord_t c
,
487 bool flip_horiz
, bool flip_vert
, int flip_diag
)
489 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
491 int z
= x
; x
= y
; y
= z
;
494 x
= board_size(b
) - 1 - x
;
497 y
= board_size(b
) - 1 - y
;
499 return coord_xy_otf(x
, y
, b
);
503 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
504 bool flip_horiz
, bool flip_vert
, int flip_diag
)
506 if (!is_pass(node
->coord
))
507 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
509 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
510 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
514 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
519 struct board_symmetry
*s
= &tree
->root_symmetry
;
520 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
522 /* playground X->h->v->d normalization
528 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
529 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
533 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
534 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
535 if (flip_vert
? x
< cy
: x
> cy
) {
541 fprintf(stderr
, "%s [%d,%d -> %d,%d;%d,%d] will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
543 cx
, cy
, s
->x1
, s
->y1
, s
->x2
, s
->y2
,
544 flip_horiz
, flip_vert
, flip_diag
,
545 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
546 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
548 if (flip_horiz
|| flip_vert
|| flip_diag
)
549 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
554 tree_unlink_node(struct tree_node
*node
)
556 struct tree_node
*ni
= node
->parent
;
557 if (ni
->children
== node
) {
558 ni
->children
= node
->sibling
;
561 while (ni
->sibling
!= node
)
563 ni
->sibling
= node
->sibling
;
565 node
->sibling
= NULL
;
570 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
572 tree_unlink_node(node
);
573 tree_done_node(tree
, node
);
577 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
579 assert(node
->parent
== tree
->root
);
580 tree_unlink_node(node
);
581 tree_done_node(tree
, tree
->root
);
583 tree
->root_color
= stone_other(tree
->root_color
);
584 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
586 if (tree
->chchvals
) { free(tree
->chchvals
); tree
->chchvals
= NULL
; }
587 if (tree
->chvals
) { free(tree
->chvals
); tree
->chvals
= NULL
; }
591 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
593 tree_fix_symmetry(tree
, b
, c
);
595 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
596 if (ni
->coord
== c
) {
597 tree_promote_node(tree
, ni
);