1 #ifndef PACHI_PATTERNPROB_H
2 #define PACHI_PATTERNPROB_H
4 /* Pattern probability table. */
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). */
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
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
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
))
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
;