From 81c1931a25937dc7822b4c5c12ceface57ed1773 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 8 Jan 2012 20:29:16 +0100 Subject: [PATCH] Patternplay rate_moves -> patternprob pattern_rate_moves() Also decouple normalization to separate pass. --- patternplay/patternplay.c | 42 +++++++----------------------------------- patternprob.c | 26 ++++++++++++++++++++++++++ patternprob.h | 8 ++++++++ 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/patternplay/patternplay.c b/patternplay/patternplay.c index 4d5f7db..2e6d8c0 100644 --- a/patternplay/patternplay.c +++ b/patternplay/patternplay.c @@ -23,39 +23,6 @@ struct patternplay { }; -/* Evaluate patterns for all available moves. Stores found patterns - * to pats[b->flen] and normalized probability of each pattern (or NaN - * in case of no match) to probs[b->flen]. */ -static void -rate_moves(struct pattern_config *pc, pattern_spec *ps, struct pattern_pdict *pd, - struct board *b, enum stone color, - struct pattern *pats, floating_t *probs) -{ - /* First pass: Gather probabilities. */ - floating_t total = 0; - for (int f = 0; f < b->flen; f++) { - probs[f] = NAN; - - struct move mo = { .coord = b->f[f], .color = color }; - if (is_pass(mo.coord)) - continue; - if (!board_is_valid_move(b, &mo)) - continue; - - pattern_match(pc, *ps, &pats[f], b, &mo); - floating_t prob = pattern_prob(pd, &pats[f]); - if (!isnan(prob)) { - probs[f] = prob; - total += prob; - } - } - - /* Second pass: Rescale. */ - for (int f = 0; f < b->flen; f++) { - probs[f] /= total; - } -} - static coord_t * patternplay_genmove(struct engine *e, struct board *b, struct time_info *ti, enum stone color, bool pass_all_alive) { @@ -63,7 +30,7 @@ patternplay_genmove(struct engine *e, struct board *b, struct time_info *ti, enu struct pattern pats[b->flen]; floating_t probs[b->flen]; - rate_moves(&pp->pc, &pp->ps, pp->pd, b, color, pats, probs); + pattern_rate_moves(&pp->pc, &pp->ps, pp->pd, b, color, pats, probs); int best = 0; for (int f = 0; f < b->flen; f++) { @@ -84,7 +51,12 @@ patternplay_evaluate(struct engine *e, struct board *b, struct time_info *ti, fl struct patternplay *pp = e->data; struct pattern pats[b->flen]; - rate_moves(&pp->pc, &pp->ps, pp->pd, b, color, pats, vals); + floating_t total = pattern_rate_moves(&pp->pc, &pp->ps, pp->pd, b, color, pats, vals); + + /* Rescale properly. */ + for (int f = 0; f < b->flen; f++) { + probs[f] /= total; + } if (pp->debug_level >= 4) { for (int f = 0; f < b->flen; f++) { diff --git a/patternprob.c b/patternprob.c index 09de23e..30a3778 100644 --- a/patternprob.c +++ b/patternprob.c @@ -84,3 +84,29 @@ pattern_pdict_init(char *filename, struct pattern_config *pc) cached_dict = dict; return dict; } + +floating_t +pattern_rate_moves(struct pattern_config *pc, pattern_spec *ps, struct pattern_pdict *pd, + struct board *b, enum stone color, + struct pattern *pats, floating_t *probs) +{ + floating_t total = 0; + for (int f = 0; f < b->flen; f++) { + probs[f] = NAN; + + struct move mo = { .coord = b->f[f], .color = color }; + if (is_pass(mo.coord)) + continue; + if (!board_is_valid_move(b, &mo)) + continue; + + pattern_match(pc, *ps, &pats[f], b, &mo); + floating_t prob = pattern_prob(pd, &pats[f]); + if (!isnan(prob)) { + probs[f] = prob; + total += prob; + } + } + return total; +} + diff --git a/patternprob.h b/patternprob.h index 198a147..9bb4d55 100644 --- a/patternprob.h +++ b/patternprob.h @@ -39,6 +39,14 @@ struct pattern_pdict *pattern_pdict_init(char *filename, struct pattern_config * * the pattern cannot be found. */ static floating_t pattern_prob(struct pattern_pdict *dict, struct pattern *p); +/* Evaluate patterns for all available moves. Stores found patterns + * to pats[b->flen] and NON-normalized probability of each pattern + * (or NaN in case of no match) to probs[b->flen]. Returns the sum + * of all probabilities that can be used for normalization. */ +floating_t pattern_rate_moves(struct pattern_config *pc, pattern_spec *ps, struct pattern_pdict *pd, + struct board *b, enum stone color, + struct pattern *pats, floating_t *probs); + /* Utility function - extract spatial id from a pattern. If the pattern * has no spatial feature, it is represented by the highest spatial id * plus one. */ -- 2.11.4.GIT