Merge remote-tracking branch 'origin/master' into patterns
[pachi.git] / patternprob.h
blob198a1478f3f05423596c33bb48c52ab7fb64b41f
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 /* Utility function - extract spatial id from a pattern. If the pattern
43 * has no spatial feature, it is represented by the highest spatial id
44 * plus one. */
45 static uint32_t pattern2spatial(struct pattern_pdict *dict, struct pattern *p);
48 static inline floating_t
49 pattern_prob(struct pattern_pdict *dict, struct pattern *p)
51 uint32_t spi = pattern2spatial(dict, p);
52 for (struct pattern_prob *pb = dict->table[spi]; pb; pb = pb->next)
53 if (pattern_eq(p, &pb->p))
54 return pb->prob;
55 return NAN; // XXX: We assume quiet NAN existence
58 static inline uint32_t
59 pattern2spatial(struct pattern_pdict *dict, struct pattern *p)
61 for (int i = 0; i < p->n; i++)
62 if (p->f[i].id == FEAT_SPATIAL)
63 return p->f[i].payload;
64 return dict->pc->spat_dict->nspatials;
67 #endif