9 #include "joseki/base.h"
12 #include "tactics/util.h"
13 #include "uct/internal.h"
14 #include "uct/plugins.h"
15 #include "uct/prior.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
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. */
29 int even_eqex
, policy_eqex
, b19_eqex
, eye_eqex
, ko_eqex
, plugin_eqex
, joseki_eqex
, pattern_eqex
;
30 int cfgdn
; int *cfgd_eqex
;
34 uct_prior_even(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
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
])
43 add_prior_value(map
, c
, 0.5, u
->prior
->even_eqex
);
44 } foreach_free_point_end
;
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:
57 foreach_free_point(map
->b
) {
58 if (!map
->consider
[c
])
60 if (!board_is_one_point_eye(map
->b
, c
, map
->to_play
))
62 add_prior_value(map
, c
, 0, u
->prior
->eye_eqex
);
63 } foreach_free_point_end
;
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
])
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
);
78 uct_prior_b19(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
81 /* Specific hints for 19x19 board - priors for certain edge distances. */
82 foreach_free_point(map
->b
) {
83 if (!map
->consider
[c
])
85 int d
= coord_edge_distance(c
, map
->b
);
88 /* The bonus applies only with no stones in immediate
90 if (board_stone_radar(map
->b
, c
, 2))
94 add_prior_value(map
, c
, d
== 2, u
->prior
->b19_eqex
);
95 } foreach_free_point_end
;
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
);
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
))
115 foreach_free_point(map
->b
) {
116 if (!map
->consider
[c
])
118 if (map
->distances
[c
] > u
->prior
->cfgdn
)
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
;
127 uct_prior_joseki(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
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];
136 for (; !is_pass(*cc
); cc
++) {
137 if (coord_quadrant(*cc
, map
->b
) != i
)
139 add_prior_value(map
, *cc
, 1.0, u
->prior
->joseki_eqex
);
145 uct_prior_pattern(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
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
);
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)
163 assert(!is_pass(b
->f
[f
]));
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
);
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
);
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
204 p
->pattern_eqex
= -400;
205 p
->joseki_eqex
= -200;
209 p
->eqex
= board_large(b
) ? 20 : 14;
212 char *optspec
, *next
= arg
;
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
));
244 for (i
= 1; *optval
; i
++, optval
+= strcspn(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
);
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
);
269 fprintf(stderr
, "uct: Invalid prior argument %s or missing value\n", optname
);
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;
285 static int large_bonuses
[] = { 0, 55, 50, 15 };
286 static int small_bonuses
[] = { 0, 45, 40, 15 };
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
);
303 uct_prior_done(struct uct_prior
*p
)
305 assert(p
->cfgd_eqex
);