From 6b7916c6effd5357eb7bd5013d5a2c3d2bdd67e2 Mon Sep 17 00:00:00 2001 From: liefooga Date: Tue, 26 Jun 2007 12:14:33 +0000 Subject: [PATCH] add replacement git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@380 331e1502-861f-0410-8da2-ba01fb791d7f --- .../src/replacement/moeoElitistReplacement.h | 130 +++++++++++++++ .../moeoEnvironmentalReplacement.h | 146 +++++++++++++++++ .../moeoEnvironmentalReplacementExtremes.h | 155 ++++++++++++++++++ .../replacement/moeoGenerationalReplacement.h | 39 +++++ .../src/replacement/moeoReplacement.h | 24 +++ 5 files changed, 494 insertions(+) create mode 100644 branches/paradiseo-moeo-1.0/src/replacement/moeoElitistReplacement.h create mode 100755 branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacement.h create mode 100644 branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacementExtremes.h create mode 100644 branches/paradiseo-moeo-1.0/src/replacement/moeoGenerationalReplacement.h create mode 100644 branches/paradiseo-moeo-1.0/src/replacement/moeoReplacement.h diff --git a/branches/paradiseo-moeo-1.0/src/replacement/moeoElitistReplacement.h b/branches/paradiseo-moeo-1.0/src/replacement/moeoElitistReplacement.h new file mode 100644 index 000000000..b6bb9807d --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/replacement/moeoElitistReplacement.h @@ -0,0 +1,130 @@ +// -*- 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 +#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 _fitnessAssignment the fitness assignment strategy + * @param _diversityAssignment the diversity assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (_comparator) + {} + + + /** + * Constructor without comparator. A moeoFitThenDivComparator is used as default. + * @param _fitnessAssignment the fitness assignment strategy + * @param _diversityAssignment the diversity assignment strategy + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (defaultComparator) + {} + + + /** + * Constructor without moeoDiversityAssignement. A dummy diversity is used as default. + * @param _fitnessAssignment the fitness assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (_comparator) + {} + + + /** + * Constructor without moeoDiversityAssignement nor moeoComparator. + * A moeoFitThenDivComparator and a dummy diversity are used as default. + * @param _fitnessAssignment the fitness assignment strategy + */ + moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (defaultComparator) + {} + + + /** + * 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 int sz = _parents.size (); + // merges offspring and parents into a global population + _parents.reserve (_parents.size () + _offspring.size ()); + std::copy (_offspring.begin (), _offspring.end (), back_inserter (_parents)); + // evaluates the fitness and the diversity of this global population + fitnessAssignment (_parents); + diversityAssignment (_parents); + // sorts the whole population according to the comparator + std::sort(_parents.begin(), _parents.end(), comparator); + // finally, resize this global population + _parents.resize (sz); + // and clear the offspring population + _offspring.clear (); + } + + +protected: + + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & fitnessAssignment; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & diversityAssignment; + /** a dummy diversity assignment can be used as default */ + moeoDummyDiversityAssignment < MOEOT > defaultDiversity; + /** a fitness then diversity comparator can be used as default */ + moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; + /** this object is used to compare solutions in order to sort the population */ + class Cmp + { + public: + /** + * Ctor. + * @param _comp the comparator + */ + Cmp(moeoComparator < MOEOT > & _comp) : comp(_comp) + {} + /** + * 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 comp(_moeo2,_moeo1); + } + private: + /** the comparator */ + moeoComparator < MOEOT > & comp; + } comparator; + +}; + +#endif /*MOEOELITISTREPLACEMENT_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacement.h b/branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacement.h new file mode 100755 index 000000000..68bc032c1 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacement.h @@ -0,0 +1,146 @@ +// -*- 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 +#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 _fitnessAssignment the fitness assignment strategy + * @param _diversityAssignment the diversity assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (_comparator) + {} + + + /** + * Constructor without comparator. A moeoFitThenDivComparator is used as default. + * @param _fitnessAssignment the fitness assignment strategy + * @param _diversityAssignment the diversity assignment strategy + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (defaultComparator) + {} + + + /** + * Constructor without moeoDiversityAssignement. A dummy diversity is used as default. + * @param _fitnessAssignment the fitness assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (_comparator) + {} + + + /** + * Constructor without moeoDiversityAssignement nor moeoComparator. + * A moeoFitThenDivComparator and a dummy diversity are used as default. + * @param _fitnessAssignment the fitness assignment strategy + */ + moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (defaultComparator) + {} + + + /** + * 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 int sz = _parents.size(); + // merges offspring and parents into a global population + _parents.reserve (_parents.size() + _offspring.size()); + std::copy (_offspring.begin(), _offspring.end(), back_inserter(_parents)); + // evaluates the fitness and the diversity of this global population + fitnessAssignment (_parents); + diversityAssignment (_parents); + // remove individuals 1 by 1 and update the fitness values + unsigned int worstIdx; + ObjectiveVector worstObjVec; + while (_parents.size() > sz) + { + // the individual to delete + worstIdx = std::min_element(_parents.begin(), _parents.end(), comparator) - _parents.begin(); + worstObjVec = _parents[worstIdx].objectiveVector(); + // remove the woorst individual + _parents[worstIdx] = _parents.back(); + _parents.pop_back(); + // update of the fitness and diversity values + fitnessAssignment.updateByDeleting(_parents, worstObjVec); + diversityAssignment.updateByDeleting(_parents, worstObjVec); + + } + // clear the offspring population + _offspring.clear (); + } + + +protected: + + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & fitnessAssignment; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & diversityAssignment; + /** a dummy diversity assignment can be used as default */ + moeoDummyDiversityAssignment < MOEOT > defaultDiversity; + /** a fitness then diversity comparator can be used as default */ + moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; + /** this object is used to compare solutions in order to sort the population */ + class Cmp + { + public: + /** + * Ctor. + * @param _comp the comparator + */ + Cmp(moeoComparator < MOEOT > & _comp) : comp(_comp) + {} + /** + * 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 comp(_moeo1,_moeo2); + } + private: + /** the comparator */ + moeoComparator < MOEOT > & comp; + } comparator; + +}; + +#endif /*MOEOENVIRONMENTALREPLACEMENT_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacementExtremes.h b/branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacementExtremes.h new file mode 100644 index 000000000..26ec57572 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/replacement/moeoEnvironmentalReplacementExtremes.h @@ -0,0 +1,155 @@ +// -*- 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 +#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 > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), 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 > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (defaultComparator) + {} + + + /** + * 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 > & _fitnessAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), 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 > & _fitnessAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (defaultComparator) + {} + + + /** + * 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 int sz = _parents.size(); + // merges offspring and parents into a global population + _parents.reserve (_parents.size() + _offspring.size()); + std::copy (_offspring.begin(), _offspring.end(), back_inserter(_parents)); + // evaluates the fitness and the diversity of this global population + fitnessAssignment (_parents); + diversityAssignment (_parents); + // remove individuals 1 by 1 and update the fitness values + unsigned int worstIdx; + ObjectiveVector worstObjVec; +/////////////////////////////////////////////// +// let's put the extreme solutions at the beginning of _parents ! +moeoOneObjectiveComparator comp0(0); +unsigned int a = std::max_element(_parents.begin(), _parents.end(), comp0) - _parents.begin(); +swap(_parents[0], _parents[a]); +moeoOneObjectiveComparator comp1(1); +unsigned int b = std::max_element(_parents.begin() + 1, _parents.end(), comp1) - _parents.begin(); +swap(_parents[1], _parents[b]); +/////////////////////////////////////////////// + while (_parents.size() > sz) + { + // the individual to delete + worstIdx = std::min_element(_parents.begin()/* virer ! */ + 2 /* virer ! */, _parents.end(), comparator) - _parents.begin(); + worstObjVec = _parents[worstIdx].objectiveVector(); + // remove the woorst individual + _parents[worstIdx] = _parents.back(); + _parents.pop_back(); + // update of the fitness and diversity values + fitnessAssignment.updateByDeleting(_parents, worstObjVec); + diversityAssignment.updateByDeleting(_parents, worstObjVec); + + } + // clear the offspring population + _offspring.clear (); + } + + +protected: + + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & fitnessAssignment; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & diversityAssignment; + /** a dummy diversity assignment can be used as default */ + moeoDummyDiversityAssignment < MOEOT > defaultDiversity; + /** a fitness then diversity comparator can be used as default */ + moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; + /** this object is used to compare solutions in order to sort the population */ + class Cmp + { + public: + /** + * Ctor. + * @param _comparator the comparator + */ + Cmp(moeoComparator < MOEOT > & _comp) : comp(_comp) + {} + /** + * 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 comp(_moeo1,_moeo2); + } + private: + /** the comparator */ + moeoComparator < MOEOT > & comp; + } comparator; + +}; + +#endif /*MOEOENVIRONMENTALREPLACEMENT_H_ */ diff --git a/branches/paradiseo-moeo-1.0/src/replacement/moeoGenerationalReplacement.h b/branches/paradiseo-moeo-1.0/src/replacement/moeoGenerationalReplacement.h new file mode 100644 index 000000000..e3d1f6235 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/replacement/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/branches/paradiseo-moeo-1.0/src/replacement/moeoReplacement.h b/branches/paradiseo-moeo-1.0/src/replacement/moeoReplacement.h new file mode 100644 index 000000000..aa0631754 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/replacement/moeoReplacement.h @@ -0,0 +1,24 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoReplacement.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOREPLACEMENT_H_ +#define MOEOREPLACEMENT_H_ + +#include + +/** + * Replacement strategy for multi-objective optimization. + */ +template < class MOEOT > +class moeoReplacement : public eoReplacement < MOEOT > {}; + +#endif /*MOEOREPLACEMENT_H_*/