- Main Page
- Classes
- Files

- Search for |

# moeoObjectiveVectorComparator.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // moeoObjectiveVectorComparator.h
    00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
    00006 /*
    00007     This library...
    00008
    00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
    00010  */
    00011 //-----------------------------------------------------------------------------
    00012
    00013 #ifndef MOEOOBJECTIVEVECTORCOMPARATOR_H_
    00014 #define MOEOOBJECTIVEVECTORCOMPARATOR_H_
    00015
    00016 #include <math.h>
    00017 #include <eoFunctor.h>
    00018
    00023 template < class ObjectiveVector >
    00024 class moeoObjectiveVectorComparator : public eoBF < const ObjectiveVector &, const ObjectiveVector &, bool >
    00025     {};
    00026
    00027
    00031 template < class ObjectiveVector >
    00032 class moeoParetoObjectiveVectorComparator : public moeoObjectiveVectorComparator < ObjectiveVector >
    00033 {
    00034 public:
    00035
    00041     bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2)
    00042     {
    00043         bool dom = false;
    00044         for (unsigned i=0; i<ObjectiveVector::nObjectives(); i++)
    00045         {
    00046             // first, we have to check if the 2 objective values are not equal for the ith objective
    00047             if ( fabs(_objectiveVector1[i] - _objectiveVector2[i]) > ObjectiveVector::Traits::tolerance() )
    00048             {
    00049                 // if the ith objective have to be minimized...
    00050                 if (ObjectiveVector::minimizing(i))
    00051                 {
    00052                     if (_objectiveVector1[i] < _objectiveVector2[i])
    00053                     {
    00054                         dom = true;             //_objectiveVector1[i] is better than _objectiveVector2[i]
    00055                     }
    00056                     else
    00057                     {
    00058                         return false;   //_objectiveVector1 cannot dominate _objectiveVector2
    00059                     }
    00060                 }
    00061                 // if the ith objective have to be maximized...
    00062                 else if (ObjectiveVector::maximizing(i))
    00063                 {
    00064                     if (_objectiveVector1[i] > _objectiveVector2[i])
    00065                     {
    00066                         dom = true;             //_objectiveVector1[i] is better than _objectiveVector2[i]
    00067                     }
    00068                     else
    00069                     {
    00070                         return false;   //_objectiveVector1 cannot dominate _objectiveVector2
    00071                     }
    00072                 }
    00073             }
    00074         }
    00075         return dom;
    00076     }
    00077
    00078 };
    00079
    00080
    00087 template < class ObjectiveVector >
    00088 class moeoGDominanceObjectiveVectorComparator : public moeoObjectiveVectorComparator < ObjectiveVector >
    00089 {
    00090 public:
    00091
    00096     moeoGDominanceObjectiveVectorComparator(ObjectiveVector _ref) : ref(_ref)
    00097     {}
    00098
    00099
    00105     bool operator()(const ObjectiveVector & _objectiveVector1, const ObjectiveVector & _objectiveVector2)
    00106     {
    00107         unsigned flag1 = flag(_objectiveVector1);
    00108         unsigned flag2 = flag(_objectiveVector2);
    00109         if (flag1==0)
    00110         {
    00111             // cannot dominate
    00112             return false;
    00113         }
    00114         else if ( (flag1==1) && (flag2==0) )
    00115         {
    00116             // dominates
    00117             return true;
    00118         }
    00119         else // (flag1==1) && (flag2==1)
    00120         {
    00121             // both are on the good region, so let's use the classical Pareto dominance
    00122             return paretoComparator(_objectiveVector1, _objectiveVector2);
    00123         }
    00124     }
    00125
    00126
    00127 private:
    00128
    00130     ObjectiveVector ref;
    00132     moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator;
    00133
    00134
    00139     unsigned flag(const ObjectiveVector & _objectiveVector)
    00140     {
    00141         unsigned result=1;
    00142         for (unsigned i=0; i<ref.nObjectives(); i++)
    00143         {
    00144             if (_objectiveVector[i] > ref[i])
    00145             {
    00146                 result=0;
    00147             }
    00148         }
    00149         if (result==0)
    00150         {
    00151             result=1;
    00152             for (unsigned i=0; i<ref.nObjectives(); i++)
    00153             {
    00154                 if (_objectiveVector[i] < ref[i])
    00155                 {
    00156                     result=0;
    00157                 }
    00158             }
    00159         }
    00160         return result;
    00161     }
    00162
    00163 };
    00164
    00165 #endif /*MOEOOBJECTIVEVECTORCOMPARATOR_H_*/