14 #include "uct/internal.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
));
24 if (depth
> t
->max_depth
)
30 tree_init(struct board
*board
, enum stone color
)
32 struct tree
*t
= calloc(1, sizeof(*t
));
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
;
42 tree_done_node(struct tree
*t
, struct tree_node
*n
)
44 struct tree_node
*ni
= n
->children
;
46 struct tree_node
*nj
= ni
->sibling
;
47 tree_done_node(t
, ni
);
54 tree_done(struct tree
*t
)
56 tree_done_node(t
, t
->root
);
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
);
66 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
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
)
79 for (int i
= 0; i
< nboxl
; i
++)
80 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
84 tree_node_dump(tree
, nbox
[best
], l
+ 1, node
->u
.value
< 0.1 ? 0 : thres
);
90 tree_dump(struct tree
*tree
, int thres
)
92 tree_node_dump(tree
, tree
->root
, 0, thres
);
97 tree_book_name(struct board
*b
)
100 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
105 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
108 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
109 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
112 if (node
->u
.playouts
>= thres
)
113 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
114 tree_node_save(f
, ni
, thres
);
120 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
122 char *filename
= tree_book_name(b
);
123 FILE *f
= fopen(filename
, "wb");
128 tree_node_save(f
, tree
->root
, thres
);
135 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
, bool invert
)
139 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
140 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
143 /* Keep values in sane scale, otherwise we start overflowing.
144 * We may go slow here but we must be careful about not getting
145 * too huge integers.*/
146 #define MAX_PLAYOUTS 10000000
147 if (node
->u
.playouts
> MAX_PLAYOUTS
) {
148 int over
= node
->u
.playouts
- MAX_PLAYOUTS
;
149 node
->u
.wins
-= ((double) node
->u
.wins
/ node
->u
.playouts
) * over
;
150 node
->u
.playouts
= MAX_PLAYOUTS
;
152 if (node
->amaf
.playouts
> MAX_PLAYOUTS
) {
153 int over
= node
->amaf
.playouts
- MAX_PLAYOUTS
;
154 node
->amaf
.wins
-= ((double) node
->amaf
.wins
/ node
->amaf
.playouts
) * over
;
155 node
->amaf
.playouts
= MAX_PLAYOUTS
;
159 node
->u
.wins
= node
->u
.playouts
- node
->u
.wins
;
160 node
->u
.value
= 1 - node
->u
.value
;
161 node
->amaf
.wins
= node
->amaf
.playouts
- node
->amaf
.wins
;
162 node
->amaf
.value
= 1 - node
->amaf
.value
;
163 node
->prior
.wins
= node
->prior
.playouts
- node
->prior
.wins
;
164 node
->prior
.value
= 1 - node
->prior
.value
;
167 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
169 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
173 ni_prev
->sibling
= ni
;
175 tree_node_load(f
, ni
, num
, invert
);
180 tree_load(struct tree
*tree
, struct board
*b
, enum stone color
)
182 char *filename
= tree_book_name(b
);
183 FILE *f
= fopen(filename
, "rb");
187 fprintf(stderr
, "Loading opening book %s...\n", filename
);
191 tree_node_load(f
, tree
->root
, &num
, color
!= S_BLACK
);
192 fprintf(stderr
, "Loaded %d nodes.\n", num
);
198 static struct tree_node
*
199 tree_node_copy(struct tree_node
*node
)
201 struct tree_node
*n2
= malloc(sizeof(*n2
));
205 struct tree_node
*ni
= node
->children
;
206 struct tree_node
*ni2
= tree_node_copy(ni
);
207 n2
->children
= ni2
; ni2
->parent
= n2
;
208 while ((ni
= ni
->sibling
)) {
209 ni2
->sibling
= tree_node_copy(ni
);
210 ni2
= ni2
->sibling
; ni2
->parent
= n2
;
216 tree_copy(struct tree
*tree
)
218 struct tree
*t2
= malloc(sizeof(*t2
));
220 t2
->root
= tree_node_copy(tree
->root
);
226 tree_node_merge(struct tree_node
*dest
, struct tree_node
*src
)
228 dest
->hints
|= src
->hints
;
230 /* Merge the children, both are coord-sorted lists. */
231 struct tree_node
*di
= dest
->children
, *dip
= NULL
;
232 struct tree_node
*si
= src
->children
, *sip
= NULL
;
234 if (di
->coord
!= si
->coord
) {
235 /* src has some extra items or misses di */
236 struct tree_node
*si2
= si
->sibling
;
237 while (si2
&& di
->coord
!= si2
->coord
) {
241 goto next_di
; /* src misses di, move on */
242 /* chain the extra [si,si2) items before di */
247 while (si
->sibling
!= si2
) {
258 /* Matching nodes - recurse... */
259 tree_node_merge(di
, si
);
260 /* ...and move on. */
261 sip
= si
; si
= si
->sibling
;
263 dip
= di
; di
= di
->sibling
;
277 src
->children
= NULL
;
280 dest
->prior
.playouts
+= src
->prior
.playouts
;
281 dest
->prior
.wins
+= src
->prior
.wins
;
282 if (dest
->prior
.playouts
)
283 dest
->prior
.value
= dest
->prior
.wins
/ dest
->prior
.playouts
;
284 dest
->amaf
.playouts
+= src
->amaf
.playouts
;
285 dest
->amaf
.wins
+= src
->amaf
.wins
;
286 if (dest
->amaf
.playouts
)
287 dest
->amaf
.value
= dest
->amaf
.wins
/ dest
->amaf
.playouts
;
288 dest
->u
.playouts
+= src
->u
.playouts
;
289 dest
->u
.wins
+= src
->u
.wins
;
290 if (dest
->prior
.playouts
+ dest
->amaf
.playouts
+ dest
->u
.playouts
)
291 tree_update_node_value(dest
);
294 /* Merge two trees built upon the same board. Note that the operation is
295 * destructive on src. */
297 tree_merge(struct tree
*dest
, struct tree
*src
)
299 if (src
->max_depth
> dest
->max_depth
)
300 dest
->max_depth
= src
->max_depth
;
301 tree_node_merge(dest
->root
, src
->root
);
305 /* Tree symmetry: When possible, we will localize the tree to a single part
306 * of the board in tree_expand_node() and possibly flip along symmetry axes
307 * to another part of the board in tree_promote_at(). We follow b->symmetry
308 * guidelines here. */
312 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct_policy
*policy
, int parity
)
314 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
315 ni
->parent
= node
; node
->children
= ni
;
317 /* The loop considers only the symmetry playground. */
319 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
320 coord2sstr(node
->coord
, b
),
321 b
->symmetry
.x1
, b
->symmetry
.y1
,
322 b
->symmetry
.x2
, b
->symmetry
.y2
,
323 b
->symmetry
.type
, b
->symmetry
.d
);
325 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
326 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
328 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
331 fprintf(stderr
, "drop %d,%d\n", i
, j
);
336 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
337 if (board_at(b
, c
) != S_NONE
)
339 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
340 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
343 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
344 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
347 policy
->prior(policy
, t
, ni
, b
, color
, parity
);
354 flip_coord(struct board
*b
, coord_t c
,
355 bool flip_horiz
, bool flip_vert
, int flip_diag
)
357 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
359 int z
= x
; x
= y
; y
= z
;
362 x
= board_size(b
) - 1 - x
;
365 y
= board_size(b
) - 1 - y
;
367 return coord_xy_otf(x
, y
, b
);
371 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
372 bool flip_horiz
, bool flip_vert
, int flip_diag
)
374 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
376 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
377 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
381 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
383 struct board_symmetry
*s
= &tree
->root_symmetry
;
384 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
386 /* playground X->h->v->d normalization
392 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
393 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
397 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
398 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
399 if (flip_vert
? x
< cy
: x
> cy
) {
405 fprintf(stderr
, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
406 coord2sstr(c
, b
), flip_horiz
, flip_vert
, flip_diag
,
407 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
408 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
410 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
415 tree_unlink_node(struct tree_node
*node
)
417 struct tree_node
*ni
= node
->parent
;
418 if (ni
->children
== node
) {
419 ni
->children
= node
->sibling
;
422 while (ni
->sibling
!= node
)
424 ni
->sibling
= node
->sibling
;
429 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
431 tree_unlink_node(node
);
432 tree_done_node(tree
, node
);
436 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
438 assert(node
->parent
== tree
->root
);
439 tree_unlink_node(node
);
440 tree_done_node(tree
, tree
->root
);
442 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
447 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
449 tree_fix_symmetry(tree
, b
, c
);
451 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
452 if (ni
->coord
== c
) {
453 tree_promote_node(tree
, ni
);
461 tree_leaf_node(struct tree_node
*node
)
463 return !(node
->children
);
467 tree_update_node_value(struct tree_node
*node
)
469 bool noamaf
= node
->hints
& NODE_HINT_NOAMAF
;
470 node
->u
.value
= (float)(node
->u
.wins
+ node
->prior
.wins
+ (!noamaf
? node
->amaf
.wins
: 0))
471 / (node
->u
.playouts
+ node
->prior
.playouts
+ (!noamaf
? node
->amaf
.playouts
: 0));
473 { struct board b2
; board_size(&b2
) = 9+2;
474 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
); }