UCT patterns parameter: Introduce; allows e.g. pdict customization for priors
[pachi.git] / uct / prior.c
blob8a97db2f9ead3bcc4ac50e1b506828746b3592d4
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 p->joseki_eqex = -200;
202 p->cfgdn = -1;
204 /* Even number! */
205 p->eqex = board_large(b) ? 20 : 14;
207 if (arg) {
208 char *optspec, *next = arg;
209 while (*next) {
210 optspec = next;
211 next += strcspn(next, ":");
212 if (*next) { *next++ = 0; } else { *next = 0; }
214 char *optname = optspec;
215 char *optval = strchr(optspec, '=');
216 if (optval) *optval++ = 0;
218 if (!strcasecmp(optname, "eqex") && optval) {
219 p->eqex = atoi(optval);
221 /* In the following settings, you can use negative
222 * numbers to give the hundredths of default eqex.
223 * E.g. -100 is default eqex, -50 is half of the
224 * default eqex, -200 is double the default eqex. */
225 } else if (!strcasecmp(optname, "even") && optval) {
226 p->even_eqex = atoi(optval);
227 } else if (!strcasecmp(optname, "policy") && optval) {
228 p->policy_eqex = atoi(optval);
229 } else if (!strcasecmp(optname, "b19") && optval) {
230 p->b19_eqex = atoi(optval);
231 } else if (!strcasecmp(optname, "cfgd") && optval) {
232 /* cfgd=3%40%20%20 - 3 levels; immediate libs
233 * of last move => 40 wins, their neighbors
234 * 20 wins, 2nd-level neighbors 20 wins;
235 * neighbors are group-transitive. */
236 p->cfgdn = atoi(optval); optval += strcspn(optval, "%");
237 p->cfgd_eqex = calloc2(p->cfgdn + 1, sizeof(*p->cfgd_eqex));
238 p->cfgd_eqex[0] = 0;
239 int i;
240 for (i = 1; *optval; i++, optval += strcspn(optval, "%")) {
241 optval++;
242 p->cfgd_eqex[i] = atoi(optval);
244 if (i != p->cfgdn + 1) {
245 fprintf(stderr, "uct: Missing prior cfdn level %d/%d\n", i, p->cfgdn);
246 exit(1);
249 } else if (!strcasecmp(optname, "joseki") && optval) {
250 p->joseki_eqex = atoi(optval);
251 } else if (!strcasecmp(optname, "eye") && optval) {
252 p->eye_eqex = atoi(optval);
253 } else if (!strcasecmp(optname, "ko") && optval) {
254 p->ko_eqex = atoi(optval);
255 } else if (!strcasecmp(optname, "pattern") && optval) {
256 /* Pattern-based prior eqex. */
257 /* Note that this prior is still going to be
258 * used only if you have downloaded or
259 * generated the pattern files! */
260 p->pattern_eqex = atoi(optval);
261 } else if (!strcasecmp(optname, "plugin") && optval) {
262 /* Unlike others, this is just a *recommendation*. */
263 p->plugin_eqex = atoi(optval);
264 } else {
265 fprintf(stderr, "uct: Invalid prior argument %s or missing value\n", optname);
266 exit(1);
271 if (p->even_eqex < 0) p->even_eqex = p->eqex * -p->even_eqex / 100;
272 if (p->policy_eqex < 0) p->policy_eqex = p->eqex * -p->policy_eqex / 100;
273 if (p->b19_eqex < 0) p->b19_eqex = p->eqex * -p->b19_eqex / 100;
274 if (p->eye_eqex < 0) p->eye_eqex = p->eqex * -p->eye_eqex / 100;
275 if (p->ko_eqex < 0) p->ko_eqex = p->eqex * -p->ko_eqex / 100;
276 if (p->joseki_eqex < 0) p->joseki_eqex = p->eqex * -p->joseki_eqex / 100;
277 if (p->pattern_eqex < 0) p->pattern_eqex = p->eqex * -p->pattern_eqex / 100;
278 if (p->plugin_eqex < 0) p->plugin_eqex = p->eqex * -p->plugin_eqex / 100;
280 if (p->cfgdn < 0) {
281 static int large_bonuses[] = { 0, 55, 50, 15 };
282 static int small_bonuses[] = { 0, 45, 40, 15 };
283 p->cfgdn = 3;
284 p->cfgd_eqex = calloc2(p->cfgdn + 1, sizeof(*p->cfgd_eqex));
285 memcpy(p->cfgd_eqex, board_large(b) ? large_bonuses : small_bonuses, sizeof(large_bonuses));
287 if (p->cfgdn > TREE_NODE_D_MAX) {
288 fprintf(stderr, "uct: CFG distances only up to %d available\n", TREE_NODE_D_MAX);
289 exit(1);
292 if (p->pattern_eqex)
293 u->want_pat = true;
295 return p;
298 void
299 uct_prior_done(struct uct_prior *p)
301 assert(p->cfgd_eqex);
302 free(p->cfgd_eqex);
303 free(p);