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 }
00162 objComparator;
00163
00164
00169 void oneObjective (eoPop < MOEOT > & _pop)
00170 {
00171
00172 std::sort(_pop.begin(), _pop.end(), objComparator);
00173
00174 unsigned int rank = 1;
00175 _pop[_pop.size()-1].fitness(rank);
00176 for (unsigned int i=_pop.size()-2; i>=0; i--)
00177 {
00178 if (_pop[i].objectiveVector() != _pop[i+1].objectiveVector())
00179 {
00180 rank++;
00181 }
00182 _pop[i].fitness(rank);
00183 }
00184 }
00185
00186
00191 void twoObjectives (eoPop < MOEOT > & _pop)
00192 {
00193
00194 }
00195
00196
00201 void mObjectives (eoPop < MOEOT > & _pop)
00202 {
00203
00204 std::vector < std::vector<unsigned int> > S(_pop.size());
00205
00206 std::vector < unsigned int > n(_pop.size(), 0);
00207
00208 std::vector < std::vector<unsigned int> > F(_pop.size()+2);
00209
00210 F[1].reserve(_pop.size());
00211 for (unsigned int p=0; p<_pop.size(); p++)
00212 {
00213 for (unsigned int q=0; q<_pop.size(); q++)
00214 {
00215
00216 if ( comparator(_pop[q].objectiveVector(), _pop[p].objectiveVector()) )
00217 {
00218
00219 S[p].push_back(q);
00220 }
00221
00222 else if ( comparator(_pop[p].objectiveVector(), _pop[q].objectiveVector()) )
00223 {
00224
00225 n[p]++;
00226 }
00227 }
00228
00229 if (n[p] == 0)
00230 {
00231
00232 _pop[p].fitness(1);
00233 F[1].push_back(p);
00234 }
00235 }
00236
00237 unsigned int counter=1;
00238 unsigned int p,q;
00239 while (! F[counter].empty())
00240 {
00241
00242 F[counter+1].reserve(_pop.size());
00243 for (unsigned int i=0; i<F[counter].size(); i++)
00244 {
00245 p = F[counter][i];
00246 for (unsigned int j=0; j<S[p].size(); j++)
00247 {
00248 q = S[p][j];
00249 n[q]--;
00250
00251 if (n[q] == 0)
00252 {
00253
00254 _pop[q].fitness(counter+1);
00255 F[counter+1].push_back(q);
00256 }
00257 }
00258 }
00259 counter++;
00260 }
00261 }
00262
00263 };
00264
00265 #endif