Merge branch 'master' of git+ssh://repo.or.cz/srv/git/pachi
[pachi/json.git] / pattern.h
bloba019e9ba6694f9bc6042b4d7b7f58276f9815957
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 /* Spatial configuration of stones in certain board area,
75 * with black to play. */
76 /* Payload: [bits 31-24] Pattern radius (gridcular) */
77 #define PF_SPATIAL_RADIUS 24
78 /* [other bits] Index in the spatial_dict. */
79 #define PF_SPATIAL_INDEX 0
80 FEAT_SPATIAL,
83 /* Unimplemented - TODO: */
85 /* Monte-carlo owner. */
86 /* Payload: #of playouts owning this point at the final
87 * position, scaled to 0..15 (lowest 4 bits). */
88 FEAT_MCOWNER,
90 FEAT_MAX
93 struct feature {
94 enum feature_id id;
95 uint32_t payload;
98 struct pattern {
99 /* Pattern (matched) is set of features. */
100 int n;
101 #define FEATURES 32
102 struct feature f[FEATURES];
105 struct spatial_dict;
106 struct pattern_config {
107 /* FEAT_SPATIAL: Generate patterns only for these sizes (gridcular). */
108 int spat_min, spat_max;
109 /* FEAT_BORDER: Generate features only up to this board distance. */
110 int bdist_max;
111 /* FEAT_LDIST, FEAT_LLDIST: Generate features only for these move
112 * distances. */
113 int ldist_min, ldist_max;
114 /* FEAT_MCOWNER: Generate feature after this number of simulations. */
115 int mcsims;
117 /* The spatial patterns dictionary, used by FEAT_SPATIAL. */
118 struct spatial_dict *spat_dict;
120 extern struct pattern_config DEFAULT_PATTERN_CONFIG;
121 extern struct pattern_config FAST_PATTERN_CONFIG;
123 /* The pattern_spec[] specifies which features to tests for;
124 * highest bit controls whether to test for the feature at all,
125 * then for bitmap features (except FEAT_SPATIAL) the rest
126 * of the bits controls various PF tests; for non-bitmap
127 * features, you will need to tweak the patternconfig to
128 * fine-tune them. */
129 typedef uint32_t pattern_spec[FEAT_MAX];
130 /* Match all supported features. */
131 extern pattern_spec PATTERN_SPEC_MATCHALL;
132 /* Match only "quick" features, suitable for MC simulations. */
133 extern pattern_spec PATTERN_SPEC_MATCHFAST;
136 /* Append feature to string. */
137 char *feature2str(char *str, struct feature *f);
138 /* Convert string to feature, return pointer after the featurespec. */
139 char *str2feature(char *str, struct feature *f);
141 /* Append pattern as feature spec string. */
142 char *pattern2str(char *str, struct pattern *p);
144 /* Initialize p and fill it with features matched by the
145 * given board move. */
146 void pattern_match(struct pattern_config *pc, pattern_spec ps, struct pattern *p, struct board *b, struct move *m);
149 /* Comparative strengths of all feature-payload pairs (initialized to 1 for
150 * unspecified pairs). */
151 struct features_gamma {
152 /* Indexed by feature and payload; each feature array is allocated for
153 * all possible payloads to fit in. */
154 float *gamma[FEAT_MAX];
155 struct pattern_config *pc;
157 /* Default gamma filename to use. */
158 extern const char *features_gamma_filename;
160 /* Initializes gamma values, pre-loading existing records from given file
161 * (NULL for default), falling back to gamma==1 for unspecified values. */
162 struct features_gamma *features_gamma_init(struct pattern_config *pc, const char *file);
164 /* Look up gamma of given feature, or set one if gamma is not NULL. */
165 float feature_gamma(struct features_gamma *fg, struct feature *f, float *gamma);
167 #endif