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
= 5,
25 .ldist_min
= 0, .ldist_max
= 256,
29 pattern_spec PATTERN_SPEC_MATCHALL
= {
33 [FEAT_SELFATARI
] = ~0,
42 pattern_spec PATTERN_SPEC_MATCHFAST
= {
44 [FEAT_CAPTURE
] = ~(1<<PF_CAPTURE_ATARIDEF
)|~(1<<PF_CAPTURE_RECAPTURE
),
46 [FEAT_SELFATARI
] = ~(1<<PF_SELFATARI_SMART
),
55 static const struct feature_info
{
58 } features
[FEAT_MAX
] = {
59 [FEAT_PASS
] = { .name
= "pass", .payloads
= 2 },
60 [FEAT_CAPTURE
] = { .name
= "capture", .payloads
= 16 },
61 [FEAT_AESCAPE
] = { .name
= "atariescape", .payloads
= 2 },
62 [FEAT_SELFATARI
] = { .name
= "selfatari", .payloads
= 2 },
63 [FEAT_ATARI
] = { .name
= "atari", .payloads
= 4 },
64 [FEAT_BORDER
] = { .name
= "border", .payloads
= 1 },
65 [FEAT_LDIST
] = { .name
= "ldist", .payloads
= -1 },
66 [FEAT_LLDIST
] = { .name
= "lldist", .payloads
= -1 },
67 [FEAT_SPATIAL
] = { .name
= "s", .payloads
= -1 },
68 [FEAT_MCOWNER
] = { .name
= "mcowner", .payloads
= 16 },
72 feature2str(char *str
, struct feature
*f
)
74 return str
+ sprintf(str
+ strlen(str
), "%s:%"PRIx32
, features
[f
->id
].name
, f
->payload
);
78 str2feature(char *str
, struct feature
*f
)
80 while (isspace(*str
)) str
++;
82 int flen
= strcspn(str
, ":");
83 for (int i
= 0; i
< sizeof(features
)/sizeof(features
[0]); i
++)
84 if (strlen(features
[i
].name
) == flen
&& !strncmp(features
[i
].name
, str
, flen
)) {
88 fprintf(stderr
, "invalid featurespec: %s[%d]\n", str
, flen
);
93 f
->payload
= strtoull(str
, &str
, 16);
98 /* pattern_spec helpers */
99 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << 31))
100 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
102 static struct feature
*
103 pattern_match_capture(struct pattern_config
*pc
, pattern_spec ps
,
104 struct pattern
*p
, struct feature
*f
,
105 struct board
*b
, struct move
*m
)
107 foreach_neighbor(b
, m
->coord
, {
108 if (board_at(b
, c
) != stone_other(m
->color
))
110 group_t g
= group_at(b
, c
);
111 if (!g
|| board_group_info(b
, g
).libs
!= 1)
115 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
117 if (PS_PF(CAPTURE
, LADDER
))
118 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_CAPTURE_LADDER
;
119 /* TODO: is_ladder() is too conservative in some
120 * very obvious situations, look at complete.gtp. */
122 /* TODO: PF_CAPTURE_RECAPTURE */
124 if (PS_PF(CAPTURE
, ATARIDEF
))
125 foreach_in_group(b
, g
) {
126 foreach_neighbor(b
, c
, {
127 assert(board_at(b
, c
) != S_NONE
|| c
== m
->coord
);
128 if (board_at(b
, c
) != m
->color
)
130 group_t g
= group_at(b
, c
);
131 if (!g
|| board_group_info(b
, g
).libs
!= 1)
133 /* A neighboring group of ours is in atari. */
134 f
->payload
|= 1 << PF_CAPTURE_ATARIDEF
;
136 } foreach_in_group_end
;
138 if (PS_PF(CAPTURE
, KO
)
139 && group_is_onestone(b
, g
)
140 && neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))
141 + neighbor_count_at(b
, m
->coord
, S_OFFBOARD
) == 4)
142 f
->payload
|= 1 << PF_CAPTURE_KO
;
149 static struct feature
*
150 pattern_match_aescape(struct pattern_config
*pc
, pattern_spec ps
,
151 struct pattern
*p
, struct feature
*f
,
152 struct board
*b
, struct move
*m
)
154 /* Find if a neighboring group of ours is in atari, AND that we provide
155 * a liberty to connect out. XXX: No connect-and-die check. */
156 group_t in_atari
= -1;
157 bool has_extra_lib
= false;
160 foreach_neighbor(b
, m
->coord
, {
161 if (board_at(b
, c
) != m
->color
) {
162 if (board_at(b
, c
) == S_NONE
)
163 has_extra_lib
= true; // free point
164 else if (board_at(b
, c
) == stone_other(m
->color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
165 has_extra_lib
= true; // capturable enemy group
168 group_t g
= group_at(b
, c
); assert(g
);
169 if (board_group_info(b
, g
).libs
!= 1) {
170 has_extra_lib
= true;
177 if (PS_PF(AESCAPE
, LADDER
))
178 payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_AESCAPE_LADDER
;
179 /* TODO: is_ladder() is too conservative in some
180 * very obvious situations, look at complete.gtp. */
182 if (in_atari
>= 0 && has_extra_lib
) {
183 f
->id
= FEAT_AESCAPE
; f
->payload
= payload
;
189 static struct feature
*
190 pattern_match_atari(struct pattern_config
*pc
, pattern_spec ps
,
191 struct pattern
*p
, struct feature
*f
,
192 struct board
*b
, struct move
*m
)
194 foreach_neighbor(b
, m
->coord
, {
195 if (board_at(b
, c
) != stone_other(m
->color
))
197 group_t g
= group_at(b
, c
);
198 if (!g
|| board_group_info(b
, g
).libs
!= 2)
202 f
->id
= FEAT_ATARI
; f
->payload
= 0;
204 if (PS_PF(ATARI
, LADDER
)) {
205 /* Opponent will escape by the other lib. */
206 coord_t lib
= board_group_info(b
, g
).lib
[0];
207 if (lib
== m
->coord
) lib
= board_group_info(b
, g
).lib
[1];
208 /* TODO: is_ladder() is too conservative in some
209 * very obvious situations, look at complete.gtp. */
210 f
->payload
|= is_ladder(b
, lib
, g
, true, true) << PF_ATARI_LADDER
;
213 if (PS_PF(ATARI
, KO
) && !is_pass(b
->ko
.coord
))
214 f
->payload
|= 1 << PF_ATARI_KO
;
222 pattern_match_spatial(struct pattern_config
*pc
, pattern_spec ps
,
223 struct pattern
*p
, struct feature
*f
,
224 struct board
*b
, struct move
*m
)
226 /* XXX: This is partially duplicated from spatial_from_board(), but
227 * we build a hash instead of spatial record. */
229 assert(pc
->spat_min
> 0);
231 /* We record all spatial patterns black-to-play; simply
232 * reverse all colors if we are white-to-play. */
233 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
234 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
235 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
237 hash_t h
= pthashes
[0][0][S_NONE
];
238 #ifdef BOARD_SPATHASH
239 for (int d
= 2; d
<= BOARD_SPATHASH_MAXD
; d
++) {
240 /* Reuse all incrementally matched data. */
241 h
^= b
->spathash
[m
->coord
][d
- 1];
242 if (d
< pc
->spat_min
)
244 /* Record spatial feature, one per distance. */
245 f
->id
= FEAT_SPATIAL
;
246 f
->payload
= (d
<< PF_SPATIAL_RADIUS
);
247 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
249 f
->payload
|= sid
<< PF_SPATIAL_INDEX
;
251 } /* else not found, ignore */
254 #undef BOARD_SPATHASH_MAXD
255 #define BOARD_SPATHASH_MAXD 1
257 for (int d
= BOARD_SPATHASH_MAXD
+ 1; d
<= pc
->spat_max
; d
++) {
258 /* Recompute missing outer circles:
259 * Go through all points in given distance. */
260 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
261 int x
= coord_x(m
->coord
, b
) + ptcoords
[j
].x
;
262 int y
= coord_y(m
->coord
, b
) + ptcoords
[j
].y
;
263 if (x
>= board_size(b
)) x
= board_size(b
) - 1; else if (x
< 0) x
= 0;
264 if (y
>= board_size(b
)) y
= board_size(b
) - 1; else if (y
< 0) y
= 0;
265 h
^= pthashes
[0][j
][(*bt
)[board_atxy(b
, x
, y
)]];
267 if (d
< pc
->spat_min
)
269 /* Record spatial feature, one per distance. */
270 f
->id
= FEAT_SPATIAL
;
271 f
->payload
= (d
<< PF_SPATIAL_RADIUS
);
272 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
274 f
->payload
|= sid
<< PF_SPATIAL_INDEX
;
276 } /* else not found, ignore */
283 is_simple_selfatari(struct board
*b
, enum stone color
, coord_t coord
)
285 /* Very rough check, no connect-and-die checks or other trickery. */
286 int libs
= immediate_liberty_count(b
, coord
);
287 if (libs
>= 2) return false; // open space
290 foreach_neighbor(b
, coord
, {
291 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1) {
292 return false; // can capture
294 } else if (board_at(b
, c
) == color
) {
295 // friendly group, does it have liberties?
296 group_t g
= group_at(b
, c
);
297 if (board_group_info(b
, g
).libs
== 1 || seen
== g
)
299 libs
+= board_group_info(b
, g
).libs
- 1;
300 if (libs
>= 2) return false;
301 // don't consider the same group twice
309 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
310 struct pattern
*p
, struct board
*b
, struct move
*m
)
313 struct feature
*f
= &p
->f
[0];
315 /* TODO: We should match pretty much all of these features
318 if (is_pass(m
->coord
)) {
320 f
->id
= FEAT_PASS
; f
->payload
= 0;
321 if (PS_PF(PASS
, LASTPASS
))
322 f
->payload
|= (b
->moves
> 0 && is_pass(b
->last_move
.coord
))
329 if (PS_ANY(CAPTURE
)) {
330 f
= pattern_match_capture(pc
, ps
, p
, f
, b
, m
);
333 if (PS_ANY(AESCAPE
)) {
334 f
= pattern_match_aescape(pc
, ps
, p
, f
, b
, m
);
337 if (PS_ANY(SELFATARI
)) {
338 bool simple
= is_simple_selfatari(b
, m
->color
, m
->coord
);
339 bool thorough
= false;
340 if (PS_PF(SELFATARI
, SMART
)) {
341 thorough
= is_bad_selfatari(b
, m
->color
, m
->coord
);
343 if (simple
|| thorough
) {
344 f
->id
= FEAT_SELFATARI
;
345 f
->payload
= thorough
<< PF_SELFATARI_SMART
;
351 f
= pattern_match_atari(pc
, ps
, p
, f
, b
, m
);
354 if (PS_ANY(BORDER
)) {
355 int bdist
= coord_edge_distance(m
->coord
, b
);
356 if (bdist
<= pc
->bdist_max
) {
363 if (PS_ANY(LDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move
.coord
)) {
364 int ldist
= coord_gridcular_distance(m
->coord
, b
->last_move
.coord
, b
);
365 if (pc
->ldist_min
<= ldist
&& ldist
<= pc
->ldist_max
) {
372 if (PS_ANY(LLDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move2
.coord
)) {
373 int lldist
= coord_gridcular_distance(m
->coord
, b
->last_move2
.coord
, b
);
374 if (pc
->ldist_min
<= lldist
&& lldist
<= pc
->ldist_max
) {
381 if (PS_ANY(SPATIAL
) && pc
->spat_max
> 0 && pc
->spat_dict
) {
382 f
= pattern_match_spatial(pc
, ps
, p
, f
, b
, m
);
385 /* FEAT_MCOWNER: TODO */
390 pattern2str(char *str
, struct pattern
*p
)
392 str
= stpcpy(str
, "(");
393 for (int i
= 0; i
< p
->n
; i
++) {
394 if (i
> 0) str
= stpcpy(str
, " ");
395 str
= feature2str(str
, &p
->f
[i
]);
397 str
= stpcpy(str
, ")");
403 /*** Features gamma set */
406 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
408 FILE *f
= fopen(filename
, "r");
411 while (fgets(buf
, 256, f
)) {
414 bufp
= str2feature(bufp
, &f
);
415 while (isspace(*bufp
)) bufp
++;
416 float gamma
= strtof(bufp
, &bufp
);
417 feature_gamma(fg
, &f
, &gamma
);
422 const char *features_gamma_filename
= "patterns.gamma";
424 struct features_gamma
*
425 features_gamma_init(struct pattern_config
*pc
, const char *file
)
427 struct features_gamma
*fg
= calloc(1, sizeof(*fg
));
429 for (int i
= 0; i
< FEAT_MAX
; i
++) {
430 int n
= features
[i
].payloads
;
434 n
= pc
->spat_dict
->nspatials
; break;
437 n
= pc
->ldist_max
; break;
442 fg
->gamma
[i
] = malloc(n
* sizeof(float));
443 for (int j
= 0; j
< n
; j
++) {
444 fg
->gamma
[i
][j
] = 1.0f
;
447 features_gamma_load(fg
, file
? file
: features_gamma_filename
);
452 feature_gamma(struct features_gamma
*fg
, struct feature
*f
, float *gamma
)
454 /* XXX: We mask out spatial distance unconditionally since it shouldn't
455 * affect any other feature. */
456 int payid
= f
->payload
& ((1<<24)-1);
457 if (gamma
) fg
->gamma
[f
->id
][payid
] = *gamma
;
458 return fg
->gamma
[f
->id
][payid
];