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 #define FAST_NO_LADDER 1 /* 1: Don't match ladders in fast playouts */
46 pattern_spec PATTERN_SPEC_MATCHFAST
= {
48 [FEAT_CAPTURE
] = ~(1<<PF_CAPTURE_ATARIDEF
| 1<<PF_CAPTURE_RECAPTURE
| FAST_NO_LADDER
<<PF_CAPTURE_LADDER
),
49 [FEAT_AESCAPE
] = ~(FAST_NO_LADDER
<<PF_AESCAPE_LADDER
),
50 [FEAT_SELFATARI
] = ~(1<<PF_SELFATARI_SMART
),
55 [FEAT_CONTIGUITY
] = ~0,
61 static const struct feature_info
{
64 } features
[FEAT_MAX
] = {
65 [FEAT_PASS
] = { .name
= "pass", .payloads
= 2 },
66 [FEAT_CAPTURE
] = { .name
= "capture", .payloads
= 16 },
67 [FEAT_AESCAPE
] = { .name
= "atariescape", .payloads
= 2 },
68 [FEAT_SELFATARI
] = { .name
= "selfatari", .payloads
= 2 },
69 [FEAT_ATARI
] = { .name
= "atari", .payloads
= 4 },
70 [FEAT_BORDER
] = { .name
= "border", .payloads
= -1 },
71 [FEAT_LDIST
] = { .name
= "ldist", .payloads
= -1 },
72 [FEAT_LLDIST
] = { .name
= "lldist", .payloads
= -1 },
73 [FEAT_CONTIGUITY
] = { .name
= "cont", .payloads
= 2 },
74 [FEAT_SPATIAL
] = { .name
= "s", .payloads
= -1 },
75 [FEAT_PATTERN3
] = { .name
= "p", .payloads
= 2<<16 },
76 [FEAT_MCOWNER
] = { .name
= "mcowner", .payloads
= 16 },
80 feature2str(char *str
, struct feature
*f
)
82 return str
+ sprintf(str
+ strlen(str
), "%s:%d", features
[f
->id
].name
, f
->payload
);
86 str2feature(char *str
, struct feature
*f
)
88 while (isspace(*str
)) str
++;
90 int flen
= strcspn(str
, ":");
91 for (int i
= 0; i
< sizeof(features
)/sizeof(features
[0]); i
++)
92 if (strlen(features
[i
].name
) == flen
&& !strncmp(features
[i
].name
, str
, flen
)) {
96 fprintf(stderr
, "invalid featurespec: %s[%d]\n", str
, flen
);
101 f
->payload
= strtoull(str
, &str
, 10);
106 feature_name(enum feature_id f
)
108 return features
[f
].name
;
112 feature_payloads(struct pattern_config
*pc
, enum feature_id f
)
116 assert(features
[f
].payloads
< 0);
117 return pc
->spat_dict
->nspatials
;
120 assert(features
[f
].payloads
< 0);
121 return pc
->ldist_max
+ 1;
123 assert(features
[f
].payloads
< 0);
124 return pc
->bdist_max
+ 1;
126 assert(features
[f
].payloads
> 0);
127 return features
[f
].payloads
;
132 /* pattern_spec helpers */
133 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << 15))
134 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
136 static struct feature
*
137 pattern_match_capture(struct pattern_config
*pc
, pattern_spec ps
,
138 struct pattern
*p
, struct feature
*f
,
139 struct board
*b
, struct move
*m
)
141 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
143 if (!trait_at(b
, m
->coord
, m
->color
).cap
)
146 if (!(PS_PF(CAPTURE
, LADDER
)
147 || PS_PF(CAPTURE
, RECAPTURE
)
148 || PS_PF(CAPTURE
, ATARIDEF
)
149 || PS_PF(CAPTURE
, KO
))) {
153 /* We need to know details, so we still have to go through
157 /* Furthermore, we will now create one feature per capturable
159 /* XXX: I'm not sure if this is really good idea. --pasky */
160 foreach_neighbor(b
, m
->coord
, {
161 if (board_at(b
, c
) != stone_other(m
->color
))
163 group_t g
= group_at(b
, c
);
164 if (!g
|| board_group_info(b
, g
).libs
!= 1)
169 if (PS_PF(CAPTURE
, LADDER
))
170 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_CAPTURE_LADDER
;
171 /* TODO: is_ladder() is too conservative in some
172 * very obvious situations, look at complete.gtp. */
174 /* TODO: PF_CAPTURE_RECAPTURE */
176 if (PS_PF(CAPTURE
, ATARIDEF
))
177 foreach_in_group(b
, g
) {
178 foreach_neighbor(b
, c
, {
179 assert(board_at(b
, c
) != S_NONE
|| c
== m
->coord
);
180 if (board_at(b
, c
) != m
->color
)
182 group_t g
= group_at(b
, c
);
183 if (!g
|| board_group_info(b
, g
).libs
!= 1)
185 /* A neighboring group of ours is in atari. */
186 f
->payload
|= 1 << PF_CAPTURE_ATARIDEF
;
188 } foreach_in_group_end
;
190 if (PS_PF(CAPTURE
, KO
)
191 && group_is_onestone(b
, g
)
192 && neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))
193 + neighbor_count_at(b
, m
->coord
, S_OFFBOARD
) == 4)
194 f
->payload
|= 1 << PF_CAPTURE_KO
;
197 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
202 static struct feature
*
203 pattern_match_aescape(struct pattern_config
*pc
, pattern_spec ps
,
204 struct pattern
*p
, struct feature
*f
,
205 struct board
*b
, struct move
*m
)
208 if (!trait_at(b
, m
->coord
, stone_other(m
->color
)).cap
209 || !trait_at(b
, m
->coord
, m
->color
).safe
)
211 /* Opponent can capture something and this move is safe
213 if (!PS_PF(AESCAPE
, LADDER
)) {
214 f
->id
= FEAT_AESCAPE
; f
->payload
= 0;
218 /* We need to know details, so we still have to go through
222 /* Find if a neighboring group of ours is in atari, AND that we provide
223 * a liberty to connect out. XXX: No connect-and-die check. */
224 group_t in_atari
= -1;
225 bool has_extra_lib
= false;
228 foreach_neighbor(b
, m
->coord
, {
229 if (board_at(b
, c
) != m
->color
) {
230 if (board_at(b
, c
) == S_NONE
)
231 has_extra_lib
= true; // free point
232 else if (board_at(b
, c
) == stone_other(m
->color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
233 has_extra_lib
= true; // capturable enemy group
236 group_t g
= group_at(b
, c
); assert(g
);
237 if (board_group_info(b
, g
).libs
!= 1) {
238 has_extra_lib
= true;
245 if (PS_PF(AESCAPE
, LADDER
))
246 payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_AESCAPE_LADDER
;
247 /* TODO: is_ladder() is too conservative in some
248 * very obvious situations, look at complete.gtp. */
250 if (in_atari
>= 0 && has_extra_lib
) {
251 f
->id
= FEAT_AESCAPE
; f
->payload
= payload
;
257 static struct feature
*
258 pattern_match_atari(struct pattern_config
*pc
, pattern_spec ps
,
259 struct pattern
*p
, struct feature
*f
,
260 struct board
*b
, struct move
*m
)
262 foreach_neighbor(b
, m
->coord
, {
263 if (board_at(b
, c
) != stone_other(m
->color
))
265 group_t g
= group_at(b
, c
);
266 if (!g
|| board_group_info(b
, g
).libs
!= 2)
270 f
->id
= FEAT_ATARI
; f
->payload
= 0;
272 if (PS_PF(ATARI
, LADDER
)) {
273 /* Opponent will escape by the other lib. */
274 coord_t lib
= board_group_info(b
, g
).lib
[0];
275 if (lib
== m
->coord
) lib
= board_group_info(b
, g
).lib
[1];
276 /* TODO: is_ladder() is too conservative in some
277 * very obvious situations, look at complete.gtp. */
278 f
->payload
|= is_ladder(b
, lib
, g
, true, true) << PF_ATARI_LADDER
;
281 if (PS_PF(ATARI
, KO
) && !is_pass(b
->ko
.coord
))
282 f
->payload
|= 1 << PF_ATARI_KO
;
289 #ifndef BOARD_SPATHASH
290 #undef BOARD_SPATHASH_MAXD
291 #define BOARD_SPATHASH_MAXD 1
294 /* Match spatial features that are too distant to be pre-matched
297 pattern_match_spatial_outer(struct pattern_config
*pc
, pattern_spec ps
,
298 struct pattern
*p
, struct feature
*f
,
299 struct board
*b
, struct move
*m
, hash_t h
)
301 /* We record all spatial patterns black-to-play; simply
302 * reverse all colors if we are white-to-play. */
303 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
304 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
305 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
307 for (int d
= BOARD_SPATHASH_MAXD
+ 1; d
<= pc
->spat_max
; d
++) {
308 /* Recompute missing outer circles:
309 * Go through all points in given distance. */
310 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
311 ptcoords_at(x
, y
, m
->coord
, b
, j
);
312 h
^= pthashes
[0][j
][(*bt
)[board_atxy(b
, x
, y
)]];
314 if (d
< pc
->spat_min
)
316 /* Record spatial feature, one per distance. */
317 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
319 f
->id
= FEAT_SPATIAL
;
322 } /* else not found, ignore */
328 pattern_match_spatial(struct pattern_config
*pc
, pattern_spec ps
,
329 struct pattern
*p
, struct feature
*f
,
330 struct board
*b
, struct move
*m
)
332 /* XXX: This is partially duplicated from spatial_from_board(), but
333 * we build a hash instead of spatial record. */
335 assert(pc
->spat_min
> 0);
337 hash_t h
= pthashes
[0][0][S_NONE
];
338 #ifdef BOARD_SPATHASH
339 bool w_to_play
= m
->color
== S_WHITE
;
340 for (int d
= 2; d
<= BOARD_SPATHASH_MAXD
; d
++) {
341 /* Reuse all incrementally matched data. */
342 h
^= b
->spathash
[m
->coord
][d
- 1][w_to_play
];
343 if (d
< pc
->spat_min
)
345 /* Record spatial feature, one per distance. */
346 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
348 f
->id
= FEAT_SPATIAL
;
351 } /* else not found, ignore */
354 assert(BOARD_SPATHASH_MAXD
< 2);
356 if (unlikely(pc
->spat_max
> BOARD_SPATHASH_MAXD
))
357 f
= pattern_match_spatial_outer(pc
, ps
, p
, f
, b
, m
, h
);
363 is_simple_selfatari(struct board
*b
, enum stone color
, coord_t coord
)
366 return !trait_at(b
, coord
, color
).safe
;
369 /* Very rough check, no connect-and-die checks or other trickery. */
370 int libs
= immediate_liberty_count(b
, coord
);
371 if (libs
>= 2) return false; // open space
374 foreach_neighbor(b
, coord
, {
375 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1) {
376 return false; // can capture
378 } else if (board_at(b
, c
) == color
) {
379 // friendly group, does it have liberties?
380 group_t g
= group_at(b
, c
);
381 if (board_group_info(b
, g
).libs
== 1 || seen
== g
)
383 libs
+= board_group_info(b
, g
).libs
- 1;
384 if (libs
>= 2) return false;
385 // don't consider the same group twice
393 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
394 struct pattern
*p
, struct board
*b
, struct move
*m
)
397 struct feature
*f
= &p
->f
[0];
399 /* TODO: We should match pretty much all of these features
402 if (is_pass(m
->coord
)) {
404 f
->id
= FEAT_PASS
; f
->payload
= 0;
405 if (PS_PF(PASS
, LASTPASS
))
406 f
->payload
|= (b
->moves
> 0 && is_pass(b
->last_move
.coord
))
413 if (PS_ANY(CAPTURE
)) {
414 f
= pattern_match_capture(pc
, ps
, p
, f
, b
, m
);
417 if (PS_ANY(AESCAPE
)) {
418 f
= pattern_match_aescape(pc
, ps
, p
, f
, b
, m
);
421 if (PS_ANY(SELFATARI
)) {
422 bool simple
= is_simple_selfatari(b
, m
->color
, m
->coord
);
423 bool thorough
= false;
424 if (PS_PF(SELFATARI
, SMART
)) {
425 thorough
= is_bad_selfatari(b
, m
->color
, m
->coord
);
427 if (simple
|| thorough
) {
428 f
->id
= FEAT_SELFATARI
;
429 f
->payload
= thorough
<< PF_SELFATARI_SMART
;
435 f
= pattern_match_atari(pc
, ps
, p
, f
, b
, m
);
438 if (PS_ANY(BORDER
)) {
439 int bdist
= coord_edge_distance(m
->coord
, b
);
440 if (bdist
<= pc
->bdist_max
) {
447 if (PS_ANY(CONTIGUITY
) && !is_pass(b
->last_move
.coord
)
448 && coord_is_8adjecent(m
->coord
, b
->last_move
.coord
, b
)) {
449 f
->id
= FEAT_CONTIGUITY
;
454 if (PS_ANY(LDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move
.coord
)) {
455 int ldist
= coord_gridcular_distance(m
->coord
, b
->last_move
.coord
, b
);
456 if (pc
->ldist_min
<= ldist
&& ldist
<= pc
->ldist_max
) {
463 if (PS_ANY(LLDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move2
.coord
)) {
464 int lldist
= coord_gridcular_distance(m
->coord
, b
->last_move2
.coord
, b
);
465 if (pc
->ldist_min
<= lldist
&& lldist
<= pc
->ldist_max
) {
472 if (PS_ANY(SPATIAL
) && pc
->spat_max
> 0 && pc
->spat_dict
) {
473 f
= pattern_match_spatial(pc
, ps
, p
, f
, b
, m
);
476 if (PS_ANY(PATTERN3
) && !is_pass(m
->coord
)) {
478 int pat
= b
->pat3
[m
->coord
];
480 int pat
= pattern3_hash(b
, m
->coord
);
482 if (m
->color
== S_WHITE
) {
483 /* We work with the pattern3s as black-to-play. */
484 pat
= pattern3_reverse(pat
);
486 f
->id
= FEAT_PATTERN3
;
491 /* FEAT_MCOWNER: TODO */
496 pattern2str(char *str
, struct pattern
*p
)
498 str
= stpcpy(str
, "(");
499 for (int i
= 0; i
< p
->n
; i
++) {
500 if (i
> 0) str
= stpcpy(str
, " ");
501 str
= feature2str(str
, &p
->f
[i
]);
503 str
= stpcpy(str
, ")");
509 /*** Features gamma set */
512 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
514 FILE *f
= fopen(filename
, "r");
517 while (fgets(buf
, 256, f
)) {
520 bufp
= str2feature(bufp
, &f
);
521 while (isspace(*bufp
)) bufp
++;
522 float gamma
= strtof(bufp
, &bufp
);
523 /* Record feature's gamma. */
524 feature_gamma(fg
, &f
, &gamma
);
525 /* In case of 3x3 patterns, record gamma also
526 * for all rotations and transpositions. */
527 if (f
.id
== FEAT_PATTERN3
) {
529 pattern3_transpose(f
.payload
, &transp
);
530 for (int i
= 1; i
< 8; i
++) {
531 f
.payload
= transp
[i
];
532 feature_gamma(fg
, &f
, &gamma
);
534 f
.payload
= transp
[0];
540 const char *features_gamma_filename
= "patterns.gamma";
542 struct features_gamma
*
543 features_gamma_init(struct pattern_config
*pc
, const char *file
)
545 struct features_gamma
*fg
= calloc(1, sizeof(*fg
));
547 for (int i
= 0; i
< FEAT_MAX
; i
++) {
548 int n
= feature_payloads(pc
, i
);
549 fg
->gamma
[i
] = malloc(n
* sizeof(float));
550 for (int j
= 0; j
< n
; j
++) {
551 fg
->gamma
[i
][j
] = 1.0f
;
554 features_gamma_load(fg
, file
? file
: features_gamma_filename
);
559 features_gamma_done(struct features_gamma
*fg
)
561 for (int i
= 0; i
< FEAT_MAX
; i
++)