Spatial: Accurate analysis of hashing function performance
[pachi/nmclean.git] / patternprob.h
blob7ca52b2d628fbe48a0f646f5a61d9c3d684789d3
1 #ifndef PACHI_PATTERNPROB_H
2 #define PACHI_PATTERNPROB_H
4 /* Pattern probability table. */
6 #include <math.h>
8 #include "board.h"
9 #include "move.h"
10 #include "pattern.h"
13 /* The pattern probability table considers each pattern as a whole
14 * (not dividing it to individual features) and stores probability
15 * of the pattern being played. */
17 /* The table primary key is the pattern spatial (most distinctive
18 * feature); within a single primary key chain, the entries are
19 * unsorted (for now). */
21 struct pattern_prob {
22 struct pattern p;
23 floating_t prob;
24 struct pattern_prob *next;
27 struct pattern_pdict {
28 struct pattern_config *pc;
30 struct pattern_prob **table; /* [pc->spat_dict->nspatials + 1] */
33 /* Initialize the pdict data structure from a given file (pass NULL
34 * to use default filename). Returns NULL if the file with patterns
35 * has been found. */
36 struct pattern_pdict *pattern_pdict_init(char *filename, struct pattern_config *pc);
38 /* Return probability associated with given pattern. Returns NaN if
39 * the pattern cannot be found. */
40 static floating_t pattern_prob(struct pattern_pdict *dict, struct pattern *p);
42 /* Evaluate patterns for all available moves. Stores found patterns
43 * to pats[b->flen] and NON-normalized probability of each pattern
44 * (or NaN in case of no match) to probs[b->flen]. Returns the sum
45 * of all probabilities that can be used for normalization. */
46 floating_t pattern_rate_moves(struct pattern_setup *pat,
47 struct board *b, enum stone color,
48 struct pattern *pats, floating_t *probs);
50 /* Utility function - extract spatial id from a pattern. If the pattern
51 * has no spatial feature, it is represented by the highest spatial id
52 * plus one. */
53 static uint32_t pattern2spatial(struct pattern_pdict *dict, struct pattern *p);
56 static inline floating_t
57 pattern_prob(struct pattern_pdict *dict, struct pattern *p)
59 uint32_t spi = pattern2spatial(dict, p);
60 for (struct pattern_prob *pb = dict->table[spi]; pb; pb = pb->next)
61 if (pattern_eq(p, &pb->p))
62 return pb->prob;
63 return NAN; // XXX: We assume quiet NAN existence
66 static inline uint32_t
67 pattern2spatial(struct pattern_pdict *dict, struct pattern *p)
69 for (int i = 0; i < p->n; i++)
70 if (p->f[i].id == FEAT_SPATIAL)
71 return p->f[i].payload;
72 return dict->pc->spat_dict->nspatials;
75 #endif