Split off BOARD_SPATPROB; board.spatprob is not interesting for us so far
[pachi.git] / patternprob.h
blobe8d26480906d7f9b518510e8bf74659636c5c7a9
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"
11 #include "patternsp.h"
14 /* The pattern probability table considers each pattern as a whole
15 * (not dividing it to individual features) and stores probability
16 * of the pattern being played. */
18 /* The table primary key is the pattern spatial (most distinctive
19 * feature); within a single primary key chain, the entries are
20 * unsorted (for now). */
22 struct pattern_prob {
23 struct pattern p;
24 floating_t prob;
25 struct pattern_prob *next;
28 struct pattern_pdict {
29 struct pattern_config *pc;
31 struct pattern_prob **table; /* [pc->spat_dict->nspatials + 1] */
34 /* Initialize the pdict data structure from a given file (pass NULL
35 * to use default filename). Returns NULL if the file with patterns
36 * has been found. */
37 struct pattern_pdict *pattern_pdict_init(char *filename, struct pattern_config *pc);
39 /* Return probability associated with given pattern. Returns NaN if
40 * the pattern cannot be found. */
41 static floating_t pattern_prob(struct pattern_pdict *dict, struct pattern *p);
43 /* Evaluate patterns for all available moves. Stores found patterns
44 * to pats[b->flen] and NON-normalized probability of each pattern
45 * (or NaN in case of no match) to probs[b->flen]. Returns the sum
46 * of all probabilities that can be used for normalization. */
47 floating_t pattern_rate_moves(struct pattern_setup *pat,
48 struct board *b, enum stone color,
49 struct pattern *pats, floating_t *probs);
51 /* pattern_rate_moves() but for only some set of moves. */
52 floating_t pattern_rate_some_moves(struct pattern_setup *pat,
53 struct board *b, enum stone color,
54 coord_t *coords, int coord_n,
55 struct pattern *pats, floating_t *probs);
57 /* Utility function - extract spatial id from a pattern. If the pattern
58 * has no spatial feature, it is represented by the highest spatial id
59 * plus one. */
60 static uint32_t pattern2spatial(struct pattern_pdict *dict, struct pattern *p);
63 static inline floating_t
64 pattern_prob(struct pattern_pdict *dict, struct pattern *p)
66 uint32_t spi = pattern2spatial(dict, p);
67 for (struct pattern_prob *pb = dict->table[spi]; pb; pb = pb->next)
68 if (pattern_eq(p, &pb->p))
69 return pb->prob;
70 return NAN; // XXX: We assume quiet NAN existence
73 static inline uint32_t
74 pattern2spatial(struct pattern_pdict *dict, struct pattern *p)
76 for (int i = 0; i < p->n; i++)
77 if (p->f[i].id == FEAT_SPATIAL)
78 return p->f[i].payload;
79 return dict->pc->spat_dict->nspatials;
82 #endif