GTP: Implement pachi-result command
[pachi/pachi-r6144.git] / pattern.c
blobbe3d105ce0cbcf80d14f3d946fd2bbf68200ac14
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 #define PF_MATCH 15
32 pattern_spec PATTERN_SPEC_MATCHALL = {
33 [FEAT_PASS] = ~0,
34 [FEAT_CAPTURE] = ~0,
35 [FEAT_AESCAPE] = ~0,
36 [FEAT_SELFATARI] = ~0,
37 [FEAT_ATARI] = ~0,
38 [FEAT_BORDER] = ~0,
39 [FEAT_LDIST] = ~0,
40 [FEAT_LLDIST] = ~0,
41 [FEAT_CONTIGUITY] = 0,
42 [FEAT_SPATIAL] = ~0,
43 [FEAT_PATTERN3] = 0,
44 [FEAT_MCOWNER] = ~0,
47 /* !!! Note that in order for ELO playout policy to work correctly, this
48 * pattern specification MUST exactly match the features matched by the
49 * BOARD_GAMMA code! You cannot just tinker with this spec freely. */
50 pattern_spec PATTERN_SPEC_MATCHFAST = {
51 [FEAT_PASS] = 0,
52 [FEAT_CAPTURE] = (1<<PF_MATCH | 1<<PF_CAPTURE_1STONE | 1<<PF_CAPTURE_TRAPPED | 1<<PF_CAPTURE_CONNECTION),
53 [FEAT_AESCAPE] = (1<<PF_MATCH | 1<<PF_AESCAPE_1STONE | 1<<PF_AESCAPE_TRAPPED | 1<<PF_AESCAPE_CONNECTION),
54 [FEAT_SELFATARI] = (1<<PF_MATCH | 1<<PF_SELFATARI_STUPID),
55 [FEAT_ATARI] = 0,
56 [FEAT_BORDER] = 0,
57 [FEAT_LDIST] = 0,
58 [FEAT_LLDIST] = 0,
59 [FEAT_CONTIGUITY] = ~0,
60 [FEAT_SPATIAL] = 0,
61 [FEAT_PATTERN3] = ~0,
62 [FEAT_MCOWNER] = 0,
65 static const struct feature_info {
66 char *name;
67 int payloads;
68 } features[FEAT_MAX] = {
69 [FEAT_PASS] = { .name = "pass", .payloads = 2 },
70 [FEAT_CAPTURE] = { .name = "capture", .payloads = 128 },
71 [FEAT_AESCAPE] = { .name = "atariescape", .payloads = 16 },
72 [FEAT_SELFATARI] = { .name = "selfatari", .payloads = 4 },
73 [FEAT_ATARI] = { .name = "atari", .payloads = 4 },
74 [FEAT_BORDER] = { .name = "border", .payloads = -1 },
75 [FEAT_LDIST] = { .name = "ldist", .payloads = -1 },
76 [FEAT_LLDIST] = { .name = "lldist", .payloads = -1 },
77 [FEAT_CONTIGUITY] = { .name = "cont", .payloads = 2 },
78 [FEAT_SPATIAL] = { .name = "s", .payloads = -1 },
79 [FEAT_PATTERN3] = { .name = "p", .payloads = 2<<16 },
80 [FEAT_MCOWNER] = { .name = "mcowner", .payloads = 16 },
83 char *
84 feature2str(char *str, struct feature *f)
86 return str + sprintf(str + strlen(str), "%s:%d", features[f->id].name, f->payload);
89 char *
90 str2feature(char *str, struct feature *f)
92 while (isspace(*str)) str++;
94 int unsigned flen = strcspn(str, ":");
95 for (unsigned int i = 0; i < sizeof(features)/sizeof(features[0]); i++)
96 if (strlen(features[i].name) == flen && !strncmp(features[i].name, str, flen)) {
97 f->id = i;
98 goto found;
100 fprintf(stderr, "invalid featurespec: %s[%d]\n", str, flen);
101 exit(EXIT_FAILURE);
103 found:
104 str += flen + 1;
105 f->payload = strtoull(str, &str, 10);
106 return str;
109 char *
110 feature_name(enum feature_id f)
112 return features[f].name;
116 feature_payloads(struct pattern_config *pc, enum feature_id f)
118 switch (f) {
119 case FEAT_SPATIAL:
120 assert(features[f].payloads < 0);
121 return pc->spat_dict->nspatials;
122 case FEAT_LDIST:
123 case FEAT_LLDIST:
124 assert(features[f].payloads < 0);
125 return pc->ldist_max + 1;
126 case FEAT_BORDER:
127 assert(features[f].payloads < 0);
128 return pc->bdist_max + 1;
129 default:
130 assert(features[f].payloads > 0);
131 return features[f].payloads;
136 /* pattern_spec helpers */
137 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << PF_MATCH))
138 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
140 //#undef BOARD_TRAITS // for cross-testing of pattern matchers - enable also elo_check_probdist()
142 static struct feature *
143 pattern_match_capture(struct pattern_config *pc, pattern_spec ps,
144 struct pattern *p, struct feature *f,
145 struct board *b, struct move *m)
147 f->id = FEAT_CAPTURE; f->payload = 0;
148 #ifdef BOARD_TRAITS
149 if (!trait_at(b, m->coord, m->color).cap)
150 return f;
151 /* Capturable! */
152 if ((ps[FEAT_CAPTURE] & ~(1<<PF_CAPTURE_1STONE | 1<<PF_CAPTURE_TRAPPED | 1<<PF_CAPTURE_CONNECTION)) == 1<<PF_MATCH) {
153 if (PS_PF(CAPTURE, 1STONE))
154 f->payload |= (trait_at(b, m->coord, m->color).cap1 == trait_at(b, m->coord, m->color).cap) << PF_CAPTURE_1STONE;
155 if (PS_PF(CAPTURE, TRAPPED))
156 f->payload |= (!trait_at(b, m->coord, stone_other(m->color)).safe) << PF_CAPTURE_TRAPPED;
157 if (PS_PF(CAPTURE, CONNECTION))
158 f->payload |= (trait_at(b, m->coord, m->color).cap < neighbor_count_at(b, m->coord, stone_other(m->color))) << PF_CAPTURE_CONNECTION;
159 (f++, p->n++);
160 return f;
162 /* We need to know details, so we still have to go through
163 * the neighbors. */
164 #endif
166 /* We look at neighboring groups we could capture, and also if the
167 * opponent could save them. */
168 /* This is very similar in spirit to board_safe_to_play(), and almost
169 * a color inverse of pattern_match_aescape(). */
171 /* Whether an escape move would be safe for the opponent. */
172 int captures = 0;
173 coord_t onelib = -1;
174 int extra_libs = 0, connectable_groups = 0;
175 bool onestone = false, multistone = false;
177 foreach_neighbor(b, m->coord, {
178 if (board_at(b, c) != stone_other(m->color)) {
179 if (board_at(b, c) == S_NONE)
180 extra_libs++; // free point
181 else if (board_at(b, c) == m->color && board_group_info(b, group_at(b, c)).libs == 1)
182 extra_libs += 2; // capturable enemy group
183 continue;
186 group_t g = group_at(b, c); assert(g);
187 if (board_group_info(b, g).libs > 1) {
188 connectable_groups++;
189 if (board_group_info(b, g).libs > 2) {
190 extra_libs += 2; // connected out
191 } else {
192 /* This is a bit tricky; we connect our 2-lib
193 * group to another 2-lib group, which counts
194 * as one liberty, but only if the other lib
195 * is not shared too. */
196 if (onelib == -1) {
197 onelib = board_group_other_lib(b, g, c);
198 extra_libs++;
199 } else {
200 if (c == onelib)
201 extra_libs--; // take that back
202 else
203 extra_libs++;
206 continue;
209 /* Capture! */
210 captures++;
212 if (PS_PF(CAPTURE, LADDER))
213 f->payload |= is_ladder(b, m->coord, g, true, true) << PF_CAPTURE_LADDER;
214 /* TODO: is_ladder() is too conservative in some
215 * very obvious situations, look at complete.gtp. */
217 /* TODO: PF_CAPTURE_RECAPTURE */
219 if (PS_PF(CAPTURE, ATARIDEF))
220 foreach_in_group(b, g) {
221 foreach_neighbor(b, c, {
222 assert(board_at(b, c) != S_NONE || c == m->coord);
223 if (board_at(b, c) != m->color)
224 continue;
225 group_t g = group_at(b, c);
226 if (!g || board_group_info(b, g).libs != 1)
227 continue;
228 /* A neighboring group of ours is in atari. */
229 f->payload |= 1 << PF_CAPTURE_ATARIDEF;
231 } foreach_in_group_end;
233 if (PS_PF(CAPTURE, KO)
234 && group_is_onestone(b, g)
235 && neighbor_count_at(b, m->coord, stone_other(m->color))
236 + neighbor_count_at(b, m->coord, S_OFFBOARD) == 4)
237 f->payload |= 1 << PF_CAPTURE_KO;
239 if (group_is_onestone(b, g))
240 onestone = true;
241 else
242 multistone = true;
245 if (captures > 0) {
246 if (PS_PF(CAPTURE, 1STONE))
247 f->payload |= (onestone && !multistone) << PF_CAPTURE_1STONE;
248 if (PS_PF(CAPTURE, TRAPPED))
249 f->payload |= (extra_libs < 2) << PF_CAPTURE_TRAPPED;
250 if (PS_PF(CAPTURE, CONNECTION))
251 f->payload |= (connectable_groups > 0) << PF_CAPTURE_CONNECTION;
252 (f++, p->n++);
254 return f;
257 static struct feature *
258 pattern_match_aescape(struct pattern_config *pc, pattern_spec ps,
259 struct pattern *p, struct feature *f,
260 struct board *b, struct move *m)
262 f->id = FEAT_AESCAPE; f->payload = 0;
263 #ifdef BOARD_TRAITS
264 if (!trait_at(b, m->coord, stone_other(m->color)).cap)
265 return f;
266 /* Opponent can capture something! */
267 if ((ps[FEAT_AESCAPE] & ~(1<<PF_AESCAPE_1STONE | 1<<PF_AESCAPE_TRAPPED | 1<<PF_AESCAPE_CONNECTION)) == 1<<PF_MATCH) {
268 if (PS_PF(AESCAPE, 1STONE))
269 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;
270 if (PS_PF(AESCAPE, TRAPPED))
271 f->payload |= (!trait_at(b, m->coord, m->color).safe) << PF_AESCAPE_TRAPPED;
272 if (PS_PF(AESCAPE, CONNECTION))
273 f->payload |= (trait_at(b, m->coord, stone_other(m->color)).cap < neighbor_count_at(b, m->coord, m->color)) << PF_AESCAPE_CONNECTION;
274 (f++, p->n++);
275 return f;
277 /* We need to know details, so we still have to go through
278 * the neighbors. */
279 #endif
281 /* Find if a neighboring group of ours is in atari, AND that we provide
282 * a liberty to connect out. XXX: No connect-and-die check. */
283 /* This is very similar in spirit to board_safe_to_play(). */
284 group_t in_atari = -1;
285 coord_t onelib = -1;
286 int extra_libs = 0, connectable_groups = 0;
287 bool onestone = false, multistone = false;
289 foreach_neighbor(b, m->coord, {
290 if (board_at(b, c) != m->color) {
291 if (board_at(b, c) == S_NONE)
292 extra_libs++; // free point
293 else if (board_at(b, c) == stone_other(m->color) && board_group_info(b, group_at(b, c)).libs == 1) {
294 extra_libs += 2; // capturable enemy group
295 /* XXX: We just consider this move safe
296 * unconditionally. */
298 continue;
300 group_t g = group_at(b, c); assert(g);
301 if (board_group_info(b, g).libs > 1) {
302 connectable_groups++;
303 if (board_group_info(b, g).libs > 2) {
304 extra_libs += 2; // connected out
305 } else {
306 /* This is a bit tricky; we connect our 2-lib
307 * group to another 2-lib group, which counts
308 * as one liberty, but only if the other lib
309 * is not shared too. */
310 if (onelib == -1) {
311 onelib = board_group_other_lib(b, g, c);
312 extra_libs++;
313 } else {
314 if (c == onelib)
315 extra_libs--; // take that back
316 else
317 extra_libs++;
320 continue;
323 /* In atari! */
324 in_atari = g;
326 if (PS_PF(AESCAPE, LADDER))
327 f->payload |= is_ladder(b, m->coord, g, true, true) << PF_AESCAPE_LADDER;
328 /* TODO: is_ladder() is too conservative in some
329 * very obvious situations, look at complete.gtp. */
331 if (group_is_onestone(b, g))
332 onestone = true;
333 else
334 multistone = true;
337 if (in_atari >= 0) {
338 if (PS_PF(AESCAPE, 1STONE))
339 f->payload |= (onestone && !multistone) << PF_AESCAPE_1STONE;
340 if (PS_PF(AESCAPE, TRAPPED))
341 f->payload |= (extra_libs < 2) << PF_AESCAPE_TRAPPED;
342 if (PS_PF(AESCAPE, CONNECTION))
343 f->payload |= (connectable_groups > 0) << PF_AESCAPE_CONNECTION;
344 (f++, p->n++);
346 return f;
349 static struct feature *
350 pattern_match_atari(struct pattern_config *pc, pattern_spec ps,
351 struct pattern *p, struct feature *f,
352 struct board *b, struct move *m)
354 foreach_neighbor(b, m->coord, {
355 if (board_at(b, c) != stone_other(m->color))
356 continue;
357 group_t g = group_at(b, c);
358 if (!g || board_group_info(b, g).libs != 2)
359 continue;
361 /* Can atari! */
362 f->id = FEAT_ATARI; f->payload = 0;
364 if (PS_PF(ATARI, LADDER)) {
365 /* Opponent will escape by the other lib. */
366 coord_t lib = board_group_other_lib(b, g, m->coord);
367 /* TODO: is_ladder() is too conservative in some
368 * very obvious situations, look at complete.gtp. */
369 f->payload |= is_ladder(b, lib, g, true, true) << PF_ATARI_LADDER;
372 if (PS_PF(ATARI, KO) && !is_pass(b->ko.coord))
373 f->payload |= 1 << PF_ATARI_KO;
375 (f++, p->n++);
377 return f;
380 #ifndef BOARD_SPATHASH
381 #undef BOARD_SPATHASH_MAXD
382 #define BOARD_SPATHASH_MAXD 1
383 #endif
385 /* Match spatial features that are too distant to be pre-matched
386 * incrementally. */
387 struct feature *
388 pattern_match_spatial_outer(struct pattern_config *pc, pattern_spec ps,
389 struct pattern *p, struct feature *f,
390 struct board *b, struct move *m, hash_t h)
392 /* We record all spatial patterns black-to-play; simply
393 * reverse all colors if we are white-to-play. */
394 static enum stone bt_black[4] = { S_NONE, S_BLACK, S_WHITE, S_OFFBOARD };
395 static enum stone bt_white[4] = { S_NONE, S_WHITE, S_BLACK, S_OFFBOARD };
396 enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black;
398 for (int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) {
399 /* Recompute missing outer circles:
400 * Go through all points in given distance. */
401 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
402 ptcoords_at(x, y, m->coord, b, j);
403 h ^= pthashes[0][j][(*bt)[board_atxy(b, x, y)]];
405 if (d < pc->spat_min)
406 continue;
407 /* Record spatial feature, one per distance. */
408 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
409 if (sid > 0) {
410 f->id = FEAT_SPATIAL;
411 f->payload = sid;
412 (f++, p->n++);
413 } /* else not found, ignore */
415 return f;
418 struct feature *
419 pattern_match_spatial(struct pattern_config *pc, pattern_spec ps,
420 struct pattern *p, struct feature *f,
421 struct board *b, struct move *m)
423 /* XXX: This is partially duplicated from spatial_from_board(), but
424 * we build a hash instead of spatial record. */
426 assert(pc->spat_min > 0);
428 hash_t h = pthashes[0][0][S_NONE];
429 #ifdef BOARD_SPATHASH
430 bool w_to_play = m->color == S_WHITE;
431 for (int d = 2; d <= BOARD_SPATHASH_MAXD; d++) {
432 /* Reuse all incrementally matched data. */
433 h ^= b->spathash[m->coord][d - 1][w_to_play];
434 if (d < pc->spat_min)
435 continue;
436 /* Record spatial feature, one per distance. */
437 int sid = spatial_dict_get(pc->spat_dict, d, h & spatial_hash_mask);
438 if (sid > 0) {
439 f->id = FEAT_SPATIAL;
440 f->payload = sid;
441 (f++, p->n++);
442 } /* else not found, ignore */
444 #else
445 assert(BOARD_SPATHASH_MAXD < 2);
446 #endif
447 if (unlikely(pc->spat_max > BOARD_SPATHASH_MAXD))
448 f = pattern_match_spatial_outer(pc, ps, p, f, b, m, h);
449 return f;
453 void
454 pattern_match(struct pattern_config *pc, pattern_spec ps,
455 struct pattern *p, struct board *b, struct move *m)
457 p->n = 0;
458 struct feature *f = &p->f[0];
460 /* TODO: We should match pretty much all of these features
461 * incrementally. */
463 if (is_pass(m->coord)) {
464 if (PS_ANY(PASS)) {
465 f->id = FEAT_PASS; f->payload = 0;
466 if (PS_PF(PASS, LASTPASS))
467 f->payload |= (b->moves > 0 && is_pass(b->last_move.coord))
468 << PF_PASS_LASTPASS;
469 p->n++;
471 return;
474 if (PS_ANY(CAPTURE)) {
475 f = pattern_match_capture(pc, ps, p, f, b, m);
478 if (PS_ANY(AESCAPE)) {
479 f = pattern_match_aescape(pc, ps, p, f, b, m);
482 if (PS_ANY(SELFATARI)) {
483 bool simple = false;
484 if (PS_PF(SELFATARI, STUPID)) {
485 #ifdef BOARD_TRAITS
486 if (!b->precise_selfatari)
487 simple = !trait_at(b, m->coord, m->color).safe;
488 else
489 #endif
490 simple = !board_safe_to_play(b, m->coord, m->color);
492 bool thorough = false;
493 if (PS_PF(SELFATARI, SMART)) {
494 #ifdef BOARD_TRAITS
495 if (b->precise_selfatari)
496 thorough = !trait_at(b, m->coord, m->color).safe;
497 else
498 #endif
499 thorough = is_bad_selfatari(b, m->color, m->coord);
501 if (simple || thorough) {
502 f->id = FEAT_SELFATARI;
503 f->payload = simple << PF_SELFATARI_STUPID;
504 f->payload |= thorough << PF_SELFATARI_SMART;
505 (f++, p->n++);
509 if (PS_ANY(ATARI)) {
510 f = pattern_match_atari(pc, ps, p, f, b, m);
513 if (PS_ANY(BORDER)) {
514 int bdist = coord_edge_distance(m->coord, b);
515 if (bdist <= pc->bdist_max) {
516 f->id = FEAT_BORDER;
517 f->payload = bdist;
518 (f++, p->n++);
522 if (PS_ANY(CONTIGUITY) && !is_pass(b->last_move.coord)
523 && coord_is_8adjecent(m->coord, b->last_move.coord, b)) {
524 f->id = FEAT_CONTIGUITY;
525 f->payload = 1;
526 (f++, p->n++);
529 if (PS_ANY(LDIST) && pc->ldist_max > 0 && !is_pass(b->last_move.coord)) {
530 int ldist = coord_gridcular_distance(m->coord, b->last_move.coord, b);
531 if (pc->ldist_min <= ldist && ldist <= pc->ldist_max) {
532 f->id = FEAT_LDIST;
533 f->payload = ldist;
534 (f++, p->n++);
538 if (PS_ANY(LLDIST) && pc->ldist_max > 0 && !is_pass(b->last_move2.coord)) {
539 int lldist = coord_gridcular_distance(m->coord, b->last_move2.coord, b);
540 if (pc->ldist_min <= lldist && lldist <= pc->ldist_max) {
541 f->id = FEAT_LLDIST;
542 f->payload = lldist;
543 (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);
551 if (PS_ANY(PATTERN3) && !is_pass(m->coord)) {
552 #ifdef BOARD_PAT3
553 hash3_t pat = b->pat3[m->coord];
554 #else
555 hash3_t pat = pattern3_hash(b, m->coord);
556 #endif
557 if (m->color == S_WHITE) {
558 /* We work with the pattern3s as black-to-play. */
559 pat = pattern3_reverse(pat);
561 f->id = FEAT_PATTERN3;
562 f->payload = pat;
563 (f++, p->n++);
566 /* FEAT_MCOWNER: TODO */
567 assert(!pc->mcsims);
570 char *
571 pattern2str(char *str, struct pattern *p)
573 str = stpcpy(str, "(");
574 for (int i = 0; i < p->n; i++) {
575 if (i > 0) str = stpcpy(str, " ");
576 str = feature2str(str, &p->f[i]);
578 str = stpcpy(str, ")");
579 return str;
584 /*** Features gamma set */
586 static void
587 features_gamma_load(struct features_gamma *fg, const char *filename)
589 FILE *f = fopen(filename, "r");
590 if (!f) return;
591 char buf[256];
592 while (fgets(buf, 256, f)) {
593 char *bufp = buf;
594 struct feature f;
595 bufp = str2feature(bufp, &f);
596 while (isspace(*bufp)) bufp++;
597 double gamma = strtod(bufp, &bufp);
598 /* Record feature's gamma. */
599 feature_gamma(fg, &f, &gamma);
600 /* In case of 3x3 patterns, record gamma also
601 * for all rotations and transpositions. */
602 if (f.id == FEAT_PATTERN3) {
603 hash3_t transp[8];
604 pattern3_transpose(f.payload, &transp);
605 for (int i = 1; i < 8; i++) {
606 f.payload = transp[i];
607 feature_gamma(fg, &f, &gamma);
609 f.payload = transp[0];
612 fclose(f);
615 const char *features_gamma_filename = "patterns.gamma";
617 struct features_gamma *
618 features_gamma_init(struct pattern_config *pc, const char *file)
620 struct features_gamma *fg = calloc2(1, sizeof(*fg));
621 fg->pc = pc;
622 for (int i = 0; i < FEAT_MAX; i++) {
623 int n = feature_payloads(pc, i);
624 fg->gamma[i] = malloc2(n * sizeof(fg->gamma[0][0]));
625 for (int j = 0; j < n; j++) {
626 fg->gamma[i][j] = 1.0f;
629 features_gamma_load(fg, file ? file : features_gamma_filename);
630 return fg;
633 void
634 features_gamma_done(struct features_gamma *fg)
636 for (int i = 0; i < FEAT_MAX; i++)
637 free(fg->gamma[i]);
638 free(fg);