Added some safety test in roulette_wheel procedures:
if total is zero, used to return iterator -1 - now returns uniform choice
This commit is contained in:
parent
e3e4a0b719
commit
01e4aa9cdc
1 changed files with 10 additions and 0 deletions
|
|
@ -132,7 +132,11 @@ double sum_fitness(const eoPop<EOT>& _pop, std::pair<double, double>& _minmax)
|
||||||
template <class It>
|
template <class It>
|
||||||
It roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
|
It roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
|
||||||
{
|
{
|
||||||
|
|
||||||
float roulette = _gen.uniform(total);
|
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;
|
It i = _begin;
|
||||||
|
|
||||||
|
|
@ -149,6 +153,9 @@ const EOT& roulette_wheel(const eoPop<EOT>& _pop, double total, eoRng& _gen = rn
|
||||||
{
|
{
|
||||||
float roulette = _gen.uniform(total);
|
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<EOT>::const_iterator i = _pop.begin();
|
eoPop<EOT>::const_iterator i = _pop.begin();
|
||||||
|
|
||||||
while (roulette > 0.0)
|
while (roulette > 0.0)
|
||||||
|
|
@ -164,6 +171,9 @@ EOT& roulette_wheel(eoPop<EOT>& _pop, double total, eoRng& _gen = rng)
|
||||||
{
|
{
|
||||||
float roulette = _gen.uniform(total);
|
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<EOT>::iterator i = _pop.begin();
|
eoPop<EOT>::iterator i = _pop.begin();
|
||||||
|
|
||||||
while (roulette > 0.0)
|
while (roulette > 0.0)
|
||||||
|
|
|
||||||
Reference in a new issue