Pattern fast: Fix FEAT_CAPTURE patternspec
[pachi.git] / pattern.c
blob767339fc71ec2b992f2a676428088b38329a43b7
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 = 3,
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 bool w_to_play = m->color == S_WHITE;
236 enum stone (*bt)[4] = w_to_play ? &bt_white : &bt_black;
238 hash_t h = pthashes[0][0][S_NONE];
239 #ifdef BOARD_SPATHASH
240 for (int d = 2; d <= BOARD_SPATHASH_MAXD; d++) {
241 /* Reuse all incrementally matched data. */
242 h ^= b->spathash[m->coord][d - 1][w_to_play];
243 if (d < pc->spat_min)
244 continue;
245 /* Record spatial feature, one per distance. */
246 f->id = FEAT_SPATIAL;
247 f->payload = (d << PF_SPATIAL_RADIUS);
248 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
249 if (sid > 0) {
250 f->payload |= sid << PF_SPATIAL_INDEX;
251 (f++, p->n++);
252 } /* else not found, ignore */
254 #else
255 #undef BOARD_SPATHASH_MAXD
256 #define BOARD_SPATHASH_MAXD 1
257 #endif
258 for (int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) {
259 /* Recompute missing outer circles:
260 * Go through all points in given distance. */
261 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
262 ptcoords_at(x, y, m->coord, b, j);
263 h ^= pthashes[0][j][(*bt)[board_atxy(b, x, y)]];
265 if (d < pc->spat_min)
266 continue;
267 /* Record spatial feature, one per distance. */
268 f->id = FEAT_SPATIAL;
269 f->payload = (d << PF_SPATIAL_RADIUS);
270 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
271 if (sid > 0) {
272 f->payload |= sid << PF_SPATIAL_INDEX;
273 (f++, p->n++);
274 } /* else not found, ignore */
276 return f;
280 static bool
281 is_simple_selfatari(struct board *b, enum stone color, coord_t coord)
283 /* Very rough check, no connect-and-die checks or other trickery. */
284 int libs = immediate_liberty_count(b, coord);
285 if (libs >= 2) return false; // open space
287 group_t seen = -1;
288 foreach_neighbor(b, coord, {
289 if (board_at(b, c) == stone_other(color) && board_group_info(b, group_at(b, c)).libs == 1) {
290 return false; // can capture
292 } else if (board_at(b, c) == color) {
293 // friendly group, does it have liberties?
294 group_t g = group_at(b, c);
295 if (board_group_info(b, g).libs == 1 || seen == g)
296 continue;
297 libs += board_group_info(b, g).libs - 1;
298 if (libs >= 2) return false;
299 // don't consider the same group twice
300 seen = g;
303 return true;
306 void
307 pattern_match(struct pattern_config *pc, pattern_spec ps,
308 struct pattern *p, struct board *b, struct move *m)
310 p->n = 0;
311 struct feature *f = &p->f[0];
313 /* TODO: We should match pretty much all of these features
314 * incrementally. */
316 if (is_pass(m->coord)) {
317 if (PS_ANY(PASS)) {
318 f->id = FEAT_PASS; f->payload = 0;
319 if (PS_PF(PASS, LASTPASS))
320 f->payload |= (b->moves > 0 && is_pass(b->last_move.coord))
321 << PF_PASS_LASTPASS;
322 p->n++;
324 return;
327 if (PS_ANY(CAPTURE)) {
328 f = pattern_match_capture(pc, ps, p, f, b, m);
331 if (PS_ANY(AESCAPE)) {
332 f = pattern_match_aescape(pc, ps, p, f, b, m);
335 if (PS_ANY(SELFATARI)) {
336 bool simple = is_simple_selfatari(b, m->color, m->coord);
337 bool thorough = false;
338 if (PS_PF(SELFATARI, SMART)) {
339 thorough = is_bad_selfatari(b, m->color, m->coord);
341 if (simple || thorough) {
342 f->id = FEAT_SELFATARI;
343 f->payload = thorough << PF_SELFATARI_SMART;
344 (f++, p->n++);
348 if (PS_ANY(ATARI)) {
349 f = pattern_match_atari(pc, ps, p, f, b, m);
352 if (PS_ANY(BORDER)) {
353 int bdist = coord_edge_distance(m->coord, b);
354 if (bdist <= pc->bdist_max) {
355 f->id = FEAT_BORDER;
356 f->payload = bdist;
357 (f++, p->n++);
361 if (PS_ANY(LDIST) && pc->ldist_max > 0 && !is_pass(b->last_move.coord)) {
362 int ldist = coord_gridcular_distance(m->coord, b->last_move.coord, b);
363 if (pc->ldist_min <= ldist && ldist <= pc->ldist_max) {
364 f->id = FEAT_LDIST;
365 f->payload = ldist;
366 (f++, p->n++);
370 if (PS_ANY(LLDIST) && pc->ldist_max > 0 && !is_pass(b->last_move2.coord)) {
371 int lldist = coord_gridcular_distance(m->coord, b->last_move2.coord, b);
372 if (pc->ldist_min <= lldist && lldist <= pc->ldist_max) {
373 f->id = FEAT_LLDIST;
374 f->payload = lldist;
375 (f++, p->n++);
379 if (PS_ANY(SPATIAL) && pc->spat_max > 0 && pc->spat_dict) {
380 f = pattern_match_spatial(pc, ps, p, f, b, m);
383 /* FEAT_MCOWNER: TODO */
384 assert(!pc->mcsims);
387 char *
388 pattern2str(char *str, struct pattern *p)
390 str = stpcpy(str, "(");
391 for (int i = 0; i < p->n; i++) {
392 if (i > 0) str = stpcpy(str, " ");
393 str = feature2str(str, &p->f[i]);
395 str = stpcpy(str, ")");
396 return str;
401 /*** Features gamma set */
403 static void
404 features_gamma_load(struct features_gamma *fg, const char *filename)
406 FILE *f = fopen(filename, "r");
407 if (!f) return;
408 char buf[256];
409 while (fgets(buf, 256, f)) {
410 char *bufp = buf;
411 struct feature f;
412 bufp = str2feature(bufp, &f);
413 while (isspace(*bufp)) bufp++;
414 float gamma = strtof(bufp, &bufp);
415 feature_gamma(fg, &f, &gamma);
417 fclose(f);
420 const char *features_gamma_filename = "patterns.gamma";
422 struct features_gamma *
423 features_gamma_init(struct pattern_config *pc, const char *file)
425 struct features_gamma *fg = calloc(1, sizeof(*fg));
426 fg->pc = pc;
427 for (int i = 0; i < FEAT_MAX; i++) {
428 int n = features[i].payloads;
429 if (n <= 0) {
430 switch (i) {
431 case FEAT_SPATIAL:
432 n = pc->spat_dict->nspatials; break;
433 case FEAT_LDIST:
434 case FEAT_LLDIST:
435 n = pc->ldist_max + 1; break;
436 case FEAT_BORDER:
437 n = pc->bdist_max + 1; break;
438 default:
439 assert(0);
442 fg->gamma[i] = malloc(n * sizeof(float));
443 for (int j = 0; j < n; j++) {
444 fg->gamma[i][j] = 1.0f;
447 features_gamma_load(fg, file ? file : features_gamma_filename);
448 return fg;