15 #include "playout/elo.h"
18 #include "uct/dynkomi.h"
19 #include "uct/internal.h"
20 #include "uct/search.h"
26 uct_progress_status(struct uct
*u
, struct tree
*t
, enum stone color
, int playouts
)
32 struct tree_node
*best
= u
->policy
->choose(u
->policy
, t
->root
, t
->board
, color
, resign
);
34 fprintf(stderr
, "... No moves left\n");
37 fprintf(stderr
, "[%d] ", playouts
);
38 fprintf(stderr
, "best %f ", tree_node_get_value(t
, 1, best
->u
.value
));
41 if (t
->use_extra_komi
)
42 fprintf(stderr
, "komi %.1f ", t
->extra_komi
);
45 fprintf(stderr
, "| seq ");
46 for (int depth
= 0; depth
< 4; depth
++) {
47 if (best
&& best
->u
.playouts
>= 25) {
48 fprintf(stderr
, "%3s ", coord2sstr(best
->coord
, t
->board
));
49 best
= u
->policy
->choose(u
->policy
, best
, t
->board
, color
, resign
);
56 fprintf(stderr
, "| can ");
58 struct tree_node
*can
[cans
];
59 memset(can
, 0, sizeof(can
));
60 best
= t
->root
->children
;
63 while ((!can
[c
] || best
->u
.playouts
> can
[c
]->u
.playouts
) && ++c
< cans
);
64 for (int d
= 0; d
< c
; d
++) can
[d
] = can
[d
+ 1];
65 if (c
> 0) can
[c
- 1] = best
;
70 fprintf(stderr
, "%3s(%.3f) ",
71 coord2sstr(can
[cans
]->coord
, t
->board
),
72 tree_node_get_value(t
, 1, can
[cans
]->u
.value
));
78 fprintf(stderr
, "\n");
83 record_amaf_move(struct playout_amafmap
*amaf
, coord_t coord
, enum stone color
)
85 if (amaf
->map
[coord
] == S_NONE
|| amaf
->map
[coord
] == color
) {
86 amaf
->map
[coord
] = color
;
87 } else { // XXX: Respect amaf->record_nakade
88 amaf_op(amaf
->map
[coord
], +);
90 amaf
->game
[amaf
->gamelen
].coord
= coord
;
91 amaf
->game
[amaf
->gamelen
].color
= color
;
93 assert(amaf
->gamelen
< sizeof(amaf
->game
) / sizeof(amaf
->game
[0]));
97 ltree_node_gamma(struct tree_node
*li
, enum stone color
)
99 /* TODO: How to do this? */
100 #define li_value(color, li) (li->u.playouts * (color == S_BLACK ? li->u.value : (1 - li->u.value)))
101 return 0.5 + li_value(color
, li
);
105 struct uct_playout_callback
{
108 struct tree_node
*lnode
;
112 uct_playout_probdist(void *data
, struct board
*b
, enum stone to_play
, struct probdist
*pd
)
114 /* Create probability distribution according to found local tree
116 struct uct_playout_callback
*upc
= data
;
117 assert(upc
&& upc
->tree
&& pd
&& b
);
118 coord_t c
= b
->last_move
.coord
;
119 enum stone color
= b
->last_move
.color
;
122 /* Break local sequence. */
124 } else if (upc
->lnode
) {
125 /* Try to follow local sequence. */
126 upc
->lnode
= tree_get_node(upc
->tree
, upc
->lnode
, c
, false);
129 if (!upc
->lnode
|| !upc
->lnode
->children
) {
130 /* There's no local sequence, start new one! */
131 upc
->lnode
= color
== S_BLACK
? upc
->tree
->ltree_black
: upc
->tree
->ltree_white
;
132 upc
->lnode
= tree_get_node(upc
->tree
, upc
->lnode
, c
, false);
135 if (!upc
->lnode
|| !upc
->lnode
->children
) {
136 /* We have no local sequence and we cannot find any starting
137 * by node corresponding to last move. */
138 if (!upc
->uct
->local_tree_pseqroot
) {
139 /* Give up then, we have nothing to contribute. */
142 /* Construct probability distribution from possible first
143 * sequence move. Remember that @color is color of the
145 upc
->lnode
= color
== S_BLACK
? upc
->tree
->ltree_white
: upc
->tree
->ltree_black
;
146 if (!upc
->lnode
->children
) {
147 /* We don't even have anything in our tree yet. */
152 /* The probdist has the right structure only if BOARD_GAMMA is defined. */
157 /* Construct probability distribution from lnode children. */
158 struct tree_node
*li
= upc
->lnode
->children
;
160 if (is_pass(li
->coord
)) {
162 /* TODO: Spread tenuki gamma over all moves we don't touch. */
165 for (; li
; li
= li
->sibling
) {
166 if (board_at(b
, li
->coord
) != S_NONE
)
168 double gamma
= fixp_to_double(pd
->items
[li
->coord
]) * ltree_node_gamma(li
, to_play
);
169 probdist_set(pd
, li
->coord
, double_to_fixp(gamma
));
175 uct_leaf_node(struct uct
*u
, struct board
*b
, enum stone player_color
,
176 struct playout_amafmap
*amaf
,
177 struct tree
*t
, struct tree_node
*n
, enum stone node_color
,
180 enum stone next_color
= stone_other(node_color
);
181 int parity
= (next_color
== player_color
? 1 : -1);
183 /* If we don't anticipate well the opponent move during pondering
184 * (the played move has few playouts) we still need more memory
185 * during genmove to explore the tree actually played.
186 * For fast_alloc, the tree compaction will free enough memory
188 unsigned long max_tree_size
= u
->max_tree_size
;
189 if (u
->pondering
&& !u
->fast_alloc
)
190 max_tree_size
= (max_tree_size
* (100 - MIN_FREE_MEM_PERCENT
)) / 100;
192 /* We need to make sure only one thread expands the node. If
193 * we are unlucky enough for two threads to meet in the same
194 * node, the latter one will simply do another simulation from
195 * the node itself, no big deal. t->nodes_size may exceed
196 * the maximum in multi-threaded case but not by much so it's ok.
197 * The size test must be before the test&set not after, to allow
198 * expansion of the node later if enough nodes have been freed. */
199 if (n
->u
.playouts
>= u
->expand_p
&& t
->nodes_size
< max_tree_size
200 && !__sync_lock_test_and_set(&n
->is_expanded
, 1)) {
201 tree_expand_node(t
, n
, b
, next_color
, u
, parity
);
204 fprintf(stderr
, "%s*-- UCT playout #%d start [%s] %f\n",
205 spaces
, n
->u
.playouts
, coord2sstr(n
->coord
, t
->board
),
206 tree_node_get_value(t
, parity
, n
->u
.value
));
208 /* TODO: Don't necessarily restart the sequence walk when entering
210 struct uct_playout_callback upc
= { .uct
= u
, .tree
= t
, .lnode
= NULL
};
211 if (u
->local_tree_playout
) {
212 /* N.B.: We know this is ELO playout. */
213 playout_elo_callback(u
->playout
, uct_playout_probdist
, &upc
);
216 struct playout_setup ps
= { .gamelen
= u
->gamelen
, .mercymin
= u
->mercymin
};
217 int result
= play_random_game(&ps
, b
, next_color
,
218 u
->playout_amaf
? amaf
: NULL
,
219 &u
->ownermap
, u
->playout
);
220 if (next_color
== S_WHITE
) {
221 /* We need the result from black's perspective. */
225 fprintf(stderr
, "%s -- [%d..%d] %s random playout result %d\n",
226 spaces
, player_color
, next_color
, coord2sstr(n
->coord
, t
->board
), result
);
232 scale_value(struct uct
*u
, struct board
*b
, int result
)
234 float rval
= result
> 0;
236 int vp
= u
->val_points
;
238 vp
= board_size(b
) - 1; vp
*= vp
; vp
*= 2;
241 float sval
= (float) abs(result
) / vp
;
242 sval
= sval
> 1 ? 1 : sval
;
243 if (result
< 0) sval
= 1 - sval
;
245 rval
+= u
->val_scale
* sval
;
247 rval
= (1 - u
->val_scale
) * rval
+ u
->val_scale
* sval
;
248 // fprintf(stderr, "score %d => sval %f, rval %f\n", result, sval, rval);
254 record_local_sequence(struct uct
*u
, struct tree
*t
,
255 struct uct_descent
*descent
, int dlen
, int di
,
256 enum stone seq_color
, float rval
)
258 /* Ignore pass sequences. */
259 if (is_pass(descent
[di
].node
->coord
))
262 #define LTREE_DEBUG if (UDEBUGL(6))
263 LTREE_DEBUG
fprintf(stderr
, "recording result %f in local %s sequence: ",
264 rval
, stone2str(seq_color
));
267 /* Pick the right local tree root... */
268 struct tree_node
*lnode
= seq_color
== S_BLACK
? t
->ltree_black
: t
->ltree_white
;
271 /* ...and record the sequence. */
272 while (di
< dlen
&& (di
== di0
|| descent
[di
].node
->d
< u
->tenuki_d
)) {
273 LTREE_DEBUG
fprintf(stderr
, "%s[%d] ",
274 coord2sstr(descent
[di
].node
->coord
, t
->board
),
275 descent
[di
].node
->d
);
276 lnode
= tree_get_node(t
, lnode
, descent
[di
++].node
->coord
, true);
278 stats_add_result(&lnode
->u
, rval
, 1);
281 /* Add lnode for tenuki (pass) if we descended further. */
283 LTREE_DEBUG
fprintf(stderr
, "pass ");
284 lnode
= tree_get_node(t
, lnode
, pass
, true);
286 stats_add_result(&lnode
->u
, rval
, 1);
289 LTREE_DEBUG
fprintf(stderr
, "\n");
294 uct_playout(struct uct
*u
, struct board
*b
, enum stone player_color
, struct tree
*t
)
299 struct playout_amafmap
*amaf
= NULL
;
300 if (u
->policy
->wants_amaf
) {
301 amaf
= calloc2(1, sizeof(*amaf
));
302 amaf
->map
= calloc2(board_size2(&b2
) + 1, sizeof(*amaf
->map
));
303 amaf
->map
++; // -1 is pass
306 /* Walk the tree until we find a leaf, then expand it and do
307 * a random playout. */
308 struct tree_node
*n
= t
->root
;
309 enum stone node_color
= stone_other(player_color
);
310 assert(node_color
== t
->root_color
);
312 /* Tree descent history. */
313 /* XXX: This is somewhat messy since @n and descent[dlen-1].node are
316 struct uct_descent descent
[DLEN
];
317 descent
[0].node
= n
; descent
[0].lnode
= NULL
;
319 /* Total value of the sequence. */
320 struct move_stats seq_value
= { .playouts
= 0 };
323 int pass_limit
= (board_size(&b2
) - 2) * (board_size(&b2
) - 2) / 2;
324 int passes
= is_pass(b
->last_move
.coord
) && b
->moves
> 0;
328 static char spaces
[] = "\0 ";
331 fprintf(stderr
, "--- UCT walk with color %d\n", player_color
);
333 while (!tree_leaf_node(n
) && passes
< 2) {
334 spaces
[depth
++] = ' '; spaces
[depth
] = 0;
337 /*** Choose a node to descend to: */
339 /* Parity is chosen already according to the child color, since
340 * it is applied to children. */
341 node_color
= stone_other(node_color
);
342 int parity
= (node_color
== player_color
? 1 : -1);
345 descent
[dlen
] = descent
[dlen
- 1];
346 if (u
->local_tree
&& (!descent
[dlen
].lnode
|| descent
[dlen
].node
->d
>= u
->tenuki_d
)) {
347 /* Start new local sequence. */
348 /* Remember that node_color already holds color of the
349 * to-be-found child. */
350 descent
[dlen
].lnode
= node_color
== S_BLACK
? t
->ltree_black
: t
->ltree_white
;
353 if (!u
->random_policy_chance
|| fast_random(u
->random_policy_chance
))
354 u
->policy
->descend(u
->policy
, t
, &descent
[dlen
], parity
, b2
.moves
> pass_limit
);
356 u
->random_policy
->descend(u
->random_policy
, t
, &descent
[dlen
], parity
, b2
.moves
> pass_limit
);
359 /*** Perform the descent: */
361 seq_value
.playouts
+= descent
[dlen
].value
.playouts
;
362 seq_value
.value
+= descent
[dlen
].value
.value
* descent
[dlen
].value
.playouts
;
363 n
= descent
[dlen
++].node
;
364 assert(n
== t
->root
|| n
->parent
);
366 fprintf(stderr
, "%s+-- UCT sent us to [%s:%d] %f\n",
367 spaces
, coord2sstr(n
->coord
, t
->board
), n
->coord
,
368 tree_node_get_value(t
, parity
, n
->u
.value
));
370 /* Add virtual loss if we need to; this is used to discourage
371 * other threads from visiting this node in case of multiple
372 * threads doing the tree search. */
374 stats_add_result(&n
->u
, tree_parity(t
, parity
) > 0 ? 0 : 1, 1);
376 assert(n
->coord
>= -1);
377 if (amaf
&& !is_pass(n
->coord
))
378 record_amaf_move(amaf
, n
->coord
, node_color
);
380 struct move m
= { n
->coord
, node_color
};
381 int res
= board_play(&b2
, &m
);
383 if (res
< 0 || (!is_pass(m
.coord
) && !group_at(&b2
, m
.coord
)) /* suicide */
384 || b2
.superko_violation
) {
386 for (struct tree_node
*ni
= n
; ni
; ni
= ni
->parent
)
387 fprintf(stderr
, "%s<%"PRIhash
"> ", coord2sstr(ni
->coord
, t
->board
), ni
->hash
);
388 fprintf(stderr
, "marking invalid %s node %d,%d res %d group %d spk %d\n",
389 stone2str(node_color
), coord_x(n
->coord
,b
), coord_y(n
->coord
,b
),
390 res
, group_at(&b2
, m
.coord
), b2
.superko_violation
);
392 n
->hints
|= TREE_HINT_INVALID
;
397 if (is_pass(n
->coord
))
404 amaf
->game_baselen
= amaf
->gamelen
;
405 amaf
->record_nakade
= u
->playout_amaf_nakade
;
408 if (t
->use_extra_komi
&& u
->dynkomi
->persim
) {
409 b2
.komi
+= round(u
->dynkomi
->persim(u
->dynkomi
, &b2
, t
, n
));
413 /* XXX: No dead groups support. */
414 float score
= board_official_score(&b2
, NULL
);
415 /* Result from black's perspective (no matter who
416 * the player; black's perspective is always
417 * what the tree stores. */
418 result
= - (score
* 2);
421 fprintf(stderr
, "[%d..%d] %s p-p scoring playout result %d (W %f)\n",
422 player_color
, node_color
, coord2sstr(n
->coord
, t
->board
), result
, score
);
424 board_print(&b2
, stderr
);
426 board_ownermap_fill(&u
->ownermap
, &b2
);
428 } else { // assert(tree_leaf_node(n));
429 /* In case of parallel tree search, the assertion might
430 * not hold if two threads chew on the same node. */
431 result
= uct_leaf_node(u
, &b2
, player_color
, amaf
, t
, n
, node_color
, spaces
);
434 if (amaf
&& u
->playout_amaf_cutoff
) {
435 unsigned int cutoff
= amaf
->game_baselen
;
436 cutoff
+= (amaf
->gamelen
- amaf
->game_baselen
) * u
->playout_amaf_cutoff
/ 100;
437 /* Now, reconstruct the amaf map. */
438 memset(amaf
->map
, 0, board_size2(&b2
) * sizeof(*amaf
->map
));
439 for (unsigned int i
= 0; i
< cutoff
; i
++) {
440 coord_t coord
= amaf
->game
[i
].coord
;
441 enum stone color
= amaf
->game
[i
].color
;
442 if (amaf
->map
[coord
] == S_NONE
|| amaf
->map
[coord
] == color
) {
443 amaf
->map
[coord
] = color
;
444 /* Nakade always recorded for in-tree part */
445 } else if (amaf
->record_nakade
|| i
<= amaf
->game_baselen
) {
446 amaf_op(amaf
->map
[n
->coord
], +);
451 assert(n
== t
->root
|| n
->parent
);
453 float rval
= scale_value(u
, b
, result
);
454 u
->policy
->update(u
->policy
, t
, n
, node_color
, player_color
, amaf
, rval
);
456 if (t
->use_extra_komi
) {
457 stats_add_result(&u
->dynkomi
->score
, result
/ 2, 1);
458 stats_add_result(&u
->dynkomi
->value
, rval
, 1);
461 if (u
->local_tree
&& n
->parent
&& !is_pass(n
->coord
) && dlen
> 0) {
462 /* Possibly transform the rval appropriately. */
463 float expval
= seq_value
.value
/ seq_value
.playouts
;
464 rval
= stats_temper_value(rval
, expval
, u
->local_tree
);
466 /* Get the local sequences and record them in ltree. */
467 /* We will look for sequence starts in our descent
468 * history, then run record_local_sequence() for each
469 * found sequence start; record_local_sequence() may
470 * pick longer sequences from descent history then,
471 * which is expected as it will create new lnodes. */
472 enum stone seq_color
= player_color
;
473 /* First move always starts a sequence. */
474 record_local_sequence(u
, t
, descent
, dlen
, 1, seq_color
, rval
);
475 seq_color
= stone_other(seq_color
);
476 for (int dseqi
= 2; dseqi
< dlen
; dseqi
++, seq_color
= stone_other(seq_color
)) {
477 if (u
->local_tree_allseq
) {
478 /* We are configured to record all subsequences. */
479 record_local_sequence(u
, t
, descent
, dlen
, dseqi
, seq_color
, rval
);
482 if (descent
[dseqi
].node
->d
>= u
->tenuki_d
) {
483 /* Tenuki! Record the fresh sequence. */
484 record_local_sequence(u
, t
, descent
, dlen
, dseqi
, seq_color
, rval
);
487 if (descent
[dseqi
].lnode
&& !descent
[dseqi
].lnode
) {
488 /* Record result for in-descent picked sequence. */
489 record_local_sequence(u
, t
, descent
, dlen
, dseqi
, seq_color
, rval
);
497 /* We need to undo the virtual loss we added during descend. */
498 if (u
->virtual_loss
) {
499 int parity
= (node_color
== player_color
? 1 : -1);
500 for (; n
->parent
; n
= n
->parent
) {
501 stats_rm_result(&n
->u
, tree_parity(t
, parity
) > 0 ? 0 : 1, 1);
510 board_done_noalloc(&b2
);
515 uct_playouts(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
)
518 for (i
= 0; !uct_halt
; i
++)
519 uct_playout(u
, b
, color
, t
);