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
));
26 static long c
= 1000000;
28 if (depth
> t
->max_depth
)
34 tree_init(struct board
*board
, enum stone color
)
36 struct tree
*t
= calloc(1, sizeof(*t
));
38 /* The root PASS move is only virtual, we never play it. */
39 t
->root
= tree_init_node(t
, pass
, 0);
40 t
->root_symmetry
= board
->symmetry
;
41 t
->root_color
= stone_other(color
); // to research black moves, root will be white
47 tree_done_node(struct tree
*t
, struct tree_node
*n
)
49 struct tree_node
*ni
= n
->children
;
51 struct tree_node
*nj
= ni
->sibling
;
52 tree_done_node(t
, ni
);
59 tree_done(struct tree
*t
)
61 tree_done_node(t
, t
->root
);
67 tree_node_dump(struct tree
*tree
, struct tree_node
*node
, int l
, int thres
)
69 for (int i
= 0; i
< l
; i
++) fputc(' ', stderr
);
71 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
73 /* We use 1 as parity, since for all nodes we want to know the
74 * win probability of _us_, not the node color. */
75 fprintf(stderr
, "[%s] %f (%d/%d playouts [prior %d/%d amaf %d/%d]; hints %x; %d children) <%lld>\n",
76 coord2sstr(node
->coord
, tree
->board
),
77 tree_node_get_value(tree
, node
, u
, 1),
78 tree_node_get_wins(tree
, node
, u
, 1), node
->u
.playouts
,
79 tree_node_get_wins(tree
, node
, prior
, 1), node
->prior
.playouts
,
80 tree_node_get_wins(tree
, node
, amaf
, 1), node
->amaf
.playouts
,
81 node
->hints
, children
, node
->hash
);
83 /* Print nodes sorted by #playouts. */
85 struct tree_node
*nbox
[1000]; int nboxl
= 0;
86 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
87 if (ni
->u
.playouts
> thres
)
92 for (int i
= 0; i
< nboxl
; i
++)
93 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
97 tree_node_dump(tree
, nbox
[best
], l
+ 1, /* node->u.value < 0.1 ? 0 : */ thres
);
103 tree_dump(struct tree
*tree
, int thres
)
105 if (thres
&& tree
->root
->u
.playouts
/ thres
> 100) {
106 /* Be a bit sensible about this; the opening book can create
107 * huge dumps at first. */
108 thres
= tree
->root
->u
.playouts
/ 100 * (thres
< 1000 ? 1 : thres
/ 1000);
110 tree_node_dump(tree
, tree
->root
, 0, thres
);
115 tree_book_name(struct board
*b
)
117 static char buf
[256];
118 if (b
->handicap
> 0) {
119 sprintf(buf
, "uctbook-%d-%02.01f-h%d.pachitree", b
->size
- 2, b
->komi
, b
->handicap
);
121 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
127 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
130 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
131 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
134 if (node
->u
.playouts
>= thres
)
135 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
136 tree_node_save(f
, ni
, thres
);
142 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
144 char *filename
= tree_book_name(b
);
145 FILE *f
= fopen(filename
, "wb");
150 tree_node_save(f
, tree
->root
, thres
);
157 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
)
161 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
162 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
165 /* Keep values in sane scale, otherwise we start overflowing.
166 * We may go slow here but we must be careful about not getting
167 * too huge integers.*/
168 #define MAX_PLAYOUTS 10000000
169 if (node
->u
.playouts
> MAX_PLAYOUTS
) {
170 int over
= node
->u
.playouts
- MAX_PLAYOUTS
;
171 node
->u
.wins
-= ((double) node
->u
.wins
/ node
->u
.playouts
) * over
;
172 node
->u
.playouts
= MAX_PLAYOUTS
;
174 if (node
->amaf
.playouts
> MAX_PLAYOUTS
) {
175 int over
= node
->amaf
.playouts
- MAX_PLAYOUTS
;
176 node
->amaf
.wins
-= ((double) node
->amaf
.wins
/ node
->amaf
.playouts
) * over
;
177 node
->amaf
.playouts
= MAX_PLAYOUTS
;
180 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
181 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
183 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
185 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
189 ni_prev
->sibling
= ni
;
191 tree_node_load(f
, ni
, num
);
196 tree_load(struct tree
*tree
, struct board
*b
)
198 char *filename
= tree_book_name(b
);
199 FILE *f
= fopen(filename
, "rb");
203 fprintf(stderr
, "Loading opening book %s...\n", filename
);
207 tree_node_load(f
, tree
->root
, &num
);
208 fprintf(stderr
, "Loaded %d nodes.\n", num
);
214 static struct tree_node
*
215 tree_node_copy(struct tree_node
*node
)
217 struct tree_node
*n2
= malloc(sizeof(*n2
));
221 struct tree_node
*ni
= node
->children
;
222 struct tree_node
*ni2
= tree_node_copy(ni
);
223 n2
->children
= ni2
; ni2
->parent
= n2
;
224 while ((ni
= ni
->sibling
)) {
225 ni2
->sibling
= tree_node_copy(ni
);
226 ni2
= ni2
->sibling
; ni2
->parent
= n2
;
232 tree_copy(struct tree
*tree
)
234 struct tree
*t2
= malloc(sizeof(*t2
));
236 t2
->root
= tree_node_copy(tree
->root
);
242 tree_node_merge(struct tree_node
*dest
, struct tree_node
*src
, bool amaf_prior
)
244 /* Do not merge nodes that weren't touched at all. */
245 assert(dest
->pamaf
.playouts
== src
->pamaf
.playouts
);
246 assert(dest
->pu
.playouts
== src
->pu
.playouts
);
247 if (src
->amaf
.playouts
- src
->pamaf
.playouts
== 0
248 && src
->u
.playouts
- src
->pu
.playouts
== 0) {
252 dest
->hints
|= src
->hints
;
254 /* Merge the children, both are coord-sorted lists. */
255 struct tree_node
*di
= dest
->children
, **dref
= &dest
->children
;
256 struct tree_node
*si
= src
->children
, **sref
= &src
->children
;
258 if (di
->coord
!= si
->coord
) {
259 /* src has some extra items or misses di */
260 struct tree_node
*si2
= si
->sibling
;
261 while (si2
&& di
->coord
!= si2
->coord
) {
265 goto next_di
; /* src misses di, move on */
266 /* chain the extra [si,si2) items before di */
268 while (si
->sibling
!= si2
) {
277 /* Matching nodes - recurse... */
278 tree_node_merge(di
, si
, amaf_prior
);
279 /* ...and move on. */
280 sref
= &si
->sibling
; si
= si
->sibling
;
282 dref
= &di
->sibling
; di
= di
->sibling
;
285 /* Some outstanding nodes are left on src side, rechain
295 /* Priors should be constant. */
296 assert(dest
->prior
.playouts
== src
->prior
.playouts
&& dest
->prior
.wins
== src
->prior
.wins
);
298 dest
->amaf
.playouts
+= src
->amaf
.playouts
;
299 dest
->amaf
.wins
+= src
->amaf
.wins
;
300 if (dest
->amaf
.playouts
)
301 tree_update_node_rvalue(dest
, amaf_prior
);
303 dest
->u
.playouts
+= src
->u
.playouts
;
304 dest
->u
.wins
+= src
->u
.wins
;
305 if (dest
->u
.playouts
)
306 tree_update_node_value(dest
, amaf_prior
);
309 /* Merge two trees built upon the same board. Note that the operation is
310 * destructive on src. */
312 tree_merge(struct tree
*dest
, struct tree
*src
, bool amaf_prior
)
314 if (src
->max_depth
> dest
->max_depth
)
315 dest
->max_depth
= src
->max_depth
;
316 tree_node_merge(dest
->root
, src
->root
, amaf_prior
);
321 tree_node_normalize(struct tree_node
*node
, int factor
)
323 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
324 tree_node_normalize(ni
, factor
);
326 #define normalize(s1, s2, t) node->s2.t = node->s1.t + (node->s2.t - node->s1.t) / factor;
327 normalize(pamaf
, amaf
, playouts
);
328 normalize(pamaf
, amaf
, wins
);
329 memcpy(&node
->pamaf
, &node
->amaf
, sizeof(node
->amaf
));
331 normalize(pu
, u
, playouts
);
332 normalize(pu
, u
, wins
);
333 memcpy(&node
->pu
, &node
->u
, sizeof(node
->u
));
337 /* Normalize a tree, dividing the amaf and u values by given
338 * factor; otherwise, simulations run in independent threads
339 * two trees built upon the same board. To correctly handle
340 * results taken from previous simulation run, they are backed
343 tree_normalize(struct tree
*tree
, int factor
)
345 tree_node_normalize(tree
->root
, factor
);
349 /* Tree symmetry: When possible, we will localize the tree to a single part
350 * of the board in tree_expand_node() and possibly flip along symmetry axes
351 * to another part of the board in tree_promote_at(). We follow b->symmetry
352 * guidelines here. */
356 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct
*u
, int parity
)
358 /* First, get a map of prior values to initialize the new
360 struct prior_map map
= {
363 .parity
= tree_parity(t
, parity
),
365 // Include pass in the prior map.
366 map
.prior
= calloc(board_size2(b
) + 1, sizeof(*map
.prior
)); map
.prior
++;
367 map
.consider
= calloc(board_size2(b
) + 1, sizeof(*map
.consider
)); map
.consider
++;
368 struct move pm
= { .color
= color
};
369 map
.consider
[pass
] = true;
371 if (board_at(b
, c
) != S_NONE
)
373 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
374 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
377 if (!board_is_valid_move(b
, &pm
))
379 map
.consider
[c
] = true;
381 uct_prior(u
, node
, &map
);
383 /* Now, create the nodes. */
384 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
385 ni
->parent
= node
; node
->children
= ni
;
386 ni
->prior
= map
.prior
[pass
];
387 if (ni
->prior
.playouts
) {
389 tree_update_node_rvalue(ni
, u
->amaf_prior
);
391 tree_update_node_value(ni
, u
->amaf_prior
);
394 /* The loop considers only the symmetry playground. */
396 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
397 coord2sstr(node
->coord
, b
),
398 b
->symmetry
.x1
, b
->symmetry
.y1
,
399 b
->symmetry
.x2
, b
->symmetry
.y2
,
400 b
->symmetry
.type
, b
->symmetry
.d
);
402 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
403 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
405 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
408 fprintf(stderr
, "drop %d,%d\n", i
, j
);
413 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
414 if (!map
.consider
[c
]) // Filter out invalid moves
416 assert(c
!= node
->coord
); // I have spotted "C3 C3" in some sequence...
418 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
419 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
421 ni
->prior
= map
.prior
[c
];
422 if (ni
->prior
.playouts
) {
424 tree_update_node_rvalue(ni
, u
->amaf_prior
);
426 tree_update_node_value(ni
, u
->amaf_prior
);
434 flip_coord(struct board
*b
, coord_t c
,
435 bool flip_horiz
, bool flip_vert
, int flip_diag
)
437 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
439 int z
= x
; x
= y
; y
= z
;
442 x
= board_size(b
) - 1 - x
;
445 y
= board_size(b
) - 1 - y
;
447 return coord_xy_otf(x
, y
, b
);
451 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
452 bool flip_horiz
, bool flip_vert
, int flip_diag
)
454 if (!is_pass(node
->coord
))
455 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
457 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
458 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
462 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
467 struct board_symmetry
*s
= &tree
->root_symmetry
;
468 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
470 /* playground X->h->v->d normalization
476 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
477 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
481 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
482 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
483 if (flip_vert
? x
< cy
: x
> cy
) {
489 fprintf(stderr
, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
490 coord2sstr(c
, b
), flip_horiz
, flip_vert
, flip_diag
,
491 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
492 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
494 if (flip_horiz
|| flip_vert
|| flip_diag
)
495 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
500 tree_unlink_node(struct tree_node
*node
)
502 struct tree_node
*ni
= node
->parent
;
503 if (ni
->children
== node
) {
504 ni
->children
= node
->sibling
;
507 while (ni
->sibling
!= node
)
509 ni
->sibling
= node
->sibling
;
511 node
->sibling
= NULL
;
516 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
518 tree_unlink_node(node
);
519 tree_done_node(tree
, node
);
523 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
525 assert(node
->parent
== tree
->root
);
526 tree_unlink_node(node
);
527 tree_done_node(tree
, tree
->root
);
529 tree
->root_color
= stone_other(tree
->root_color
);
530 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
534 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
536 tree_fix_symmetry(tree
, b
, c
);
538 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
539 if (ni
->coord
== c
) {
540 tree_promote_node(tree
, ni
);