New style for MOEO

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@788 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
canape 2007-11-16 11:29:25 +00:00
commit 39709d3d12
103 changed files with 2607 additions and 2521 deletions

View file

@ -1,4 +1,4 @@
/*
/*
* <MOEO.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -57,8 +57,8 @@
*/
template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity >
class MOEO : public EO < MOEOObjectiveVector >
{
public:
{
public:
/** the objective vector type of a solution */
typedef MOEOObjectiveVector ObjectiveVector;
@ -75,32 +75,33 @@ public:
*/
MOEO()
{
// default values for every parameters
objectiveVectorValue = ObjectiveVector();
fitnessValue = Fitness();
diversityValue = Diversity();
// invalidate all
invalidate();
// default values for every parameters
objectiveVectorValue = ObjectiveVector();
fitnessValue = Fitness();
diversityValue = Diversity();
// invalidate all
invalidate();
}
/**
* Virtual dtor
*/
virtual ~MOEO() {};
virtual ~MOEO()
{};
/**
* Returns the objective vector of the current solution
*/
ObjectiveVector objectiveVector() const
{
{
if ( invalidObjectiveVector() )
{
{
throw std::runtime_error("invalid objective vector in MOEO");
}
}
return objectiveVectorValue;
}
}
/**
@ -109,8 +110,8 @@ public:
*/
void objectiveVector(const ObjectiveVector & _objectiveVectorValue)
{
objectiveVectorValue = _objectiveVectorValue;
invalidObjectiveVectorValue = false;
objectiveVectorValue = _objectiveVectorValue;
invalidObjectiveVectorValue = false;
}
@ -119,7 +120,7 @@ public:
*/
void invalidateObjectiveVector()
{
invalidObjectiveVectorValue = true;
invalidObjectiveVectorValue = true;
}
@ -127,22 +128,22 @@ public:
* Returns true if the objective vector is invalid, false otherwise
*/
bool invalidObjectiveVector() const
{
{
return invalidObjectiveVectorValue;
}
}
/**
* Returns the fitness value of the current solution
*/
Fitness fitness() const
{
{
if ( invalidFitness() )
{
{
throw std::runtime_error("invalid fitness in MOEO");
}
}
return fitnessValue;
}
}
/**
@ -151,8 +152,8 @@ public:
*/
void fitness(const Fitness & _fitnessValue)
{
fitnessValue = _fitnessValue;
invalidFitnessValue = false;
fitnessValue = _fitnessValue;
invalidFitnessValue = false;
}
@ -161,7 +162,7 @@ public:
*/
void invalidateFitness()
{
invalidFitnessValue = true;
invalidFitnessValue = true;
}
@ -169,22 +170,22 @@ public:
* Returns true if the fitness value is invalid, false otherwise
*/
bool invalidFitness() const
{
{
return invalidFitnessValue;
}
}
/**
* Returns the diversity value of the current solution
*/
Diversity diversity() const
{
{
if ( invalidDiversity() )
{
{
throw std::runtime_error("invalid diversity in MOEO");
}
}
return diversityValue;
}
}
/**
@ -193,8 +194,8 @@ public:
*/
void diversity(const Diversity & _diversityValue)
{
diversityValue = _diversityValue;
invalidDiversityValue = false;
diversityValue = _diversityValue;
invalidDiversityValue = false;
}
@ -203,7 +204,7 @@ public:
*/
void invalidateDiversity()
{
invalidDiversityValue = true;
invalidDiversityValue = true;
}
@ -211,9 +212,9 @@ public:
* Returns true if the diversity value is invalid, false otherwise
*/
bool invalidDiversity() const
{
{
return invalidDiversityValue;
}
}
/**
@ -221,9 +222,9 @@ public:
*/
void invalidate()
{
invalidateObjectiveVector();
invalidateFitness();
invalidateDiversity();
invalidateObjectiveVector();
invalidateFitness();
invalidateDiversity();
}
@ -231,9 +232,9 @@ public:
* Returns true if the fitness value is invalid, false otherwise
*/
bool invalid() const
{
{
return invalidObjectiveVector();
}
}
/**
@ -243,18 +244,18 @@ public:
* @param _other the other MOEO object to compare with
*/
bool operator<(const MOEO & _other) const
{
{
return objectiveVector() < _other.objectiveVector();
}
}
/**
* Return the class id (the class name as a std::string)
*/
virtual std::string className() const
{
{
return "MOEO";
}
}
/**
@ -262,16 +263,16 @@ public:
* @param _os output stream
*/
virtual void printOn(std::ostream & _os) const
{
{
if ( invalidObjectiveVector() )
{
{
_os << "INVALID\t";
}
}
else
{
{
_os << objectiveVectorValue << '\t';
}
}
}
}
/**
@ -280,23 +281,23 @@ public:
*/
virtual void readFrom(std::istream & _is)
{
std::string objectiveVector_str;
int pos = _is.tellg();
_is >> objectiveVector_str;
if (objectiveVector_str == "INVALID")
std::string objectiveVector_str;
int pos = _is.tellg();
_is >> objectiveVector_str;
if (objectiveVector_str == "INVALID")
{
invalidateObjectiveVector();
invalidateObjectiveVector();
}
else
else
{
invalidObjectiveVectorValue = false;
_is.seekg(pos); // rewind
_is >> objectiveVectorValue;
invalidObjectiveVectorValue = false;
_is.seekg(pos); // rewind
_is >> objectiveVectorValue;
}
}
private:
private:
/** the objective vector of this solution */
ObjectiveVector objectiveVectorValue;
@ -311,6 +312,6 @@ private:
/** true if the diversity value is invalid */
bool invalidDiversityValue;
};
};
#endif /*MOEO_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoBitVector.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -45,8 +45,8 @@
*/
template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity >
class moeoBitVector : public moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool >
{
public:
{
public:
using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: begin;
using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: end;
@ -67,22 +67,22 @@ public:
* Returns the class name as a std::string
*/
virtual std::string className() const
{
return "moeoBitVector";
}
{
return "moeoBitVector";
}
/**
* Writing object
* @param _os output stream
*/
virtual void printOn(std::ostream & _os) const
{
{
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::printOn(_os);
_os << ' ';
_os << size() << ' ';
std::copy(begin(), end(), std::ostream_iterator<bool>(_os));
}
}
/**
@ -91,18 +91,18 @@ public:
*/
virtual void readFrom(std::istream & _is)
{
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
unsigned int s;
_is >> s;
std::string bits;
_is >> bits;
if (_is)
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
unsigned int s;
_is >> s;
std::string bits;
_is >> bits;
if (_is)
{
resize(bits.size());
std::transform(bits.begin(), bits.end(), begin(), std::bind2nd(std::equal_to<char>(), '1'));
resize(bits.size());
std::transform(bits.begin(), bits.end(), begin(), std::bind2nd(std::equal_to<char>(), '1'));
}
}
};
};
#endif /*MOEOBITVECTOR_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoEvalFunc.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -44,6 +44,7 @@
* Functor that evaluates one MOEO by setting all its objective values.
*/
template < class MOEOT >
class moeoEvalFunc : public eoEvalFunc< MOEOT > {};
class moeoEvalFunc : public eoEvalFunc< MOEOT >
{};
#endif /*MOEOEVALFUNC_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoObjectiveVector.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -48,8 +48,8 @@
*/
template < class ObjectiveVectorTraits, class ObjectiveVectorType >
class moeoObjectiveVector : public std::vector < ObjectiveVectorType >
{
public:
{
public:
/** The traits of objective vectors */
typedef ObjectiveVectorTraits Traits;
@ -79,7 +79,7 @@ public:
*/
static void setup(unsigned int _nObjectives, std::vector < bool > & _bObjectives)
{
ObjectiveVectorTraits::setup(_nObjectives, _bObjectives);
ObjectiveVectorTraits::setup(_nObjectives, _bObjectives);
}
@ -88,7 +88,7 @@ public:
*/
static unsigned int nObjectives()
{
return ObjectiveVectorTraits::nObjectives();
return ObjectiveVectorTraits::nObjectives();
}
@ -98,7 +98,7 @@ public:
*/
static bool minimizing(unsigned int _i)
{
return ObjectiveVectorTraits::minimizing(_i);
return ObjectiveVectorTraits::minimizing(_i);
}
@ -108,9 +108,9 @@ public:
*/
static bool maximizing(unsigned int _i)
{
return ObjectiveVectorTraits::maximizing(_i);
return ObjectiveVectorTraits::maximizing(_i);
}
};
};
#endif /*MOEOOBJECTIVEVECTOR_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoObjectiveVectorTraits.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoObjectiveVectorTraits.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -46,8 +46,8 @@
* A traits class for moeoObjectiveVector to specify the number of objectives and which ones have to be minimized or maximized.
*/
class moeoObjectiveVectorTraits
{
public:
{
public:
/**
* Parameters setting
@ -56,20 +56,21 @@ public:
*/
static void setup(unsigned int _nObjectives, std::vector < bool > & _bObjectives)
{
// in case the number of objectives was already set to a different value
if ( nObj && (nObj != _nObjectives) ) {
std::cout << "WARNING\n";
std::cout << "WARNING : the number of objectives are changing\n";
std::cout << "WARNING : Make sure all existing objects are destroyed\n";
std::cout << "WARNING\n";
// in case the number of objectives was already set to a different value
if ( nObj && (nObj != _nObjectives) )
{
std::cout << "WARNING\n";
std::cout << "WARNING : the number of objectives are changing\n";
std::cout << "WARNING : Make sure all existing objects are destroyed\n";
std::cout << "WARNING\n";
}
// number of objectives
nObj = _nObjectives;
// min/max vector
bObj = _bObjectives;
// in case the number of objectives and the min/max vector size don't match
if (nObj != bObj.size())
throw std::runtime_error("Number of objectives and min/max size don't match in moeoObjectiveVectorTraits::setup");
// number of objectives
nObj = _nObjectives;
// min/max vector
bObj = _bObjectives;
// in case the number of objectives and the min/max vector size don't match
if (nObj != bObj.size())
throw std::runtime_error("Number of objectives and min/max size don't match in moeoObjectiveVectorTraits::setup");
}
@ -78,10 +79,10 @@ public:
*/
static unsigned int nObjectives()
{
// in case the number of objectives would not be assigned yet
if (! nObj)
throw std::runtime_error("Number of objectives not assigned in moeoObjectiveVectorTraits");
return nObj;
// in case the number of objectives would not be assigned yet
if (! nObj)
throw std::runtime_error("Number of objectives not assigned in moeoObjectiveVectorTraits");
return nObj;
}
@ -91,10 +92,10 @@ public:
*/
static bool minimizing(unsigned int _i)
{
// in case there would be a wrong index
if (_i >= bObj.size())
throw std::runtime_error("Wrong index in moeoObjectiveVectorTraits");
return bObj[_i];
// in case there would be a wrong index
if (_i >= bObj.size())
throw std::runtime_error("Wrong index in moeoObjectiveVectorTraits");
return bObj[_i];
}
@ -102,8 +103,9 @@ public:
* Returns true if the _ith objective have to be maximized
* @param _i the index
*/
static bool maximizing(unsigned int _i) {
return (! minimizing(_i));
static bool maximizing(unsigned int _i)
{
return (! minimizing(_i));
}
@ -112,17 +114,17 @@ public:
*/
static double tolerance()
{
return 1e-6;
return 1e-6;
}
private:
private:
/** The number of objectives */
static unsigned int nObj;
/** The min/max vector */
static std::vector < bool > bObj;
};
};
#endif /*MOEOOBJECTIVEVECTORTRAITS_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoRealObjectiveVector.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -50,8 +50,8 @@
*/
template < class ObjectiveVectorTraits >
class moeoRealObjectiveVector : public moeoObjectiveVector < ObjectiveVectorTraits, double >
{
public:
{
public:
using moeoObjectiveVector < ObjectiveVectorTraits, double >::size;
using moeoObjectiveVector < ObjectiveVectorTraits, double >::operator[];
@ -77,10 +77,10 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool dominates(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
moeoParetoObjectiveVectorComparator < moeoRealObjectiveVector<ObjectiveVectorTraits> > comparator;
return comparator(_other, *this);
}
}
/**
@ -88,16 +88,16 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool operator==(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
for (unsigned int i=0; i < size(); i++)
{
{
if ( fabs(operator[](i) - _other[i]) > ObjectiveVectorTraits::tolerance() )
{
{
return false;
}
}
}
}
return true;
}
}
/**
@ -105,9 +105,9 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool operator!=(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
return ! operator==(_other);
}
}
/**
@ -116,10 +116,10 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool operator<(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
moeoObjectiveObjectiveVectorComparator < moeoRealObjectiveVector < ObjectiveVectorTraits > > cmp;
return cmp(*this, _other);
}
}
/**
@ -128,9 +128,9 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool operator>(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
return _other < *this;
}
}
/**
@ -139,9 +139,9 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool operator<=(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
return operator==(_other) || operator<(_other);
}
}
/**
@ -150,11 +150,11 @@ public:
* @param _other the other moeoRealObjectiveVector object to compare with
*/
bool operator>=(const moeoRealObjectiveVector < ObjectiveVectorTraits > & _other) const
{
{
return operator==(_other) || operator>(_other);
}
}
};
};
/**
@ -165,11 +165,11 @@ public:
template < class ObjectiveVectorTraits >
std::ostream & operator<<(std::ostream & _os, const moeoRealObjectiveVector < ObjectiveVectorTraits > & _objectiveVector)
{
for (unsigned int i=0; i<_objectiveVector.size(); i++)
for (unsigned int i=0; i<_objectiveVector.size(); i++)
{
_os << _objectiveVector[i] << '\t';
_os << _objectiveVector[i] << '\t';
}
return _os;
return _os;
}
/**
@ -180,12 +180,12 @@ std::ostream & operator<<(std::ostream & _os, const moeoRealObjectiveVector < Ob
template < class ObjectiveVectorTraits >
std::istream & operator>>(std::istream & _is, moeoRealObjectiveVector < ObjectiveVectorTraits > & _objectiveVector)
{
_objectiveVector = moeoRealObjectiveVector < ObjectiveVectorTraits > ();
for (unsigned int i=0; i<_objectiveVector.size(); i++)
_objectiveVector = moeoRealObjectiveVector < ObjectiveVectorTraits > ();
for (unsigned int i=0; i<_objectiveVector.size(); i++)
{
_is >> _objectiveVector[i];
_is >> _objectiveVector[i];
}
return _is;
return _is;
}
#endif /*MOEOREALOBJECTIVEVECTOR_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoRealVector.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -45,8 +45,8 @@
*/
template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity >
class moeoRealVector : public moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, double >
{
public:
{
public:
/**
* Ctor
@ -55,16 +55,16 @@ public:
*/
moeoRealVector(unsigned int _size = 0, double _value = 0.0) : moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, double >(_size, _value)
{}
/**
* Returns the class name as a std::string
*/
virtual std::string className() const
{
return "moeoRealVector";
}
};
{
return "moeoRealVector";
}
};
#endif /*MOEOREALVECTOR_H_*/

View file

@ -1,4 +1,4 @@
/*
/*
* <moeoVector.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -48,8 +48,8 @@
*/
template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
class moeoVector : public MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >, public std::vector < GeneType >
{
public:
{
public:
using MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity > :: invalidate;
using std::vector < GeneType > :: operator[];
@ -70,30 +70,30 @@ public:
* @param _value Initial value of all elements (default is default value of type GeneType)
*/
moeoVector(unsigned int _size = 0, GeneType _value = GeneType()) :
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >(), std::vector<GeneType>(_size, _value)
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >(), std::vector<GeneType>(_size, _value)
{}
/**
* We can't have a Ctor from a std::vector as it would create ambiguity with the copy Ctor.
* @param _v a vector of GeneType
*/
void value(const std::vector < GeneType > & _v)
{
if (_v.size() != size()) // safety check
if (_v.size() != size()) // safety check
{
if (size()) // NOT an initial empty std::vector
if (size()) // NOT an initial empty std::vector
{
std::cout << "Warning: Changing size in moeoVector assignation"<<std::endl;
resize(_v.size());
std::cout << "Warning: Changing size in moeoVector assignation"<<std::endl;
resize(_v.size());
}
else
else
{
throw std::runtime_error("Size not initialized in moeoVector");
throw std::runtime_error("Size not initialized in moeoVector");
}
}
std::copy(_v.begin(), _v.end(), begin());
invalidate();
std::copy(_v.begin(), _v.end(), begin());
invalidate();
}
@ -102,9 +102,9 @@ public:
* @param _moeo the object to compare with
*/
bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo) const
{
{
return MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::operator<(_moeo);
}
}
/**
@ -112,12 +112,12 @@ public:
* @param _os output stream
*/
virtual void printOn(std::ostream & _os) const
{
{
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::printOn(_os);
_os << ' ';
_os << size() << ' ';
std::copy(begin(), end(), std::ostream_iterator<AtomType>(_os, " "));
}
}
/**
@ -126,20 +126,20 @@ public:
*/
virtual void readFrom(std::istream & _is)
{
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
unsigned int sz;
_is >> sz;
resize(sz);
unsigned int i;
for (i = 0; i < sz; ++i)
MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is);
unsigned int sz;
_is >> sz;
resize(sz);
unsigned int i;
for (i = 0; i < sz; ++i)
{
AtomType atom;
_is >> atom;
operator[](i) = atom;
AtomType atom;
_is >> atom;
operator[](i) = atom;
}
}
};
};
/**
@ -150,7 +150,7 @@ public:
template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo1, const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo2)
{
return _moeo1.operator<(_moeo2);
return _moeo1.operator<(_moeo2);
}
@ -162,7 +162,7 @@ bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity
template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType >
bool operator>(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo1, const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo2)
{
return _moeo1.operator>(_moeo2);
return _moeo1.operator>(_moeo2);
}
#endif /*MOEOVECTOR_H_*/