slave_loop(): Add function send_command() to clean the code.
[pachi/derm.git] / pattern.h
blob68ba1616b718f4d42bac876b88f40208f02aa724
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 FEAT_CAPTURE,
51 /* Atari escape (extension). */
52 /* Payload: [bit0] Escaping with laddered group? */
53 #define PF_AESCAPE_LADDER 0
54 FEAT_AESCAPE,
56 /* Self-atari move. */
57 /* Payload: [bit0] Matched by trivial definition? */
58 /* [bit1] Matched by complex definition? (tries to be aware of nakade, throwins, ...) */
59 #define PF_SELFATARI_STUPID 0
60 #define PF_SELFATARI_SMART 1
61 FEAT_SELFATARI,
63 /* Atari move. */
64 /* Payload: [bit0] The atari'd group gets laddered? */
65 #define PF_ATARI_LADDER 0
66 /* [bit1] Playing ko? */
67 #define PF_ATARI_KO 1
68 FEAT_ATARI,
70 /* Border distance. */
71 /* Payload: The distance - "line number". Only up to 4. */
72 FEAT_BORDER,
74 /* Last move distance. */
75 /* Payload: The distance - gridcular metric. */
76 FEAT_LDIST,
78 /* Next-to-last move distance. */
79 /* Payload: The distance - gridcular metric. */
80 FEAT_LLDIST,
82 /* Continuity. */
83 /* Payload: [bit0] The move is in 8-neighborhood of last move (ldist<=3) */
84 /* This is a fast substitution to ldist/lldist. */
85 FEAT_CONTIGUITY,
87 /* Spatial configuration of stones in certain board area,
88 * with black to play. */
89 /* Payload: Index in the spatial_dict. */
90 FEAT_SPATIAL,
92 /* Spatial configuration of stones in fixed 3x3 square,
93 * with black to play. */
94 /* This is a fast substitution to spatial. */
95 /* Payload: Pattern3 hash (see pattern3.h). */
96 /* Note that the hash describes only one particular rotation;
97 * no normalization across rotations and transpositions is done
98 * during the matching, only color normalization. The patternscan
99 * and gamma machineries is taking care of the rotations. */
100 FEAT_PATTERN3,
103 /* Unimplemented - TODO: */
105 /* Monte-carlo owner. */
106 /* Payload: #of playouts owning this point at the final
107 * position, scaled to 0..15 (lowest 4 bits). */
108 FEAT_MCOWNER,
110 FEAT_MAX
113 struct feature {
114 enum feature_id id;
115 uint16_t payload;
118 struct pattern {
119 /* Pattern (matched) is set of features. */
120 int n;
121 #define FEATURES 32
122 struct feature f[FEATURES];
125 struct spatial_dict;
126 struct pattern_config {
127 /* FEAT_SPATIAL: Generate patterns only for these sizes (gridcular). */
128 int spat_min, spat_max;
129 /* FEAT_BORDER: Generate features only up to this board distance. */
130 int bdist_max;
131 /* FEAT_LDIST, FEAT_LLDIST: Generate features only for these move
132 * distances. */
133 int ldist_min, ldist_max;
134 /* FEAT_MCOWNER: Generate feature after this number of simulations. */
135 int mcsims;
137 /* The spatial patterns dictionary, used by FEAT_SPATIAL. */
138 struct spatial_dict *spat_dict;
140 extern struct pattern_config DEFAULT_PATTERN_CONFIG;
141 extern struct pattern_config FAST_PATTERN_CONFIG;
143 /* The pattern_spec[] specifies which features to tests for;
144 * highest bit controls whether to test for the feature at all,
145 * then for bitmap features (except FEAT_SPATIAL) the rest
146 * of the bits controls various PF tests; for non-bitmap
147 * features, you will need to tweak the patternconfig to
148 * fine-tune them. */
149 typedef uint16_t pattern_spec[FEAT_MAX];
150 /* Match all supported features. */
151 extern pattern_spec PATTERN_SPEC_MATCHALL;
152 /* Match only "quick" features, suitable for MC simulations. */
153 extern pattern_spec PATTERN_SPEC_MATCHFAST;
156 /* Append feature to string. */
157 char *feature2str(char *str, struct feature *f);
158 /* Convert string to feature, return pointer after the featurespec. */
159 char *str2feature(char *str, struct feature *f);
160 /* Get name of given feature. */
161 char *feature_name(enum feature_id f);
162 /* Get number of possible payload values associated with the feature. */
163 int feature_payloads(struct pattern_config *pc, enum feature_id f);
165 /* Append pattern as feature spec string. */
166 char *pattern2str(char *str, struct pattern *p);
168 /* Initialize p and fill it with features matched by the
169 * given board move. */
170 void pattern_match(struct pattern_config *pc, pattern_spec ps, struct pattern *p, struct board *b, struct move *m);
173 /* Comparative strengths of all feature-payload pairs (initialized to 1 for
174 * unspecified pairs). */
175 struct features_gamma {
176 /* Indexed by feature and payload; each feature array is allocated for
177 * all possible payloads to fit in. */
178 double *gamma[FEAT_MAX];
179 struct pattern_config *pc;
181 /* Default gamma filename to use. */
182 extern const char *features_gamma_filename;
184 /* Initializes gamma values, pre-loading existing records from given file
185 * (NULL for default), falling back to gamma==1 for unspecified values. */
186 struct features_gamma *features_gamma_init(struct pattern_config *pc, const char *file);
188 /* Look up gamma of given feature, or set one if gamma is not NULL. */
189 static double feature_gamma(struct features_gamma *fg, struct feature *f, double *gamma);
191 /* Destroy the structure. */
192 void features_gamma_done(struct features_gamma *fg);
195 static inline double
196 feature_gamma(struct features_gamma *fg, struct feature *f, double *gamma)
198 if (gamma) fg->gamma[f->id][f->payload] = *gamma;
199 return fg->gamma[f->id][f->payload];
202 #endif