Added Multi-objective fitness monitoring added support for vector<vector<double> > in eoParam
and made eoFileSnapshot type-safe
This commit is contained in:
parent
3449314a28
commit
68904d7650
5 changed files with 142 additions and 15 deletions
|
|
@ -12,3 +12,4 @@
|
|||
#include <utils/eoStat.h>
|
||||
#include <utils/eoScalarFitnessStat.h>
|
||||
#include <utils/eoFDCStat.h>
|
||||
#include <utils/eoMOFitnessStat.h>
|
||||
|
|
|
|||
|
|
@ -160,6 +160,17 @@ public :
|
|||
{ return dirname;}
|
||||
virtual const string baseFileName() // the title for eoGnuPlot
|
||||
{ return filename;}
|
||||
|
||||
/// add checks whether it is a vector of doubles
|
||||
void add(const eoParam& _param)
|
||||
{
|
||||
if (!dynamic_cast<const eoValueParam<vector<double> >*>(&_param))
|
||||
{
|
||||
throw logic_error("eoFileSnapshot: I can only monitor vectors of doubles, sorry");
|
||||
}
|
||||
eoMonitor::add(_param);
|
||||
}
|
||||
|
||||
private :
|
||||
std::string dirname;
|
||||
unsigned frequency;
|
||||
|
|
|
|||
73
eo/src/utils/eoMOFitnessStat.h
Normal file
73
eo/src/utils/eoMOFitnessStat.h
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoFitnessStat.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
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mkeijzer@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoFitnessStat_h
|
||||
#define _eoFitnessStat_h
|
||||
|
||||
#include <utils/eoStat.h>
|
||||
|
||||
/**
|
||||
The fitnesses of a whole population, as a vector
|
||||
*/
|
||||
template <class EOT, class FitT = typename EOT::Fitness>
|
||||
class eoFitnessStat : public eoSortedStat<EOT, vector<FitT> >
|
||||
{
|
||||
public :
|
||||
eoFitnessStat(std::string _description = "AllFitnesses") :
|
||||
eoSortedStat<EOT, vector<FitT> >(vector<FitT>(0), _description) {}
|
||||
|
||||
virtual void operator()(const vector<const EOT*>& _popPters)
|
||||
{
|
||||
value().resize(_popPters.size());
|
||||
for (unsigned i=0; i<_popPters.size(); i++)
|
||||
value()[i] = _popPters[i]->fitness();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** For multi-objective fitness, we need to translate a stat<vector<double> >
|
||||
into a vector<stat>, so each objective gets a seperate stat
|
||||
*/
|
||||
template <class EOT, class PartFitT = double>
|
||||
class eoMOFitnessStat : public eoSortedStat<EOT, vector<PartFitT> >
|
||||
{
|
||||
public :
|
||||
/** Ctor: say what component you want
|
||||
*/
|
||||
eoMOFitnessStat(unsigned _objective, std::string _description = "MO-Fitness") :
|
||||
eoSortedStat<EOT, vector<PartFitT> >(vector<PartFitT>(0), _description),
|
||||
objective(_objective) {}
|
||||
|
||||
virtual void operator()(const vector<const EOT*>& _popPters)
|
||||
{
|
||||
value().resize(_popPters.size());
|
||||
for (unsigned i=0; i<_popPters.size(); i++)
|
||||
value()[i] = _popPters[i]->fitness()[objective];
|
||||
}
|
||||
private:
|
||||
unsigned int objective; // The objective we're storing
|
||||
};
|
||||
#endif
|
||||
|
|
@ -46,7 +46,12 @@ class eoMonitor : public eoF<eoMonitor&>
|
|||
public :
|
||||
|
||||
virtual void lastCall() {}
|
||||
virtual void add(const eoParam& _param) { vec.push_back(&_param); }
|
||||
|
||||
/**
|
||||
Adds a parameter to the monitor. It is virtual so you can do some type checking
|
||||
in derived classes if you must.
|
||||
*/
|
||||
virtual void add(const eoParam& _param) { vec.push_back(&_param); }
|
||||
|
||||
|
||||
protected :
|
||||
|
|
|
|||
|
|
@ -204,6 +204,43 @@ void eoValueParam<std::pair<double, double> >::setValue(std::string _value)
|
|||
is >> repValue.second;
|
||||
}
|
||||
|
||||
/// Because MSVC does not support partial specialization, the vector is a vector of doubles, not a T
|
||||
template <>
|
||||
std::string eoValueParam<std::vector<std::vector<double> > >::getValue(void) const
|
||||
{
|
||||
std::ostrstream os;
|
||||
os << repValue.size() << ' ';
|
||||
for (unsigned i = 0; i < repValue.size(); ++i)
|
||||
{
|
||||
os << repValue[i].size() << ' ';
|
||||
std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
|
||||
}
|
||||
|
||||
os << std::ends;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/// Because MSVC does not support partial specialization, the vector is a vector of doubles, not a T
|
||||
template <>
|
||||
void eoValueParam<std::vector<std::vector<double> > >::setValue(std::string _value)
|
||||
{
|
||||
std::istrstream is(_value.c_str());
|
||||
unsigned sz;
|
||||
is >> sz;
|
||||
repValue.resize(sz);
|
||||
|
||||
for (unsigned i = 0; i < repValue.size(); ++i)
|
||||
{
|
||||
unsigned sz2;
|
||||
is >> sz2;
|
||||
repValue[i].resize(sz2);
|
||||
for (unsigned j = 0; j < sz2; ++j)
|
||||
{
|
||||
is >> repValue[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Because MSVC does not support partial specialization, the vector is a double, not a T
|
||||
template <>
|
||||
std::string eoValueParam<std::vector<double> >::getValue(void) const
|
||||
|
|
|
|||
Reference in a new issue