moeoCrowdingDistanceDiversityAssignment.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // moeoCrowdingDistanceDiversityAssignment.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 MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_
00014 #define MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_
00015 
00016 #include <eoPop.h>
00017 #include <moeoComparator.h>
00018 #include <moeoDiversityAssignment.h>
00019 
00025 template < class MOEOT >
00026 class moeoCrowdingDistanceDiversityAssignment : public moeoDiversityAssignment < MOEOT >
00027 {
00028 public:
00029 
00031     typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00032 
00033 
00037     double inf() const
00038     {
00039         return std::numeric_limits<double>::max();
00040     }
00041 
00042 
00047     void operator()(eoPop < MOEOT > & _pop)
00048     {
00049         if (_pop.size() <= 2)
00050         {
00051             for (unsigned i=0; i<_pop.size(); i++)
00052             {
00053                 _pop[i].diversity(inf());
00054             }
00055         }
00056         else
00057         {
00058             setDistances(_pop);
00059         }
00060     }
00061 
00062 
00070     void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
00071     {
00072         cout << "WARNING : updateByDeleting not implemented in moeoCrowdingDiversityAssignment" << endl;
00073     }
00074 
00075 
00076 private:
00077 
00082     void setDistances (eoPop < MOEOT > & _pop)
00083     {
00084         double min, max, distance;
00085         unsigned nObjectives = MOEOT::ObjectiveVector::nObjectives();
00086         // set diversity to 0
00087         for (unsigned i=0; i<_pop.size(); i++)
00088         {
00089             _pop[i].diversity(0);
00090         }
00091         // for each objective
00092         for (unsigned obj=0; obj<nObjectives; obj++)
00093         {
00094             // comparator
00095             moeoOneObjectiveComparator < MOEOT > comp(obj);
00096             // sort
00097             std::sort(_pop.begin(), _pop.end(), comp);
00098             // min & max
00099             min = _pop[0].objectiveVector()[obj];
00100             max = _pop[_pop.size()-1].objectiveVector()[obj];
00101             // set the diversity value to infiny for min and max
00102             _pop[0].diversity(inf());
00103             _pop[_pop.size()-1].diversity(inf());
00104             for (unsigned i=1; i<_pop.size()-1; i++)
00105             {
00106                 distance = (_pop[i+1].objectiveVector()[obj] - _pop[i-1].objectiveVector()[obj]) / (max-min);
00107                 _pop[i].diversity(_pop[i].diversity() + distance);
00108             }
00109         }
00110     }
00111 
00112 };
00113 
00114 #endif /*MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_*/

Generated on Tue Apr 17 16:53:21 2007 for ParadisEO-MOEO by  doxygen 1.5.1