Merge branch 'master' into win2
[pachi/t.git] / uct / policy / ucb1amaf.c
blobfb0e9e9647873ae1256c316c50dfe60930df4007
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "board.h"
8 #include "debug.h"
9 #include "move.h"
10 #include "random.h"
11 #include "uct/internal.h"
12 #include "uct/tree.h"
13 #include "uct/policy/generic.h"
15 /* This implements the UCB1 policy with an extra AMAF heuristics. */
17 struct ucb1_policy_amaf {
18 /* This is what the Modification of UCT with Patterns in Monte Carlo Go
19 * paper calls 'p'. Original UCB has this on 2, but this seems to
20 * produce way too wide searches; reduce this to get deeper and
21 * narrower readouts - try 0.2. */
22 floating_t explore_p;
23 /* In distributed mode, encourage different slaves to work on different
24 * parts of the tree by adding virtual wins to different nodes. */
25 int virtual_win;
26 int root_virtual_win;
27 int vwin_min_playouts;
28 /* First Play Urgency - if set to less than infinity (the MoGo paper
29 * above reports 1.0 as the best), new branches are explored only
30 * if none of the existing ones has higher urgency than fpu. */
31 floating_t fpu;
32 unsigned int equiv_rave;
33 bool check_nakade;
34 bool sylvain_rave;
35 /* Coefficient of local tree values embedded in RAVE. */
36 floating_t ltree_rave;
37 /* Coefficient of criticality embedded in RAVE. */
38 floating_t crit_rave;
39 int crit_min_playouts;
40 bool crit_negative;
41 bool crit_amaf;
45 static inline floating_t fast_sqrt(unsigned int x)
47 static const floating_t table[] = {
48 0, 1, 1.41421356237309504880, 1.73205080756887729352,
49 2.00000000000000000000, 2.23606797749978969640,
50 2.44948974278317809819, 2.64575131106459059050,
51 2.82842712474619009760, 3.00000000000000000000,
52 3.16227766016837933199, 3.31662479035539984911,
53 3.46410161513775458705, 3.60555127546398929311,
54 3.74165738677394138558, 3.87298334620741688517,
55 4.00000000000000000000, 4.12310562561766054982,
56 4.24264068711928514640, 4.35889894354067355223,
57 4.47213595499957939281, 4.58257569495584000658,
58 4.69041575982342955456, 4.79583152331271954159,
59 4.89897948556635619639, 5.00000000000000000000,
60 5.09901951359278483002, 5.19615242270663188058,
61 5.29150262212918118100, 5.38516480713450403125,
62 5.47722557505166113456, 5.56776436283002192211,
63 5.65685424949238019520, 5.74456264653802865985,
64 5.83095189484530047087, 5.91607978309961604256,
65 6.00000000000000000000, 6.08276253029821968899,
66 6.16441400296897645025, 6.24499799839839820584,
67 6.32455532033675866399, 6.40312423743284868648,
68 6.48074069840786023096, 6.55743852430200065234,
69 6.63324958071079969822, 6.70820393249936908922,
70 6.78232998312526813906, 6.85565460040104412493,
71 6.92820323027550917410, 7.00000000000000000000,
72 7.07106781186547524400, 7.14142842854284999799,
73 7.21110255092797858623, 7.28010988928051827109,
74 7.34846922834953429459, 7.41619848709566294871,
75 7.48331477354788277116, 7.54983443527074969723,
76 7.61577310586390828566, 7.68114574786860817576,
77 7.74596669241483377035, 7.81024967590665439412,
78 7.87400787401181101968, 7.93725393319377177150,
80 if (x < sizeof(table) / sizeof(*table)) {
81 return table[x];
82 } else {
83 return sqrt(x);
87 #define URAVE_DEBUG if (0)
88 static floating_t inline
89 ucb1rave_evaluate(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity)
91 struct ucb1_policy_amaf *b = p->data;
92 struct tree_node *node = descent->node;
93 struct tree_node *lnode = descent->lnode;
95 struct move_stats n = node->u, r = node->amaf;
96 if (p->uct->amaf_prior) {
97 stats_merge(&r, &node->prior);
98 } else {
99 stats_merge(&n, &node->prior);
102 /* Local tree heuristics. */
103 assert(!lnode || lnode->parent);
104 if (p->uct->local_tree && b->ltree_rave > 0 && lnode
105 && (p->uct->local_tree_rootchoose || lnode->parent->parent)) {
106 struct move_stats l = lnode->u;
107 l.playouts = ((floating_t) l.playouts) * b->ltree_rave / LTREE_PLAYOUTS_MULTIPLIER;
108 URAVE_DEBUG fprintf(stderr, "[ltree] adding [%s] %f%%%d to [%s] RAVE %f%%%d\n",
109 coord2sstr(lnode->coord, tree->board), l.value, l.playouts,
110 coord2sstr(node->coord, tree->board), r.value, r.playouts);
111 stats_merge(&r, &l);
114 /* Criticality heuristics. */
115 if (b->crit_rave > 0 && node->u.playouts > b->crit_min_playouts) {
116 floating_t crit = tree_node_criticality(tree, node);
117 if (b->crit_negative || crit > 0) {
118 struct move_stats c = {
119 .value = tree_node_get_value(tree, parity, 1.0f),
120 .playouts = crit * r.playouts * b->crit_rave
122 URAVE_DEBUG fprintf(stderr, "[crit] adding %f%%%d to [%s] RAVE %f%%%d\n",
123 c.value, c.playouts,
124 coord2sstr(node->coord, tree->board), r.value, r.playouts);
125 stats_merge(&r, &c);
130 floating_t value = 0;
131 if (n.playouts) {
132 if (r.playouts) {
133 /* At the beginning, beta is at 1 and RAVE is used.
134 * At b->equiv_rate, beta is at 1/3 and gets steeper on. */
135 floating_t beta;
136 if (b->sylvain_rave) {
137 beta = (floating_t) r.playouts / (r.playouts + n.playouts
138 + (floating_t) n.playouts * r.playouts / b->equiv_rave);
139 } else {
140 /* XXX: This can be cached in descend; but we don't use this by default. */
141 beta = sqrt(b->equiv_rave / (3 * node->parent->u.playouts + b->equiv_rave));
144 value = beta * r.value + (1.f - beta) * n.value;
145 URAVE_DEBUG fprintf(stderr, "\t%s value = %f * %f + (1 - %f) * %f (prior %f)\n",
146 coord2sstr(node->coord, tree->board), beta, r.value, beta, n.value, node->prior.value);
147 } else {
148 value = n.value;
149 URAVE_DEBUG fprintf(stderr, "\t%s value = %f (prior %f)\n",
150 coord2sstr(node->coord, tree->board), n.value, node->prior.value);
152 } else if (r.playouts) {
153 value = r.value;
154 URAVE_DEBUG fprintf(stderr, "\t%s value = rave %f (prior %f)\n",
155 coord2sstr(node->coord, tree->board), r.value, node->prior.value);
157 descent->value.playouts = r.playouts + n.playouts;
158 descent->value.value = value;
159 return tree_node_get_value(tree, parity, value);
162 void
163 ucb1rave_descend(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity, bool allow_pass)
165 struct ucb1_policy_amaf *b = p->data;
166 floating_t nconf = 1.f;
167 if (b->explore_p > 0)
168 nconf = sqrt(log(descent->node->u.playouts + descent->node->prior.playouts));
169 struct uct *u = p->uct;
170 int vwin = 0;
171 if (u->max_slaves > 0 && u->slave_index >= 0)
172 vwin = descent->node == tree->root ? b->root_virtual_win : b->virtual_win;
173 int child = 0;
175 uctd_try_node_children(tree, descent, allow_pass, parity, u->tenuki_d, di, urgency) {
176 struct tree_node *ni = di.node;
177 urgency = ucb1rave_evaluate(p, tree, &di, parity);
179 /* In distributed mode, encourage different slaves to work on different
180 * parts of the tree. We rely on the fact that children (if they exist)
181 * are the same and in the same order in all slaves. */
182 if (vwin > 0 && ni->u.playouts > b->vwin_min_playouts && (child - u->slave_index) % u->max_slaves == 0)
183 urgency += vwin / (ni->u.playouts + vwin);
185 if (ni->u.playouts > 0 && b->explore_p > 0) {
186 urgency += b->explore_p * nconf / fast_sqrt(ni->u.playouts);
188 } else if (ni->u.playouts + ni->amaf.playouts + ni->prior.playouts == 0) {
189 /* assert(!u->even_eqex); */
190 urgency = b->fpu;
192 } uctd_set_best_child(di, urgency);
194 uctd_get_best_child(descent);
198 void
199 ucb1amaf_update(struct uct_policy *p, struct tree *tree, struct tree_node *node,
200 enum stone node_color, enum stone player_color,
201 struct playout_amafmap *map, struct board *final_board,
202 floating_t result)
204 struct ucb1_policy_amaf *b = p->data;
205 enum stone winner_color = result > 0.5 ? S_BLACK : S_WHITE;
206 enum stone child_color = stone_other(node_color);
208 #if 0
209 struct board bb; bb.size = 9+2;
210 for (struct tree_node *ni = node; ni; ni = ni->parent)
211 fprintf(stderr, "%s ", coord2sstr(ni->coord, &bb));
212 fprintf(stderr, "[color %d] update result %d (color %d)\n",
213 node_color, result, player_color);
214 #endif
216 while (node) {
217 if (node->parent == NULL)
218 assert(tree->root_color == stone_other(child_color));
220 if (!b->crit_amaf && !is_pass(node->coord)) {
221 stats_add_result(&node->winner_owner, board_at(final_board, node->coord) == winner_color ? 1.0 : 0.0, 1);
222 stats_add_result(&node->black_owner, board_at(final_board, node->coord) == S_BLACK ? 1.0 : 0.0, 1);
224 stats_add_result(&node->u, result, 1);
225 if (amaf_nakade(map->map[node->coord]))
226 amaf_op(map->map[node->coord], -);
228 /* This loop ignores symmetry considerations, but they should
229 * matter only at a point when AMAF doesn't help much. */
230 assert(map->game_baselen >= 0);
231 for (struct tree_node *ni = node->children; ni; ni = ni->sibling) {
232 enum stone amaf_color = map->map[ni->coord];
233 assert(amaf_color != S_OFFBOARD);
234 if (amaf_color == S_NONE)
235 continue;
236 if (amaf_nakade(map->map[ni->coord])) {
237 if (!b->check_nakade)
238 continue;
239 unsigned int i;
240 for (i = map->game_baselen; i < map->gamelen; i++)
241 if (map->game[i].coord == ni->coord
242 && map->game[i].color == child_color)
243 break;
244 if (i == map->gamelen)
245 continue;
246 amaf_color = child_color;
249 floating_t nres = result;
250 if (amaf_color != child_color) {
251 continue;
253 /* For child_color != player_color, we still want
254 * to record the result unmodified; in that case,
255 * we will correctly negate them at the descend phase. */
257 if (b->crit_amaf && !is_pass(node->coord)) {
258 stats_add_result(&ni->winner_owner, board_at(final_board, ni->coord) == winner_color ? 1.0 : 0.0, 1);
259 stats_add_result(&ni->black_owner, board_at(final_board, ni->coord) == S_BLACK ? 1.0 : 0.0, 1);
261 stats_add_result(&ni->amaf, nres, 1);
263 #if 0
264 struct board bb; bb.size = 9+2;
265 fprintf(stderr, "* %s<%"PRIhash"> -> %s<%"PRIhash"> [%d/%f => %d/%f]\n",
266 coord2sstr(node->coord, &bb), node->hash,
267 coord2sstr(ni->coord, &bb), ni->hash,
268 player_color, result, child_color, nres);
269 #endif
272 if (!is_pass(node->coord)) {
273 map->game_baselen--;
275 node = node->parent; child_color = stone_other(child_color);
280 struct uct_policy *
281 policy_ucb1amaf_init(struct uct *u, char *arg)
283 struct uct_policy *p = calloc2(1, sizeof(*p));
284 struct ucb1_policy_amaf *b = calloc2(1, sizeof(*b));
285 p->uct = u;
286 p->data = b;
287 p->choose = uctp_generic_choose;
288 p->winner = uctp_generic_winner;
289 p->evaluate = ucb1rave_evaluate;
290 p->descend = ucb1rave_descend;
291 p->update = ucb1amaf_update;
292 p->wants_amaf = true;
294 b->explore_p = 0; // 0.02 can be also good on 19x19 with prior=eqex=40
295 b->equiv_rave = 3000;
296 b->fpu = INFINITY;
297 b->check_nakade = true;
298 b->sylvain_rave = true;
299 b->ltree_rave = 0.75f;
301 b->crit_rave = 1.0f;
302 b->crit_min_playouts = 2000;
303 b->crit_negative = 1;
304 b->crit_amaf = 0;
306 b->root_virtual_win = -1;
307 b->vwin_min_playouts = 1000;
309 if (arg) {
310 char *optspec, *next = arg;
311 while (*next) {
312 optspec = next;
313 next += strcspn(next, ":");
314 if (*next) { *next++ = 0; } else { *next = 0; }
316 char *optname = optspec;
317 char *optval = strchr(optspec, '=');
318 if (optval) *optval++ = 0;
320 if (!strcasecmp(optname, "explore_p")) {
321 b->explore_p = atof(optval);
322 } else if (!strcasecmp(optname, "fpu") && optval) {
323 b->fpu = atof(optval);
324 } else if (!strcasecmp(optname, "equiv_rave") && optval) {
325 b->equiv_rave = atof(optval);
326 } else if (!strcasecmp(optname, "sylvain_rave")) {
327 b->sylvain_rave = !optval || *optval == '1';
328 } else if (!strcasecmp(optname, "check_nakade")) {
329 b->check_nakade = !optval || *optval == '1';
330 } else if (!strcasecmp(optname, "ltree_rave") && optval) {
331 b->ltree_rave = atof(optval);
332 } else if (!strcasecmp(optname, "crit_rave") && optval) {
333 b->crit_rave = atof(optval);
334 } else if (!strcasecmp(optname, "crit_min_playouts") && optval) {
335 b->crit_min_playouts = atoi(optval);
336 } else if (!strcasecmp(optname, "crit_negative")) {
337 b->crit_negative = !optval || *optval == '1';
338 } else if (!strcasecmp(optname, "crit_amaf")) {
339 b->crit_amaf = !optval || *optval == '1';
340 } else if (!strcasecmp(optname, "virtual_win") && optval) {
341 b->virtual_win = atoi(optval);
342 } else if (!strcasecmp(optname, "root_virtual_win") && optval) {
343 b->root_virtual_win = atoi(optval);
344 } else if (!strcasecmp(optname, "vwin_min_playouts") && optval) {
345 b->vwin_min_playouts = atoi(optval);
346 } else {
347 fprintf(stderr, "ucb1amaf: Invalid policy argument %s or missing value\n",
348 optname);
349 exit(1);
353 if (b->root_virtual_win < 0)
354 b->root_virtual_win = b->virtual_win;
356 return p;