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/%d playouts [prior %d/%d amaf %d/%d]; hints %x; %d children) <%lld>\n",
80 coord2sstr(node
->coord
, tree
->board
),
81 tree_node_get_value(tree
, node
, u
, 1),
82 tree_node_get_wins(tree
, node
, u
, 1), node
->u
.playouts
,
83 tree_node_get_wins(tree
, node
, prior
, 1), node
->prior
.playouts
,
84 tree_node_get_wins(tree
, node
, amaf
, 1), node
->amaf
.playouts
,
85 node
->hints
, children
, node
->hash
);
87 /* Print nodes sorted by #playouts. */
89 struct tree_node
*nbox
[1000]; int nboxl
= 0;
90 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
91 if (ni
->u
.playouts
> thres
)
96 for (int i
= 0; i
< nboxl
; i
++)
97 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
101 tree_node_dump(tree
, nbox
[best
], l
+ 1, /* node->u.value < 0.1 ? 0 : */ thres
);
107 tree_dump(struct tree
*tree
, int thres
)
109 if (thres
&& tree
->root
->u
.playouts
/ thres
> 100) {
110 /* Be a bit sensible about this; the opening book can create
111 * huge dumps at first. */
112 thres
= tree
->root
->u
.playouts
/ 100 * (thres
< 1000 ? 1 : thres
/ 1000);
114 tree_node_dump(tree
, tree
->root
, 0, thres
);
119 tree_book_name(struct board
*b
)
121 static char buf
[256];
122 if (b
->handicap
> 0) {
123 sprintf(buf
, "uctbook-%d-%02.01f-h%d.pachitree", b
->size
- 2, b
->komi
, b
->handicap
);
125 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
131 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
134 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
135 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
138 if (node
->u
.playouts
>= thres
)
139 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
140 tree_node_save(f
, ni
, thres
);
146 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
148 char *filename
= tree_book_name(b
);
149 FILE *f
= fopen(filename
, "wb");
154 tree_node_save(f
, tree
->root
, thres
);
161 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
)
165 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
166 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
169 /* Keep values in sane scale, otherwise we start overflowing.
170 * We may go slow here but we must be careful about not getting
171 * too huge integers.*/
172 #define MAX_PLAYOUTS 10000000
173 if (node
->u
.playouts
> MAX_PLAYOUTS
) {
174 int over
= node
->u
.playouts
- MAX_PLAYOUTS
;
175 node
->u
.wins
-= ((double) node
->u
.wins
/ node
->u
.playouts
) * over
;
176 node
->u
.playouts
= MAX_PLAYOUTS
;
178 if (node
->amaf
.playouts
> MAX_PLAYOUTS
) {
179 int over
= node
->amaf
.playouts
- MAX_PLAYOUTS
;
180 node
->amaf
.wins
-= ((double) node
->amaf
.wins
/ node
->amaf
.playouts
) * over
;
181 node
->amaf
.playouts
= MAX_PLAYOUTS
;
184 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
185 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
187 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
189 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
193 ni_prev
->sibling
= ni
;
195 tree_node_load(f
, ni
, num
);
200 tree_load(struct tree
*tree
, struct board
*b
)
202 char *filename
= tree_book_name(b
);
203 FILE *f
= fopen(filename
, "rb");
207 fprintf(stderr
, "Loading opening book %s...\n", filename
);
211 tree_node_load(f
, tree
->root
, &num
);
212 fprintf(stderr
, "Loaded %d nodes.\n", num
);
218 static struct tree_node
*
219 tree_node_copy(struct tree_node
*node
)
221 struct tree_node
*n2
= malloc(sizeof(*n2
));
225 struct tree_node
*ni
= node
->children
;
226 struct tree_node
*ni2
= tree_node_copy(ni
);
227 n2
->children
= ni2
; ni2
->parent
= n2
;
228 while ((ni
= ni
->sibling
)) {
229 ni2
->sibling
= tree_node_copy(ni
);
230 ni2
= ni2
->sibling
; ni2
->parent
= n2
;
236 tree_copy(struct tree
*tree
)
238 struct tree
*t2
= malloc(sizeof(*t2
));
240 t2
->root
= tree_node_copy(tree
->root
);
246 tree_node_merge(struct tree_node
*dest
, struct tree_node
*src
, bool amaf_prior
)
248 /* Do not merge nodes that weren't touched at all. */
249 assert(dest
->pamaf
.playouts
== src
->pamaf
.playouts
);
250 assert(dest
->pu
.playouts
== src
->pu
.playouts
);
251 if (src
->amaf
.playouts
- src
->pamaf
.playouts
== 0
252 && src
->u
.playouts
- src
->pu
.playouts
== 0) {
256 dest
->hints
|= src
->hints
;
258 /* Merge the children, both are coord-sorted lists. */
259 struct tree_node
*di
= dest
->children
, **dref
= &dest
->children
;
260 struct tree_node
*si
= src
->children
, **sref
= &src
->children
;
262 if (di
->coord
!= si
->coord
) {
263 /* src has some extra items or misses di */
264 struct tree_node
*si2
= si
->sibling
;
265 while (si2
&& di
->coord
!= si2
->coord
) {
269 goto next_di
; /* src misses di, move on */
270 /* chain the extra [si,si2) items before di */
272 while (si
->sibling
!= si2
) {
281 /* Matching nodes - recurse... */
282 tree_node_merge(di
, si
, amaf_prior
);
283 /* ...and move on. */
284 sref
= &si
->sibling
; si
= si
->sibling
;
286 dref
= &di
->sibling
; di
= di
->sibling
;
289 /* Some outstanding nodes are left on src side, rechain
299 /* Priors should be constant. */
300 assert(dest
->prior
.playouts
== src
->prior
.playouts
&& dest
->prior
.wins
== src
->prior
.wins
);
302 dest
->amaf
.playouts
+= src
->amaf
.playouts
;
303 dest
->amaf
.wins
+= src
->amaf
.wins
;
304 if (dest
->amaf
.playouts
)
305 tree_update_node_rvalue(dest
, amaf_prior
);
307 dest
->u
.playouts
+= src
->u
.playouts
;
308 dest
->u
.wins
+= src
->u
.wins
;
309 if (dest
->u
.playouts
)
310 tree_update_node_value(dest
, amaf_prior
);
313 /* Merge two trees built upon the same board. Note that the operation is
314 * destructive on src. */
316 tree_merge(struct tree
*dest
, struct tree
*src
, bool amaf_prior
)
318 if (src
->max_depth
> dest
->max_depth
)
319 dest
->max_depth
= src
->max_depth
;
320 tree_node_merge(dest
->root
, src
->root
, amaf_prior
);
325 tree_node_normalize(struct tree_node
*node
, int factor
)
327 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
328 tree_node_normalize(ni
, factor
);
330 #define normalize(s1, s2, t) node->s2.t = node->s1.t + (node->s2.t - node->s1.t) / factor;
331 normalize(pamaf
, amaf
, playouts
);
332 normalize(pamaf
, amaf
, wins
);
333 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
335 normalize(pu
, u
, playouts
);
336 normalize(pu
, u
, wins
);
337 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
341 /* Normalize a tree, dividing the amaf and u values by given
342 * factor; otherwise, simulations run in independent threads
343 * two trees built upon the same board. To correctly handle
344 * results taken from previous simulation run, they are backed
347 tree_normalize(struct tree
*tree
, int factor
)
349 tree_node_normalize(tree
->root
, factor
);
353 /* Tree symmetry: When possible, we will localize the tree to a single part
354 * of the board in tree_expand_node() and possibly flip along symmetry axes
355 * to another part of the board in tree_promote_at(). We follow b->symmetry
356 * guidelines here. */
360 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct
*u
, int parity
)
362 /* First, get a map of prior values to initialize the new
364 struct prior_map map
= {
367 .parity
= tree_parity(t
, parity
),
369 // Include pass in the prior map.
370 struct move_stats map_prior
[board_size2(b
) + 1]; map
.prior
= &map_prior
[1];
371 bool map_consider
[board_size2(b
) + 1]; map
.consider
= &map_consider
[1];
372 memset(map_prior
, 0, sizeof(map_prior
));
373 memset(map_consider
, 0, sizeof(map_consider
));
374 struct move pm
= { .color
= color
};
375 map
.consider
[pass
] = true;
377 if (board_at(b
, c
) != S_NONE
)
379 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
380 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
383 if (!board_is_valid_move(b
, &pm
))
385 map
.consider
[c
] = true;
387 uct_prior(u
, node
, &map
);
389 /* Now, create the nodes. */
390 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
391 ni
->parent
= node
; node
->children
= ni
;
392 ni
->prior
= map
.prior
[pass
];
393 if (ni
->prior
.playouts
) {
395 tree_update_node_rvalue(ni
, u
->amaf_prior
);
397 tree_update_node_value(ni
, u
->amaf_prior
);
400 /* The loop considers only the symmetry playground. */
402 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
403 coord2sstr(node
->coord
, b
),
404 b
->symmetry
.x1
, b
->symmetry
.y1
,
405 b
->symmetry
.x2
, b
->symmetry
.y2
,
406 b
->symmetry
.type
, b
->symmetry
.d
);
408 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
409 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
411 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
414 fprintf(stderr
, "drop %d,%d\n", i
, j
);
419 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
420 if (!map
.consider
[c
]) // Filter out invalid moves
422 assert(c
!= node
->coord
); // I have spotted "C3 C3" in some sequence...
424 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
425 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
427 ni
->prior
= map
.prior
[c
];
428 if (ni
->prior
.playouts
) {
430 tree_update_node_rvalue(ni
, u
->amaf_prior
);
432 tree_update_node_value(ni
, u
->amaf_prior
);
440 flip_coord(struct board
*b
, coord_t c
,
441 bool flip_horiz
, bool flip_vert
, int flip_diag
)
443 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
445 int z
= x
; x
= y
; y
= z
;
448 x
= board_size(b
) - 1 - x
;
451 y
= board_size(b
) - 1 - y
;
453 return coord_xy_otf(x
, y
, b
);
457 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
458 bool flip_horiz
, bool flip_vert
, int flip_diag
)
460 if (!is_pass(node
->coord
))
461 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
463 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
464 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
468 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
473 struct board_symmetry
*s
= &tree
->root_symmetry
;
474 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
476 /* playground X->h->v->d normalization
482 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
483 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
487 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
488 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
489 if (flip_vert
? x
< cy
: x
> cy
) {
495 fprintf(stderr
, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
496 coord2sstr(c
, b
), flip_horiz
, flip_vert
, flip_diag
,
497 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
498 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
500 if (flip_horiz
|| flip_vert
|| flip_diag
)
501 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
506 tree_unlink_node(struct tree_node
*node
)
508 struct tree_node
*ni
= node
->parent
;
509 if (ni
->children
== node
) {
510 ni
->children
= node
->sibling
;
513 while (ni
->sibling
!= node
)
515 ni
->sibling
= node
->sibling
;
517 node
->sibling
= NULL
;
522 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
524 tree_unlink_node(node
);
525 tree_done_node(tree
, node
);
529 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
531 assert(node
->parent
== tree
->root
);
532 tree_unlink_node(node
);
533 tree_done_node(tree
, tree
->root
);
535 tree
->root_color
= stone_other(tree
->root_color
);
536 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
540 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
542 tree_fix_symmetry(tree
, b
, c
);
544 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
545 if (ni
->coord
== c
) {
546 tree_promote_node(tree
, ni
);