board_group_other_lib(): Introduce nifty helper
[pachi/json.git] / pattern.h
blob5b1e5edc0dd75e8d2cd94fe26bba370cfdc929dd
1 #ifndef ZZGO_PATTERN_H
2 #define ZZGO_PATTERN_H
4 /* Matching of multi-featured patterns. */
6 #include "board.h"
7 #include "move.h"
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
31 * in feature_info. */
32 enum feature_id {
33 /* Implemented: */
35 /* This is a pass. */
36 /* Payload: [bit0] Last move was also pass? */
37 #define PF_PASS_LASTPASS 0
38 FEAT_PASS,
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
53 FEAT_CAPTURE,
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
62 FEAT_AESCAPE,
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
69 FEAT_SELFATARI,
71 /* Atari move. */
72 /* Payload: [bit0] The atari'd group gets laddered? */
73 #define PF_ATARI_LADDER 0
74 /* [bit1] Playing ko? */
75 #define PF_ATARI_KO 1
76 FEAT_ATARI,
78 /* Border distance. */
79 /* Payload: The distance - "line number". Only up to 4. */
80 FEAT_BORDER,
82 /* Last move distance. */
83 /* Payload: The distance - gridcular metric. */
84 FEAT_LDIST,
86 /* Next-to-last move distance. */
87 /* Payload: The distance - gridcular metric. */
88 FEAT_LLDIST,
90 /* Continuity. */
91 /* Payload: [bit0] The move is in 8-neighborhood of last move (ldist<=3) */
92 /* This is a fast substitution to ldist/lldist. */
93 FEAT_CONTIGUITY,
95 /* Spatial configuration of stones in certain board area,
96 * with black to play. */
97 /* Payload: Index in the spatial_dict. */
98 FEAT_SPATIAL,
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. */
108 FEAT_PATTERN3,
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). */
116 FEAT_MCOWNER,
118 FEAT_MAX
121 struct feature {
122 enum feature_id id;
123 uint16_t payload;
126 struct pattern {
127 /* Pattern (matched) is set of features. */
128 int n;
129 #define FEATURES 32
130 struct feature f[FEATURES];
133 struct spatial_dict;
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. */
138 int bdist_max;
139 /* FEAT_LDIST, FEAT_LLDIST: Generate features only for these move
140 * distances. */
141 int ldist_min, ldist_max;
142 /* FEAT_MCOWNER: Generate feature after this number of simulations. */
143 int mcsims;
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
156 * fine-tune them. */
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);
203 static inline double
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];
210 #endif