4 /* Matching of multi-featured patterns. */
9 /* When someone says "pattern", you imagine a configuration of stones in given
10 * area (e.g. as matched very efficiently by pattern3 in case of 3x3 area).
11 * However, we use a richer definition of pattern, where this is merely one
12 * pattern _feature_. Another features may be is-a-selfatari, is-a-capture,
13 * number of liberties, distance from last move, etc. */
15 /* ! NOTE NOTE NOTE ! We provide infrastructure for matching patterns, but we
16 * also replicate the most bare-bone part of it for tiny subset of features
17 * in board.c:board_gamma_update() for fast incremental probability
18 * distribution maintenance. Aside of using the constants defined here, that
19 * implementation is completely independent and does not call back here. */
21 /* Each feature is represented by its id and an optional 32-bit payload;
22 * when matching, discrete (id,payload) pairs are considered. */
24 /* This is heavily influenced by (Coulom, 2007), of course. */
25 /* TODO: Try completely separate ko / no-ko features. */
27 /* See the HACKING file for another description of the pattern matcher and
28 * instructions on how to harvest and inspect patterns. */
30 /* If you add a payload bit for a feature, don't forget to update the value
36 /* Payload: [bit0] Last move was also pass? */
37 #define PF_PASS_LASTPASS 0
40 /* Simple capture move. */
41 /* Payload: [bit0] Capturing laddered group? */
42 #define PF_CAPTURE_LADDER 0
43 /* [bit1] Re-capturing last move? */
44 #define PF_CAPTURE_RECAPTURE 1 /* TODO */
45 /* [bit2] Enables our atari group get more libs? */
46 #define PF_CAPTURE_ATARIDEF 2
47 /* [bit3] Capturing ko? */
48 #define PF_CAPTURE_KO 3
49 /* [bit4] Single-stone group? */
50 #define PF_CAPTURE_1STONE 4
51 /* [bit5] Unsafe move for opponent? */
52 #define PF_CAPTURE_TRAPPED 5
55 /* Atari escape (extension). */
56 /* Payload: [bit0] Escaping with laddered group? */
57 #define PF_AESCAPE_LADDER 0
58 /* [bit1] Single-stone group? */
59 #define PF_AESCAPE_1STONE 1
60 /* [bit2] Unsafe move for us? */
61 #define PF_AESCAPE_TRAPPED 2
64 /* Self-atari move. */
65 /* Payload: [bit0] Matched by trivial definition? */
66 /* [bit1] Matched by complex definition? (tries to be aware of nakade, throwins, ...) */
67 #define PF_SELFATARI_STUPID 0
68 #define PF_SELFATARI_SMART 1
72 /* Payload: [bit0] The atari'd group gets laddered? */
73 #define PF_ATARI_LADDER 0
74 /* [bit1] Playing ko? */
78 /* Border distance. */
79 /* Payload: The distance - "line number". Only up to 4. */
82 /* Last move distance. */
83 /* Payload: The distance - gridcular metric. */
86 /* Next-to-last move distance. */
87 /* Payload: The distance - gridcular metric. */
91 /* Payload: [bit0] The move is in 8-neighborhood of last move (ldist<=3) */
92 /* This is a fast substitution to ldist/lldist. */
95 /* Spatial configuration of stones in certain board area,
96 * with black to play. */
97 /* Payload: Index in the spatial_dict. */
100 /* Spatial configuration of stones in fixed 3x3 square,
101 * with black to play. */
102 /* This is a fast substitution to spatial. */
103 /* Payload: Pattern3 hash (see pattern3.h). */
104 /* Note that the hash describes only one particular rotation;
105 * no normalization across rotations and transpositions is done
106 * during the matching, only color normalization. The patternscan
107 * and gamma machineries is taking care of the rotations. */
111 /* Unimplemented - TODO: */
113 /* Monte-carlo owner. */
114 /* Payload: #of playouts owning this point at the final
115 * position, scaled to 0..15 (lowest 4 bits). */
127 /* Pattern (matched) is set of features. */
130 struct feature f
[FEATURES
];
134 struct pattern_config
{
135 /* FEAT_SPATIAL: Generate patterns only for these sizes (gridcular). */
136 int spat_min
, spat_max
;
137 /* FEAT_BORDER: Generate features only up to this board distance. */
139 /* FEAT_LDIST, FEAT_LLDIST: Generate features only for these move
141 int ldist_min
, ldist_max
;
142 /* FEAT_MCOWNER: Generate feature after this number of simulations. */
145 /* The spatial patterns dictionary, used by FEAT_SPATIAL. */
146 struct spatial_dict
*spat_dict
;
148 extern struct pattern_config DEFAULT_PATTERN_CONFIG
;
149 extern struct pattern_config FAST_PATTERN_CONFIG
;
151 /* The pattern_spec[] specifies which features to tests for;
152 * highest bit controls whether to test for the feature at all,
153 * then for bitmap features (except FEAT_SPATIAL) the rest
154 * of the bits controls various PF tests; for non-bitmap
155 * features, you will need to tweak the patternconfig to
157 typedef uint16_t pattern_spec
[FEAT_MAX
];
158 /* Match all supported features. */
159 extern pattern_spec PATTERN_SPEC_MATCHALL
;
160 /* Match only "quick" features, suitable for MC simulations. */
161 extern pattern_spec PATTERN_SPEC_MATCHFAST
;
164 /* Append feature to string. */
165 char *feature2str(char *str
, struct feature
*f
);
166 /* Convert string to feature, return pointer after the featurespec. */
167 char *str2feature(char *str
, struct feature
*f
);
168 /* Get name of given feature. */
169 char *feature_name(enum feature_id f
);
170 /* Get number of possible payload values associated with the feature. */
171 int feature_payloads(struct pattern_config
*pc
, enum feature_id f
);
173 /* Append pattern as feature spec string. */
174 char *pattern2str(char *str
, struct pattern
*p
);
176 /* Initialize p and fill it with features matched by the
177 * given board move. */
178 void pattern_match(struct pattern_config
*pc
, pattern_spec ps
, struct pattern
*p
, struct board
*b
, struct move
*m
);
181 /* Comparative strengths of all feature-payload pairs (initialized to 1 for
182 * unspecified pairs). */
183 struct features_gamma
{
184 /* Indexed by feature and payload; each feature array is allocated for
185 * all possible payloads to fit in. */
186 double *gamma
[FEAT_MAX
];
187 struct pattern_config
*pc
;
189 /* Default gamma filename to use. */
190 extern const char *features_gamma_filename
;
192 /* Initializes gamma values, pre-loading existing records from given file
193 * (NULL for default), falling back to gamma==1 for unspecified values. */
194 struct features_gamma
*features_gamma_init(struct pattern_config
*pc
, const char *file
);
196 /* Look up gamma of given feature, or set one if gamma is not NULL. */
197 static double feature_gamma(struct features_gamma
*fg
, struct feature
*f
, double *gamma
);
199 /* Destroy the structure. */
200 void features_gamma_done(struct features_gamma
*fg
);
204 feature_gamma(struct features_gamma
*fg
, struct feature
*f
, double *gamma
)
206 if (gamma
) fg
->gamma
[f
->id
][f
->payload
] = *gamma
;
207 return fg
->gamma
[f
->id
][f
->payload
];