00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef MOEOFASTNONDOMINATEDSORTINGFITNESSASSIGNMENT_H_
00039 #define MOEOFASTNONDOMINATEDSORTINGFITNESSASSIGNMENT_H_
00040
00041 #include <vector>
00042 #include <eoPop.h>
00043 #include <comparator/moeoObjectiveObjectiveVectorComparator.h>
00044 #include <comparator/moeoObjectiveVectorComparator.h>
00045 #include <comparator/moeoParetoObjectiveVectorComparator.h>
00046 #include <fitness/moeoParetoBasedFitnessAssignment.h>
00047
00048
00056 template < class MOEOT >
00057 class moeoFastNonDominatedSortingFitnessAssignment : public moeoParetoBasedFitnessAssignment < MOEOT >
00058 {
00059 public:
00060
00062 typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00063
00064
00068 moeoFastNonDominatedSortingFitnessAssignment() : comparator(paretoComparator)
00069 {}
00070
00071
00076 moeoFastNonDominatedSortingFitnessAssignment(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator) : comparator(_comparator)
00077 {}
00078
00079
00084 void operator()(eoPop < MOEOT > & _pop)
00085 {
00086
00087 unsigned int nObjectives = MOEOT::ObjectiveVector::nObjectives();
00088 if (nObjectives == 1)
00089 {
00090
00091 oneObjective(_pop);
00092 }
00093 else if (nObjectives == 2)
00094 {
00095
00096 mObjectives(_pop);
00097 }
00098 else if (nObjectives > 2)
00099 {
00100
00101 mObjectives(_pop);
00102 }
00103 else
00104 {
00105
00106 throw std::runtime_error("Problem with the number of objectives in moeoNonDominatedSortingFitnessAssignment");
00107 }
00108
00109 double max = _pop[0].fitness();
00110 for (unsigned int i=1 ; i<_pop.size() ; i++)
00111 {
00112 max = std::max(max, _pop[i].fitness());
00113 }
00114 for (unsigned int i=0 ; i<_pop.size() ; i++)
00115 {
00116 _pop[i].fitness(max - _pop[i].fitness());
00117 }
00118 }
00119
00120
00126 void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
00127 {
00128 for (unsigned int i=0; i<_pop.size(); i++)
00129 {
00130
00131 if ( comparator(_pop[i].objectiveVector(), _objVec) )
00132 {
00133 _pop[i].fitness(_pop[i].fitness()+1);
00134 }
00135 }
00136 }
00137
00138
00139 private:
00140
00142 moeoObjectiveVectorComparator < ObjectiveVector > & comparator;
00144 moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator;
00146 class ObjectiveComparator : public moeoComparator < MOEOT >
00147 {
00148 public:
00154 const bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2)
00155 {
00156 return cmp(_moeo1.objectiveVector(), _moeo2.objectiveVector());
00157 }
00158 private:
00160 moeoObjectiveObjectiveVectorComparator < ObjectiveVector > cmp;
00161 } objComparator;
00162
00163
00168 void oneObjective (eoPop < MOEOT > & _pop)
00169 {
00170
00171 std::sort(_pop.begin(), _pop.end(), objComparator);
00172
00173 unsigned int rank = 1;
00174 _pop[_pop.size()-1].fitness(rank);
00175 for (unsigned int i=_pop.size()-2; i>=0; i--)
00176 {
00177 if (_pop[i].objectiveVector() != _pop[i+1].objectiveVector())
00178 {
00179 rank++;
00180 }
00181 _pop[i].fitness(rank);
00182 }
00183 }
00184
00185
00190 void twoObjectives (eoPop < MOEOT > & _pop)
00191 {
00192
00193 }
00194
00195
00200 void mObjectives (eoPop < MOEOT > & _pop)
00201 {
00202
00203 std::vector < std::vector<unsigned int> > S(_pop.size());
00204
00205 std::vector < unsigned int > n(_pop.size(), 0);
00206
00207 std::vector < std::vector<unsigned int> > F(_pop.size()+2);
00208
00209 F[1].reserve(_pop.size());
00210 for (unsigned int p=0; p<_pop.size(); p++)
00211 {
00212 for (unsigned int q=0; q<_pop.size(); q++)
00213 {
00214
00215 if ( comparator(_pop[q].objectiveVector(), _pop[p].objectiveVector()) )
00216 {
00217
00218 S[p].push_back(q);
00219 }
00220
00221 else if ( comparator(_pop[p].objectiveVector(), _pop[q].objectiveVector()) )
00222 {
00223
00224 n[p]++;
00225 }
00226 }
00227
00228 if (n[p] == 0)
00229 {
00230
00231 _pop[p].fitness(1);
00232 F[1].push_back(p);
00233 }
00234 }
00235
00236 unsigned int counter=1;
00237 unsigned int p,q;
00238 while (! F[counter].empty())
00239 {
00240
00241 F[counter+1].reserve(_pop.size());
00242 for (unsigned int i=0; i<F[counter].size(); i++)
00243 {
00244 p = F[counter][i];
00245 for (unsigned int j=0; j<S[p].size(); j++)
00246 {
00247 q = S[p][j];
00248 n[q]--;
00249
00250 if (n[q] == 0)
00251 {
00252
00253 _pop[q].fitness(counter+1);
00254 F[counter+1].push_back(q);
00255 }
00256 }
00257 }
00258 counter++;
00259 }
00260 }
00261
00262 };
00263
00264 #endif