15 #include "uct/internal.h"
16 #include "uct/prior.h"
20 static struct tree_node
*
21 tree_init_node(struct tree
*t
, coord_t coord
, int depth
)
23 struct tree_node
*n
= calloc(1, sizeof(*n
));
25 fprintf(stderr
, "tree_init_node(): OUT OF MEMORY\n");
30 static long c
= 1000000;
32 if (depth
> t
->max_depth
)
38 tree_init(struct board
*board
, enum stone color
)
40 struct tree
*t
= calloc(1, sizeof(*t
));
42 /* The root PASS move is only virtual, we never play it. */
43 t
->root
= tree_init_node(t
, pass
, 0);
44 t
->root_symmetry
= board
->symmetry
;
45 t
->root_color
= stone_other(color
); // to research black moves, root will be white
51 tree_done_node(struct tree
*t
, struct tree_node
*n
)
53 struct tree_node
*ni
= n
->children
;
55 struct tree_node
*nj
= ni
->sibling
;
56 tree_done_node(t
, ni
);
63 tree_done(struct tree
*t
)
65 tree_done_node(t
, t
->root
);
71 tree_node_dump(struct tree
*tree
, struct tree_node
*node
, int l
, int thres
)
73 for (int i
= 0; i
< l
; i
++) fputc(' ', stderr
);
75 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
77 /* We use 1 as parity, since for all nodes we want to know the
78 * win probability of _us_, not the node color. */
79 fprintf(stderr
, "[%s] %f %% %d [prior %f %% %d amaf %f %% %d]; hints %x; %d children <%lld>\n",
80 coord2sstr(node
->coord
, tree
->board
),
81 tree_node_get_value(tree
, node
, u
, 1), node
->u
.playouts
,
82 tree_node_get_value(tree
, node
, prior
, 1), node
->prior
.playouts
,
83 tree_node_get_value(tree
, node
, amaf
, 1), node
->amaf
.playouts
,
84 node
->hints
, children
, node
->hash
);
86 /* Print nodes sorted by #playouts. */
88 struct tree_node
*nbox
[1000]; int nboxl
= 0;
89 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
90 if (ni
->u
.playouts
> thres
)
95 for (int i
= 0; i
< nboxl
; i
++)
96 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
100 tree_node_dump(tree
, nbox
[best
], l
+ 1, /* node->u.value < 0.1 ? 0 : */ thres
);
106 tree_dump(struct tree
*tree
, int thres
)
108 if (thres
&& tree
->root
->u
.playouts
/ thres
> 100) {
109 /* Be a bit sensible about this; the opening book can create
110 * huge dumps at first. */
111 thres
= tree
->root
->u
.playouts
/ 100 * (thres
< 1000 ? 1 : thres
/ 1000);
113 tree_node_dump(tree
, tree
->root
, 0, thres
);
118 tree_book_name(struct board
*b
)
120 static char buf
[256];
121 if (b
->handicap
> 0) {
122 sprintf(buf
, "uctbook-%d-%02.01f-h%d.pachitree", b
->size
- 2, b
->komi
, b
->handicap
);
124 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
130 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
133 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
134 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
137 if (node
->u
.playouts
>= thres
)
138 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
139 tree_node_save(f
, ni
, thres
);
145 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
147 char *filename
= tree_book_name(b
);
148 FILE *f
= fopen(filename
, "wb");
153 tree_node_save(f
, tree
->root
, thres
);
160 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
)
164 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
165 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
168 /* Keep values in sane scale, otherwise we start overflowing. */
169 #define MAX_PLAYOUTS 10000000
170 if (node
->u
.playouts
> MAX_PLAYOUTS
) {
171 node
->u
.playouts
= MAX_PLAYOUTS
;
173 if (node
->amaf
.playouts
> MAX_PLAYOUTS
) {
174 node
->amaf
.playouts
= MAX_PLAYOUTS
;
177 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
178 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
180 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
182 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
186 ni_prev
->sibling
= ni
;
188 tree_node_load(f
, ni
, num
);
193 tree_load(struct tree
*tree
, struct board
*b
)
195 char *filename
= tree_book_name(b
);
196 FILE *f
= fopen(filename
, "rb");
200 fprintf(stderr
, "Loading opening book %s...\n", filename
);
204 tree_node_load(f
, tree
->root
, &num
);
205 fprintf(stderr
, "Loaded %d nodes.\n", num
);
211 static struct tree_node
*
212 tree_node_copy(struct tree_node
*node
)
214 struct tree_node
*n2
= malloc(sizeof(*n2
));
218 struct tree_node
*ni
= node
->children
;
219 struct tree_node
*ni2
= tree_node_copy(ni
);
220 n2
->children
= ni2
; ni2
->parent
= n2
;
221 while ((ni
= ni
->sibling
)) {
222 ni2
->sibling
= tree_node_copy(ni
);
223 ni2
= ni2
->sibling
; ni2
->parent
= n2
;
229 tree_copy(struct tree
*tree
)
231 struct tree
*t2
= malloc(sizeof(*t2
));
233 t2
->root
= tree_node_copy(tree
->root
);
239 tree_node_merge(struct tree_node
*dest
, struct tree_node
*src
)
241 /* Do not merge nodes that weren't touched at all. */
242 assert(dest
->pamaf
.playouts
== src
->pamaf
.playouts
);
243 assert(dest
->pu
.playouts
== src
->pu
.playouts
);
244 if (src
->amaf
.playouts
- src
->pamaf
.playouts
== 0
245 && src
->u
.playouts
- src
->pu
.playouts
== 0) {
249 dest
->hints
|= src
->hints
;
251 /* Merge the children, both are coord-sorted lists. */
252 struct tree_node
*di
= dest
->children
, **dref
= &dest
->children
;
253 struct tree_node
*si
= src
->children
, **sref
= &src
->children
;
255 if (di
->coord
!= si
->coord
) {
256 /* src has some extra items or misses di */
257 struct tree_node
*si2
= si
->sibling
;
258 while (si2
&& di
->coord
!= si2
->coord
) {
262 goto next_di
; /* src misses di, move on */
263 /* chain the extra [si,si2) items before di */
265 while (si
->sibling
!= si2
) {
274 /* Matching nodes - recurse... */
275 tree_node_merge(di
, si
);
276 /* ...and move on. */
277 sref
= &si
->sibling
; si
= si
->sibling
;
279 dref
= &di
->sibling
; di
= di
->sibling
;
282 /* Some outstanding nodes are left on src side, rechain
292 /* Priors should be constant. */
293 assert(dest
->prior
.playouts
== src
->prior
.playouts
&& dest
->prior
.value
== src
->prior
.value
);
295 stats_merge(&dest
->amaf
, &src
->amaf
);
296 stats_merge(&dest
->u
, &src
->u
);
299 /* Merge two trees built upon the same board. Note that the operation is
300 * destructive on src. */
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
);
311 tree_node_normalize(struct tree_node
*node
, int factor
)
313 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
314 tree_node_normalize(ni
, factor
);
316 #define normalize(s1, s2, t) node->s2.t = node->s1.t + (node->s2.t - node->s1.t) / factor;
317 normalize(pamaf
, amaf
, playouts
);
318 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
320 normalize(pu
, u
, playouts
);
321 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
325 /* Normalize a tree, dividing the amaf and u values by given
326 * factor; otherwise, simulations run in independent threads
327 * two trees built upon the same board. To correctly handle
328 * results taken from previous simulation run, they are backed
331 tree_normalize(struct tree
*tree
, int factor
)
333 tree_node_normalize(tree
->root
, factor
);
337 /* Tree symmetry: When possible, we will localize the tree to a single part
338 * of the board in tree_expand_node() and possibly flip along symmetry axes
339 * to another part of the board in tree_promote_at(). We follow b->symmetry
340 * guidelines here. */
344 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct
*u
, int parity
)
346 /* First, get a map of prior values to initialize the new
348 struct prior_map map
= {
351 .parity
= tree_parity(t
, parity
),
353 // Include pass in the prior map.
354 struct move_stats map_prior
[board_size2(b
) + 1]; map
.prior
= &map_prior
[1];
355 bool map_consider
[board_size2(b
) + 1]; map
.consider
= &map_consider
[1];
356 memset(map_prior
, 0, sizeof(map_prior
));
357 memset(map_consider
, 0, sizeof(map_consider
));
358 struct move pm
= { .color
= color
};
359 map
.consider
[pass
] = true;
361 if (board_at(b
, c
) != S_NONE
)
363 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
364 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
367 if (!board_is_valid_move(b
, &pm
))
369 map
.consider
[c
] = true;
371 uct_prior(u
, node
, &map
);
373 /* Now, create the nodes. */
374 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
375 ni
->parent
= node
; node
->children
= ni
;
376 ni
->prior
= map
.prior
[pass
];
378 /* The loop considers only the symmetry playground. */
380 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
381 coord2sstr(node
->coord
, b
),
382 b
->symmetry
.x1
, b
->symmetry
.y1
,
383 b
->symmetry
.x2
, b
->symmetry
.y2
,
384 b
->symmetry
.type
, b
->symmetry
.d
);
386 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
387 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
389 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
392 fprintf(stderr
, "drop %d,%d\n", i
, j
);
397 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
398 if (!map
.consider
[c
]) // Filter out invalid moves
400 assert(c
!= node
->coord
); // I have spotted "C3 C3" in some sequence...
402 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
403 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
405 ni
->prior
= map
.prior
[c
];
412 flip_coord(struct board
*b
, coord_t c
,
413 bool flip_horiz
, bool flip_vert
, int flip_diag
)
415 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
417 int z
= x
; x
= y
; y
= z
;
420 x
= board_size(b
) - 1 - x
;
423 y
= board_size(b
) - 1 - y
;
425 return coord_xy_otf(x
, y
, b
);
429 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
430 bool flip_horiz
, bool flip_vert
, int flip_diag
)
432 if (!is_pass(node
->coord
))
433 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
435 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
436 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
440 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
445 struct board_symmetry
*s
= &tree
->root_symmetry
;
446 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
448 /* playground X->h->v->d normalization
454 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
455 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
459 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
460 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
461 if (flip_vert
? x
< cy
: x
> cy
) {
467 fprintf(stderr
, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
468 coord2sstr(c
, b
), flip_horiz
, flip_vert
, flip_diag
,
469 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
470 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
472 if (flip_horiz
|| flip_vert
|| flip_diag
)
473 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
478 tree_unlink_node(struct tree_node
*node
)
480 struct tree_node
*ni
= node
->parent
;
481 if (ni
->children
== node
) {
482 ni
->children
= node
->sibling
;
485 while (ni
->sibling
!= node
)
487 ni
->sibling
= node
->sibling
;
489 node
->sibling
= NULL
;
494 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
496 tree_unlink_node(node
);
497 tree_done_node(tree
, node
);
501 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
503 assert(node
->parent
== tree
->root
);
504 tree_unlink_node(node
);
505 tree_done_node(tree
, tree
->root
);
507 tree
->root_color
= stone_other(tree
->root_color
);
508 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
512 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
514 tree_fix_symmetry(tree
, b
, c
);
516 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
517 if (ni
->coord
== c
) {
518 tree_promote_node(tree
, ni
);