UCB1AMAF: set distance_rave=3 by default, adapt equiv_rave & cfgd accordingly
[pachi.git] / uct / policy / ucb1amaf.c
blobe3f377c14a4b604934e504ba9260d7ba35940e4f
1 #include <assert.h>
2 #include <limits.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "move.h"
11 #include "random.h"
12 #include "tactics/util.h"
13 #include "uct/internal.h"
14 #include "uct/tree.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. */
24 floating_t explore_p;
25 /* In distributed mode, encourage different slaves to work on different
26 * parts of the tree by adding virtual wins to different nodes. */
27 int virtual_win;
28 int root_virtual_win;
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. */
33 floating_t fpu;
34 unsigned int equiv_rave;
35 bool sylvain_rave;
36 /* Give more weight to moves played earlier. */
37 int distance_rave;
38 /* Coefficient of local tree values embedded in RAVE. */
39 floating_t ltree_rave;
40 /* Coefficient of criticality embedded in RAVE. */
41 floating_t crit_rave;
42 int crit_min_playouts;
43 floating_t crit_plthres_coef;
44 bool crit_negative;
45 bool crit_negflip;
46 bool crit_amaf;
47 bool crit_lvalue;
51 static inline floating_t fast_sqrt(unsigned int x)
53 static const floating_t table[] = {
54 0, 1, 1.41421356237309504880, 1.73205080756887729352,
55 2.00000000000000000000, 2.23606797749978969640,
56 2.44948974278317809819, 2.64575131106459059050,
57 2.82842712474619009760, 3.00000000000000000000,
58 3.16227766016837933199, 3.31662479035539984911,
59 3.46410161513775458705, 3.60555127546398929311,
60 3.74165738677394138558, 3.87298334620741688517,
61 4.00000000000000000000, 4.12310562561766054982,
62 4.24264068711928514640, 4.35889894354067355223,
63 4.47213595499957939281, 4.58257569495584000658,
64 4.69041575982342955456, 4.79583152331271954159,
65 4.89897948556635619639, 5.00000000000000000000,
66 5.09901951359278483002, 5.19615242270663188058,
67 5.29150262212918118100, 5.38516480713450403125,
68 5.47722557505166113456, 5.56776436283002192211,
69 5.65685424949238019520, 5.74456264653802865985,
70 5.83095189484530047087, 5.91607978309961604256,
71 6.00000000000000000000, 6.08276253029821968899,
72 6.16441400296897645025, 6.24499799839839820584,
73 6.32455532033675866399, 6.40312423743284868648,
74 6.48074069840786023096, 6.55743852430200065234,
75 6.63324958071079969822, 6.70820393249936908922,
76 6.78232998312526813906, 6.85565460040104412493,
77 6.92820323027550917410, 7.00000000000000000000,
78 7.07106781186547524400, 7.14142842854284999799,
79 7.21110255092797858623, 7.28010988928051827109,
80 7.34846922834953429459, 7.41619848709566294871,
81 7.48331477354788277116, 7.54983443527074969723,
82 7.61577310586390828566, 7.68114574786860817576,
83 7.74596669241483377035, 7.81024967590665439412,
84 7.87400787401181101968, 7.93725393319377177150,
86 if (x < sizeof(table) / sizeof(*table)) {
87 return table[x];
88 } else {
89 return sqrt(x);
93 #define URAVE_DEBUG if (0)
94 static floating_t inline
95 ucb1rave_evaluate(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity)
97 struct ucb1_policy_amaf *b = p->data;
98 struct tree_node *node = descent->node;
99 struct tree_node *lnode = descent->lnode;
101 struct move_stats n = node->u, r = node->amaf;
102 if (p->uct->amaf_prior) {
103 stats_merge(&r, &node->prior);
104 } else {
105 stats_merge(&n, &node->prior);
108 /* Local tree heuristics. */
109 assert(!lnode || lnode->parent);
110 if (p->uct->local_tree && b->ltree_rave > 0 && lnode
111 && (p->uct->local_tree_rootchoose || lnode->parent->parent)) {
112 struct move_stats l = lnode->u;
113 l.playouts = ((floating_t) l.playouts) * b->ltree_rave / LTREE_PLAYOUTS_MULTIPLIER;
114 URAVE_DEBUG fprintf(stderr, "[ltree] adding [%s] %f%%%d to [%s] RAVE %f%%%d\n",
115 coord2sstr(node_coord(lnode), tree->board), l.value, l.playouts,
116 coord2sstr(node_coord(node), tree->board), r.value, r.playouts);
117 stats_merge(&r, &l);
120 /* Criticality heuristics. */
121 if (b->crit_rave > 0 && (b->crit_plthres_coef > 0
122 ? node->u.playouts > tree->root->u.playouts * b->crit_plthres_coef
123 : node->u.playouts > b->crit_min_playouts)) {
124 floating_t crit = tree_node_criticality(tree, node);
125 if (b->crit_negative || crit > 0) {
126 floating_t val = 1.0f;
127 if (b->crit_negflip && crit < 0) {
128 val = 0;
129 crit = -crit;
131 struct move_stats c = {
132 .value = tree_node_get_value(tree, parity, val),
133 .playouts = crit * r.playouts * b->crit_rave
135 URAVE_DEBUG fprintf(stderr, "[crit] adding %f%%%d to [%s] RAVE %f%%%d\n",
136 c.value, c.playouts,
137 coord2sstr(node_coord(node), tree->board), r.value, r.playouts);
138 stats_merge(&r, &c);
143 floating_t value = 0;
144 if (n.playouts) {
145 if (r.playouts) {
146 /* At the beginning, beta is at 1 and RAVE is used.
147 * At b->equiv_rate, beta is at 1/3 and gets steeper on. */
148 floating_t beta;
149 if (b->sylvain_rave) {
150 beta = (floating_t) r.playouts / (r.playouts + n.playouts
151 + (floating_t) n.playouts * r.playouts / b->equiv_rave);
152 } else {
153 /* XXX: This can be cached in descend; but we don't use this by default. */
154 beta = sqrt(b->equiv_rave / (3 * node->parent->u.playouts + b->equiv_rave));
157 value = beta * r.value + (1.f - beta) * n.value;
158 URAVE_DEBUG fprintf(stderr, "\t%s value = %f * %f + (1 - %f) * %f (prior %f)\n",
159 coord2sstr(node_coord(node), tree->board), beta, r.value, beta, n.value, node->prior.value);
160 } else {
161 value = n.value;
162 URAVE_DEBUG fprintf(stderr, "\t%s value = %f (prior %f)\n",
163 coord2sstr(node_coord(node), tree->board), n.value, node->prior.value);
165 } else if (r.playouts) {
166 value = r.value;
167 URAVE_DEBUG fprintf(stderr, "\t%s value = rave %f (prior %f)\n",
168 coord2sstr(node_coord(node), tree->board), r.value, node->prior.value);
170 descent->value.playouts = r.playouts + n.playouts;
171 descent->value.value = value;
172 return tree_node_get_value(tree, parity, value);
175 void
176 ucb1rave_descend(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity, bool allow_pass)
178 struct ucb1_policy_amaf *b = p->data;
179 floating_t nconf = 1.f;
180 if (b->explore_p > 0)
181 nconf = sqrt(log(descent->node->u.playouts + descent->node->prior.playouts));
182 struct uct *u = p->uct;
183 int vwin = 0;
184 if (u->max_slaves > 0 && u->slave_index >= 0)
185 vwin = descent->node == tree->root ? b->root_virtual_win : b->virtual_win;
186 int child = 0;
188 uctd_try_node_children(tree, descent, allow_pass, parity, u->tenuki_d, di, urgency) {
189 struct tree_node *ni = di.node;
190 urgency = ucb1rave_evaluate(p, tree, &di, parity);
192 /* In distributed mode, encourage different slaves to work on different
193 * parts of the tree. We rely on the fact that children (if they exist)
194 * are the same and in the same order in all slaves. */
195 if (vwin > 0 && ni->u.playouts > b->vwin_min_playouts && (child - u->slave_index) % u->max_slaves == 0)
196 urgency += vwin / (ni->u.playouts + vwin);
198 if (ni->u.playouts > 0 && b->explore_p > 0) {
199 urgency += b->explore_p * nconf / fast_sqrt(ni->u.playouts);
201 } else if (ni->u.playouts + ni->amaf.playouts + ni->prior.playouts == 0) {
202 /* assert(!u->even_eqex); */
203 urgency = b->fpu;
205 } uctd_set_best_child(di, urgency);
207 uctd_get_best_child(descent);
211 void
212 ucb1amaf_update(struct uct_policy *p, struct tree *tree, struct tree_node *node,
213 enum stone node_color, enum stone player_color,
214 struct playout_amafmap *map, struct board *final_board,
215 floating_t result)
217 struct ucb1_policy_amaf *b = p->data;
218 enum stone winner_color = result > 0.5 ? S_BLACK : S_WHITE;
220 /* Record of the random playout - for each intersection coord,
221 * first_move[coord] is the index map->game of the first move
222 * at this coordinate, or INT_MAX if the move was not played.
223 * The parity gives the color of this move.
225 int first_map[board_size2(final_board)+1];
226 int *first_move = &first_map[1]; // +1 for pass
228 #if 0
229 struct board bb; bb.size = 9+2;
230 for (struct tree_node *ni = node; ni; ni = ni->parent)
231 fprintf(stderr, "%s ", coord2sstr(node_coord(ni), &bb));
232 fprintf(stderr, "[color %d] update result %d (color %d)\n",
233 node_color, result, player_color);
234 #endif
236 /* Initialize first_move */
237 for (int i = pass; i < board_size2(final_board); i++) first_move[i] = INT_MAX;
238 int move;
239 assert(map->gamelen > 0);
240 for (move = map->gamelen - 1; move >= map->game_baselen; move--)
241 first_move[map->game[move]] = move;
243 while (node) {
244 if (!b->crit_amaf && !is_pass(node_coord(node))) {
245 stats_add_result(&node->winner_owner, board_local_value(b->crit_lvalue, final_board, node_coord(node), winner_color), 1);
246 stats_add_result(&node->black_owner, board_local_value(b->crit_lvalue, final_board, node_coord(node), S_BLACK), 1);
248 stats_add_result(&node->u, result, 1);
250 /* This loop ignores symmetry considerations, but they should
251 * matter only at a point when AMAF doesn't help much. */
252 assert(map->game_baselen >= 0);
253 for (struct tree_node *ni = node->children; ni; ni = ni->sibling) {
254 if (is_pass(node_coord(ni))) continue;
256 /* Use the child move only if it was first played by the same color. */
257 int first = first_move[node_coord(ni)];
258 if (first == INT_MAX) continue;
259 assert(first > move && first < map->gamelen);
260 int distance = first - (move + 1);
261 if (distance & 1) continue;
263 /* Give more weight to moves played earlier */
264 int weight = 1;
265 if (b->distance_rave != 0) {
266 weight += b->distance_rave * (map->gamelen - first) / (map->gamelen - move);
268 stats_add_result(&ni->amaf, result, weight);
270 if (b->crit_amaf) {
271 stats_add_result(&ni->winner_owner, board_local_value(b->crit_lvalue, final_board, node_coord(ni), winner_color), 1);
272 stats_add_result(&ni->black_owner, board_local_value(b->crit_lvalue, final_board, node_coord(ni), S_BLACK), 1);
274 #if 0
275 struct board bb; bb.size = 9+2;
276 fprintf(stderr, "* %s<%"PRIhash"> -> %s<%"PRIhash"> [%d/%f => %d/%f]\n",
277 coord2sstr(node_coord(node), &bb), node->hash,
278 coord2sstr(node_coord(ni), &bb), ni->hash,
279 player_color, result, move, res);
280 #endif
282 if (node->parent) {
283 assert(move >= 0 && map->game[move] == node_coord(node) && first_move[node_coord(node)] > move);
284 first_move[node_coord(node)] = move;
285 move--;
287 node = node->parent;
292 struct uct_policy *
293 policy_ucb1amaf_init(struct uct *u, char *arg, struct board *board)
295 struct uct_policy *p = calloc2(1, sizeof(*p));
296 struct ucb1_policy_amaf *b = calloc2(1, sizeof(*b));
297 p->uct = u;
298 p->data = b;
299 p->choose = uctp_generic_choose;
300 p->winner = uctp_generic_winner;
301 p->evaluate = ucb1rave_evaluate;
302 p->descend = ucb1rave_descend;
303 p->update = ucb1amaf_update;
304 p->wants_amaf = true;
306 b->explore_p = 0;
307 b->equiv_rave = board_large(board) ? 4000 : 3000;
308 b->fpu = INFINITY;
309 b->sylvain_rave = true;
310 b->distance_rave = 3;
311 b->ltree_rave = 0.75f;
313 b->crit_rave = 1.1f;
314 b->crit_min_playouts = 2000;
315 b->crit_negative = 1;
316 b->crit_amaf = 0;
318 b->root_virtual_win = -1;
319 b->vwin_min_playouts = 1000;
321 if (arg) {
322 char *optspec, *next = arg;
323 while (*next) {
324 optspec = next;
325 next += strcspn(next, ":");
326 if (*next) { *next++ = 0; } else { *next = 0; }
328 char *optname = optspec;
329 char *optval = strchr(optspec, '=');
330 if (optval) *optval++ = 0;
332 if (!strcasecmp(optname, "explore_p")) {
333 b->explore_p = atof(optval);
334 } else if (!strcasecmp(optname, "fpu") && optval) {
335 b->fpu = atof(optval);
336 } else if (!strcasecmp(optname, "equiv_rave") && optval) {
337 b->equiv_rave = atof(optval);
338 } else if (!strcasecmp(optname, "sylvain_rave")) {
339 b->sylvain_rave = !optval || *optval == '1';
340 } else if (!strcasecmp(optname, "distance_rave") && optval) {
341 b->distance_rave = atoi(optval);
342 } else if (!strcasecmp(optname, "ltree_rave") && optval) {
343 b->ltree_rave = atof(optval);
344 } else if (!strcasecmp(optname, "crit_rave") && optval) {
345 b->crit_rave = atof(optval);
346 } else if (!strcasecmp(optname, "crit_min_playouts") && optval) {
347 b->crit_min_playouts = atoi(optval);
348 } else if (!strcasecmp(optname, "crit_plthres_coef") && optval) {
349 b->crit_plthres_coef = atof(optval);
350 } else if (!strcasecmp(optname, "crit_negative")) {
351 b->crit_negative = !optval || *optval == '1';
352 } else if (!strcasecmp(optname, "crit_negflip")) {
353 b->crit_negflip = !optval || *optval == '1';
354 } else if (!strcasecmp(optname, "crit_amaf")) {
355 b->crit_amaf = !optval || *optval == '1';
356 } else if (!strcasecmp(optname, "crit_lvalue")) {
357 b->crit_lvalue = !optval || *optval == '1';
358 } else if (!strcasecmp(optname, "virtual_win") && optval) {
359 b->virtual_win = atoi(optval);
360 } else if (!strcasecmp(optname, "root_virtual_win") && optval) {
361 b->root_virtual_win = atoi(optval);
362 } else if (!strcasecmp(optname, "vwin_min_playouts") && optval) {
363 b->vwin_min_playouts = atoi(optval);
364 } else {
365 fprintf(stderr, "ucb1amaf: Invalid policy argument %s or missing value\n",
366 optname);
367 exit(1);
371 if (b->root_virtual_win < 0)
372 b->root_virtual_win = b->virtual_win;
374 return p;