12 #include "tactics/util.h"
13 #include "uct/internal.h"
15 #include "uct/policy/generic.h"
17 /* This implements the UCB1 policy with an extra AMAF heuristics. */
19 struct ucb1_policy_amaf
{
20 /* This is what the Modification of UCT with Patterns in Monte Carlo Go
21 * paper calls 'p'. Original UCB has this on 2, but this seems to
22 * produce way too wide searches; reduce this to get deeper and
23 * narrower readouts - try 0.2. */
25 /* In distributed mode, encourage different slaves to work on different
26 * parts of the tree by adding virtual wins to different nodes. */
29 int vwin_min_playouts
;
30 /* First Play Urgency - if set to less than infinity (the MoGo paper
31 * above reports 1.0 as the best), new branches are explored only
32 * if none of the existing ones has higher urgency than fpu. */
34 unsigned int equiv_rave
;
36 /* Give more weight to moves played earlier. */
38 /* Coefficient of local tree values embedded in RAVE. */
39 floating_t ltree_rave
;
40 /* Coefficient of criticality embedded in RAVE. */
42 int crit_min_playouts
;
43 floating_t crit_plthres_coef
;
48 /* Rosin-based bias with pattern information. */
50 floating_t rosin_p
, rosin_rp
;
51 bool rosin_shift
; /* t'=t+2 for monotonous sqrt() */
55 static inline floating_t
fast_sqrt(unsigned int x
)
57 static const floating_t table
[] = {
58 0, 1, 1.41421356237309504880, 1.73205080756887729352,
59 2.00000000000000000000, 2.23606797749978969640,
60 2.44948974278317809819, 2.64575131106459059050,
61 2.82842712474619009760, 3.00000000000000000000,
62 3.16227766016837933199, 3.31662479035539984911,
63 3.46410161513775458705, 3.60555127546398929311,
64 3.74165738677394138558, 3.87298334620741688517,
65 4.00000000000000000000, 4.12310562561766054982,
66 4.24264068711928514640, 4.35889894354067355223,
67 4.47213595499957939281, 4.58257569495584000658,
68 4.69041575982342955456, 4.79583152331271954159,
69 4.89897948556635619639, 5.00000000000000000000,
70 5.09901951359278483002, 5.19615242270663188058,
71 5.29150262212918118100, 5.38516480713450403125,
72 5.47722557505166113456, 5.56776436283002192211,
73 5.65685424949238019520, 5.74456264653802865985,
74 5.83095189484530047087, 5.91607978309961604256,
75 6.00000000000000000000, 6.08276253029821968899,
76 6.16441400296897645025, 6.24499799839839820584,
77 6.32455532033675866399, 6.40312423743284868648,
78 6.48074069840786023096, 6.55743852430200065234,
79 6.63324958071079969822, 6.70820393249936908922,
80 6.78232998312526813906, 6.85565460040104412493,
81 6.92820323027550917410, 7.00000000000000000000,
82 7.07106781186547524400, 7.14142842854284999799,
83 7.21110255092797858623, 7.28010988928051827109,
84 7.34846922834953429459, 7.41619848709566294871,
85 7.48331477354788277116, 7.54983443527074969723,
86 7.61577310586390828566, 7.68114574786860817576,
87 7.74596669241483377035, 7.81024967590665439412,
88 7.87400787401181101968, 7.93725393319377177150,
90 if (x
< sizeof(table
) / sizeof(*table
)) {
97 #define URAVE_DEBUG if (0)
98 static floating_t
inline
99 ucb1rave_evaluate(struct uct_policy
*p
, struct tree
*tree
, struct uct_descent
*descent
, int parity
)
101 struct ucb1_policy_amaf
*b
= p
->data
;
102 struct tree_node
*node
= descent
->node
;
103 struct tree_node
*lnode
= descent
->lnode
;
105 struct move_stats n
= node
->u
, r
= node
->amaf
;
106 if (p
->uct
->amaf_prior
) {
107 stats_merge(&r
, &node
->prior
);
109 stats_merge(&n
, &node
->prior
);
112 /* Local tree heuristics. */
113 assert(!lnode
|| lnode
->parent
);
114 if (p
->uct
->local_tree
&& b
->ltree_rave
> 0 && lnode
115 && (p
->uct
->local_tree_rootchoose
|| lnode
->parent
->parent
)) {
116 struct move_stats l
= lnode
->u
;
117 l
.playouts
= ((floating_t
) l
.playouts
) * b
->ltree_rave
/ LTREE_PLAYOUTS_MULTIPLIER
;
118 URAVE_DEBUG
fprintf(stderr
, "[ltree] adding [%s] %f%%%d to [%s] RAVE %f%%%d\n",
119 coord2sstr(node_coord(lnode
), tree
->board
), l
.value
, l
.playouts
,
120 coord2sstr(node_coord(node
), tree
->board
), r
.value
, r
.playouts
);
124 /* Criticality heuristics. */
125 if (b
->crit_rave
> 0 && (b
->crit_plthres_coef
> 0
126 ? node
->u
.playouts
> tree
->root
->u
.playouts
* b
->crit_plthres_coef
127 : node
->u
.playouts
> b
->crit_min_playouts
)) {
128 floating_t crit
= tree_node_criticality(tree
, node
);
129 if (b
->crit_negative
|| crit
> 0) {
130 floating_t val
= 1.0f
;
131 if (b
->crit_negflip
&& crit
< 0) {
135 struct move_stats c
= {
136 .value
= tree_node_get_value(tree
, parity
, val
),
137 .playouts
= crit
* r
.playouts
* b
->crit_rave
139 URAVE_DEBUG
fprintf(stderr
, "[crit] adding %f%%%d to [%s] RAVE %f%%%d\n",
141 coord2sstr(node_coord(node
), tree
->board
), r
.value
, r
.playouts
);
147 floating_t value
= 0;
150 /* At the beginning, beta is at 1 and RAVE is used.
151 * At b->equiv_rate, beta is at 1/3 and gets steeper on. */
153 if (b
->sylvain_rave
) {
154 beta
= (floating_t
) r
.playouts
/ (r
.playouts
+ n
.playouts
155 + (floating_t
) n
.playouts
* r
.playouts
/ b
->equiv_rave
);
157 /* XXX: This can be cached in descend; but we don't use this by default. */
158 beta
= sqrt(b
->equiv_rave
/ (3 * node
->parent
->u
.playouts
+ b
->equiv_rave
));
161 value
= beta
* r
.value
+ (1.f
- beta
) * n
.value
;
162 URAVE_DEBUG
fprintf(stderr
, "\t%s value = %f * %f + (1 - %f) * %f (prior %f)\n",
163 coord2sstr(node_coord(node
), tree
->board
), beta
, r
.value
, beta
, n
.value
, node
->prior
.value
);
166 URAVE_DEBUG
fprintf(stderr
, "\t%s value = %f (prior %f)\n",
167 coord2sstr(node_coord(node
), tree
->board
), n
.value
, node
->prior
.value
);
169 } else if (r
.playouts
) {
171 URAVE_DEBUG
fprintf(stderr
, "\t%s value = rave %f (prior %f)\n",
172 coord2sstr(node_coord(node
), tree
->board
), r
.value
, node
->prior
.value
);
175 descent
->value
.playouts
= r
.playouts
+ n
.playouts
;
176 descent
->value
.value
= value
;
177 return tree_node_get_value(tree
, parity
, value
);
181 ucb1rave_descend(struct uct_policy
*p
, struct tree
*tree
, struct uct_descent
*descent
, int parity
, bool allow_pass
)
183 struct ucb1_policy_amaf
*b
= p
->data
;
184 floating_t nconf
= 1.f
, rconf
= 1.f
;
185 if (b
->explore_p
> 0)
186 nconf
= sqrt(log(descent
->node
->u
.playouts
+ descent
->node
->prior
.playouts
));
188 unsigned int rplayouts
= descent
->node
->u
.playouts
;
189 if (b
->rosin_shift
) rplayouts
+= 2;
190 rconf
= b
->rosin_p
* (rplayouts
> 1 ? sqrt(log(b
->rosin_rp
* rplayouts
)/rplayouts
) : 1.f
);
192 struct uct
*u
= p
->uct
;
194 if (u
->max_slaves
> 0 && u
->slave_index
>= 0)
195 vwin
= descent
->node
== tree
->root
? b
->root_virtual_win
: b
->virtual_win
;
198 uctd_try_node_children(tree
, descent
, allow_pass
, parity
, u
->tenuki_d
, di
, urgency
) {
199 struct tree_node
*ni
= di
.node
;
200 urgency
= ucb1rave_evaluate(p
, tree
, &di
, parity
);
202 /* In distributed mode, encourage different slaves to work on different
203 * parts of the tree. We rely on the fact that children (if they exist)
204 * are the same and in the same order in all slaves. */
205 if (vwin
> 0 && ni
->u
.playouts
> b
->vwin_min_playouts
&& (child
- u
->slave_index
) % u
->max_slaves
== 0)
206 urgency
+= vwin
/ (ni
->u
.playouts
+ vwin
);
209 floating_t rosinval
= ni
->rosinval
* rconf
;
210 // fprintf(stderr, "\t\t%s (%.3f) - Rosin %.5f (i %.5f)\n", coord2sstr(ni->coord, tree->board), urgency, rosinval, ni->rosinval);
214 if (ni
->u
.playouts
> 0 && b
->explore_p
> 0) {
215 urgency
+= b
->explore_p
* nconf
/ fast_sqrt(ni
->u
.playouts
);
217 } else if (ni
->u
.playouts
+ ni
->amaf
.playouts
+ ni
->prior
.playouts
== 0) {
218 /* assert(!u->even_eqex); */
221 } uctd_set_best_child(di
, urgency
);
223 uctd_get_best_child(descent
);
228 ucb1amaf_update(struct uct_policy
*p
, struct tree
*tree
, struct tree_node
*node
,
229 enum stone node_color
, enum stone player_color
,
230 struct playout_amafmap
*map
, struct board
*final_board
,
233 struct ucb1_policy_amaf
*b
= p
->data
;
234 enum stone winner_color
= result
> 0.5 ? S_BLACK
: S_WHITE
;
236 /* Record of the random playout - for each intersection coord,
237 * first_move[coord] is the index map->game of the first move
238 * at this coordinate, or INT_MAX if the move was not played.
239 * The parity gives the color of this move.
241 int first_map
[board_size2(final_board
)+1];
242 int *first_move
= &first_map
[1]; // +1 for pass
245 struct board bb
; bb
.size
= 9+2;
246 for (struct tree_node
*ni
= node
; ni
; ni
= ni
->parent
)
247 fprintf(stderr
, "%s ", coord2sstr(node_coord(ni
), &bb
));
248 fprintf(stderr
, "[color %d] update result %d (color %d)\n",
249 node_color
, result
, player_color
);
252 /* Initialize first_move */
253 for (int i
= pass
; i
< board_size2(final_board
); i
++) first_move
[i
] = INT_MAX
;
255 assert(map
->gamelen
> 0);
256 for (move
= map
->gamelen
- 1; move
>= map
->game_baselen
; move
--)
257 first_move
[map
->game
[move
]] = move
;
260 if (!b
->crit_amaf
&& !is_pass(node_coord(node
))) {
261 stats_add_result(&node
->winner_owner
, board_local_value(b
->crit_lvalue
, final_board
, node_coord(node
), winner_color
), 1);
262 stats_add_result(&node
->black_owner
, board_local_value(b
->crit_lvalue
, final_board
, node_coord(node
), S_BLACK
), 1);
264 stats_add_result(&node
->u
, result
, 1);
266 /* This loop ignores symmetry considerations, but they should
267 * matter only at a point when AMAF doesn't help much. */
268 assert(map
->game_baselen
>= 0);
269 for (struct tree_node
*ni
= node
->children
; ni
; ni
= ni
->sibling
) {
270 if (is_pass(node_coord(ni
))) continue;
272 /* Use the child move only if it was first played by the same color. */
273 int first
= first_move
[node_coord(ni
)];
274 if (first
== INT_MAX
) continue;
275 assert(first
> move
&& first
< map
->gamelen
);
276 int distance
= first
- (move
+ 1);
277 if (distance
& 1) continue;
279 /* Give more weight to moves played earlier */
281 if (b
->distance_rave
!= 0) {
282 weight
+= b
->distance_rave
* (map
->gamelen
- first
) / (map
->gamelen
- move
);
284 stats_add_result(&ni
->amaf
, result
, weight
);
287 stats_add_result(&ni
->winner_owner
, board_local_value(b
->crit_lvalue
, final_board
, node_coord(ni
), winner_color
), 1);
288 stats_add_result(&ni
->black_owner
, board_local_value(b
->crit_lvalue
, final_board
, node_coord(ni
), S_BLACK
), 1);
291 struct board bb
; bb
.size
= 9+2;
292 fprintf(stderr
, "* %s<%"PRIhash
"> -> %s<%"PRIhash
"> [%d/%f => %d/%f]\n",
293 coord2sstr(node_coord(node
), &bb
), node
->hash
,
294 coord2sstr(node_coord(ni
), &bb
), ni
->hash
,
295 player_color
, result
, move
, res
);
299 assert(move
>= 0 && map
->game
[move
] == node_coord(node
) && first_move
[node_coord(node
)] > move
);
300 first_move
[node_coord(node
)] = move
;
309 policy_ucb1amaf_init(struct uct
*u
, char *arg
, struct board
*board
)
311 struct uct_policy
*p
= calloc2(1, sizeof(*p
));
312 struct ucb1_policy_amaf
*b
= calloc2(1, sizeof(*b
));
315 p
->choose
= uctp_generic_choose
;
316 p
->winner
= uctp_generic_winner
;
317 p
->evaluate
= ucb1rave_evaluate
;
318 p
->descend
= ucb1rave_descend
;
319 p
->update
= ucb1amaf_update
;
320 p
->wants_amaf
= true;
323 b
->equiv_rave
= board_large(board
) ? 4000 : 3000;
325 b
->sylvain_rave
= true;
326 b
->distance_rave
= 3;
327 b
->ltree_rave
= 0.75f
;
330 b
->crit_min_playouts
= 2000;
331 b
->crit_negative
= 1;
334 b
->root_virtual_win
= -1;
335 b
->vwin_min_playouts
= 1000;
341 char *optspec
, *next
= arg
;
344 next
+= strcspn(next
, ":");
345 if (*next
) { *next
++ = 0; } else { *next
= 0; }
347 char *optname
= optspec
;
348 char *optval
= strchr(optspec
, '=');
349 if (optval
) *optval
++ = 0;
351 if (!strcasecmp(optname
, "explore_p")) {
352 b
->explore_p
= atof(optval
);
353 } else if (!strcasecmp(optname
, "fpu") && optval
) {
354 b
->fpu
= atof(optval
);
355 } else if (!strcasecmp(optname
, "equiv_rave") && optval
) {
356 b
->equiv_rave
= atof(optval
);
357 } else if (!strcasecmp(optname
, "sylvain_rave")) {
358 b
->sylvain_rave
= !optval
|| *optval
== '1';
359 } else if (!strcasecmp(optname
, "distance_rave") && optval
) {
360 b
->distance_rave
= atoi(optval
);
361 } else if (!strcasecmp(optname
, "ltree_rave") && optval
) {
362 b
->ltree_rave
= atof(optval
);
363 } else if (!strcasecmp(optname
, "crit_rave") && optval
) {
364 b
->crit_rave
= atof(optval
);
365 } else if (!strcasecmp(optname
, "crit_min_playouts") && optval
) {
366 b
->crit_min_playouts
= atoi(optval
);
367 } else if (!strcasecmp(optname
, "crit_plthres_coef") && optval
) {
368 b
->crit_plthres_coef
= atof(optval
);
369 } else if (!strcasecmp(optname
, "crit_negative")) {
370 b
->crit_negative
= !optval
|| *optval
== '1';
371 } else if (!strcasecmp(optname
, "crit_negflip")) {
372 b
->crit_negflip
= !optval
|| *optval
== '1';
373 } else if (!strcasecmp(optname
, "crit_amaf")) {
374 b
->crit_amaf
= !optval
|| *optval
== '1';
375 } else if (!strcasecmp(optname
, "crit_lvalue")) {
376 b
->crit_lvalue
= !optval
|| *optval
== '1';
377 } else if (!strcasecmp(optname
, "virtual_win") && optval
) {
378 b
->virtual_win
= atoi(optval
);
379 } else if (!strcasecmp(optname
, "root_virtual_win") && optval
) {
380 b
->root_virtual_win
= atoi(optval
);
381 } else if (!strcasecmp(optname
, "vwin_min_playouts") && optval
) {
382 b
->vwin_min_playouts
= atoi(optval
);
383 } else if (!strcasecmp(optname
, "rosin")) {
384 b
->rosin
= !optval
|| *optval
== '1';
386 u
->want_rosinval
= true;
387 } else if (!strcasecmp(optname
, "rosin_p") && optval
) {
388 b
->rosin_p
= atof(optval
);
389 } else if (!strcasecmp(optname
, "rosin_shift")) {
390 b
->rosin_shift
= !optval
|| *optval
== '1';
392 fprintf(stderr
, "ucb1amaf: Invalid policy argument %s or missing value\n",
398 if (b
->root_virtual_win
< 0)
399 b
->root_virtual_win
= b
->virtual_win
;