moeoFrontByFrontCrowdingDistanceDiversityAssignment.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // moeoFrontByFrontCrowdingDistanceDiversityAssignment.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 MOEOFRONTBYFRONTCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_
00014 #define MOEOFRONTBYFRONTCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_
00015 
00016 #include <diversity/moeoCrowdingDistanceDiversityAssignment.h>
00017 #include <comparator/moeoFitnessThenDiversityComparator.h>
00018 
00024 template < class MOEOT >
00025 class moeoFrontByFrontCrowdingDistanceDiversityAssignment : public moeoCrowdingDistanceDiversityAssignment < MOEOT >
00026 {
00027 public:
00028 
00030     typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00031 
00032 
00040     void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
00041     {
00042         std::cout << "WARNING : updateByDeleting not implemented in moeoFrontByFrontCrowdingDistanceDiversityAssignment" << std::endl;
00043     }
00044 
00045 
00046 private:
00047 
00048     using moeoCrowdingDistanceDiversityAssignment < MOEOT >::inf;
00049     using moeoCrowdingDistanceDiversityAssignment < MOEOT >::tiny;
00050 
00055     void setDistances (eoPop < MOEOT > & _pop)
00056     {
00057         unsigned int a,b;
00058         double min, max, distance;
00059         unsigned int nObjectives = MOEOT::ObjectiveVector::nObjectives();
00060         // set diversity to 0 for every individual
00061         for (unsigned int i=0; i<_pop.size(); i++)
00062         {
00063             _pop[i].diversity(0.0);
00064         }
00065         // sort the whole pop according to fitness values
00066         moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator;
00067         std::sort(_pop.begin(), _pop.end(), fitnessComparator);
00068         // compute the crowding distance values for every individual "front" by "front" (front : from a to b)
00069         a = 0;                                  // the front starts at a
00070         while (a < _pop.size())
00071         {
00072             b = lastIndex(_pop,a);      // the front ends at b
00073             // if there is less than 2 individuals in the front...
00074             if ((b-a) < 2)
00075             {
00076                 for (unsigned int i=a; i<=b; i++)
00077                 {
00078                     _pop[i].diversity(inf());
00079                 }
00080             }
00081             // else...
00082             else
00083             {
00084                 // for each objective
00085                 for (unsigned int obj=0; obj<nObjectives; obj++)
00086                 {
00087                     // sort in the descending order using the values of the objective 'obj'
00088                     moeoOneObjectiveComparator < MOEOT > objComp(obj);
00089                     std::sort(_pop.begin()+a, _pop.begin()+b+1, objComp);
00090                     // min & max
00091                     min = _pop[b].objectiveVector()[obj];
00092                     max = _pop[a].objectiveVector()[obj];
00093                     // avoid extreme case
00094                     if (min == max)
00095                     {
00096                         min -= tiny();
00097                         max += tiny();
00098                     }
00099                     // set the diversity value to infiny for min and max
00100                     _pop[a].diversity(inf());
00101                     _pop[b].diversity(inf());
00102                     // set the diversity values for the other individuals
00103                     for (unsigned int i=a+1; i<b; i++)
00104                     {
00105                         distance = (_pop[i-1].objectiveVector()[obj] - _pop[i+1].objectiveVector()[obj]) / (max-min);
00106                         _pop[i].diversity(_pop[i].diversity() + distance);
00107                     }
00108                 }
00109             }
00110             // go to the next front
00111             a = b+1;
00112         }
00113     }
00114 
00115 
00121     unsigned int lastIndex (eoPop < MOEOT > & _pop, unsigned int _start)
00122     {
00123         unsigned int i=_start;
00124         while ( (i<_pop.size()-1) && (_pop[i].fitness()==_pop[i+1].fitness()) )
00125         {
00126             i++;
00127         }
00128         return i;
00129     }
00130 
00131 };
00132 
00133 #endif /*MOEOFRONTBYFRONTCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_*/

Generated on Tue Jun 26 15:13:04 2007 for ParadisEO-MOEO by  doxygen 1.4.7