Spatial: Accurate analysis of hashing function performance
[pachi/nmclean.git] / uct / prior.c
blobb179bcff4b59afe96988f3f15e2062743b6615cd
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 "joseki/base.h"
10 #include "move.h"
11 #include "random.h"
12 #include "tactics/util.h"
13 #include "uct/internal.h"
14 #include "uct/plugins.h"
15 #include "uct/prior.h"
16 #include "uct/tree.h"
18 /* Applying heuristic values to the tree nodes, skewing the reading in
19 * most interesting directions. */
21 /* TODO: Introduce foreach_fpoint() to iterate only over non-occupied
22 * positions. */
24 struct uct_prior {
25 /* Equivalent experience for prior knowledge. MoGo paper recommends
26 * 50 playouts per source; in practice, esp. with RAVE, about 6
27 * playouts per source seems best. */
28 int eqex;
29 int even_eqex, policy_eqex, b19_eqex, eye_eqex, ko_eqex, plugin_eqex, joseki_eqex, pattern_eqex;
30 int cfgdn; int *cfgd_eqex;
33 void
34 uct_prior_even(struct uct *u, struct tree_node *node, struct prior_map *map)
36 /* Q_{even} */
37 /* This may be dubious for normal UCB1 but is essential for
38 * reading stability of RAVE, it appears. */
39 add_prior_value(map, pass, 0.5, u->prior->even_eqex);
40 foreach_free_point(map->b) {
41 if (!map->consider[c])
42 continue;
43 add_prior_value(map, c, 0.5, u->prior->even_eqex);
44 } foreach_free_point_end;
47 void
48 uct_prior_eye(struct uct *u, struct tree_node *node, struct prior_map *map)
50 /* Discourage playing into our own eyes. However, we cannot
51 * completely prohibit it:
52 * #######
53 * ...XX.#
54 * XOOOXX#
55 * X.OOOO#
56 * .XXXX.# */
57 foreach_free_point(map->b) {
58 if (!map->consider[c])
59 continue;
60 if (!board_is_one_point_eye(map->b, c, map->to_play))
61 continue;
62 add_prior_value(map, c, 0, u->prior->eye_eqex);
63 } foreach_free_point_end;
66 void
67 uct_prior_ko(struct uct *u, struct tree_node *node, struct prior_map *map)
69 /* Favor fighting ko, if we took it le 10 moves ago. */
70 coord_t ko = map->b->last_ko.coord;
71 if (is_pass(ko) || map->b->moves - map->b->last_ko_age > 10 || !map->consider[ko])
72 return;
73 // fprintf(stderr, "prior ko-fight @ %s %s\n", stone2str(map->to_play), coord2sstr(ko, map->b));
74 add_prior_value(map, ko, 1, u->prior->ko_eqex);
77 void
78 uct_prior_b19(struct uct *u, struct tree_node *node, struct prior_map *map)
80 /* Q_{b19} */
81 /* Specific hints for 19x19 board - priors for certain edge distances. */
82 foreach_free_point(map->b) {
83 if (!map->consider[c])
84 continue;
85 int d = coord_edge_distance(c, map->b);
86 if (d != 0 && d != 2)
87 continue;
88 /* The bonus applies only with no stones in immediate
89 * vincinity. */
90 if (board_stone_radar(map->b, c, 2))
91 continue;
92 /* First line: 0 */
93 /* Third line: 1 */
94 add_prior_value(map, c, d == 2, u->prior->b19_eqex);
95 } foreach_free_point_end;
98 void
99 uct_prior_playout(struct uct *u, struct tree_node *node, struct prior_map *map)
101 /* Q_{playout-policy} */
102 if (u->playout->assess)
103 u->playout->assess(u->playout, map, u->prior->policy_eqex);
106 void
107 uct_prior_cfgd(struct uct *u, struct tree_node *node, struct prior_map *map)
109 /* Q_{common_fate_graph_distance} */
110 /* Give bonus to moves local to the last move, where "local" means
111 * local in terms of groups, not just manhattan distance. */
112 if (is_pass(map->b->last_move.coord) || is_resign(map->b->last_move.coord))
113 return;
115 foreach_free_point(map->b) {
116 if (!map->consider[c])
117 continue;
118 if (map->distances[c] > u->prior->cfgdn)
119 continue;
120 assert(map->distances[c] != 0);
121 int bonus = u->prior->cfgd_eqex[map->distances[c]];
122 add_prior_value(map, c, 1, bonus);
123 } foreach_free_point_end;
126 void
127 uct_prior_joseki(struct uct *u, struct tree_node *node, struct prior_map *map)
129 /* Q_{joseki} */
130 if (!u->jdict)
131 return;
132 for (int i = 0; i < 4; i++) {
133 hash_t h = map->b->qhash[i] & joseki_hash_mask;
134 coord_t *cc = u->jdict->patterns[h].moves[map->to_play - 1];
135 if (!cc) continue;
136 for (; !is_pass(*cc); cc++) {
137 if (coord_quadrant(*cc, map->b) != i)
138 continue;
139 add_prior_value(map, *cc, 1.0, u->prior->joseki_eqex);
144 void
145 uct_prior_pattern(struct uct *u, struct tree_node *node, struct prior_map *map)
147 /* Q_{pattern} */
148 if (!u->pat.pd)
149 return;
151 struct board *b = map->b;
152 struct pattern pats[b->flen];
153 floating_t probs[b->flen];
154 pattern_rate_moves(&u->pat, b, map->to_play, pats, probs);
155 if (UDEBUGL(5)) {
156 fprintf(stderr, "Pattern prior at node %s\n", coord2sstr(node->coord, b));
157 board_print(b, stderr);
160 for (int f = 0; f < b->flen; f++) {
161 if (isnan(probs[f]) || probs[f] < 0.001)
162 continue;
163 assert(!is_pass(b->f[f]));
164 if (UDEBUGL(5)) {
165 char s[256]; pattern2str(s, &pats[f]);
166 fprintf(stderr, "\t%s: %.3f %s\n", coord2sstr(b->f[f], b), probs[f], s);
168 add_prior_value(map, b->f[f], 1.0, sqrt(probs[f]) * u->prior->pattern_eqex);
172 void
173 uct_prior(struct uct *u, struct tree_node *node, struct prior_map *map)
175 if (u->prior->even_eqex)
176 uct_prior_even(u, node, map);
177 if (u->prior->eye_eqex)
178 uct_prior_eye(u, node, map);
179 if (u->prior->ko_eqex)
180 uct_prior_ko(u, node, map);
181 if (u->prior->b19_eqex)
182 uct_prior_b19(u, node, map);
183 if (u->prior->policy_eqex)
184 uct_prior_playout(u, node, map);
185 if (u->prior->cfgd_eqex)
186 uct_prior_cfgd(u, node, map);
187 if (u->prior->joseki_eqex)
188 uct_prior_joseki(u, node, map);
189 if (u->prior->pattern_eqex)
190 uct_prior_pattern(u, node, map);
191 if (u->prior->plugin_eqex)
192 plugin_prior(u->plugins, node, map, u->prior->plugin_eqex);
195 struct uct_prior *
196 uct_prior_init(char *arg, struct board *b, struct uct *u)
198 struct uct_prior *p = calloc2(1, sizeof(struct uct_prior));
200 p->even_eqex = p->policy_eqex = p->b19_eqex = p->eye_eqex = p->ko_eqex = p->plugin_eqex = -100;
201 /* FIXME: Optimal pattern_eqex is about -1000 with small playout counts
202 * but only -400 on a cluster. We need a better way to set the default
203 * here. */
204 p->pattern_eqex = -400;
205 p->joseki_eqex = -200;
206 p->cfgdn = -1;
208 /* Even number! */
209 p->eqex = board_large(b) ? 20 : 14;
211 if (arg) {
212 char *optspec, *next = arg;
213 while (*next) {
214 optspec = next;
215 next += strcspn(next, ":");
216 if (*next) { *next++ = 0; } else { *next = 0; }
218 char *optname = optspec;
219 char *optval = strchr(optspec, '=');
220 if (optval) *optval++ = 0;
222 if (!strcasecmp(optname, "eqex") && optval) {
223 p->eqex = atoi(optval);
225 /* In the following settings, you can use negative
226 * numbers to give the hundredths of default eqex.
227 * E.g. -100 is default eqex, -50 is half of the
228 * default eqex, -200 is double the default eqex. */
229 } else if (!strcasecmp(optname, "even") && optval) {
230 p->even_eqex = atoi(optval);
231 } else if (!strcasecmp(optname, "policy") && optval) {
232 p->policy_eqex = atoi(optval);
233 } else if (!strcasecmp(optname, "b19") && optval) {
234 p->b19_eqex = atoi(optval);
235 } else if (!strcasecmp(optname, "cfgd") && optval) {
236 /* cfgd=3%40%20%20 - 3 levels; immediate libs
237 * of last move => 40 wins, their neighbors
238 * 20 wins, 2nd-level neighbors 20 wins;
239 * neighbors are group-transitive. */
240 p->cfgdn = atoi(optval); optval += strcspn(optval, "%");
241 p->cfgd_eqex = calloc2(p->cfgdn + 1, sizeof(*p->cfgd_eqex));
242 p->cfgd_eqex[0] = 0;
243 int i;
244 for (i = 1; *optval; i++, optval += strcspn(optval, "%")) {
245 optval++;
246 p->cfgd_eqex[i] = atoi(optval);
248 if (i != p->cfgdn + 1) {
249 fprintf(stderr, "uct: Missing prior cfdn level %d/%d\n", i, p->cfgdn);
250 exit(1);
253 } else if (!strcasecmp(optname, "joseki") && optval) {
254 p->joseki_eqex = atoi(optval);
255 } else if (!strcasecmp(optname, "eye") && optval) {
256 p->eye_eqex = atoi(optval);
257 } else if (!strcasecmp(optname, "ko") && optval) {
258 p->ko_eqex = atoi(optval);
259 } else if (!strcasecmp(optname, "pattern") && optval) {
260 /* Pattern-based prior eqex. */
261 /* Note that this prior is still going to be
262 * used only if you have downloaded or
263 * generated the pattern files! */
264 p->pattern_eqex = atoi(optval);
265 } else if (!strcasecmp(optname, "plugin") && optval) {
266 /* Unlike others, this is just a *recommendation*. */
267 p->plugin_eqex = atoi(optval);
268 } else {
269 fprintf(stderr, "uct: Invalid prior argument %s or missing value\n", optname);
270 exit(1);
275 if (p->even_eqex < 0) p->even_eqex = p->eqex * -p->even_eqex / 100;
276 if (p->policy_eqex < 0) p->policy_eqex = p->eqex * -p->policy_eqex / 100;
277 if (p->b19_eqex < 0) p->b19_eqex = p->eqex * -p->b19_eqex / 100;
278 if (p->eye_eqex < 0) p->eye_eqex = p->eqex * -p->eye_eqex / 100;
279 if (p->ko_eqex < 0) p->ko_eqex = p->eqex * -p->ko_eqex / 100;
280 if (p->joseki_eqex < 0) p->joseki_eqex = p->eqex * -p->joseki_eqex / 100;
281 if (p->pattern_eqex < 0) p->pattern_eqex = p->eqex * -p->pattern_eqex / 100;
282 if (p->plugin_eqex < 0) p->plugin_eqex = p->eqex * -p->plugin_eqex / 100;
284 if (p->cfgdn < 0) {
285 static int large_bonuses[] = { 0, 55, 50, 15 };
286 static int small_bonuses[] = { 0, 45, 40, 15 };
287 p->cfgdn = 3;
288 p->cfgd_eqex = calloc2(p->cfgdn + 1, sizeof(*p->cfgd_eqex));
289 memcpy(p->cfgd_eqex, board_large(b) ? large_bonuses : small_bonuses, sizeof(large_bonuses));
291 if (p->cfgdn > TREE_NODE_D_MAX) {
292 fprintf(stderr, "uct: CFG distances only up to %d available\n", TREE_NODE_D_MAX);
293 exit(1);
296 if (p->pattern_eqex)
297 u->want_pat = true;
299 return p;
302 void
303 uct_prior_done(struct uct_prior *p)
305 assert(p->cfgd_eqex);
306 free(p->cfgd_eqex);
307 free(p);