14 #include "uct/internal.h"
15 #include "uct/prior.h"
19 static struct tree_node
*
20 tree_init_node(struct tree
*t
, coord_t coord
, int depth
)
22 struct tree_node
*n
= calloc(1, sizeof(*n
));
25 static long c
= 1000000;
27 if (depth
> t
->max_depth
)
33 tree_init(struct board
*board
, enum stone color
)
35 struct tree
*t
= calloc(1, sizeof(*t
));
37 /* The root PASS move is only virtual, we never play it. */
38 t
->root
= tree_init_node(t
, pass
, 0);
39 t
->root_symmetry
= board
->symmetry
;
40 t
->root_color
= stone_other(color
); // to research black moves, root will be white
46 tree_done_node(struct tree
*t
, struct tree_node
*n
)
48 struct tree_node
*ni
= n
->children
;
50 struct tree_node
*nj
= ni
->sibling
;
51 tree_done_node(t
, ni
);
58 tree_done(struct tree
*t
)
60 tree_done_node(t
, t
->root
);
66 tree_node_dump(struct tree
*tree
, struct tree_node
*node
, int l
, int thres
)
68 for (int i
= 0; i
< l
; i
++) fputc(' ', stderr
);
70 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
72 /* We use 1 as parity, since for all nodes we want to know the
73 * win probability of _us_, not the node color. */
74 fprintf(stderr
, "[%s] %f (%d/%d playouts [prior %d/%d amaf %d/%d]; hints %x; %d children) <%lld>\n",
75 coord2sstr(node
->coord
, tree
->board
),
76 tree_node_get_value(tree
, node
, u
, 1),
77 tree_node_get_wins(tree
, node
, u
, 1), node
->u
.playouts
,
78 tree_node_get_wins(tree
, node
, prior
, 1), node
->prior
.playouts
,
79 tree_node_get_wins(tree
, node
, amaf
, 1), node
->amaf
.playouts
,
80 node
->hints
, children
, node
->hash
);
82 /* Print nodes sorted by #playouts. */
84 struct tree_node
*nbox
[1000]; int nboxl
= 0;
85 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
86 if (ni
->u
.playouts
> thres
)
91 for (int i
= 0; i
< nboxl
; i
++)
92 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
96 tree_node_dump(tree
, nbox
[best
], l
+ 1, /* node->u.value < 0.1 ? 0 : */ thres
);
102 tree_dump(struct tree
*tree
, int thres
)
104 if (thres
&& tree
->root
->u
.playouts
/ thres
> 100) {
105 /* Be a bit sensible about this; the opening book can create
106 * huge dumps at first. */
107 thres
= tree
->root
->u
.playouts
/ 100 * (thres
< 1000 ? 1 : thres
/ 1000);
109 tree_node_dump(tree
, tree
->root
, 0, thres
);
114 tree_book_name(struct board
*b
)
116 static char buf
[256];
117 if (b
->handicap
> 0) {
118 sprintf(buf
, "uctbook-%d-%02.01f-h%d.pachitree", b
->size
- 2, b
->komi
, b
->handicap
);
120 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
126 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
129 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
130 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
133 if (node
->u
.playouts
>= thres
)
134 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
135 tree_node_save(f
, ni
, thres
);
141 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
143 char *filename
= tree_book_name(b
);
144 FILE *f
= fopen(filename
, "wb");
149 tree_node_save(f
, tree
->root
, thres
);
156 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
)
160 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
161 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
164 /* Keep values in sane scale, otherwise we start overflowing.
165 * We may go slow here but we must be careful about not getting
166 * too huge integers.*/
167 #define MAX_PLAYOUTS 10000000
168 if (node
->u
.playouts
> MAX_PLAYOUTS
) {
169 int over
= node
->u
.playouts
- MAX_PLAYOUTS
;
170 node
->u
.wins
-= ((double) node
->u
.wins
/ node
->u
.playouts
) * over
;
171 node
->u
.playouts
= MAX_PLAYOUTS
;
173 if (node
->amaf
.playouts
> MAX_PLAYOUTS
) {
174 int over
= node
->amaf
.playouts
- MAX_PLAYOUTS
;
175 node
->amaf
.wins
-= ((double) node
->amaf
.wins
/ node
->amaf
.playouts
) * over
;
176 node
->amaf
.playouts
= MAX_PLAYOUTS
;
179 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
180 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
182 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
184 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
188 ni_prev
->sibling
= ni
;
190 tree_node_load(f
, ni
, num
);
195 tree_load(struct tree
*tree
, struct board
*b
)
197 char *filename
= tree_book_name(b
);
198 FILE *f
= fopen(filename
, "rb");
202 fprintf(stderr
, "Loading opening book %s...\n", filename
);
206 tree_node_load(f
, tree
->root
, &num
);
207 fprintf(stderr
, "Loaded %d nodes.\n", num
);
213 static struct tree_node
*
214 tree_node_copy(struct tree_node
*node
)
216 struct tree_node
*n2
= malloc(sizeof(*n2
));
220 struct tree_node
*ni
= node
->children
;
221 struct tree_node
*ni2
= tree_node_copy(ni
);
222 n2
->children
= ni2
; ni2
->parent
= n2
;
223 while ((ni
= ni
->sibling
)) {
224 ni2
->sibling
= tree_node_copy(ni
);
225 ni2
= ni2
->sibling
; ni2
->parent
= n2
;
231 tree_copy(struct tree
*tree
)
233 struct tree
*t2
= malloc(sizeof(*t2
));
235 t2
->root
= tree_node_copy(tree
->root
);
241 tree_node_merge(struct tree_node
*dest
, struct tree_node
*src
)
243 dest
->hints
|= src
->hints
;
245 /* Merge the children, both are coord-sorted lists. */
246 struct tree_node
*di
= dest
->children
, **dref
= &dest
->children
;
247 struct tree_node
*si
= src
->children
, **sref
= &src
->children
;
249 if (di
->coord
!= si
->coord
) {
250 /* src has some extra items or misses di */
251 struct tree_node
*si2
= si
->sibling
;
252 while (si2
&& di
->coord
!= si2
->coord
) {
256 goto next_di
; /* src misses di, move on */
257 /* chain the extra [si,si2) items before di */
259 while (si
->sibling
!= si2
) {
268 /* Matching nodes - recurse... */
269 tree_node_merge(di
, si
);
270 /* ...and move on. */
271 sref
= &si
->sibling
; si
= si
->sibling
;
273 dref
= &di
->sibling
; di
= di
->sibling
;
276 /* Some outstanding nodes are left on src side, rechain
286 /* Priors should be constant. */
287 assert(dest
->prior
.playouts
== src
->prior
.playouts
&& dest
->prior
.wins
== src
->prior
.wins
);
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
.value
+ src
->amaf
.value
) / 2;
294 dest
->u
.playouts
+= src
->u
.playouts
;
295 dest
->u
.wins
+= src
->u
.wins
;
296 if (dest
->prior
.playouts
+ dest
->amaf
.playouts
+ dest
->u
.playouts
)
297 dest
->u
.value
= (dest
->u
.value
+ src
->u
.value
) / 2;
300 /* Merge two trees built upon the same board. Note that the operation is
301 * destructive on src. */
303 tree_merge(struct tree
*dest
, struct tree
*src
)
305 if (src
->max_depth
> dest
->max_depth
)
306 dest
->max_depth
= src
->max_depth
;
307 tree_node_merge(dest
->root
, src
->root
);
312 tree_node_normalize(struct tree_node
*node
, int factor
)
314 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
315 tree_node_normalize(ni
, factor
);
317 #define normalize(s1, s2, t) node->s2.t = node->s1.t + (node->s2.t - node->s1.t) / factor;
318 normalize(pamaf
, amaf
, playouts
);
319 normalize(pamaf
, amaf
, wins
);
320 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
322 normalize(pu
, u
, playouts
);
323 normalize(pu
, u
, wins
);
324 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
328 /* Normalize a tree, dividing the amaf and u values by given
329 * factor; otherwise, simulations run in independent threads
330 * two trees built upon the same board. To correctly handle
331 * results taken from previous simulation run, they are backed
334 tree_normalize(struct tree
*tree
, int factor
)
336 tree_node_normalize(tree
->root
, factor
);
340 /* Tree symmetry: When possible, we will localize the tree to a single part
341 * of the board in tree_expand_node() and possibly flip along symmetry axes
342 * to another part of the board in tree_promote_at(). We follow b->symmetry
343 * guidelines here. */
347 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct
*u
, int parity
)
349 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
350 ni
->parent
= node
; node
->children
= ni
;
351 uct_prior(u
, t
, ni
, b
, color
, parity
);
353 /* The loop considers only the symmetry playground. */
355 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
356 coord2sstr(node
->coord
, b
),
357 b
->symmetry
.x1
, b
->symmetry
.y1
,
358 b
->symmetry
.x2
, b
->symmetry
.y2
,
359 b
->symmetry
.type
, b
->symmetry
.d
);
361 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
362 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
364 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
367 fprintf(stderr
, "drop %d,%d\n", i
, j
);
372 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
373 if (board_at(b
, c
) != S_NONE
)
375 assert(c
!= node
->coord
); // I have spotted "C3 C3" in some sequence...
376 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
377 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
380 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
381 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
383 uct_prior(u
, t
, ni
, b
, color
, parity
);
390 flip_coord(struct board
*b
, coord_t c
,
391 bool flip_horiz
, bool flip_vert
, int flip_diag
)
393 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
395 int z
= x
; x
= y
; y
= z
;
398 x
= board_size(b
) - 1 - x
;
401 y
= board_size(b
) - 1 - y
;
403 return coord_xy_otf(x
, y
, b
);
407 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
408 bool flip_horiz
, bool flip_vert
, int flip_diag
)
410 if (!is_pass(node
->coord
))
411 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
413 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
414 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
418 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
423 struct board_symmetry
*s
= &tree
->root_symmetry
;
424 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
426 /* playground X->h->v->d normalization
432 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
433 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
437 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
438 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
439 if (flip_vert
? x
< cy
: x
> cy
) {
445 fprintf(stderr
, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
446 coord2sstr(c
, b
), flip_horiz
, flip_vert
, flip_diag
,
447 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
448 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
450 if (flip_horiz
|| flip_vert
|| flip_diag
)
451 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
456 tree_unlink_node(struct tree_node
*node
)
458 struct tree_node
*ni
= node
->parent
;
459 if (ni
->children
== node
) {
460 ni
->children
= node
->sibling
;
463 while (ni
->sibling
!= node
)
465 ni
->sibling
= node
->sibling
;
467 node
->sibling
= NULL
;
472 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
474 tree_unlink_node(node
);
475 tree_done_node(tree
, node
);
479 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
481 assert(node
->parent
== tree
->root
);
482 tree_unlink_node(node
);
483 tree_done_node(tree
, tree
->root
);
485 tree
->root_color
= stone_other(tree
->root_color
);
486 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
490 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
492 tree_fix_symmetry(tree
, b
, c
);
494 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
495 if (ni
->coord
== c
) {
496 tree_promote_node(tree
, ni
);