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,
32 pattern_spec PATTERN_SPEC_MATCHALL
= {
36 [FEAT_SELFATARI
] = ~0,
41 [FEAT_CONTIGUITY
] = 0,
47 /* !!! Note that in order for ELO playout policy to work correctly, this
48 * pattern specification MUST exactly match the features matched by the
49 * BOARD_GAMMA code! You cannot just tinker with this spec freely. */
50 pattern_spec PATTERN_SPEC_MATCHFAST
= {
52 [FEAT_CAPTURE
] = (1<<PF_MATCH
| 1<<PF_CAPTURE_1STONE
| 1<<PF_CAPTURE_TRAPPED
| 1<<PF_CAPTURE_CONNECTION
),
53 [FEAT_AESCAPE
] = (1<<PF_MATCH
| 1<<PF_AESCAPE_1STONE
| 1<<PF_AESCAPE_TRAPPED
| 1<<PF_AESCAPE_CONNECTION
),
54 [FEAT_SELFATARI
] = (1<<PF_MATCH
| 1<<PF_SELFATARI_STUPID
),
59 [FEAT_CONTIGUITY
] = ~0,
65 static const struct feature_info
{
68 } features
[FEAT_MAX
] = {
69 [FEAT_PASS
] = { .name
= "pass", .payloads
= 2 },
70 [FEAT_CAPTURE
] = { .name
= "capture", .payloads
= 128 },
71 [FEAT_AESCAPE
] = { .name
= "atariescape", .payloads
= 16 },
72 [FEAT_SELFATARI
] = { .name
= "selfatari", .payloads
= 4 },
73 [FEAT_ATARI
] = { .name
= "atari", .payloads
= 4 },
74 [FEAT_BORDER
] = { .name
= "border", .payloads
= -1 },
75 [FEAT_LDIST
] = { .name
= "ldist", .payloads
= -1 },
76 [FEAT_LLDIST
] = { .name
= "lldist", .payloads
= -1 },
77 [FEAT_CONTIGUITY
] = { .name
= "cont", .payloads
= 2 },
78 [FEAT_SPATIAL
] = { .name
= "s", .payloads
= -1 },
79 [FEAT_PATTERN3
] = { .name
= "p", .payloads
= 2<<16 },
80 [FEAT_MCOWNER
] = { .name
= "mcowner", .payloads
= 16 },
84 feature2str(char *str
, struct feature
*f
)
86 return str
+ sprintf(str
+ strlen(str
), "%s:%d", features
[f
->id
].name
, f
->payload
);
90 str2feature(char *str
, struct feature
*f
)
92 while (isspace(*str
)) str
++;
94 int unsigned flen
= strcspn(str
, ":");
95 for (unsigned int i
= 0; i
< sizeof(features
)/sizeof(features
[0]); i
++)
96 if (strlen(features
[i
].name
) == flen
&& !strncmp(features
[i
].name
, str
, flen
)) {
100 fprintf(stderr
, "invalid featurespec: %s[%d]\n", str
, flen
);
105 f
->payload
= strtoull(str
, &str
, 10);
110 feature_name(enum feature_id f
)
112 return features
[f
].name
;
116 feature_payloads(struct pattern_config
*pc
, enum feature_id f
)
120 assert(features
[f
].payloads
< 0);
121 return pc
->spat_dict
->nspatials
;
124 assert(features
[f
].payloads
< 0);
125 return pc
->ldist_max
+ 1;
127 assert(features
[f
].payloads
< 0);
128 return pc
->bdist_max
+ 1;
130 assert(features
[f
].payloads
> 0);
131 return features
[f
].payloads
;
136 /* pattern_spec helpers */
137 #define PS_ANY(F) (ps[FEAT_ ## F] & (1 << PF_MATCH))
138 #define PS_PF(F, P) (ps[FEAT_ ## F] & (1 << PF_ ## F ## _ ## P))
140 //#undef BOARD_TRAITS // for cross-testing of pattern matchers - enable also elo_check_probdist()
142 static struct feature
*
143 pattern_match_capture(struct pattern_config
*pc
, pattern_spec ps
,
144 struct pattern
*p
, struct feature
*f
,
145 struct board
*b
, struct move
*m
)
147 f
->id
= FEAT_CAPTURE
; f
->payload
= 0;
149 if (!trait_at(b
, m
->coord
, m
->color
).cap
)
152 if ((ps
[FEAT_CAPTURE
] & ~(1<<PF_CAPTURE_1STONE
| 1<<PF_CAPTURE_TRAPPED
| 1<<PF_CAPTURE_CONNECTION
)) == 1<<PF_MATCH
) {
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
, stone_other(m
->color
)).safe
) << PF_CAPTURE_TRAPPED
;
157 if (PS_PF(CAPTURE
, CONNECTION
))
158 f
->payload
|= (trait_at(b
, m
->coord
, m
->color
).cap
< neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))) << PF_CAPTURE_CONNECTION
;
162 /* We need to know details, so we still have to go through
166 /* We look at neighboring groups we could capture, and also if the
167 * opponent could save them. */
168 /* This is very similar in spirit to board_safe_to_play(), and almost
169 * a color inverse of pattern_match_aescape(). */
171 /* Whether an escape move would be safe for the opponent. */
174 int extra_libs
= 0, connectable_groups
= 0;
175 bool onestone
= false, multistone
= false;
177 foreach_neighbor(b
, m
->coord
, {
178 if (board_at(b
, c
) != stone_other(m
->color
)) {
179 if (board_at(b
, c
) == S_NONE
)
180 extra_libs
++; // free point
181 else if (board_at(b
, c
) == m
->color
&& board_group_info(b
, group_at(b
, c
)).libs
== 1)
182 extra_libs
+= 2; // capturable enemy group
186 group_t g
= group_at(b
, c
); assert(g
);
187 if (board_group_info(b
, g
).libs
> 1) {
188 connectable_groups
++;
189 if (board_group_info(b
, g
).libs
> 2) {
190 extra_libs
+= 2; // connected out
192 /* This is a bit tricky; we connect our 2-lib
193 * group to another 2-lib group, which counts
194 * as one liberty, but only if the other lib
195 * is not shared too. */
197 onelib
= board_group_other_lib(b
, g
, c
);
201 extra_libs
--; // take that back
212 if (PS_PF(CAPTURE
, LADDER
))
213 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_CAPTURE_LADDER
;
214 /* TODO: is_ladder() is too conservative in some
215 * very obvious situations, look at complete.gtp. */
217 /* TODO: PF_CAPTURE_RECAPTURE */
219 if (PS_PF(CAPTURE
, ATARIDEF
))
220 foreach_in_group(b
, g
) {
221 foreach_neighbor(b
, c
, {
222 assert(board_at(b
, c
) != S_NONE
|| c
== m
->coord
);
223 if (board_at(b
, c
) != m
->color
)
225 group_t g
= group_at(b
, c
);
226 if (!g
|| board_group_info(b
, g
).libs
!= 1)
228 /* A neighboring group of ours is in atari. */
229 f
->payload
|= 1 << PF_CAPTURE_ATARIDEF
;
231 } foreach_in_group_end
;
233 if (PS_PF(CAPTURE
, KO
)
234 && group_is_onestone(b
, g
)
235 && neighbor_count_at(b
, m
->coord
, stone_other(m
->color
))
236 + neighbor_count_at(b
, m
->coord
, S_OFFBOARD
) == 4)
237 f
->payload
|= 1 << PF_CAPTURE_KO
;
239 if (group_is_onestone(b
, g
))
246 if (PS_PF(CAPTURE
, 1STONE
))
247 f
->payload
|= (onestone
&& !multistone
) << PF_CAPTURE_1STONE
;
248 if (PS_PF(CAPTURE
, TRAPPED
))
249 f
->payload
|= (extra_libs
< 2) << PF_CAPTURE_TRAPPED
;
250 if (PS_PF(CAPTURE
, CONNECTION
))
251 f
->payload
|= (connectable_groups
> 0) << PF_CAPTURE_CONNECTION
;
257 static struct feature
*
258 pattern_match_aescape(struct pattern_config
*pc
, pattern_spec ps
,
259 struct pattern
*p
, struct feature
*f
,
260 struct board
*b
, struct move
*m
)
262 f
->id
= FEAT_AESCAPE
; f
->payload
= 0;
264 if (!trait_at(b
, m
->coord
, stone_other(m
->color
)).cap
)
266 /* Opponent can capture something! */
267 if ((ps
[FEAT_AESCAPE
] & ~(1<<PF_AESCAPE_1STONE
| 1<<PF_AESCAPE_TRAPPED
| 1<<PF_AESCAPE_CONNECTION
)) == 1<<PF_MATCH
) {
268 if (PS_PF(AESCAPE
, 1STONE
))
269 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
;
270 if (PS_PF(AESCAPE
, TRAPPED
))
271 f
->payload
|= (!trait_at(b
, m
->coord
, m
->color
).safe
) << PF_AESCAPE_TRAPPED
;
272 if (PS_PF(AESCAPE
, CONNECTION
))
273 f
->payload
|= (trait_at(b
, m
->coord
, stone_other(m
->color
)).cap
< neighbor_count_at(b
, m
->coord
, m
->color
)) << PF_AESCAPE_CONNECTION
;
277 /* We need to know details, so we still have to go through
281 /* Find if a neighboring group of ours is in atari, AND that we provide
282 * a liberty to connect out. XXX: No connect-and-die check. */
283 /* This is very similar in spirit to board_safe_to_play(). */
284 group_t in_atari
= -1;
286 int extra_libs
= 0, connectable_groups
= 0;
287 bool onestone
= false, multistone
= false;
289 foreach_neighbor(b
, m
->coord
, {
290 if (board_at(b
, c
) != m
->color
) {
291 if (board_at(b
, c
) == S_NONE
)
292 extra_libs
++; // free point
293 else if (board_at(b
, c
) == stone_other(m
->color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1) {
294 extra_libs
+= 2; // capturable enemy group
295 /* XXX: We just consider this move safe
296 * unconditionally. */
300 group_t g
= group_at(b
, c
); assert(g
);
301 if (board_group_info(b
, g
).libs
> 1) {
302 connectable_groups
++;
303 if (board_group_info(b
, g
).libs
> 2) {
304 extra_libs
+= 2; // connected out
306 /* This is a bit tricky; we connect our 2-lib
307 * group to another 2-lib group, which counts
308 * as one liberty, but only if the other lib
309 * is not shared too. */
311 onelib
= board_group_other_lib(b
, g
, c
);
315 extra_libs
--; // take that back
326 if (PS_PF(AESCAPE
, LADDER
))
327 f
->payload
|= is_ladder(b
, m
->coord
, g
, true, true) << PF_AESCAPE_LADDER
;
328 /* TODO: is_ladder() is too conservative in some
329 * very obvious situations, look at complete.gtp. */
331 if (group_is_onestone(b
, g
))
338 if (PS_PF(AESCAPE
, 1STONE
))
339 f
->payload
|= (onestone
&& !multistone
) << PF_AESCAPE_1STONE
;
340 if (PS_PF(AESCAPE
, TRAPPED
))
341 f
->payload
|= (extra_libs
< 2) << PF_AESCAPE_TRAPPED
;
342 if (PS_PF(AESCAPE
, CONNECTION
))
343 f
->payload
|= (connectable_groups
> 0) << PF_AESCAPE_CONNECTION
;
349 static struct feature
*
350 pattern_match_atari(struct pattern_config
*pc
, pattern_spec ps
,
351 struct pattern
*p
, struct feature
*f
,
352 struct board
*b
, struct move
*m
)
354 foreach_neighbor(b
, m
->coord
, {
355 if (board_at(b
, c
) != stone_other(m
->color
))
357 group_t g
= group_at(b
, c
);
358 if (!g
|| board_group_info(b
, g
).libs
!= 2)
362 f
->id
= FEAT_ATARI
; f
->payload
= 0;
364 if (PS_PF(ATARI
, LADDER
)) {
365 /* Opponent will escape by the other lib. */
366 coord_t lib
= board_group_other_lib(b
, g
, m
->coord
);
367 /* TODO: is_ladder() is too conservative in some
368 * very obvious situations, look at complete.gtp. */
369 f
->payload
|= is_ladder(b
, lib
, g
, true, true) << PF_ATARI_LADDER
;
372 if (PS_PF(ATARI
, KO
) && !is_pass(b
->ko
.coord
))
373 f
->payload
|= 1 << PF_ATARI_KO
;
380 #ifndef BOARD_SPATHASH
381 #undef BOARD_SPATHASH_MAXD
382 #define BOARD_SPATHASH_MAXD 1
385 /* Match spatial features that are too distant to be pre-matched
388 pattern_match_spatial_outer(struct pattern_config
*pc
, pattern_spec ps
,
389 struct pattern
*p
, struct feature
*f
,
390 struct board
*b
, struct move
*m
, hash_t h
)
392 /* We record all spatial patterns black-to-play; simply
393 * reverse all colors if we are white-to-play. */
394 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
395 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
396 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
398 for (int d
= BOARD_SPATHASH_MAXD
+ 1; d
<= pc
->spat_max
; d
++) {
399 /* Recompute missing outer circles:
400 * Go through all points in given distance. */
401 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
402 ptcoords_at(x
, y
, m
->coord
, b
, j
);
403 h
^= pthashes
[0][j
][(*bt
)[board_atxy(b
, x
, y
)]];
405 if (d
< pc
->spat_min
)
407 /* Record spatial feature, one per distance. */
408 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
410 f
->id
= FEAT_SPATIAL
;
413 } /* else not found, ignore */
419 pattern_match_spatial(struct pattern_config
*pc
, pattern_spec ps
,
420 struct pattern
*p
, struct feature
*f
,
421 struct board
*b
, struct move
*m
)
423 /* XXX: This is partially duplicated from spatial_from_board(), but
424 * we build a hash instead of spatial record. */
426 assert(pc
->spat_min
> 0);
428 hash_t h
= pthashes
[0][0][S_NONE
];
429 #ifdef BOARD_SPATHASH
430 bool w_to_play
= m
->color
== S_WHITE
;
431 for (int d
= 2; d
<= BOARD_SPATHASH_MAXD
; d
++) {
432 /* Reuse all incrementally matched data. */
433 h
^= b
->spathash
[m
->coord
][d
- 1][w_to_play
];
434 if (d
< pc
->spat_min
)
436 /* Record spatial feature, one per distance. */
437 int sid
= spatial_dict_get(pc
->spat_dict
, d
, h
& spatial_hash_mask
);
439 f
->id
= FEAT_SPATIAL
;
442 } /* else not found, ignore */
445 assert(BOARD_SPATHASH_MAXD
< 2);
447 if (unlikely(pc
->spat_max
> BOARD_SPATHASH_MAXD
))
448 f
= pattern_match_spatial_outer(pc
, ps
, p
, f
, b
, m
, h
);
454 pattern_match(struct pattern_config
*pc
, pattern_spec ps
,
455 struct pattern
*p
, struct board
*b
, struct move
*m
)
458 struct feature
*f
= &p
->f
[0];
460 /* TODO: We should match pretty much all of these features
463 if (is_pass(m
->coord
)) {
465 f
->id
= FEAT_PASS
; f
->payload
= 0;
466 if (PS_PF(PASS
, LASTPASS
))
467 f
->payload
|= (b
->moves
> 0 && is_pass(b
->last_move
.coord
))
474 if (PS_ANY(CAPTURE
)) {
475 f
= pattern_match_capture(pc
, ps
, p
, f
, b
, m
);
478 if (PS_ANY(AESCAPE
)) {
479 f
= pattern_match_aescape(pc
, ps
, p
, f
, b
, m
);
482 if (PS_ANY(SELFATARI
)) {
484 if (PS_PF(SELFATARI
, STUPID
)) {
486 if (!b
->precise_selfatari
)
487 simple
= !trait_at(b
, m
->coord
, m
->color
).safe
;
490 simple
= !board_safe_to_play(b
, m
->coord
, m
->color
);
492 bool thorough
= false;
493 if (PS_PF(SELFATARI
, SMART
)) {
495 if (b
->precise_selfatari
)
496 thorough
= !trait_at(b
, m
->coord
, m
->color
).safe
;
499 thorough
= is_bad_selfatari(b
, m
->color
, m
->coord
);
501 if (simple
|| thorough
) {
502 f
->id
= FEAT_SELFATARI
;
503 f
->payload
= simple
<< PF_SELFATARI_STUPID
;
504 f
->payload
|= thorough
<< PF_SELFATARI_SMART
;
510 f
= pattern_match_atari(pc
, ps
, p
, f
, b
, m
);
513 if (PS_ANY(BORDER
)) {
514 int bdist
= coord_edge_distance(m
->coord
, b
);
515 if (bdist
<= pc
->bdist_max
) {
522 if (PS_ANY(CONTIGUITY
) && !is_pass(b
->last_move
.coord
)
523 && coord_is_8adjecent(m
->coord
, b
->last_move
.coord
, b
)) {
524 f
->id
= FEAT_CONTIGUITY
;
529 if (PS_ANY(LDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move
.coord
)) {
530 int ldist
= coord_gridcular_distance(m
->coord
, b
->last_move
.coord
, b
);
531 if (pc
->ldist_min
<= ldist
&& ldist
<= pc
->ldist_max
) {
538 if (PS_ANY(LLDIST
) && pc
->ldist_max
> 0 && !is_pass(b
->last_move2
.coord
)) {
539 int lldist
= coord_gridcular_distance(m
->coord
, b
->last_move2
.coord
, b
);
540 if (pc
->ldist_min
<= lldist
&& lldist
<= pc
->ldist_max
) {
547 if (PS_ANY(SPATIAL
) && pc
->spat_max
> 0 && pc
->spat_dict
) {
548 f
= pattern_match_spatial(pc
, ps
, p
, f
, b
, m
);
551 if (PS_ANY(PATTERN3
) && !is_pass(m
->coord
)) {
553 hash3_t pat
= b
->pat3
[m
->coord
];
555 hash3_t pat
= pattern3_hash(b
, m
->coord
);
557 if (m
->color
== S_WHITE
) {
558 /* We work with the pattern3s as black-to-play. */
559 pat
= pattern3_reverse(pat
);
561 f
->id
= FEAT_PATTERN3
;
566 /* FEAT_MCOWNER: TODO */
571 pattern2str(char *str
, struct pattern
*p
)
573 str
= stpcpy(str
, "(");
574 for (int i
= 0; i
< p
->n
; i
++) {
575 if (i
> 0) str
= stpcpy(str
, " ");
576 str
= feature2str(str
, &p
->f
[i
]);
578 str
= stpcpy(str
, ")");
584 /*** Features gamma set */
587 features_gamma_load(struct features_gamma
*fg
, const char *filename
)
589 FILE *f
= fopen(filename
, "r");
592 while (fgets(buf
, 256, f
)) {
595 bufp
= str2feature(bufp
, &f
);
596 while (isspace(*bufp
)) bufp
++;
597 double gamma
= strtod(bufp
, &bufp
);
598 /* Record feature's gamma. */
599 feature_gamma(fg
, &f
, &gamma
);
600 /* In case of 3x3 patterns, record gamma also
601 * for all rotations and transpositions. */
602 if (f
.id
== FEAT_PATTERN3
) {
604 pattern3_transpose(f
.payload
, &transp
);
605 for (int i
= 1; i
< 8; i
++) {
606 f
.payload
= transp
[i
];
607 feature_gamma(fg
, &f
, &gamma
);
609 f
.payload
= transp
[0];
615 const char *features_gamma_filename
= "patterns.gamma";
617 struct features_gamma
*
618 features_gamma_init(struct pattern_config
*pc
, const char *file
)
620 struct features_gamma
*fg
= calloc2(1, sizeof(*fg
));
622 for (int i
= 0; i
< FEAT_MAX
; i
++) {
623 int n
= feature_payloads(pc
, i
);
624 fg
->gamma
[i
] = malloc2(n
* sizeof(fg
->gamma
[0][0]));
625 for (int j
= 0; j
< n
; j
++) {
626 fg
->gamma
[i
][j
] = 1.0f
;
629 features_gamma_load(fg
, file
? file
: features_gamma_filename
);
634 features_gamma_done(struct features_gamma
*fg
)
636 for (int i
= 0; i
< FEAT_MAX
; i
++)