1 /* Playout player based on probability distribution generated over
2 * the available moves. */
4 /* We use the ELO-based (Coulom, 2007) approach, where each board
5 * feature (matched pattern, self-atari, capture, MC owner?, ...)
6 * is pre-assigned "playing strength" (gamma).
8 * Then, the problem of choosing a move is basically a team
9 * competition in ELO terms - each spot is represented by a team
10 * of features appearing there; the team gamma is product of feature
11 * gammas. The team gammas make for a probability distribution of
14 * We use the general pattern classifier that will find the features
15 * for us, and external datasets that can be harvested from a set
16 * of game records (see the HACKING file for details): patterns.spat
17 * as a dictionary of spatial stone configurations, and patterns.gamma
18 * with strengths of particular features. */
29 #include "patternsp.h"
31 #include "playout/elo.h"
34 #include "uct/prior.h"
36 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
39 /* Note that the context can be shared by multiple threads! */
43 struct pattern_config pc
;
44 struct features_gamma
*fg
;
49 struct patternset choose
, assess
;
50 playout_elo_callbackp callback
; void *callback_data
;
54 /* This is the core of the policy - initializes and constructs the
55 * probability distribution over the move candidates. */
58 elo_get_probdist(struct playout_policy
*p
, struct patternset
*ps
, struct board
*b
, enum stone to_play
, struct probdist
*pd
)
60 //struct elo_policy *pp = p->data;
63 /* First, assign per-point probabilities. */
65 for (int f
= 0; f
< b
->flen
; f
++) {
66 struct move m
= { .coord
= b
->f
[f
], .color
= to_play
};
68 /* Skip pass (for now)? */
69 if (is_pass(m
.coord
)) {
71 probdist_set(pd
, f
, 0);
74 //fprintf(stderr, "<%d> %s\n", f, coord2sstr(m.coord, b));
76 /* Skip invalid moves. */
77 if (!board_is_valid_move(b
, &m
))
80 /* We shall never fill our own single-point eyes. */
81 /* XXX: In some rare situations, this prunes the best move:
82 * Bulk-five nakade with eye at 1-1 point. */
83 if (board_is_one_point_eye(b
, m
.coord
, to_play
)) {
88 /* Each valid move starts with gamma 1. */
91 /* Some easy features: */
92 /* XXX: We just disable them for now since we call the
93 * pattern matcher; you need the gammas file. */
95 if (is_bad_selfatari(b
, to_play
, m
.coord
))
99 /* Match pattern features: */
101 pattern_match(&ps
->pc
, ps
->ps
, &p
, b
, &m
);
102 for (int i
= 0; i
< p
.n
; i
++) {
103 /* Multiply together gammas of all pattern features. */
104 double gamma
= feature_gamma(ps
->fg
, &p
.f
[i
], NULL
);
105 //char buf[256] = ""; feature2str(buf, &p.f[i]);
106 //fprintf(stderr, "<%d> %s feat %s gamma %f\n", f, coord2sstr(m.coord, b), buf, gamma);
110 probdist_set(pd
, f
, g
);
111 //fprintf(stderr, "<%d> %s %f (E %f)\n", f, coord2sstr(m.coord, b), probdist_one(pd, f), pd->items[f]);
119 elo_check_probdist(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct probdist
*pd
)
122 struct elo_policy
*pp
= p
->data
;
123 if (pd
->total
< PROBDIST_EPSILON
)
126 /* Compare to the manually created distribution. */
128 double pdi
[b
->flen
]; memset(pdi
, 0, sizeof(pdi
));
129 struct probdist pdx
= { .n
= b
->flen
, .items
= pdi
, .total
= 0 };
130 elo_get_probdist(p
, &pp
->choose
, b
, to_play
, &pdx
);
131 for (int i
= 0; i
< pdx
.n
; i
++) {
132 if (is_pass(b
->f
[i
])) continue;
133 if (fabs(pdx
.items
[i
] - pd
->items
[b
->f
[i
]]) < PROBDIST_EPSILON
)
135 printf("[%s %d] manual %f board %f ", coord2sstr(b
->f
[i
], b
), b
->pat3
[b
->f
[i
]], pdx
.items
[i
], pd
->items
[b
->f
[i
]]);
136 board_gamma_update(b
, b
->f
[i
], to_play
);
137 printf("plainboard %f\n", pd
->items
[b
->f
[i
]]);
144 playout_elo_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
146 struct elo_policy
*pp
= p
->data
;
148 struct probdist
*pd
= &b
->prob
[to_play
- 1];
149 /* Make sure ko-prohibited move does not get picked. */
150 if (!is_pass(b
->ko
.coord
)) {
151 assert(b
->ko
.color
== to_play
);
152 probdist_set(pd
, b
->ko
.coord
, 0);
154 /* Contiguity detection. */
155 if (!is_pass(b
->last_move
.coord
)) {
156 foreach_8neighbor(b
, b
->last_move
.coord
) {
157 probdist_set(pd
, c
, pd
->items
[c
] * b
->gamma
->gamma
[FEAT_CONTIGUITY
][1]);
158 } foreach_8neighbor_end
;
160 elo_check_probdist(p
, b
, to_play
, pd
);
161 /* The engine might want to adjust our probdist. */
163 pp
->callback(pp
->callback_data
, b
, to_play
, pd
);
165 coord_t c
= pd
->total
>= PROBDIST_EPSILON
? probdist_pick(pd
) : pass
;
166 /* Repair the damage. */
168 /* XXX: Do something less horribly inefficient
169 * than just recomputing the whole pd. */
171 for (int i
= 0; i
< b
->flen
; i
++) {
172 pd
->items
[b
->f
[i
]] = 0;
173 board_gamma_update(b
, b
->f
[i
], to_play
);
176 if (!is_pass(b
->ko
.coord
))
177 board_gamma_update(b
, b
->ko
.coord
, to_play
);
178 if (!is_pass(b
->last_move
.coord
)) {
179 foreach_8neighbor(b
, b
->last_move
.coord
) {
180 board_gamma_update(b
, c
, to_play
);
181 } foreach_8neighbor_end
;
186 double pdi
[b
->flen
]; memset(pdi
, 0, sizeof(pdi
));
187 struct probdist pd
= { .n
= b
->flen
, .items
= pdi
, .total
= 0 };
188 elo_get_probdist(p
, &pp
->choose
, b
, to_play
, &pd
);
190 pp
->callback(pp
->callback_data
, b
, to_play
, &pd
);
191 if (pd
.total
< PROBDIST_EPSILON
)
193 int f
= probdist_pick(&pd
);
199 playout_elo_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
201 struct elo_policy
*pp
= p
->data
;
202 double pdi
[map
->b
->flen
]; memset(pdi
, 0, sizeof(pdi
));
203 struct probdist pd
= { .n
= map
->b
->flen
, .items
= pdi
, .total
= 0 };
206 moves
= elo_get_probdist(p
, &pp
->assess
, map
->b
, map
->to_play
, &pd
);
208 /* It is a question how to transform the gamma to won games; we use
209 * a naive approach currently, but not sure how well it works. */
210 /* TODO: Try sqrt(p), atan(p)/pi*2. */
212 for (int f
= 0; f
< map
->b
->flen
; f
++) {
213 coord_t c
= map
->b
->f
[f
];
214 if (!map
->consider
[c
])
216 add_prior_value(map
, c
, probdist_one(&pd
, f
) / probdist_total(&pd
), games
);
221 playout_elo_done(struct playout_policy
*p
)
223 struct elo_policy
*pp
= p
->data
;
224 features_gamma_done(pp
->choose
.fg
);
225 features_gamma_done(pp
->assess
.fg
);
230 playout_elo_callback(struct playout_policy
*p
, playout_elo_callbackp callback
, void *data
)
232 struct elo_policy
*pp
= p
->data
;
233 pp
->callback
= callback
;
234 pp
->callback_data
= data
;
237 struct playout_policy
*
238 playout_elo_init(char *arg
, struct board
*b
)
240 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
241 struct elo_policy
*pp
= calloc2(1, sizeof(*pp
));
243 p
->choose
= playout_elo_choose
;
244 p
->assess
= playout_elo_assess
;
245 p
->done
= playout_elo_done
;
247 const char *gammafile
= features_gamma_filename
;
248 /* Some defaults based on the table in Remi Coulom's paper. */
249 pp
->selfatari
= 0.06;
251 struct pattern_config pc
= DEFAULT_PATTERN_CONFIG
;
253 bool precise_selfatari
= false;
256 char *optspec
, *next
= arg
;
259 next
+= strcspn(next
, ":");
260 if (*next
) { *next
++ = 0; } else { *next
= 0; }
262 char *optname
= optspec
;
263 char *optval
= strchr(optspec
, '=');
264 if (optval
) *optval
++ = 0;
266 if (!strcasecmp(optname
, "selfatari") && optval
) {
267 pp
->selfatari
= atof(optval
);
268 } else if (!strcasecmp(optname
, "precisesa")) {
269 /* Use precise self-atari detection within
271 precise_selfatari
= !optval
|| atoi(optval
);
272 } else if (!strcasecmp(optname
, "gammafile") && optval
) {
273 /* patterns.gamma by default. We use this,
274 * and need also ${gammafile}f (e.g.
275 * patterns.gammaf) for fast (MC) features. */
276 gammafile
= strdup(optval
);
277 } else if (!strcasecmp(optname
, "xspat") && optval
) {
278 /* xspat==0: don't match spatial features
279 * xspat==1: match *only* spatial features */
280 xspat
= atoi(optval
);
282 fprintf(stderr
, "playout-elo: Invalid policy argument %s or missing value\n", optname
);
288 pc
.spat_dict
= spatial_dict_init(false);
291 pp
->assess
.fg
= features_gamma_init(&pp
->assess
.pc
, gammafile
);
292 memcpy(pp
->assess
.ps
, PATTERN_SPEC_MATCHALL
, sizeof(pattern_spec
));
293 for (int i
= 0; i
< FEAT_MAX
; i
++)
294 if ((xspat
== 0 && i
== FEAT_SPATIAL
) || (xspat
== 1 && i
!= FEAT_SPATIAL
))
295 pp
->assess
.ps
[i
] = 0;
297 /* In playouts, we need to operate with much smaller set of features
298 * in order to keep reasonable speed. */
299 /* TODO: Configurable. */ /* TODO: Tune. */
300 pp
->choose
.pc
= FAST_PATTERN_CONFIG
;
301 pp
->choose
.pc
.spat_dict
= pc
.spat_dict
;
302 char cgammafile
[256]; strcpy(stpcpy(cgammafile
, gammafile
), "f");
303 pp
->choose
.fg
= features_gamma_init(&pp
->choose
.pc
, cgammafile
);
304 memcpy(pp
->choose
.ps
, PATTERN_SPEC_MATCHFAST
, sizeof(pattern_spec
));
305 for (int i
= 0; i
< FEAT_MAX
; i
++)
306 if ((xspat
== 0 && i
== FEAT_SPATIAL
) || (xspat
== 1 && i
!= FEAT_SPATIAL
))
307 pp
->choose
.ps
[i
] = 0;
308 if (precise_selfatari
)
309 pp
->choose
.ps
[FEAT_SELFATARI
] = ~(1<<PF_SELFATARI_STUPID
);
310 board_gamma_set(b
, pp
->choose
.fg
, precise_selfatari
);