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);
40 tree_done_node(struct tree
*t
, struct tree_node
*n
)
42 struct tree_node
*ni
= n
->children
;
44 struct tree_node
*nj
= ni
->sibling
;
45 tree_done_node(t
, ni
);
52 tree_done(struct tree
*t
)
54 tree_done_node(t
, t
->root
);
60 tree_node_dump(struct tree
*tree
, struct tree_node
*node
, int l
, int thres
)
62 for (int i
= 0; i
< l
; i
++) fputc(' ', stderr
);
63 fprintf(stderr
, "[%s] %f (%d/%d playouts [prior %d/%d amaf %d/%d]; hints %x)\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
);
65 /* Print nodes sorted by #playouts. */
67 struct tree_node
*nbox
[1000]; int nboxl
= 0;
68 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
69 if (ni
->u
.playouts
> thres
)
74 for (int i
= 0; i
< nboxl
; i
++)
75 if (nbox
[i
] && (best
< 0 || nbox
[i
]->u
.playouts
> nbox
[best
]->u
.playouts
))
79 tree_node_dump(tree
, nbox
[best
], l
+ 1, thres
);
85 tree_dump(struct tree
*tree
, int thres
)
87 tree_node_dump(tree
, tree
->root
, 0, thres
);
92 tree_book_name(struct board
*b
)
95 sprintf(buf
, "uct-%d-%02.01f.pachibook", b
->size
- 2, b
->komi
);
100 tree_node_save(FILE *f
, struct tree_node
*node
, int thres
)
102 if (node
->u
.playouts
< thres
)
106 fwrite(((void *) node
) + offsetof(struct tree_node
, depth
),
107 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
110 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
) {
111 tree_node_save(f
, ni
, thres
);
118 tree_save(struct tree
*tree
, struct board
*b
, int thres
)
120 char *filename
= tree_book_name(b
);
121 FILE *f
= fopen(filename
, "wb");
126 tree_node_save(f
, tree
->root
, thres
);
133 tree_node_load(FILE *f
, struct tree_node
*node
, int *num
)
137 fread(((void *) node
) + offsetof(struct tree_node
, depth
),
138 sizeof(struct tree_node
) - offsetof(struct tree_node
, depth
),
141 struct tree_node
*ni
= NULL
, *ni_prev
= NULL
;
143 ni_prev
= ni
; ni
= calloc(1, sizeof(*ni
));
147 ni_prev
->sibling
= ni
;
149 tree_node_load(f
, ni
, num
);
154 tree_load(struct tree
*tree
, struct board
*b
)
156 char *filename
= tree_book_name(b
);
157 FILE *f
= fopen(filename
, "rb");
161 fprintf(stderr
, "Loading opening book %s...\n", filename
);
165 tree_node_load(f
, tree
->root
, &num
);
166 fprintf(stderr
, "Loaded %d nodes.\n", num
);
172 /* Tree symmetry: When possible, we will localize the tree to a single part
173 * of the board in tree_expand_node() and possibly flip along symmetry axes
174 * to another part of the board in tree_promote_at(). We follow b->symmetry
175 * guidelines here. */
179 tree_expand_node(struct tree
*t
, struct tree_node
*node
, struct board
*b
, enum stone color
, int radar
, struct uct_policy
*policy
, int parity
)
181 struct tree_node
*ni
= tree_init_node(t
, pass
, node
->depth
+ 1);
182 ni
->parent
= node
; node
->children
= ni
;
184 /* The loop considers only the symmetry playground. */
185 for (int i
= b
->symmetry
.x1
; i
<= b
->symmetry
.x2
; i
++) {
186 for (int j
= b
->symmetry
.y1
; j
<= b
->symmetry
.y2
; j
++) {
188 int x
= b
->symmetry
.type
== SYM_DIAG_DOWN
? board_size(b
) - i
: i
;
189 if (b
->symmetry
.d
< 0 ? x
< j
: x
> j
)
193 coord_t c
= coord_xy_otf(i
, j
, t
->board
);
194 if (board_at(b
, c
) != S_NONE
)
196 /* This looks very useful on large boards - weeds out huge amount of crufty moves. */
197 if (b
->hash
/* not empty board */ && radar
&& !board_stone_radar(b
, c
, radar
))
200 struct tree_node
*nj
= tree_init_node(t
, c
, node
->depth
+ 1);
201 nj
->parent
= node
; ni
->sibling
= nj
; ni
= nj
;
204 policy
->prior(policy
, t
, ni
, b
, color
, parity
);
211 tree_fix_node_symmetry(struct board
*b
, struct tree_node
*node
,
212 bool flip_horiz
, bool flip_vert
, int flip_diag
)
214 int x
= coord_x(node
->coord
, b
), y
= coord_y(node
->coord
, b
);
217 x
= flip_diag
== 1 ? y
: board_size(b
) - y
;
218 y
= flip_diag
== 1 ? z
: board_size(b
) - z
;
221 x
= board_size(b
) - x
;
224 y
= board_size(b
) - y
;
226 node
->coord
= coord_xy_otf(x
, y
, b
);
228 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
)
229 tree_fix_node_symmetry(b
, ni
, flip_horiz
, flip_vert
, flip_diag
);
233 tree_fix_symmetry(struct tree
*tree
, struct board
*b
, coord_t c
)
235 /* XXX: We hard-coded assume c is the same move that tree-root,
236 * just possibly flipped. */
238 if (c
== tree
->root
->coord
)
241 int cx
= coord_x(c
, b
), cy
= coord_y(c
, b
);
242 int rx
= coord_x(tree
->root
->coord
, b
), ry
= coord_y(tree
->root
->coord
, b
);
244 /* playground X->h->v->d normalization
250 bool flip_horiz
= cy
== ry
;
251 bool flip_vert
= cx
== rx
;
253 int nx
= flip_horiz
? board_size(b
) - rx
: rx
;
254 int ny
= flip_vert
? board_size(b
) - ry
: ry
;
257 if (nx
== cy
&& ny
== cx
) {
259 } else if (board_size(b
) - nx
== cy
&& ny
== board_size(b
) - cx
) {
263 tree_fix_node_symmetry(b
, tree
->root
, flip_horiz
, flip_vert
, flip_diag
);
268 tree_unlink_node(struct tree_node
*node
)
270 struct tree_node
*ni
= node
->parent
;
271 if (ni
->children
== node
) {
272 ni
->children
= node
->sibling
;
275 while (ni
->sibling
!= node
)
277 ni
->sibling
= node
->sibling
;
282 tree_delete_node(struct tree
*tree
, struct tree_node
*node
)
284 tree_unlink_node(node
);
285 tree_done_node(tree
, node
);
289 tree_promote_node(struct tree
*tree
, struct tree_node
*node
)
291 assert(node
->parent
== tree
->root
);
292 tree_unlink_node(node
);
293 tree_done_node(tree
, tree
->root
);
299 tree_promote_at(struct tree
*tree
, struct board
*b
, coord_t c
)
301 tree_fix_symmetry(tree
, b
, c
);
303 for (struct tree_node
*ni
= tree
->root
->children
; ni
; ni
= ni
->sibling
)
304 if (ni
->coord
== c
) {
305 tree_promote_node(tree
, ni
);
312 tree_leaf_node(struct tree_node
*node
)
314 return !(node
->children
);
318 tree_update_node_value(struct tree_node
*node
, bool add_amaf
)
320 node
->u
.value
= (float)(node
->u
.wins
+ node
->prior
.wins
+ (add_amaf
? node
->amaf
.wins
: 0))
321 / (node
->u
.playouts
+ node
->prior
.playouts
+ (add_amaf
? node
->amaf
.playouts
: 0));
323 { struct board b2
; board_size(&b2
) = 9+2;
324 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
); }