Adjust code to perform to C++ standard according to gcc-3.4

interpretation... (Have not compiled/checked/changed paradisEO.)

That is, the current code compiles with gcc-3.4 and the checks
(besides t-MGE1bit) all pass.
This commit is contained in:
kuepper 2004-12-23 15:29:07 +00:00
commit 85a326c5e4
35 changed files with 1057 additions and 864 deletions

View file

@ -38,25 +38,29 @@
/**
Average fitness values of a population, where the fitness is
of type eoScalarAssembledFitness. Specify in the constructor,
of type eoScalarAssembledFitness. Specify in the constructor,
for which fitness term (index) the average should be evaluated.
Only values of object where the failed boolean = false is set are counted.
*/
template <class EOT>
template <class EOT>
class eoAssembledFitnessAverageStat : public eoStat<EOT, double>
{
public :
typedef typename EOT::Fitness Fitness;
eoAssembledFitnessAverageStat(unsigned _whichTerm=0, std::string _description = "Average Fitness")
: eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm) {}
virtual void operator()(const eoPop<EOT>& _pop){
if ( whichFitnessTerm >= _pop[0].fitness().size() )
throw std::logic_error("Fitness term requested out of range");
using eoAssembledFitnessAverageStat< EOT >::value;
typedef typename EOT::Fitness Fitness;
eoAssembledFitnessAverageStat(unsigned _whichTerm=0, std::string _description = "Average Fitness")
: eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm)
{}
virtual void operator()(const eoPop<EOT>& _pop) {
if( whichFitnessTerm >= _pop[0].fitness().size() )
throw std::logic_error("Fitness term requested out of range");
double result =0.0;
unsigned count = 0;
for (typename eoPop<EOT>::const_iterator it = _pop.begin(); it != _pop.end(); ++it){
@ -68,7 +72,7 @@ public :
value() = result / (double) count;
}
private:
// Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled
unsigned whichFitnessTerm;
@ -76,29 +80,33 @@ private:
/**
Fitness values of best individuum in a population, where the fitness is
of type eoScalarAssembledFitness. Specify in the constructor,
of type eoScalarAssembledFitness. Specify in the constructor,
for which fitness term (index) the value should be evaluated.
*/
template <class EOT>
template <class EOT>
class eoAssembledFitnessBestStat : public eoStat<EOT, double>
{
public :
typedef typename EOT::Fitness Fitness;
eoAssembledFitnessBestStat(unsigned _whichTerm=0, std::string _description = "Best Fitness")
: eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm) {}
virtual void operator()(const eoPop<EOT>& _pop){
if ( whichFitnessTerm >= _pop[0].fitness().size() )
throw std::logic_error("Fitness term requested out of range");
public:
using eoAssembledFitnessBestStat< EOT >::value;
typedef typename EOT::Fitness Fitness;
eoAssembledFitnessBestStat(unsigned _whichTerm=0, std::string _description = "Best Fitness")
: eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm)
{}
virtual void operator()(const eoPop<EOT>& _pop) {
if( whichFitnessTerm >= _pop[0].fitness().size() )
throw std::logic_error("Fitness term requested out of range");
value() = _pop.best_element().fitness()[whichFitnessTerm];
}
value() = _pop.best_element().fitness()[whichFitnessTerm];
}
private:
// Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled
unsigned whichFitnessTerm;
// Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled
unsigned whichFitnessTerm;
};
#endif

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoFDCStat.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000, 2001
/*
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -38,9 +38,11 @@ so they can be snapshot by some eoGnuplotSnapshot ...
template <class EOT>
class eoFDCStat : public eoStat<EOT, double>
{
public :
/** Ctor without the optimum
*/
public:
using eoFDCStat < EOT>::value;
/** Ctor without the optimum */
eoFDCStat(eoDistance<EOT> & _dist, std::string _description = "FDC") :
eoStat<EOT,double>(0, _description), dist(_dist), boolOpt(false) {}

View file

