EO: added overloaded printing of fitness (for vectors and pairs)
Various bugs and ommissions in eoPerf2Worth and eoSelectFromWorth
This commit is contained in:
parent
492899bfb2
commit
4222e64829
4 changed files with 144 additions and 46 deletions
52
eo/src/EO.h
52
eo/src/EO.h
|
|
@ -31,6 +31,54 @@
|
||||||
#include <eoObject.h> // eoObject
|
#include <eoObject.h> // eoObject
|
||||||
#include <eoPersistent.h> // eoPersistent
|
#include <eoPersistent.h> // eoPersistent
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
ostream& print_fitness(ostream& _os, const std::vector<T>& vec)
|
||||||
|
{
|
||||||
|
_os << vec.size() << ' ';
|
||||||
|
std::copy(vec.begin(), vec.end(), ostream_iterator<T>(_os));
|
||||||
|
return _os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class U>
|
||||||
|
ostream& print_fitness(ostream& _os, const std::pair<T, U>& pair)
|
||||||
|
{
|
||||||
|
return _os << pair.first << ' ' << pair.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
ostream& print_fitness(ostream& _os, const T& t)
|
||||||
|
{ // general, try operator<<
|
||||||
|
return _os << t;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
istream& read_fitness(istream& _is, vector<T>& vec)
|
||||||
|
{
|
||||||
|
unsigned sz;
|
||||||
|
_is >> sz;
|
||||||
|
vec.resize(sz);
|
||||||
|
for (unsigned i = 0; i < vec.size(); ++i)
|
||||||
|
{
|
||||||
|
_is >> vec[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return _is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class U>
|
||||||
|
istream& read_fitness(istream& _is, pair<T, U>& pair)
|
||||||
|
{
|
||||||
|
_is >> pair.first;
|
||||||
|
_is >> pair.second;
|
||||||
|
return _is;
|
||||||
|
}
|
||||||
|
template <class T>
|
||||||
|
istream& read_fitness(istream& _is, T& t)
|
||||||
|
{
|
||||||
|
_is >> t;
|
||||||
|
return _is;
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/** EO is a base class for evolvable objects, that is, the subjects of
|
/** EO is a base class for evolvable objects, that is, the subjects of
|
||||||
evolution. EOs have only got a fitness, which at the same time needs to be
|
evolution. EOs have only got a fitness, which at the same time needs to be
|
||||||
|
|
@ -103,7 +151,7 @@ public:
|
||||||
* @throw runtime_exception If a valid object can't be read.
|
* @throw runtime_exception If a valid object can't be read.
|
||||||
*/
|
*/
|
||||||
virtual void readFrom(istream& _is) {
|
virtual void readFrom(istream& _is) {
|
||||||
_is >> repFitness;
|
read_fitness(_is, repFitness);
|
||||||
if (_is)
|
if (_is)
|
||||||
invalidFitness = false;
|
invalidFitness = false;
|
||||||
else
|
else
|
||||||
|
|
@ -118,7 +166,7 @@ public:
|
||||||
if (invalid())
|
if (invalid())
|
||||||
_os << "INVALID ";
|
_os << "INVALID ";
|
||||||
else
|
else
|
||||||
_os << repFitness << ' '; // trailing space to make reading in that much easier
|
print_fitness(_os, repFitness) << ' '; // trailing space to make reading in that much easier
|
||||||
}
|
}
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,9 @@
|
||||||
// a simple transformer
|
// a simple transformer
|
||||||
#include <eoSGATransform.h>
|
#include <eoSGATransform.h>
|
||||||
|
|
||||||
|
// Perf2Worth
|
||||||
|
#include <eoParetoRanking.h>
|
||||||
|
|
||||||
// Algorithms
|
// Algorithms
|
||||||
#include <eoEasyEA.h>
|
#include <eoEasyEA.h>
|
||||||
#include <eoSGA.h>
|
#include <eoSGA.h>
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,8 @@ template <class EOT, class WorthT = typename EOT::Fitness>
|
||||||
class eoPerf2Worth : public eoStat<EOT, vector<WorthT> >
|
class eoPerf2Worth : public eoStat<EOT, vector<WorthT> >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
eoPerf2Worth():eoStat<EOT, vector<WorthT> >(vector<WorthT>(0),
|
eoPerf2Worth(std::string _description = "Worths"):eoStat<EOT, vector<WorthT> >(vector<WorthT>(0),
|
||||||
"Worths") {}
|
_description) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -60,10 +60,32 @@ public:
|
||||||
virtual void setup(const eoPop<EOT>& _pop)
|
virtual void setup(const eoPop<EOT>& _pop)
|
||||||
{
|
{
|
||||||
perf2Worth(_pop);
|
perf2Worth(_pop);
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
fitness.resize(_pop.size());
|
||||||
|
for (unsigned i = 0; i < _pop.size(); ++i)
|
||||||
|
{
|
||||||
|
fitness[i] = _pop[i].fitness();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
eoPerf2Worth<EOT, WorthType> & perf2Worth;
|
eoPerf2Worth<EOT, WorthType> & perf2Worth;
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
vector<typename EOT::Fitness> fitness; // for debugging purposes, to check that the perf2worth and pop are in sync
|
||||||
|
|
||||||
|
void check_sync(unsigned index, const EOT& _eo)
|
||||||
|
{
|
||||||
|
if (fitness[index] != _eo.fitness())
|
||||||
|
{
|
||||||
|
throw runtime_error("eoSelectFromWorth: fitnesses are not in sync");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -91,7 +113,15 @@ public:
|
||||||
worthIterator it = deterministic_tournament(
|
worthIterator it = deterministic_tournament(
|
||||||
perf2Worth.value().begin(),
|
perf2Worth.value().begin(),
|
||||||
perf2Worth.value().end(), tSize);
|
perf2Worth.value().end(), tSize);
|
||||||
return _pop[it-perf2Worth.value().begin()];
|
|
||||||
|
unsigned index = it - perf2Worth.value().begin();
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
// check whether the stuff is still in sync
|
||||||
|
check_sync(index, _pop[index]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return _pop[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -99,7 +129,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
/** An instance of eoSelectPerf2Worth that does selection from the Worthes
|
/** An instance of eoSelectPerf2Worth that does selection from the Worthes
|
||||||
* using a ... determinisitic tournament, yes!
|
* using a ... stochastic tournament, yes!
|
||||||
*/
|
*/
|
||||||
template <class EOT, class WorthT = double>
|
template <class EOT, class WorthT = double>
|
||||||
class eoStochTournamentWorthSelect : public eoSelectFromWorth<EOT, WorthT>
|
class eoStochTournamentWorthSelect : public eoSelectFromWorth<EOT, WorthT>
|
||||||
|
|
@ -118,10 +148,18 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual const EOT& operator()(const eoPop<EOT>& _pop)
|
virtual const EOT& operator()(const eoPop<EOT>& _pop)
|
||||||
{
|
{
|
||||||
worthIterator it = deterministic_tournament(
|
worthIterator it = stochastic_tournament(
|
||||||
perf2Worth.value().begin(),
|
perf2Worth.value().begin(),
|
||||||
perf2Worth.value().end(), tRate);
|
perf2Worth.value().end(), tRate);
|
||||||
return _pop[it-perf2Worth.value().begin()];
|
|
||||||
|
unsigned index = it - perf2Worth.value().begin();
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
// check whether the stuff is still in sync
|
||||||
|
check_sync(index, _pop[index]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return _pop[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -147,7 +185,7 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void setup(const eoPop<EOT>& _pop)
|
virtual void setup(const eoPop<EOT>& _pop)
|
||||||
{
|
{
|
||||||
perf2Worth(_pop);
|
eoSelectFromWorth<EOT, WorthT>::setup(_pop);
|
||||||
total = 0.0;
|
total = 0.0;
|
||||||
for (worthIterator it = perf2Worth.value().begin();
|
for (worthIterator it = perf2Worth.value().begin();
|
||||||
it<perf2Worth.value().end(); ++it)
|
it<perf2Worth.value().end(); ++it)
|
||||||
|
|
@ -164,6 +202,15 @@ public:
|
||||||
perf2Worth.value().begin(),
|
perf2Worth.value().begin(),
|
||||||
perf2Worth.value().end(),
|
perf2Worth.value().end(),
|
||||||
total);
|
total);
|
||||||
|
|
||||||
|
unsigned index = it - perf2Worth.value().begin();
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
// check whether the stuff is still in sync
|
||||||
|
check_sync(index, _pop[index]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return _pop[index];
|
||||||
return _pop[it-perf2Worth.value().begin()];
|
return _pop[it-perf2Worth.value().begin()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue