diff --git a/trunk/paradiseo-moeo/src/MOEO.h b/trunk/paradiseo-moeo/src/MOEO.h new file mode 100644 index 000000000..05d9b32a2 --- /dev/null +++ b/trunk/paradiseo-moeo/src/MOEO.h @@ -0,0 +1,288 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// MOEO.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEO_H_ +#define MOEO_H_ + +#include +#include +#include +#include + +/** + * Base class allowing to represent a solution (an individual) for multi-objective optimization. + * The template argument MOEOObjectiveVector allows to represent the solution in the objective space (it can be a moeoObjectiveVector object). + * The template argument MOEOFitness is an object reflecting the quality of the solution in term of convergence (the fitness of a solution is always to be maximized). + * The template argument MOEODiversity is an object reflecting the quality of the solution in term of diversity (the diversity of a solution is always to be maximized). + * All template arguments must have a void and a copy constructor. + * Besides, note that, contrary to the mono-objective case (and to EO) where the fitness value of a solution is confused with its objective value, + * the fitness value differs of the objectives values in the multi-objective case. + */ +template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity > +class MOEO : public EO < MOEOObjectiveVector > +{ +public: + + /** the objective vector type of a solution */ + typedef MOEOObjectiveVector ObjectiveVector; + + /** the fitness type of a solution */ + typedef MOEOFitness Fitness; + + /** the diversity type of a solution */ + typedef MOEODiversity Diversity; + + + /** + * Ctor + */ + MOEO() + { + // default values for every parameters + objectiveVectorValue = ObjectiveVector(); + fitnessValue = Fitness(); + diversityValue = Diversity(); + // invalidate all + invalidate(); + } + + + /** + * Virtual dtor + */ + virtual ~MOEO() {}; + + + /** + * Returns the objective vector of the current solution + */ + 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; + } + + + /** + * Sets the objective vector as invalid + */ + void invalidateObjectiveVector() + { + invalidObjectiveVectorValue = true; + } + + + /** + * 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 (MOEO)"); + } + 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; + } + + + /** + * Sets the fitness value as invalid + */ + void invalidateFitness() + { + invalidFitnessValue = true; + } + + + /** + * 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"); + } + 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; + } + + + /** + * Sets the diversity value as invalid + */ + void invalidateDiversity() + { + invalidDiversityValue = true; + } + + + /** + * Returns true if the diversity value is invalid, false otherwise + */ + bool invalidDiversity() const + { + return invalidDiversityValue; + } + + + /** + * Sets the objective vector, the fitness value and the diversity value as invalid + */ + void invalidate() + { + invalidateObjectiveVector(); + invalidateFitness(); + invalidateDiversity(); + } + + + /** + * Returns true if the fitness value is invalid, false otherwise + */ + bool invalid() const + { + return invalidObjectiveVector(); + } + + + /** + * Returns true if the objective vector of the current solution is smaller than the objective vector of _other on the first objective, + * then on the second, and so on (can be usefull for sorting/printing). + * You should implement another function in the sub-class of MOEO to have another sorting mecanism. + * @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"; + } + + + /** + * Writing object + * @param _os output stream + */ + 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; + } + } + + +private: + + /** the objective vector of this solution */ + ObjectiveVector objectiveVectorValue; + /** true if the objective vector is invalid */ + bool invalidObjectiveVectorValue; + /** the fitness value of this solution */ + Fitness fitnessValue; + /** true if the fitness value is invalid */ + bool invalidFitnessValue; + /** the diversity value of this solution */ + Diversity diversityValue; + /** true if the diversity value is invalid */ + bool invalidDiversityValue; + +}; + +#endif /*MOEO_H_*/ diff --git a/trunk/paradiseo-moeo/src/Makefile.am b/trunk/paradiseo-moeo/src/Makefile.am old mode 100755 new mode 100644 diff --git a/trunk/paradiseo-moeo/src/moeo b/trunk/paradiseo-moeo/src/moeo index 3bf19e773..5a978a61d 100644 --- a/trunk/paradiseo-moeo/src/moeo +++ b/trunk/paradiseo-moeo/src/moeo @@ -2,7 +2,7 @@ //----------------------------------------------------------------------------- // moeo -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 /* This library... @@ -13,6 +13,43 @@ #ifndef MOEO_ #define MOEO_ -#include +#include -#endif /*MOEO_H_*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif /*MOEO_*/ diff --git a/trunk/paradiseo-moeo/src/moeo.h b/trunk/paradiseo-moeo/src/moeo.h deleted file mode 100644 index 8478ee48e..000000000 --- a/trunk/paradiseo-moeo/src/moeo.h +++ /dev/null @@ -1,31 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeo.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr - */ -//----------------------------------------------------------------------------- - -#ifndef MOEO_H_ -#define MOEO_H_ - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#endif /*MOEO_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoArchive.h b/trunk/paradiseo-moeo/src/moeoArchive.h index 9547dd06e..d9dfa0a01 100644 --- a/trunk/paradiseo-moeo/src/moeoArchive.h +++ b/trunk/paradiseo-moeo/src/moeoArchive.h @@ -2,7 +2,7 @@ //----------------------------------------------------------------------------- // moeoArchive.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 /* This library... @@ -14,92 +14,165 @@ #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 EOT > class moeoArchive:public eoPop < EOT > +template < class MOEOT > +class moeoArchive : public eoPop < MOEOT > { public: - using std::vector < EOT >::size; - using std::vector < EOT >::operator[]; - using std::vector < EOT >::back; - using std::vector < EOT >::pop_back; + using std::vector < MOEOT > :: size; + using std::vector < MOEOT > :: operator[]; + using std::vector < MOEOT > :: back; + using std::vector < MOEOT > :: pop_back; - /** - * The fitness type of a solution - */ - typedef typename EOT::Fitness EOFitness; - /** - * Returns true if the current archive dominates _fit - * @param _fit the (Pareto) fitness to compare with the current archive - */ - bool dominates (const EOFitness & _fit) const - { - for (unsigned i = 0; i < size; i++) - if (operator[](i).fitness ().dominates (_fit)) - return true; - return false; - } + /** + * The type of an objective vector for a solution + */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; - /** - * Returns true if the current archive contains _fit - * @param _fit the (Pareto) fitness to search within the current archive - */ - bool contains (const EOFitness & _fit) const - { - for (unsigned i = 0; i < size; i++) - if (operator[](i).fitness () == _fit) - return true; - return false; - } - /** - * Updates the archive with a given individual _eo - * @param _eo the given individual - */ - void update (const EOT & _eo) - { - // Removing the dominated solutions from the archive - for (unsigned j = 0; j < size ();) - { - if (_eo.fitness ().dominates (operator[](j).fitness ())) - { - operator[](j) = back (); - pop_back (); - } - else if (_eo.fitness () == operator[](j).fitness ()) - { - operator[](j) = back (); - pop_back (); - } - else - j++; - } + /** + * Default ctor. + * The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance + */ + moeoArchive() : eoPop < MOEOT >(), comparator(paretoComparator) + {} - // Dominated ? - bool dom = false; - for (unsigned j = 0; j < size (); j++) - if (operator [](j).fitness ().dominates (_eo.fitness ())) - { - dom = true; - break; - } - if (!dom) - push_back (_eo); - } - /** - * Updates the archive with a given population _pop - * @param _pop the given population - */ - void update (const eoPop < EOT > &_pop) - { - for (unsigned i = 0; i < _pop.size (); i++) - update (_pop[i]); - } + /** + * Ctor + * @param _comparator the moeoObjectiveVectorComparator used to compare solutions + */ + 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 & _pop) + { + for (unsigned i=0; i<_pop.size(); i++) + { + update(_pop[i]); + } + } + + + /** + * Returns true if the current archive contains the same objective vectors + * than the given archive _arch + * @param _arch the given archive + */ + bool equals (const moeoArchive < MOEOT > & _arch) + { + for (unsigned i=0; i & comparator; + /** A moeoObjectiveVectorComparator based on Pareto dominance (used as default) */ + moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; }; diff --git a/trunk/paradiseo-moeo/src/moeoArchiveFitnessSavingUpdater.h b/trunk/paradiseo-moeo/src/moeoArchiveFitnessSavingUpdater.h deleted file mode 100644 index b1e839331..000000000 --- a/trunk/paradiseo-moeo/src/moeoArchiveFitnessSavingUpdater.h +++ /dev/null @@ -1,73 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeoArchiveFitnessSavingUpdater.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr - */ -//----------------------------------------------------------------------------- - -#ifndef MOEOARCHIVEFITNESSSAVINGUPDATER_H_ -#define MOEOARCHIVEFITNESSSAVINGUPDATER_H_ - -#include -#include -#include -#include -#include - -#define MAX_BUFFER_SIZE 1000 - -/** - * 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 -{ -public: - - /** - * Ctor - * @param _arch local archive - * @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) - { - } - - /** - * Saves the fitness of the archive's members into the file - */ - void operator () () - { - char buff[MAX_BUFFER_SIZE]; - if (id == -1) - sprintf (buff, "%s.%u", filename.c_str (), counter++); - else - 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].fitness () << std::endl; - f.close (); - } - - -private: - - /** local archive */ - moeoArchive < EOT > &arch; - /** target filename */ - std::string filename; - /** own ID */ - int id; - /** counter */ - unsigned counter; - -}; - -#endif /*MOEOARCHIVEFITNESSSAVINGUPDATER_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoArchiveObjectiveVectorSavingUpdater.h b/trunk/paradiseo-moeo/src/moeoArchiveObjectiveVectorSavingUpdater.h new file mode 100644 index 000000000..16c3c3d4c --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoArchiveObjectiveVectorSavingUpdater.h @@ -0,0 +1,70 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoArchiveObjectiveVectorSavingUpdater.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOARCHIVEOBJECTIVEVECTORSAVINGUPDATER_H_ +#define MOEOARCHIVEOBJECTIVEVECTORSAVINGUPDATER_H_ + +#include +#include +#include +#include +#include + +#define MAX_BUFFER_SIZE 1000 + +/** + * This class allows to save the fitnesses of solutions contained in an archive into a file at each generation. + */ +template +class moeoArchiveObjectiveVectorSavingUpdater : public eoUpdater +{ +public: + + /** + * Ctor + * @param _arch local archive + * @param _filename target filename + * @param _id own ID + */ + moeoArchiveObjectiveVectorSavingUpdater (moeoArchive & _arch, const std::string & _filename, int _id = -1) : arch(_arch), filename(_filename), id(_id), counter(0) + {} + + /** + * Saves the fitness of the archive's members into the file + */ + void operator()() { + char buff[MAX_BUFFER_SIZE]; + if (id == -1) + sprintf (buff, "%s.%u", filename.c_str(), counter ++); + else + 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.close (); + } + + +private: + + /** local archive */ + moeoArchive & arch; + /** target filename */ + std::string filename; + /** own ID */ + int id; + /** counter */ + unsigned counter; + +}; + +#endif /*MOEOARCHIVEOBJECTIVEVECTORSAVINGUPDATER_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoArchiveUpdater.h b/trunk/paradiseo-moeo/src/moeoArchiveUpdater.h index 581742849..73fbdc687 100644 --- a/trunk/paradiseo-moeo/src/moeoArchiveUpdater.h +++ b/trunk/paradiseo-moeo/src/moeoArchiveUpdater.h @@ -2,7 +2,7 @@ //----------------------------------------------------------------------------- // moeoArchiveUpdater.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 /* This library... @@ -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 MOEOT > +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) - { - } + /** + * Ctor + * @param _arch an archive of non-dominated solutions + * @param _pop the main population + */ + moeoArchiveUpdater(moeoArchive < MOEOT > & _arch, const eoPop < MOEOT > & _pop) : arch(_arch), pop(_pop) + {} - /** - * Updates the archive with newly found non-dominated solutions contained in the main population - */ - void operator () () - { - arch.update (pop); - } + /** + * Updates the archive with newly found non-dominated solutions contained in the main population + */ + void operator()() { + arch.update(pop); + } private: - /** the archive of non-dominated solutions */ - moeoArchive < EOT > &arch; - /** the main population */ - const eoPop < EOT > &pop; + /** the archive of non-dominated solutions */ + moeoArchive < MOEOT > & arch; + /** the main population */ + const eoPop < MOEOT > & pop; }; -#endif /*MOEOARCHIVEUPDATER_H_ */ +#endif /*MOEOARCHIVEUPDATER_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoCombinedLS.h b/trunk/paradiseo-moeo/src/moeoCombinedLS.h new file mode 100644 index 000000000..98727f700 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoCombinedLS.h @@ -0,0 +1,66 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoCombinedLS.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOCOMBINEDLS_H_ +#define MOEOCOMBINEDLS_H_ + +#include +#include + +/** + * 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. + */ +template < class MOEOT, class Type > +class moeoCombinedLS : public moeoLS < MOEOT, Type > +{ +public: + + /** + * Ctor + * @param _first_mols the first multi-objective local search to add + */ + moeoCombinedLS(moeoLS < MOEOT, Type > & _first_mols) + { + combinedLS.push_back (& _first_mols); + } + + /** + * Adds a new local search to combine + * @param _mols the multi-objective local search to add + */ + void add(moeoLS < MOEOT, Type > & _mols) + { + combinedLS.push_back(& _mols); + } + + /** + * Gives a new solution in order to explore the neigborhood. + * The new non-dominated solutions are added to the archive + * @param _type the object to apply the local search to + * @param _arch the archive of non-dominated solutions + */ + void operator () (Type _type, moeoArchive < MOEOT > & _arch) + { + for (unsigned i=0; i operator()(_type, _arch); + } + + +private: + + /** the vector that contains the combined LS */ + std::vector< moeoLS < MOEOT, Type > * > combinedLS; + +}; + +#endif /*MOEOCOMBINEDLS_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoCombinedMOLS.h b/trunk/paradiseo-moeo/src/moeoCombinedMOLS.h deleted file mode 100644 index 30c9dc206..000000000 --- a/trunk/paradiseo-moeo/src/moeoCombinedMOLS.h +++ /dev/null @@ -1,71 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeoCombinedMOLS.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr - */ -//----------------------------------------------------------------------------- - -#ifndef MOEOCOMBINEDMOLS_H_ -#define MOEOCOMBINEDMOLS_H_ - -#include -#include -#include - -/** - * 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 - */ -template < class EOT > class moeoCombinedMOLS:public moeoMOLS < EOT > -{ -public: - - /** - * Ctor - * @param _eval the full evaluator of a solution - * @param _first_ls the first multi-objective local search to add - */ -moeoCombinedMOLS (eoEvalFunc < EOT > &_eval, moeoMOLS < EOT > &_first_ls):eval - (_eval) - { - combinedMOLS.push_back (&_first_ls); - } - - /** - * Adds a new local search to combine - * @param _ls the multi-objective local search to add - */ - void add (moeoMOLS < EOT > &_ls) - { - combinedMOLS.push_back (&_ls); - } - - /** - * 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 _arch the archive of non-dominated solutions - */ - void operator () (const EOT & _eo, moeoArchive < EOT > &_arch) - { - eval (const_cast < EOT & >(_eo)); - for (unsigned i = 0; i < combinedMOLS.size (); i++) - combinedMOLS[i]->operator ()(_eo, _arch); - } - - -private: - - /** the full evaluator of a solution */ - eoEvalFunc < EOT > &eval; - /** the vector that contains the combined MOLS */ - std::vector < moeoMOLS < EOT > *>combinedMOLS; - -}; - -#endif /*MOEOCOMBINEDMOLS_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoComparator.h b/trunk/paradiseo-moeo/src/moeoComparator.h new file mode 100644 index 000000000..064c0f78a --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoComparator.h @@ -0,0 +1,133 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOCOMPARATOR_H_ +#define MOEOCOMPARATOR_H_ + +#include + +/** + * Functor allowing to compare two solutions. + */ +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. + */ +template < class MOEOT > +class moeoObjectiveComparator : public moeoComparator < MOEOT > +{ +public: + /** + * Returns true if _moeo1 is greater than _moeo2 on the first objective, then on the second, and so on + * @param _moeo1 the first solution + * @param _moeo2 the second solution + */ + const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return _moeo1.objectiveVector() > _moeo2.objectiveVector(); + } +}; + +/** + * Functor allowing to compare two solutions according to one objective. + */ +template < class MOEOT > +class moeoOneObjectiveComparator : public moeoComparator < MOEOT > +{ +public: + + /** + * Ctor. + * @param _obj the index of objective + */ + moeoOneObjectiveComparator(unsigned _obj) : obj(_obj) + { + if (obj > MOEOT::ObjectiveVector::nObjectives()) + { + throw std::runtime_error("Problem with the index of objective in moeoOneObjectiveComparator"); + } + } + + /** + * Returns true if _moeo1 is greater than _moeo2 on the obj objective + * @param _moeo1 the first solution + * @param _moeo2 the second solution + */ + const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return _moeo1.objectiveVector()[obj] > _moeo2.objectiveVector()[obj]; + } + +private: + /** the index of objective */ + unsigned obj; + +}; + + +/** + * 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 > +{ +public: + /** + * Returns true if _moeo1 is greater than _moeo2 according to their fitness values, then according to their diversity values + * @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(); + } + } +}; + + +/** + * 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 > +{ +public: + /** + * Returns true if _moeo1 is greater than _moeo2 according to their diversity values, then according to their fitness values + * @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(); + } + } +}; + + +#endif /*MOEOCOMPARATOR_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoConvertPopToObjectiveVectors.h b/trunk/paradiseo-moeo/src/moeoConvertPopToObjectiveVectors.h new file mode 100644 index 000000000..8afca7a7b --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoConvertPopToObjectiveVectors.h @@ -0,0 +1,42 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoConvertPopToObjectiveVectors.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOPOPTOOBJECTIVEVECTORS_H_ +#define MOEOPOPTOOBJECTIVEVECTORS_H_ + +#include + +/** + * Functor allowing to get a vector of objective vectors from a population + */ +template < class MOEOT, class ObjectiveVector = typename MOEOT::ObjectiveVector > +class moeoConvertPopToObjectiveVectors : public eoUF < const eoPop < MOEOT >, const std::vector < ObjectiveVector > > +{ +public: + + /** + * Returns a vector of the objective vectors from the population _pop + * @param _pop the population + */ + const std::vector < ObjectiveVector > operator()(const eoPop < MOEOT > _pop) + { + std::vector < ObjectiveVector > result; + result.resize(_pop.size()); + for (unsigned i=0; i<_pop.size(); i++) + { + result.push_back(_pop[i].objectiveVector()); + } + return result; + } +}; + +#endif /*MOEOPOPTOOBJECTIVEVECTORS_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoCrowdingDistanceDiversityAssignment.h b/trunk/paradiseo-moeo/src/moeoCrowdingDistanceDiversityAssignment.h new file mode 100755 index 000000000..aa8eec28e --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoCrowdingDistanceDiversityAssignment.h @@ -0,0 +1,114 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoCrowdingDistanceDiversityAssignment.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_ +#define MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_ + +#include +#include +#include + +/** + * Diversity assignment sheme based on crowding distance proposed 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-II. + */ +template < class MOEOT > +class moeoCrowdingDistanceDiversityAssignment : public moeoDiversityAssignment < MOEOT > +{ +public: + + /** the objective vector type of the solutions */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Returns a big value (regarded as infinite) + */ + double inf() const + { + return std::numeric_limits::max(); + } + + + /** + * Computes diversity values for every solution contained in the population _pop + * @param _pop the population + */ + void operator()(eoPop < MOEOT > & _pop) + { + if (_pop.size() <= 2) + { + for (unsigned i=0; i<_pop.size(); i++) + { + _pop[i].diversity(inf()); + } + } + else + { + setDistances(_pop); + } + } + + + /** + * @warning NOT IMPLEMENTED, DO NOTHING ! + * Updates the diversity values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + * @warning NOT IMPLEMENTED, DO NOTHING ! + */ + void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + cout << "WARNING : updateByDeleting not implemented in moeoCrowdingDiversityAssignment" << endl; + } + + +private: + + /** + * Sets the distance values + * @param _pop the population + */ + void setDistances (eoPop < MOEOT > & _pop) + { + double min, max, distance; + unsigned nObjectives = MOEOT::ObjectiveVector::nObjectives(); + // set diversity to 0 + for (unsigned i=0; i<_pop.size(); i++) + { + _pop[i].diversity(0); + } + // for each objective + for (unsigned obj=0; obj comp(obj); + // sort + std::sort(_pop.begin(), _pop.end(), comp); + // min & max + min = _pop[0].objectiveVector()[obj]; + max = _pop[_pop.size()-1].objectiveVector()[obj]; + // set the diversity value to infiny for min and max + _pop[0].diversity(inf()); + _pop[_pop.size()-1].diversity(inf()); + for (unsigned i=1; i<_pop.size()-1; i++) + { + distance = (_pop[i+1].objectiveVector()[obj] - _pop[i-1].objectiveVector()[obj]) / (max-min); + _pop[i].diversity(_pop[i].diversity() + distance); + } + } + } + +}; + +#endif /*MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoDetTournamentSelect.h b/trunk/paradiseo-moeo/src/moeoDetTournamentSelect.h new file mode 100644 index 000000000..e2b565ef2 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoDetTournamentSelect.h @@ -0,0 +1,84 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoDetTournamentSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEODETTOURNAMENTSELECT_H_ +#define MOEODETTOURNAMENTSELECT_H_ + +#include +#include +#include + +/** + * Selection strategy that selects ONE individual by deterministic tournament. + */ +template < class MOEOT > class moeoDetTournamentSelect:public moeoSelectOne < + MOEOT > +{ +public: + + /** + * Full Ctor. + * @param _comparator the comparator (used to compare 2 individuals) + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoDetTournamentSelect (moeoComparator < MOEOT > &_comparator, unsigned _tSize = 2): + comparator (_comparator), tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + + /** + * Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default. + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoDetTournamentSelect (unsigned _tSize = 2): + comparator (*(new moeoFitnessThenDiversityComparator < MOEOT > ())), + tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + + /** + * Apply the tournament to the given population + * @param _pop the population + */ + const MOEOT & operator () (const eoPop < MOEOT > &_pop) + { + // use the selector + return mo_deterministic_tournament (_pop, tSize, comparator); + } + + +protected: + + /** the comparator (used to compare 2 individuals) */ + moeoComparator < MOEOT > &comparator; + + /** the number of individuals in the tournament */ + unsigned tSize; +}; + +#endif /*MOEODETTOURNAMENTSELECT_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoDiversityAssignment.h b/trunk/paradiseo-moeo/src/moeoDiversityAssignment.h new file mode 100644 index 000000000..66e50dd38 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoDiversityAssignment.h @@ -0,0 +1,93 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoDiversityAssignment.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEODIVERSITYASSIGNMENT_H_ +#define MOEODIVERSITYASSIGNMENT_H_ + +#include +#include + +/** + * Functor that sets the diversity values of a whole population. + */ +template < class MOEOT > +class moeoDiversityAssignment : public eoUF < eoPop < MOEOT > &, void > +{ +public: + + /** The type for objective vector */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Updates the diversity values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + */ + virtual void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) = 0; + + + /** + * Updates the diversity values of the whole population _pop by taking the deletion of the individual _moeo into account. + * @param _pop the population + * @param _moeo the individual + */ + void updateByDeleting(eoPop < MOEOT > & _pop, MOEOT & _moeo) + { + updateByDeleting(_pop, _moeo.objectiveVector()); + } + +}; + + +/** + * moeoDummyDiversityAssignment is a moeoDiversityAssignment that gives the value '0' as the individual's diversity for a whole population if it is invalid. + */ +template < class MOEOT > +class moeoDummyDiversityAssignment : public moeoDiversityAssignment < MOEOT > +{ +public: + + /** The type for objective vector */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Sets the diversity to '0' for every individuals of the population _pop if it is invalid + * @param _pop the population + */ + void operator () (eoPop < MOEOT > & _pop) + { + for (unsigned idx = 0; idx<_pop.size (); idx++) + { + if (_pop[idx].invalidDiversity()) + { + // set the diversity to 0 + _pop[idx].diversity(0.0); + } + } + } + + + /** + * Updates the diversity values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + */ + void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + // nothing to do... ;-) + } + +}; + +#endif /*MOEODIVERSITYASSIGNMENT_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoEA.h b/trunk/paradiseo-moeo/src/moeoEA.h new file mode 100644 index 000000000..901dba540 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoEA.h @@ -0,0 +1,25 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoEA.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOEA_H_ +#define MOEOEA_H_ + +#include + +/** + * Abstract class for multi-objective evolutionary algorithms. + */ +template < class MOEOT > +class moeoEA : public eoAlgo < MOEOT > {}; + + +#endif /*MOEOEA_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoEasyEA.h b/trunk/paradiseo-moeo/src/moeoEasyEA.h new file mode 100755 index 000000000..718bd2cab --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoEasyEA.h @@ -0,0 +1,120 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoEasyEA.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef _MOEOEASYEA_H +#define _MOEOEASYEA_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * An easy class to design multi-objective evolutionary algorithms. + */ +template < class MOEOT > +class moeoEasyEA: public moeoEA < MOEOT > +{ +public: + + /** + * Ctor. + * @param _continuator the stopping criteria + * @param _eval the evaluation functions + * @param _breed the breeder + * @param _replace the replacment strategy + * @param _fitnessEval the fitness evaluation scheme + * @param _diversityEval the diversity evaluation scheme + * @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process + */ + moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoBreed < MOEOT > & _breed, eoReplacement < MOEOT > & _replace, + moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) + : + continuator(_continuator), eval (_eval), loopEval(_eval), popEval(loopEval), breed(_breed), replace(_replace), fitnessEval(_fitnessEval), + diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection) + {} + + + /** + * Applies a few generation of evolution to the population _pop. + * @param _pop the population + */ + virtual void operator()(eoPop < MOEOT > & _pop) + { + eoPop < MOEOT > offspring, empty_pop; + popEval(empty_pop, _pop); // A first eval of pop. + bool firstTime = true; + do + { + try + { + unsigned pSize = _pop.size(); + offspring.clear(); // new offspring + // fitness and diversity assignment (if you want to or if it is the first generation) + if (evalFitAndDivBeforeSelection || firstTime) + { + firstTime = false; + fitnessEval(_pop); + diversityEval(_pop); + } + breed(_pop, offspring); + popEval(_pop, offspring); // eval of parents + offspring if necessary + replace(_pop, offspring); // after replace, the new pop. is in _pop + if (pSize > _pop.size()) + { + throw std::runtime_error("Population shrinking!"); + } + else if (pSize < _pop.size()) + { + throw std::runtime_error("Population growing!"); + } + } + catch (std::exception& e) + { + std::string s = e.what(); + s.append( " in moeoEasyEA"); + throw std::runtime_error( s ); + } + } while (continuator(_pop)); + } + + +protected: + + /** the stopping criteria */ + eoContinue < MOEOT > & continuator; + /** the evaluation functions */ + eoEvalFunc < MOEOT > & eval; + /** to evaluate the whole population */ + eoPopLoopEval < MOEOT > loopEval; + /** to evaluate the whole population */ + eoPopEvalFunc < MOEOT > & popEval; + /** the breeder */ + eoBreed < MOEOT > & breed; + /** the replacment strategy */ + eoReplacement < MOEOT > & replace; + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & fitnessEval; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & diversityEval; + /** if this parameter is set to 'true', the fitness and the diversity of the whole population will be re-evaluated before the selection process */ + bool evalFitAndDivBeforeSelection; + +}; + +#endif /*MOEOEASYEA_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoElitistReplacement.h b/trunk/paradiseo-moeo/src/moeoElitistReplacement.h new file mode 100644 index 000000000..49b8e4cd4 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoElitistReplacement.h @@ -0,0 +1,137 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoElitistReplacement.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOELITISTREPLACEMENT_H_ +#define MOEOELITISTREPLACEMENT_H_ + +#include +#include +#include +#include + +/** + * Elitist replacement strategy that consists in keeping the N best individuals. + */ +template < class MOEOT > class moeoElitistReplacement:public moeoReplacement < MOEOT > +{ +public: + + /** + * Full constructor. + * @param _evalFitness the fitness assignment strategy + * @param _evalDiversity the diversity assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoDiversityAssignment < MOEOT > & _evalDiversity, moeoComparator < MOEOT > & _comparator) : + evalFitness (_evalFitness), evalDiversity (_evalDiversity), comparator (_comparator) + {} + + + /** + * Constructor without comparator. A moeoFitThenDivComparator is used as default. + * @param _evalFitness the fitness assignment strategy + * @param _evalDiversity the diversity assignment strategy + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoDiversityAssignment < MOEOT > & _evalDiversity) : + evalFitness (_evalFitness), evalDiversity (_evalDiversity), comparator (*(new moeoFitnessThenDiversityComparator < MOEOT >)) + {} + + + /** + * Constructor without moeoDiversityAssignement. A dummy diversity is used as default. + * @param _evalFitness the fitness assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoComparator < MOEOT > & _comparator) : + evalFitness (_evalFitness), evalDiversity (*(new moeoDummyDiversityAssignment < MOEOT >)), comparator (_comparator) + {} + + + /** + * Constructor without moeoDiversityAssignement nor moeoComparator. + * A moeoFitThenDivComparator and a dummy diversity are used as default. + * @param _evalFitness the fitness assignment strategy + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness) : + evalFitness (_evalFitness), evalDiversity (*(new moeoDummyDiversityAssignment < MOEOT >)), comparator (*(new moeoFitnessThenDiversityComparator < MOEOT >)) + {} + + + /** + * Replaces the first population by adding the individuals of the second one, sorting with a moeoComparator and resizing the whole population obtained. + * @param _parents the population composed of the parents (the population you want to replace) + * @param _offspring the offspring population + */ + void operator () (eoPop < MOEOT > &_parents, eoPop < MOEOT > &_offspring) + { + unsigned sz = _parents.size (); + // merges offspring and parents into a global population + _parents.reserve (_parents.size () + _offspring.size ()); + copy (_offspring.begin (), _offspring.end (), back_inserter (_parents)); + // evaluates the fitness and the diversity of this global population + evalFitness (_parents); + evalDiversity (_parents); + // sorts the whole population according to the comparator + Cmp cmp(comparator); + std::sort(_parents.begin(), _parents.end(), cmp); + // finally, resize this global population + _parents.resize (sz); + // and clear the offspring population + _offspring.clear (); + } + + +protected: + + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & evalFitness; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & evalDiversity; + /** the comparator (used to compare 2 individuals) */ + moeoComparator < MOEOT > & comparator; + + /** + * This class is used to compare solutions in order to sort the population. + */ + class Cmp + { + public: + + /** + * Ctor. + * @param _comparator the comparator + */ + Cmp(moeoComparator < MOEOT > & _comparator) : comparator(_comparator) + {} + + + /** + * Returns true if _moeo1 is greater than _moeo2 according to the comparator + * _moeo1 the first individual + * _moeo2 the first individual + */ + bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return comparator(_moeo1,_moeo2); + } + + + private: + + /** the comparator */ + moeoComparator < MOEOT > & comparator; + + }; + +}; + +#endif /*MOEOELITISTREPLACEMENT_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoEnvironmentalReplacement.h b/trunk/paradiseo-moeo/src/moeoEnvironmentalReplacement.h new file mode 100755 index 000000000..f7dce3c50 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoEnvironmentalReplacement.h @@ -0,0 +1,149 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoEnvironmentalReplacement.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOENVIRONMENTALREPLACEMENT_H_ +#define MOEOENVIRONMENTALREPLACEMENT_H_ + +#include +#include +#include +#include + +/** + * Environmental replacement strategy that consists in keeping the N best individuals by deleting individuals 1 by 1 + * and by updating the fitness and diversity values after each deletion. + */ +template < class MOEOT > class moeoEnvironmentalReplacement:public moeoReplacement < MOEOT > +{ +public: + + /** The type for objective vector */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Full constructor. + * @param _evalFitness the fitness assignment strategy + * @param _evalDiversity the diversity assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoDiversityAssignment < MOEOT > & _evalDiversity, moeoComparator < MOEOT > & _comparator) : + evalFitness (_evalFitness), evalDiversity (_evalDiversity), comparator (_comparator) + {} + + + /** + * Constructor without comparator. A moeoFitThenDivComparator is used as default. + * @param _evalFitness the fitness assignment strategy + * @param _evalDiversity the diversity assignment strategy + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoDiversityAssignment < MOEOT > & _evalDiversity) : + evalFitness (_evalFitness), evalDiversity (_evalDiversity), comparator (*(new moeoFitnessThenDiversityComparator < MOEOT >)) + {} + + + /** + * Constructor without moeoDiversityAssignement. A dummy diversity is used as default. + * @param _evalFitness the fitness assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoComparator < MOEOT > & _comparator) : + evalFitness (_evalFitness), evalDiversity (*(new moeoDummyDiversityAssignment < MOEOT >)), comparator (_comparator) + {} + + + /** + * Constructor without moeoDiversityAssignement nor moeoComparator. + * A moeoFitThenDivComparator and a dummy diversity are used as default. + * @param _evalFitness the fitness assignment strategy + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness) : + evalFitness (_evalFitness), evalDiversity (*(new moeoDummyDiversityAssignment < MOEOT >)), comparator (*(new moeoFitnessThenDiversityComparator < MOEOT >)) + {} + + + /** + * Replaces the first population by adding the individuals of the second one, sorting with a moeoComparator and resizing the whole population obtained. + * @param _parents the population composed of the parents (the population you want to replace) + * @param _offspring the offspring population + */ + void operator () (eoPop < MOEOT > &_parents, eoPop < MOEOT > &_offspring) + { + unsigned sz = _parents.size(); + // merges offspring and parents into a global population + _parents.reserve (_parents.size() + _offspring.size()); + copy (_offspring.begin(), _offspring.end(), back_inserter(_parents)); + // evaluates the fitness and the diversity of this global population + evalFitness (_parents); + evalDiversity (_parents); + // remove individuals 1 by 1 and update the fitness values + Cmp cmp(comparator); + ObjectiveVector worstObjVec; + while (_parents.size() > sz) + { + std::sort (_parents.begin(), _parents.end(), cmp); + worstObjVec = _parents[_parents.size()-1].objectiveVector(); + _parents.resize(_parents.size()-1); + evalFitness.updateByDeleting(_parents, worstObjVec); + evalDiversity.updateByDeleting(_parents, worstObjVec); + } + // clear the offspring population + _offspring.clear (); + } + + +protected: + + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & evalFitness; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & evalDiversity; + /** the comparator (used to compare 2 individuals) */ + moeoComparator < MOEOT > & comparator; + + + /** + * This class is used to compare solutions in order to sort the population. + */ + class Cmp + { + public: + + /** + * Ctor. + * @param _comparator the comparator + */ + Cmp(moeoComparator < MOEOT > & _comparator) : comparator(_comparator) + {} + + + /** + * Returns true if _moeo1 is greater than _moeo2 according to the comparator + * _moeo1 the first individual + * _moeo2 the first individual + */ + bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return comparator(_moeo1,_moeo2); + } + + + private: + + /** the comparator */ + moeoComparator < MOEOT > & comparator; + + }; + +}; + +#endif /*MOEOENVIRONMENTALREPLACEMENT_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoEvalFunc.h b/trunk/paradiseo-moeo/src/moeoEvalFunc.h new file mode 100644 index 000000000..1f6b7544d --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoEvalFunc.h @@ -0,0 +1,24 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoEvalFunc.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOEVALFUNC_H_ +#define MOEOEVALFUNC_H_ + +#include + +/* + * Functor that evaluates one MOEO by setting all its objective values. + */ +template < class MOEOT > +class moeoEvalFunc : public eoEvalFunc< MOEOT > {}; + +#endif /*MOEOEVALFUNC_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoFastNonDominatedSortingFitnessAssignment.h b/trunk/paradiseo-moeo/src/moeoFastNonDominatedSortingFitnessAssignment.h new file mode 100644 index 000000000..95e558c02 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoFastNonDominatedSortingFitnessAssignment.h @@ -0,0 +1,210 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoFastNonDominatedSortingFitnessAssignment.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOFASTNONDOMINATEDSORTINGFITNESSASSIGNMENT_H_ +#define MOEOFASTNONDOMINATEDSORTINGFITNESSASSIGNMENT_H_ + +#include +#include +#include +#include + +/** + * 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: + * 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 > +{ +public: + + /** the objective vector type of the solutions */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Default ctor + */ + moeoFastNonDominatedSortingFitnessAssignment() : comparator(paretoComparator) + {} + + + /** + * Ctor where you can choose your own way to compare objective vectors + * @param _comparator the functor used to compare objective vectors + */ + moeoFastNonDominatedSortingFitnessAssignment(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator) : comparator(_comparator) + {} + + + /** + * Sets the 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 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 moeoNonDominatedSortingFitnessAssignment"); + } + // a higher fitness is better, so the values need to be inverted + double max = _pop[0].fitness(); + for (unsigned i=1 ; i<_pop.size() ; i++) + { + max = std::max(max, _pop[i].fitness()); + } + for (unsigned i=0 ; i<_pop.size() ; i++) + { + _pop[i].fitness(max - _pop[i].fitness()); + } + } + + + /** + * @warning NOT IMPLEMENTED, DO NOTHING ! + * Updates the fitness values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + * @warning NOT IMPLEMENTED, DO NOTHING ! + */ + void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + cout << "WARNING : updateByDeleting not implemented in moeoNonDominatedSortingFitnessAssignment" << endl; + } + + +private: + + /** Functor to compare two objective vectors */ + moeoObjectiveVectorComparator < ObjectiveVector > & comparator; + /** Functor to compare two objective vectors according to Pareto dominance relation */ + moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; + + + + + /** + * Sets the fitness values for mono-objective problems + * @param _pop the population + */ + void oneObjective (eoPop < MOEOT > & _pop) + { + // Functor to compare two solutions on the first objective, then on the second, and so on + moeoObjectiveComparator < MOEOT > objComparator; + 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 ! + } + + + /** + * 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 > 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 +#include + +/** + * Functor that sets the fitness values of a whole population. + */ +template < class MOEOT > +class moeoFitnessAssignment : public eoUF < eoPop < MOEOT > &, void > +{ +public: + + /** The type for objective vector */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Updates the fitness values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + */ + virtual void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) = 0; + + + /** + * Updates the fitness values of the whole population _pop by taking the deletion of the individual _moeo into account. + * @param _pop the population + * @param _moeo the individual + */ + void updateByDeleting(eoPop < MOEOT > & _pop, MOEOT & _moeo) + { + updateByDeleting(_pop, _moeo.objectiveVector()); + } + +}; + + +/** + * moeoDummyFitnessAssignment is a moeoFitnessAssignment that gives the value '0' as the individual's fitness for a whole population if it is invalid. + */ +template < class MOEOT > +class moeoDummyFitnessAssignment : public moeoFitnessAssignment < MOEOT > +{ +public: + + /** The type for objective vector */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Sets the fitness to '0' for every individuals of the population _pop if it is invalid + * @param _pop the population + */ + void operator () (eoPop < MOEOT > & _pop) + { + for (unsigned idx = 0; idx<_pop.size (); idx++) + { + if (_pop[idx].invalidFitness()) + { + // set the diversity to 0 + _pop[idx].fitness(0.0); + } + } + } + + + /** + * Updates the fitness values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + */ + void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + // nothing to do... ;-) + } + +}; + + +/** + * moeoScalarFitnessAssignment is a moeoFitnessAssignment for scalar strategies. + */ +template < class MOEOT > +class moeoScalarFitnessAssignment : public moeoFitnessAssignment < MOEOT > + {}; + + +/** + * moeoCriterionBasedFitnessAssignment is a moeoFitnessAssignment for criterion-based strategies. + */ +template < class MOEOT > +class moeoCriterionBasedFitnessAssignment : public moeoFitnessAssignment < MOEOT > + {}; + + +/** + * moeoParetoBasedFitnessAssignment is a moeoFitnessAssignment for Pareto-based strategies. + */ +template < class MOEOT > +class moeoParetoBasedFitnessAssignment : public moeoFitnessAssignment < MOEOT > + {}; + + +#endif /*MOEOFITNESSASSIGNMENT_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoGenerationalReplacement.h b/trunk/paradiseo-moeo/src/moeoGenerationalReplacement.h new file mode 100644 index 000000000..b7b800625 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoGenerationalReplacement.h @@ -0,0 +1,39 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoGenerationalReplacement.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOGENERATIONALREPLACEMENT_H_ +#define MOEOGENERATIONALREPLACEMENT_H_ + +#include +#include + +/** + * Generational replacement: only the new individuals are preserved. + */ +template < class MOEOT > +class moeoGenerationalReplacement : public moeoReplacement < MOEOT >, public eoGenerationalReplacement < MOEOT > +{ +public: + + /** + * Swaps _parents and _offspring + * @param _parents the parents population + * @param _offspring the offspring population + */ + void operator()(eoPop < MOEOT > & _parents, eoPop < MOEOT > & _offspring) + { + eoGenerationalReplacement < MOEOT >::operator ()(_parents, _offspring); + } + +}; + +#endif /*MOEOGENERATIONALREPLACEMENT_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoHybridLS.h b/trunk/paradiseo-moeo/src/moeoHybridLS.h new file mode 100644 index 000000000..95aad42e0 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoHybridLS.h @@ -0,0 +1,76 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoHybridLS.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOHYBRIDLS_H_ +#define MOEOHYBRIDLS_H_ + +#include +#include +#include +#include +#include +#include + +/** + * 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 +{ +public: + + /** + * Ctor + * @param _term stopping criteria + * @param _select selector + * @param _mols a multi-objective local search + * @param _arch the archive + */ + moeoHybridLS (eoContinue < MOEOT > & _term, eoSelect < MOEOT > & _select, moeoLS < MOEOT, 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 (! term (arch)) + { + // selection of solutions + eoPop < MOEOT > selectedSolutions; + select(arch, selectedSolutions); + // apply the local search to every selected solution + for (unsigned i=0; i & term; + /** selector */ + eoSelect < MOEOT > & select; + /** multi-objective local search */ + moeoLS < MOEOT, MOEOT > & mols; + /** archive */ + moeoArchive < MOEOT > & arch; + +}; + +#endif /*MOEOHYBRIDLS_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoHybridMOLS.h b/trunk/paradiseo-moeo/src/moeoHybridMOLS.h deleted file mode 100644 index fb1160cac..000000000 --- a/trunk/paradiseo-moeo/src/moeoHybridMOLS.h +++ /dev/null @@ -1,74 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeoHybridMOLS.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr - */ -//----------------------------------------------------------------------------- - -#ifndef MOEOHYBRIDMOLS_H_ -#define MOEOHYBRIDMOLS_H_ - -#include -#include -#include -#include -#include -#include - -/** - * 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 EOT > class moeoHybridMOLS:public eoUpdater -{ -public: - - /** - * Ctor - * @param _term stopping criteria - * @param _select selector - * @param _mols a multi-objective local search - * @param _arch the archive - */ -moeoHybridMOLS (eoContinue < EOT > &_term, eoSelect < EOT > &_select, moeoMOLS < EOT > &_mols, moeoArchive < EOT > &_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 (!term (arch)) - { - // selection of solutions - eoPop < EOT > selectedSolutions; - select (arch, selectedSolutions); - // apply the local search to every selected solution - for (unsigned i = 0; i < selectedSolutions.size (); i++) - mols (selectedSolutions[i], arch); - } - } - - -private: - - /** stopping criteria*/ - eoContinue < EOT > &term; - /** selector */ - eoSelect < EOT > &select; - /** multi-objective local search */ - moeoMOLS < EOT > &mols; - /** archive */ - moeoArchive < EOT > &arch; - -}; - -#endif /*MOEOHYBRIDMOLS_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoIndicatorBasedFitnessAssignment.h b/trunk/paradiseo-moeo/src/moeoIndicatorBasedFitnessAssignment.h new file mode 100644 index 000000000..0fcec7363 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoIndicatorBasedFitnessAssignment.h @@ -0,0 +1,201 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoIndicatorBasedFitnessAssignment.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOINDICATORBASEDFITNESSASSIGNMENT_H_ +#define MOEOINDICATORBASEDFITNESSASSIGNMENT_H_ + +#include +#include +#include +#include +#include + +/** + * Fitness assignment sheme based an Indicator proposed in: + * E. Zitzler, S. Künzli, "Indicator-Based Selection in Multiobjective Search", Proc. 8th International Conference on Parallel Problem Solving from Nature (PPSN VIII), pp. 832-842, Birmingham, UK (2004). + * This strategy is, for instance, used in IBEA. + */ +template < class MOEOT > +class moeoIndicatorBasedFitnessAssignment : public moeoParetoBasedFitnessAssignment < MOEOT > +{ +public: + + /** The type of objective vector */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Ctor. + * @param _metric the quality indicator + * @param _kappa the scaling factor + */ + moeoIndicatorBasedFitnessAssignment(moeoNormalizedSolutionVsSolutionBinaryMetric < ObjectiveVector, double > * _metric, const double _kappa) : metric(_metric), kappa(_kappa) + {} + + + /** + * Sets the fitness values for every solution contained in the population _pop + * @param _pop the population + */ + void operator()(eoPop < MOEOT > & _pop) + { + // 1 - setting of the bounds + setup(_pop); + // 2 - computing every indicator values + computeValues(_pop); + // 3 - setting fitnesses + setFitnesses(_pop); + } + + + /** + * Updates the fitness values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + */ + void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + vector < double > v; + v.resize(_pop.size()); + for (unsigned i=0; i<_pop.size(); i++) + { + v[i] = (*metric)(_objVec, _pop[i].objectiveVector()); + } + for (unsigned i=0; i<_pop.size(); i++) + { + _pop[i].fitness( _pop[i].fitness() + exp(-v[i]/kappa) ); + } + } + + + /** + * Updates the fitness values of the whole population _pop by taking the adding of the objective vector _objVec into account + * and returns the fitness value of _objVec. + * @param _pop the population + * @param _objVec the objective vector + */ + double updateByAdding(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + vector < double > v; + // update every fitness values to take the new individual into account + v.resize(_pop.size()); + for (unsigned i=0; i<_pop.size(); i++) + { + v[i] = (*metric)(_objVec, _pop[i].objectiveVector()); + } + for (unsigned i=0; i<_pop.size(); i++) + { + _pop[i].fitness( _pop[i].fitness() - exp(-v[i]/kappa) ); + } + // compute the fitness of the new individual + v.clear(); + v.resize(_pop.size()); + for (unsigned i=0; i<_pop.size(); i++) + { + v[i] = (*metric)(_pop[i].objectiveVector(), _objVec); + } + double result = 0; + for (unsigned i=0; i * metric; + /** the scaling factor */ + double kappa; + /** the computed indicator values */ + std::vector < std::vector > values; + + + /** + * Sets the bounds for every objective using the min and the max value for every objective vector of _pop + * @param _pop the population + */ + void setup(const eoPop < MOEOT > & _pop) + { + double min, max; + for (unsigned i=0; i & _pop) + { + values.clear(); + values.resize(_pop.size()); + for (unsigned i=0; i<_pop.size(); i++) + { + values[i].resize(_pop.size()); + for (unsigned j=0; j<_pop.size(); j++) + { + if (i != j) + { + values[i][j] = (*metric)(_pop[i].objectiveVector(), _pop[j].objectiveVector()); + } + } + } + } + + + /** + * Sets the fitness value of the whple population + * @param _pop the population + */ + void setFitnesses(eoPop < MOEOT > & _pop) + { + for (unsigned i=0; i<_pop.size(); i++) + { + _pop[i].fitness(computeFitness(i)); + } + } + + + /** + * Returns the fitness value of the _idx th individual of the population + * @param _idx the index + */ + double computeFitness(const unsigned _idx) + { + double result = 0; + for (unsigned i=0; i #include /** * Abstract class for local searches applied to multi-objective optimization. - * Starting from only one solution, it produces a set of new non-dominated solutions. + * Starting from a Type (i.e.: an individual, a pop, an archive...), it produces a set of new non-dominated solutions. */ -template < class EOT > class moeoMOLS:public eoBF < const EOT &, moeoArchive < EOT > &, - void > -{ -}; +template < class MOEOT, class Type > +class moeoLS: public eoBF < Type, moeoArchive < MOEOT > &, void > + {}; -#endif /*MOEOMOLS_H_ */ +#endif /*MOEOLS_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoNDSorting.h b/trunk/paradiseo-moeo/src/moeoNDSorting.h deleted file mode 100644 index 8cd7c4aa6..000000000 --- a/trunk/paradiseo-moeo/src/moeoNDSorting.h +++ /dev/null @@ -1,108 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeoNDSorting.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr - */ -//----------------------------------------------------------------------------- - -#ifndef moeoNDSorting_h -#define moeoNDSorting_h - -#include -#include - -# define INF 1.0e14 // DBL_MAX - -/** - * Fast Elitist Non-Dominant Sorting Genetic Algorithm assignment strategie - * Note : This is a corrected version of the original eoNDSorting_II class - * @see eoNDSorting_II - */ -template < class EOT > class moeoNDSorting_II:public eoNDSorting < EOT > -{ -public: - - /** - * constructor - */ -moeoNDSorting_II (bool nasty_flag_ = false):eoNDSorting < EOT > - (nasty_flag_) - { - } - - /** - * index pair - */ - typedef std::pair < double, unsigned >double_index_pair; - - /** - * A class to compare the nodes - */ - class compare_nodes - { - public:bool operator () (const double_index_pair & a, - const double_index_pair & b) const - { - return a.first < b.first; - } - }; - - /// _cf points into the elements that consist of the current front - std::vector < double >niche_penalty (const std::vector < unsigned >&_cf, - const eoPop < EOT > &_pop) - { - typedef typename EOT::Fitness::fitness_traits traits; - unsigned i; - std::vector < double >niche_count (_cf.size (), 0.); - - - unsigned nObjectives = traits::nObjectives (); //_pop[_cf[0]].fitness().size(); - - for (unsigned o = 0; o < nObjectives; ++o) - { - std::vector < std::pair < double, - unsigned > >performance (_cf.size ()); - for (i = 0; i < _cf.size (); ++i) - { - performance[i].first = _pop[_cf[i]].fitness ()[o]; - performance[i].second = i; - } - - std::sort (performance.begin (), performance.end (), compare_nodes ()); // a lambda operator would've been nice here - - // set boundary at INF (so it will get chosen over all the others - niche_count[performance[0].second] = INF; - niche_count[performance.back ().second] = INF; - - if (performance[0].first != performance.back ().first) - { - for (i = 1; i < _cf.size () - 1; ++i) - { - if (niche_count[performance[i].second] != INF) - { - niche_count[performance[i].second] += - (performance[i + 1].first - - performance[i - - 1].first) / (performance.back ().first - - performance[0].first); - } - } - } - } - - // transform niche_count into penality - for (i = 0; i < niche_count.size (); ++i) - { - niche_count[i] = INF - niche_count[i]; - } - - return niche_count; - } -}; - -#endif diff --git a/trunk/paradiseo-moeo/src/moeoNSGAII.h b/trunk/paradiseo-moeo/src/moeoNSGAII.h new file mode 100644 index 000000000..a83f6bb9a --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoNSGAII.h @@ -0,0 +1,127 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoNSGAII.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEONSGAII_H_ +#define MOEONSGAII_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * The NSGA-II algorithm as described in: + * Deb, K., S. Agrawal, A. Pratap, and T. Meyarivan : "A fast elitist non-dominated sorting genetic algorithm for multi-objective optimization: NSGA-II". + * In IEEE Transactions on Evolutionary Computation, Vol. 6, No 2, pp 182-197 (April 2002). + * This class builds the NSGA-II algorithm only by using the components of the ParadisEO-MOEO framework. + */ +template < class MOEOT > +class moeoNSGAII: public moeoEA < MOEOT > +{ +public: + + /** + * This constructor builds the algorithm as descibed in the paper. + * @param _max_gen number of generations before stopping + * @param _eval evaluation function + * @param _op variation operator + */ + moeoNSGAII (unsigned _max_gen, eoEvalFunc < MOEOT > & _eval, eoGenOp < MOEOT > &_op) : + continuator (*(new eoGenContinue < MOEOT > (_max_gen))), eval (_eval), loopEval (_eval), popEval (loopEval), select (2), // binary tournament selection + replace (fitnessAssignment, diversityAssignment), genBreed (select, _op), breed (genBreed) + {} + + + /** + * Ctor taking _max_gen, crossover and mutation. + * @param _max_gen number of generations before stopping + * @param _eval evaluation function + * @param _crossover crossover + * @param _pCross crossover probability + * @param _mutation mutation + * @param _pMut mutation probability + */ + moeoNSGAII (unsigned _max_gen, eoEvalFunc < MOEOT > &_eval, eoQuadOp < MOEOT > & _crossover, double _pCross, eoMonOp < MOEOT > & _mutation, double _pMut) : + continuator (*(new eoGenContinue < MOEOT > (_max_gen))), eval (_eval), loopEval (_eval), popEval (loopEval), select (2), // binary tournament selection + replace (fitnessAssignment, diversityAssignment), genBreed (select, *new eoSGAGenOp < MOEOT > (_crossover, _pCross, _mutation, _pMut)), breed (genBreed) + {} + + + /** + * Ctor taking a continuator instead of _gen_max. + * @param _continuator stopping criteria + * @param _eval evaluation function + * @param _op variation operator + */ + moeoNSGAII (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoGenOp < MOEOT > & _op) : + continuator (_continuator), eval (_eval), loopEval (_eval), popEval (loopEval), select (2), // binary tournament selection + replace (fitnessAssignment, diversityAssignment), genBreed (select, _op), breed (genBreed) + {} + + + /** + * Apply a few generation of evolution to the population _pop. + * @param _pop the population + */ + virtual void operator () (eoPop < MOEOT > &_pop) + { + eoPop < MOEOT > offspring, empty_pop; + popEval (empty_pop, _pop); // a first eval of _pop + // evaluate fitness and diversity + fitnessAssignment(_pop); + diversityAssignment(_pop); + do + { + // generate offspring, worths are recalculated if necessary + breed (_pop, offspring); + // eval of offspring + popEval (_pop, offspring); + // after replace, the new pop is in _pop. Worths are recalculated if necessary + replace (_pop, offspring); + } while (continuator (_pop)); + } + + +protected: + + /** stopping criteria */ + eoContinue < MOEOT > & continuator; + /** evaluation function */ + eoEvalFunc < MOEOT > & eval; + /** to evaluate the whole population */ + eoPopLoopEval < MOEOT > loopEval; + /** to evaluate the whole population */ + eoPopEvalFunc < MOEOT > & popEval; + /** binary tournament selection */ + moeoDetTournamentSelect < MOEOT > select; + /** elitist replacement */ + moeoElitistReplacement < MOEOT > replace; + /** general breeder */ + eoGeneralBreeder < MOEOT > genBreed; + /** breeder */ + eoBreed < MOEOT > & breed; + /** fitness assignment used in NSGA-II */ + moeoFastNonDominatedSortingFitnessAssignment < MOEOT > fitnessAssignment; + /** Diversity assignment used in NSGA-II */ + moeoCrowdingDistanceDiversityAssignment < MOEOT > diversityAssignment; + +}; + +#endif /*MOEONSGAII_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoNSGA_II.h b/trunk/paradiseo-moeo/src/moeoNSGA_II.h deleted file mode 100644 index 07f19f0a8..000000000 --- a/trunk/paradiseo-moeo/src/moeoNSGA_II.h +++ /dev/null @@ -1,106 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeoNSGA_II.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -// (c) Deneche Abdelhakim, 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr - */ -//----------------------------------------------------------------------------- -#ifndef MOEONSGA_II_H_ -#define MOEONSGA_II_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -/** -*/ -template < class EOT > class moeoNSGA_II:public eoAlgo < EOT > -{ -public: - - /** - This constructor builds the algorithm as descibed in the paper - - Deb, K., S. Agrawal, A. Pratap, and T. Meyarivan, - A fast elitist non-dominated sorting genetic algorithm for multi-objective optimization: NSGA-II. - In IEEE Transactions on Evolutionary Computation, Vol. 6, No 2, pp 182-197, April 2002. - @param _max_gen number of generations before stopping - @param _eval evaluation function - @param _op variation operator - */ - - moeoNSGA_II (unsigned _max_gen, eoEvalFunc < EOT > &_eval, eoGenOp < EOT > &_op):continuator (*(new eoGenContinue < EOT > (_max_gen))), eval (_eval), loopEval (_eval), popEval (loopEval), selectOne (sorting, 2), // binary tournament selection - replace (sorting), genBreed (selectOne, _op), breed (genBreed) - { - } - - /// Ctor taking _max_gen, crossover and mutation - moeoNSGA_II (unsigned _max_gen, eoEvalFunc < EOT > &_eval, eoQuadOp < EOT > &crossover, double pCross, eoMonOp < EOT > &mutation, double pMut):continuator (*(new eoGenContinue < EOT > (_max_gen))), eval (_eval), loopEval (_eval), popEval (loopEval), selectOne (sorting, 2), // binary tournament selection - - replace (sorting), - genBreed (selectOne, - *new eoSGAGenOp < EOT > (crossover, pCross, mutation, pMut)), - breed (genBreed) - { - } - - /// Ctor taking a continuator instead of _gen_max -moeoNSGA_II (eoContinue < EOT > &_continuator, eoEvalFunc < EOT > &_eval, eoGenOp < EOT > &_op): - continuator (_continuator), eval (_eval), loopEval (_eval), popEval (loopEval), selectOne (sorting, 2), // binary tournament selection - replace (sorting), genBreed (selectOne, _op), breed (genBreed) - { - } - - ///Apply a few generation of evolution to the population. - virtual void operator () (eoPop < EOT > &_pop) - { - eoPop < EOT > offspring, empty_pop; - popEval (empty_pop, _pop); // a first eval of _pop - do - { - // generate offspring, worths are recalculated if necessary - breed (_pop, offspring); - - // eval of offspring - popEval (_pop, offspring); - - // after replace, the new pop is in _pop. Worths are recalculated if necessary - replace (_pop, offspring); - - } - while (continuator (_pop)); - } - -protected: - eoContinue < EOT > &continuator; - - eoEvalFunc < EOT > &eval; - eoPopLoopEval < EOT > loopEval; - - eoPopEvalFunc < EOT > &popEval; - - /// NSGAII sorting - moeoNDSorting_II < EOT > sorting; - /// Binary tournament selection - eoDetTournamentWorthSelect < EOT > selectOne; - /// Elitist replacement - moeoElitistReplacement < EOT > replace; - eoGeneralBreeder < EOT > genBreed; - eoBreed < EOT > &breed; -}; - -#endif diff --git a/trunk/paradiseo-moeo/src/moeoObjectiveVector.h b/trunk/paradiseo-moeo/src/moeoObjectiveVector.h new file mode 100644 index 000000000..b4b2b78bd --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoObjectiveVector.h @@ -0,0 +1,231 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoObjectiveVector.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOOBJECTIVEVECTOR_H_ +#define MOEOOBJECTIVEVECTOR_H_ + +#include +#include +#include +#include + +/** + * Abstract class allowing to represent a solution in the objective space (phenotypic representation). + * The template argument ObjectiveVectorTraits defaults to moeoObjectiveVectorTraits, + * 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 +{ +public: + + /** The traits of objective vectors */ + 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); + } + + + /** + * Returns the number of objectives + */ + 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); + } + + + /** + * Returns true if the _ith objective have to be maximized + * @param _i the index + */ + static bool maximizing(unsigned _i) { + return ObjectiveVectorTraits::maximizing(_i); + } + +}; + + +/** + * 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 > +{ +public: + + using std::vector< double >::size; + using std::vector< double >::operator[]; + + /** + * Ctor + */ + moeoObjectiveVectorDouble() : std::vector < double > (ObjectiveVectorTraits::nObjectives(), 0.0) {} + + + /** + * Ctor from a vector of doubles + * @param _v the std::vector < double > + */ + 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 > 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; + } + + + /** + * 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); + } + + + /** + * 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 + { + 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; + } + + + /** + * 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); + } + + + /** + * 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); + } + +}; + + +/** + * Output for a moeoObjectiveVectorDouble object + * @param _os output stream + * @param _objectiveVector the objective vector to write + */ +template < class ObjectiveVectorTraits > +std::ostream & operator<<(std::ostream & _os, const moeoObjectiveVectorDouble < ObjectiveVectorTraits > & _objectiveVector) +{ + for (unsigned i=0; i<_objectiveVector.size(); i++) + { + _os << _objectiveVector[i] << '\t'; + } + return _os; +} + +/** + * Input for a moeoObjectiveVectorDouble object + * @param _is input stream + * @param _objectiveVector the objective vector to read + */ +template < class ObjectiveVectorTraits > +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; +} + +#endif /*MOEOOBJECTIVEVECTOR_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoObjectiveVectorComparator.h b/trunk/paradiseo-moeo/src/moeoObjectiveVectorComparator.h new file mode 100644 index 000000000..4f2cfee8f --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoObjectiveVectorComparator.h @@ -0,0 +1,165 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoObjectiveVectorComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOOBJECTIVEVECTORCOMPARATOR_H_ +#define MOEOOBJECTIVEVECTORCOMPARATOR_H_ + +#include +#include + +/** + * 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 &, bool > + {}; + + +/** + * This functor class allows to compare 2 objective vectors according to Pareto dominance. + */ +template < class ObjectiveVector > +class moeoParetoObjectiveVectorComparator : public moeoObjectiveVectorComparator < ObjectiveVector > +{ +public: + + /** + * Returns true if _objectiveVector1 dominates _objectiveVector2 + * @param _objectiveVector1 the first objective vector + * @param _objectiveVector2 the second objective vector + */ + 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; + } + +}; + + +/** + * This functor class allows to compare 2 objective vectors according to g-dominance. + * The concept of g-dominance as been introduced in: + * J. Molina, L. V. Santana, A. G. Hernandez-Diaz, C. A. Coello Coello, R. Caballero, + * "g-dominance: Reference point based dominance" (2007) + */ +template < class ObjectiveVector > +class moeoGDominanceObjectiveVectorComparator : public moeoObjectiveVectorComparator < ObjectiveVector > +{ +public: + + /** + * Ctor. + * @param _ref the reference point + */ + moeoGDominanceObjectiveVectorComparator(ObjectiveVector _ref) : ref(_ref) + {} + + + /** + * Returns true if _objectiveVector1 g-dominates _objectiveVector2. + * @param _objectiveVector1 the first objective vector + * @param _objectiveVector2 the second objective vector + */ + bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2) + { + unsigned flag1 = flag(_objectiveVector1); + unsigned flag2 = flag(_objectiveVector2); + if (flag1==0) + { + // cannot dominate + return false; + } + else if ( (flag1==1) && (flag2==0) ) + { + // dominates + return true; + } + else // (flag1==1) && (flag2==1) + { + // both are on the good region, so let's use the classical Pareto dominance + return paretoComparator(_objectiveVector1, _objectiveVector2); + } + } + + +private: + + /** the reference point */ + ObjectiveVector ref; + /** Pareto comparator */ + moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; + + + /** + * Returns the flag of _objectiveVector according to the reference point + * @param _objectiveVector the first objective vector + */ + unsigned flag(const ObjectiveVector & _objectiveVector) + { + unsigned result=1; + for (unsigned i=0; i ref[i]) + { + result=0; + } + } + if (result==0) + { + result=1; + for (unsigned i=0; i +#include +#include + +/** + * A traits class for moeoObjectiveVector to specify the number of objectives and which ones have to be minimized or maximized. + */ +class moeoObjectiveVectorTraits +{ +public: + + /** + * 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"); + } + + /** + * 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; + } + + /** + * Returns true if the _ith objective have to be minimized + * @param _i the index + */ + 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)); + } + + /** + * Returns the tolerance value (to compare solutions) + */ + static double tolerance() + { + return 1e-6; + } + + +private: + + /** The number of objectives */ + static unsigned nObj; + /** The min/max vector */ + static std::vector < bool > bObj; + +}; + +#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; +std::vector < bool > moeoObjectiveVectorTraits::bObj; diff --git a/trunk/paradiseo-moeo/src/moeoRandomSelect.h b/trunk/paradiseo-moeo/src/moeoRandomSelect.h new file mode 100644 index 000000000..04d7cfcc0 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoRandomSelect.h @@ -0,0 +1,42 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoRandomSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEORANDOMSELECT_H_ +#define MOEORANDOMSELECT_H_ + +#include +#include + +/** + * Selection strategy that selects only one element randomly from a whole population. + */ +template < class MOEOT > class moeoRandomSelect:public moeoSelectOne < MOEOT >, public eoRandomSelect +{ +public: + + /** + * Ctor. + */ + moeoRandomSelect(){} + + + /** + * Return one individual at random by using an eoRandomSelect. + */ + const MOEOT & operator () (const eoPop < MOEOT > &_pop) + { + return eoRandomSelect < MOEOT >::operator ()(_pop); + } + +}; + +#endif /*MOEORANDOMSELECT_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoReplacement.h b/trunk/paradiseo-moeo/src/moeoReplacement.h index a1148e119..708529db0 100644 --- a/trunk/paradiseo-moeo/src/moeoReplacement.h +++ b/trunk/paradiseo-moeo/src/moeoReplacement.h @@ -2,7 +2,7 @@ //----------------------------------------------------------------------------- // moeoReplacement.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 /* This library... @@ -13,157 +13,13 @@ #ifndef MOEOREPLACEMENT_H_ #define MOEOREPLACEMENT_H_ -#include -#include #include - /** - * Replacement strategy for multi-objective optimization + * Replacement strategy for multi-objective optimization. */ -template < class EOT, class WorthT > class moeoReplacement:public eoReplacement < - EOT > -{ -}; +template < class MOEOT > +class moeoReplacement : public eoReplacement < MOEOT > + {}; - -/** - * Keep all the best individuals - * (almost cut-and-pasted from eoNDPlusReplacement, (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2002) - */ -template < class EOT, class WorthT = - double >class moeoElitistReplacement:public moeoReplacement < EOT, WorthT > -{ -public: - - /** - * constructor - * @param _perf2worth the functor class to transform raw fitnesses into fitness for selection - */ - moeoElitistReplacement (eoPerf2Worth < EOT, - WorthT > &_perf2worth):perf2worth (_perf2worth) - { - } - - - /** - * replacement - result in _parents - * @param _parents parents population - * @param _offspring offspring population - */ - void operator () (eoPop < EOT > &_parents, eoPop < EOT > &_offspring) - { - unsigned size = _parents.size (); - _parents.reserve (_parents.size () + _offspring.size ()); - copy (_offspring.begin (), _offspring.end (), back_inserter (_parents)); - - // calculate worths - perf2worth (_parents); - perf2worth.sort_pop (_parents); - perf2worth.resize (_parents, size); - - _offspring.clear (); - } - -private: - /** the functor object to transform raw fitnesses into fitness for selection */ - eoPerf2Worth < EOT, WorthT > &perf2worth; -}; - - -/** - * Same than moeoElitistReplacement except that distinct individuals are privilegied - */ -template < class EOT, class WorthT = - double >class moeoDisctinctElitistReplacement:public moeoReplacement < EOT, - WorthT > -{ -public: - - /** - * constructor - * @param _perf2worth the functor class to transform raw fitnesses into fitness for selection - */ - moeoDisctinctElitistReplacement (eoPerf2Worth < EOT, - WorthT > - &_perf2worth):perf2worth (_perf2worth) - { - } - - - /** - * replacement - result in _parents - * @param _parents parents population - * @param _offspring offspring population - */ - void operator () (eoPop < EOT > &_parents, eoPop < EOT > &_offspring) - { - unsigned size = _parents.size (); - _parents.reserve (_parents.size () + _offspring.size ()); - copy (_offspring.begin (), _offspring.end (), back_inserter (_parents)); - - // creation of the new population (of size 'size') - createNewPop (_parents, size); - - _offspring.clear (); - } - - -private: - - /** the functor object to transform raw fitnesses into fitness for selection */ - eoPerf2Worth < EOT, WorthT > &perf2worth; - - - /** - * creation of the new population of size _size - * @param _pop the initial population (will be modified) - * @param _size the size of the population to create - */ - void createNewPop (eoPop < EOT > &_pop, unsigned _size) - { - // the number of occurences for each individual - std::map < EOT, unsigned >nb_occurences; - for (unsigned i = 0; i < _pop.size (); i++) - nb_occurences[_pop[i]] = 0; - // the new population - eoPop < EOT > new_pop; - new_pop.reserve (_pop.size ()); - for (unsigned i = 0; i < _pop.size (); i++) - { - if (nb_occurences[_pop[i]] == 0) - new_pop.push_back (_pop[i]); - nb_occurences[_pop[i]]++; - } - - // calculate worths (on the new population) - perf2worth (new_pop); - perf2worth.sort_pop (new_pop); - - // if case there's not enough individuals in the population... - unsigned new_pop_size_init = new_pop.size (); - unsigned k = 0; - while (new_pop.size () < _size) - { - if (k < new_pop_size_init) - { - if (nb_occurences[new_pop[k]] > 1) - { - new_pop.push_back (new_pop[k]); - nb_occurences[new_pop[k]]--; - } - k++; - } - else - k = 0; - } - - // resize and swap the populations - perf2worth.resize (new_pop, _size); - _pop.resize (_size); - _pop.swap (new_pop); - } - -}; - -#endif /*MOEOREPLACEMENT_H_ */ +#endif /*MOEOREPLACEMENT_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoRouletteSelect.h b/trunk/paradiseo-moeo/src/moeoRouletteSelect.h new file mode 100644 index 000000000..040c52609 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoRouletteSelect.h @@ -0,0 +1,82 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoRouletteSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOROULETTESELECT_H_ +#define MOEOROULETTESELECT_H_ + +#include +#include + +/** + * Selection strategy that selects ONE individual by using roulette wheel process. + */ +template < class MOEOT > +class moeoRouletteSelect:public moeoSelectOne +{ +public: + + + /** + * Full Ctor. + * @param _comparator the comparator (used to compare 2 individuals) + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoRouletteSelect (moeoComparator < MOEOT > &_comparator, unsigned _tSize = 2): + comparator (_comparator), tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + + /** + * Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default. + * @param _tSize the number of individuals in the tournament (default: 2) + */ + moeoRouletteSelect (unsigned _tSize = 2): + comparator (*(new moeoFitnessThenDiversityComparator < MOEOT > ())), + tSize (_tSize) + { + // consistency check + if (tSize < 2) + { + std:: + cout << "Warning, Tournament size should be >= 2\nAdjusted to 2\n"; + tSize = 2; + } + } + + /** + * Apply the tournament to the given population + */ + const MOEOT & operator () (const eoPop < MOEOT > &_pop) + { + // use the selector + return mo_roulette_wheel(_pop,tSize); //comparator ?? + } + + +protected: + + /** comparator */ + moeoComparator < MOEOT > &comparator; + /** size */ + double & tSize; + +}; + +#endif /*MOEOROULETTESELECT_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoSelectFromPopAndArch.h b/trunk/paradiseo-moeo/src/moeoSelectFromPopAndArch.h new file mode 100644 index 000000000..ad2d0482d --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoSelectFromPopAndArch.h @@ -0,0 +1,89 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoSelectFormPopAndArch.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSELECTONEFROMPOPANDARCH_H_ +#define MOEOSELECTONEFROMPOPANDARCH_H_ + +#include +#include +#include +#include +#include + +/** + * Elitist selection process that consists in choosing individuals in the archive as well as in the current population. + */ +template < class MOEOT > +class moeoSelectFromPopAndArch : public moeoSelectOne < MOEOT > +{ +public: + + /** + * Ctor + * @param _popSelectOne the population's selection operator + * @param _archSelectOne the archive's selection operator + * @param _arch the archive + * @param _ratioFromPop the ratio of selected individuals from the population + */ + moeoSelectFromPopAndArch (moeoSelectOne < MOEOT > & _popSelectOne, moeoSelectOne < MOEOT > _archSelectOne, moeoArchive < MOEOT > & _arch, double _ratioFromPop=0.5) + : popSelectOne(_popSelectOne), archSelectOne(_archSelectOne), arch(_arch), ratioFromPop(_ratioFromPop) + {} + + /** + * Defaulr ctor - the archive's selection operator is a random selector + * @param _popSelectOne the population's selection operator + * @param _arch the archive + * @param _ratioFromPop the ratio of selected individuals from the population + */ + moeoSelectFromPopAndArch (moeoSelectOne < MOEOT > & _popSelectOne, moeoArchive < MOEOT > & _arch, double _ratioFromPop=0.5) + : popSelectOne(_popSelectOne), archSelectOne(randomSelectOne), arch(_arch), ratioFromPop(_ratioFromPop) + {} + + /** + * The selection process + */ + virtual const MOEOT & operator () (const eoPop < MOEOT > & pop) + { + if (arch.size() > 0) + if (rng.flip(ratioFromPop)) + return popSelectOne(pop); + else + return archSelectOne(arch); + else + return popSelectOne(pop); + } + + /** + * Setups some population stats + */ + virtual void setup (const eoPop < MOEOT > & _pop) + { + popSelectOne.setup(_pop); + } + + +private: + + /** The population's selection operator */ + moeoSelectOne < MOEOT > & popSelectOne; + /** The archive's selection operator */ + moeoSelectOne < MOEOT > & archSelectOne; + /** The archive */ + moeoArchive < MOEOT > & arch; + /** The ratio of selected individuals from the population*/ + double ratioFromPop; + /** A random selection operator (used as default for archSelectOne) */ + moeoRandomSelect < MOEOT > randomSelectOne; + +}; + +#endif /*MOEOSELECTONEFROMPOPANDARCH_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoSelectOne.h b/trunk/paradiseo-moeo/src/moeoSelectOne.h new file mode 100644 index 000000000..7f4129b99 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoSelectOne.h @@ -0,0 +1,24 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoSelectOne.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSELECTONE_H_ +#define MOEOSELECTONE_H_ + +#include + +/** + * Selection strategy for multi-objective optimization that selects only one element from a whole population. + */ +template < class MOEOT > +class moeoSelectOne : public eoSelectOne < MOEOT > {}; + +#endif /*MOEOSELECTONE_H_*/ diff --git a/trunk/paradiseo-moeo/src/moeoSelectOneFromPopAndArch.h b/trunk/paradiseo-moeo/src/moeoSelectOneFromPopAndArch.h deleted file mode 100644 index aea605b51..000000000 --- a/trunk/paradiseo-moeo/src/moeoSelectOneFromPopAndArch.h +++ /dev/null @@ -1,93 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// moeoSelectOneFormPopAndArch.h -// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006 -/* - This library... - - Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr - */ -//----------------------------------------------------------------------------- - -#ifndef MOEOSELECTONEFROMPOPANDARCH_H_ -#define MOEOSELECTONEFROMPOPANDARCH_H_ - -#include -#include -#include -#include -#include - -/** - * Elitist selection process that consists in choosing individuals in the archive as well as in the current population. - */ -template < class EOT > class moeoSelectOneFromPopAndArch:public eoSelectOne < - EOT > -{ -public: - - /** - * Ctor - * @param _popSelectOne the population's selection operator - * @param _archSelectOne the archive's selection operator - * @param _arch the archive - * @param _ratioFromPop the ratio of selected individuals from the population - */ -moeoSelectOneFromPopAndArch (eoSelectOne < EOT > &_popSelectOne, eoSelectOne < EOT > _archSelectOne, moeoArchive < EOT > &_arch, double _ratioFromPop = 0.5):popSelectOne (_popSelectOne), archSelectOne (_archSelectOne), arch (_arch), - ratioFromPop - (_ratioFromPop) - { - } - - /** - * Ctor - the archive's selection operator is a random selector - * @param _popSelectOne the population's selection operator - * @param _arch the archive - * @param _ratioFromPop the ratio of selected individuals from the population - */ -moeoSelectOneFromPopAndArch (eoSelectOne < EOT > &_popSelectOne, moeoArchive < EOT > &_arch, double _ratioFromPop = 0.5):popSelectOne (_popSelectOne), archSelectOne (randomSelect), arch (_arch), - ratioFromPop - (_ratioFromPop) - { - } - - /** - * The selection process - */ - virtual const EOT & operator () (const eoPop < EOT > &pop) - { - if (arch.size () > 0) - if (rng.flip (ratioFromPop)) - return popSelectOne (pop); - else - return archSelectOne (arch); - else - return popSelectOne (pop); - } - - /** - * Setups some population stats - */ - virtual void setup (const eoPop < EOT > &_pop) - { - popSelectOne.setup (_pop); - } - - -private: - - /** The population's selection operator */ - eoSelectOne < EOT > &popSelectOne; - /** The archive's selection operator */ - eoSelectOne < EOT > &archSelectOne; - /** the archive */ - moeoArchive < EOT > &arch; - /** the ratio of selected individuals from the population*/ - double ratioFromPop; - /** the random selection operator */ - eoRandomSelect < EOT > randomSelect; - -}; - -#endif /*MOEOSELECTONEFROMPOPANDARCH_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoSelectors.h b/trunk/paradiseo-moeo/src/moeoSelectors.h new file mode 100644 index 000000000..c151001c6 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoSelectors.h @@ -0,0 +1,157 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoSelectors.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSELECTORS_H_ +#define MOEOSELECTORS_H_ + +#include + + +template +It mo_deterministic_tournament(It _begin, It _end, unsigned _t_size,moeoComparator& _comparator ,eoRng& _gen = rng) +{ + It best = _begin + _gen.random(_end - _begin); + + for (unsigned i = 0; i < _t_size - 1; ++i) + { + It competitor = _begin + _gen.random(_end - _begin); + + // compare the two individuals by using the comparator + if (_comparator(*best,*competitor)) + + // best "better" than competitor + best=competitor; + } + + return best; +} + + +template +const MOEOT& mo_deterministic_tournament(const eoPop& _pop, unsigned _t_size,moeoComparator& _comparator, eoRng& _gen = rng) +{ + return *mo_deterministic_tournament(_pop.begin(), _pop.end(),_t_size,_comparator, _gen); +} + +template +MOEOT& mo_deterministic_tournament(eoPop& _pop, unsigned _t_size,moeoComparator& _comparator,eoRng& _gen = rng) +{ + return *mo_deterministic_tournament(_pop.begin(), _pop.end(), _t_size,_comparator, _gen); +} + + + +template +It mo_stochastic_tournament(It _begin, It _end, double _t_rate,moeoComparator& _comparator ,eoRng& _gen = rng) +{ + It i1 = _begin + _gen.random(_end - _begin); + It i2 = _begin + _gen.random(_end - _begin); + + bool return_better = _gen.flip(_t_rate); + + if (_comparator(*i1 , *i2)) + { + if (return_better) return i2; + // else + + return i1; + } + else + { + if (return_better) return i1; + // else + } + // else + + return i2; +} + +template +const MOEOT& mo_stochastic_tournament(const eoPop& _pop, double _t_rate,moeoComparator& _comparator, eoRng& _gen = rng) +{ + return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate,_comparator, _gen); +} + +template +MOEOT& mo_stochastic_tournament(eoPop& _pop, double _t_rate, eoRng& _gen = rng) +{ + return *mo_stochastic_tournament(_pop.begin(), _pop.end(), _t_rate, _gen); +} + + + +template +It mo_roulette_wheel(It _begin, It _end, double total, eoRng& _gen = rng) +{ + + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _begin + _gen.random(_end - _begin); // uniform choice + + It i = _begin; + + while (roulette > 0.0) + { + roulette -= static_cast(*(i++)); + } + + return --i; +} + +template +const MOEOT& mo_roulette_wheel(const eoPop& _pop, double total, eoRng& _gen = rng) +{ + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _pop[_gen.random(_pop.size())]; // uniform choice + + typename eoPop::const_iterator i = _pop.begin(); + + while (roulette > 0.0) + { + roulette -= static_cast((i++)->fitness()); + } + + return *--i; +} + +template +MOEOT& mo_roulette_wheel(eoPop& _pop, double total, eoRng& _gen = rng) +{ + float roulette = _gen.uniform(total); + + if (roulette == 0.0) // covers the case where total==0.0 + return _pop[_gen.random(_pop.size())]; // uniform choice + + typename eoPop::iterator i = _pop.begin(); + + while (roulette > 0.0) + { + // fitness ? + roulette -= static_cast((i++)->fitness()); + } + + return *--i; +} + +#endif /*MOEOSELECTORS_H_*/ + + + + + + + + + diff --git a/trunk/paradiseo-moeo/src/moeoStochTournamentSelect.h b/trunk/paradiseo-moeo/src/moeoStochTournamentSelect.h new file mode 100644 index 000000000..1891be0b6 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoStochTournamentSelect.h @@ -0,0 +1,96 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoStochTournamentSelect.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOSTOCHTOURNAMENTSELECT_H_ +#define MOEOSTOCHTOURNAMENTSELECT_H_ + +#include +#include + +/** + * Selection strategy that selects ONE individual by stochastic tournament. + */ +template < class MOEOT > class moeoStochTournamentSelect:public moeoSelectOne +{ +public: + + /** + * Full Ctor + * @param _comparator the comparator (used to compare 2 individuals) + * @param _tRate the tournament rate + */ + moeoStochTournamentSelect (moeoComparator < MOEOT > & _comparator, double _tRate = 1.0) : + comparator (_comparator), tRate (_tRate) + { + // consistency checks + if (tRate < 0.5) + { + std:: + cerr << + "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; + tRate = 0.55; + } + if (tRate > 1) + { + std:: + cerr << "Warning, Tournament rate should be < 1\nAdjusted to 1\n"; + tRate = 1; + } + } + + /** + * Ctor without comparator. A moeoFitnessThenDiversityComparator is used as default. + * @param _tRate the tournament rate + */ + moeoStochTournamentSelect (double _tRate = 1.0) + :comparator (*(new moeoFitnessThenDiversityComparator < MOEOT > ())), tRate (_tRate) + + { + // consistency checks + if (tRate < 0.5) + { + std:: + cerr << + "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; + tRate = 0.55; + } + if (tRate > 1) + { + std:: + cerr << "Warning, Tournament rate should be < 1\nAdjusted to 1\n"; + tRate = 1; + } + } + + + /** + * Apply the tournament to the given population + * @param _pop the population + */ + const MOEOT & operator() (const eoPop < MOEOT > &_pop) + { + // use the selector + return mo_stochastic_tournament(_pop,tRate,comparator); + } + + + +protected: + + /** the diversity assignment strategy */ + moeoComparator < MOEOT > & comparator; + /** the tournament rate */ + double tRate; + +}; + +#endif /*MOEOSTOCHTOURNAMENTSELECT_H_ */ diff --git a/trunk/paradiseo-moeo/src/moeoVector.h b/trunk/paradiseo-moeo/src/moeoVector.h new file mode 100644 index 000000000..404a54ad3 --- /dev/null +++ b/trunk/paradiseo-moeo/src/moeoVector.h @@ -0,0 +1,216 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoVector.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOVECTOR_H_ +#define MOEOVECTOR_H_ + +#include +#include +#include + +/** + * Base class for fixed length chromosomes, just derives from MOEO and std::vector and redirects the smaller than operator to MOEO (objective vector based comparison). + * GeneType must have the following methods: void ctor (needed for the std::vector<>), copy ctor. + */ +template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity, class GeneType > +class moeoVector : public MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >, public std::vector < GeneType > +{ +public: + + using MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity > :: invalidate; + using std::vector < GeneType > :: operator[]; + using std::vector < GeneType > :: begin; + using std::vector < GeneType > :: end; + using std::vector < GeneType > :: resize; + using std::vector < GeneType > :: size; + + /** the atomic type */ + typedef GeneType AtomType; + /** the container type */ + typedef std::vector < GeneType > ContainerType; + + + /** + * Default ctor. + * @param _size Length of vector (default is 0) + * @param _value Initial value of all elements (default is default value of type GeneType) + */ + moeoVector(unsigned _size = 0, GeneType _value = GeneType()) : + MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >(), std::vector(_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 (size()) // NOT an initial empty std::vector + { + std::cout << "Warning: Changing size in moeoVector assignation"<::operator< + * @param _moeo the object to compare with + */ + bool operator<(const moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, GeneType> & _moeo) const + { + return MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::operator<(_moeo); + } + + + /** + * 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(_os, " ")); + } + + + /** + * Reading object + * @param _is input stream + */ + virtual void readFrom(std::istream & _is) + { + MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is); + unsigned sz; + _is >> sz; + resize(sz); + unsigned i; + for (i = 0; i < sz; ++i) + { + AtomType atom; + _is >> atom; + operator[](i) = atom; + } + } + +}; + + +/** + * To avoid conflicts between MOEO::operator< and std::vector::operator< + * @param _moeo1 the first object to compare + * @param _moeo2 the second object to compare + */ +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); +} + + +/** + * To avoid conflicts between MOEO::operator> and std::vector::operator> + * @param _moeo1 the first object to compare + * @param _moeo2 the second object to compare + */ +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); +} + + +/** + * This class is an implementationeo of a simple double-valued moeoVector. + */ +template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity > +class moeoRealVector : public moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, double > +{ +public: + + /** + * Ctor + * @param _size Length of vector (default is 0) + * @param _value Initial value of all elements (default is default value of type GeneType) + */ + moeoRealVector(unsigned _size = 0, double _value = 0.0) : moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, double >(_size, _value) + {} + +}; + + +/** + * This class is an implementationeo of a simple bit-valued moeoVector. + */ +template < class MOEOObjectiveVector, class MOEOFitness, class MOEODiversity > +class moeoBitVector : public moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > +{ +public: + + using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: begin; + using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: end; + using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: resize; + using moeoVector < MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool > :: size; + + + /** + * Ctor + * @param _size Length of vector (default is 0) + * @param _value Initial value of all elements (default is default value of type GeneType) + */ + moeoBitVector(unsigned _size = 0, bool _value = false) : moeoVector< MOEOObjectiveVector, MOEOFitness, MOEODiversity, bool >(_size, _value) + {} + + + /** + * 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(_os)); + } + + + /** + * Reading object + * @param _is input stream + */ + virtual void readFrom(std::istream & _is) + { + MOEO < MOEOObjectiveVector, MOEOFitness, MOEODiversity >::readFrom(_is); + unsigned 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(), '1')); + } + } + +}; + + +#endif /*MOEOVECTOR_H_*/