9 #include "joseki/base.h"
12 #include "tactics/ladder.h"
13 #include "tactics/util.h"
14 #include "uct/internal.h"
15 #include "uct/plugins.h"
16 #include "uct/prior.h"
19 /* Applying heuristic values to the tree nodes, skewing the reading in
20 * most interesting directions. */
22 /* TODO: Introduce foreach_fpoint() to iterate only over non-occupied
26 /* Equivalent experience for prior knowledge. MoGo paper recommends
27 * 50 playouts per source; in practice, esp. with RAVE, about 6
28 * playouts per source seems best. */
30 int even_eqex
, policy_eqex
, b19_eqex
, eye_eqex
, ko_eqex
, plugin_eqex
, joseki_eqex
, pattern_eqex
;
31 int cfgdn
; int *cfgd_eqex
;
36 uct_prior_even(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
39 /* This may be dubious for normal UCB1 but is essential for
40 * reading stability of RAVE, it appears. */
41 add_prior_value(map
, pass
, 0.5, u
->prior
->even_eqex
);
42 foreach_free_point(map
->b
) {
43 if (!map
->consider
[c
])
45 add_prior_value(map
, c
, 0.5, u
->prior
->even_eqex
);
46 } foreach_free_point_end
;
50 uct_prior_eye(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
52 /* Discourage playing into our own eyes. However, we cannot
53 * completely prohibit it:
59 foreach_free_point(map
->b
) {
60 if (!map
->consider
[c
])
62 if (!board_is_one_point_eye(map
->b
, c
, map
->to_play
))
64 add_prior_value(map
, c
, 0, u
->prior
->eye_eqex
);
65 } foreach_free_point_end
;
69 uct_prior_ko(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
71 /* Favor fighting ko, if we took it le 10 moves ago. */
72 coord_t ko
= map
->b
->last_ko
.coord
;
73 if (is_pass(ko
) || map
->b
->moves
- map
->b
->last_ko_age
> 10 || !map
->consider
[ko
])
75 // fprintf(stderr, "prior ko-fight @ %s %s\n", stone2str(map->to_play), coord2sstr(ko, map->b));
76 add_prior_value(map
, ko
, 1, u
->prior
->ko_eqex
);
80 uct_prior_b19(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
83 /* Specific hints for 19x19 board - priors for certain edge distances. */
84 foreach_free_point(map
->b
) {
85 if (!map
->consider
[c
])
87 int d
= coord_edge_distance(c
, map
->b
);
90 /* The bonus applies only with no stones in immediate
92 if (board_stone_radar(map
->b
, c
, 2))
96 add_prior_value(map
, c
, d
== 2, u
->prior
->b19_eqex
);
97 } foreach_free_point_end
;
101 uct_prior_playout(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
103 /* Q_{playout-policy} */
104 if (u
->playout
->assess
)
105 u
->playout
->assess(u
->playout
, map
, u
->prior
->policy_eqex
);
109 uct_prior_cfgd(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
111 /* Q_{common_fate_graph_distance} */
112 /* Give bonus to moves local to the last move, where "local" means
113 * local in terms of groups, not just manhattan distance. */
114 if (is_pass(map
->b
->last_move
.coord
) || is_resign(map
->b
->last_move
.coord
))
117 foreach_free_point(map
->b
) {
118 if (!map
->consider
[c
])
120 if (map
->distances
[c
] > u
->prior
->cfgdn
)
122 assert(map
->distances
[c
] != 0);
123 int bonus
= u
->prior
->cfgd_eqex
[map
->distances
[c
]];
124 add_prior_value(map
, c
, 1, bonus
);
125 } foreach_free_point_end
;
129 uct_prior_joseki(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
134 for (int i
= 0; i
< 4; i
++) {
135 hash_t h
= map
->b
->qhash
[i
] & joseki_hash_mask
;
136 coord_t
*cc
= u
->jdict
->patterns
[h
].moves
[map
->to_play
- 1];
138 for (; !is_pass(*cc
); cc
++) {
139 if (coord_quadrant(*cc
, map
->b
) != i
)
141 add_prior_value(map
, *cc
, 1.0, u
->prior
->joseki_eqex
);
147 uct_prior_pattern(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
153 struct board
*b
= map
->b
;
154 struct pattern pats
[b
->flen
];
155 floating_t probs
[b
->flen
];
156 pattern_rate_moves(&u
->pat
, b
, map
->to_play
, pats
, probs
);
158 fprintf(stderr
, "Pattern prior at node %s\n", coord2sstr(node
->coord
, b
));
159 board_print(b
, stderr
);
162 for (int f
= 0; f
< b
->flen
; f
++) {
163 if (isnan(probs
[f
]) || probs
[f
] < 0.001)
165 assert(!is_pass(b
->f
[f
]));
167 char s
[256]; pattern2str(s
, &pats
[f
]);
168 fprintf(stderr
, "\t%s: %.3f %s\n", coord2sstr(b
->f
[f
], b
), probs
[f
], s
);
170 add_prior_value(map
, b
->f
[f
], 1.0, sqrt(probs
[f
]) * u
->prior
->pattern_eqex
);
175 uct_prior(struct uct
*u
, struct tree_node
*node
, struct prior_map
*map
)
177 if (u
->prior
->prune_ladders
) {
178 foreach_free_point(map
->b
) {
179 if (!map
->consider
[c
])
181 group_t atari_neighbor
= board_get_atari_neighbor(map
->b
, c
, map
->to_play
);
182 if (atari_neighbor
&& is_ladder(map
->b
, c
, atari_neighbor
, true))
183 map
->consider
[c
] = false;
184 } foreach_free_point_end
;
187 if (u
->prior
->even_eqex
)
188 uct_prior_even(u
, node
, map
);
189 if (u
->prior
->eye_eqex
)
190 uct_prior_eye(u
, node
, map
);
191 if (u
->prior
->ko_eqex
)
192 uct_prior_ko(u
, node
, map
);
193 if (u
->prior
->b19_eqex
)
194 uct_prior_b19(u
, node
, map
);
195 if (u
->prior
->policy_eqex
)
196 uct_prior_playout(u
, node
, map
);
197 if (u
->prior
->cfgd_eqex
)
198 uct_prior_cfgd(u
, node
, map
);
199 if (u
->prior
->joseki_eqex
)
200 uct_prior_joseki(u
, node
, map
);
201 if (u
->prior
->pattern_eqex
)
202 uct_prior_pattern(u
, node
, map
);
203 if (u
->prior
->plugin_eqex
)
204 plugin_prior(u
->plugins
, node
, map
, u
->prior
->plugin_eqex
);
208 uct_prior_init(char *arg
, struct board
*b
, struct uct
*u
)
210 struct uct_prior
*p
= calloc2(1, sizeof(struct uct_prior
));
212 p
->even_eqex
= p
->policy_eqex
= p
->b19_eqex
= p
->eye_eqex
= p
->ko_eqex
= p
->plugin_eqex
= -100;
213 /* FIXME: Optimal pattern_eqex is about -1000 with small playout counts
214 * but only -400 on a cluster. We need a better way to set the default
216 p
->pattern_eqex
= -400;
217 p
->joseki_eqex
= -200;
221 p
->eqex
= board_large(b
) ? 20 : 14;
224 char *optspec
, *next
= arg
;
227 next
+= strcspn(next
, ":");
228 if (*next
) { *next
++ = 0; } else { *next
= 0; }
230 char *optname
= optspec
;
231 char *optval
= strchr(optspec
, '=');
232 if (optval
) *optval
++ = 0;
234 if (!strcasecmp(optname
, "eqex") && optval
) {
235 p
->eqex
= atoi(optval
);
237 /* In the following settings, you can use negative
238 * numbers to give the hundredths of default eqex.
239 * E.g. -100 is default eqex, -50 is half of the
240 * default eqex, -200 is double the default eqex. */
241 } else if (!strcasecmp(optname
, "even") && optval
) {
242 p
->even_eqex
= atoi(optval
);
243 } else if (!strcasecmp(optname
, "policy") && optval
) {
244 p
->policy_eqex
= atoi(optval
);
245 } else if (!strcasecmp(optname
, "b19") && optval
) {
246 p
->b19_eqex
= atoi(optval
);
247 } else if (!strcasecmp(optname
, "cfgd") && optval
) {
248 /* cfgd=3%40%20%20 - 3 levels; immediate libs
249 * of last move => 40 wins, their neighbors
250 * 20 wins, 2nd-level neighbors 20 wins;
251 * neighbors are group-transitive. */
252 p
->cfgdn
= atoi(optval
); optval
+= strcspn(optval
, "%");
253 p
->cfgd_eqex
= calloc2(p
->cfgdn
+ 1, sizeof(*p
->cfgd_eqex
));
256 for (i
= 1; *optval
; i
++, optval
+= strcspn(optval
, "%")) {
258 p
->cfgd_eqex
[i
] = atoi(optval
);
260 if (i
!= p
->cfgdn
+ 1) {
261 fprintf(stderr
, "uct: Missing prior cfdn level %d/%d\n", i
, p
->cfgdn
);
265 } else if (!strcasecmp(optname
, "joseki") && optval
) {
266 p
->joseki_eqex
= atoi(optval
);
267 } else if (!strcasecmp(optname
, "eye") && optval
) {
268 p
->eye_eqex
= atoi(optval
);
269 } else if (!strcasecmp(optname
, "ko") && optval
) {
270 p
->ko_eqex
= atoi(optval
);
271 } else if (!strcasecmp(optname
, "pattern") && optval
) {
272 /* Pattern-based prior eqex. */
273 /* Note that this prior is still going to be
274 * used only if you have downloaded or
275 * generated the pattern files! */
276 p
->pattern_eqex
= atoi(optval
);
277 } else if (!strcasecmp(optname
, "plugin") && optval
) {
278 /* Unlike others, this is just a *recommendation*. */
279 p
->plugin_eqex
= atoi(optval
);
280 } else if (!strcasecmp(optname
, "prune_ladders")) {
281 p
->prune_ladders
= !optval
|| atoi(optval
);
283 fprintf(stderr
, "uct: Invalid prior argument %s or missing value\n", optname
);
289 if (p
->even_eqex
< 0) p
->even_eqex
= p
->eqex
* -p
->even_eqex
/ 100;
290 if (p
->policy_eqex
< 0) p
->policy_eqex
= p
->eqex
* -p
->policy_eqex
/ 100;
291 if (p
->b19_eqex
< 0) p
->b19_eqex
= p
->eqex
* -p
->b19_eqex
/ 100;
292 if (p
->eye_eqex
< 0) p
->eye_eqex
= p
->eqex
* -p
->eye_eqex
/ 100;
293 if (p
->ko_eqex
< 0) p
->ko_eqex
= p
->eqex
* -p
->ko_eqex
/ 100;
294 if (p
->joseki_eqex
< 0) p
->joseki_eqex
= p
->eqex
* -p
->joseki_eqex
/ 100;
295 if (p
->pattern_eqex
< 0) p
->pattern_eqex
= p
->eqex
* -p
->pattern_eqex
/ 100;
296 if (p
->plugin_eqex
< 0) p
->plugin_eqex
= p
->eqex
* -p
->plugin_eqex
/ 100;
299 static int large_bonuses
[] = { 0, 55, 50, 15 };
300 static int small_bonuses
[] = { 0, 45, 40, 15 };
302 p
->cfgd_eqex
= calloc2(p
->cfgdn
+ 1, sizeof(*p
->cfgd_eqex
));
303 memcpy(p
->cfgd_eqex
, board_large(b
) ? large_bonuses
: small_bonuses
, sizeof(large_bonuses
));
305 if (p
->cfgdn
> TREE_NODE_D_MAX
) {
306 fprintf(stderr
, "uct: CFG distances only up to %d available\n", TREE_NODE_D_MAX
);
317 uct_prior_done(struct uct_prior
*p
)
319 assert(p
->cfgd_eqex
);