13 #include "uct/internal.h"
17 static struct tree_node
*
18 tree_init_node(struct tree
*t
, coord_t coord
, int depth
)
20 struct tree_node
*n
= calloc(1, sizeof(*n
));
23 if (depth
> t
->max_depth
)
29 tree_init(struct board
*board
, enum stone color
)
31 struct tree
*t
= calloc(1, sizeof(*t
));
33 /* The root PASS move is only virtual, we never play it. */
34 t
->root
= tree_init_node(t
, pass
, 0);
35 t
->root_symmetry
= board
->symmetry
;
41 tree_done_node(struct tree
*t
, struct tree_node
*n
)
43 struct tree_node
*ni
= n
->children
;
45 struct tree_node
*nj
= ni
->sibling
;
46 tree_done_node(t
, ni
);
53 tree_done(struct tree
*t
)
55 tree_done_node(t
, t
->root
);
61 tree_node_dump(struct tree
*tree
, struct tree_node
*node
, int l
, int thres
)
63 for (int i
= 0; i
< l
; i
++) fputc(' ', stderr
);
65 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
67 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
);
69 /* Print nodes sorted by #playouts. */
71 struct tree_node
*nbox
[1000]; int nboxl
= 0;
72 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
73 if (ni
->u
.playouts
> thres
)
78 for (int i
= 0; i
< nboxl
; i
++)
79 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
83 tree_node_dump(tree
, nbox
[best
], l
+ 1, thres
);
89 tree_dump(struct tree
*tree
, int thres
)
91 tree_node_dump(tree
, tree
->root
, 0, thres
);
96 tree_book_name(struct board
*b
)
99 sprintf(buf
, "uctbook-%d-%02.01f.pachitree", b
->size
- 2, b
->komi
);
104 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
107 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
108 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
111 if (node
->u
.playouts
>= thres
)
112 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
113 tree_node_save(f
, ni
, thres
);
119 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
121 char *filename
= tree_book_name(b
);
122 FILE *f
= fopen(filename
, "wb");
127 tree_node_save(f
, tree
->root
, thres
);
134 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
, bool invert
)
138 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
139 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
143 node
->u
.wins
= node
->u
.playouts
- node
->u
.wins
;
144 node
->u
.value
= 1 - node
->u
.value
;
145 node
->amaf
.wins
= node
->amaf
.playouts
- node
->amaf
.wins
;
146 node
->amaf
.value
= 1 - node
->amaf
.value
;
147 node
->prior
.wins
= node
->prior
.playouts
- node
->prior
.wins
;
148 node
->prior
.value
= 1 - node
->prior
.value
;
151 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
153 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
157 ni_prev
->sibling
= ni
;
159 tree_node_load(f
, ni
, num
, invert
);
164 tree_load(struct tree
*tree
, struct board
*b
, enum stone color
)
166 char *filename
= tree_book_name(b
);
167 FILE *f
= fopen(filename
, "rb");
171 fprintf(stderr
, "Loading opening book %s...\n", filename
);
175 tree_node_load(f
, tree
->root
, &num
, color
!= S_BLACK
);
176 fprintf(stderr
, "Loaded %d nodes.\n", num
);
182 /* Tree symmetry: When possible, we will localize the tree to a single part
183 * of the board in tree_expand_node() and possibly flip along symmetry axes
184 * to another part of the board in tree_promote_at(). We follow b->symmetry
185 * guidelines here. */
189 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct_policy
*policy
, int parity
)
191 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
192 ni
->parent
= node
; node
->children
= ni
;
194 /* The loop considers only the symmetry playground. */
196 fprintf(stderr
, "expanding %s within [%d,%d],[%d,%d] %d-%d\n",
197 coord2sstr(node
->coord
, b
),
198 b
->symmetry
.x1
, b
->symmetry
.y1
,
199 b
->symmetry
.x2
, b
->symmetry
.y2
,
200 b
->symmetry
.type
, b
->symmetry
.d
);
202 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
203 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
205 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - 1 - i
: i
;
208 fprintf(stderr
, "drop %d,%d\n", i
, j
);
213 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
214 if (board_at(b
, c
) != S_NONE
)
216 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
217 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
220 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
221 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
224 policy
->prior(policy
, t
, ni
, b
, color
, parity
);
231 flip_coord(struct board
*b
, coord_t c
,
232 bool flip_horiz
, bool flip_vert
, int flip_diag
)
234 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
236 int z
= x
; x
= y
; y
= z
;
239 x
= board_size(b
) - 1 - x
;
242 y
= board_size(b
) - 1 - y
;
244 return coord_xy_otf(x
, y
, b
);
248 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
249 bool flip_horiz
, bool flip_vert
, int flip_diag
)
251 node
->coord
= flip_coord(b
, node
->coord
, flip_horiz
, flip_vert
, flip_diag
);
253 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
254 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
258 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
260 struct board_symmetry
*s
= &tree
->root_symmetry
;
261 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
263 /* playground X->h->v->d normalization
269 bool flip_horiz
= cx
< s
->x1
|| cx
> s
->x2
;
270 bool flip_vert
= cy
< s
->y1
|| cy
> s
->y2
;
274 bool dir
= (s
->type
== SYM_DIAG_DOWN
);
275 int x
= dir
^ flip_horiz
^ flip_vert
? board_size(b
) - 1 - cx
: cx
;
276 if (flip_vert
? x
< cy
: x
> cy
) {
282 fprintf(stderr
, "%s will flip %d %d %d -> %s, sym %d (%d) -> %d (%d)\n",
283 coord2sstr(c
, b
), flip_horiz
, flip_vert
, flip_diag
,
284 coord2sstr(flip_coord(b
, c
, flip_horiz
, flip_vert
, flip_diag
), b
),
285 s
->type
, s
->d
, b
->symmetry
.type
, b
->symmetry
.d
);
287 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
292 tree_unlink_node(struct tree_node
*node
)
294 struct tree_node
*ni
= node
->parent
;
295 if (ni
->children
== node
) {
296 ni
->children
= node
->sibling
;
299 while (ni
->sibling
!= node
)
301 ni
->sibling
= node
->sibling
;
306 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
308 tree_unlink_node(node
);
309 tree_done_node(tree
, node
);
313 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
315 assert(node
->parent
== tree
->root
);
316 tree_unlink_node(node
);
317 tree_done_node(tree
, tree
->root
);
319 board_symmetry_update(tree
->board
, &tree
->root_symmetry
, node
->coord
);
324 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
326 tree_fix_symmetry(tree
, b
, c
);
328 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
) {
329 if (ni
->coord
== c
) {
330 tree_promote_node(tree
, ni
);
338 tree_leaf_node(struct tree_node
*node
)
340 return !(node
->children
);
344 tree_update_node_value(struct tree_node
*node
, bool add_amaf
)
346 node
->u
.value
= (float)(node
->u
.wins
+ node
->prior
.wins
+ (add_amaf
? node
->amaf
.wins
: 0))
347 / (node
->u
.playouts
+ node
->prior
.playouts
+ (add_amaf
? node
->amaf
.playouts
: 0));
349 { struct board b2
; board_size(&b2
) = 9+2;
350 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
); }