uct_search() result cannot change: Check against worst.time instead of desired.time
[pachi.git] / pattern.h
blob7ad2d4d3df3012ba77027a2d0fbe3126209f0d1b
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 /* Each feature is represented by its id and an optional 32-bit payload;
16 * when matching, discrete (id,payload) pairs are considered. */
18 /* This is heavily influenced by (Coulom, 2007), of course. */
19 /* TODO: Try completely separate ko / no-ko features. */
21 /* See the HACKING file for another description of the pattern matcher and
22 * instructions on how to harvest and inspect patterns. */
24 /* If you add a payload bit for a feature, don't forget to update the value
25 * in feature_info. */
26 enum feature_id {
27 /* Implemented: */
29 /* This is a pass. */
30 /* Payload: [bit0] Last move was also pass? */
31 #define PF_PASS_LASTPASS 0
32 FEAT_PASS,
34 /* Simple capture move. */
35 /* Payload: [bit0] Capturing laddered group? */
36 #define PF_CAPTURE_LADDER 0
37 /* [bit1] Re-capturing last move? */
38 #define PF_CAPTURE_RECAPTURE 1 /* TODO */
39 /* [bit2] Enables our atari group get more libs? */
40 #define PF_CAPTURE_ATARIDEF 2
41 /* [bit3] Capturing ko? */
42 #define PF_CAPTURE_KO 3
43 FEAT_CAPTURE,
45 /* Atari escape (extension). */
46 /* Payload: [bit0] Escaping with laddered group? */
47 #define PF_AESCAPE_LADDER 0
48 FEAT_AESCAPE,
50 /* Self-atari move. */
51 /* Payload: [bit0] Also using our complex definition? */
52 #define PF_SELFATARI_SMART 0
53 FEAT_SELFATARI,
55 /* Atari move. */
56 /* Payload: [bit0] The atari'd group gets laddered? */
57 #define PF_ATARI_LADDER 0
58 /* [bit1] Playing ko? */
59 #define PF_ATARI_KO 1
60 FEAT_ATARI,
62 /* Border distance. */
63 /* Payload: The distance - "line number". Only up to 4. */
64 FEAT_BORDER,
66 /* Last move distance. */
67 /* Payload: The distance - gridcular metric. */
68 FEAT_LDIST,
70 /* Next-to-last move distance. */
71 /* Payload: The distance - gridcular metric. */
72 FEAT_LLDIST,
74 /* Continuity. */
75 /* Payload: [bit0] The move is in 8-neighborhood of last move (ldist<=3) */
76 /* This is a fast substitution to ldist/lldist. */
77 FEAT_CONTIGUITY,
79 /* Spatial configuration of stones in certain board area,
80 * with black to play. */
81 /* Payload: Index in the spatial_dict. */
82 FEAT_SPATIAL,
84 /* Spatial configuration of stones in fixed 3x3 square,
85 * with black to play. */
86 /* This is a fast substitution to spatial. */
87 /* Payload: Pattern3 hash (see pattern3.h). */
88 /* Note that the hash describes only one particular rotation;
89 * no normalization across rotations and transpositions is done
90 * during the matching, only color normalization. The patternscan
91 * and gamma machineries is taking care of the rotations. */
92 FEAT_PATTERN3,
95 /* Unimplemented - TODO: */
97 /* Monte-carlo owner. */
98 /* Payload: #of playouts owning this point at the final
99 * position, scaled to 0..15 (lowest 4 bits). */
100 FEAT_MCOWNER,
102 FEAT_MAX
105 struct feature {
106 enum feature_id id;
107 uint16_t payload;
110 struct pattern {
111 /* Pattern (matched) is set of features. */
112 int n;
113 #define FEATURES 32
114 struct feature f[FEATURES];
117 struct spatial_dict;
118 struct pattern_config {
119 /* FEAT_SPATIAL: Generate patterns only for these sizes (gridcular). */
120 int spat_min, spat_max;
121 /* FEAT_BORDER: Generate features only up to this board distance. */
122 int bdist_max;
123 /* FEAT_LDIST, FEAT_LLDIST: Generate features only for these move
124 * distances. */
125 int ldist_min, ldist_max;
126 /* FEAT_MCOWNER: Generate feature after this number of simulations. */
127 int mcsims;
129 /* The spatial patterns dictionary, used by FEAT_SPATIAL. */
130 struct spatial_dict *spat_dict;
132 extern struct pattern_config DEFAULT_PATTERN_CONFIG;
133 extern struct pattern_config FAST_PATTERN_CONFIG;
135 /* The pattern_spec[] specifies which features to tests for;
136 * highest bit controls whether to test for the feature at all,
137 * then for bitmap features (except FEAT_SPATIAL) the rest
138 * of the bits controls various PF tests; for non-bitmap
139 * features, you will need to tweak the patternconfig to
140 * fine-tune them. */
141 typedef uint16_t pattern_spec[FEAT_MAX];
142 /* Match all supported features. */
143 extern pattern_spec PATTERN_SPEC_MATCHALL;
144 /* Match only "quick" features, suitable for MC simulations. */
145 extern pattern_spec PATTERN_SPEC_MATCHFAST;
148 /* Append feature to string. */
149 char *feature2str(char *str, struct feature *f);
150 /* Convert string to feature, return pointer after the featurespec. */
151 char *str2feature(char *str, struct feature *f);
152 /* Get name of given feature. */
153 char *feature_name(enum feature_id f);
154 /* Get number of possible payload values associated with the feature. */
155 int feature_payloads(struct pattern_config *pc, enum feature_id f);
157 /* Append pattern as feature spec string. */
158 char *pattern2str(char *str, struct pattern *p);
160 /* Initialize p and fill it with features matched by the
161 * given board move. */
162 void pattern_match(struct pattern_config *pc, pattern_spec ps, struct pattern *p, struct board *b, struct move *m);
165 /* Comparative strengths of all feature-payload pairs (initialized to 1 for
166 * unspecified pairs). */
167 struct features_gamma {
168 /* Indexed by feature and payload; each feature array is allocated for
169 * all possible payloads to fit in. */
170 float *gamma[FEAT_MAX];
171 struct pattern_config *pc;
173 /* Default gamma filename to use. */
174 extern const char *features_gamma_filename;
176 /* Initializes gamma values, pre-loading existing records from given file
177 * (NULL for default), falling back to gamma==1 for unspecified values. */
178 struct features_gamma *features_gamma_init(struct pattern_config *pc, const char *file);
180 /* Look up gamma of given feature, or set one if gamma is not NULL. */
181 static float feature_gamma(struct features_gamma *fg, struct feature *f, float *gamma);
183 /* Destroy the structure. */
184 void features_gamma_done(struct features_gamma *fg);
187 static inline float
188 feature_gamma(struct features_gamma *fg, struct feature *f, float *gamma)
190 if (gamma) fg->gamma[f->id][f->payload] = *gamma;
191 return fg->gamma[f->id][f->payload];
194 #endif