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 #define FAST_NO_LADDER 1 /* 1: Don't match ladders in fast playouts */
49 pattern_spec PATTERN_SPEC_MATCHFAST
= {
51 [FEAT_CAPTURE
] = ~(1<<PF_CAPTURE_ATARIDEF
| 1<<PF_CAPTURE_RECAPTURE
| FAST_NO_LADDER
<<PF_CAPTURE_LADDER
| 1<<PF_CAPTURE_KO
),
52 [FEAT_AESCAPE
] = ~(FAST_NO_LADDER
<<PF_AESCAPE_LADDER
),
53 [FEAT_SELFATARI
] = ~(1<<PF_SELFATARI_SMART
),
58 [FEAT_CONTIGUITY
] = ~0,
64 static const struct feature_info
{
67 } features
[FEAT_MAX
] = {
68 [FEAT_PASS
] = { .name
= "pass", .payloads
= 2 },
69 [FEAT_CAPTURE
] = { .name
= "capture", .payloads
= 16 },
70 [FEAT_AESCAPE
] = { .name
= "atariescape", .payloads
= 2 },
71 [FEAT_SELFATARI
] = { .name
= "selfatari", .payloads
= 4 },
72 [FEAT_ATARI
] = { .name
= "atari", .payloads
= 4 },
73 [FEAT_BORDER
] = { .name
= "border", .payloads
= -1 },
74 [FEAT_LDIST
] = { .name
= "ldist", .payloads
= -1 },
75 [FEAT_LLDIST
] = { .name
= "lldist", .payloads
= -1 },
76 [FEAT_CONTIGUITY
] = { .name
= "cont", .payloads
= 2 },
77 [FEAT_SPATIAL
] = { .name
= "s", .payloads
= -1 },
78 [FEAT_PATTERN3
] = { .name
= "p", .payloads
= 2<<16 },
79 [FEAT_MCOWNER
] = { .name
= "mcowner", .payloads
= 16 },
83 feature2str(char *str
, struct feature
*f
)
85 return str
+ sprintf(str
+ strlen(str
), "%s:%d", features
[f
->id
].name
, f
->payload
);
89 str2feature(char *str
, struct feature
*f
)
91 while (isspace(*str
)) str
++;
93 int unsigned flen
= strcspn(str
, ":");
94 for (unsigned int i
= 0; i
< sizeof(features
)/sizeof(features
[0]); i
++)
95 if (strlen(features
[i
].name
) == flen
&& !strncmp(features
[i
].name
, str
, flen
)) {
99 fprintf(stderr
, "invalid featurespec: %s[%d]\n", str
, flen
);
104 f
->payload
= strtoull(str
, &str
, 10);
109 feature_name(enum feature_id f
)
111 return features
[f
].name
;
115 feature_payloads(struct pattern_config
*pc
, enum feature_id f
)
119 assert(features
[f
].payloads
< 0);
120 return pc
->spat_dict
->nspatials
;
123 assert(features
[f
].payloads
< 0);
124 return pc
->ldist_max
+ 1;
126 assert(features
[f
].payloads
< 0);
127 return pc
->bdist_max
+ 1;
129 assert(features
[f
].payloads
> 0);
130 return features
[f
].payloads
;
135 /* pattern_spec helpers */
136 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << 15))
137 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
139 static struct feature
*
140 pattern_match_capture(struct pattern_config
*pc
, pattern_spec ps
,
141 struct pattern
*p
, struct feature
*f
,
142 struct board
*b
, struct move
*m
)
144 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
146 if (!trait_at(b
, m
->coord
, m
->color
).cap
)
149 if (!(PS_PF(CAPTURE
, LADDER
)
150 || PS_PF(CAPTURE
, RECAPTURE
)
151 || PS_PF(CAPTURE
, ATARIDEF
)
152 || PS_PF(CAPTURE
, KO
))) {
156 /* We need to know details, so we still have to go through
160 /* Furthermore, we will now create one feature per capturable
162 /* XXX: I'm not sure if this is really good idea. --pasky */
163 foreach_neighbor(b
, m
->coord
, {
164 if (board_at(b
, c
) != stone_other(m
->color
))
166 group_t g
= group_at(b
, c
);
167 if (!g
|| board_group_info(b
, g
).libs
!= 1)
172 if (PS_PF(CAPTURE
, LADDER
))
173 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_CAPTURE_LADDER
;
174 /* TODO: is_ladder() is too conservative in some
175 * very obvious situations, look at complete.gtp. */
177 /* TODO: PF_CAPTURE_RECAPTURE */
179 if (PS_PF(CAPTURE
, ATARIDEF
))
180 foreach_in_group(b
, g
) {
181 foreach_neighbor(b
, c
, {
182 assert(board_at(b
, c
) != S_NONE
|| c
== m
->coord
);
183 if (board_at(b
, c
) != m
->color
)
185 group_t g
= group_at(b
, c
);
186 if (!g
|| board_group_info(b
, g
).libs
!= 1)
188 /* A neighboring group of ours is in atari. */
189 f
->payload
|= 1 << PF_CAPTURE_ATARIDEF
;
191 } foreach_in_group_end
;
193 if (PS_PF(CAPTURE
, KO
)
194 && group_is_onestone(b
, g
)
195 && neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))
196 + neighbor_count_at(b
, m
->coord
, S_OFFBOARD
) == 4)
197 f
->payload
|= 1 << PF_CAPTURE_KO
;
200 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
205 static struct feature
*
206 pattern_match_aescape(struct pattern_config
*pc
, pattern_spec ps
,
207 struct pattern
*p
, struct feature
*f
,
208 struct board
*b
, struct move
*m
)
210 f
->id
= FEAT_AESCAPE
; f
->payload
= 0;
212 if (!trait_at(b
, m
->coord
, stone_other(m
->color
)).cap
213 || !trait_at(b
, m
->coord
, m
->color
).safe
)
215 /* Opponent can capture something and this move is safe
217 if (!PS_PF(AESCAPE
, LADDER
)) {
221 /* We need to know details, so we still have to go through
225 /* Find if a neighboring group of ours is in atari, AND that we provide
226 * a liberty to connect out. XXX: No connect-and-die check. */
227 group_t in_atari
= -1;
228 bool has_extra_lib
= false;
230 foreach_neighbor(b
, m
->coord
, {
231 if (board_at(b
, c
) != m
->color
) {
232 if (board_at(b
, c
) == S_NONE
)
233 has_extra_lib
= true; // free point
234 else if (board_at(b
, c
) == stone_other(m
->color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
235 has_extra_lib
= true; // capturable enemy group
238 group_t g
= group_at(b
, c
); assert(g
);
239 if (board_group_info(b
, g
).libs
!= 1) {
240 has_extra_lib
= true;
247 if (PS_PF(AESCAPE
, LADDER
))
248 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_AESCAPE_LADDER
;
249 /* TODO: is_ladder() is too conservative in some
250 * very obvious situations, look at complete.gtp. */
252 if (in_atari
>= 0 && has_extra_lib
) {
258 static struct feature
*
259 pattern_match_atari(struct pattern_config
*pc
, pattern_spec ps
,
260 struct pattern
*p
, struct feature
*f
,
261 struct board
*b
, struct move
*m
)
263 foreach_neighbor(b
, m
->coord
, {
264 if (board_at(b
, c
) != stone_other(m
->color
))
266 group_t g
= group_at(b
, c
);
267 if (!g
|| board_group_info(b
, g
).libs
!= 2)
271 f
->id
= FEAT_ATARI
; f
->payload
= 0;
273 if (PS_PF(ATARI
, LADDER
)) {
274 /* Opponent will escape by the other lib. */
275 coord_t lib
= board_group_info(b
, g
).lib
[0];
276 if (lib
== m
->coord
) lib
= board_group_info(b
, g
).lib
[1];
277 /* TODO: is_ladder() is too conservative in some
278 * very obvious situations, look at complete.gtp. */
279 f
->payload
|= is_ladder(b
, lib
, g
, true, true) << PF_ATARI_LADDER
;
282 if (PS_PF(ATARI
, KO
) && !is_pass(b
->ko
.coord
))
283 f
->payload
|= 1 << PF_ATARI_KO
;
290 #ifndef BOARD_SPATHASH
291 #undef BOARD_SPATHASH_MAXD
292 #define BOARD_SPATHASH_MAXD 1
295 /* Match spatial features that are too distant to be pre-matched
298 pattern_match_spatial_outer(struct pattern_config
*pc
, pattern_spec ps
,
299 struct pattern
*p
, struct feature
*f
,
300 struct board
*b
, struct move
*m
, hash_t h
)
302 /* We record all spatial patterns black-to-play; simply
303 * reverse all colors if we are white-to-play. */
304 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
305 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
306 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
308 for (int d
= BOARD_SPATHASH_MAXD
+ 1; d
<= pc
->spat_max
; d
++) {
309 /* Recompute missing outer circles:
310 * Go through all points in given distance. */
311 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
312 ptcoords_at(x
, y
, m
->coord
, b
, j
);
313 h
^= pthashes
[0][j
][(*bt
)[board_atxy(b
, x
, y
)]];
315 if (d
< pc
->spat_min
)
317 /* Record spatial feature, one per distance. */
318 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
320 f
->id
= FEAT_SPATIAL
;
323 } /* else not found, ignore */
329 pattern_match_spatial(struct pattern_config
*pc
, pattern_spec ps
,
330 struct pattern
*p
, struct feature
*f
,
331 struct board
*b
, struct move
*m
)
333 /* XXX: This is partially duplicated from spatial_from_board(), but
334 * we build a hash instead of spatial record. */
336 assert(pc
->spat_min
> 0);
338 hash_t h
= pthashes
[0][0][S_NONE
];
339 #ifdef BOARD_SPATHASH
340 bool w_to_play
= m
->color
== S_WHITE
;
341 for (int d
= 2; d
<= BOARD_SPATHASH_MAXD
; d
++) {
342 /* Reuse all incrementally matched data. */
343 h
^= b
->spathash
[m
->coord
][d
- 1][w_to_play
];
344 if (d
< pc
->spat_min
)
346 /* Record spatial feature, one per distance. */
347 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
349 f
->id
= FEAT_SPATIAL
;
352 } /* else not found, ignore */
355 assert(BOARD_SPATHASH_MAXD
< 2);
357 if (unlikely(pc
->spat_max
> BOARD_SPATHASH_MAXD
))
358 f
= pattern_match_spatial_outer(pc
, ps
, p
, f
, b
, m
, h
);
364 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
365 struct pattern
*p
, struct board
*b
, struct move
*m
)
368 struct feature
*f
= &p
->f
[0];
370 /* TODO: We should match pretty much all of these features
373 if (is_pass(m
->coord
)) {
375 f
->id
= FEAT_PASS
; f
->payload
= 0;
376 if (PS_PF(PASS
, LASTPASS
))
377 f
->payload
|= (b
->moves
> 0 && is_pass(b
->last_move
.coord
))
384 if (PS_ANY(CAPTURE
)) {
385 f
= pattern_match_capture(pc
, ps
, p
, f
, b
, m
);
388 if (PS_ANY(AESCAPE
)) {
389 f
= pattern_match_aescape(pc
, ps
, p
, f
, b
, m
);
392 if (PS_ANY(SELFATARI
)) {
394 if (PS_PF(SELFATARI
, STUPID
)) {
396 if (!b
->precise_selfatari
)
397 simple
= !trait_at(b
, m
->coord
, m
->color
).safe
;
400 simple
= !board_safe_to_play(b
, m
->coord
, m
->color
);
402 bool thorough
= false;
403 if (PS_PF(SELFATARI
, SMART
)) {
405 if (b
->precise_selfatari
)
406 thorough
= !trait_at(b
, m
->coord
, m
->color
).safe
;
409 thorough
= is_bad_selfatari(b
, m
->color
, m
->coord
);
411 if (simple
|| thorough
) {
412 f
->id
= FEAT_SELFATARI
;
413 f
->payload
= simple
<< PF_SELFATARI_STUPID
;
414 f
->payload
|= thorough
<< PF_SELFATARI_SMART
;
420 f
= pattern_match_atari(pc
, ps
, p
, f
, b
, m
);
423 if (PS_ANY(BORDER
)) {
424 int bdist
= coord_edge_distance(m
->coord
, b
);
425 if (bdist
<= pc
->bdist_max
) {
432 if (PS_ANY(CONTIGUITY
) && !is_pass(b
->last_move
.coord
)
433 && coord_is_8adjecent(m
->coord
, b
->last_move
.coord
, b
)) {
434 f
->id
= FEAT_CONTIGUITY
;
439 if (PS_ANY(LDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move
.coord
)) {
440 int ldist
= coord_gridcular_distance(m
->coord
, b
->last_move
.coord
, b
);
441 if (pc
->ldist_min
<= ldist
&& ldist
<= pc
->ldist_max
) {
448 if (PS_ANY(LLDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move2
.coord
)) {
449 int lldist
= coord_gridcular_distance(m
->coord
, b
->last_move2
.coord
, b
);
450 if (pc
->ldist_min
<= lldist
&& lldist
<= pc
->ldist_max
) {
457 if (PS_ANY(SPATIAL
) && pc
->spat_max
> 0 && pc
->spat_dict
) {
458 f
= pattern_match_spatial(pc
, ps
, p
, f
, b
, m
);
461 if (PS_ANY(PATTERN3
) && !is_pass(m
->coord
)) {
463 hash3_t pat
= b
->pat3
[m
->coord
];
465 hash3_t pat
= pattern3_hash(b
, m
->coord
);
467 if (m
->color
== S_WHITE
) {
468 /* We work with the pattern3s as black-to-play. */
469 pat
= pattern3_reverse(pat
);
471 f
->id
= FEAT_PATTERN3
;
476 /* FEAT_MCOWNER: TODO */
481 pattern2str(char *str
, struct pattern
*p
)
483 str
= stpcpy(str
, "(");
484 for (int i
= 0; i
< p
->n
; i
++) {
485 if (i
> 0) str
= stpcpy(str
, " ");
486 str
= feature2str(str
, &p
->f
[i
]);
488 str
= stpcpy(str
, ")");
494 /*** Features gamma set */
497 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
499 FILE *f
= fopen(filename
, "r");
502 while (fgets(buf
, 256, f
)) {
505 bufp
= str2feature(bufp
, &f
);
506 while (isspace(*bufp
)) bufp
++;
507 double gamma
= strtod(bufp
, &bufp
);
508 /* Record feature's gamma. */
509 feature_gamma(fg
, &f
, &gamma
);
510 /* In case of 3x3 patterns, record gamma also
511 * for all rotations and transpositions. */
512 if (f
.id
== FEAT_PATTERN3
) {
514 pattern3_transpose(f
.payload
, &transp
);
515 for (int i
= 1; i
< 8; i
++) {
516 f
.payload
= transp
[i
];
517 feature_gamma(fg
, &f
, &gamma
);
519 f
.payload
= transp
[0];
525 const char *features_gamma_filename
= "patterns.gamma";
527 struct features_gamma
*
528 features_gamma_init(struct pattern_config
*pc
, const char *file
)
530 struct features_gamma
*fg
= calloc2(1, sizeof(*fg
));
532 for (int i
= 0; i
< FEAT_MAX
; i
++) {
533 int n
= feature_payloads(pc
, i
);
534 fg
->gamma
[i
] = malloc2(n
* sizeof(fg
->gamma
[0][0]));
535 for (int j
= 0; j
< n
; j
++) {
536 fg
->gamma
[i
][j
] = 1.0f
;
539 features_gamma_load(fg
, file
? file
: features_gamma_filename
);
544 features_gamma_done(struct features_gamma
*fg
)
546 for (int i
= 0; i
< FEAT_MAX
; i
++)