fast_sqrt(): Reintroduce sqrts for integers up to 80
[pachi.git] / uct / policy / ucb1amaf.c
blobbd9a37aeb43d2a152dfc13777791d39f4773b5c1
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"
14 /* This implements the UCB1 policy with an extra AMAF heuristics. */
16 struct ucb1_policy_amaf {
17 /* This is what the Modification of UCT with Patterns in Monte Carlo Go
18 * paper calls 'p'. Original UCB has this on 2, but this seems to
19 * produce way too wide searches; reduce this to get deeper and
20 * narrower readouts - try 0.2. */
21 float explore_p;
22 /* First Play Urgency - if set to less than infinity (the MoGo paper
23 * above reports 1.0 as the best), new branches are explored only
24 * if none of the existing ones has higher urgency than fpu. */
25 float fpu;
26 int urg_randoma, urg_randomm;
27 float explore_p_rave;
28 int equiv_rave;
29 bool both_colors;
30 bool check_nakade;
34 struct tree_node *ucb1_choose(struct uct_policy *p, struct tree_node *node, struct board *b, enum stone color);
36 struct tree_node *ucb1_descend(struct uct_policy *p, struct tree *tree, struct tree_node *node, int parity, bool allow_pass);
39 static inline float fast_sqrt(int x)
41 static const float table[] = {
42 0, 1, 1.41421356237309504880, 1.73205080756887729352,
43 2.00000000000000000000, 2.23606797749978969640,
44 2.44948974278317809819, 2.64575131106459059050,
45 2.82842712474619009760, 3.00000000000000000000,
46 3.16227766016837933199, 3.31662479035539984911,
47 3.46410161513775458705, 3.60555127546398929311,
48 3.74165738677394138558, 3.87298334620741688517,
49 4.00000000000000000000, 4.12310562561766054982,
50 4.24264068711928514640, 4.35889894354067355223,
51 4.47213595499957939281, 4.58257569495584000658,
52 4.69041575982342955456, 4.79583152331271954159,
53 4.89897948556635619639, 5.00000000000000000000,
54 5.09901951359278483002, 5.19615242270663188058,
55 5.29150262212918118100, 5.38516480713450403125,
56 5.47722557505166113456, 5.56776436283002192211,
57 5.65685424949238019520, 5.74456264653802865985,
58 5.83095189484530047087, 5.91607978309961604256,
59 6.00000000000000000000, 6.08276253029821968899,
60 6.16441400296897645025, 6.24499799839839820584,
61 6.32455532033675866399, 6.40312423743284868648,
62 6.48074069840786023096, 6.55743852430200065234,
63 6.63324958071079969822, 6.70820393249936908922,
64 6.78232998312526813906, 6.85565460040104412493,
65 6.92820323027550917410, 7.00000000000000000000,
66 7.07106781186547524400, 7.14142842854284999799,
67 7.21110255092797858623, 7.28010988928051827109,
68 7.34846922834953429459, 7.41619848709566294871,
69 7.48331477354788277116, 7.54983443527074969723,
70 7.61577310586390828566, 7.68114574786860817576,
71 7.74596669241483377035, 7.81024967590665439412,
72 7.87400787401181101968, 7.93725393319377177150,
74 //printf("sqrt %d\n", x);
75 if (x < sizeof(table) / sizeof(*table)) {
76 return table[x];
77 } else {
78 return sqrt(x);
82 /* Sylvain RAVE function */
83 struct tree_node *
84 ucb1srave_descend(struct uct_policy *p, struct tree *tree, struct tree_node *node, int parity, bool allow_pass)
86 struct ucb1_policy_amaf *b = p->data;
87 float rave_coef = 1.0f / b->equiv_rave;
88 float nconf = 1.f, rconf = 1.f;
89 if (b->explore_p > 0)
90 nconf = sqrt(log(node->u.playouts + node->prior.playouts));
91 if (b->explore_p_rave > 0 && node->amaf.playouts)
92 rconf = sqrt(log(node->amaf.playouts + node->prior.playouts));
94 // XXX: Stack overflow danger on big boards?
95 struct tree_node *nbest[512] = { node->children }; int nbests = 1;
96 float best_urgency = -9999;
98 for (struct tree_node *ni = node->children; ni; ni = ni->sibling) {
99 /* Do not consider passing early. */
100 if (likely(!allow_pass) && unlikely(is_pass(ni->coord)))
101 continue;
103 /* TODO: Exploration? */
105 int ngames = ni->u.playouts;
106 int nwins = ni->u.wins;
107 int rgames = ni->amaf.playouts;
108 int rwins = ni->amaf.wins;
109 if (p->uct->amaf_prior) {
110 rgames += ni->prior.playouts;
111 rwins += ni->prior.wins;
112 } else {
113 ngames += ni->prior.playouts;
114 nwins += ni->prior.wins;
116 if (tree_parity(tree, parity) < 0) {
117 nwins = ngames - nwins;
118 rwins = rgames - rwins;
120 float nval = 0, rval = 0;
121 if (ngames) {
122 nval = (float) nwins / ngames;
123 if (b->explore_p > 0)
124 nval += b->explore_p * nconf / fast_sqrt(ngames);
126 if (rgames) {
127 rval = (float) rwins / rgames;
128 if (b->explore_p_rave > 0 && !is_pass(ni->coord))
129 rval += b->explore_p_rave * rconf / fast_sqrt(rgames);
132 float urgency;
133 if (ngames) {
134 if (rgames) {
135 /* At the beginning, beta is at 1 and RAVE is used.
136 * At b->equiv_rate, beta is at 1/3 and gets steeper on. */
137 float beta = (float) rgames / (rgames + ngames + rave_coef * ngames * rgames);
138 #if 0
139 //if (node->coord == 7*11+4) // D7
140 fprintf(stderr, "[beta %f = %d / (%d + %d + %f)]\n",
141 beta, rgames, rgames, ngames, rave_coef * ngames * rgames);
142 #endif
143 urgency = beta * rval + (1 - beta) * nval;
144 } else {
145 urgency = nval;
147 } else if (rgames) {
148 urgency = rval;
149 } else {
150 /* assert(!u->even_eqex); */
151 urgency = b->fpu;
154 #if 0
155 struct board bb; bb.size = 11;
156 //if (node->coord == 7*11+4) // D7
157 fprintf(stderr, "%s<%lld>-%s<%lld> urgency %f (r %d / %d, n %d / %d)\n",
158 coord2sstr(ni->parent->coord, &bb), ni->parent->hash,
159 coord2sstr(ni->coord, &bb), ni->hash, urgency,
160 rwins, rgames, nwins, ngames);
161 #endif
162 if (b->urg_randoma)
163 urgency += (float)(fast_random(b->urg_randoma) - b->urg_randoma / 2) / 1000;
164 if (b->urg_randomm)
165 urgency *= (float)(fast_random(b->urg_randomm) + 5) / b->urg_randomm;
167 if (urgency - best_urgency > __FLT_EPSILON__) { // urgency > best_urgency
168 best_urgency = urgency; nbests = 0;
170 if (urgency - best_urgency > -__FLT_EPSILON__) { // urgency >= best_urgency
171 /* We want to always choose something else than a pass
172 * in case of a tie. pass causes degenerative behaviour. */
173 if (nbests == 1 && is_pass(nbest[0]->coord)) {
174 nbests--;
176 nbest[nbests++] = ni;
179 #if 0
180 struct board bb; bb.size = 11;
181 fprintf(stderr, "[%s %d: ", coord2sstr(node->coord, &bb), nbests);
182 for (int zz = 0; zz < nbests; zz++)
183 fprintf(stderr, "%s", coord2sstr(nbest[zz]->coord, &bb));
184 fprintf(stderr, "]\n");
185 #endif
186 return nbest[fast_random(nbests)];
189 static void
190 update_node(struct uct_policy *p, struct tree_node *node, int result)
192 node->u.playouts++;
193 node->u.wins += result;
194 tree_update_node_value(node, p->uct->amaf_prior);
197 static void
198 update_node_amaf(struct uct_policy *p, struct tree_node *node, int result)
200 node->amaf.playouts++;
201 node->amaf.wins += result;
202 tree_update_node_rvalue(node, p->uct->amaf_prior);
205 void
206 ucb1amaf_update(struct uct_policy *p, struct tree *tree, struct tree_node *node, enum stone node_color, enum stone player_color, struct playout_amafmap *map, int result)
208 struct ucb1_policy_amaf *b = p->data;
209 enum stone child_color = stone_other(node_color);
211 #if 0
212 struct board bb; bb.size = 9+2;
213 for (struct tree_node *ni = node; ni; ni = ni->parent)
214 fprintf(stderr, "%s ", coord2sstr(ni->coord, &bb));
215 fprintf(stderr, "[color %d] update result %d (color %d)\n",
216 node_color, result, player_color);
217 #endif
219 while (node) {
220 if (node->parent == NULL)
221 assert(tree->root_color == stone_other(child_color));
223 update_node(p, node, result);
224 if (amaf_nakade(map->map[node->coord]))
225 amaf_op(map->map[node->coord], -);
227 /* This loop ignores symmetry considerations, but they should
228 * matter only at a point when AMAF doesn't help much. */
229 for (struct tree_node *ni = node->children; ni; ni = ni->sibling) {
230 assert(map->map[ni->coord] != S_OFFBOARD);
231 if (map->map[ni->coord] == S_NONE)
232 continue;
233 assert(map->game_baselen >= 0);
234 enum stone amaf_color = map->map[ni->coord];
235 if (amaf_nakade(map->map[ni->coord])) {
236 if (!b->check_nakade)
237 continue;
238 /* We don't care to implement both_colors
239 * properly since it sucks anyway. */
240 int i;
241 for (i = map->game_baselen; i < map->gamelen; i++)
242 if (map->game[i].coord == ni->coord
243 && map->game[i].color == child_color)
244 break;
245 if (i == map->gamelen)
246 continue;
247 amaf_color = child_color;
250 int nres = result;
251 if (amaf_color != child_color) {
252 if (!b->both_colors)
253 continue;
254 nres = !nres;
256 /* For child_color != player_color, we still want
257 * to record the result unmodified; in that case,
258 * we will correctly negate them at the descend phase. */
260 update_node_amaf(p, ni, nres);
262 #if 0
263 fprintf(stderr, "* %s<%lld> -> %s<%lld> [%d %d => %d/%d]\n", coord2sstr(node->coord, &bb), node->hash, coord2sstr(ni->coord, &bb), ni->hash, player_color, child_color, result);
264 #endif
267 if (!is_pass(node->coord)) {
268 map->game_baselen--;
270 node = node->parent; child_color = stone_other(child_color);
275 struct uct_policy *
276 policy_ucb1amaf_init(struct uct *u, char *arg)
278 struct uct_policy *p = calloc(1, sizeof(*p));
279 struct ucb1_policy_amaf *b = calloc(1, sizeof(*b));
280 p->uct = u;
281 p->data = b;
282 p->descend = ucb1srave_descend;
283 p->choose = ucb1_choose;
284 p->update = ucb1amaf_update;
285 p->wants_amaf = true;
287 // RAVE: 0.2vs0: 40% (+-7.3) 0.1vs0: 54.7% (+-3.5)
288 b->explore_p = 0.1;
289 b->explore_p_rave = 0.01;
290 b->equiv_rave = 3000;
291 b->fpu = INFINITY;
292 b->check_nakade = true;
294 if (arg) {
295 char *optspec, *next = arg;
296 while (*next) {
297 optspec = next;
298 next += strcspn(next, ":");
299 if (*next) { *next++ = 0; } else { *next = 0; }
301 char *optname = optspec;
302 char *optval = strchr(optspec, '=');
303 if (optval) *optval++ = 0;
305 if (!strcasecmp(optname, "explore_p")) {
306 b->explore_p = atof(optval);
307 } else if (!strcasecmp(optname, "fpu") && optval) {
308 b->fpu = atof(optval);
309 } else if (!strcasecmp(optname, "urg_randoma") && optval) {
310 b->urg_randoma = atoi(optval);
311 } else if (!strcasecmp(optname, "urg_randomm") && optval) {
312 b->urg_randomm = atoi(optval);
313 } else if (!strcasecmp(optname, "explore_p_rave") && optval) {
314 b->explore_p_rave = atof(optval);
315 } else if (!strcasecmp(optname, "equiv_rave") && optval) {
316 b->equiv_rave = atof(optval);
317 } else if (!strcasecmp(optname, "both_colors")) {
318 b->both_colors = true;
319 } else if (!strcasecmp(optname, "check_nakade")) {
320 b->check_nakade = !optval || *optval == '1';
321 } else {
322 fprintf(stderr, "ucb1: Invalid policy argument %s or missing value\n", optname);
327 if (b->explore_p_rave < 0) b->explore_p_rave = b->explore_p;
329 return p;