Regenerate patterns spatial dictionary and gamma files
[pachi/json.git] / patternsp.c
bloba86934799f3972dfabb4ca5ef72aaa9de72e6658
1 #define DEBUG
2 #include <assert.h>
3 #include <ctype.h>
4 #include <inttypes.h>
5 #include <stdio.h>
6 #include <stdlib.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "pattern.h"
11 #include "patternsp.h"
12 #include "tactics.h"
15 /* Mapping from point sequence to coordinate offsets (to determine
16 * coordinates relative to pattern center). The array is ordered
17 * in the gridcular metric order so that we can go through it
18 * and incrementally match spatial features in nested circles.
19 * Within one circle, coordinates are ordered by rows to keep
20 * good cache behavior. */
21 struct ptcoord ptcoords[MAX_PATTERN_AREA];
23 /* For each radius, starting index in ptcoords[]. */
24 int ptind[MAX_PATTERN_DIST + 2];
26 /* ptcoords[], ptind[] setup */
27 static void __attribute__((constructor(140)))
28 ptcoords_init(void)
30 int i = 0; /* Indexing ptcoords[] */
32 /* First, center point. */
33 ptind[0] = ptind[1] = 0;
34 ptcoords[i].x = ptcoords[i].y = 0; i++;
36 for (int d = 2; d <= MAX_PATTERN_DIST; d++) {
37 ptind[d] = i;
38 /* For each y, examine all integer solutions
39 * of d = |x| + |y| + max(|x|, |y|). */
40 /* TODO: (Stern, 2006) uses a hand-modified
41 * circles that are finer for small d. */
42 for (short y = d / 2; y >= 0; y--) {
43 short x;
44 if (y > d / 3) {
45 /* max(|x|, |y|) = |y|, non-zero x */
46 x = d - y * 2;
47 if (x + y * 2 != d) continue;
48 } else {
49 /* max(|x|, |y|) = |x| */
50 /* Or, max(|x|, |y|) = |y| and x is zero */
51 x = (d - y) / 2;
52 if (x * 2 + y != d) continue;
55 assert((x > y ? x : y) + x + y == d);
57 ptcoords[i].x = x; ptcoords[i].y = y; i++;
58 if (x != 0) { ptcoords[i].x = -x; ptcoords[i].y = y; i++; }
59 if (y != 0) { ptcoords[i].x = x; ptcoords[i].y = -y; i++; }
60 if (x != 0 && y != 0) { ptcoords[i].x = -x; ptcoords[i].y = -y; i++; }
63 ptind[MAX_PATTERN_DIST + 1] = i;
65 #if 0
66 for (int d = 0; d <= MAX_PATTERN_DIST; d++) {
67 fprintf(stderr, "d=%d (%d) ", d, ptind[d]);
68 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
69 fprintf(stderr, "%d,%d ", ptcoords[j].x, ptcoords[j].y);
71 fprintf(stderr, "\n");
73 #endif
77 /* Zobrist hashes used for points in patterns. */
78 hash_t pthashes[PTH__ROTATIONS][MAX_PATTERN_AREA][S_MAX];
80 static void __attribute__((constructor(160)))
81 pthashes_init(void)
83 /* We need fixed hashes for all pattern-relative in
84 * all pattern users! This is a simple way to generate
85 * hopefully good ones. Park-Miller powa. :) */
87 /* We create a virtual board (centered at the sequence start),
88 * plant the hashes there, then pick them up into the sequence
89 * with correct coordinates. It would be possible to generate
90 * the sequence point hashes directly, but the rotations would
91 * make for enormous headaches. */
92 hash_t pthboard[MAX_PATTERN_AREA][4];
93 int pthbc = MAX_PATTERN_AREA / 2; // tengen coord
95 /* The magic numbers are tuned for minimal collisions. */
96 hash_t h = 0x313131;
97 for (int i = 0; i < MAX_PATTERN_AREA; i++) {
98 pthboard[i][S_NONE] = (h = h * 16803 - 7);
99 pthboard[i][S_BLACK] = (h = h * 16805 + 7);
100 pthboard[i][S_WHITE] = (h = h * 16807 + 3);
101 pthboard[i][S_OFFBOARD] = (h = h * 16809 - 3);
104 /* Virtual board with hashes created, now fill
105 * pthashes[] with hashes for points in actual
106 * sequences, also considering various rotations. */
107 #define PTH_VMIRROR 1
108 #define PTH_HMIRROR 2
109 #define PTH_90ROT 4
110 for (int r = 0; r < PTH__ROTATIONS; r++) {
111 for (int i = 0; i < MAX_PATTERN_AREA; i++) {
112 /* Rotate appropriately. */
113 int rx = ptcoords[i].x;
114 int ry = ptcoords[i].y;
115 if (r & PTH_VMIRROR) ry = -ry;
116 if (r & PTH_HMIRROR) rx = -rx;
117 if (r & PTH_90ROT) {
118 int rs = rx; rx = -ry; ry = rs;
120 int bi = pthbc + ry * MAX_PATTERN_DIST + rx;
122 /* Copy info. */
123 pthashes[r][i][S_NONE] = pthboard[bi][S_NONE];
124 pthashes[r][i][S_BLACK] = pthboard[bi][S_BLACK];
125 pthashes[r][i][S_WHITE] = pthboard[bi][S_WHITE];
126 pthashes[r][i][S_OFFBOARD] = pthboard[bi][S_OFFBOARD];
131 inline hash_t
132 spatial_hash(int rotation, struct spatial *s)
134 hash_t h = 0;
135 for (int i = 0; i < ptind[s->dist + 1]; i++) {
136 h ^= pthashes[rotation][i][spatial_point_at(*s, i)];
138 return h & spatial_hash_mask;
141 char *
142 spatial2str(struct spatial *s)
144 static char buf[1024];
145 for (int i = 0; i < ptind[s->dist + 1]; i++) {
146 buf[i] = stone2char(spatial_point_at(*s, i));
148 buf[ptind[s->dist + 1]] = 0;
149 return buf;
152 void
153 spatial_from_board(struct pattern_config *pc, struct spatial *s,
154 struct board *b, struct move *m)
156 assert(pc->spat_min > 0);
158 /* We record all spatial patterns black-to-play; simply
159 * reverse all colors if we are white-to-play. */
160 static enum stone bt_black[4] = { S_NONE, S_BLACK, S_WHITE, S_OFFBOARD };
161 static enum stone bt_white[4] = { S_NONE, S_WHITE, S_BLACK, S_OFFBOARD };
162 enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black;
164 memset(s, 0, sizeof(*s));
165 for (int j = 0; j < ptind[pc->spat_max + 1]; j++) {
166 ptcoords_at(x, y, m->coord, b, j);
167 s->points[j / 4] |= (*bt)[board_atxy(b, x, y)] << ((j % 4) * 2);
169 s->dist = pc->spat_max;
172 /* Compare two spatials, allowing for differences up to isomorphism.
173 * True means the spatials are equivalent. */
174 static bool
175 spatial_cmp(struct spatial *s1, struct spatial *s2)
177 /* Quick preliminary check. */
178 if (s1->dist != s2->dist)
179 return false;
181 /* We could create complex transposition tables, but it seems most
182 * foolproof to just check if the sets of rotation hashes are the
183 * same for both. */
184 hash_t s1r[PTH__ROTATIONS];
185 for (int r = 0; r < PTH__ROTATIONS; r++)
186 s1r[r] = spatial_hash(r, s1);
187 for (int r = 0; r < PTH__ROTATIONS; r++) {
188 hash_t s2r = spatial_hash(r, s2);
189 for (int p = 0; p < PTH__ROTATIONS; p++)
190 if (s2r == s1r[p])
191 goto found_rot;
192 /* Rotation hash s2r does not correspond to s1r. */
193 return false;
194 found_rot:;
197 /* All rotation hashes of s2 occur in s1. Hopefully that
198 * indicates something. */
199 return true;
203 /* Spatial dict manipulation. */
205 static int
206 spatial_dict_addc(struct spatial_dict *dict, struct spatial *s)
208 /* Allocate space in 1024 blocks. */
209 #define SPATIALS_ALLOC 1024
210 if (!(dict->nspatials % SPATIALS_ALLOC)) {
211 dict->spatials = realloc(dict->spatials,
212 (dict->nspatials + SPATIALS_ALLOC)
213 * sizeof(*dict->spatials));
215 dict->spatials[dict->nspatials] = *s;
216 return dict->nspatials++;
219 static bool
220 spatial_dict_addh(struct spatial_dict *dict, hash_t hash, int id)
222 if (dict->hash[hash] && dict->hash[hash] != id)
223 dict->collisions++;
224 dict->hash[hash] = id;
225 return true;
228 /* Spatial dictionary file format:
229 * /^#/ - comments
230 * INDEX RADIUS STONES HASH...
231 * INDEX: index in the spatial table
232 * RADIUS: @d of the pattern
233 * STONES: string of ".XO#" chars
234 * HASH...: space-separated 18bit hash-table indices for the pattern */
236 static void
237 spatial_dict_read(struct spatial_dict *dict, char *buf)
239 /* XXX: We trust the data. Bad data will crash us. */
240 char *bufp = buf;
242 int index, radius;
243 index = strtol(bufp, &bufp, 10);
244 radius = strtol(bufp, &bufp, 10);
245 while (isspace(*bufp)) bufp++;
247 /* Load the stone configuration. */
248 struct spatial s = { .dist = radius };
249 int sl = 0;
250 while (!isspace(*bufp)) {
251 s.points[sl / 4] |= char2stone(*bufp++) << ((sl % 4)*2);
252 sl++;
254 while (isspace(*bufp)) bufp++;
256 /* Sanity check. */
257 if (sl != ptind[s.dist + 1]) {
258 fprintf(stderr, "Spatial dictionary: Invalid number of stones (%d != %d) on this line: %s\n",
259 sl, ptind[radius + 1] - 1, buf);
260 exit(EXIT_FAILURE);
263 /* Add to collection. */
264 int id = spatial_dict_addc(dict, &s);
266 /* Add to specified hash places. */
267 while (*bufp) {
268 int hash = strtol(bufp, &bufp, 16);
269 while (isspace(*bufp)) bufp++;
270 spatial_dict_addh(dict, hash & spatial_hash_mask, id);
274 void
275 spatial_write(struct spatial_dict *dict, struct spatial *s, int id, FILE *f)
277 fprintf(f, "%d %d ", id, s->dist);
278 fputs(spatial2str(s), f);
279 for (int r = 0; r < PTH__ROTATIONS; r++) {
280 hash_t rhash = spatial_hash(r, s);
281 int id2 = dict->hash[rhash];
282 if (id2 != id) {
283 /* This hash does not belong to us. Decide whether
284 * we or the current owner is better owner. */
285 /* TODO: Compare also # of patternscan encounters? */
286 struct spatial *s2 = &dict->spatials[id2];
287 if (s2->dist < s->dist)
288 continue;
289 if (s2->dist == s->dist && id2 < id)
290 continue;
292 fprintf(f, " %"PRIhash"", spatial_hash(r, s));
294 fputc('\n', f);
297 static void
298 spatial_dict_load(struct spatial_dict *dict, FILE *f)
300 char buf[1024];
301 while (fgets(buf, sizeof(buf), f)) {
302 if (buf[0] == '#') continue;
303 spatial_dict_read(dict, buf);
307 void
308 spatial_dict_writeinfo(struct spatial_dict *dict, FILE *f)
310 /* New file. First, create a comment describing order
311 * of points in the array. This is just for purposes
312 * of external tools, Pachi never interprets it itself. */
313 fprintf(f, "# Pachi spatial patterns dictionary v1.0 maxdist %d\n",
314 MAX_PATTERN_DIST);
315 for (int d = 0; d <= MAX_PATTERN_DIST; d++) {
316 fprintf(f, "# Point order: d=%d ", d);
317 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
318 fprintf(f, "%d,%d ", ptcoords[j].x, ptcoords[j].y);
320 fprintf(f, "\n");
324 const char *spatial_dict_filename = "patterns.spat";
325 struct spatial_dict *
326 spatial_dict_init(bool will_append)
328 FILE *f = fopen(spatial_dict_filename, "r");
329 if (!f && !will_append) {
330 if (DEBUGL(1))
331 fprintf(stderr, "No spatial dictionary, will not match spatial pattern features.\n");
332 return NULL;
335 struct spatial_dict *dict = calloc(1, sizeof(*dict));
336 /* We create a dummy record for index 0 that we will
337 * never reference. This is so that hash value 0 can
338 * represent "no value". */
339 struct spatial dummy = { .dist = 0 };
340 spatial_dict_addc(dict, &dummy);
342 if (f) {
343 spatial_dict_load(dict, f);
344 fclose(f); f = NULL;
345 } else {
346 assert(will_append);
349 return dict;
353 spatial_dict_put(struct spatial_dict *dict, struct spatial *s, hash_t h)
355 /* We avoid spatial_dict_get() here, since we want to ignore radius
356 * differences - we have custom collision detection. */
357 int id = dict->hash[h];
358 if (id > 0) {
359 /* Is this the same or isomorphous spatial? */
360 if (spatial_cmp(s, &dict->spatials[id]))
361 return id;
363 /* Look a bit harder - perhaps one of our rotations still
364 * points at the correct spatial. */
365 for (int r = 0; r < PTH__ROTATIONS; r++) {
366 hash_t rhash = spatial_hash(r, s);
367 int rid = dict->hash[rhash];
368 /* No match means we definitely aren't stored yet. */
369 if (!rid)
370 break;
371 if (id != rid && spatial_cmp(s, &dict->spatials[rid])) {
372 /* Yay, this is us! */
373 if (DEBUGL(3))
374 fprintf(stderr, "Repeated collision %d vs %d\n", id, rid);
375 id = rid;
376 /* Point the hashes back to us. */
377 goto hash_store;
381 if (DEBUGL(1))
382 fprintf(stderr, "Collision %d vs %d\n", id, dict->nspatials);
383 id = 0;
384 /* dict->collisions++; gets done by addh */
387 /* Add new pattern! */
388 id = spatial_dict_addc(dict, s);
389 if (DEBUGL(4)) {
390 fprintf(stderr, "new spat %d(%d) %s <%"PRIhash"> ", id, s->dist, spatial2str(s), h);
391 for (int r = 0; r < 8; r++)
392 fprintf(stderr,"[%"PRIhash"] ", spatial_hash(r, s));
393 fprintf(stderr, "\n");
396 /* Store new pattern in the hash. */
397 hash_store:
398 for (int r = 0; r < PTH__ROTATIONS; r++)
399 spatial_dict_addh(dict, spatial_hash(r, s), id);
401 return id;
405 /** Pattern3 helpers */
407 /* XXX: We have hard-coded this point order:
408 * # Point order: d=1 0,0
409 * # Point order: d=2 0,1 0,-1 1,0 -1,0
410 * # Point order: d=3 1,1 -1,1 1,-1 -1,-1
412 /* p3bits describe location of given point in the
413 * pattern3 hash word. */
414 static const int p3bits[] = { -1, 1, 6, 3, 4, 0, 2, 5, 7 };
417 static hash_t
418 pattern3_to_spatial(int pat3)
420 hash_t h = pthashes[0][0][S_NONE];
421 for (int i = 1; i < 9; i++)
422 h ^= pthashes[0][i][(pat3 >> (p3bits[i] * 2)) & 0x3];
423 return h & spatial_hash_mask;
427 spatial_to_pattern3(struct spatial *s)
429 assert(s->dist == 3);
430 int pat3 = 0;
431 for (int i = 1; i < 9; i++)
432 pat3 |= spatial_point_at(*s, i) << (p3bits[i] * 2);
433 return pat3;
437 pattern3_by_spatial(struct spatial_dict *dict, int pat3)
439 /* Just pull pat3 through the spatial database to generate
440 * hash of its canonical form. */
441 hash_t h = pattern3_to_spatial(pat3);
442 int s = spatial_dict_get(dict, 3, h);
443 /* XXX: We assume our spatial dictionary is _sane_, that is,
444 * all valid 3x3 patterns we could encounter are in the
445 * dictionary. If you hit this assert(), you probably
446 * generated the spatial dict over too few games; it is best
447 * to generate it over the same set of games as you match
448 * patterns on afterwards. */
449 assert(s > 0);
450 return spatial_to_pattern3(&dict->spatials[s]);