Give 0 or negative rave bonus to ko threats before taking the ko.
[pachi/t.git] / uct / policy / ucb1amaf.c
blob12498c02d25365ed792207e6e163407c8bb00692
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 /* Give 0 or negative rave bonus to ko threats before taking the ko.
39 0=no bonus, 1=invert rave bonus, 2=double penalty, etc... */
40 int threat_rave;
41 /* Coefficient of local tree values embedded in RAVE. */
42 floating_t ltree_rave;
43 /* Coefficient of criticality embedded in RAVE. */
44 floating_t crit_rave;
45 int crit_min_playouts;
46 floating_t crit_plthres_coef;
47 bool crit_negative;
48 bool crit_negflip;
49 bool crit_amaf;
50 bool crit_lvalue;
54 static inline floating_t fast_sqrt(unsigned int x)
56 static const floating_t table[] = {
57 0, 1, 1.41421356237309504880, 1.73205080756887729352,
58 2.00000000000000000000, 2.23606797749978969640,
59 2.44948974278317809819, 2.64575131106459059050,
60 2.82842712474619009760, 3.00000000000000000000,
61 3.16227766016837933199, 3.31662479035539984911,
62 3.46410161513775458705, 3.60555127546398929311,
63 3.74165738677394138558, 3.87298334620741688517,
64 4.00000000000000000000, 4.12310562561766054982,
65 4.24264068711928514640, 4.35889894354067355223,
66 4.47213595499957939281, 4.58257569495584000658,
67 4.69041575982342955456, 4.79583152331271954159,
68 4.89897948556635619639, 5.00000000000000000000,
69 5.09901951359278483002, 5.19615242270663188058,
70 5.29150262212918118100, 5.38516480713450403125,
71 5.47722557505166113456, 5.56776436283002192211,
72 5.65685424949238019520, 5.74456264653802865985,
73 5.83095189484530047087, 5.91607978309961604256,
74 6.00000000000000000000, 6.08276253029821968899,
75 6.16441400296897645025, 6.24499799839839820584,
76 6.32455532033675866399, 6.40312423743284868648,
77 6.48074069840786023096, 6.55743852430200065234,
78 6.63324958071079969822, 6.70820393249936908922,
79 6.78232998312526813906, 6.85565460040104412493,
80 6.92820323027550917410, 7.00000000000000000000,
81 7.07106781186547524400, 7.14142842854284999799,
82 7.21110255092797858623, 7.28010988928051827109,
83 7.34846922834953429459, 7.41619848709566294871,
84 7.48331477354788277116, 7.54983443527074969723,
85 7.61577310586390828566, 7.68114574786860817576,
86 7.74596669241483377035, 7.81024967590665439412,
87 7.87400787401181101968, 7.93725393319377177150,
89 if (x < sizeof(table) / sizeof(*table)) {
90 return table[x];
91 } else {
92 return sqrt(x);
96 #define URAVE_DEBUG if (0)
97 static floating_t inline
98 ucb1rave_evaluate(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity)
100 struct ucb1_policy_amaf *b = p->data;
101 struct tree_node *node = descent->node;
102 struct tree_node *lnode = descent->lnode;
104 struct move_stats n = node->u, r = node->amaf;
105 if (p->uct->amaf_prior) {
106 stats_merge(&r, &node->prior);
107 } else {
108 stats_merge(&n, &node->prior);
111 /* Local tree heuristics. */
112 assert(!lnode || lnode->parent);
113 if (p->uct->local_tree && b->ltree_rave > 0 && lnode
114 && (p->uct->local_tree_rootchoose || lnode->parent->parent)) {
115 struct move_stats l = lnode->u;
116 l.playouts = ((floating_t) l.playouts) * b->ltree_rave / LTREE_PLAYOUTS_MULTIPLIER;
117 URAVE_DEBUG fprintf(stderr, "[ltree] adding [%s] %f%%%d to [%s] RAVE %f%%%d\n",
118 coord2sstr(node_coord(lnode), tree->board), l.value, l.playouts,
119 coord2sstr(node_coord(node), tree->board), r.value, r.playouts);
120 stats_merge(&r, &l);
123 /* Criticality heuristics. */
124 if (b->crit_rave > 0 && (b->crit_plthres_coef > 0
125 ? node->u.playouts > tree->root->u.playouts * b->crit_plthres_coef
126 : node->u.playouts > b->crit_min_playouts)) {
127 floating_t crit = tree_node_criticality(tree, node);
128 if (b->crit_negative || crit > 0) {
129 floating_t val = 1.0f;
130 if (b->crit_negflip && crit < 0) {
131 val = 0;
132 crit = -crit;
134 struct move_stats c = {
135 .value = tree_node_get_value(tree, parity, val),
136 .playouts = crit * r.playouts * b->crit_rave
138 URAVE_DEBUG fprintf(stderr, "[crit] adding %f%%%d to [%s] RAVE %f%%%d\n",
139 c.value, c.playouts,
140 coord2sstr(node_coord(node), tree->board), r.value, r.playouts);
141 stats_merge(&r, &c);
146 floating_t value = 0;
147 if (n.playouts) {
148 if (r.playouts) {
149 /* At the beginning, beta is at 1 and RAVE is used.
150 * At b->equiv_rate, beta is at 1/3 and gets steeper on. */
151 floating_t beta;
152 if (b->sylvain_rave) {
153 beta = (floating_t) r.playouts / (r.playouts + n.playouts
154 + (floating_t) n.playouts * r.playouts / b->equiv_rave);
155 } else {
156 /* XXX: This can be cached in descend; but we don't use this by default. */
157 beta = sqrt(b->equiv_rave / (3 * node->parent->u.playouts + b->equiv_rave));
160 value = beta * r.value + (1.f - beta) * n.value;
161 URAVE_DEBUG fprintf(stderr, "\t%s value = %f * %f + (1 - %f) * %f (prior %f)\n",
162 coord2sstr(node_coord(node), tree->board), beta, r.value, beta, n.value, node->prior.value);
163 } else {
164 value = n.value;
165 URAVE_DEBUG fprintf(stderr, "\t%s value = %f (prior %f)\n",
166 coord2sstr(node_coord(node), tree->board), n.value, node->prior.value);
168 } else if (r.playouts) {
169 value = r.value;
170 URAVE_DEBUG fprintf(stderr, "\t%s value = rave %f (prior %f)\n",
171 coord2sstr(node_coord(node), tree->board), r.value, node->prior.value);
173 descent->value.playouts = r.playouts + n.playouts;
174 descent->value.value = value;
175 return tree_node_get_value(tree, parity, value);
178 void
179 ucb1rave_descend(struct uct_policy *p, struct tree *tree, struct uct_descent *descent, int parity, bool allow_pass)
181 struct ucb1_policy_amaf *b = p->data;
182 floating_t nconf = 1.f;
183 if (b->explore_p > 0)
184 nconf = sqrt(log(descent->node->u.playouts + descent->node->prior.playouts));
185 struct uct *u = p->uct;
186 int vwin = 0;
187 if (u->max_slaves > 0 && u->slave_index >= 0)
188 vwin = descent->node == tree->root ? b->root_virtual_win : b->virtual_win;
189 int child = 0;
191 uctd_try_node_children(tree, descent, allow_pass, parity, u->tenuki_d, di, urgency) {
192 struct tree_node *ni = di.node;
193 urgency = ucb1rave_evaluate(p, tree, &di, parity);
195 /* In distributed mode, encourage different slaves to work on different
196 * parts of the tree. We rely on the fact that children (if they exist)
197 * are the same and in the same order in all slaves. */
198 if (vwin > 0 && ni->u.playouts > b->vwin_min_playouts && (child - u->slave_index) % u->max_slaves == 0)
199 urgency += vwin / (ni->u.playouts + vwin);
201 if (ni->u.playouts > 0 && b->explore_p > 0) {
202 urgency += b->explore_p * nconf / fast_sqrt(ni->u.playouts);
204 } else if (ni->u.playouts + ni->amaf.playouts + ni->prior.playouts == 0) {
205 /* assert(!u->even_eqex); */
206 urgency = b->fpu;
208 } uctd_set_best_child(di, urgency);
210 uctd_get_best_child(descent);
214 void
215 ucb1amaf_update(struct uct_policy *p, struct tree *tree, struct tree_node *node,
216 enum stone node_color, enum stone player_color,
217 struct playout_amafmap *map, struct board *final_board,
218 floating_t result)
220 struct ucb1_policy_amaf *b = p->data;
221 enum stone winner_color = result > 0.5 ? S_BLACK : S_WHITE;
223 /* Record of the random playout - for each intersection coord,
224 * first_move[coord] is the index map->game of the first move
225 * at this coordinate, or INT_MAX if the move was not played.
226 * The parity gives the color of this move.
228 int first_map[board_size2(final_board)+1];
229 int *first_move = &first_map[1]; // +1 for pass
231 #if 0
232 struct board bb; bb.size = 9+2;
233 for (struct tree_node *ni = node; ni; ni = ni->parent)
234 fprintf(stderr, "%s ", coord2sstr(node_coord(ni), &bb));
235 fprintf(stderr, "[color %d] update result %d (color %d)\n",
236 node_color, result, player_color);
237 #endif
239 /* Initialize first_move */
240 for (int i = pass; i < board_size2(final_board); i++) first_move[i] = INT_MAX;
241 int move;
242 assert(map->gamelen > 0);
243 for (move = map->gamelen - 1; move >= map->game_baselen; move--)
244 first_move[map->game[move]] = move;
246 while (node) {
247 if (!b->crit_amaf && !is_pass(node_coord(node))) {
248 stats_add_result(&node->winner_owner, board_local_value(b->crit_lvalue, final_board, node_coord(node), winner_color), 1);
249 stats_add_result(&node->black_owner, board_local_value(b->crit_lvalue, final_board, node_coord(node), S_BLACK), 1);
251 stats_add_result(&node->u, result, 1);
253 bool capturing_ko = move + 1 < map->gamelen && map->is_ko_capture[move+1];
255 /* This loop ignores symmetry considerations, but they should
256 * matter only at a point when AMAF doesn't help much. */
257 assert(map->game_baselen >= 0);
258 for (struct tree_node *ni = node->children; ni; ni = ni->sibling) {
259 if (is_pass(node_coord(ni))) continue;
261 /* Use the child move only if it was first played by the same color. */
262 int first = first_move[node_coord(ni)];
263 if (first == INT_MAX) continue;
264 assert(first > move && first < map->gamelen);
265 int distance = first - (move + 1);
266 if (distance & 1) continue;
268 int weight = 1;
269 floating_t res = result;
271 /* Don't give amaf bonus to a ko threat before taking the ko.
272 * http://www.grappa.univ-lille3.fr/~coulom/Aja_PhD_Thesis.pdf
273 * move+1: B captures a ko
274 * move+2: W plays a ko threat
275 * move+3: B answers ko threat
276 * move+4: W re-captures the ko
277 * move+5: B plays a ko threat
278 * then do not give a amaf bonus to this threat at level move+1, prefer taking ko.
280 if (capturing_ko && distance == 4 && map->is_ko_capture[move+4]) {
281 weight = b->threat_rave;
282 res = 1.0 - res;
283 } else if (b->distance_rave != 0) {
284 /* Give more weight to moves played earlier */
285 weight += b->distance_rave * (map->gamelen - first) / (map->gamelen - move);
287 stats_add_result(&ni->amaf, res, weight);
289 if (b->crit_amaf) {
290 stats_add_result(&ni->winner_owner, board_local_value(b->crit_lvalue, final_board, node_coord(ni), winner_color), 1);
291 stats_add_result(&ni->black_owner, board_local_value(b->crit_lvalue, final_board, node_coord(ni), S_BLACK), 1);
293 #if 0
294 struct board bb; bb.size = 9+2;
295 fprintf(stderr, "* %s<%"PRIhash"> -> %s<%"PRIhash"> [%d/%f => %d/%f]\n",
296 coord2sstr(node_coord(node), &bb), node->hash,
297 coord2sstr(node_coord(ni), &bb), ni->hash,
298 player_color, result, move, res);
299 #endif
301 if (node->parent) {
302 assert(move >= 0 && map->game[move] == node_coord(node) && first_move[node_coord(node)] > move);
303 first_move[node_coord(node)] = move;
304 move--;
306 node = node->parent;
311 struct uct_policy *
312 policy_ucb1amaf_init(struct uct *u, char *arg, struct board *board)
314 struct uct_policy *p = calloc2(1, sizeof(*p));
315 struct ucb1_policy_amaf *b = calloc2(1, sizeof(*b));
316 p->uct = u;
317 p->data = b;
318 p->choose = uctp_generic_choose;
319 p->winner = uctp_generic_winner;
320 p->evaluate = ucb1rave_evaluate;
321 p->descend = ucb1rave_descend;
322 p->update = ucb1amaf_update;
323 p->wants_amaf = true;
325 b->explore_p = 0;
326 b->equiv_rave = board_large(board) ? 4000 : 3000;
327 b->fpu = INFINITY;
328 b->sylvain_rave = true;
329 b->distance_rave = 3;
330 b->threat_rave = 0;
331 b->ltree_rave = 0.75f;
333 b->crit_rave = 1.1f;
334 b->crit_min_playouts = 2000;
335 b->crit_negative = 1;
336 b->crit_amaf = 0;
338 b->root_virtual_win = -1;
339 b->vwin_min_playouts = 1000;
341 if (arg) {
342 char *optspec, *next = arg;
343 while (*next) {
344 optspec = next;
345 next += strcspn(next, ":");
346 if (*next) { *next++ = 0; } else { *next = 0; }
348 char *optname = optspec;
349 char *optval = strchr(optspec, '=');
350 if (optval) *optval++ = 0;
352 if (!strcasecmp(optname, "explore_p")) {
353 b->explore_p = atof(optval);
354 } else if (!strcasecmp(optname, "fpu") && optval) {
355 b->fpu = atof(optval);
356 } else if (!strcasecmp(optname, "equiv_rave") && optval) {
357 b->equiv_rave = atof(optval);
358 } else if (!strcasecmp(optname, "sylvain_rave")) {
359 b->sylvain_rave = !optval || *optval == '1';
360 } else if (!strcasecmp(optname, "distance_rave") && optval) {
361 b->distance_rave = atoi(optval);
362 } else if (!strcasecmp(optname, "threat_rave") && optval) {
363 b->threat_rave = atoi(optval);
364 } else if (!strcasecmp(optname, "ltree_rave") && optval) {
365 b->ltree_rave = atof(optval);
366 } else if (!strcasecmp(optname, "crit_rave") && optval) {
367 b->crit_rave = atof(optval);
368 } else if (!strcasecmp(optname, "crit_min_playouts") && optval) {
369 b->crit_min_playouts = atoi(optval);
370 } else if (!strcasecmp(optname, "crit_plthres_coef") && optval) {
371 b->crit_plthres_coef = atof(optval);
372 } else if (!strcasecmp(optname, "crit_negative")) {
373 b->crit_negative = !optval || *optval == '1';
374 } else if (!strcasecmp(optname, "crit_negflip")) {
375 b->crit_negflip = !optval || *optval == '1';
376 } else if (!strcasecmp(optname, "crit_amaf")) {
377 b->crit_amaf = !optval || *optval == '1';
378 } else if (!strcasecmp(optname, "crit_lvalue")) {
379 b->crit_lvalue = !optval || *optval == '1';
380 } else if (!strcasecmp(optname, "virtual_win") && optval) {
381 b->virtual_win = atoi(optval);
382 } else if (!strcasecmp(optname, "root_virtual_win") && optval) {
383 b->root_virtual_win = atoi(optval);
384 } else if (!strcasecmp(optname, "vwin_min_playouts") && optval) {
385 b->vwin_min_playouts = atoi(optval);
386 } else {
387 fprintf(stderr, "ucb1amaf: Invalid policy argument %s or missing value\n",
388 optname);
389 exit(1);
393 if (b->root_virtual_win < 0)
394 b->root_virtual_win = b->virtual_win;
396 return p;