Added an eoSortedStatBase for more efficient statistic calculations

updated a few makefiles to include the html and latex docs with the
distribution.
This commit is contained in:
maartenkeijzer 2000-10-06 10:41:38 +00:00
commit 1d0794c46a
4 changed files with 66 additions and 18 deletions

View file

@ -6,4 +6,12 @@
############################################################################### ###############################################################################
EXTRA_DIST = eo.cfg foot.html EXTRA_DIST = eo.cfg foot.html html/* latex/*
all :
doc : eo.cfg
doxygen eo.cfg
clean :
rm -rf html latex man

View file

@ -27,6 +27,7 @@
#include <vector> #include <vector>
#include <strstream> #include <strstream>
#include <algorithm>
// EO includes // EO includes
#include <eoOp.h> // for eoInit #include <eoOp.h> // for eoInit
@ -90,6 +91,15 @@ class eoPop: public vector<EOT>, public eoObject, public eoPersistent
/// ///
~eoPop() {}; ~eoPop() {};
/// helper struct for getting a pointer
struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}};
/// helper struct for comparing on pointers
struct Cmp {
bool operator()(const EO<Fitness>* a, const EO<Fitness>* b) const
{ return b->operator<(*a); }
};
/** /**
sort the population. Use this member to sort in order sort the population. Use this member to sort in order
of descending Fitness, so the first individual is the best! of descending Fitness, so the first individual is the best!
@ -99,6 +109,15 @@ class eoPop: public vector<EOT>, public eoObject, public eoPersistent
std::sort(begin(), end(), greater<EO<Fitness> >()); std::sort(begin(), end(), greater<EO<Fitness> >());
} }
void sort(vector<const EOT*>& result) const
{
result.resize(size());
std::transform(begin(), end(), result.begin(), Ref());
std::sort(result.begin(), result.end(), Cmp());
}
/** /**
slightly faster algorithm than sort to find all individuals that are better slightly faster algorithm than sort to find all individuals that are better
than the nth individual than the nth individual
@ -123,11 +142,6 @@ class eoPop: public vector<EOT>, public eoObject, public eoPersistent
return *it; return *it;
} }
struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}};
struct Cmp {
bool operator()(const EO<Fitness>* a, const EO<Fitness>* b) const
{ return b->operator<(*a); }
};
/// const nth_element function, returns pointers to sorted individuals /// const nth_element function, returns pointers to sorted individuals
void nth_element(int which, vector<const EOT*>& result) const void nth_element(int which, vector<const EOT*>& result) const
{ {

View file

@ -30,6 +30,7 @@
#include <eoContinue.h> #include <eoContinue.h>
template <class EOT> class eoStatBase; template <class EOT> class eoStatBase;
template <class EOT> class eoSortedStatBase;
class eoMonitor; class eoMonitor;
class eoUpdater; class eoUpdater;
@ -42,6 +43,7 @@ public :
bool operator()(const eoPop<EOT>& _pop); bool operator()(const eoPop<EOT>& _pop);
void add(eoSortedStatBase<EOT>& _stat) { sorted.push_back(&_stat); }
void add(eoStatBase<EOT>& _stat) { stats.push_back(&_stat); } void add(eoStatBase<EOT>& _stat) { stats.push_back(&_stat); }
void add(eoMonitor& _mon) { monitors.push_back(&_mon); } void add(eoMonitor& _mon) { monitors.push_back(&_mon); }
void add(eoUpdater& _upd) { updaters.push_back(&_upd); } void add(eoUpdater& _upd) { updaters.push_back(&_upd); }
@ -51,6 +53,7 @@ public :
private : private :
eoContinue<EOT>& cont; eoContinue<EOT>& cont;
std::vector<eoSortedStatBase<EOT>*> sorted;
std::vector<eoStatBase<EOT>*> stats; std::vector<eoStatBase<EOT>*> stats;
std::vector<eoMonitor*> monitors; std::vector<eoMonitor*> monitors;
std::vector<eoUpdater*> updaters; std::vector<eoUpdater*> updaters;
@ -60,6 +63,18 @@ template <class EOT>
bool eoCheckPoint<EOT>::operator()(const eoPop<EOT>& _pop) bool eoCheckPoint<EOT>::operator()(const eoPop<EOT>& _pop)
{ {
unsigned i; unsigned i;
if (!sorted.empty())
{
vector<const EOT*> sorted_pop;
_pop.sort(sorted_pop);
for (i = 0; i < sorted.size(); ++i)
{
(*sorted[i])(sorted_pop);
}
}
for (i = 0; i < stats.size(); ++i) for (i = 0; i < stats.size(); ++i)
(*stats[i])(_pop); (*stats[i])(_pop);

View file

@ -31,17 +31,13 @@
#include <utils/eoParam.h> #include <utils/eoParam.h>
#include <eoPop.h> #include <eoPop.h>
/**
Base class for all statistics that need to be calculated
over the (unsorted) population
*/
template <class EOT> template <class EOT>
class eoStatBase : public eoUnaryFunctor<void, const eoPop<EOT>&> class eoStatBase : public eoUnaryFunctor<void, const eoPop<EOT>&>
{ {};
public :
virtual ~eoStatBase(){}
/**
calculate some statistic on the population
*/
virtual void operator()(const eoPop<EOT>& _pop) = 0;
};
template <class EOT, class T> template <class EOT, class T>
class eoStat : public eoValueParam<T>, public eoStatBase<EOT> class eoStat : public eoValueParam<T>, public eoStatBase<EOT>
@ -50,6 +46,21 @@ public :
eoStat(T _value, std::string _description) : eoValueParam<T>(_value, _description) {} eoStat(T _value, std::string _description) : eoValueParam<T>(_value, _description) {}
}; };
/**
Base class for statistics calculated over a sorted snapshot of the population
*/
template <class EOT>
class eoSortedStatBase : public eoUnaryFunctor<void, const vector<const EOT*>&>
{
};
template <class EOT, class ParamType>
class eoSortedStat : public eoSortedStatBase<EOT>, public eoValueParam<ParamType>
{
public :
eoSortedStat(ParamType _value, std::string _desc) : eoValueParam<ParamType>(_value, _desc) {}
};
#include <numeric> #include <numeric>
/** /**
@ -104,19 +115,19 @@ public :
}; };
template <class EOT> template <class EOT>
class eoNthElementFitnessStat : public eoStat<EOT, typename EOT::Fitness > class eoNthElementFitnessStat : public eoSortedStat<EOT, typename EOT::Fitness >
{ {
public : public :
typedef typename EOT::Fitness Fitness; typedef typename EOT::Fitness Fitness;
eoNthElementFitnessStat(int _which, std::string _description = "nth element fitness") : eoStat<EOT, Fitness>(Fitness(), _description), which(_which) {} eoNthElementFitnessStat(int _which, std::string _description = "nth element fitness") : eoSortedStat<EOT, Fitness>(Fitness(), _description), which(_which) {}
virtual void operator()(const eoPop<EOT>& _pop) virtual void operator()(const vector<const EOT*>& _pop)
{ {
if (which > _pop.size()) if (which > _pop.size())
throw logic_error("fitness requested of element outside of pop"); throw logic_error("fitness requested of element outside of pop");
value() = _pop.nth_element_fitness(which); value() = _pop[which]->fitness();
} }
private : private :