11 #include "patternsp.h"
16 struct pattern_config DEFAULT_PATTERN_CONFIG
= {
17 .spat_min
= 3, .spat_max
= MAX_PATTERN_DIST
,
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,
26 .ldist_min
= 0, .ldist_max
= 256,
30 pattern_spec PATTERN_SPEC_MATCHALL
= {
34 [FEAT_SELFATARI
] = ~0,
39 [FEAT_CONTIGUITY
] = 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
= {
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
),
57 [FEAT_CONTIGUITY
] = ~0,
63 static const struct feature_info
{
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 },
82 feature2str(char *str
, struct feature
*f
)
84 return str
+ sprintf(str
+ strlen(str
), "%s:%d", features
[f
->id
].name
, f
->payload
);
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
)) {
98 fprintf(stderr
, "invalid featurespec: %s[%d]\n", str
, flen
);
103 f
->payload
= strtoull(str
, &str
, 10);
108 feature_name(enum feature_id f
)
110 return features
[f
].name
;
114 feature_payloads(struct pattern_config
*pc
, enum feature_id f
)
118 assert(features
[f
].payloads
< 0);
119 return pc
->spat_dict
->nspatials
;
122 assert(features
[f
].payloads
< 0);
123 return pc
->ldist_max
+ 1;
125 assert(features
[f
].payloads
< 0);
126 return pc
->bdist_max
+ 1;
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;
145 if (!trait_at(b
, m
->coord
, m
->color
).cap
)
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
;
159 /* We need to know details, so we still have to go through
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. */
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
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
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. */
193 onelib
= board_group_other_lib(b
, g
, c
);
197 extra_libs
--; // take that back
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
)
221 group_t g
= group_at(b
, c
);
222 if (!g
|| board_group_info(b
, g
).libs
!= 1)
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
))
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
;
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;
258 if (!trait_at(b
, m
->coord
, stone_other(m
->color
)).cap
)
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
;
269 /* We need to know details, so we still have to go through
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;
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. */
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
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. */
302 onelib
= board_group_other_lib(b
, g
, c
);
306 extra_libs
--; // take that back
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
))
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
;
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
))
346 group_t g
= group_at(b
, c
);
347 if (!g
|| board_group_info(b
, g
).libs
!= 2)
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
;
369 #ifndef BOARD_SPATHASH
370 #undef BOARD_SPATHASH_MAXD
371 #define BOARD_SPATHASH_MAXD 1
374 /* Match spatial features that are too distant to be pre-matched
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
)
396 /* Record spatial feature, one per distance. */
397 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
399 f
->id
= FEAT_SPATIAL
;
402 } /* else not found, ignore */
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
)
425 /* Record spatial feature, one per distance. */
426 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
428 f
->id
= FEAT_SPATIAL
;
431 } /* else not found, ignore */
434 assert(BOARD_SPATHASH_MAXD
< 2);
436 if (unlikely(pc
->spat_max
> BOARD_SPATHASH_MAXD
))
437 f
= pattern_match_spatial_outer(pc
, ps
, p
, f
, b
, m
, h
);
443 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
444 struct pattern
*p
, struct board
*b
, struct move
*m
)
447 struct feature
*f
= &p
->f
[0];
449 /* TODO: We should match pretty much all of these features
452 if (is_pass(m
->coord
)) {
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
))
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
)) {
473 if (PS_PF(SELFATARI
, STUPID
)) {
475 if (!b
->precise_selfatari
)
476 simple
= !trait_at(b
, m
->coord
, m
->color
).safe
;
479 simple
= !board_safe_to_play(b
, m
->coord
, m
->color
);
481 bool thorough
= false;
482 if (PS_PF(SELFATARI
, SMART
)) {
484 if (b
->precise_selfatari
)
485 thorough
= !trait_at(b
, m
->coord
, m
->color
).safe
;
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
;
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
) {
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
;
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
) {
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
) {
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
)) {
542 hash3_t pat
= b
->pat3
[m
->coord
];
544 hash3_t pat
= pattern3_hash(b
, m
->coord
);
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
;
555 /* FEAT_MCOWNER: TODO */
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
, ")");
573 /*** Features gamma set */
576 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
578 FILE *f
= fopen(filename
, "r");
581 while (fgets(buf
, 256, 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
) {
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];
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
));
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
);
623 features_gamma_done(struct features_gamma
*fg
)
625 for (int i
= 0; i
< FEAT_MAX
; i
++)