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
= 64 },
70 [FEAT_AESCAPE
] = { .name
= "atariescape", .payloads
= 8 },
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
))) {
153 if (PS_PF(CAPTURE
, 1STONE
))
154 f
->payload
|= (trait_at(b
, m
->coord
, m
->color
).cap1
== trait_at(b
, m
->coord
, m
->color
).cap
) << PF_CAPTURE_1STONE
;
155 if (PS_PF(CAPTURE
, TRAPPED
))
156 f
->payload
|= (!trait_at(b
, m
->coord
, m
->color
).safe
) << PF_CAPTURE_TRAPPED
;
160 /* We need to know details, so we still have to go through
164 /* Furthermore, we will now create one feature per capturable
166 /* XXX: I'm not sure if this is really good idea. --pasky */
168 /* Whether an escape move would be safe for the opponent. */
169 bool can_escape
= false;
170 foreach_neighbor(b
, m
->coord
, {
171 if (board_at(b
, c
) != stone_other(m
->color
)) {
172 if (board_at(b
, c
) == S_NONE
)
173 can_escape
= true; // free point
174 else if (board_at(b
, c
) == m
->color
&& board_group_info(b
, group_at(b
, c
)).libs
== 1)
175 can_escape
= true; // capturable our group
178 group_t g
= group_at(b
, c
); assert(g
);
179 if (board_group_info(b
, g
).libs
!= 1)
184 foreach_neighbor(b
, m
->coord
, {
185 if (board_at(b
, c
) != stone_other(m
->color
))
188 group_t g
= group_at(b
, c
); assert(g
);
189 if (board_group_info(b
, g
).libs
!= 1)
194 if (PS_PF(CAPTURE
, LADDER
))
195 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_CAPTURE_LADDER
;
196 /* TODO: is_ladder() is too conservative in some
197 * very obvious situations, look at complete.gtp. */
199 /* TODO: PF_CAPTURE_RECAPTURE */
201 if (PS_PF(CAPTURE
, ATARIDEF
))
202 foreach_in_group(b
, g
) {
203 foreach_neighbor(b
, c
, {
204 assert(board_at(b
, c
) != S_NONE
|| c
== m
->coord
);
205 if (board_at(b
, c
) != m
->color
)
207 group_t g
= group_at(b
, c
);
208 if (!g
|| board_group_info(b
, g
).libs
!= 1)
210 /* A neighboring group of ours is in atari. */
211 f
->payload
|= 1 << PF_CAPTURE_ATARIDEF
;
213 } foreach_in_group_end
;
215 if (PS_PF(CAPTURE
, KO
)
216 && group_is_onestone(b
, g
)
217 && neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))
218 + neighbor_count_at(b
, m
->coord
, S_OFFBOARD
) == 4)
219 f
->payload
|= 1 << PF_CAPTURE_KO
;
221 if (PS_PF(CAPTURE
, 1STONE
))
222 f
->payload
|= group_is_onestone(b
, g
) << PF_CAPTURE_1STONE
;
224 if (PS_PF(CAPTURE
, TRAPPED
))
225 f
->payload
|= (!can_escape
) << PF_CAPTURE_TRAPPED
;
228 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
233 static struct feature
*
234 pattern_match_aescape(struct pattern_config
*pc
, pattern_spec ps
,
235 struct pattern
*p
, struct feature
*f
,
236 struct board
*b
, struct move
*m
)
238 f
->id
= FEAT_AESCAPE
; f
->payload
= 0;
240 if (!trait_at(b
, m
->coord
, stone_other(m
->color
)).cap
)
242 /* Opponent can capture something! */
243 if (!PS_PF(AESCAPE
, LADDER
)) {
244 if (PS_PF(AESCAPE
, 1STONE
))
245 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
;
246 if (PS_PF(CAPTURE
, TRAPPED
))
247 f
->payload
|= (!trait_at(b
, m
->coord
, m
->color
).safe
) << PF_AESCAPE_TRAPPED
;
251 /* We need to know details, so we still have to go through
255 /* Find if a neighboring group of ours is in atari, AND that we provide
256 * a liberty to connect out. XXX: No connect-and-die check. */
257 group_t in_atari
= -1;
258 bool has_extra_lib
= false;
259 bool onestone
= false, multistone
= false;
261 foreach_neighbor(b
, m
->coord
, {
262 if (board_at(b
, c
) != m
->color
) {
263 if (board_at(b
, c
) == S_NONE
)
264 has_extra_lib
= true; // free point
265 else if (board_at(b
, c
) == stone_other(m
->color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
266 has_extra_lib
= true; // capturable enemy group
269 group_t g
= group_at(b
, c
); assert(g
);
270 if (board_group_info(b
, g
).libs
!= 1) {
271 has_extra_lib
= true;
278 if (PS_PF(AESCAPE
, LADDER
))
279 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_AESCAPE_LADDER
;
280 /* TODO: is_ladder() is too conservative in some
281 * very obvious situations, look at complete.gtp. */
283 if (group_is_onestone(b
, g
))
288 if (PS_PF(AESCAPE
, 1STONE
))
289 f
->payload
|= (onestone
&& !multistone
) << PF_AESCAPE_1STONE
;
290 if (PS_PF(AESCAPE
, TRAPPED
))
291 f
->payload
|= has_extra_lib
<< PF_AESCAPE_TRAPPED
;
298 static struct feature
*
299 pattern_match_atari(struct pattern_config
*pc
, pattern_spec ps
,
300 struct pattern
*p
, struct feature
*f
,
301 struct board
*b
, struct move
*m
)
303 foreach_neighbor(b
, m
->coord
, {
304 if (board_at(b
, c
) != stone_other(m
->color
))
306 group_t g
= group_at(b
, c
);
307 if (!g
|| board_group_info(b
, g
).libs
!= 2)
311 f
->id
= FEAT_ATARI
; f
->payload
= 0;
313 if (PS_PF(ATARI
, LADDER
)) {
314 /* Opponent will escape by the other lib. */
315 coord_t lib
= board_group_info(b
, g
).lib
[0];
316 if (lib
== m
->coord
) lib
= board_group_info(b
, g
).lib
[1];
317 /* TODO: is_ladder() is too conservative in some
318 * very obvious situations, look at complete.gtp. */
319 f
->payload
|= is_ladder(b
, lib
, g
, true, true) << PF_ATARI_LADDER
;
322 if (PS_PF(ATARI
, KO
) && !is_pass(b
->ko
.coord
))
323 f
->payload
|= 1 << PF_ATARI_KO
;
330 #ifndef BOARD_SPATHASH
331 #undef BOARD_SPATHASH_MAXD
332 #define BOARD_SPATHASH_MAXD 1
335 /* Match spatial features that are too distant to be pre-matched
338 pattern_match_spatial_outer(struct pattern_config
*pc
, pattern_spec ps
,
339 struct pattern
*p
, struct feature
*f
,
340 struct board
*b
, struct move
*m
, hash_t h
)
342 /* We record all spatial patterns black-to-play; simply
343 * reverse all colors if we are white-to-play. */
344 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
345 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
346 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
348 for (int d
= BOARD_SPATHASH_MAXD
+ 1; d
<= pc
->spat_max
; d
++) {
349 /* Recompute missing outer circles:
350 * Go through all points in given distance. */
351 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
352 ptcoords_at(x
, y
, m
->coord
, b
, j
);
353 h
^= pthashes
[0][j
][(*bt
)[board_atxy(b
, x
, y
)]];
355 if (d
< pc
->spat_min
)
357 /* Record spatial feature, one per distance. */
358 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
360 f
->id
= FEAT_SPATIAL
;
363 } /* else not found, ignore */
369 pattern_match_spatial(struct pattern_config
*pc
, pattern_spec ps
,
370 struct pattern
*p
, struct feature
*f
,
371 struct board
*b
, struct move
*m
)
373 /* XXX: This is partially duplicated from spatial_from_board(), but
374 * we build a hash instead of spatial record. */
376 assert(pc
->spat_min
> 0);
378 hash_t h
= pthashes
[0][0][S_NONE
];
379 #ifdef BOARD_SPATHASH
380 bool w_to_play
= m
->color
== S_WHITE
;
381 for (int d
= 2; d
<= BOARD_SPATHASH_MAXD
; d
++) {
382 /* Reuse all incrementally matched data. */
383 h
^= b
->spathash
[m
->coord
][d
- 1][w_to_play
];
384 if (d
< pc
->spat_min
)
386 /* Record spatial feature, one per distance. */
387 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
389 f
->id
= FEAT_SPATIAL
;
392 } /* else not found, ignore */
395 assert(BOARD_SPATHASH_MAXD
< 2);
397 if (unlikely(pc
->spat_max
> BOARD_SPATHASH_MAXD
))
398 f
= pattern_match_spatial_outer(pc
, ps
, p
, f
, b
, m
, h
);
404 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
405 struct pattern
*p
, struct board
*b
, struct move
*m
)
408 struct feature
*f
= &p
->f
[0];
410 /* TODO: We should match pretty much all of these features
413 if (is_pass(m
->coord
)) {
415 f
->id
= FEAT_PASS
; f
->payload
= 0;
416 if (PS_PF(PASS
, LASTPASS
))
417 f
->payload
|= (b
->moves
> 0 && is_pass(b
->last_move
.coord
))
424 if (PS_ANY(CAPTURE
)) {
425 f
= pattern_match_capture(pc
, ps
, p
, f
, b
, m
);
428 if (PS_ANY(AESCAPE
)) {
429 f
= pattern_match_aescape(pc
, ps
, p
, f
, b
, m
);
432 if (PS_ANY(SELFATARI
)) {
434 if (PS_PF(SELFATARI
, STUPID
)) {
436 if (!b
->precise_selfatari
)
437 simple
= !trait_at(b
, m
->coord
, m
->color
).safe
;
440 simple
= !board_safe_to_play(b
, m
->coord
, m
->color
);
442 bool thorough
= false;
443 if (PS_PF(SELFATARI
, SMART
)) {
445 if (b
->precise_selfatari
)
446 thorough
= !trait_at(b
, m
->coord
, m
->color
).safe
;
449 thorough
= is_bad_selfatari(b
, m
->color
, m
->coord
);
451 if (simple
|| thorough
) {
452 f
->id
= FEAT_SELFATARI
;
453 f
->payload
= simple
<< PF_SELFATARI_STUPID
;
454 f
->payload
|= thorough
<< PF_SELFATARI_SMART
;
460 f
= pattern_match_atari(pc
, ps
, p
, f
, b
, m
);
463 if (PS_ANY(BORDER
)) {
464 int bdist
= coord_edge_distance(m
->coord
, b
);
465 if (bdist
<= pc
->bdist_max
) {
472 if (PS_ANY(CONTIGUITY
) && !is_pass(b
->last_move
.coord
)
473 && coord_is_8adjecent(m
->coord
, b
->last_move
.coord
, b
)) {
474 f
->id
= FEAT_CONTIGUITY
;
479 if (PS_ANY(LDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move
.coord
)) {
480 int ldist
= coord_gridcular_distance(m
->coord
, b
->last_move
.coord
, b
);
481 if (pc
->ldist_min
<= ldist
&& ldist
<= pc
->ldist_max
) {
488 if (PS_ANY(LLDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move2
.coord
)) {
489 int lldist
= coord_gridcular_distance(m
->coord
, b
->last_move2
.coord
, b
);
490 if (pc
->ldist_min
<= lldist
&& lldist
<= pc
->ldist_max
) {
497 if (PS_ANY(SPATIAL
) && pc
->spat_max
> 0 && pc
->spat_dict
) {
498 f
= pattern_match_spatial(pc
, ps
, p
, f
, b
, m
);
501 if (PS_ANY(PATTERN3
) && !is_pass(m
->coord
)) {
503 hash3_t pat
= b
->pat3
[m
->coord
];
505 hash3_t pat
= pattern3_hash(b
, m
->coord
);
507 if (m
->color
== S_WHITE
) {
508 /* We work with the pattern3s as black-to-play. */
509 pat
= pattern3_reverse(pat
);
511 f
->id
= FEAT_PATTERN3
;
516 /* FEAT_MCOWNER: TODO */
521 pattern2str(char *str
, struct pattern
*p
)
523 str
= stpcpy(str
, "(");
524 for (int i
= 0; i
< p
->n
; i
++) {
525 if (i
> 0) str
= stpcpy(str
, " ");
526 str
= feature2str(str
, &p
->f
[i
]);
528 str
= stpcpy(str
, ")");
534 /*** Features gamma set */
537 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
539 FILE *f
= fopen(filename
, "r");
542 while (fgets(buf
, 256, f
)) {
545 bufp
= str2feature(bufp
, &f
);
546 while (isspace(*bufp
)) bufp
++;
547 double gamma
= strtod(bufp
, &bufp
);
548 /* Record feature's gamma. */
549 feature_gamma(fg
, &f
, &gamma
);
550 /* In case of 3x3 patterns, record gamma also
551 * for all rotations and transpositions. */
552 if (f
.id
== FEAT_PATTERN3
) {
554 pattern3_transpose(f
.payload
, &transp
);
555 for (int i
= 1; i
< 8; i
++) {
556 f
.payload
= transp
[i
];
557 feature_gamma(fg
, &f
, &gamma
);
559 f
.payload
= transp
[0];
565 const char *features_gamma_filename
= "patterns.gamma";
567 struct features_gamma
*
568 features_gamma_init(struct pattern_config
*pc
, const char *file
)
570 struct features_gamma
*fg
= calloc2(1, sizeof(*fg
));
572 for (int i
= 0; i
< FEAT_MAX
; i
++) {
573 int n
= feature_payloads(pc
, i
);
574 fg
->gamma
[i
] = malloc2(n
* sizeof(fg
->gamma
[0][0]));
575 for (int j
= 0; j
< n
; j
++) {
576 fg
->gamma
[i
][j
] = 1.0f
;
579 features_gamma_load(fg
, file
? file
: features_gamma_filename
);
584 features_gamma_done(struct features_gamma
*fg
)
586 for (int i
= 0; i
< FEAT_MAX
; i
++)