From 01e4aa9cdc1b4d8f9045e1bef1b1a2678f930b58 Mon Sep 17 00:00:00 2001 From: evomarc Date: Mon, 30 Oct 2000 14:54:29 +0000 Subject: [PATCH] Added some safety test in roulette_wheel procedures: if total is zero, used to return iterator -1 - now returns uniform choice --- eo/src/utils/selectors.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/eo/src/utils/selectors.h b/eo/src/utils/selectors.h index 349bf7cc..9d860873 100644 --- a/eo/src/utils/selectors.h +++ b/eo/src/utils/selectors.h @@ -132,7 +132,11 @@ double sum_fitness(const eoPop& _pop, std::pair& _minmax) template It roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng) { + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _min + _gen.random(_end - _min); // uniform choice It i = _begin; @@ -149,6 +153,9 @@ const EOT& roulette_wheel(const eoPop& _pop, double total, eoRng& _gen = rn { float roulette = _gen.uniform(total); + if (roulette == 0.0) // covers the case where total==0.0 + return _pop[_gen.random(_pop.size())]; // uniform choice + eoPop::const_iterator i = _pop.begin(); while (roulette > 0.0) @@ -164,6 +171,9 @@ EOT& roulette_wheel(eoPop& _pop, double total, eoRng& _gen = rng) { float roulette = _gen.uniform(total); + if (roulette == 0.0) // covers the case where total==0.0 + return _pop[_gen.random(_pop.size())]; // uniform choice + eoPop::iterator i = _pop.begin(); while (roulette > 0.0)