From 7e47894417e8c614ed1e8dafd9c03c2cb922224c Mon Sep 17 00:00:00 2001 From: liefooga Date: Tue, 26 Jun 2007 12:08:19 +0000 Subject: [PATCH] add comparator git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@371 331e1502-861f-0410-8da2-ba01fb791d7f --- .../comparator/moeoAggregativeComparator.h | 55 ++++++++++ .../src/comparator/moeoComparator.h | 24 +++++ .../moeoDiversityThenFitnessComparator.h | 45 ++++++++ .../moeoFitnessThenDiversityComparator.h | 45 ++++++++ .../moeoGDominanceObjectiveVectorComparator.h | 102 ++++++++++++++++++ .../moeoObjectiveObjectiveVectorComparator.h | 52 +++++++++ .../moeoObjectiveVectorComparator.h | 26 +++++ .../comparator/moeoOneObjectiveComparator.h | 57 ++++++++++ .../moeoParetoObjectiveVectorComparator.h | 70 ++++++++++++ 9 files changed, 476 insertions(+) create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoAggregativeComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoDiversityThenFitnessComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoFitnessThenDiversityComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoGDominanceObjectiveVectorComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoObjectiveObjectiveVectorComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoObjectiveVectorComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoOneObjectiveComparator.h create mode 100644 branches/paradiseo-moeo-1.0/src/comparator/moeoParetoObjectiveVectorComparator.h diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoAggregativeComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoAggregativeComparator.h new file mode 100644 index 000000000..a217bd52d --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoAggregativeComparator.h @@ -0,0 +1,55 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoAggregativeComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOAGGREGATIVECOMPARATOR_H_ +#define MOEOAGGREGATIVECOMPARATOR_H_ + +#include + +/** + * Functor allowing to compare two solutions according to their fitness and diversity values, each according to its aggregative value. + */ +template < class MOEOT > +class moeoAggregativeComparator : public moeoComparator < MOEOT > +{ +public: + + /** + * Ctor. + * @param _weightFitness the weight for fitness + * @param _weightDiversity the weight for diversity + */ + moeoAggregativeComparator(double _weightFitness = 1.0, double _weightDiversity = 1.0) : weightFitness(_weightFitness), weightDiversity(_weightDiversity) + {} + + + /** + * Returns true if _moeo1 < _moeo2 according to the aggregation of their fitness and diversity values + * @param _moeo1 the first solution + * @param _moeo2 the second solution + */ + const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return ( weightFitness * _moeo1.fitness() + weightDiversity * _moeo1.diversity() ) < ( weightFitness * _moeo2.fitness() + weightDiversity * _moeo2.diversity() ); + } + + +private: + + /** the weight for fitness */ + double weightFitness; + /** the weight for diversity */ + double weightDiversity; + +}; + +#endif /*MOEOAGGREGATIVECOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoComparator.h new file mode 100644 index 000000000..9243a57bc --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoComparator.h @@ -0,0 +1,24 @@ +// -*- 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 > {}; + +#endif /*MOEOCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoDiversityThenFitnessComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoDiversityThenFitnessComparator.h new file mode 100644 index 000000000..54df59b82 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoDiversityThenFitnessComparator.h @@ -0,0 +1,45 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoDiversityThenFitnessComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEODIVERSITYTHENFITNESSCOMPARATOR_H_ +#define MOEODIVERSITYTHENFITNESSCOMPARATOR_H_ + +#include + +/** + * 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 < _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 /*MOEODIVERSITYTHENFITNESSCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoFitnessThenDiversityComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoFitnessThenDiversityComparator.h new file mode 100644 index 000000000..5baf10097 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoFitnessThenDiversityComparator.h @@ -0,0 +1,45 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoFitnessThenDiversityComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOFITNESSTHENDIVERSITYCOMPARATOR_H_ +#define MOEOFITNESSTHENDIVERSITYCOMPARATOR_H_ + +#include + +/** + * 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 < _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(); + } + } + +}; + +#endif /*MOEOFITNESSTHENDIVERSITYCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoGDominanceObjectiveVectorComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoGDominanceObjectiveVectorComparator.h new file mode 100644 index 000000000..006d1c4f2 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoGDominanceObjectiveVectorComparator.h @@ -0,0 +1,102 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoGDominanceObjectiveVectorComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOGDOMINANCEOBJECTIVEVECTORCOMPARATOR_H_ +#define MOEOGDOMINANCEOBJECTIVEVECTORCOMPARATOR_H_ + +#include + +/** + * 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 is g-dominated by _objectiveVector2. + * @param _objectiveVector1 the first objective vector + * @param _objectiveVector2 the second objective vector + */ + const bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2) + { + unsigned int flag1 = flag(_objectiveVector1); + unsigned int flag2 = flag(_objectiveVector2); + if (flag2==0) + { + // cannot dominate + return false; + } + else if ( (flag2==1) && (flag1==0) ) + { + // is dominated + 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 int flag(const ObjectiveVector & _objectiveVector) + { + unsigned int result=1; + for (unsigned int i=0; i ref[i]) + { + result=0; + } + } + if (result==0) + { + result=1; + for (unsigned int i=0; i + +/** + * Functor allowing to compare two objective vectors according to their first objective value, then their second, and so on. + */ +template < class ObjectiveVector > +class moeoObjectiveObjectiveVectorComparator : public moeoObjectiveVectorComparator < ObjectiveVector > +{ +public: + + /** + * Returns true if _objectiveVector1 < _objectiveVector2 on the first objective, then on the second, and so on + * @param _objectiveVector1 the first objective vector + * @param _objectiveVector2 the second objective vector + */ + const bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2) + { + for (unsigned int i=0; i ObjectiveVector::Traits::tolerance() ) + { + if (_objectiveVector1[i] < _objectiveVector2[i]) + { + return true; + } + else + { + return false; + } + } + } + return false; + } + +}; + +#endif /*MOEOOBJECTIVEOBJECTIVEVECTORCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoObjectiveVectorComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoObjectiveVectorComparator.h new file mode 100644 index 000000000..75608ad12 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoObjectiveVectorComparator.h @@ -0,0 +1,26 @@ +// -*- 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 &, const bool > {}; + +#endif /*MOEOOBJECTIVEVECTORCOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoOneObjectiveComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoOneObjectiveComparator.h new file mode 100644 index 000000000..74be47bb3 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoOneObjectiveComparator.h @@ -0,0 +1,57 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoOneObjectiveComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOONEOBJECTIVECOMPARATOR_H_ +#define MOEOONEOBJECTIVECOMPARATOR_H_ + +#include + +/** + * 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 int _obj) : obj(_obj) + { + if (obj > MOEOT::ObjectiveVector::nObjectives()) + { + throw std::runtime_error("Problem with the index of objective in moeoOneObjectiveComparator"); + } + } + + + /** + * Returns true if _moeo1 < _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 int obj; + +}; + +#endif /*MOEOONEOBJECTIVECOMPARATOR_H_*/ diff --git a/branches/paradiseo-moeo-1.0/src/comparator/moeoParetoObjectiveVectorComparator.h b/branches/paradiseo-moeo-1.0/src/comparator/moeoParetoObjectiveVectorComparator.h new file mode 100644 index 000000000..c9f8e8260 --- /dev/null +++ b/branches/paradiseo-moeo-1.0/src/comparator/moeoParetoObjectiveVectorComparator.h @@ -0,0 +1,70 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// moeoParetoObjectiveVectorComparator.h +// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef MOEOPARETOOBJECTIVEVECTORCOMPARATOR_H_ +#define MOEOPARETOOBJECTIVEVECTORCOMPARATOR_H_ + +#include + +/** + * 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 is dominated by _objectiveVector2 + * @param _objectiveVector1 the first objective vector + * @param _objectiveVector2 the second objective vector + */ + const bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2) + { + bool dom = false; + for (unsigned int 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 not better than _objectiveVector2[i] + } + else + { + return false; //_objectiveVector2 cannot dominate _objectiveVector1 + } + } + // if the ith objective have to be maximized... + else if (ObjectiveVector::maximizing(i)) + { + if (_objectiveVector1[i] > _objectiveVector2[i]) + { + dom = true; //_objectiveVector1[i] is not better than _objectiveVector2[i] + } + else + { + return false; //_objectiveVector2 cannot dominate _objectiveVector1 + } + } + } + } + return dom; + } + +}; + +#endif /*MOEOPARETOOBJECTIVEVECTORCOMPARATOR_H_*/