00001
00002
00003
00004
00005
00006
00007
00008
00009
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
00061 for (unsigned int i=0; i<_pop.size(); i++)
00062 {
00063 _pop[i].diversity(0.0);
00064 }
00065
00066 moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator;
00067 std::sort(_pop.begin(), _pop.end(), fitnessComparator);
00068
00069 a = 0;
00070 while (a < _pop.size())
00071 {
00072 b = lastIndex(_pop,a);
00073
00074 if ((b-a) < 2)
00075 {
00076 for (unsigned int i=a; i<=b; i++)
00077 {
00078 _pop[i].diversity(inf());
00079 }
00080 }
00081
00082 else
00083 {
00084
00085 for (unsigned int obj=0; obj<nObjectives; obj++)
00086 {
00087
00088 moeoOneObjectiveComparator < MOEOT > objComp(obj);
00089 std::sort(_pop.begin()+a, _pop.begin()+b+1, objComp);
00090
00091 min = _pop[b].objectiveVector()[obj];
00092 max = _pop[a].objectiveVector()[obj];
00093
00094 if (min == max)
00095 {
00096 min -= tiny();
00097 max += tiny();
00098 }
00099
00100 _pop[a].diversity(inf());
00101 _pop[b].diversity(inf());
00102
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
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