From 878e91a50715d63f9e93d123a361ed45675d85b2 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 14 Feb 2010 02:50:11 +0100 Subject: [PATCH] Probdist: Introduce PROBDIST_EPSILON --- playout/elo.c | 2 +- probdist.c | 9 +++++---- probdist.h | 3 +++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/playout/elo.c b/playout/elo.c index 92f76e8..7444441 100644 --- a/playout/elo.c +++ b/playout/elo.c @@ -126,7 +126,7 @@ playout_elo_choose(struct playout_policy *p, struct board *b, enum stone to_play } /* TODO: FEAT_CONTIGUITY support. */ /* Pick a move. */ - coord_t c = pd->total >= 0.01 ? probdist_pick(pd) : pass; + coord_t c = pd->total >= PROBDIST_EPSILON ? probdist_pick(pd) : pass; /* Repair the damage. */ if (!is_pass(b->ko.coord)) board_gamma_update(b, b->ko.coord, to_play); diff --git a/probdist.c b/probdist.c index d6bfa6b..5f8b39a 100644 --- a/probdist.c +++ b/probdist.c @@ -10,16 +10,17 @@ int probdist_pick(struct probdist *pd) { - assert(probdist_total(pd) >= 0); + float total = probdist_total(pd) - PROBDIST_EPSILON; + assert(total >= 0); /* TODO: float random */ - double stab = fast_frandom() * probdist_total(pd); - //fprintf(stderr, "stab %f / %f\n", stab, pd->items[pd->n - 1]); + double stab = fast_frandom() * total; + //fprintf(stderr, "stab %f / %f\n", stab, total); for (int i = 0; i < pd->n; i++) { if (stab <= pd->items[i]) return i; stab -= pd->items[i]; } - //fprintf(stderr, "overstab %f (total %f, sum %f)\n", stab, pd->items[pd->n - 1], sum); + fprintf(stderr, "overstab %f (total %f)\n", stab, total); assert(0); return -1; } diff --git a/probdist.h b/probdist.h index addf2ad..de00b91 100644 --- a/probdist.h +++ b/probdist.h @@ -20,6 +20,9 @@ struct probdist { }; #define probdist_total(pd) ((pd)->total) #define probdist_one(pd, i) ((pd)->items[i]) +/* Probability so small that it's same as zero; used to compensate + * for probdist.total inaccuracies. */ +#define PROBDIST_EPSILON 0.01 static void probdist_set(struct probdist *pd, int i, float val); -- 2.11.4.GIT