Do not prevent node expansion later when more memory becomes available.
[pachi/derm.git] / pattern.h
blob585dc2bcd3ee0866646cbc86dd22c96b84a9ccb5
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: [other bits] Index in the spatial_dict. */
82 FEAT_SPATIAL,
85 /* Unimplemented - TODO: */
87 /* Monte-carlo owner. */
88 /* Payload: #of playouts owning this point at the final
89 * position, scaled to 0..15 (lowest 4 bits). */
90 FEAT_MCOWNER,
92 FEAT_MAX
95 struct feature {
96 enum feature_id id;
97 uint16_t payload;
100 struct pattern {
101 /* Pattern (matched) is set of features. */
102 int n;
103 #define FEATURES 32
104 struct feature f[FEATURES];
107 struct spatial_dict;
108 struct pattern_config {
109 /* FEAT_SPATIAL: Generate patterns only for these sizes (gridcular). */
110 int spat_min, spat_max;
111 /* FEAT_BORDER: Generate features only up to this board distance. */
112 int bdist_max;
113 /* FEAT_LDIST, FEAT_LLDIST: Generate features only for these move
114 * distances. */
115 int ldist_min, ldist_max;
116 /* FEAT_MCOWNER: Generate feature after this number of simulations. */
117 int mcsims;
119 /* The spatial patterns dictionary, used by FEAT_SPATIAL. */
120 struct spatial_dict *spat_dict;
122 extern struct pattern_config DEFAULT_PATTERN_CONFIG;
123 extern struct pattern_config FAST_PATTERN_CONFIG;
125 /* The pattern_spec[] specifies which features to tests for;
126 * highest bit controls whether to test for the feature at all,
127 * then for bitmap features (except FEAT_SPATIAL) the rest
128 * of the bits controls various PF tests; for non-bitmap
129 * features, you will need to tweak the patternconfig to
130 * fine-tune them. */
131 typedef uint16_t pattern_spec[FEAT_MAX];
132 /* Match all supported features. */
133 extern pattern_spec PATTERN_SPEC_MATCHALL;
134 /* Match only "quick" features, suitable for MC simulations. */
135 extern pattern_spec PATTERN_SPEC_MATCHFAST;
138 /* Append feature to string. */
139 char *feature2str(char *str, struct feature *f);
140 /* Convert string to feature, return pointer after the featurespec. */
141 char *str2feature(char *str, struct feature *f);
142 /* Get name of given feature. */
143 char *feature_name(enum feature_id f);
144 /* Get number of possible payload values associated with the feature. */
145 int feature_payloads(struct pattern_config *pc, enum feature_id f);
147 /* Append pattern as feature spec string. */
148 char *pattern2str(char *str, struct pattern *p);
150 /* Initialize p and fill it with features matched by the
151 * given board move. */
152 void pattern_match(struct pattern_config *pc, pattern_spec ps, struct pattern *p, struct board *b, struct move *m);
155 /* Comparative strengths of all feature-payload pairs (initialized to 1 for
156 * unspecified pairs). */
157 struct features_gamma {
158 /* Indexed by feature and payload; each feature array is allocated for
159 * all possible payloads to fit in. */
160 float *gamma[FEAT_MAX];
161 struct pattern_config *pc;
163 /* Default gamma filename to use. */
164 extern const char *features_gamma_filename;
166 /* Initializes gamma values, pre-loading existing records from given file
167 * (NULL for default), falling back to gamma==1 for unspecified values. */
168 struct features_gamma *features_gamma_init(struct pattern_config *pc, const char *file);
170 /* Look up gamma of given feature, or set one if gamma is not NULL. */
171 static float feature_gamma(struct features_gamma *fg, struct feature *f, float *gamma);
173 /* Destroy the structure. */
174 void features_gamma_done(struct features_gamma *fg);
177 static inline float
178 feature_gamma(struct features_gamma *fg, struct feature *f, float *gamma)
180 if (gamma) fg->gamma[f->id][f->payload] = *gamma;
181 return fg->gamma[f->id][f->payload];
184 #endif