From 086b51c370e3d697db00904a1abb8f3a9e6eb654 Mon Sep 17 00:00:00 2001 From: liefooga Date: Fri, 23 Feb 2007 12:49:11 +0000 Subject: [PATCH] 23/02/07 modifications git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@184 331e1502-861f-0410-8da2-ba01fb791d7f --- branches/paradiseo-moeo-1.0/src/MOEO.h | 339 +++++++++--------- branches/paradiseo-moeo-1.0/src/moeo | 3 + branches/paradiseo-moeo-1.0/src/moeoArchive.h | 203 +++++------ .../src/moeoArchiveFitnessSavingUpdater.h | 31 +- .../src/moeoArchiveUpdater.h | 28 +- .../paradiseo-moeo-1.0/src/moeoCombinedLS.h | 52 +-- .../paradiseo-moeo-1.0/src/moeoComparator.h | 160 ++------- .../src/moeoDiversityAssignment.h | 42 +-- branches/paradiseo-moeo-1.0/src/moeoEA.h | 9 +- .../paradiseo-moeo-1.0/src/moeoEvalFunc.h | 7 +- ...FastNonDominatedSortingFitnessAssignment.h | 243 ++++++------- .../src/moeoFitnessAssignment.h | 45 +-- .../src/moeoGenerationalReplacement.h | 11 +- .../paradiseo-moeo-1.0/src/moeoHybridLS.h | 50 ++- branches/paradiseo-moeo-1.0/src/moeoLS.h | 9 +- .../src/moeoObjectiveVector.h | 273 ++++++-------- .../src/moeoObjectiveVectorComparator.h | 105 +++--- .../src/moeoObjectiveVectorTraits.h | 114 +++--- .../paradiseo-moeo-1.0/src/moeoReplacement.h | 9 +- .../paradiseo-moeo-1.0/src/moeoSelectOne.h | 7 +- 20 files changed, 771 insertions(+), 969 deletions(-) diff --git a/branches/paradiseo-moeo-1.0/src/MOEO.h b/branches/paradiseo-moeo-1.0/src/MOEO.h index 1ad4fc6fb..a191aa929 100644 --- a/branches/paradiseo-moeo-1.0/src/MOEO.h +++ b/branches/paradiseo-moeo-1.0/src/MOEO.h @@ -31,253 +31,250 @@ * operator '<' et '>' ??? * !!!!!!!!!!!!!!!!!!!!!!! */ -template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity > class MOEO:public EO < - MOEOFitness - > +template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity > +class MOEO : public EO < MOEOFitness > { public: /** the objective vector type of a solution */ - typedef MOEOObjectiveVector ObjectiveVector; - + typedef MOEOObjectiveVector ObjectiveVector; + /** the fitness type of a solution */ - typedef MOEOFitness Fitness; - + typedef MOEOFitness Fitness; + /** the diversity type of a solution */ - typedef MOEODiversity Diversity; - - + typedef MOEODiversity Diversity; + + /** * Ctor */ - MOEO () - { - // default values for every parameters - objectiveVectorValue = ObjectiveVector (); - fitnessValue = Fitness (); - diversityValue = Diversity (); - // invalidate all - invalidate (); - } - - + MOEO() + { + // 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"); - } - return objectiveVectorValue; - } - - + ObjectiveVector objectiveVector() const + { + if ( invalidObjectiveVector() ) + { + throw std::runtime_error("invalid objective vector"); + } + return objectiveVectorValue; + } + + /** * Sets the objective vector of the current solution * @param _objectiveVectorValue the new objective vector */ - void objectiveVector (const ObjectiveVector & _objectiveVectorValue) - { - objectiveVectorValue = _objectiveVectorValue; - invalidObjectiveVectorValue = false; - } - - + void objectiveVector(const ObjectiveVector & _objectiveVectorValue) + { + objectiveVectorValue = _objectiveVectorValue; + invalidObjectiveVectorValue = false; + } + + /** * Sets the objective vector as invalid */ - void invalidateObjectiveVector () - { - invalidObjectiveVectorValue = true; - } - - + void invalidateObjectiveVector() + { + invalidObjectiveVectorValue = true; + } + + /** * Returns true if the objective vector is invalid, false otherwise */ - bool invalidObjectiveVector () const - { - return invalidObjectiveVectorValue; - } - - + bool invalidObjectiveVector() const + { + return invalidObjectiveVectorValue; + } + + /** * Returns the fitness value of the current solution */ - Fitness fitness () const - { - if (invalidFitness ()) - { - throw std::runtime_error ("invalid fitness"); - } - return fitnessValue; - } - - + Fitness fitness() const + { + if ( invalidFitness() ) + { + throw std::runtime_error("invalid fitness"); + } + return fitnessValue; + } + + /** * Sets the fitness value of the current solution * @param _fitnessValue the new fitness value */ - void fitness (const Fitness & _fitnessValue) - { - fitnessValue = _fitnessValue; - invalidFitnessValue = false; - } - - + void fitness(const Fitness & _fitnessValue) + { + fitnessValue = _fitnessValue; + invalidFitnessValue = false; + } + + /** * Sets the fitness value as invalid */ - void invalidateFitness () - { - invalidFitnessValue = true; - } - - + void invalidateFitness() + { + invalidFitnessValue = true; + } + + /** * Returns true if the fitness value is invalid, false otherwise */ - bool invalidFitness () const - { - return invalidFitnessValue; - } - - + bool invalidFitness() const + { + return invalidFitnessValue; + } + + /** * Returns the diversity value of the current solution */ - Diversity diversity () const - { - if (invalidDiversity ()) - { - throw std::runtime_error ("invalid diversity"); - } - return diversityValue; - } - - + Diversity diversity() const + { + if ( invalidDiversity() ) + { + throw std::runtime_error("invalid diversity"); + } + return diversityValue; + } + + /** * Sets the diversity value of the current solution * @param _diversityValue the new diversity value */ - void diversity (const Diversity & _diversityValue) - { - diversityValue = _diversityValue; - invalidDiversityValue = false; - } - - + void diversity(const Diversity & _diversityValue) + { + diversityValue = _diversityValue; + invalidDiversityValue = false; + } + + /** * Sets the diversity value as invalid */ - void invalidateDiversity () - { - invalidDiversityValue = true; - } - - + void invalidateDiversity() + { + invalidDiversityValue = true; + } + + /** * Returns true if the diversity value is invalid, false otherwise */ - bool invalidDiversity () const - { - return invalidDiversityValue; - } - - + bool invalidDiversity() const + { + return invalidDiversityValue; + } + + /** * Sets the objective vector, the fitness value and the diversity value as invalid */ - void invalidate () - { - invalidateObjectiveVector (); - invalidateFitness (); - invalidateDiversity (); - } - - + void invalidate() + { + invalidateObjectiveVector(); + invalidateFitness(); + invalidateDiversity(); + } + + /** * Returns true if the fitness value is invalid, false otherwise - */ - bool invalid () const - { - return invalidObjectiveVector (); - } - - + */ + bool invalid() const + { + return invalidObjectiveVector(); + } + + /** * Return the class id (the class name as a std::string) */ - virtual std::string className () const - { - return "MOEO"; - } - - + virtual std::string className() const + { + return "MOEO"; + } + + /** * Writing object * @param _os output stream */ - virtual void printOn (std::ostream & _os) const - { - if (invalidObjectiveVector ()) - { - _os << "INVALID\t"; - } - else - { - _os << objectiveVectorValue << '\t'; - } - } - - + virtual void printOn(std::ostream & _os) const + { + if ( invalidObjectiveVector() ) + { + _os << "INVALID\t"; + } + else + { + _os << objectiveVectorValue << '\t'; + } + } + + /** * Reading object * @param _is input stream - */ - virtual void readFrom (std::istream & _is) - { - std::string objectiveVector_str; - int pos = _is.tellg (); - _is >> objectiveVector_str; - if (objectiveVector_str == "INVALID") - { - invalidateObjectiveVector (); - } - else - { - invalidObjectiveVectorValue = false; - _is.seekg (pos); // rewind - _is >> objectiveVectorValue; - } - } + */ + virtual void readFrom(std::istream & _is) + { + std::string objectiveVector_str; + int pos = _is.tellg(); + _is >> objectiveVector_str; + if (objectiveVector_str == "INVALID") + { + invalidateObjectiveVector(); + } + else + { + invalidObjectiveVectorValue = false; + _is.seekg(pos); // rewind + _is >> objectiveVectorValue; + } + } private: /** the objective vector of this solution */ - ObjectiveVector objectiveVectorValue; + ObjectiveVector objectiveVectorValue; /** true if the objective vector is invalid */ - bool invalidObjectiveVectorValue; + bool invalidObjectiveVectorValue; /** the fitness value of this solution */ - Fitness fitnessValue; + Fitness fitnessValue; /** true if the fitness value is invalid */ - bool invalidFitnessValue; + bool invalidFitnessValue; /** the diversity value of this solution */ - Diversity diversityValue; + Diversity diversityValue; /** true if the diversity value is invalid */ - bool invalidDiversityValue; + bool invalidDiversityValue; }; -#endif /*MOEO_H_ */ +#endif /*MOEO_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeo b/branches/paradiseo-moeo-1.0/src/moeo index aeb808560..903393725 100644 --- a/branches/paradiseo-moeo-1.0/src/moeo +++ b/branches/paradiseo-moeo-1.0/src/moeo @@ -15,9 +15,11 @@ #include +#include #include #include #include +#include #include #include #include @@ -28,6 +30,7 @@ #include #include #include +#include #include #include #include diff --git a/branches/paradiseo-moeo-1.0/src/moeoArchive.h b/branches/paradiseo-moeo-1.0/src/moeoArchive.h index 3f18105ef..09fff58bb 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoArchive.h +++ b/branches/paradiseo-moeo-1.0/src/moeoArchive.h @@ -14,148 +14,141 @@ #define MOEOARCHIVE_H_ #include +#include /** - * An archive is a secondary population that stores non-dominated solutions + * An archive is a secondary population that stores non-dominated solutions. */ -template < class MOEOT > class moeoArchive:public eoPop < MOEOT > +template < class MOEOT > +class moeoArchive : public eoPop < MOEOT > { public: - using std::vector < MOEOT >::size; - using std::vector < MOEOT >::operator[]; - using std::vector < MOEOT >::back; - using std::vector < MOEOT >::pop_back; - - + using std::vector < MOEOT > :: size; + using std::vector < MOEOT > :: operator[]; + using std::vector < MOEOT > :: back; + using std::vector < MOEOT > :: pop_back; + + /** * The type of an objective vector for a solution */ - typedef typename MOEOT::ObjectiveVector ObjectiveVector; - - + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + /** * Default ctor. * The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance */ -moeoArchive ():eoPop < MOEOT > (), comparator (paretoComparator) - { - } - - + moeoArchive() : eoPop < MOEOT >(), comparator(paretoComparator) + {} + + /** * Ctor * @param _comparator the moeoObjectiveVectorComparator used to compare solutions */ -moeoArchive (moeoObjectiveVectorComparator < ObjectiveVector > &_comparator):eoPop < MOEOT > (), - comparator - (_comparator) - { - } - - + moeoArchive(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator) : eoPop < MOEOT >(), comparator(_comparator) + {} + + /** * Returns true if the current archive dominates _objectiveVector according to the moeoObjectiveVectorComparator given in the constructor * @param _objectiveVector the objective vector to compare with the current archive */ - bool dominates (const ObjectiveVector & _objectiveVector) const - { - for (unsigned i = 0; i < size (); i++) - { - if (comparator (operator[](i).fitness (), _objectiveVector) == 1) - { - return true; - } - } - return false; - } - - + bool dominates (const ObjectiveVector & _objectiveVector) const + { + for (unsigned i = 0; i &_pop) - { - for (unsigned i = 0; i < _pop.size (); i++) - { - update (_pop[i]); - } - } - - + void update (const eoPop < MOEOT > & _pop) + { + for (unsigned i=0; i<_pop.size(); i++) + { + update(_pop[i]); + } + } + + private: /** The moeoObjectiveVectorComparator used to compare solutions */ - moeoObjectiveVectorComparator < ObjectiveVector > &comparator; + moeoObjectiveVectorComparator < ObjectiveVector > & comparator; /** A moeoObjectiveVectorComparator based on Pareto dominance (used as default) */ - moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; - + moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; + }; #endif /*MOEOARCHIVE_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/moeoArchiveFitnessSavingUpdater.h b/branches/paradiseo-moeo-1.0/src/moeoArchiveFitnessSavingUpdater.h index e4f0f62a9..013b1d744 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoArchiveFitnessSavingUpdater.h +++ b/branches/paradiseo-moeo-1.0/src/moeoArchiveFitnessSavingUpdater.h @@ -24,7 +24,8 @@ /** * This class allows to save the fitnesses of solutions contained in an archive into a file at each generation. */ -template < class EOT > class moeoArchiveFitnessSavingUpdater:public eoUpdater +template +class moeoArchiveFitnessSavingUpdater : public eoUpdater { public: @@ -34,25 +35,21 @@ public: * @param _filename target filename * @param _id own ID */ -moeoArchiveFitnessSavingUpdater (moeoArchive < EOT > &_arch, const std::string & _filename = "Res/Arch", int _id = -1):arch (_arch), filename (_filename), id (_id), - counter - (0) - { - } + moeoArchiveFitnessSavingUpdater (moeoArchive & _arch, const std::string & _filename = "Res/Arch", int _id = -1) : arch(_arch), filename(_filename), id(_id), counter(0) + {} /** * Saves the fitness of the archive's members into the file */ - void operator () () - { + void operator()() { char buff[MAX_BUFFER_SIZE]; if (id == -1) - sprintf (buff, "%s.%u", filename.c_str (), counter++); + sprintf (buff, "%s.%u", filename.c_str(), counter ++); else - sprintf (buff, "%s.%u.%u", filename.c_str (), id, counter++); - std::ofstream f (buff); + sprintf (buff, "%s.%u.%u", filename.c_str(), id, counter ++); + std::ofstream f(buff); for (unsigned i = 0; i < arch.size (); i++) - f << arch[i].objectiveVector () << std::endl; + f << arch[i].objectiveVector() << std::endl; f.close (); } @@ -60,14 +57,14 @@ moeoArchiveFitnessSavingUpdater (moeoArchive < EOT > &_arch, const std::string & private: /** local archive */ - moeoArchive < EOT > &arch; + moeoArchive & arch; /** target filename */ - std::string filename; + std::string filename; /** own ID */ - int id; + int id; /** counter */ - unsigned counter; + unsigned counter; }; -#endif /*MOEOARCHIVEFITNESSSAVINGUPDATER_H_ */ +#endif /*MOEOARCHIVEFITNESSSAVINGUPDATER_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoArchiveUpdater.h b/branches/paradiseo-moeo-1.0/src/moeoArchiveUpdater.h index 581742849..db0d5f3b9 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoArchiveUpdater.h +++ b/branches/paradiseo-moeo-1.0/src/moeoArchiveUpdater.h @@ -18,39 +18,37 @@ #include /** - * This class allows to update the archive at each generation with newly found non-dominated solutions + * This class allows to update the archive at each generation with newly found non-dominated solutions. */ -template < class EOT > class moeoArchiveUpdater:public eoUpdater +template < class EOT > +class moeoArchiveUpdater : public eoUpdater { public: - + /** * Ctor * @param _arch an archive of non-dominated solutions * @param _pop the main population */ - moeoArchiveUpdater (moeoArchive < EOT > &_arch, - const eoPop < EOT > &_pop):arch (_arch), pop (_pop) - { - } - - + moeoArchiveUpdater(moeoArchive & _arch, const eoPop & _pop) : arch(_arch), pop(_pop) + {} + + /** * Updates the archive with newly found non-dominated solutions contained in the main population */ - void operator () () - { - arch.update (pop); + void operator()() { + arch.update(pop); } private: /** the archive of non-dominated solutions */ - moeoArchive < EOT > &arch; + moeoArchive & arch; /** the main population */ - const eoPop < EOT > &pop; + const eoPop & pop; }; -#endif /*MOEOARCHIVEUPDATER_H_ */ +#endif /*MOEOARCHIVEUPDATER_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoCombinedLS.h b/branches/paradiseo-moeo-1.0/src/moeoCombinedLS.h index 615e06b47..59f380f3d 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoCombinedLS.h +++ b/branches/paradiseo-moeo-1.0/src/moeoCombinedLS.h @@ -19,53 +19,53 @@ /** * This class allows to embed a set of local searches that are sequentially applied, - * and so working and updating the same archive of non-dominated solutions + * and so working and updating the same archive of non-dominated solutions. */ -template < class MOEOT > class moeoCombinedLS:public moeoLS < MOEOT > +template < class MOEOT > +class moeoCombinedLS : public moeoLS < MOEOT > { public: - + /** * Ctor * @param _eval the full evaluator of a solution - * @param _first_ls the first multi-objective local search to add + * @param _first_mols the first multi-objective local search to add */ -moeoCombinedLS (moeoEvalFunc < MOEOT > &_eval, moeoLS < MOEOT > &_first_ls):eval - (_eval) - { - combinedLS.push_back (&_first_ls); - } - + moeoCombinedLS(moeoEvalFunc < MOEOT > & _eval, moeoLS < MOEOT > & _first_mols) : eval (_eval) + { + combinedLS.push_back (& _first_mols); + } + /** * Adds a new local search to combine - * @param _ls the multi-objective local search to add + * @param _mols the multi-objective local search to add */ - void add (moeoLS < MOEOT > &_ls) - { - combinedMOLS.push_back (&_ls); - } - + void add(moeoLS < MOEOT > & _mols) + { + combinedMOLS.push_back(& _mols); + } + /** * Gives a new solution in order to explore the neigborhood. * The new non-dominated solutions are added to the archive - * @param _eo the solution + * @param _moeo the solution * @param _arch the archive of non-dominated solutions */ - void operator () (const MOEOT & _eo, moeoArchive < MOEOT > &_arch) - { - eval (const_cast < MOEOT & >(_eo)); - for (unsigned i = 0; i < combinedLS.size (); i++) - combinedLS[i]->operator ()(_eo, _arch); - } + void operator () (const MOEOT & _moeo, moeoArchive < MOEOT > & _arch) + { + eval(const_cast < MOEOT & > (_moeo)); + for (unsigned i=0; i operator()(_moeo, _arch); + } private: /** the full evaluator of a solution */ - moeoEvalFunc < MOEOT > &eval; + moeoEvalFunc < MOEOT > & eval; /** the vector that contains the combined LS */ - std::vector < moeoLS < MOEOT > *>combinedLS; + std::vector< moeoLS < MOEOT > * > combinedLS; }; -#endif /*MOEOCOMBINEDLS_H_ */ +#endif /*MOEOCOMBINEDLS_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoComparator.h b/branches/paradiseo-moeo-1.0/src/moeoComparator.h index 6b6467c32..21f2d355b 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoComparator.h +++ b/branches/paradiseo-moeo-1.0/src/moeoComparator.h @@ -14,24 +14,20 @@ #define MOEOCOMPARATOR_H_ #include -#include /** - * Functor allowing to compare two solutions + * Functor allowing to compare two solutions. */ -template < class MOEOT > class moeoComparator:public eoBF < const MOEOT &, const MOEOT &, - const bool > -{ -public: - // virtual const bool operator () (const MOEOT & _moeo1, const MOEOT & _moeo){} -}; +template < class MOEOT > +class moeoComparator : public eoBF < const MOEOT &, const MOEOT &, const bool > +{}; /** - * Functor allowing to compare two solutions according to their first objective value, then their second, and so on + * Functor allowing to compare two solutions according to their first objective value, then their second, and so on. */ -template < class MOEOT > class moeoObjectiveComparator:public moeoComparator < - MOEOT > +template < class MOEOT > +class moeoObjectiveComparator : public moeoComparator < MOEOT > { public: /** @@ -39,56 +35,18 @@ public: * @param _moeo1 the first solution * @param _moeo2 the second solution */ - const bool operator () (const MOEOT & _moeo1, const MOEOT & _moeo2) - { - return _moeo1.objectiveVector () < _moeo2.objectiveVector (); - } + const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return _moeo1.objectiveVector() < _moeo2.objectiveVector(); + } }; /** - * Functor allowing to compare two solutions according to their fitness values + * Functor allowing to compare two solutions according to their fitness values, then according to their diversity values. */ -//template < class MOEOT > -//class moeoFitnessComparator : public moeoComparator < MOEOT > -//{ -//public: -// /** -// * Returns true if the fitness value of _moeo1 is smaller than the fitness value of _moeo2 -// * @param _moeo1 the first solution -// * @param _moeo2 the second solution -// */ -// const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) -// { -// return _moeo1.fitness() < _moeo2.fitness(); -// } -//}; -// -// -///** -// * Functor allowing to compare two solutions according to their diversity values -// */ -//template < class MOEOT > -//class moeoDiversityComparator : public moeoComparator < MOEOT > -//{ -//public: -// /** -// * Returns true if the diversity value of _moeo1 is smaller than the diversity value of _moeo2 -// * @param _moeo1 the first solution -// * @param _moeo2 the second solution -// */ -// const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) -// { -// return _moeo1.diversity() < _moeo2.diversity(); -// } -//}; - - -/** - * Functor allowing to compare two solutions according to their fitness values, then according to their diversity values - */ -template < class MOEOT > class moeoFitnessThenDiversityComparator:public moeoComparator < - MOEOT > +template < class MOEOT > +class moeoFitnessThenDiversityComparator : public moeoComparator < MOEOT > { public: /** @@ -96,25 +54,25 @@ public: * @param _moeo1 the first solution * @param _moeo2 the second solution */ - const bool operator () (const MOEOT & _moeo1, const MOEOT & _moeo2) - { - if (_moeo1.fitness () == _moeo2.fitness ()) - { - return _moeo1.diversity () < _moeo2.diversity (); - } - else - { - return _moeo1.fitness () < _moeo2.fitness (); - } - } + const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + if (_moeo1.fitness() == _moeo2.fitness()) + { + return _moeo1.diversity() < _moeo2.diversity(); + } + else + { + return _moeo1.fitness() < _moeo2.fitness(); + } + } }; /** - * Functor allowing to compare two solutions according to their diversity values, then according to their fitness values + * Functor allowing to compare two solutions according to their diversity values, then according to their fitness values. */ -template < class MOEOT > class moeoDiversityThenFitnessComparator:public moeoComparator < - MOEOT > +template < class MOEOT > +class moeoDiversityThenFitnessComparator : public moeoComparator < MOEOT > { public: /** @@ -122,64 +80,18 @@ public: * @param _moeo1 the first solution * @param _moeo2 the second solution */ - const bool operator () (const MOEOT & _moeo1, const MOEOT & _moeo2) - { - if (_moeo1.diversity () == _moeo2.diversity ()) - { - return _moeo1.fitness () < _moeo2.fitness (); - } - else - { - return _moeo1.diversity () < _moeo2.diversity (); - } - } -}; - - -/** - * Functor allowing to compare two solutions according to Pareto dominance relation => USEFULL ??? - * -template < class MOEOT > -class moeoParetoDominanceComparator : public moeoComparator < MOEOT > -{ -public: - /** - * Returns true if _moeo1 is dominated by _moeo2 according to Pareto dominance relation - * @param _moeo1 the first solution - * @param _moeo2 the second solution - * const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) { - bool result = false; - typedef typename MOEOT::ObjectiveVector::Traits ObjectiveVectorTraits; - for (unsigned i=0; i ObjectiveVectorTraits::tolerance() ) - { - // if the ith objective have to be minimized... - if (ObjectiveVectorTraits::minimizing(i)) - { - if (_moeo1.objectiveVector()[i] < _moeo2.objectiveVector()[i]) - { - return false; // _moeo2 cannot dominate _moeo1 - } - result = true; - } - // if the ith objective have to be maximized... - else if (ObjectiveVectorTraits::maximizing(i)) - { - if (_moeo1.objectiveVector()[i] > _moeo2.objectiveVector()[i]) - { - return false; // _moeo2 cannot dominate _moeo1 - } - result = true; - } - } + return _moeo1.fitness() < _moeo2.fitness(); + } + else + { + return _moeo1.diversity() < _moeo2.diversity(); } - return result; } }; -*/ -#endif /*MOEOCOMPARATOR_H_ */ + +#endif /*MOEOCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoDiversityAssignment.h b/branches/paradiseo-moeo-1.0/src/moeoDiversityAssignment.h index de5195c39..57f9577d1 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoDiversityAssignment.h +++ b/branches/paradiseo-moeo-1.0/src/moeoDiversityAssignment.h @@ -17,32 +17,34 @@ #include /** - * Functor that sets the diversity values of a whole population + * Functor that sets the diversity values of a whole population. */ -template < class MOEOT > class moeoDiversityAssignment:public eoUF < eoPop < MOEOT > &, - void > -{ -}; +template < class MOEOT > +class moeoDiversityAssignment : public eoUF < eoPop < MOEOT > &, void > +{}; /** - * moeoDummyDiversityAssignment is a moeoDiversityAssignment which gives 0 as the diversity for the whole population. + * moeoDummyDiversityAssignment is a moeoDiversityAssignment that gives the value '0' as the individual's diversity for a whole population. */ -template < class MOEOT > class moeoDummyDiversityAssignment:public moeoDiversityAssignment < - MOEOT > +template < class MOEOT > +class moeoDummyDiversityAssignment : public moeoDiversityAssignment < MOEOT > { - // main operator - void operator () (eoPop < MOEOT > &_pop) - { - for (int idx = 0; idx < _pop.size (); idx++) - { - // set the diversity to 0 - _pop[idx].diversity (0); - } - } +public: + /** + * Sets the diversity to '0' for every individuals of the population _pop + * @param _pop the population + */ + void operator () (eoPop < MOEOT > & _pop) + { + for (unsigned idx = 0; idx<_pop.size (); idx++) + { + // set the diversity to 0 + _pop[idx].diversity(0); + } + } + }; - - -#endif /*MOEODIVERSITYASSIGNMENT_H_ */ +#endif /*MOEODIVERSITYASSIGNMENT_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoEA.h b/branches/paradiseo-moeo-1.0/src/moeoEA.h index 3e7d554bc..901dba540 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoEA.h +++ b/branches/paradiseo-moeo-1.0/src/moeoEA.h @@ -16,11 +16,10 @@ #include /** - * Abstract class for multi-objective evolutionary algorithms + * Abstract class for multi-objective evolutionary algorithms. */ -template < class MOEOT > class moeoEA:public eoAlgo < MOEOT > -{ -}; +template < class MOEOT > +class moeoEA : public eoAlgo < MOEOT > {}; -#endif /*MOEOEA_H_ */ +#endif /*MOEOEA_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoEvalFunc.h b/branches/paradiseo-moeo-1.0/src/moeoEvalFunc.h index 0e3497f7e..1f6b7544d 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoEvalFunc.h +++ b/branches/paradiseo-moeo-1.0/src/moeoEvalFunc.h @@ -18,8 +18,7 @@ /* * Functor that evaluates one MOEO by setting all its objective values. */ -template < class MOEOT > class moeoEvalFunc:public eoEvalFunc < MOEOT > -{ -}; +template < class MOEOT > +class moeoEvalFunc : public eoEvalFunc< MOEOT > {}; -#endif /*MOEOEVALFUNC_H_ */ +#endif /*MOEOEVALFUNC_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoFastNonDominatedSortingFitnessAssignment.h b/branches/paradiseo-moeo-1.0/src/moeoFastNonDominatedSortingFitnessAssignment.h index 106ebf650..624df8c73 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoFastNonDominatedSortingFitnessAssignment.h +++ b/branches/paradiseo-moeo-1.0/src/moeoFastNonDominatedSortingFitnessAssignment.h @@ -14,170 +14,159 @@ #define MOEOFASTNONDOMINATEDSORTINGFITNESSASSIGNMENT_H_ #include -#include #include +#include #include /** - * Fitness assignment sheme based on Pareto-dominance count proposed in + * Fitness assignment sheme based on Pareto-dominance count proposed in: * N. Srinivas, K. Deb, "Multiobjective Optimization Using Nondominated Sorting in Genetic Algorithms", Evolutionary Computation vol. 2, no. 3, pp. 221-248 (1994) - * and in + * and in: * K. Deb, A. Pratap, S. Agarwal, T. Meyarivan, "A Fast and Elitist Multi-Objective Genetic Algorithm: NSGA-II", IEEE Transactions on Evolutionary Computation, vol. 6, no. 2 (2002). * This strategy is, for instance, used in NSGA and NSGA-II. */ -template < class MOEOT > class moeoFastNonDominatedSortingFitnessAssignment:public moeoParetoBasedFitnessAssignment < - MOEOT - > +template < class MOEOT > +class moeoFastNonDominatedSortingFitnessAssignment : public moeoParetoBasedFitnessAssignment < MOEOT > { public: /** * Ctor */ - moeoFastNonDominatedSortingFitnessAssignment () - { - } - - + moeoFastNonDominatedSortingFitnessAssignment() {} + + /** * Computes fitness values for every solution contained in the population _pop * @param _pop the population */ - void operator () (eoPop < MOEOT > &_pop) - { - // number of objectives for the problem under consideration - unsigned nObjectives = MOEOT::ObjectiveVector::nObjectives (); - if (nObjectives == 1) - { - // one objective - oneObjective (_pop); - } - else if (nObjectives == 2) - { - // two objectives (the two objectives function is still to do) - mObjectives (_pop); - } - else if (nObjectives > 2) - { - // more than two objectives - mObjectives (_pop); - } - else - { - // problem with the number of objectives - throw std:: - runtime_error - ("Problem with the number of objectives in moeoFastNonDominatedSortingFitnessAssignment"); - } - } + void operator()(eoPop < MOEOT > & _pop) + { + // number of objectives for the problem under consideration + unsigned nObjectives = MOEOT::ObjectiveVector::nObjectives(); + if (nObjectives == 1) + { + // one objective + oneObjective(_pop); + } + else if (nObjectives == 2) + { + // two objectives (the two objectives function is still to implement) + mObjectives(_pop); + } + else if (nObjectives > 2) + { + // more than two objectives + mObjectives(_pop); + } + else + { + // problem with the number of objectives + throw std::runtime_error("Problem with the number of objectives in moeoFastNonDominatedSortingFitnessAssignment"); + } + } private: /** the objective vector type of the solutions */ - typedef typename MOEOT::ObjectiveVector ObjectiveVector; + typedef typename MOEOT::ObjectiveVector ObjectiveVector; /** Functor to compare two objective vectors according to Pareto dominance relation */ - moeoParetoObjectiveVectorComparator < ObjectiveVector > comparator; + moeoParetoObjectiveVectorComparator < ObjectiveVector > comparator; /** Functor to compare two solutions on the first objective, then on the second, and so on */ - moeoObjectiveComparator < MOEOT > objComparator; - - + moeoObjectiveComparator < MOEOT > objComparator; + + /** * Sets the fitness values for mono-objective problems * @param _pop the population */ - void oneObjective (eoPop < MOEOT > &_pop) - { - std::sort (_pop.begin (), _pop.end (), objComparator); - for (unsigned i = 0; i < _pop.size (); i++) - { - _pop[i].fitness (i + 1); - } - } - - + void oneObjective (eoPop < MOEOT > & _pop) + { + std::sort(_pop.begin(), _pop.end(), objComparator); + for (unsigned i=0; i<_pop.size(); i++) + { + _pop[i].fitness(i+1); + } + } + + /** * Sets the fitness values for bi-objective problems with a complexity of O(n log n), where n stands for the population size * @param _pop the population */ - void twoObjectives (eoPop < MOEOT > &_pop) - { - //... TO DO ! - } - - + void twoObjectives (eoPop < MOEOT > & _pop) + { + //... TO DO ! + } + + /** * Sets the fitness values for problems with more than two objectives with a complexity of O(n² log n), where n stands for the population size * @param _pop the population */ - void mObjectives (eoPop < MOEOT > &_pop) - { - // S[i] = indexes of the individuals dominated by _pop[i] - std::vector < std::vector < unsigned >>S (_pop.size ()); - // n[i] = number of individuals that dominate the individual _pop[i] - std::vector < unsigned >n (_pop.size (), 0); - // fronts: F[i] = indexes of the individuals contained in the ith front - std::vector < std::vector < unsigned >>F (_pop.size () + 1); - // used to store the number of the first front - F[1].reserve (_pop.size ()); - // flag to comparae solutions - int comparatorFlag; - for (unsigned p = 0; p < _pop.size (); p++) - { - for (unsigned q = 0; q < _pop.size (); q++) - { - // comparison of the 2 solutions according to Pareto dominance - comparatorFlag = - comparator (_pop[p].objectiveVector (), - _pop[q].objectiveVector ()); - // if p dominates q - if (comparatorFlag == 1) - { - // add q to the set of solutions dominated by p - S[p].push_back (q); - } - // if q dominates p - else if (comparatorFlag == -1) - { - // increment the domination counter of p - n[p]++; - } - } - // if no individual dominates p - if (n[p] == 0) - { - // p belongs to the first front - _pop[p].fitness (1); - F[1].push_back (p); - } - } - // front counter - unsigned counter = 1; - unsigned p, q; - while (!F[counter].empty ()) - { - // used to store the number of the next front - F[counter + 1].reserve (_pop.size ()); - for (unsigned i = 0; i < F[counter].size (); i++) - { - p = F[counter][i]; - for (unsigned j = 0; j < S[p].size (); j++) - { - q = S[p][j]; - n[q]--; - // if no individual dominates q anymore - if (n[q] == 0) - { - // q belongs to the next front - _pop[q].fitness (counter + 1); - F[counter + 1].push_back (q); - } - } - } - counter++; - } - } + void mObjectives (eoPop < MOEOT > & _pop) + { + // S[i] = indexes of the individuals dominated by _pop[i] + std::vector < std::vector > S(_pop.size()); + // n[i] = number of individuals that dominate the individual _pop[i] + std::vector < unsigned > n(_pop.size(), 0); + // fronts: F[i] = indexes of the individuals contained in the ith front + std::vector < std::vector > F(_pop.size()+1); + // used to store the number of the first front + F[1].reserve(_pop.size()); + for (unsigned p=0; p<_pop.size(); p++) + { + for (unsigned q=0; q<_pop.size(); q++) + { + // if p dominates q + if ( comparator(_pop[p].objectiveVector(), _pop[q].objectiveVector()) ) + { + // add q to the set of solutions dominated by p + S[p].push_back(q); + } + // if q dominates p + else if ( comparator(_pop[q].objectiveVector(), _pop[p].objectiveVector()) ) + { + // increment the domination counter of p + n[p]++; + } + } + // if no individual dominates p + if (n[p] == 0) + { + // p belongs to the first front + _pop[p].fitness(1); + F[1].push_back(p); + } + } + // front counter + unsigned counter=1; + unsigned p,q; + while (! F[counter].empty()) + { + // used to store the number of the next front + F[counter+1].reserve(_pop.size()); + for (unsigned i=0; i /** - * Functor that sets the fitness values of a whole population + * Functor that sets the fitness values of a whole population. */ -template < class MOEOT > class moeoFitnessAssignment:public eoUF < eoPop < MOEOT > &, - void > -{ -}; +template < class MOEOT > +class moeoFitnessAssignment : public eoUF < eoPop < MOEOT > &, void > +{}; /** - * moeoScalarFitnessAssignment is a moeoFitnessAssignment for scalar strategies + * moeoScalarFitnessAssignment is a moeoFitnessAssignment for scalar strategies. */ -template < class MOEOT > class moeoScalarFitnessAssignment:public moeoFitnessAssignment < - MOEOT > -{ -}; +template < class MOEOT > +class moeoScalarFitnessAssignment : public moeoFitnessAssignment < MOEOT > +{}; /** - * moeoCriterionBasedFitnessAssignment is a moeoFitnessAssignment for criterion-based strategies + * moeoCriterionBasedFitnessAssignment is a moeoFitnessAssignment for criterion-based strategies. */ -template < class MOEOT > class moeoCriterionBasedFitnessAssignment:public moeoFitnessAssignment < - MOEOT > -{ -}; +template < class MOEOT > +class moeoCriterionBasedFitnessAssignment : public moeoFitnessAssignment < MOEOT > +{}; /** - * moeoParetoBasedFitnessAssignment is a moeoFitnessAssignment for Pareto-based strategies + * moeoParetoBasedFitnessAssignment is a moeoFitnessAssignment for Pareto-based strategies. */ -template < class MOEOT > class moeoParetoBasedFitnessAssignment:public moeoFitnessAssignment < - MOEOT > -{ - void operator () (eoPop < MOEOT > &_pop) - { - // do nothing for the moment ... - } -}; +template < class MOEOT > +class moeoParetoBasedFitnessAssignment : public moeoFitnessAssignment < MOEOT > +{}; - - - -#endif /*MOEOFITNESSASSIGNMENT_H_ */ +#endif /*MOEOFITNESSASSIGNMENT_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoGenerationalReplacement.h b/branches/paradiseo-moeo-1.0/src/moeoGenerationalReplacement.h index c6804ccac..99e7b9024 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoGenerationalReplacement.h +++ b/branches/paradiseo-moeo-1.0/src/moeoGenerationalReplacement.h @@ -17,12 +17,9 @@ #include /** - * Generational replacement: only the new individuals are preserved + * Generational replacement: only the new individuals are preserved. */ -template < class MOEOT > class moeoGenerationalReplacement:public moeoReplacement < MOEOT >, - public eoGenerationalReplacement < - MOEOT > -{ -}; +template < class MOEOT > +class moeoGenerationalReplacement : public moeoReplacement < MOEOT >, public eoGenerationalReplacement < MOEOT > {}; -#endif /*MOEOGENERATIONALREPLACEMENT_H_ */ +#endif /*MOEOGENERATIONALREPLACEMENT_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoHybridLS.h b/branches/paradiseo-moeo-1.0/src/moeoHybridLS.h index 8891d1ab5..c28fc3a8c 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoHybridLS.h +++ b/branches/paradiseo-moeo-1.0/src/moeoHybridLS.h @@ -24,7 +24,8 @@ * This class allows to apply a multi-objective local search to a number of selected individuals contained in the archive * at every generation until a stopping criteria is verified. */ -template < class MOEOT > class moeoHybridLS:public eoUpdater +template < class MOEOT > +class moeoHybridLS : public eoUpdater { public: @@ -32,45 +33,42 @@ public: * Ctor * @param _term stopping criteria * @param _select selector - * @param _ls a multi-objective local search + * @param _mols a multi-objective local search * @param _arch the archive */ -eoHybridLS (eoContinue < MOEOT > &_term, eoSelect < MOEOT > &_select, moeoLS < MOEOT > &_ls, moeoArchive < MOEOT > &_arch):term (_term), select (_select), ls (_ls), - arch - (_arch) - { - } + eoHybridLS (eoContinue < MOEOT > & _term, eoSelect < MOEOT > & _select, moeoLS < MOEOT > & _mols, moeoArchive < MOEOT > & _arch) : term(_term), select(_select), mols(_mols), arch(_arch) + {} /** * Applies the multi-objective local search to selected individuals contained in the archive if the stopping criteria is not verified */ - void operator () () - { - if (!cont (arch)) - { - // selection of solutions - eoPop < MOEOT > selectedSolutions; - select (arch, selectedSolutions); - // apply the local search to every selected solution - for (unsigned i = 0; i < selectedSolutions.size (); i++) - { - ls (selectedSolutions[i], arch); - } - } - } + void operator () () + { + if (! cont (arch)) + { + // selection of solutions + eoPop < MOEOT > selectedSolutions; + select(arch, selectedSolutions); + // apply the local search to every selected solution + for (unsigned i=0; i &term; + eoContinue < MOEOT > & term; /** selector */ - eoSelect < MOEOT > &select; + eoSelect < MOEOT > & select; /** multi-objective local search */ - moeoLS < MOEOT > &ls; + moeoLS < MOEOT > & mols; /** archive */ - moeoArchive < MOEOT > &arch; + moeoArchive < MOEOT > & arch; }; -#endif /*MOEOHYBRIDLS_H_ */ +#endif /*MOEOHYBRIDLS_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoLS.h b/branches/paradiseo-moeo-1.0/src/moeoLS.h index 7f9504597..f6cca796f 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoLS.h +++ b/branches/paradiseo-moeo-1.0/src/moeoLS.h @@ -20,9 +20,8 @@ * Abstract class for local searches applied to multi-objective optimization. * Starting from only one solution, it produces a set of new non-dominated solutions. */ -template < class MOEOT > class moeoLS:public eoBF < const MOEOT &, moeoArchive < MOEOT > &, - void > -{ -}; +template < class MOEOT > +class moeoLS: public eoBF < const MOEOT &, moeoArchive < MOEOT > &, void > +{}; -#endif /*MOEOLS_H_ */ +#endif /*MOEOLS_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoObjectiveVector.h b/branches/paradiseo-moeo-1.0/src/moeoObjectiveVector.h index ddabe0af6..229dfb0f3 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoObjectiveVector.h +++ b/branches/paradiseo-moeo-1.0/src/moeoObjectiveVector.h @@ -24,54 +24,52 @@ * but it can be replaced at will by any other class that implements the static functions defined therein. * Some static funtions to access to the traits characteristics are re-defined in order not to write a lot of typedef's. */ -template < class ObjectiveVectorTraits > class moeoObjectiveVector +template < class ObjectiveVectorTraits > +class moeoObjectiveVector { public: /** The traits of objective vectors */ - typedef ObjectiveVectorTraits Traits; - - + typedef ObjectiveVectorTraits Traits; + + /** * Parameters setting (for the objective vector of any solution) * @param _nObjectives the number of objectives * @param _bObjectives the min/max vector (true = min / false = max) */ - static void setup (unsigned _nObjectives, - std::vector < bool > &_bObjectives) - { - ObjectiveVectorTraits::setup (_nObjectives, _bObjectives); - } - - + static void setup(unsigned _nObjectives, std::vector < bool > & _bObjectives) + { + ObjectiveVectorTraits::setup(_nObjectives, _bObjectives); + } + + /** * Returns the number of objectives */ - static unsigned nObjectives () - { - return ObjectiveVectorTraits::nObjectives (); - } - - + static unsigned nObjectives() + { + return ObjectiveVectorTraits::nObjectives(); + } + + /** * Returns true if the _ith objective have to be minimized * @param _i the index */ - static bool minimizing (unsigned _i) - { - return ObjectiveVectorTraits::minimizing (_i); - } - - + static bool minimizing(unsigned _i) { + return ObjectiveVectorTraits::minimizing(_i); + } + + /** * Returns true if the _ith objective have to be maximized * @param _i the index */ - static bool maximizing (unsigned _i) - { - return ObjectiveVectorTraits::maximizing (_i); - } - + static bool maximizing(unsigned _i) { + return ObjectiveVectorTraits::maximizing(_i); + } + }; @@ -79,159 +77,122 @@ public: * This class allows to represent a solution in the objective space (phenotypic representation) by a std::vector of doubles, * i.e. that an objective value is represented using a double, and this for any objective. */ -template < class ObjectiveVectorTraits > class moeoObjectiveVectorDouble:public moeoObjectiveVector < ObjectiveVectorTraits >, - public std::vector < - double > +template < class ObjectiveVectorTraits > +class moeoObjectiveVectorDouble : public moeoObjectiveVector < ObjectiveVectorTraits >, public std::vector < double > { -public: - - using - std::vector < double >::size; - using - std::vector < double >::operator[]; - +public: + + using std::vector< double >::size; + using std::vector< double >::operator[]; + /** * Ctor */ - moeoObjectiveVectorDouble (): - std::vector < double >(ObjectiveVectorTraits::nObjectives (), 0.0) - { - } - - + moeoObjectiveVectorDouble() : std::vector < double > (ObjectiveVectorTraits::nObjectives(), 0.0) {} + + /** * Ctor from a vector of doubles * @param _v the std::vector < double > */ - moeoObjectiveVectorDouble (std::vector < double >&_v): - std::vector < double >(_v) - { - } - - + moeoObjectiveVectorDouble(std::vector & _v) : std::vector < double > (_v) {} + + /** * Returns true if the current objective vector dominates _other according to the Pareto dominance relation * (but it's better to use a moeoObjectiveVectorComparator object to compare solutions) * @param _other the other moeoObjectiveVectorDouble object to compare with */ - bool - dominates (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - moeoParetoObjectiveVectorComparator < - moeoObjectiveVectorDouble < - ObjectiveVectorTraits > > - comparator; - return - comparator (*this, _other) == 1; - } - - + bool dominates(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + moeoParetoObjectiveVectorComparator < moeoObjectiveVectorDouble > comparator; + return comparator(*this, _other); + } + + /** * Returns true if the current objective vector is equal to _other (according to a tolerance value) * @param _other the other moeoObjectiveVectorDouble object to compare with */ - bool - operator== (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - for (unsigned i = 0; i < size (); i++) - { - if (fabs (operator[](i) - _other[i]) > - ObjectiveVectorTraits::tolerance ()) - { - return false; - } - } - return - true; - } - - + bool operator==(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + for (unsigned i=0; i < size(); i++) + { + if ( fabs(operator[](i) - _other[i]) > ObjectiveVectorTraits::tolerance() ) + { + return false; + } + } + return true; + } + + /** * Returns true if the current objective vector is different than _other (according to a tolerance value) * @param _other the other moeoObjectiveVectorDouble object to compare with */ - bool - operator!= (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - return ! - operator== (_other); - } - - + bool operator!=(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + return ! operator==(_other); + } + + /** * Returns true if the current objective vector is smaller than _other on the first objective, then on the second, and so on * (can be usefull for sorting/printing) * @param _other the other moeoObjectiveVectorDouble object to compare with - */ - bool - operator< (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - for (unsigned i = 0; i < size (); i++) - { - if (fabs (operator[](i) - _other[i]) > - ObjectiveVectorTraits::tolerance ()) - { - if (operator[](i) < _other[i]) - { - return true; - } - else - { + */ + bool operator<(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + for (unsigned i=0; i < size(); i++) + { + if ( fabs(operator[](i) - _other[i]) > ObjectiveVectorTraits::tolerance() ) + { + if (operator[](i) < _other[i]) + { + return true; + } + else + { + return false; + } + } + } return false; - } - } - } - return false; - } - - + } + + /** * Returns true if the current objective vector is greater than _other on the first objective, then on the second, and so on * (can be usefull for sorting/printing) * @param _other the other moeoObjectiveVectorDouble object to compare with */ - bool - operator> (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - return - _other < * - this; - } - - + bool operator>(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + return _other < *this; + } + + /** * Returns true if the current objective vector is smaller than or equal to _other on the first objective, then on the second, and so on * (can be usefull for sorting/printing) * @param _other the other moeoObjectiveVectorDouble object to compare with */ - bool - operator<= (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - return - operator== (_other) || - operator< (_other); - } - - + bool operator<=(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + return operator==(_other) || operator<(_other); + } + + /** * Returns true if the current objective vector is greater than or equal to _other on the first objective, then on the second, and so on * (can be usefull for sorting/printing) * @param _other the other moeoObjectiveVectorDouble object to compare with */ - bool - operator>= (const moeoObjectiveVectorDouble < ObjectiveVectorTraits > - &_other) const - { - return - operator== (_other) || - operator> (_other); - } + bool operator>=(const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _other) const + { + return operator==(_other) || operator>(_other); + } }; @@ -242,15 +203,13 @@ public: * @param _objectiveVector the objective vector to write */ template < class ObjectiveVectorTraits > - std::ostream & operator<< (std::ostream & _os, - const moeoObjectiveVectorDouble < - ObjectiveVectorTraits > &_objectiveVector) +std::ostream & operator<<(std::ostream & _os, const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _objectiveVector) { - for (unsigned i = 0; i < _objectiveVector.size (); i++) - { - _os << _objectiveVector[i] << ' '; - } - return _os; + for (unsigned i=0; i<_objectiveVector.size(); i++) + { + _os << _objectiveVector[i] << ' '; + } + return _os; } /** @@ -259,16 +218,14 @@ template < class ObjectiveVectorTraits > * @param _objectiveVector the objective vector to read */ template < class ObjectiveVectorTraits > - std::istream & operator>> (std::istream & _is, - moeoObjectiveVectorDouble < - ObjectiveVectorTraits > &_objectiveVector) +std::istream & operator>>(std::istream & _is, moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _objectiveVector) { - _objectiveVector = moeoObjectiveVectorDouble < ObjectiveVectorTraits > (); - for (unsigned i = 0; i < _objectiveVector.size (); i++) - { - _is >> _objectiveVector[i]; - } - return _is; + _objectiveVector = moeoObjectiveVectorDouble < ObjectiveVectorTraits > (); + for (unsigned i=0; i<_objectiveVector.size(); i++) + { + _is >> _objectiveVector[i]; + } + return _is; } -#endif /*MOEOOBJECTIVEVECTOR_H_ */ +#endif /*MOEOOBJECTIVEVECTOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorComparator.h b/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorComparator.h index bd4491f19..9ec09b85b 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorComparator.h +++ b/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorComparator.h @@ -20,74 +20,61 @@ * Abstract class allowing to compare 2 objective vectors. * The template argument ObjectiveVector have to be a moeoObjectiveVector. */ -template < class ObjectiveVector > class moeoObjectiveVectorComparator:public eoBF < const ObjectiveVector &, const ObjectiveVector &, - int > -{ -}; +template < class ObjectiveVector > +class moeoObjectiveVectorComparator : public eoBF < const ObjectiveVector &, const ObjectiveVector &, bool > +{}; /** - * This functor class allows to compare 2 objective vectors according to Pareto dominance + * This functor class allows to compare 2 objective vectors according to Pareto dominance. */ -template < class ObjectiveVector > class moeoParetoObjectiveVectorComparator:public moeoObjectiveVectorComparator < - ObjectiveVector - > +template < class ObjectiveVector > +class moeoParetoObjectiveVectorComparator : public moeoObjectiveVectorComparator < ObjectiveVector > { public: - + /** - * Returns 1 if _objectiveVector1 dominates _objectiveVector2, -1 if _objectiveVector2 dominates _objectiveVector1 and 0 if no one dominates the other + * Returns true if _objectiveVector1 dominates _objectiveVector2 * @param _objectiveVector1 the first objective vector * @param _objectiveVector2 the second objective vector - */ - int operator () (const ObjectiveVector & _objectiveVector1, - const ObjectiveVector & _objectiveVector2) - { - bool dom1 = false; - bool dom2 = false; - for (unsigned i = 0; i < ObjectiveVector::nObjectives (); i++) - { - // first, we have to check if the 2 objective values are not equal for the ith objective - if (fabs (_objectiveVector1[i] - _objectiveVector2[i]) > - ObjectiveVector::Traits::tolerance ()) - { - // if the ith objective have to be minimized... - if (ObjectiveVector::minimizing (i)) - { - if (_objectiveVector1[i] > _objectiveVector2[i]) - { - dom2 = true; //_objectiveVector2[i] is better than _objectiveVector1[i] - } - else // _objectiveVector1[i] < _objectiveVector2[i] - { - dom1 = true; //_objectiveVector1[i] is better than _objectiveVector2[i] - } - } - // if the ith objective have to be maximized... - else if (ObjectiveVector::maximizing (i)) - { - if (_objectiveVector1[i] > _objectiveVector2[i]) - { - dom1 = true; //_objectiveVector1[i] is better than _objectiveVector2[i] - } - else // _objectiveVector1[i] < _objectiveVector2[i] - { - dom2 = true; //_objectiveVector2[i] is better than _objectiveVector1[i] - } - } - } - } - if (dom1 == dom2) - { - return 0; // no one dominates the other - } - if (dom1) - { - return 1; //_objectiveVector1 dominates _objectiveVector2 - } - return -1; //_objectiveVector2 dominates _objectiveVector1 - } + */ + bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2) + { + bool dom = false; + for (unsigned i=0; i ObjectiveVector::Traits::tolerance() ) + { + // if the ith objective have to be minimized... + if (ObjectiveVector::minimizing(i)) + { + if (_objectiveVector1[i] < _objectiveVector2[i]) + { + dom = true; //_objectiveVector1[i] is better than _objectiveVector2[i] + } + else + { + return false; //_objectiveVector1 cannot dominate _objectiveVector2 + } + } + // if the ith objective have to be maximized... + else if (ObjectiveVector::maximizing(i)) + { + if (_objectiveVector1[i] > _objectiveVector2[i]) + { + dom = true; //_objectiveVector1[i] is better than _objectiveVector2[i] + } + else + { + return false; //_objectiveVector1 cannot dominate _objectiveVector2 + } + } + } + } + return dom; + } }; -#endif /*MOEOOBJECTIVEVECTORCOMPARATOR_H_ */ +#endif /*MOEOOBJECTIVEVECTORCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorTraits.h b/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorTraits.h index 5fd360c4b..3b58ccbde 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorTraits.h +++ b/branches/paradiseo-moeo-1.0/src/moeoObjectiveVectorTraits.h @@ -18,105 +18,91 @@ #include /** - * A traits class for moeoObjectiveVector to specify the number of objectives and which ones have to be minimized or maximized + * A traits class for moeoObjectiveVector to specify the number of objectives and which ones have to be minimized or maximized. */ class moeoObjectiveVectorTraits { public: /** The tolerance value (used to compare solutions) */ - const static double tol = 1e-6; - + const static double tol = 1e-6; + /** * Parameters setting * @param _nObjectives the number of objectives * @param _bObjectives the min/max vector (true = min / false = max) */ - static void setup (unsigned _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"; - } - // 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"); - } - + static void setup(unsigned _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"; + } + // 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"); + } + /** * Returns the number of objectives */ - static unsigned 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; - } - + static unsigned 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; + } + /** * Returns true if the _ith objective have to be minimized * @param _i the index */ - static bool minimizing (unsigned _i) - { - // in case the min/max vector would not be assigned yet - if (!bObj[_i]) - throw std:: - runtime_error - ("We don't know if the ith objective have to be minimized or maximized in moeoObjectiveVectorTraits"); - // in case there would be a wrong index - if (_i >= bObj.size ()) - throw std::runtime_error ("Wrong index in moeoObjectiveVectorTraits"); - return bObj[_i]; - } - + static bool minimizing(unsigned _i) + { + // in case there would be a wrong index + if (_i >= bObj.size()) + throw std::runtime_error("Wrong index in moeoObjectiveVectorTraits"); + return bObj[_i]; + } + /** * Returns true if the _ith objective have to be maximized * @param _i the index */ - static bool maximizing (unsigned _i) - { - return (!minimizing (_i)); - } - + static bool maximizing(unsigned _i) { + return (! minimizing(_i)); + } + /** * Returns the tolerance value (to compare solutions) */ - static double tolerance () - { - return tol; - } + static double tolerance() + { + return tol; + } private: /** The number of objectives */ - static unsigned nObj; + static unsigned nObj; /** The min/max vector */ - static std::vector < bool > bObj; - + static std::vector < bool > bObj; + }; -#endif /*MOEOOBJECTIVEVECTORTRAITS_H_ */ +#endif /*MOEOOBJECTIVEVECTORTRAITS_H_*/ // The static variables of the moeoObjectiveVectorTraits class need to be allocated // (maybe it would have been better to put this on a moeoObjectiveVectorTraits.cpp file) -unsigned - moeoObjectiveVectorTraits::nObj; +unsigned moeoObjectiveVectorTraits::nObj; std::vector < bool > moeoObjectiveVectorTraits::bObj; diff --git a/branches/paradiseo-moeo-1.0/src/moeoReplacement.h b/branches/paradiseo-moeo-1.0/src/moeoReplacement.h index 62b1b9f66..aa0631754 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoReplacement.h +++ b/branches/paradiseo-moeo-1.0/src/moeoReplacement.h @@ -16,10 +16,9 @@ #include /** - * Replacement strategy for multi-objective optimization + * Replacement strategy for multi-objective optimization. */ -template < class MOEOT > class moeoReplacement:public eoReplacement < MOEOT > -{ -}; +template < class MOEOT > +class moeoReplacement : public eoReplacement < MOEOT > {}; -#endif /*MOEOREPLACEMENT_H_ */ +#endif /*MOEOREPLACEMENT_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/moeoSelectOne.h b/branches/paradiseo-moeo-1.0/src/moeoSelectOne.h index 1094a89ec..7f4129b99 100644 --- a/branches/paradiseo-moeo-1.0/src/moeoSelectOne.h +++ b/branches/paradiseo-moeo-1.0/src/moeoSelectOne.h @@ -16,8 +16,9 @@ #include /** - * Selection strategy for multi-objective optimization that selects only one element from a whole population + * Selection strategy for multi-objective optimization that selects only one element from a whole population. */ -template < class MOEOT > class moeoSelectOne : public eoSelectOne < MOEOT > {}; +template < class MOEOT > +class moeoSelectOne : public eoSelectOne < MOEOT > {}; -#endif /*MOEOSELECTONE_H_ */ +#endif /*MOEOSELECTONE_H_*/