[FEAT_CAPTURE] payloads: Include countstones payloads only in feature_payloads()...
[pachi.git] / pattern.c
blob6726e4eccbf1da86ca16fee99604827c276cbb48
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 "patternprob.h"
13 #include "tactics/ladder.h"
14 #include "tactics/selfatari.h"
15 #include "tactics/util.h"
17 #define CAPTURE_COUNTSTONES_MAX ((1 << CAPTURE_COUNTSTONES_PAYLOAD_SIZE) - 1)
20 struct pattern_config DEFAULT_PATTERN_CONFIG = {
21 .bdist_max = 4,
23 .spat_min = 3, .spat_max = MAX_PATTERN_DIST,
24 .spat_largest = true,
27 #define PF_MATCH 15
29 pattern_spec PATTERN_SPEC_MATCH_DEFAULT = {
30 [FEAT_CAPTURE] = ~0,
31 [FEAT_AESCAPE] = ~0,
32 [FEAT_SELFATARI] = ~0,
33 [FEAT_ATARI] = ~0,
34 [FEAT_BORDER] = ~0,
35 [FEAT_CONTIGUITY] = 0,
36 [FEAT_SPATIAL] = ~0,
39 static const struct feature_info {
40 char *name;
41 int payloads;
42 } features[FEAT_MAX] = {
43 [FEAT_CAPTURE] = { .name = "capture", .payloads = 64 },
44 [FEAT_AESCAPE] = { .name = "atariescape", .payloads = 16 },
45 [FEAT_SELFATARI] = { .name = "selfatari", .payloads = 4 },
46 [FEAT_ATARI] = { .name = "atari", .payloads = 4 },
47 [FEAT_BORDER] = { .name = "border", .payloads = -1 },
48 [FEAT_CONTIGUITY] = { .name = "cont", .payloads = 2 },
49 [FEAT_SPATIAL] = { .name = "s", .payloads = -1 },
52 char *
53 feature2str(char *str, struct feature *f)
55 return str + sprintf(str + strlen(str), "%s:%d", features[f->id].name, f->payload);
58 char *
59 str2feature(char *str, struct feature *f)
61 while (isspace(*str)) str++;
63 int unsigned flen = strcspn(str, ":");
64 for (unsigned int i = 0; i < sizeof(features)/sizeof(features[0]); i++)
65 if (strlen(features[i].name) == flen && !strncmp(features[i].name, str, flen)) {
66 f->id = i;
67 goto found;
69 fprintf(stderr, "invalid featurespec: %s[%d]\n", str, flen);
70 exit(EXIT_FAILURE);
72 found:
73 str += flen + 1;
74 f->payload = strtoull(str, &str, 10);
75 return str;
78 char *
79 feature_name(enum feature_id f)
81 return features[f].name;
84 int
85 feature_payloads(struct pattern_setup *pat, enum feature_id f)
87 switch (f) {
88 case FEAT_CAPTURE:
89 int payloads = features[f].payloads;
90 if (pat->ps[FEAT_CAPTURE] & (1<<PF_CAPTURE_COUNTSTONES))
91 payloads *= CAPTURE_COUNTSTONES_MAX + 1;
92 return payloads;
93 case FEAT_SPATIAL:
94 assert(features[f].payloads < 0);
95 return pat->pc.spat_dict->nspatials;
96 case FEAT_BORDER:
97 assert(features[f].payloads < 0);
98 return pat->pc.bdist_max + 1;
99 default:
100 assert(features[f].payloads > 0);
101 return features[f].payloads;
106 void
107 patterns_init(struct pattern_setup *pat, char *arg, bool will_append, bool load_prob)
109 char *pdict_file = NULL;
111 memset(pat, 0, sizeof(*pat));
113 pat->pc = DEFAULT_PATTERN_CONFIG;
114 pat->pc.spat_dict = spatial_dict_init(will_append, !load_prob);
116 memcpy(&pat->ps, PATTERN_SPEC_MATCH_DEFAULT, sizeof(pattern_spec));
118 if (arg) {
119 char *optspec, *next = arg;
120 while (*next) {
121 optspec = next;
122 next += strcspn(next, ":");
123 if (*next) { *next++ = 0; } else { *next = 0; }
125 char *optname = optspec;
126 char *optval = strchr(optspec, '=');
127 if (optval) *optval++ = 0;
129 /* See pattern.h:pattern_config for description and
130 * pattern.c:DEFAULT_PATTERN_CONFIG for default values
131 * of the following options. */
132 if (!strcasecmp(optname, "bdist_max") && optval) {
133 pat->pc.bdist_max = atoi(optval);
134 } else if (!strcasecmp(optname, "spat_min") && optval) {
135 pat->pc.spat_min = atoi(optval);
136 } else if (!strcasecmp(optname, "spat_max") && optval) {
137 pat->pc.spat_max = atoi(optval);
138 } else if (!strcasecmp(optname, "spat_largest")) {
139 pat->pc.spat_largest = !optval || atoi(optval);
141 } else if (!strcasecmp(optname, "pdict_file") && optval) {
142 pdict_file = optval;
144 } else {
145 fprintf(stderr, "patterns: Invalid argument %s or missing value\n", optname);
146 exit(EXIT_FAILURE);
151 if (load_prob && pat->pc.spat_dict) {
152 pat->pd = pattern_pdict_init(pdict_file, &pat->pc);
157 /* pattern_spec helpers */
158 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << PF_MATCH))
159 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
161 static struct feature *
162 pattern_match_capture(struct pattern_config *pc, pattern_spec ps,
163 struct pattern *p, struct feature *f,
164 struct board *b, struct move *m)
166 f->id = FEAT_CAPTURE; f->payload = 0;
167 #ifdef BOARD_TRAITS
168 if (!trait_at(b, m->coord, m->color).cap)
169 return f;
170 /* Capturable! */
171 if ((ps[FEAT_CAPTURE] & ~(1<<PF_CAPTURE_1STONE | 1<<PF_CAPTURE_TRAPPED | 1<<PF_CAPTURE_CONNECTION)) == 1<<PF_MATCH) {
172 if (PS_PF(CAPTURE, 1STONE))
173 f->payload |= (trait_at(b, m->coord, m->color).cap1 == trait_at(b, m->coord, m->color).cap) << PF_CAPTURE_1STONE;
174 if (PS_PF(CAPTURE, TRAPPED))
175 f->payload |= (!trait_at(b, m->coord, stone_other(m->color)).safe) << PF_CAPTURE_TRAPPED;
176 if (PS_PF(CAPTURE, CONNECTION))
177 f->payload |= (trait_at(b, m->coord, m->color).cap < neighbor_count_at(b, m->coord, stone_other(m->color))) << PF_CAPTURE_CONNECTION;
178 (f++, p->n++);
179 return f;
181 /* We need to know details, so we still have to go through
182 * the neighbors. */
183 #endif
185 /* We look at neighboring groups we could capture, and also if the
186 * opponent could save them. */
187 /* This is very similar in spirit to board_safe_to_play(), and almost
188 * a color inverse of pattern_match_aescape(). */
190 /* Whether an escape move would be safe for the opponent. */
191 int captures = 0;
192 coord_t onelib = -1;
193 int extra_libs = 0, connectable_groups = 0;
194 bool onestone = false, multistone = false;
195 int captured_stones = 0;
197 foreach_neighbor(b, m->coord, {
198 if (board_at(b, c) != stone_other(m->color)) {
199 if (board_at(b, c) == S_NONE)
200 extra_libs++; // free point
201 else if (board_at(b, c) == m->color && board_group_info(b, group_at(b, c)).libs == 1)
202 extra_libs += 2; // capturable enemy group
203 continue;
206 group_t g = group_at(b, c); assert(g);
207 if (board_group_info(b, g).libs > 1) {
208 connectable_groups++;
209 if (board_group_info(b, g).libs > 2) {
210 extra_libs += 2; // connected out
211 } else {
212 /* This is a bit tricky; we connect our 2-lib
213 * group to another 2-lib group, which counts
214 * as one liberty, but only if the other lib
215 * is not shared too. */
216 if (onelib == -1) {
217 onelib = board_group_other_lib(b, g, c);
218 extra_libs++;
219 } else {
220 if (c == onelib)
221 extra_libs--; // take that back
222 else
223 extra_libs++;
226 continue;
229 /* Capture! */
230 captures++;
232 if (PS_PF(CAPTURE, LADDER))
233 f->payload |= is_ladder(b, m->coord, g, true) << PF_CAPTURE_LADDER;
234 /* TODO: is_ladder() is too conservative in some
235 * very obvious situations, look at complete.gtp. */
237 if (PS_PF(CAPTURE, ATARIDEF))
238 foreach_in_group(b, g) {
239 foreach_neighbor(b, c, {
240 assert(board_at(b, c) != S_NONE || c == m->coord);
241 if (board_at(b, c) != m->color)
242 continue;
243 group_t g = group_at(b, c);
244 if (!g || board_group_info(b, g).libs != 1)
245 continue;
246 /* A neighboring group of ours is in atari. */
247 f->payload |= 1 << PF_CAPTURE_ATARIDEF;
249 } foreach_in_group_end;
251 if (PS_PF(CAPTURE, KO)
252 && group_is_onestone(b, g)
253 && neighbor_count_at(b, m->coord, stone_other(m->color))
254 + neighbor_count_at(b, m->coord, S_OFFBOARD) == 4)
255 f->payload |= 1 << PF_CAPTURE_KO;
257 if (PS_PF(CAPTURE, COUNTSTONES)
258 && captured_stones < CAPTURE_COUNTSTONES_MAX)
259 captured_stones += group_stone_count(b, g, CAPTURE_COUNTSTONES_MAX - captured_stones);
261 if (group_is_onestone(b, g))
262 onestone = true;
263 else
264 multistone = true;
267 if (captures > 0) {
268 if (PS_PF(CAPTURE, 1STONE))
269 f->payload |= (onestone && !multistone) << PF_CAPTURE_1STONE;
270 if (PS_PF(CAPTURE, TRAPPED))
271 f->payload |= (extra_libs < 2) << PF_CAPTURE_TRAPPED;
272 if (PS_PF(CAPTURE, CONNECTION))
273 f->payload |= (connectable_groups > 0) << PF_CAPTURE_CONNECTION;
274 if (PS_PF(CAPTURE, COUNTSTONES))
275 f->payload |= captured_stones << PF_CAPTURE_COUNTSTONES;
276 (f++, p->n++);
278 return f;
281 static struct feature *
282 pattern_match_aescape(struct pattern_config *pc, pattern_spec ps,
283 struct pattern *p, struct feature *f,
284 struct board *b, struct move *m)
286 f->id = FEAT_AESCAPE; f->payload = 0;
287 #ifdef BOARD_TRAITS
288 if (!trait_at(b, m->coord, stone_other(m->color)).cap)
289 return f;
290 /* Opponent can capture something! */
291 if ((ps[FEAT_AESCAPE] & ~(1<<PF_AESCAPE_1STONE | 1<<PF_AESCAPE_TRAPPED | 1<<PF_AESCAPE_CONNECTION)) == 1<<PF_MATCH) {
292 if (PS_PF(AESCAPE, 1STONE))
293 f->payload |= (trait_at(b, m->coord, stone_other(m->color)).cap1 == trait_at(b, m->coord, stone_other(m->color)).cap) << PF_AESCAPE_1STONE;
294 if (PS_PF(AESCAPE, TRAPPED))
295 f->payload |= (!trait_at(b, m->coord, m->color).safe) << PF_AESCAPE_TRAPPED;
296 if (PS_PF(AESCAPE, CONNECTION))
297 f->payload |= (trait_at(b, m->coord, stone_other(m->color)).cap < neighbor_count_at(b, m->coord, m->color)) << PF_AESCAPE_CONNECTION;
298 (f++, p->n++);
299 return f;
301 /* We need to know details, so we still have to go through
302 * the neighbors. */
303 #endif
305 /* Find if a neighboring group of ours is in atari, AND that we provide
306 * a liberty to connect out. XXX: No connect-and-die check. */
307 /* This is very similar in spirit to board_safe_to_play(). */
308 group_t in_atari = -1;
309 coord_t onelib = -1;
310 int extra_libs = 0, connectable_groups = 0;
311 bool onestone = false, multistone = false;
313 foreach_neighbor(b, m->coord, {
314 if (board_at(b, c) != m->color) {
315 if (board_at(b, c) == S_NONE)
316 extra_libs++; // free point
317 else if (board_at(b, c) == stone_other(m->color) && board_group_info(b, group_at(b, c)).libs == 1) {
318 extra_libs += 2; // capturable enemy group
319 /* XXX: We just consider this move safe
320 * unconditionally. */
322 continue;
324 group_t g = group_at(b, c); assert(g);
325 if (board_group_info(b, g).libs > 1) {
326 connectable_groups++;
327 if (board_group_info(b, g).libs > 2) {
328 extra_libs += 2; // connected out
329 } else {
330 /* This is a bit tricky; we connect our 2-lib
331 * group to another 2-lib group, which counts
332 * as one liberty, but only if the other lib
333 * is not shared too. */
334 if (onelib == -1) {
335 onelib = board_group_other_lib(b, g, c);
336 extra_libs++;
337 } else {
338 if (c == onelib)
339 extra_libs--; // take that back
340 else
341 extra_libs++;
344 continue;
347 /* In atari! */
348 in_atari = g;
350 if (PS_PF(AESCAPE, LADDER))
351 f->payload |= is_ladder(b, m->coord, g, true) << PF_AESCAPE_LADDER;
352 /* TODO: is_ladder() is too conservative in some
353 * very obvious situations, look at complete.gtp. */
355 if (group_is_onestone(b, g))
356 onestone = true;
357 else
358 multistone = true;
361 if (in_atari >= 0) {
362 if (PS_PF(AESCAPE, 1STONE))
363 f->payload |= (onestone && !multistone) << PF_AESCAPE_1STONE;
364 if (PS_PF(AESCAPE, TRAPPED))
365 f->payload |= (extra_libs < 2) << PF_AESCAPE_TRAPPED;
366 if (PS_PF(AESCAPE, CONNECTION))
367 f->payload |= (connectable_groups > 0) << PF_AESCAPE_CONNECTION;
368 (f++, p->n++);
370 return f;
373 static struct feature *
374 pattern_match_atari(struct pattern_config *pc, pattern_spec ps,
375 struct pattern *p, struct feature *f,
376 struct board *b, struct move *m)
378 foreach_neighbor(b, m->coord, {
379 if (board_at(b, c) != stone_other(m->color))
380 continue;
381 group_t g = group_at(b, c);
382 if (!g || board_group_info(b, g).libs != 2)
383 continue;
385 /* Can atari! */
386 f->id = FEAT_ATARI; f->payload = 0;
388 if (PS_PF(ATARI, LADDER)) {
389 /* Opponent will escape by the other lib. */
390 coord_t lib = board_group_other_lib(b, g, m->coord);
391 /* TODO: is_ladder() is too conservative in some
392 * very obvious situations, look at complete.gtp. */
393 f->payload |= wouldbe_ladder(b, g, lib, m->coord, stone_other(m->color)) << PF_ATARI_LADDER;
396 if (PS_PF(ATARI, KO) && !is_pass(b->ko.coord))
397 f->payload |= 1 << PF_ATARI_KO;
399 (f++, p->n++);
401 return f;
404 #ifndef BOARD_SPATHASH
405 #undef BOARD_SPATHASH_MAXD
406 #define BOARD_SPATHASH_MAXD 1
407 #endif
409 /* Match spatial features that are too distant to be pre-matched
410 * incrementally. */
411 struct feature *
412 pattern_match_spatial_outer(struct pattern_config *pc, pattern_spec ps,
413 struct pattern *p, struct feature *f,
414 struct board *b, struct move *m, hash_t h)
416 /* We record all spatial patterns black-to-play; simply
417 * reverse all colors if we are white-to-play. */
418 static enum stone bt_black[4] = { S_NONE, S_BLACK, S_WHITE, S_OFFBOARD };
419 static enum stone bt_white[4] = { S_NONE, S_WHITE, S_BLACK, S_OFFBOARD };
420 enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black;
422 for (unsigned int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) {
423 /* Recompute missing outer circles:
424 * Go through all points in given distance. */
425 for (unsigned int j = ptind[d]; j < ptind[d + 1]; j++) {
426 ptcoords_at(x, y, m->coord, b, j);
427 h ^= pthashes[0][j][(*bt)[board_atxy(b, x, y)]];
429 if (d < pc->spat_min)
430 continue;
431 /* Record spatial feature, one per distance. */
432 unsigned int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
433 if (sid > 0) {
434 f->id = FEAT_SPATIAL;
435 f->payload = sid;
436 if (!pc->spat_largest)
437 (f++, p->n++);
438 } /* else not found, ignore */
440 return f;
443 struct feature *
444 pattern_match_spatial(struct pattern_config *pc, pattern_spec ps,
445 struct pattern *p, struct feature *f,
446 struct board *b, struct move *m)
448 /* XXX: This is partially duplicated from spatial_from_board(), but
449 * we build a hash instead of spatial record. */
451 assert(pc->spat_min > 0);
452 f->id = -1;
454 hash_t h = pthashes[0][0][S_NONE];
455 #ifdef BOARD_SPATHASH
456 bool w_to_play = m->color == S_WHITE;
457 for (int d = 2; d <= BOARD_SPATHASH_MAXD; d++) {
458 /* Reuse all incrementally matched data. */
459 h ^= b->spathash[m->coord][d - 1][w_to_play];
460 if (d < pc->spat_min)
461 continue;
462 /* Record spatial feature, one per distance. */
463 unsigned int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
464 if (sid > 0) {
465 f->id = FEAT_SPATIAL;
466 f->payload = sid;
467 if (!pc->spat_largest)
468 (f++, p->n++);
469 } /* else not found, ignore */
471 #else
472 assert(BOARD_SPATHASH_MAXD < 2);
473 #endif
474 if (unlikely(pc->spat_max > BOARD_SPATHASH_MAXD))
475 f = pattern_match_spatial_outer(pc, ps, p, f, b, m, h);
476 if (pc->spat_largest && f->id == FEAT_SPATIAL)
477 (f++, p->n++);
478 return f;
482 void
483 pattern_match(struct pattern_config *pc, pattern_spec ps,
484 struct pattern *p, struct board *b, struct move *m)
486 p->n = 0;
487 struct feature *f = &p->f[0];
489 /* TODO: We should match pretty much all of these features
490 * incrementally. */
492 if (PS_ANY(CAPTURE)) {
493 f = pattern_match_capture(pc, ps, p, f, b, m);
496 if (PS_ANY(AESCAPE)) {
497 f = pattern_match_aescape(pc, ps, p, f, b, m);
500 if (PS_ANY(SELFATARI)) {
501 bool simple = false;
502 if (PS_PF(SELFATARI, STUPID)) {
503 #ifdef BOARD_TRAITS
504 if (!b->precise_selfatari)
505 simple = !trait_at(b, m->coord, m->color).safe;
506 else
507 #endif
508 simple = !board_safe_to_play(b, m->coord, m->color);
510 bool thorough = false;
511 if (PS_PF(SELFATARI, SMART)) {
512 #ifdef BOARD_TRAITS
513 if (b->precise_selfatari)
514 thorough = !trait_at(b, m->coord, m->color).safe;
515 else
516 #endif
517 thorough = is_bad_selfatari(b, m->color, m->coord);
519 if (simple || thorough) {
520 f->id = FEAT_SELFATARI;
521 f->payload = simple << PF_SELFATARI_STUPID;
522 f->payload |= thorough << PF_SELFATARI_SMART;
523 (f++, p->n++);
527 if (PS_ANY(ATARI)) {
528 f = pattern_match_atari(pc, ps, p, f, b, m);
531 if (PS_ANY(BORDER)) {
532 unsigned int bdist = coord_edge_distance(m->coord, b);
533 if (bdist <= pc->bdist_max) {
534 f->id = FEAT_BORDER;
535 f->payload = bdist;
536 (f++, p->n++);
540 if (PS_ANY(CONTIGUITY) && !is_pass(b->last_move.coord)
541 && coord_is_8adjecent(m->coord, b->last_move.coord, b)) {
542 f->id = FEAT_CONTIGUITY;
543 f->payload = 1;
544 (f++, p->n++);
547 if (PS_ANY(SPATIAL) && pc->spat_max > 0 && pc->spat_dict) {
548 f = pattern_match_spatial(pc, ps, p, f, b, m);
552 char *
553 pattern2str(char *str, struct pattern *p)
555 str = stpcpy(str, "(");
556 for (int i = 0; i < p->n; i++) {
557 if (i > 0) str = stpcpy(str, " ");
558 str = feature2str(str, &p->f[i]);
560 str = stpcpy(str, ")");
561 return str;
564 char *
565 str2pattern(char *str, struct pattern *p)
567 p->n = 0;
568 while (isspace(*str)) str++;
569 if (*str++ != '(') {
570 fprintf(stderr, "invalid patternspec: %s\n", str);
571 exit(EXIT_FAILURE);
574 while (*str != ')') {
575 str = str2feature(str, &p->f[p->n++]);
578 str++;
579 return str;