11 #include "patternsp.h"
15 struct pattern_config DEFAULT_PATTERN_CONFIG
= {
16 .spat_min
= 3, .spat_max
= MAX_PATTERN_DIST
,
18 .ldist_min
= 0, .ldist_max
= 256,
19 .mcsims
= 0, /* Unsupported. */
22 struct pattern_config FAST_PATTERN_CONFIG
= {
23 .spat_min
= 3, .spat_max
= 3,
25 .ldist_min
= 0, .ldist_max
= 256,
29 pattern_spec PATTERN_SPEC_MATCHALL
= {
33 [FEAT_SELFATARI
] = ~0,
38 [FEAT_CONTIGUITY
] = 0,
43 #define FAST_NO_LADDER 1 /* 1: Don't match ladders in fast playouts */
44 pattern_spec PATTERN_SPEC_MATCHFAST
= {
46 [FEAT_CAPTURE
] = ~(1<<PF_CAPTURE_ATARIDEF
| 1<<PF_CAPTURE_RECAPTURE
| FAST_NO_LADDER
<<PF_CAPTURE_LADDER
),
47 [FEAT_AESCAPE
] = ~(FAST_NO_LADDER
<<PF_AESCAPE_LADDER
),
48 [FEAT_SELFATARI
] = ~(1<<PF_SELFATARI_SMART
),
49 [FEAT_ATARI
] = ~(FAST_NO_LADDER
<<PF_ATARI_LADDER
),
53 [FEAT_CONTIGUITY
] = ~0,
58 static const struct feature_info
{
61 } features
[FEAT_MAX
] = {
62 [FEAT_PASS
] = { .name
= "pass", .payloads
= 2 },
63 [FEAT_CAPTURE
] = { .name
= "capture", .payloads
= 16 },
64 [FEAT_AESCAPE
] = { .name
= "atariescape", .payloads
= 2 },
65 [FEAT_SELFATARI
] = { .name
= "selfatari", .payloads
= 2 },
66 [FEAT_ATARI
] = { .name
= "atari", .payloads
= 4 },
67 [FEAT_BORDER
] = { .name
= "border", .payloads
= -1 },
68 [FEAT_LDIST
] = { .name
= "ldist", .payloads
= -1 },
69 [FEAT_LLDIST
] = { .name
= "lldist", .payloads
= -1 },
70 [FEAT_CONTIGUITY
] = { .name
= "cont", .payloads
= 2 },
71 [FEAT_SPATIAL
] = { .name
= "s", .payloads
= -1 },
72 [FEAT_MCOWNER
] = { .name
= "mcowner", .payloads
= 16 },
76 feature2str(char *str
, struct feature
*f
)
78 return str
+ sprintf(str
+ strlen(str
), "%s:%d", features
[f
->id
].name
, f
->payload
);
82 str2feature(char *str
, struct feature
*f
)
84 while (isspace(*str
)) str
++;
86 int flen
= strcspn(str
, ":");
87 for (int i
= 0; i
< sizeof(features
)/sizeof(features
[0]); i
++)
88 if (strlen(features
[i
].name
) == flen
&& !strncmp(features
[i
].name
, str
, flen
)) {
92 fprintf(stderr
, "invalid featurespec: %s[%d]\n", str
, flen
);
97 f
->payload
= strtoull(str
, &str
, 10);
102 feature_name(enum feature_id f
)
104 return features
[f
].name
;
108 feature_payloads(struct pattern_config
*pc
, enum feature_id f
)
112 assert(features
[f
].payloads
< 0);
113 return pc
->spat_dict
->nspatials
;
116 assert(features
[f
].payloads
< 0);
117 return pc
->ldist_max
+ 1;
119 assert(features
[f
].payloads
< 0);
120 return pc
->bdist_max
+ 1;
122 assert(features
[f
].payloads
> 0);
123 return features
[f
].payloads
;
128 /* pattern_spec helpers */
129 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << 15))
130 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
132 static struct feature
*
133 pattern_match_capture(struct pattern_config
*pc
, pattern_spec ps
,
134 struct pattern
*p
, struct feature
*f
,
135 struct board
*b
, struct move
*m
)
137 foreach_neighbor(b
, m
->coord
, {
138 if (board_at(b
, c
) != stone_other(m
->color
))
140 group_t g
= group_at(b
, c
);
141 if (!g
|| board_group_info(b
, g
).libs
!= 1)
145 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
147 if (PS_PF(CAPTURE
, LADDER
))
148 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_CAPTURE_LADDER
;
149 /* TODO: is_ladder() is too conservative in some
150 * very obvious situations, look at complete.gtp. */
152 /* TODO: PF_CAPTURE_RECAPTURE */
154 if (PS_PF(CAPTURE
, ATARIDEF
))
155 foreach_in_group(b
, g
) {
156 foreach_neighbor(b
, c
, {
157 assert(board_at(b
, c
) != S_NONE
|| c
== m
->coord
);
158 if (board_at(b
, c
) != m
->color
)
160 group_t g
= group_at(b
, c
);
161 if (!g
|| board_group_info(b
, g
).libs
!= 1)
163 /* A neighboring group of ours is in atari. */
164 f
->payload
|= 1 << PF_CAPTURE_ATARIDEF
;
166 } foreach_in_group_end
;
168 if (PS_PF(CAPTURE
, KO
)
169 && group_is_onestone(b
, g
)
170 && neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))
171 + neighbor_count_at(b
, m
->coord
, S_OFFBOARD
) == 4)
172 f
->payload
|= 1 << PF_CAPTURE_KO
;
179 static struct feature
*
180 pattern_match_aescape(struct pattern_config
*pc
, pattern_spec ps
,
181 struct pattern
*p
, struct feature
*f
,
182 struct board
*b
, struct move
*m
)
184 /* Find if a neighboring group of ours is in atari, AND that we provide
185 * a liberty to connect out. XXX: No connect-and-die check. */
186 group_t in_atari
= -1;
187 bool has_extra_lib
= false;
190 foreach_neighbor(b
, m
->coord
, {
191 if (board_at(b
, c
) != m
->color
) {
192 if (board_at(b
, c
) == S_NONE
)
193 has_extra_lib
= true; // free point
194 else if (board_at(b
, c
) == stone_other(m
->color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
195 has_extra_lib
= true; // capturable enemy group
198 group_t g
= group_at(b
, c
); assert(g
);
199 if (board_group_info(b
, g
).libs
!= 1) {
200 has_extra_lib
= true;
207 if (PS_PF(AESCAPE
, LADDER
))
208 payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_AESCAPE_LADDER
;
209 /* TODO: is_ladder() is too conservative in some
210 * very obvious situations, look at complete.gtp. */
212 if (in_atari
>= 0 && has_extra_lib
) {
213 f
->id
= FEAT_AESCAPE
; f
->payload
= payload
;
219 static struct feature
*
220 pattern_match_atari(struct pattern_config
*pc
, pattern_spec ps
,
221 struct pattern
*p
, struct feature
*f
,
222 struct board
*b
, struct move
*m
)
224 foreach_neighbor(b
, m
->coord
, {
225 if (board_at(b
, c
) != stone_other(m
->color
))
227 group_t g
= group_at(b
, c
);
228 if (!g
|| board_group_info(b
, g
).libs
!= 2)
232 f
->id
= FEAT_ATARI
; f
->payload
= 0;
234 if (PS_PF(ATARI
, LADDER
)) {
235 /* Opponent will escape by the other lib. */
236 coord_t lib
= board_group_info(b
, g
).lib
[0];
237 if (lib
== m
->coord
) lib
= board_group_info(b
, g
).lib
[1];
238 /* TODO: is_ladder() is too conservative in some
239 * very obvious situations, look at complete.gtp. */
240 f
->payload
|= is_ladder(b
, lib
, g
, true, true) << PF_ATARI_LADDER
;
243 if (PS_PF(ATARI
, KO
) && !is_pass(b
->ko
.coord
))
244 f
->payload
|= 1 << PF_ATARI_KO
;
251 #ifndef BOARD_SPATHASH
252 #undef BOARD_SPATHASH_MAXD
253 #define BOARD_SPATHASH_MAXD 1
256 /* Match spatial features that are too distant to be pre-matched
259 pattern_match_spatial_outer(struct pattern_config
*pc
, pattern_spec ps
,
260 struct pattern
*p
, struct feature
*f
,
261 struct board
*b
, struct move
*m
, hash_t h
)
263 /* We record all spatial patterns black-to-play; simply
264 * reverse all colors if we are white-to-play. */
265 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
266 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
267 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
269 for (int d
= BOARD_SPATHASH_MAXD
+ 1; d
<= pc
->spat_max
; d
++) {
270 /* Recompute missing outer circles:
271 * Go through all points in given distance. */
272 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
273 ptcoords_at(x
, y
, m
->coord
, b
, j
);
274 h
^= pthashes
[0][j
][(*bt
)[board_atxy(b
, x
, y
)]];
276 if (d
< pc
->spat_min
)
278 /* Record spatial feature, one per distance. */
279 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
281 f
->id
= FEAT_SPATIAL
;
284 } /* else not found, ignore */
290 pattern_match_spatial(struct pattern_config
*pc
, pattern_spec ps
,
291 struct pattern
*p
, struct feature
*f
,
292 struct board
*b
, struct move
*m
)
294 /* XXX: This is partially duplicated from spatial_from_board(), but
295 * we build a hash instead of spatial record. */
297 assert(pc
->spat_min
> 0);
299 bool w_to_play
= m
->color
== S_WHITE
;
300 hash_t h
= pthashes
[0][0][S_NONE
];
301 for (int d
= 2; d
<= BOARD_SPATHASH_MAXD
; d
++) {
302 /* Reuse all incrementally matched data. */
303 h
^= b
->spathash
[m
->coord
][d
- 1][w_to_play
];
304 if (d
< pc
->spat_min
)
306 /* Record spatial feature, one per distance. */
307 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
309 f
->id
= FEAT_SPATIAL
;
312 } /* else not found, ignore */
314 if (unlikely(pc
->spat_max
> BOARD_SPATHASH_MAXD
))
315 f
= pattern_match_spatial_outer(pc
, ps
, p
, f
, b
, m
, h
);
321 is_simple_selfatari(struct board
*b
, enum stone color
, coord_t coord
)
323 /* Very rough check, no connect-and-die checks or other trickery. */
324 int libs
= immediate_liberty_count(b
, coord
);
325 if (libs
>= 2) return false; // open space
328 foreach_neighbor(b
, coord
, {
329 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1) {
330 return false; // can capture
332 } else if (board_at(b
, c
) == color
) {
333 // friendly group, does it have liberties?
334 group_t g
= group_at(b
, c
);
335 if (board_group_info(b
, g
).libs
== 1 || seen
== g
)
337 libs
+= board_group_info(b
, g
).libs
- 1;
338 if (libs
>= 2) return false;
339 // don't consider the same group twice
347 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
348 struct pattern
*p
, struct board
*b
, struct move
*m
)
351 struct feature
*f
= &p
->f
[0];
353 /* TODO: We should match pretty much all of these features
356 if (is_pass(m
->coord
)) {
358 f
->id
= FEAT_PASS
; f
->payload
= 0;
359 if (PS_PF(PASS
, LASTPASS
))
360 f
->payload
|= (b
->moves
> 0 && is_pass(b
->last_move
.coord
))
367 if (PS_ANY(CAPTURE
)) {
368 f
= pattern_match_capture(pc
, ps
, p
, f
, b
, m
);
371 if (PS_ANY(AESCAPE
)) {
372 f
= pattern_match_aescape(pc
, ps
, p
, f
, b
, m
);
375 if (PS_ANY(SELFATARI
)) {
376 bool simple
= is_simple_selfatari(b
, m
->color
, m
->coord
);
377 bool thorough
= false;
378 if (PS_PF(SELFATARI
, SMART
)) {
379 thorough
= is_bad_selfatari(b
, m
->color
, m
->coord
);
381 if (simple
|| thorough
) {
382 f
->id
= FEAT_SELFATARI
;
383 f
->payload
= thorough
<< PF_SELFATARI_SMART
;
389 f
= pattern_match_atari(pc
, ps
, p
, f
, b
, m
);
392 if (PS_ANY(BORDER
)) {
393 int bdist
= coord_edge_distance(m
->coord
, b
);
394 if (bdist
<= pc
->bdist_max
) {
401 if (PS_ANY(LDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move
.coord
)) {
402 int ldist
= coord_gridcular_distance(m
->coord
, b
->last_move
.coord
, b
);
403 if (pc
->ldist_min
<= ldist
&& ldist
<= pc
->ldist_max
) {
410 if (PS_ANY(CONTIGUITY
) && !is_pass(b
->last_move
.coord
)
411 && coord_is_8adjecent(m
->coord
, b
->last_move
.coord
, b
)) {
412 f
->id
= FEAT_CONTIGUITY
;
417 if (PS_ANY(LLDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move2
.coord
)) {
418 int lldist
= coord_gridcular_distance(m
->coord
, b
->last_move2
.coord
, b
);
419 if (pc
->ldist_min
<= lldist
&& lldist
<= pc
->ldist_max
) {
426 if (PS_ANY(SPATIAL
) && pc
->spat_max
> 0 && pc
->spat_dict
) {
427 f
= pattern_match_spatial(pc
, ps
, p
, f
, b
, m
);
430 /* FEAT_MCOWNER: TODO */
435 pattern2str(char *str
, struct pattern
*p
)
437 str
= stpcpy(str
, "(");
438 for (int i
= 0; i
< p
->n
; i
++) {
439 if (i
> 0) str
= stpcpy(str
, " ");
440 str
= feature2str(str
, &p
->f
[i
]);
442 str
= stpcpy(str
, ")");
448 /*** Features gamma set */
451 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
453 FILE *f
= fopen(filename
, "r");
456 while (fgets(buf
, 256, f
)) {
459 bufp
= str2feature(bufp
, &f
);
460 while (isspace(*bufp
)) bufp
++;
461 float gamma
= strtof(bufp
, &bufp
);
462 feature_gamma(fg
, &f
, &gamma
);
467 const char *features_gamma_filename
= "patterns.gamma";
469 struct features_gamma
*
470 features_gamma_init(struct pattern_config
*pc
, const char *file
)
472 struct features_gamma
*fg
= calloc(1, sizeof(*fg
));
474 for (int i
= 0; i
< FEAT_MAX
; i
++) {
475 int n
= feature_payloads(pc
, i
);
476 fg
->gamma
[i
] = malloc(n
* sizeof(float));
477 for (int j
= 0; j
< n
; j
++) {
478 fg
->gamma
[i
][j
] = 1.0f
;
481 features_gamma_load(fg
, file
? file
: features_gamma_filename
);
486 features_gamma_done(struct features_gamma
*fg
)
488 for (int i
= 0; i
< FEAT_MAX
; i
++)