Pattern gamma: Fix payload count for FEAT_L(L)DIST and FEAT_BORDER
[pachi.git] / pattern.c
blob1bbe2c2f8581636fdf851f4f90e868db6fb641ba
1 #define DEBUG
2 #include <assert.h>
3 #include <ctype.h>
4 #include <inttypes.h>
5 #include <stdio.h>
6 #include <stdlib.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "pattern.h"
11 #include "patternsp.h"
12 #include "tactics.h"
15 struct pattern_config DEFAULT_PATTERN_CONFIG = {
16 .spat_min = 3, .spat_max = MAX_PATTERN_DIST,
17 .bdist_max = 4,
18 .ldist_min = 0, .ldist_max = 256,
19 .mcsims = 0, /* Unsupported. */
22 struct pattern_config FAST_PATTERN_CONFIG = {
23 .spat_min = 3, .spat_max = 5,
24 .bdist_max = 4,
25 .ldist_min = 0, .ldist_max = 256,
26 .mcsims = 0,
29 pattern_spec PATTERN_SPEC_MATCHALL = {
30 [FEAT_PASS] = ~0,
31 [FEAT_CAPTURE] = ~0,
32 [FEAT_AESCAPE] = ~0,
33 [FEAT_SELFATARI] = ~0,
34 [FEAT_ATARI] = ~0,
35 [FEAT_BORDER] = ~0,
36 [FEAT_LDIST] = ~0,
37 [FEAT_LLDIST] = ~0,
38 [FEAT_SPATIAL] = ~0,
39 [FEAT_MCOWNER] = ~0,
42 pattern_spec PATTERN_SPEC_MATCHFAST = {
43 [FEAT_PASS] = ~0,
44 [FEAT_CAPTURE] = ~(1<<PF_CAPTURE_ATARIDEF)|~(1<<PF_CAPTURE_RECAPTURE),
45 [FEAT_AESCAPE] = ~0,
46 [FEAT_SELFATARI] = ~(1<<PF_SELFATARI_SMART),
47 [FEAT_ATARI] = ~0,
48 [FEAT_BORDER] = ~0,
49 [FEAT_LDIST] = ~0,
50 [FEAT_LLDIST] = ~0,
51 [FEAT_SPATIAL] = ~0,
52 [FEAT_MCOWNER] = ~0,
55 static const struct feature_info {
56 char *name;
57 int payloads;
58 } features[FEAT_MAX] = {
59 [FEAT_PASS] = { .name = "pass", .payloads = 2 },
60 [FEAT_CAPTURE] = { .name = "capture", .payloads = 16 },
61 [FEAT_AESCAPE] = { .name = "atariescape", .payloads = 2 },
62 [FEAT_SELFATARI] = { .name = "selfatari", .payloads = 2 },
63 [FEAT_ATARI] = { .name = "atari", .payloads = 4 },
64 [FEAT_BORDER] = { .name = "border", .payloads = -1 },
65 [FEAT_LDIST] = { .name = "ldist", .payloads = -1 },
66 [FEAT_LLDIST] = { .name = "lldist", .payloads = -1 },
67 [FEAT_SPATIAL] = { .name = "s", .payloads = -1 },
68 [FEAT_MCOWNER] = { .name = "mcowner", .payloads = 16 },
71 char *
72 feature2str(char *str, struct feature *f)
74 return str + sprintf(str + strlen(str), "%s:%"PRIx32, features[f->id].name, f->payload);
77 char *
78 str2feature(char *str, struct feature *f)
80 while (isspace(*str)) str++;
82 int flen = strcspn(str, ":");
83 for (int i = 0; i < sizeof(features)/sizeof(features[0]); i++)
84 if (strlen(features[i].name) == flen && !strncmp(features[i].name, str, flen)) {
85 f->id = i;
86 goto found;
88 fprintf(stderr, "invalid featurespec: %s[%d]\n", str, flen);
89 exit(EXIT_FAILURE);
91 found:
92 str += flen + 1;
93 f->payload = strtoull(str, &str, 16);
94 return str;
98 /* pattern_spec helpers */
99 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << 31))
100 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
102 static struct feature *
103 pattern_match_capture(struct pattern_config *pc, pattern_spec ps,
104 struct pattern *p, struct feature *f,
105 struct board *b, struct move *m)
107 foreach_neighbor(b, m->coord, {
108 if (board_at(b, c) != stone_other(m->color))
109 continue;
110 group_t g = group_at(b, c);
111 if (!g || board_group_info(b, g).libs != 1)
112 continue;
114 /* Capture! */
115 f->id = FEAT_CAPTURE; f->payload = 0;
117 if (PS_PF(CAPTURE, LADDER))
118 f->payload |= is_ladder(b, m->coord, g, true, true) << PF_CAPTURE_LADDER;
119 /* TODO: is_ladder() is too conservative in some
120 * very obvious situations, look at complete.gtp. */
122 /* TODO: PF_CAPTURE_RECAPTURE */
124 if (PS_PF(CAPTURE, ATARIDEF))
125 foreach_in_group(b, g) {
126 foreach_neighbor(b, c, {
127 assert(board_at(b, c) != S_NONE || c == m->coord);
128 if (board_at(b, c) != m->color)
129 continue;
130 group_t g = group_at(b, c);
131 if (!g || board_group_info(b, g).libs != 1)
132 continue;
133 /* A neighboring group of ours is in atari. */
134 f->payload |= 1 << PF_CAPTURE_ATARIDEF;
136 } foreach_in_group_end;
138 if (PS_PF(CAPTURE, KO)
139 && group_is_onestone(b, g)
140 && neighbor_count_at(b, m->coord, stone_other(m->color))
141 + neighbor_count_at(b, m->coord, S_OFFBOARD) == 4)
142 f->payload |= 1 << PF_CAPTURE_KO;
144 (f++, p->n++);
146 return f;
149 static struct feature *
150 pattern_match_aescape(struct pattern_config *pc, pattern_spec ps,
151 struct pattern *p, struct feature *f,
152 struct board *b, struct move *m)
154 /* Find if a neighboring group of ours is in atari, AND that we provide
155 * a liberty to connect out. XXX: No connect-and-die check. */
156 group_t in_atari = -1;
157 bool has_extra_lib = false;
158 int payload = 0;
160 foreach_neighbor(b, m->coord, {
161 if (board_at(b, c) != m->color) {
162 if (board_at(b, c) == S_NONE)
163 has_extra_lib = true; // free point
164 else if (board_at(b, c) == stone_other(m->color) && board_group_info(b, group_at(b, c)).libs == 1)
165 has_extra_lib = true; // capturable enemy group
166 continue;
168 group_t g = group_at(b, c); assert(g);
169 if (board_group_info(b, g).libs != 1) {
170 has_extra_lib = true;
171 continue;
174 /* In atari! */
175 in_atari = g;
177 if (PS_PF(AESCAPE, LADDER))
178 payload |= is_ladder(b, m->coord, g, true, true) << PF_AESCAPE_LADDER;
179 /* TODO: is_ladder() is too conservative in some
180 * very obvious situations, look at complete.gtp. */
182 if (in_atari >= 0 && has_extra_lib) {
183 f->id = FEAT_AESCAPE; f->payload = payload;
184 (f++, p->n++);
186 return f;
189 static struct feature *
190 pattern_match_atari(struct pattern_config *pc, pattern_spec ps,
191 struct pattern *p, struct feature *f,
192 struct board *b, struct move *m)
194 foreach_neighbor(b, m->coord, {
195 if (board_at(b, c) != stone_other(m->color))
196 continue;
197 group_t g = group_at(b, c);
198 if (!g || board_group_info(b, g).libs != 2)
199 continue;
201 /* Can atari! */
202 f->id = FEAT_ATARI; f->payload = 0;
204 if (PS_PF(ATARI, LADDER)) {
205 /* Opponent will escape by the other lib. */
206 coord_t lib = board_group_info(b, g).lib[0];
207 if (lib == m->coord) lib = board_group_info(b, g).lib[1];
208 /* TODO: is_ladder() is too conservative in some
209 * very obvious situations, look at complete.gtp. */
210 f->payload |= is_ladder(b, lib, g, true, true) << PF_ATARI_LADDER;
213 if (PS_PF(ATARI, KO) && !is_pass(b->ko.coord))
214 f->payload |= 1 << PF_ATARI_KO;
216 (f++, p->n++);
218 return f;
221 struct feature *
222 pattern_match_spatial(struct pattern_config *pc, pattern_spec ps,
223 struct pattern *p, struct feature *f,
224 struct board *b, struct move *m)
226 /* XXX: This is partially duplicated from spatial_from_board(), but
227 * we build a hash instead of spatial record. */
229 assert(pc->spat_min > 0);
231 /* We record all spatial patterns black-to-play; simply
232 * reverse all colors if we are white-to-play. */
233 static enum stone bt_black[4] = { S_NONE, S_BLACK, S_WHITE, S_OFFBOARD };
234 static enum stone bt_white[4] = { S_NONE, S_WHITE, S_BLACK, S_OFFBOARD };
235 enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black;
237 hash_t h = pthashes[0][0][S_NONE];
238 #ifdef BOARD_SPATHASH
239 for (int d = 2; d <= BOARD_SPATHASH_MAXD; d++) {
240 /* Reuse all incrementally matched data. */
241 h ^= b->spathash[m->coord][d - 1];
242 if (d < pc->spat_min)
243 continue;
244 /* Record spatial feature, one per distance. */
245 f->id = FEAT_SPATIAL;
246 f->payload = (d << PF_SPATIAL_RADIUS);
247 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
248 if (sid > 0) {
249 f->payload |= sid << PF_SPATIAL_INDEX;
250 (f++, p->n++);
251 } /* else not found, ignore */
253 #else
254 #undef BOARD_SPATHASH_MAXD
255 #define BOARD_SPATHASH_MAXD 1
256 #endif
257 for (int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) {
258 /* Recompute missing outer circles:
259 * Go through all points in given distance. */
260 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
261 ptcoords_at(x, y, m->coord, b, j);
262 h ^= pthashes[0][j][(*bt)[board_atxy(b, x, y)]];
264 if (d < pc->spat_min)
265 continue;
266 /* Record spatial feature, one per distance. */
267 f->id = FEAT_SPATIAL;
268 f->payload = (d << PF_SPATIAL_RADIUS);
269 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
270 if (sid > 0) {
271 f->payload |= sid << PF_SPATIAL_INDEX;
272 (f++, p->n++);
273 } /* else not found, ignore */
275 return f;
279 static bool
280 is_simple_selfatari(struct board *b, enum stone color, coord_t coord)
282 /* Very rough check, no connect-and-die checks or other trickery. */
283 int libs = immediate_liberty_count(b, coord);
284 if (libs >= 2) return false; // open space
286 group_t seen = -1;
287 foreach_neighbor(b, coord, {
288 if (board_at(b, c) == stone_other(color) && board_group_info(b, group_at(b, c)).libs == 1) {
289 return false; // can capture
291 } else if (board_at(b, c) == color) {
292 // friendly group, does it have liberties?
293 group_t g = group_at(b, c);
294 if (board_group_info(b, g).libs == 1 || seen == g)
295 continue;
296 libs += board_group_info(b, g).libs - 1;
297 if (libs >= 2) return false;
298 // don't consider the same group twice
299 seen = g;
302 return true;
305 void
306 pattern_match(struct pattern_config *pc, pattern_spec ps,
307 struct pattern *p, struct board *b, struct move *m)
309 p->n = 0;
310 struct feature *f = &p->f[0];
312 /* TODO: We should match pretty much all of these features
313 * incrementally. */
315 if (is_pass(m->coord)) {
316 if (PS_ANY(PASS)) {
317 f->id = FEAT_PASS; f->payload = 0;
318 if (PS_PF(PASS, LASTPASS))
319 f->payload |= (b->moves > 0 && is_pass(b->last_move.coord))
320 << PF_PASS_LASTPASS;
321 p->n++;
323 return;
326 if (PS_ANY(CAPTURE)) {
327 f = pattern_match_capture(pc, ps, p, f, b, m);
330 if (PS_ANY(AESCAPE)) {
331 f = pattern_match_aescape(pc, ps, p, f, b, m);
334 if (PS_ANY(SELFATARI)) {
335 bool simple = is_simple_selfatari(b, m->color, m->coord);
336 bool thorough = false;
337 if (PS_PF(SELFATARI, SMART)) {
338 thorough = is_bad_selfatari(b, m->color, m->coord);
340 if (simple || thorough) {
341 f->id = FEAT_SELFATARI;
342 f->payload = thorough << PF_SELFATARI_SMART;
343 (f++, p->n++);
347 if (PS_ANY(ATARI)) {
348 f = pattern_match_atari(pc, ps, p, f, b, m);
351 if (PS_ANY(BORDER)) {
352 int bdist = coord_edge_distance(m->coord, b);
353 if (bdist <= pc->bdist_max) {
354 f->id = FEAT_BORDER;
355 f->payload = bdist;
356 (f++, p->n++);
360 if (PS_ANY(LDIST) && pc->ldist_max > 0 && !is_pass(b->last_move.coord)) {
361 int ldist = coord_gridcular_distance(m->coord, b->last_move.coord, b);
362 if (pc->ldist_min <= ldist && ldist <= pc->ldist_max) {
363 f->id = FEAT_LDIST;
364 f->payload = ldist;
365 (f++, p->n++);
369 if (PS_ANY(LLDIST) && pc->ldist_max > 0 && !is_pass(b->last_move2.coord)) {
370 int lldist = coord_gridcular_distance(m->coord, b->last_move2.coord, b);
371 if (pc->ldist_min <= lldist && lldist <= pc->ldist_max) {
372 f->id = FEAT_LLDIST;
373 f->payload = lldist;
374 (f++, p->n++);
378 if (PS_ANY(SPATIAL) && pc->spat_max > 0 && pc->spat_dict) {
379 f = pattern_match_spatial(pc, ps, p, f, b, m);
382 /* FEAT_MCOWNER: TODO */
383 assert(!pc->mcsims);
386 char *
387 pattern2str(char *str, struct pattern *p)
389 str = stpcpy(str, "(");
390 for (int i = 0; i < p->n; i++) {
391 if (i > 0) str = stpcpy(str, " ");
392 str = feature2str(str, &p->f[i]);
394 str = stpcpy(str, ")");
395 return str;
400 /*** Features gamma set */
402 static void
403 features_gamma_load(struct features_gamma *fg, const char *filename)
405 FILE *f = fopen(filename, "r");
406 if (!f) return;
407 char buf[256];
408 while (fgets(buf, 256, f)) {
409 char *bufp = buf;
410 struct feature f;
411 bufp = str2feature(bufp, &f);
412 while (isspace(*bufp)) bufp++;
413 float gamma = strtof(bufp, &bufp);
414 feature_gamma(fg, &f, &gamma);
416 fclose(f);
419 const char *features_gamma_filename = "patterns.gamma";
421 struct features_gamma *
422 features_gamma_init(struct pattern_config *pc, const char *file)
424 struct features_gamma *fg = calloc(1, sizeof(*fg));
425 fg->pc = pc;
426 for (int i = 0; i < FEAT_MAX; i++) {
427 int n = features[i].payloads;
428 if (n <= 0) {
429 switch (i) {
430 case FEAT_SPATIAL:
431 n = pc->spat_dict->nspatials; break;
432 case FEAT_LDIST:
433 case FEAT_LLDIST:
434 n = pc->ldist_max + 1; break;
435 case FEAT_BORDER:
436 n = pc->bdist_max + 1; break;
437 default:
438 assert(0);
441 fg->gamma[i] = malloc(n * sizeof(float));
442 for (int j = 0; j < n; j++) {
443 fg->gamma[i][j] = 1.0f;
446 features_gamma_load(fg, file ? file : features_gamma_filename);
447 return fg;
450 float
451 feature_gamma(struct features_gamma *fg, struct feature *f, float *gamma)
453 /* XXX: We mask out spatial distance unconditionally since it shouldn't
454 * affect any other feature. */
455 int payid = f->payload & ((1<<24)-1);
456 if (gamma) fg->gamma[f->id][payid] = *gamma;
457 return fg->gamma[f->id][payid];