@ -36,6 +36,9 @@ template <class EOT, class FitT = typename EOT::Fitness>
class eoFitnessStat : public eoSortedStat<EOT, std::vector<FitT> >
{
public :
using eoFitnessStat< EOT, FitT >::value;
eoFitnessStat(std::string _description = "AllFitnesses") :
eoSortedStat<EOT,std::vector<FitT> >(std::vector<FitT>(0), _description) {}
@ -62,7 +65,10 @@ class eoMOFitnessStat : public eoSortedStat<EOT, std::vector<PartFitT> >
#endif
{
public :
public:
using eoMOFitnessStat< EOT, PartFitT >::value;
/** Ctor: say what component you want
*/
eoMOFitnessStat(unsigned _objective, std::string _description = "MO-Fitness") :

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoPopStat.h
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001
/*
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -43,25 +43,28 @@ that can be used to dump to the screen
/** Thanks to MS/VC++, eoParam mechanism is unable to handle std::vectors of stats.
This snippet is a workaround:
This snippet is a workaround:
This class will "print" a whole population into a std::string - that you can later
send to any stream
This is the plain version - see eoPopString for the Sorted version
Note: this Stat should probably be used only within eoStdOutMonitor, and not
Note: this Stat should probably be used only within eoStdOutMonitor, and not
inside an eoFileMonitor, as the eoState construct will work much better there.
*/
template <class EOT>
class eoPopStat : public eoStat<EOT, std::string>
{
public :
/** default Ctor, void std::string by default, as it appears
public:
using eoPopStat< EOT>::value;
/** default Ctor, void std::string by default, as it appears
on the description line once at beginning of evolution. and
is meaningless there. _howMany defaults to 0, that is, the whole
population*/
eoPopStat(unsigned _howMany = 0, std::string _desc ="")
eoPopStat(unsigned _howMany = 0, std::string _desc ="")
: eoStat<EOT, std::string>("", _desc), combien( _howMany) {}
/** Fills the value() of the eoParam with the dump of the population.
Adds a \n before so it does not get mixed up with the rest of the stats
that are written by the monitor it is probably used from.
@ -73,9 +76,9 @@ void operator()(const eoPop<EOT>& _pop)
unsigned howmany=combien?combien:_pop.size();
for (unsigned i = 0; i < howmany; ++i)
{
std::ostringstream os;
std::ostringstream os;
os << _pop[i] << std::endl;
// paranoid:
value() += os.str();
}
@ -91,7 +94,7 @@ void operator()(const eoPop<EOT>& _pop)
{
std::ostrstream os(buffer, 1022); // leave space for emergency terminate
os << _pop[i] << std::endl << std::ends;
// paranoid:
buffer[1022] = '\0';
value() += buffer;
@ -104,40 +107,45 @@ private:
};
/** Thanks to MS/VC++, eoParam mechanism is unable to handle std::vectors of stats.
This snippet is a workaround:
This snippet is a workaround:
This class will "print" a whole population into a std::string - that you can later
send to any stream
This is the Sorted version - see eoPopString for the plain version
Note: this Stat should probably be used only within eoStdOutMonitor, and not
Note: this Stat should probably be used only within eoStdOutMonitor, and not
inside an eoFileMonitor, as the eoState construct will work much better there.
*/
template <class EOT>
class eoSortedPopStat : public eoSortedStat<EOT, std::string>
{
public :
/** default Ctor, void std::string by default, as it appears
* on the description line once at beginning of evolution. and
* is meaningless there _howMany defaults to 0, that is, the whole
* population
*/
eoSortedPopStat(unsigned _howMany = 0, std::string _desc ="") :
eoSortedStat<EOT, std::string>("", _desc) , combien( _howMany) {}
/** Fills the value() of the eoParam with the dump of the population.
Adds a \n before so it does not get mixed up with the rest of the stats
that are written by the monitor it is probably used from.
*/
public:
using eoSortedPopStat< EOT>::value;
/** default Ctor, void std::string by default, as it appears on
the description line once at beginning of evolution. and is
meaningless there _howMany defaults to 0, that is, the whole
population
*/
eoSortedPopStat(unsigned _howMany = 0, std::string _desc ="")
: eoSortedStat<EOT, std::string>("", _desc) , combien( _howMany)
{}
/** Fills the value() of the eoParam with the dump of the
population. Adds a \n before so it does not get mixed up with
the rest of the stats that are written by the monitor it is
probably used from.
*/
#ifdef HAVE_SSTREAM
void operator()(const std::vector<const EOT*>& _pop)
{
value() = ""; // empty
void operator()(const std::vector<const EOT*>& _pop)
{
value() = ""; // empty
unsigned howMany=combien?combien:_pop.size();
for (unsigned i = 0; i < howMany; ++i)
{
std::ostringstream os;
std::ostringstream os;
os << *_pop[i] << std::endl;
// paranoid:
value() += os.str();
}
@ -146,13 +154,13 @@ public :
void operator()(const std::vector<const EOT*>& _pop)
{
char buffer[1023]; // about one K of space per member
value() = ""; // empty
value() = ""; // empty
unsigned howMany=combien?combien:_pop.size();
for (unsigned i = 0; i < howMany; ++i)
{
std::ostrstream os(buffer, 1022); // leave space for emergency terminate
os << *_pop[i] << std::endl << std::ends;
// paranoid:
buffer[1022] = '\0';
value() += buffer;

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoScalarFitnessStat.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000, 2001
/*
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -36,13 +36,16 @@
template <class EOT, class FitT = typename EOT::Fitness>
class eoScalarFitnessStat : public eoSortedStat<EOT, std::vector<double> >
{
public :
eoScalarFitnessStat(std::string _description = "FitnessES",
eoRealVectorBounds & _bounds = eoDummyVectorNoBounds) :
eoSortedStat<EOT, std::vector<double> >(std::vector<double>(0), _description) ,
public:
using eoScalarFitnessStat< EOT, FitT >::value;
eoScalarFitnessStat(std::string _description = "FitnessES",
eoRealVectorBounds & _bounds = eoDummyVectorNoBounds) :
eoSortedStat<EOT, std::vector<double> >(std::vector<double>(0), _description) ,
range(*_bounds[0])
{}
virtual void operator()(const std::vector<const EOT*>& _popPters)
{
value().resize(_popPters.size());

View file

@ -36,8 +36,8 @@
/**
Base class for all statistics that need to be calculated
over the (unsorted) population
(I guess it is not really necessary? MS.
over the (unsorted) population
(I guess it is not really necessary? MS.
Depstd::ends, there might be reasons to have a stat that is not an eoValueParam,
but maybe I'm just kidding myself, MK)
*/
@ -57,11 +57,18 @@ public:
template <class EOT, class T>
class eoStat : public eoValueParam<T>, public eoStatBase<EOT>
{
public :
eoStat(T _value, std::string _description) : eoValueParam<T>(_value, _description) {}
virtual std::string className(void) const { return "eoStat"; }
public:
eoStat(T _value, std::string _description)
: eoValueParam<T>(_value, _description)
{}
virtual std::string className(void) const
{ return "eoStat"; }
};
/**
Base class for statistics calculated over a sorted snapshot of the population
*/
@ -87,12 +94,12 @@ public :
};
/**
Average fitness of a population. Fitness can be:
Average fitness of a population. Fitness can be:
- double
- eoMinimizingFitness or eoMaximizingFitness
- eoMinimizingFitness or eoMaximizingFitness
- eoParetoFitness:
The average of each objective is evaluated.
( For eoScalarFitnessAssembled user eoAssembledFitnessStat classes.)
*/
#ifdef _MSC_VER
@ -102,9 +109,12 @@ template <class EOT> class eoAverageStat : public eoStat<EOT, typename EOT::Fitn
#endif
{
public :
using eoAverageStat< EOT >::value;
typedef typename EOT::Fitness Fitness;
eoAverageStat(std::string _description = "Average Fitness")
eoAverageStat(std::string _description = "Average Fitness")
: eoStat<EOT, Fitness>(Fitness(), _description) {}
static Fitness sumFitness(double _sum, const EOT& _eot){
@ -114,15 +124,15 @@ public :
eoAverageStat(double _value, std::string _desc) : eoStat<EOT, double>(_value, _desc) {}
virtual void operator()(const eoPop<EOT>& _pop){
virtual void operator()(const eoPop<EOT>& _pop){
doit(_pop, Fitness()); // specializations for scalar and std::vector
}
virtual std::string className(void) const { return "eoAverageStat"; }
private :
// Specialization for pareto fitness
// Specialization for pareto fitness
template <class T>
void doit(const eoPop<EOT>& _pop, eoParetoFitness<T>)
{
@ -139,7 +149,7 @@ private :
value()[o] /= _pop.size();
}
}
// Default behavior
template <class T>
void doit(const eoPop<EOT>& _pop, T)
@ -158,10 +168,16 @@ template <class EOT>
class eoSecondMomentStats : public eoStat<EOT, std::pair<double, double> >
{
public :
using eoSecondMomentStats< EOT >::value;
typedef typename EOT::Fitness fitness_type;
typedef std::pair<double, double> SquarePair;
eoSecondMomentStats(std::string _description = "Average & Stdev") : eoStat<EOT, SquarePair>(std::make_pair(0.0,0.0), _description) {}
eoSecondMomentStats(std::string _description = "Average & Stdev")
: eoStat<EOT, SquarePair>(std::make_pair(0.0,0.0), _description)
{}
static SquarePair sumOfSquares(SquarePair _sq, const EOT& _eo)
{
@ -187,7 +203,7 @@ public :
/**
The n_th element fitness in the population (see eoBestFitnessStat)
*/
#ifdef _MSC_VER
#ifdef _MSC_VER
template <class EOT>
class eoNthElementFitnessStat : public eoSortedStat<EOT, EOT::Fitness >
#else
@ -196,9 +212,11 @@ class eoNthElementFitnessStat : public eoSortedStat<EOT, typename EOT::Fitness >
#endif
{
public :
using eoNthElementFitnessStat< EOT >::value;
typedef typename EOT::Fitness Fitness;
eoNthElementFitnessStat(unsigned _whichElement, std::string _description = "nth element fitness")
eoNthElementFitnessStat(unsigned _whichElement, std::string _description = "nth element fitness")
: eoSortedStat<EOT, Fitness>(Fitness(), _description), whichElement(_whichElement) {}
virtual void operator()(const std::vector<const EOT*>& _pop)
@ -211,7 +229,7 @@ public :
virtual std::string className(void) const { return "eoNthElementFitnessStat"; }
private :
struct CmpFitness
{
CmpFitness(unsigned _whichElement, bool _maxim) : whichElement(_whichElement), maxim(_maxim) {}
@ -246,7 +264,7 @@ private :
value()[o] = (*nth)->fitness()[o];
}
}
// for everything else
template <class T>
void doit(const std::vector<const EOT*>& _pop, T)
@ -263,7 +281,7 @@ private :
But then again, if another stat needs sorted fitness anyway, getting the best
out would be very fast.
MK - 09/01/03
template <class EOT>
class eoBestFitnessStat : public eoStat<EOT, typename EOT::Fitness >
{
@ -282,9 +300,9 @@ public :
*/
/**
Best fitness of a population. Fitness can be:
Best fitness of a population. Fitness can be:
- double
- eoMinimizingFitness or eoMaximizingFitness
- eoMinimizingFitness or eoMaximizingFitness
- eoParetoFitness:
( For eoScalarFitnessAssembled look at eoAssembledFitnessStat )
@ -298,17 +316,23 @@ template <class EOT>
class eoBestFitnessStat : public eoStat<EOT, typename EOT::Fitness>
#endif
{
public :
public:
using eoBestFitnessStat< EOT >::value;
typedef typename EOT::Fitness Fitness;
eoBestFitnessStat(std::string _description = "Best ")
: eoStat<EOT, Fitness>(Fitness(), _description) {}
void operator()(const eoPop<EOT>& _pop){
doit(_pop, Fitness() ); // specializations for scalar and std::vector
eoBestFitnessStat(std::string _description = "Best ")
: eoStat<EOT, Fitness>(Fitness(), _description)
{}
void operator()(const eoPop<EOT>& _pop) {
doit(_pop, Fitness() ); // specializations for scalar and std::vector
}
virtual std::string className(void) const { return "eoBestFitnessStat"; }
virtual std::string className(void) const { return "eoBestFitnessStat"; }
private :
struct CmpFitness
@ -340,7 +364,7 @@ private :
value()[o] = it->fitness()[o];
}
}
// default
template<class T>
void doit(const eoPop<EOT>& _pop, T)
@ -353,8 +377,13 @@ private :
template <class EOT>
class eoDistanceStat : public eoStat<EOT, double>
{
public :
eoDistanceStat(std::string _name = "distance") : eoStat<EOT, double>(0.0, _name) {}
public:
using eoDistanceStat< EOT >::value;
eoDistanceStat(std::string _name = "distance")
: eoStat<EOT, double>(0.0, _name)
{}
template <class T>
double distance(T a, T b)
@ -405,7 +434,7 @@ public :
virtual void operator()(const eoPop<EOT>& _pop)
{
SquarePair result = std::accumulate(pop.begin(), pop.end(), std::make_pair(0.0, 0.0), eoSecondMomentStats::sumOfSquares);
double n = pop.size();
value() = sqrt( (result.second - (result.first / n)) / (n - 1.0)); // stdev
}

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoUpdater.h
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2000
/*
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -48,7 +48,7 @@ public:
*/
template <class T>
class eoIncrementor : public eoUpdater
{public :
{public :
/** Default Ctor - requires a reference to the thing to increment */
eoIncrementor(T& _counter, T _stepsize = 1) : counter(_counter), stepsize(_stepsize) {}
@ -70,23 +70,26 @@ private:
template <class T>
class eoIncrementorParam : public eoUpdater, public eoValueParam<T>
{
public :
public:
using eoIncrementorParam< T >::value;
/** Default Ctor : a name and optionally an increment*/
eoIncrementorParam( std::string _name, T _stepsize = 1) :
eoIncrementorParam( std::string _name, T _stepsize = 1) :
eoValueParam<T>(T(0), _name), stepsize(_stepsize) {}
/** Ctor with a name and non-zero initial value
/** Ctor with a name and non-zero initial value
* and mandatory stepSize to remove ambiguity
*/
eoIncrementorParam( std::string _name, T _countValue, T _stepsize) :
eoIncrementorParam( std::string _name, T _countValue, T _stepsize) :
eoValueParam<T>(_countValue, _name), stepsize(_stepsize) {}
/** Simply increments */
virtual void operator()()
{
value() += stepsize;
value() += stepsize;
}
virtual std::string className(void) const { return "eoIncrementorParam"; }
private:
@ -101,7 +104,7 @@ private:
class eoTimedStateSaver : public eoUpdater
{
public :
eoTimedStateSaver(time_t _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") : state(_state),
eoTimedStateSaver(time_t _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav") : state(_state),
interval(_interval), last_time(time(0)), first_time(time(0)),
prefix(_prefix), extension(_extension) {}
@ -124,12 +127,12 @@ private :
class eoCountedStateSaver : public eoUpdater
{
public :
eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix, bool _saveOnLastCall, std::string _extension = "sav", unsigned _counter = 0)
: state(_state), interval(_interval), counter(_counter),
eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix, bool _saveOnLastCall, std::string _extension = "sav", unsigned _counter = 0)
: state(_state), interval(_interval), counter(_counter),
saveOnLastCall(_saveOnLastCall),
prefix(_prefix), extension(_extension) {}
eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav", unsigned _counter = 0)
eoCountedStateSaver(unsigned _interval, const eoState& _state, std::string _prefix = "state", std::string _extension = "sav", unsigned _counter = 0)
: state(_state), interval(_interval), counter(_counter),
saveOnLastCall(true),
prefix(_prefix), extension(_extension) {}
@ -145,7 +148,7 @@ private :
const unsigned interval;
unsigned counter;
bool saveOnLastCall;
const std::string prefix;
const std::string extension;
};

View file

@ -9,7 +9,7 @@
template <class EOT>
const EOT& select(const eoPop<EOT>& pop, params, eoRng& gen = rng);
template <class EOT>
EOT& select(eoPop<EOT>& pop, params, eoRng& gen = rng);
@ -17,7 +17,7 @@
and stochastic_tournament (at the moment).
(c) Maarten Keijzer (mak@dhi.dk) and GeNeura Team, 1999, 2000
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -50,32 +50,32 @@ template <class EOT>
bool minimizing_fitness()
{
EOT eo1; // Assuming people don't do anything fancy in the default constructor!
EOT eo2;
EOT eo2;
/* Dear user, when the two line below do not compile you are most
/* Dear user, when the two line below do not compile you are most
likely not working with scalar fitness values. In that case we're sorry
but you cannot use lottery or roulette_wheel selection...
*/
#ifdef _MSC_VER
eo1.fitness( EOT::Fitness(0.0) );
eo2.fitness( EOT::Fitness(1.0) );
eo1.fitness( EOT::Fitness(0.0) );
eo2.fitness( EOT::Fitness(1.0) );
#else
eo1.fitness( typename EOT::Fitness(0.0) ); // tried to cast it to an EOT::Fitness, but for some reason GNU barfs on this
eo2.fitness( typename EOT::Fitness(1.0) );
eo1.fitness( typename EOT::Fitness(0.0) ); // tried to cast it to an EOT::Fitness, but for some reason GNU barfs on this
eo2.fitness( typename EOT::Fitness(1.0) );
#endif
return eo2 < eo1; // check whether we have a minimizing fitness
};
inline double scale_fitness(const std::pair<double, double>& _minmax, double _value)
inline double scale_fitness(const std::pair<double, double>& _minmax, double _value)
{
if (_minmax.first == _minmax.second)
{
return 0.0; // no differences in fitness, population converged!
}
// else
return (_value - _minmax.first) / (_minmax.second - _minmax.first);
}
@ -104,6 +104,8 @@ double sum_fitness(const eoPop<EOT>& _pop)
template <class EOT>
double sum_fitness(const eoPop<EOT>& _pop, std::pair<double, double>& _minmax)
{
double rawTotal, scaledTotal;
typename eoPop<EOT>::const_iterator it = _pop.begin();
_minmax.first = it->fitness();
@ -112,13 +114,13 @@ double sum_fitness(const eoPop<EOT>& _pop, std::pair<double, double>& _minmax)
for(; it != _pop.end(); ++it)
{
double v = static_cast<double>(it->fitness());
_minmax.first = std::min(_minmax.first, v);
_minmax.second = max(_minmax.second, v);
_minmax.second = std::max(_minmax.second, v);
rawTotal += v;
}
if (minimizing_fitness<EOT>())
{
std::swap(_minmax.first, _minmax.second);
@ -129,10 +131,12 @@ double sum_fitness(const eoPop<EOT>& _pop, std::pair<double, double>& _minmax)
// unfortunately a second loop is neccessary to scale the fitness
for (it = _pop.begin(); it != _pop.end(); ++it)
{
double v = scale_fitness(static_cast<double>(it->fitness()));
double v = scale_fitness(_minmax, static_cast<double>(it->fitness()));
scaledTotal += v;
}
return scaledTotal;
}
template <class It>
@ -143,14 +147,14 @@ It roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng)
if (roulette == 0.0) // covers the case where total==0.0
return _begin + _gen.random(_end - _begin); // uniform choice
It i = _begin;
while (roulette > 0.0)
{
roulette -= static_cast<double>(*(i++));
}
return --i;
}
@ -158,43 +162,43 @@ template <class EOT>
const EOT& roulette_wheel(const eoPop<EOT>& _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
typename eoPop<EOT>::const_iterator i = _pop.begin();
while (roulette > 0.0)
{
roulette -= static_cast<double>((i++)->fitness());
}
return *--i;
}
}
template <class EOT>
EOT& roulette_wheel(eoPop<EOT>& _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
typename eoPop<EOT>::iterator i = _pop.begin();
while (roulette > 0.0)
{
roulette -= static_cast<double>((i++)->fitness());
}
return *--i;
}
}
template <class It>
It deterministic_tournament(It _begin, It _end, unsigned _t_size, eoRng& _gen = rng)
{
It best = _begin + _gen.random(_end - _begin);
It best = _begin + _gen.random(_end - _begin);
for (unsigned i = 0; i < _t_size - 1; ++i)
{
It competitor = _begin + _gen.random(_end - _begin);
@ -223,8 +227,8 @@ EOT& deterministic_tournament(eoPop<EOT>& _pop, unsigned _t_size, eoRng& _gen =
template <class It>
It inverse_deterministic_tournament(It _begin, It _end, unsigned _t_size, eoRng& _gen = rng)
{
It worst = _begin + _gen.random(_end - _begin);
It worst = _begin + _gen.random(_end - _begin);
for (unsigned i = 1; i < _t_size; ++i)
{
It competitor = _begin + _gen.random(_end - _begin);
@ -257,27 +261,27 @@ EOT& inverse_deterministic_tournament(eoPop<EOT>& _pop, unsigned _t_size, eoRng&
}
template <class It>
It stochastic_tournament(It _begin, It _end, double _t_rate, eoRng& _gen = rng)
It stochastic_tournament(It _begin, It _end, double _t_rate, eoRng& _gen = rng)
{
It i1 = _begin + _gen.random(_end - _begin);
It i2 = _begin + _gen.random(_end - _begin);
bool return_better = _gen.flip(_t_rate);
if (*i1 < *i2)
if (*i1 < *i2)
{
if (return_better) return i2;
// else
return i1;
}
else
else
{
if (return_better) return i1;
// else
}
// else
return i2;
}
@ -294,27 +298,27 @@ EOT& stochastic_tournament(eoPop<EOT>& _pop, double _t_rate, eoRng& _gen = rng)
}
template <class It>
It inverse_stochastic_tournament(It _begin, It _end, double _t_rate, eoRng& _gen = rng)
It inverse_stochastic_tournament(It _begin, It _end, double _t_rate, eoRng& _gen = rng)
{
It i1 = _begin + _gen.random(_end - _begin);
It i2 = _begin + _gen.random(_end - _begin);
bool return_worse = _gen.flip(_t_rate);
if (*i1 < *i2)
if (*i1 < *i2)
{
if (return_worse) return i1;
// else
return i2;
}
else
else
{
if (return_worse) return i2;
// else
}
// else
return i1;
}