17 #include "tactics/util.h"
18 #include "uct/dynkomi.h"
19 #include "uct/internal.h"
20 #include "uct/search.h"
25 #define DESCENT_DLEN 512
28 uct_progress_status(struct uct
*u
, struct tree
*t
, enum stone color
, int playouts
)
34 struct tree_node
*best
= u
->policy
->choose(u
->policy
, t
->root
, t
->board
, color
, resign
);
36 fprintf(stderr
, "... No moves left\n");
39 fprintf(stderr
, "[%d] ", playouts
);
40 fprintf(stderr
, "best %f ", tree_node_get_value(t
, 1, best
->u
.value
));
43 if (t
->use_extra_komi
)
44 fprintf(stderr
, "komi %.1f ", t
->extra_komi
);
47 fprintf(stderr
, "| seq ");
48 for (int depth
= 0; depth
< 4; depth
++) {
49 if (best
&& best
->u
.playouts
>= 25) {
50 fprintf(stderr
, "%3s ", coord2sstr(node_coord(best
), t
->board
));
51 best
= u
->policy
->choose(u
->policy
, best
, t
->board
, color
, resign
);
58 fprintf(stderr
, "| can ");
60 struct tree_node
*can
[cans
];
61 memset(can
, 0, sizeof(can
));
62 best
= t
->root
->children
;
65 while ((!can
[c
] || best
->u
.playouts
> can
[c
]->u
.playouts
) && ++c
< cans
);
66 for (int d
= 0; d
< c
; d
++) can
[d
] = can
[d
+ 1];
67 if (c
> 0) can
[c
- 1] = best
;
72 fprintf(stderr
, "%3s(%.3f) ",
73 coord2sstr(node_coord(can
[cans
]), t
->board
),
74 tree_node_get_value(t
, 1, can
[cans
]->u
.value
));
80 fprintf(stderr
, "\n");
85 record_amaf_move(struct playout_amafmap
*amaf
, coord_t coord
)
87 assert(amaf
->gamelen
< MAX_GAMELEN
);
88 amaf
->game
[amaf
->gamelen
++] = coord
;
92 struct uct_playout_callback
{
95 struct tree_node
*lnode
;
100 uct_playout_hook(struct playout_policy
*playout
, struct playout_setup
*setup
, struct board
*b
, enum stone color
, int mode
)
102 /* XXX: This is used in some non-master branches. */
107 uct_playout_prepolicy(struct playout_policy
*playout
, struct playout_setup
*setup
, struct board
*b
, enum stone color
)
109 return uct_playout_hook(playout
, setup
, b
, color
, 0);
113 uct_playout_postpolicy(struct playout_policy
*playout
, struct playout_setup
*setup
, struct board
*b
, enum stone color
)
115 return uct_playout_hook(playout
, setup
, b
, color
, 1);
120 uct_leaf_node(struct uct
*u
, struct board
*b
, enum stone player_color
,
121 struct playout_amafmap
*amaf
,
122 struct uct_descent
*descent
, int *dlen
,
123 struct tree_node
*significant
[2],
124 struct tree
*t
, struct tree_node
*n
, enum stone node_color
,
127 enum stone next_color
= stone_other(node_color
);
128 int parity
= (next_color
== player_color
? 1 : -1);
131 fprintf(stderr
, "%s*-- UCT playout #%d start [%s] %f\n",
132 spaces
, n
->u
.playouts
, coord2sstr(node_coord(n
), t
->board
),
133 tree_node_get_value(t
, parity
, n
->u
.value
));
135 struct uct_playout_callback upc
= {
138 /* TODO: Don't necessarily restart the sequence walk when
139 * entering playout. */
143 struct playout_setup ps
= {
144 .gamelen
= u
->gamelen
,
145 .mercymin
= u
->mercymin
,
146 .prepolicy_hook
= uct_playout_prepolicy
,
147 .postpolicy_hook
= uct_playout_postpolicy
,
150 int result
= play_random_game(&ps
, b
, next_color
,
151 u
->playout_amaf
? amaf
: NULL
,
152 &u
->ownermap
, u
->playout
);
153 if (next_color
== S_WHITE
) {
154 /* We need the result from black's perspective. */
158 fprintf(stderr
, "%s -- [%d..%d] %s random playout result %d\n",
159 spaces
, player_color
, next_color
, coord2sstr(node_coord(n
), t
->board
), result
);
165 scale_value(struct uct
*u
, struct board
*b
, int result
)
167 floating_t rval
= result
> 0 ? 1.0 : result
< 0 ? 0.0 : 0.5;
168 if (u
->val_scale
&& result
!= 0) {
169 int vp
= u
->val_points
;
171 vp
= board_size(b
) - 1; vp
*= vp
; vp
*= 2;
174 floating_t sval
= (floating_t
) abs(result
) / vp
;
175 sval
= sval
> 1 ? 1 : sval
;
176 if (result
< 0) sval
= 1 - sval
;
178 rval
+= u
->val_scale
* sval
;
180 rval
= (1 - u
->val_scale
) * rval
+ u
->val_scale
* sval
;
181 // fprintf(stderr, "score %d => sval %f, rval %f\n", result, sval, rval);
187 local_value(struct uct
*u
, struct board
*b
, coord_t coord
, enum stone color
)
189 /* Tactical evaluation of move @coord by color @color, given
190 * simulation end position @b. I.e., a move is tactically good
191 * if the resulting group stays on board until the game end. */
192 /* We can also take into account surrounding stones, e.g. to
193 * encourage taking off external liberties during a semeai. */
194 double val
= board_local_value(u
->local_tree_neival
, b
, coord
, color
);
195 return (color
== S_WHITE
) ? 1.f
- val
: val
;
199 record_local_sequence(struct uct
*u
, struct tree
*t
, struct board
*endb
,
200 struct uct_descent
*descent
, int dlen
, int di
,
201 enum stone seq_color
)
203 #define LTREE_DEBUG if (UDEBUGL(6))
205 /* Ignore pass sequences. */
206 if (is_pass(node_coord(descent
[di
].node
)))
209 LTREE_DEBUG
board_print(endb
, stderr
);
210 LTREE_DEBUG
fprintf(stderr
, "recording local %s sequence: ",
211 stone2str(seq_color
));
213 /* Sequences starting deeper are less relevant in general. */
214 int pval
= LTREE_PLAYOUTS_MULTIPLIER
;
215 if (u
->local_tree
&& u
->local_tree_depth_decay
> 0)
216 pval
= ((floating_t
) pval
) / pow(u
->local_tree_depth_decay
, di
- 1);
218 LTREE_DEBUG
fprintf(stderr
, "too deep @%d\n", di
);
222 /* Pick the right local tree root... */
223 struct tree_node
*lnode
= seq_color
== S_BLACK
? t
->ltree_black
: t
->ltree_white
;
226 /* ...determine the sequence value... */
228 if (u
->local_tree_eval
!= LTE_EACH
) {
229 sval
= local_value(u
, endb
, node_coord(descent
[di
].node
), seq_color
);
230 LTREE_DEBUG
fprintf(stderr
, "(goal %s[%s %1.3f][%d]) ",
231 coord2sstr(node_coord(descent
[di
].node
), t
->board
),
232 stone2str(seq_color
), sval
, descent
[di
].node
->d
);
234 if (u
->local_tree_eval
== LTE_TOTAL
) {
236 while (di
< dlen
&& (di
== di0
|| descent
[di
].node
->d
< u
->tenuki_d
)) {
237 enum stone color
= (di
- di0
) % 2 ? stone_other(seq_color
) : seq_color
;
238 double rval
= local_value(u
, endb
, node_coord(descent
[di
].node
), color
);
244 sval
/= (di
- di0
+ 1);
249 /* ...and record the sequence. */
251 while (di
< dlen
&& !is_pass(node_coord(descent
[di
].node
))
252 && (di
== di0
|| descent
[di
].node
->d
< u
->tenuki_d
)) {
253 enum stone color
= (di
- di0
) % 2 ? stone_other(seq_color
) : seq_color
;
255 if (u
->local_tree_eval
!= LTE_EACH
)
258 rval
= local_value(u
, endb
, node_coord(descent
[di
].node
), color
);
259 LTREE_DEBUG
fprintf(stderr
, "%s[%s %1.3f][%d] ",
260 coord2sstr(node_coord(descent
[di
].node
), t
->board
),
261 stone2str(color
), rval
, descent
[di
].node
->d
);
262 lnode
= tree_get_node(t
, lnode
, node_coord(descent
[di
++].node
), true);
264 stats_add_result(&lnode
->u
, rval
, pval
);
267 /* Add lnode for tenuki (pass) if we descended further. */
269 double rval
= u
->local_tree_eval
!= LTE_EACH
? sval
: 0.5;
270 LTREE_DEBUG
fprintf(stderr
, "pass ");
271 lnode
= tree_get_node(t
, lnode
, pass
, true);
273 stats_add_result(&lnode
->u
, rval
, pval
);
276 LTREE_DEBUG
fprintf(stderr
, "\n");
281 uct_playout(struct uct
*u
, struct board
*b
, enum stone player_color
, struct tree
*t
)
286 struct playout_amafmap amaf
;
287 amaf
.gamelen
= amaf
.game_baselen
= 0;
289 /* Walk the tree until we find a leaf, then expand it and do
290 * a random playout. */
291 struct tree_node
*n
= t
->root
;
292 enum stone node_color
= stone_other(player_color
);
293 assert(node_color
== t
->root_color
);
295 /* Make sure the root node is expanded. */
296 if (tree_leaf_node(n
) && !__sync_lock_test_and_set(&n
->is_expanded
, 1))
297 tree_expand_node(t
, n
, &b2
, player_color
, u
, 1);
299 /* Tree descent history. */
300 /* XXX: This is somewhat messy since @n and descent[dlen-1].node are
302 struct uct_descent descent
[DESCENT_DLEN
];
303 descent
[0].node
= n
; descent
[0].lnode
= NULL
;
305 /* Total value of the sequence. */
306 struct move_stats seq_value
= { .playouts
= 0 };
307 /* The last "significant" node along the descent (i.e. node
308 * with higher than configured number of playouts). For black
310 struct tree_node
*significant
[2] = { NULL
, NULL
};
311 if (n
->u
.playouts
>= u
->significant_threshold
)
312 significant
[node_color
- 1] = n
;
315 int pass_limit
= (board_size(&b2
) - 2) * (board_size(&b2
) - 2) / 2;
316 int passes
= is_pass(b
->last_move
.coord
) && b
->moves
> 0;
319 static char spaces
[] = "\0 ";
322 fprintf(stderr
, "--- UCT walk with color %d\n", player_color
);
324 while (!tree_leaf_node(n
) && passes
< 2) {
325 spaces
[dlen
- 1] = ' '; spaces
[dlen
] = 0;
328 /*** Choose a node to descend to: */
330 /* Parity is chosen already according to the child color, since
331 * it is applied to children. */
332 node_color
= stone_other(node_color
);
333 int parity
= (node_color
== player_color
? 1 : -1);
335 assert(dlen
< DESCENT_DLEN
);
336 descent
[dlen
] = descent
[dlen
- 1];
337 if (u
->local_tree
&& (!descent
[dlen
].lnode
|| descent
[dlen
].node
->d
>= u
->tenuki_d
)) {
338 /* Start new local sequence. */
339 /* Remember that node_color already holds color of the
340 * to-be-found child. */
341 descent
[dlen
].lnode
= node_color
== S_BLACK
? t
->ltree_black
: t
->ltree_white
;
344 if (!u
->random_policy_chance
|| fast_random(u
->random_policy_chance
))
345 u
->policy
->descend(u
->policy
, t
, &descent
[dlen
], parity
, b2
.moves
> pass_limit
);
347 u
->random_policy
->descend(u
->random_policy
, t
, &descent
[dlen
], parity
, b2
.moves
> pass_limit
);
350 /*** Perform the descent: */
352 if (descent
[dlen
].node
->u
.playouts
>= u
->significant_threshold
) {
353 significant
[node_color
- 1] = descent
[dlen
].node
;
356 seq_value
.playouts
+= descent
[dlen
].value
.playouts
;
357 seq_value
.value
+= descent
[dlen
].value
.value
* descent
[dlen
].value
.playouts
;
358 n
= descent
[dlen
++].node
;
359 assert(n
== t
->root
|| n
->parent
);
361 fprintf(stderr
, "%s+-- UCT sent us to [%s:%d] %d,%f\n",
362 spaces
, coord2sstr(node_coord(n
), t
->board
),
363 node_coord(n
), n
->u
.playouts
,
364 tree_node_get_value(t
, parity
, n
->u
.value
));
366 /* Add virtual loss if we need to; this is used to discourage
367 * other threads from visiting this node in case of multiple
368 * threads doing the tree search. */
370 stats_add_result(&n
->u
, node_color
== S_BLACK
? 0.0 : 1.0, u
->virtual_loss
);
372 assert(node_coord(n
) >= -1);
373 record_amaf_move(&amaf
, node_coord(n
));
375 struct move m
= { node_coord(n
), node_color
};
376 int res
= board_play(&b2
, &m
);
378 if (res
< 0 || (!is_pass(m
.coord
) && !group_at(&b2
, m
.coord
)) /* suicide */
379 || b2
.superko_violation
) {
381 for (struct tree_node
*ni
= n
; ni
; ni
= ni
->parent
)
382 fprintf(stderr
, "%s<%"PRIhash
"> ", coord2sstr(node_coord(ni
), t
->board
), ni
->hash
);
383 fprintf(stderr
, "marking invalid %s node %d,%d res %d group %d spk %d\n",
384 stone2str(node_color
), coord_x(node_coord(n
),b
), coord_y(node_coord(n
),b
),
385 res
, group_at(&b2
, m
.coord
), b2
.superko_violation
);
387 n
->hints
|= TREE_HINT_INVALID
;
392 if (is_pass(node_coord(n
)))
397 enum stone next_color
= stone_other(node_color
);
398 /* We need to make sure only one thread expands the node. If
399 * we are unlucky enough for two threads to meet in the same
400 * node, the latter one will simply do another simulation from
401 * the node itself, no big deal. t->nodes_size may exceed
402 * the maximum in multi-threaded case but not by much so it's ok.
403 * The size test must be before the test&set not after, to allow
404 * expansion of the node later if enough nodes have been freed. */
405 if (tree_leaf_node(n
)
406 && n
->u
.playouts
- u
->virtual_loss
>= u
->expand_p
&& t
->nodes_size
< u
->max_tree_size
407 && !__sync_lock_test_and_set(&n
->is_expanded
, 1))
408 tree_expand_node(t
, n
, &b2
, next_color
, u
, -parity
);
411 amaf
.game_baselen
= amaf
.gamelen
;
413 if (t
->use_extra_komi
&& u
->dynkomi
->persim
) {
414 b2
.komi
+= round(u
->dynkomi
->persim(u
->dynkomi
, &b2
, t
, n
));
418 /* XXX: No dead groups support. */
419 floating_t score
= board_official_score(&b2
, NULL
);
420 /* Result from black's perspective (no matter who
421 * the player; black's perspective is always
422 * what the tree stores. */
423 result
= - (score
* 2);
426 fprintf(stderr
, "[%d..%d] %s p-p scoring playout result %d (W %f)\n",
427 player_color
, node_color
, coord2sstr(node_coord(n
), t
->board
), result
, score
);
429 board_print(&b2
, stderr
);
431 board_ownermap_fill(&u
->ownermap
, &b2
);
433 } else { // assert(tree_leaf_node(n));
434 /* In case of parallel tree search, the assertion might
435 * not hold if two threads chew on the same node. */
436 result
= uct_leaf_node(u
, &b2
, player_color
, &amaf
, descent
, &dlen
, significant
, t
, n
, node_color
, spaces
);
439 if (u
->policy
->wants_amaf
&& u
->playout_amaf_cutoff
) {
440 unsigned int cutoff
= amaf
.game_baselen
;
441 cutoff
+= (amaf
.gamelen
- amaf
.game_baselen
) * u
->playout_amaf_cutoff
/ 100;
442 amaf
.gamelen
= cutoff
;
445 /* Record the result. */
447 assert(n
== t
->root
|| n
->parent
);
448 floating_t rval
= scale_value(u
, b
, result
);
449 u
->policy
->update(u
->policy
, t
, n
, node_color
, player_color
, &amaf
, &b2
, rval
);
451 if (t
->use_extra_komi
) {
452 stats_add_result(&u
->dynkomi
->score
, result
/ 2, 1);
453 stats_add_result(&u
->dynkomi
->value
, rval
, 1);
456 if (u
->local_tree
&& n
->parent
&& !is_pass(node_coord(n
)) && dlen
> 0) {
457 /* Get the local sequences and record them in ltree. */
458 /* We will look for sequence starts in our descent
459 * history, then run record_local_sequence() for each
460 * found sequence start; record_local_sequence() may
461 * pick longer sequences from descent history then,
462 * which is expected as it will create new lnodes. */
463 enum stone seq_color
= player_color
;
464 /* First move always starts a sequence. */
465 record_local_sequence(u
, t
, &b2
, descent
, dlen
, 1, seq_color
);
466 seq_color
= stone_other(seq_color
);
467 for (int dseqi
= 2; dseqi
< dlen
; dseqi
++, seq_color
= stone_other(seq_color
)) {
468 if (u
->local_tree_allseq
) {
469 /* We are configured to record all subsequences. */
470 record_local_sequence(u
, t
, &b2
, descent
, dlen
, dseqi
, seq_color
);
473 if (descent
[dseqi
].node
->d
>= u
->tenuki_d
) {
474 /* Tenuki! Record the fresh sequence. */
475 record_local_sequence(u
, t
, &b2
, descent
, dlen
, dseqi
, seq_color
);
478 if (descent
[dseqi
].lnode
&& !descent
[dseqi
].lnode
) {
479 /* Record result for in-descent picked sequence. */
480 record_local_sequence(u
, t
, &b2
, descent
, dlen
, dseqi
, seq_color
);
487 /* We need to undo the virtual loss we added during descend. */
488 if (u
->virtual_loss
) {
489 floating_t loss
= node_color
== S_BLACK
? 0.0 : 1.0;
490 for (; n
->parent
; n
= n
->parent
) {
491 stats_rm_result(&n
->u
, loss
, u
->virtual_loss
);
496 board_done_noalloc(&b2
);
501 uct_playouts(struct uct
*u
, struct board
*b
, enum stone color
, struct tree
*t
, struct time_info
*ti
)
504 if (ti
&& ti
->dim
== TD_GAMES
) {
505 for (i
= 0; t
->root
->u
.playouts
<= ti
->len
.games
&& !uct_halt
; i
++)
506 uct_playout(u
, b
, color
, t
);
508 for (i
= 0; !uct_halt
; i
++)
509 uct_playout(u
, b
, color
, t
);