11 #include "patternsp.h"
14 /* Mapping from point sequence to coordinate offsets (to determine
15 * coordinates relative to pattern center). The array is ordered
16 * in the gridcular metric order so that we can go through it
17 * and incrementally match spatial features in nested circles.
18 * Within one circle, coordinates are ordered by rows to keep
19 * good cache behavior. */
20 struct ptcoord ptcoords
[MAX_PATTERN_AREA
];
22 /* For each radius, starting index in ptcoords[]. */
23 int ptind
[MAX_PATTERN_DIST
+ 2];
25 /* ptcoords[], ptind[] setup */
29 int i
= 0; /* Indexing ptcoords[] */
31 /* First, center point. */
32 ptind
[0] = ptind
[1] = 0;
33 ptcoords
[i
].x
= ptcoords
[i
].y
= 0; i
++;
35 for (int d
= 2; d
<= MAX_PATTERN_DIST
; d
++) {
37 /* For each y, examine all integer solutions
38 * of d = |x| + |y| + max(|x|, |y|). */
39 /* TODO: (Stern, 2006) uses a hand-modified
40 * circles that are finer for small d. */
41 for (short y
= d
/ 2; y
>= 0; y
--) {
44 /* max(|x|, |y|) = |y|, non-zero x */
46 if (x
+ y
* 2 != d
) continue;
48 /* max(|x|, |y|) = |x| */
49 /* Or, max(|x|, |y|) = |y| and x is zero */
51 if (x
* 2 + y
!= d
) continue;
54 assert((x
> y
? x
: y
) + x
+ y
== d
);
56 ptcoords
[i
].x
= x
; ptcoords
[i
].y
= y
; i
++;
57 if (x
!= 0) { ptcoords
[i
].x
= -x
; ptcoords
[i
].y
= y
; i
++; }
58 if (y
!= 0) { ptcoords
[i
].x
= x
; ptcoords
[i
].y
= -y
; i
++; }
59 if (x
!= 0 && y
!= 0) { ptcoords
[i
].x
= -x
; ptcoords
[i
].y
= -y
; i
++; }
62 ptind
[MAX_PATTERN_DIST
+ 1] = i
;
65 for (int d
= 0; d
<= MAX_PATTERN_DIST
; d
++) {
66 fprintf(stderr
, "d=%d (%d) ", d
, ptind
[d
]);
67 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
68 fprintf(stderr
, "%d,%d ", ptcoords
[j
].x
, ptcoords
[j
].y
);
70 fprintf(stderr
, "\n");
76 /* Zobrist hashes used for points in patterns. */
77 hash_t pthashes
[PTH__ROTATIONS
][MAX_PATTERN_AREA
][S_MAX
];
82 /* We need fixed hashes for all pattern-relative in
83 * all pattern users! This is a simple way to generate
84 * hopefully good ones. Park-Miller powa. :) */
86 /* We create a virtual board (centered at the sequence start),
87 * plant the hashes there, then pick them up into the sequence
88 * with correct coordinates. It would be possible to generate
89 * the sequence point hashes directly, but the rotations would
90 * make for enormous headaches. */
91 hash_t pthboard
[MAX_PATTERN_AREA
][4];
92 int pthbc
= MAX_PATTERN_AREA
/ 2; // tengen coord
94 /* The magic numbers are tuned for minimal collisions. */
96 for (int i
= 0; i
< MAX_PATTERN_AREA
; i
++) {
97 pthboard
[i
][S_NONE
] = (h
= h
* 16803 - 7);
98 pthboard
[i
][S_BLACK
] = (h
= h
* 16805 + 7);
99 pthboard
[i
][S_WHITE
] = (h
= h
* 16807 + 3);
100 pthboard
[i
][S_OFFBOARD
] = (h
= h
* 16809 - 3);
103 /* Virtual board with hashes created, now fill
104 * pthashes[] with hashes for points in actual
105 * sequences, also considering various rotations. */
106 #define PTH_VMIRROR 1
107 #define PTH_HMIRROR 2
109 for (int r
= 0; r
< PTH__ROTATIONS
; r
++) {
110 for (int i
= 0; i
< MAX_PATTERN_AREA
; i
++) {
111 /* Rotate appropriately. */
112 int rx
= ptcoords
[i
].x
;
113 int ry
= ptcoords
[i
].y
;
114 if (r
& PTH_VMIRROR
) ry
= -ry
;
115 if (r
& PTH_HMIRROR
) rx
= -rx
;
117 int rs
= rx
; rx
= -ry
; ry
= rs
;
119 int bi
= pthbc
+ ry
* MAX_PATTERN_DIST
+ rx
;
122 pthashes
[r
][i
][S_NONE
] = pthboard
[bi
][S_NONE
];
123 pthashes
[r
][i
][S_BLACK
] = pthboard
[bi
][S_BLACK
];
124 pthashes
[r
][i
][S_WHITE
] = pthboard
[bi
][S_WHITE
];
125 pthashes
[r
][i
][S_OFFBOARD
] = pthboard
[bi
][S_OFFBOARD
];
130 static void __attribute__((constructor
))
133 /* Initialization of various static data structures for
134 * fast pattern processing. */
140 spatial_hash(int rotation
, struct spatial
*s
)
143 for (int i
= 0; i
< ptind
[s
->dist
+ 1]; i
++) {
144 h
^= pthashes
[rotation
][i
][spatial_point_at(*s
, i
)];
146 return h
& spatial_hash_mask
;
150 spatial2str(struct spatial
*s
)
152 static char buf
[1024];
153 for (int i
= 0; i
< ptind
[s
->dist
+ 1]; i
++) {
154 buf
[i
] = stone2char(spatial_point_at(*s
, i
));
156 buf
[ptind
[s
->dist
+ 1]] = 0;
161 spatial_from_board(struct pattern_config
*pc
, struct spatial
*s
,
162 struct board
*b
, struct move
*m
)
164 assert(pc
->spat_min
> 0);
166 /* We record all spatial patterns black-to-play; simply
167 * reverse all colors if we are white-to-play. */
168 static enum stone bt_black
[4] = { S_NONE
, S_BLACK
, S_WHITE
, S_OFFBOARD
};
169 static enum stone bt_white
[4] = { S_NONE
, S_WHITE
, S_BLACK
, S_OFFBOARD
};
170 enum stone (*bt
)[4] = m
->color
== S_WHITE
? &bt_white
: &bt_black
;
172 memset(s
, 0, sizeof(*s
));
173 for (int j
= 0; j
< ptind
[pc
->spat_max
+ 1]; j
++) {
174 ptcoords_at(x
, y
, m
->coord
, b
, j
);
175 s
->points
[j
/ 4] |= (*bt
)[board_atxy(b
, x
, y
)] << ((j
% 4) * 2);
177 s
->dist
= pc
->spat_max
;
180 /* Compare two spatials, allowing for differences up to isomorphism.
181 * True means the spatials are equivalent. */
183 spatial_cmp(struct spatial
*s1
, struct spatial
*s2
)
185 /* Quick preliminary check. */
186 if (s1
->dist
!= s2
->dist
)
189 /* We could create complex transposition tables, but it seems most
190 * foolproof to just check if the sets of rotation hashes are the
192 hash_t s1r
[PTH__ROTATIONS
];
193 for (int r
= 0; r
< PTH__ROTATIONS
; r
++)
194 s1r
[r
] = spatial_hash(r
, s1
);
195 for (int r
= 0; r
< PTH__ROTATIONS
; r
++) {
196 hash_t s2r
= spatial_hash(r
, s2
);
197 for (int p
= 0; p
< PTH__ROTATIONS
; p
++)
200 /* Rotation hash s2r does not correspond to s1r. */
205 /* All rotation hashes of s2 occur in s1. Hopefully that
206 * indicates something. */
211 /* Spatial dict manipulation. */
214 spatial_dict_addc(struct spatial_dict
*dict
, struct spatial
*s
)
216 /* Allocate space in 1024 blocks. */
217 #define SPATIALS_ALLOC 1024
218 if (!(dict
->nspatials
% SPATIALS_ALLOC
)) {
219 dict
->spatials
= realloc(dict
->spatials
,
220 (dict
->nspatials
+ SPATIALS_ALLOC
)
221 * sizeof(*dict
->spatials
));
223 dict
->spatials
[dict
->nspatials
] = *s
;
224 return dict
->nspatials
++;
228 spatial_dict_addh(struct spatial_dict
*dict
, hash_t hash
, unsigned int id
)
230 if (dict
->hash
[hash
] && dict
->hash
[hash
] != id
)
232 dict
->hash
[hash
] = id
;
236 /* Spatial dictionary file format:
238 * INDEX RADIUS STONES HASH...
239 * INDEX: index in the spatial table
240 * RADIUS: @d of the pattern
241 * STONES: string of ".XO#" chars
242 * HASH...: space-separated 18bit hash-table indices for the pattern */
245 spatial_dict_read(struct spatial_dict
*dict
, char *buf
)
247 /* XXX: We trust the data. Bad data will crash us. */
251 index
= strtol(bufp
, &bufp
, 10);
252 radius
= strtol(bufp
, &bufp
, 10);
253 while (isspace(*bufp
)) bufp
++;
255 /* Load the stone configuration. */
256 struct spatial s
= { .dist
= radius
};
258 while (!isspace(*bufp
)) {
259 s
.points
[sl
/ 4] |= char2stone(*bufp
++) << ((sl
% 4)*2);
262 while (isspace(*bufp
)) bufp
++;
265 if (sl
!= ptind
[s
.dist
+ 1]) {
266 fprintf(stderr
, "Spatial dictionary: Invalid number of stones (%d != %d) on this line: %s\n",
267 sl
, ptind
[radius
+ 1] - 1, buf
);
271 /* Add to collection. */
272 unsigned int id
= spatial_dict_addc(dict
, &s
);
274 /* Add to specified hash places. */
276 int hash
= strtol(bufp
, &bufp
, 16);
277 while (isspace(*bufp
)) bufp
++;
278 spatial_dict_addh(dict
, hash
& spatial_hash_mask
, id
);
283 spatial_write(struct spatial_dict
*dict
, struct spatial
*s
, int id
, FILE *f
)
285 fprintf(f
, "%d %d ", id
, s
->dist
);
286 fputs(spatial2str(s
), f
);
287 for (int r
= 0; r
< PTH__ROTATIONS
; r
++) {
288 hash_t rhash
= spatial_hash(r
, s
);
289 int id2
= dict
->hash
[rhash
];
291 /* This hash does not belong to us. Decide whether
292 * we or the current owner is better owner. */
293 /* TODO: Compare also # of patternscan encounters? */
294 struct spatial
*s2
= &dict
->spatials
[id2
];
295 if (s2
->dist
< s
->dist
)
297 if (s2
->dist
== s
->dist
&& id2
< id
)
300 fprintf(f
, " %"PRIhash
"", spatial_hash(r
, s
));
306 spatial_dict_load(struct spatial_dict
*dict
, FILE *f
)
309 while (fgets(buf
, sizeof(buf
), f
)) {
310 if (buf
[0] == '#') continue;
311 spatial_dict_read(dict
, buf
);
316 spatial_dict_writeinfo(struct spatial_dict
*dict
, FILE *f
)
318 /* New file. First, create a comment describing order
319 * of points in the array. This is just for purposes
320 * of external tools, Pachi never interprets it itself. */
321 fprintf(f
, "# Pachi spatial patterns dictionary v1.0 maxdist %d\n",
323 for (int d
= 0; d
<= MAX_PATTERN_DIST
; d
++) {
324 fprintf(f
, "# Point order: d=%d ", d
);
325 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
326 fprintf(f
, "%d,%d ", ptcoords
[j
].x
, ptcoords
[j
].y
);
332 const char *spatial_dict_filename
= "patterns.spat";
333 struct spatial_dict
*
334 spatial_dict_init(bool will_append
)
336 FILE *f
= fopen(spatial_dict_filename
, "r");
337 if (!f
&& !will_append
) {
339 fprintf(stderr
, "No spatial dictionary, will not match spatial pattern features.\n");
343 struct spatial_dict
*dict
= calloc2(1, sizeof(*dict
));
344 /* We create a dummy record for index 0 that we will
345 * never reference. This is so that hash value 0 can
346 * represent "no value". */
347 struct spatial dummy
= { .dist
= 0 };
348 spatial_dict_addc(dict
, &dummy
);
351 spatial_dict_load(dict
, f
);
361 spatial_dict_put(struct spatial_dict
*dict
, struct spatial
*s
, hash_t h
)
363 /* We avoid spatial_dict_get() here, since we want to ignore radius
364 * differences - we have custom collision detection. */
365 int id
= dict
->hash
[h
];
367 /* Is this the same or isomorphous spatial? */
368 if (spatial_cmp(s
, &dict
->spatials
[id
]))
371 /* Look a bit harder - perhaps one of our rotations still
372 * points at the correct spatial. */
373 for (int r
= 0; r
< PTH__ROTATIONS
; r
++) {
374 hash_t rhash
= spatial_hash(r
, s
);
375 int rid
= dict
->hash
[rhash
];
376 /* No match means we definitely aren't stored yet. */
379 if (id
!= rid
&& spatial_cmp(s
, &dict
->spatials
[rid
])) {
380 /* Yay, this is us! */
382 fprintf(stderr
, "Repeated collision %d vs %d\n", id
, rid
);
384 /* Point the hashes back to us. */
390 fprintf(stderr
, "Collision %d vs %d\n", id
, dict
->nspatials
);
392 /* dict->collisions++; gets done by addh */
395 /* Add new pattern! */
396 id
= spatial_dict_addc(dict
, s
);
398 fprintf(stderr
, "new spat %d(%d) %s <%"PRIhash
"> ", id
, s
->dist
, spatial2str(s
), h
);
399 for (int r
= 0; r
< 8; r
++)
400 fprintf(stderr
,"[%"PRIhash
"] ", spatial_hash(r
, s
));
401 fprintf(stderr
, "\n");
404 /* Store new pattern in the hash. */
406 for (int r
= 0; r
< PTH__ROTATIONS
; r
++)
407 spatial_dict_addh(dict
, spatial_hash(r
, s
), id
);
413 /** Pattern3 helpers */
415 /* XXX: We have hard-coded this point order:
416 * # Point order: d=1 0,0
417 * # Point order: d=2 0,1 0,-1 1,0 -1,0
418 * # Point order: d=3 1,1 -1,1 1,-1 -1,-1
420 /* p3bits describe location of given point in the
421 * pattern3 hash word. */
422 static const int p3bits
[] = { -1, 1, 6, 3, 4, 0, 2, 5, 7 };
426 pattern3_to_spatial(int r
, hash3_t pat3
)
428 hash_t h
= pthashes
[r
][0][S_NONE
];
429 for (int i
= 1; i
< 9; i
++)
430 h
^= pthashes
[r
][i
][(pat3
>> (p3bits
[i
] * 2)) & 0x3];
431 return h
& spatial_hash_mask
;
435 spatial_to_pattern3(struct spatial
*s
)
437 assert(s
->dist
== 3);
439 for (int i
= 1; i
< 9; i
++)
440 pat3
|= spatial_point_at(*s
, i
) << (p3bits
[i
] * 2);
445 pattern3_by_spatial(struct spatial_dict
*dict
, hash3_t pat3
)
447 /* Just pull pat3 through the spatial database to generate
448 * hash of its canonical form. */
450 for (int r
= 0; r
< PTH__ROTATIONS
; r
++) {
451 hash_t h
= pattern3_to_spatial(r
, pat3
);
452 s
= spatial_dict_get(dict
, 3, h
);
454 /* We might need to try another rotation in case
457 /* XXX: We assume our spatial dictionary is _sane_, that is,
458 * all valid 3x3 patterns we could encounter are in the
459 * dictionary. If you hit this assert(), you probably
460 * generated the spatial dict over too few games; it is best
461 * to generate it over the same set of games as you match
462 * patterns on afterwards. */
464 return spatial_to_pattern3(&dict
->spatials
[s
]);