PATTERN_SPEC_MATCHFAST: Whitelist PFs instead of blacklisting
[pachi.git] / pattern.c
blob9def68410e5b6ea3b7ca7a95ec4bb565205af200
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 "pattern3.h"
13 #include "tactics.h"
16 struct pattern_config DEFAULT_PATTERN_CONFIG = {
17 .spat_min = 3, .spat_max = MAX_PATTERN_DIST,
18 .bdist_max = 4,
19 .ldist_min = 0, .ldist_max = 256,
20 .mcsims = 0, /* Unsupported. */
23 struct pattern_config FAST_PATTERN_CONFIG = {
24 .spat_min = 3, .spat_max = 3,
25 .bdist_max = -1,
26 .ldist_min = 0, .ldist_max = 256,
27 .mcsims = 0,
30 pattern_spec PATTERN_SPEC_MATCHALL = {
31 [FEAT_PASS] = ~0,
32 [FEAT_CAPTURE] = ~0,
33 [FEAT_AESCAPE] = ~0,
34 [FEAT_SELFATARI] = ~0,
35 [FEAT_ATARI] = ~0,
36 [FEAT_BORDER] = ~0,
37 [FEAT_LDIST] = ~0,
38 [FEAT_LLDIST] = ~0,
39 [FEAT_CONTIGUITY] = 0,
40 [FEAT_SPATIAL] = ~0,
41 [FEAT_PATTERN3] = 0,
42 [FEAT_MCOWNER] = ~0,
45 /* !!! Note that in order for ELO playout policy to work correctly, this
46 * pattern specification MUST exactly match the features matched by the
47 * BOARD_GAMMA code! You cannot just tinker with this spec freely. */
48 pattern_spec PATTERN_SPEC_MATCHFAST = {
49 [FEAT_PASS] = 0,
50 [FEAT_CAPTURE] = (1<<PF_CAPTURE_1STONE | 1<<PF_CAPTURE_TRAPPED),
51 [FEAT_AESCAPE] = (1<<PF_AESCAPE_1STONE | 1<<PF_AESCAPE_TRAPPED),
52 [FEAT_SELFATARI] = (1<<PF_SELFATARI_STUPID),
53 [FEAT_ATARI] = 0,
54 [FEAT_BORDER] = 0,
55 [FEAT_LDIST] = 0,
56 [FEAT_LLDIST] = 0,
57 [FEAT_CONTIGUITY] = ~0,
58 [FEAT_SPATIAL] = 0,
59 [FEAT_PATTERN3] = ~0,
60 [FEAT_MCOWNER] = 0,
63 static const struct feature_info {
64 char *name;
65 int payloads;
66 } features[FEAT_MAX] = {
67 [FEAT_PASS] = { .name = "pass", .payloads = 2 },
68 [FEAT_CAPTURE] = { .name = "capture", .payloads = 64 },
69 [FEAT_AESCAPE] = { .name = "atariescape", .payloads = 8 },
70 [FEAT_SELFATARI] = { .name = "selfatari", .payloads = 4 },
71 [FEAT_ATARI] = { .name = "atari", .payloads = 4 },
72 [FEAT_BORDER] = { .name = "border", .payloads = -1 },
73 [FEAT_LDIST] = { .name = "ldist", .payloads = -1 },
74 [FEAT_LLDIST] = { .name = "lldist", .payloads = -1 },
75 [FEAT_CONTIGUITY] = { .name = "cont", .payloads = 2 },
76 [FEAT_SPATIAL] = { .name = "s", .payloads = -1 },
77 [FEAT_PATTERN3] = { .name = "p", .payloads = 2<<16 },
78 [FEAT_MCOWNER] = { .name = "mcowner", .payloads = 16 },
81 char *
82 feature2str(char *str, struct feature *f)
84 return str + sprintf(str + strlen(str), "%s:%d", features[f->id].name, f->payload);
87 char *
88 str2feature(char *str, struct feature *f)
90 while (isspace(*str)) str++;
92 int unsigned flen = strcspn(str, ":");
93 for (unsigned int i = 0; i < sizeof(features)/sizeof(features[0]); i++)
94 if (strlen(features[i].name) == flen && !strncmp(features[i].name, str, flen)) {
95 f->id = i;
96 goto found;
98 fprintf(stderr, "invalid featurespec: %s[%d]\n", str, flen);
99 exit(EXIT_FAILURE);
101 found:
102 str += flen + 1;
103 f->payload = strtoull(str, &str, 10);
104 return str;
107 char *
108 feature_name(enum feature_id f)
110 return features[f].name;
114 feature_payloads(struct pattern_config *pc, enum feature_id f)
116 switch (f) {
117 case FEAT_SPATIAL:
118 assert(features[f].payloads < 0);
119 return pc->spat_dict->nspatials;
120 case FEAT_LDIST:
121 case FEAT_LLDIST:
122 assert(features[f].payloads < 0);
123 return pc->ldist_max + 1;
124 case FEAT_BORDER:
125 assert(features[f].payloads < 0);
126 return pc->bdist_max + 1;
127 default:
128 assert(features[f].payloads > 0);
129 return features[f].payloads;
134 /* pattern_spec helpers */
135 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << 15))
136 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
138 static struct feature *
139 pattern_match_capture(struct pattern_config *pc, pattern_spec ps,
140 struct pattern *p, struct feature *f,
141 struct board *b, struct move *m)
143 f->id = FEAT_CAPTURE; f->payload = 0;
144 #ifdef BOARD_TRAITS
145 if (!trait_at(b, m->coord, m->color).cap)
146 return f;
147 /* Capturable! */
148 if (!(PS_PF(CAPTURE, LADDER)
149 || PS_PF(CAPTURE, RECAPTURE)
150 || PS_PF(CAPTURE, ATARIDEF)
151 || PS_PF(CAPTURE, KO))) {
152 if (PS_PF(CAPTURE, 1STONE))
153 f->payload |= (trait_at(b, m->coord, m->color).cap1 == trait_at(b, m->coord, m->color).cap) << PF_CAPTURE_1STONE;
154 if (PS_PF(CAPTURE, TRAPPED))
155 f->payload |= (!trait_at(b, m->coord, stone_other(m->color)).safe) << PF_CAPTURE_TRAPPED;
156 (f++, p->n++);
157 return f;
159 /* We need to know details, so we still have to go through
160 * the neighbors. */
161 #endif
163 /* We look at neighboring groups we could capture, and also if the
164 * opponent could save them. */
165 /* This is very similar in spirit to board_safe_to_play(), and almost
166 * a color inverse of pattern_match_aescape(). */
168 /* Whether an escape move would be safe for the opponent. */
169 int captures = 0;
170 coord_t onelib = -1;
171 int extra_libs = 0;
172 bool onestone = false, multistone = false;
174 foreach_neighbor(b, m->coord, {
175 if (board_at(b, c) != stone_other(m->color)) {
176 if (board_at(b, c) == S_NONE)
177 extra_libs++; // free point
178 else if (board_at(b, c) == m->color && board_group_info(b, group_at(b, c)).libs == 1)
179 extra_libs += 2; // capturable enemy group
180 continue;
183 group_t g = group_at(b, c); assert(g);
184 if (board_group_info(b, g).libs > 1) {
185 if (board_group_info(b, g).libs > 2) {
186 extra_libs += 2; // connected out
187 } else {
188 /* This is a bit tricky; we connect our 2-lib
189 * group to another 2-lib group, which counts
190 * as one liberty, but only if the other lib
191 * is not shared too. */
192 if (onelib == -1) {
193 onelib = board_group_other_lib(b, g, c);
194 extra_libs++;
195 } else {
196 if (c == onelib)
197 extra_libs--; // take that back
198 else
199 extra_libs++;
202 continue;
205 /* Capture! */
206 captures++;
208 if (PS_PF(CAPTURE, LADDER))
209 f->payload |= is_ladder(b, m->coord, g, true, true) << PF_CAPTURE_LADDER;
210 /* TODO: is_ladder() is too conservative in some
211 * very obvious situations, look at complete.gtp. */
213 /* TODO: PF_CAPTURE_RECAPTURE */
215 if (PS_PF(CAPTURE, ATARIDEF))
216 foreach_in_group(b, g) {
217 foreach_neighbor(b, c, {
218 assert(board_at(b, c) != S_NONE || c == m->coord);
219 if (board_at(b, c) != m->color)
220 continue;
221 group_t g = group_at(b, c);
222 if (!g || board_group_info(b, g).libs != 1)
223 continue;
224 /* A neighboring group of ours is in atari. */
225 f->payload |= 1 << PF_CAPTURE_ATARIDEF;
227 } foreach_in_group_end;
229 if (PS_PF(CAPTURE, KO)
230 && group_is_onestone(b, g)
231 && neighbor_count_at(b, m->coord, stone_other(m->color))
232 + neighbor_count_at(b, m->coord, S_OFFBOARD) == 4)
233 f->payload |= 1 << PF_CAPTURE_KO;
235 if (group_is_onestone(b, g))
236 onestone = true;
237 else
238 multistone = true;
241 if (captures > 0) {
242 if (PS_PF(CAPTURE, 1STONE))
243 f->payload |= (onestone && !multistone) << PF_CAPTURE_1STONE;
244 if (PS_PF(CAPTURE, TRAPPED))
245 f->payload |= (extra_libs < 2) << PF_CAPTURE_TRAPPED;
246 (f++, p->n++);
248 return f;
251 static struct feature *
252 pattern_match_aescape(struct pattern_config *pc, pattern_spec ps,
253 struct pattern *p, struct feature *f,
254 struct board *b, struct move *m)
256 f->id = FEAT_AESCAPE; f->payload = 0;
257 #ifdef BOARD_TRAITS
258 if (!trait_at(b, m->coord, stone_other(m->color)).cap)
259 return f;
260 /* Opponent can capture something! */
261 if (!PS_PF(AESCAPE, LADDER)) {
262 if (PS_PF(AESCAPE, 1STONE))
263 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;
264 if (PS_PF(CAPTURE, TRAPPED))
265 f->payload |= (!trait_at(b, m->coord, m->color).safe) << PF_AESCAPE_TRAPPED;
266 (f++, p->n++);
267 return f;
269 /* We need to know details, so we still have to go through
270 * the neighbors. */
271 #endif
273 /* Find if a neighboring group of ours is in atari, AND that we provide
274 * a liberty to connect out. XXX: No connect-and-die check. */
275 /* This is very similar in spirit to board_safe_to_play(). */
276 group_t in_atari = -1;
277 coord_t onelib = -1;
278 int extra_libs = 0;
279 bool onestone = false, multistone = false;
281 foreach_neighbor(b, m->coord, {
282 if (board_at(b, c) != m->color) {
283 if (board_at(b, c) == S_NONE)
284 extra_libs++; // free point
285 else if (board_at(b, c) == stone_other(m->color) && board_group_info(b, group_at(b, c)).libs == 1) {
286 extra_libs += 2; // capturable enemy group
287 /* XXX: We just consider this move safe
288 * unconditionally. */
290 continue;
292 group_t g = group_at(b, c); assert(g);
293 if (board_group_info(b, g).libs > 1) {
294 if (board_group_info(b, g).libs > 2) {
295 extra_libs += 2; // connected out
296 } else {
297 /* This is a bit tricky; we connect our 2-lib
298 * group to another 2-lib group, which counts
299 * as one liberty, but only if the other lib
300 * is not shared too. */
301 if (onelib == -1) {
302 onelib = board_group_other_lib(b, g, c);
303 extra_libs++;
304 } else {
305 if (c == onelib)
306 extra_libs--; // take that back
307 else
308 extra_libs++;
311 continue;
314 /* In atari! */
315 in_atari = g;
317 if (PS_PF(AESCAPE, LADDER))
318 f->payload |= is_ladder(b, m->coord, g, true, true) << PF_AESCAPE_LADDER;
319 /* TODO: is_ladder() is too conservative in some
320 * very obvious situations, look at complete.gtp. */
322 if (group_is_onestone(b, g))
323 onestone = true;
324 else
325 multistone = true;
328 if (in_atari >= 0) {
329 if (PS_PF(AESCAPE, 1STONE))
330 f->payload |= (onestone && !multistone) << PF_AESCAPE_1STONE;
331 if (PS_PF(AESCAPE, TRAPPED))
332 f->payload |= (extra_libs < 2) << PF_AESCAPE_TRAPPED;
333 (f++, p->n++);
335 return f;
338 static struct feature *
339 pattern_match_atari(struct pattern_config *pc, pattern_spec ps,
340 struct pattern *p, struct feature *f,
341 struct board *b, struct move *m)
343 foreach_neighbor(b, m->coord, {
344 if (board_at(b, c) != stone_other(m->color))
345 continue;
346 group_t g = group_at(b, c);
347 if (!g || board_group_info(b, g).libs != 2)
348 continue;
350 /* Can atari! */
351 f->id = FEAT_ATARI; f->payload = 0;
353 if (PS_PF(ATARI, LADDER)) {
354 /* Opponent will escape by the other lib. */
355 coord_t lib = board_group_other_lib(b, g, m->coord);
356 /* TODO: is_ladder() is too conservative in some
357 * very obvious situations, look at complete.gtp. */
358 f->payload |= is_ladder(b, lib, g, true, true) << PF_ATARI_LADDER;
361 if (PS_PF(ATARI, KO) && !is_pass(b->ko.coord))
362 f->payload |= 1 << PF_ATARI_KO;
364 (f++, p->n++);
366 return f;
369 #ifndef BOARD_SPATHASH
370 #undef BOARD_SPATHASH_MAXD
371 #define BOARD_SPATHASH_MAXD 1
372 #endif
374 /* Match spatial features that are too distant to be pre-matched
375 * incrementally. */
376 struct feature *
377 pattern_match_spatial_outer(struct pattern_config *pc, pattern_spec ps,
378 struct pattern *p, struct feature *f,
379 struct board *b, struct move *m, hash_t h)
381 /* We record all spatial patterns black-to-play; simply
382 * reverse all colors if we are white-to-play. */
383 static enum stone bt_black[4] = { S_NONE, S_BLACK, S_WHITE, S_OFFBOARD };
384 static enum stone bt_white[4] = { S_NONE, S_WHITE, S_BLACK, S_OFFBOARD };
385 enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black;
387 for (int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) {
388 /* Recompute missing outer circles:
389 * Go through all points in given distance. */
390 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
391 ptcoords_at(x, y, m->coord, b, j);
392 h ^= pthashes[0][j][(*bt)[board_atxy(b, x, y)]];
394 if (d < pc->spat_min)
395 continue;
396 /* Record spatial feature, one per distance. */
397 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
398 if (sid > 0) {
399 f->id = FEAT_SPATIAL;
400 f->payload = sid;
401 (f++, p->n++);
402 } /* else not found, ignore */
404 return f;
407 struct feature *
408 pattern_match_spatial(struct pattern_config *pc, pattern_spec ps,
409 struct pattern *p, struct feature *f,
410 struct board *b, struct move *m)
412 /* XXX: This is partially duplicated from spatial_from_board(), but
413 * we build a hash instead of spatial record. */
415 assert(pc->spat_min > 0);
417 hash_t h = pthashes[0][0][S_NONE];
418 #ifdef BOARD_SPATHASH
419 bool w_to_play = m->color == S_WHITE;
420 for (int d = 2; d <= BOARD_SPATHASH_MAXD; d++) {
421 /* Reuse all incrementally matched data. */
422 h ^= b->spathash[m->coord][d - 1][w_to_play];
423 if (d < pc->spat_min)
424 continue;
425 /* Record spatial feature, one per distance. */
426 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
427 if (sid > 0) {
428 f->id = FEAT_SPATIAL;
429 f->payload = sid;
430 (f++, p->n++);
431 } /* else not found, ignore */
433 #else
434 assert(BOARD_SPATHASH_MAXD < 2);
435 #endif
436 if (unlikely(pc->spat_max > BOARD_SPATHASH_MAXD))
437 f = pattern_match_spatial_outer(pc, ps, p, f, b, m, h);
438 return f;
442 void
443 pattern_match(struct pattern_config *pc, pattern_spec ps,
444 struct pattern *p, struct board *b, struct move *m)
446 p->n = 0;
447 struct feature *f = &p->f[0];
449 /* TODO: We should match pretty much all of these features
450 * incrementally. */
452 if (is_pass(m->coord)) {
453 if (PS_ANY(PASS)) {
454 f->id = FEAT_PASS; f->payload = 0;
455 if (PS_PF(PASS, LASTPASS))
456 f->payload |= (b->moves > 0 && is_pass(b->last_move.coord))
457 << PF_PASS_LASTPASS;
458 p->n++;
460 return;
463 if (PS_ANY(CAPTURE)) {
464 f = pattern_match_capture(pc, ps, p, f, b, m);
467 if (PS_ANY(AESCAPE)) {
468 f = pattern_match_aescape(pc, ps, p, f, b, m);
471 if (PS_ANY(SELFATARI)) {
472 bool simple = false;
473 if (PS_PF(SELFATARI, STUPID)) {
474 #ifdef BOARD_TRAITS
475 if (!b->precise_selfatari)
476 simple = !trait_at(b, m->coord, m->color).safe;
477 else
478 #endif
479 simple = !board_safe_to_play(b, m->coord, m->color);
481 bool thorough = false;
482 if (PS_PF(SELFATARI, SMART)) {
483 #ifdef BOARD_TRAITS
484 if (b->precise_selfatari)
485 thorough = !trait_at(b, m->coord, m->color).safe;
486 else
487 #endif
488 thorough = is_bad_selfatari(b, m->color, m->coord);
490 if (simple || thorough) {
491 f->id = FEAT_SELFATARI;
492 f->payload = simple << PF_SELFATARI_STUPID;
493 f->payload |= thorough << PF_SELFATARI_SMART;
494 (f++, p->n++);
498 if (PS_ANY(ATARI)) {
499 f = pattern_match_atari(pc, ps, p, f, b, m);
502 if (PS_ANY(BORDER)) {
503 int bdist = coord_edge_distance(m->coord, b);
504 if (bdist <= pc->bdist_max) {
505 f->id = FEAT_BORDER;
506 f->payload = bdist;
507 (f++, p->n++);
511 if (PS_ANY(CONTIGUITY) && !is_pass(b->last_move.coord)
512 && coord_is_8adjecent(m->coord, b->last_move.coord, b)) {
513 f->id = FEAT_CONTIGUITY;
514 f->payload = 1;
515 (f++, p->n++);
518 if (PS_ANY(LDIST) && pc->ldist_max > 0 && !is_pass(b->last_move.coord)) {
519 int ldist = coord_gridcular_distance(m->coord, b->last_move.coord, b);
520 if (pc->ldist_min <= ldist && ldist <= pc->ldist_max) {
521 f->id = FEAT_LDIST;
522 f->payload = ldist;
523 (f++, p->n++);
527 if (PS_ANY(LLDIST) && pc->ldist_max > 0 && !is_pass(b->last_move2.coord)) {
528 int lldist = coord_gridcular_distance(m->coord, b->last_move2.coord, b);
529 if (pc->ldist_min <= lldist && lldist <= pc->ldist_max) {
530 f->id = FEAT_LLDIST;
531 f->payload = lldist;
532 (f++, p->n++);
536 if (PS_ANY(SPATIAL) && pc->spat_max > 0 && pc->spat_dict) {
537 f = pattern_match_spatial(pc, ps, p, f, b, m);
540 if (PS_ANY(PATTERN3) && !is_pass(m->coord)) {
541 #ifdef BOARD_PAT3
542 hash3_t pat = b->pat3[m->coord];
543 #else
544 hash3_t pat = pattern3_hash(b, m->coord);
545 #endif
546 if (m->color == S_WHITE) {
547 /* We work with the pattern3s as black-to-play. */
548 pat = pattern3_reverse(pat);
550 f->id = FEAT_PATTERN3;
551 f->payload = pat;
552 (f++, p->n++);
555 /* FEAT_MCOWNER: TODO */
556 assert(!pc->mcsims);
559 char *
560 pattern2str(char *str, struct pattern *p)
562 str = stpcpy(str, "(");
563 for (int i = 0; i < p->n; i++) {
564 if (i > 0) str = stpcpy(str, " ");
565 str = feature2str(str, &p->f[i]);
567 str = stpcpy(str, ")");
568 return str;
573 /*** Features gamma set */
575 static void
576 features_gamma_load(struct features_gamma *fg, const char *filename)
578 FILE *f = fopen(filename, "r");
579 if (!f) return;
580 char buf[256];
581 while (fgets(buf, 256, f)) {
582 char *bufp = buf;
583 struct feature f;
584 bufp = str2feature(bufp, &f);
585 while (isspace(*bufp)) bufp++;
586 double gamma = strtod(bufp, &bufp);
587 /* Record feature's gamma. */
588 feature_gamma(fg, &f, &gamma);
589 /* In case of 3x3 patterns, record gamma also
590 * for all rotations and transpositions. */
591 if (f.id == FEAT_PATTERN3) {
592 hash3_t transp[8];
593 pattern3_transpose(f.payload, &transp);
594 for (int i = 1; i < 8; i++) {
595 f.payload = transp[i];
596 feature_gamma(fg, &f, &gamma);
598 f.payload = transp[0];
601 fclose(f);
604 const char *features_gamma_filename = "patterns.gamma";
606 struct features_gamma *
607 features_gamma_init(struct pattern_config *pc, const char *file)
609 struct features_gamma *fg = calloc2(1, sizeof(*fg));
610 fg->pc = pc;
611 for (int i = 0; i < FEAT_MAX; i++) {
612 int n = feature_payloads(pc, i);
613 fg->gamma[i] = malloc2(n * sizeof(fg->gamma[0][0]));
614 for (int j = 0; j < n; j++) {
615 fg->gamma[i][j] = 1.0f;
618 features_gamma_load(fg, file ? file : features_gamma_filename);
619 return fg;
622 void
623 features_gamma_done(struct features_gamma *fg)
625 for (int i = 0; i < FEAT_MAX; i++)
626 free(fg->gamma[i]);
627 free(fg);