fix eoProportionalSelect for negative fitnesses
This commit is contained in:
parent
1d092a5840
commit
41e3d8a648
1 changed files with 35 additions and 11 deletions
|
|
@ -29,6 +29,9 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "utils/eoRNG.h"
|
#include "utils/eoRNG.h"
|
||||||
#include "utils/selectors.h"
|
#include "utils/selectors.h"
|
||||||
#include "eoSelectOne.h"
|
#include "eoSelectOne.h"
|
||||||
|
|
@ -49,21 +52,33 @@ public:
|
||||||
/// Sanity check
|
/// Sanity check
|
||||||
eoProportionalSelect(const eoPop<EOT>& /*pop*/ = eoPop<EOT>())
|
eoProportionalSelect(const eoPop<EOT>& /*pop*/ = eoPop<EOT>())
|
||||||
{
|
{
|
||||||
if (minimizing_fitness<EOT>())
|
if (minimizing_fitness<EOT>()) {
|
||||||
throw eoException("eoProportionalSelect: minimizing fitness");
|
std::string msg = "eoProportionalSelect cannot be used with minimizing fitness";
|
||||||
|
eo::log << eo::errors << "ERROR: " << msg << std::endl;
|
||||||
|
throw eoException(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup(const eoPop<EOT>& _pop)
|
void setup(const eoPop<EOT>& _pop)
|
||||||
{
|
{
|
||||||
if (_pop.size() == 0) return;
|
if (_pop.size() == 0) {
|
||||||
|
eo::log << eo::warnings << "Warning: eoProportionalSelect setup called on an empty population." << std::endl;
|
||||||
cumulative.resize(_pop.size());
|
return;
|
||||||
cumulative[0] = _pop[0].fitness();
|
|
||||||
|
|
||||||
for (unsigned i = 1; i < _pop.size(); ++i)
|
|
||||||
{
|
|
||||||
cumulative[i] = _pop[i].fitness() + cumulative[i-1];
|
|
||||||
}
|
}
|
||||||
|
assert(not _pop[0].invalid());
|
||||||
|
|
||||||
|
const typename EOT::Fitness min_fit
|
||||||
|
= std::min_element( std::begin(_pop), std::end(_pop) )
|
||||||
|
->fitness();
|
||||||
|
|
||||||
|
cumulative.clear();
|
||||||
|
cumulative.push_back(_pop[0].fitness() - min_fit);
|
||||||
|
|
||||||
|
for (unsigned i = 1; i < _pop.size(); ++i) {
|
||||||
|
assert(not _pop[i].invalid());
|
||||||
|
cumulative.push_back(cumulative.back() + _pop[i].fitness() - min_fit);
|
||||||
|
}
|
||||||
|
assert(cumulative.size() == _pop.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** do the selection,
|
/** do the selection,
|
||||||
|
|
@ -72,9 +87,18 @@ public:
|
||||||
{
|
{
|
||||||
if (cumulative.size() == 0) setup(_pop);
|
if (cumulative.size() == 0) setup(_pop);
|
||||||
|
|
||||||
double fortune = rng.uniform() * cumulative.back();
|
double frac = rng.uniform();
|
||||||
typename FitVec::iterator result = std::upper_bound(cumulative.begin(), cumulative.end(), fortune);
|
double fortune = frac * cumulative.back();
|
||||||
return _pop[result - cumulative.begin()];
|
typename FitVec::iterator result
|
||||||
|
= std::upper_bound(cumulative.begin(), cumulative.end(), fortune);
|
||||||
|
|
||||||
|
assert(fortune <= cumulative.back());
|
||||||
|
|
||||||
|
if(result - cumulative.begin() == _pop.size()) {
|
||||||
|
return _pop.back();
|
||||||
|
} else {
|
||||||
|
return _pop[result - cumulative.begin()];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue