From 34a339e96a257929b382a4e30f21587cb3900db4 Mon Sep 17 00:00:00 2001 From: liefooga Date: Mon, 14 May 2007 13:30:11 +0000 Subject: [PATCH] bug corrected git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@294 331e1502-861f-0410-8da2-ba01fb791d7f --- .../moeoCrowdingDistanceDiversityAssignment.h | 90 +++++++++++++++---- 1 file changed, 73 insertions(+), 17 deletions(-) diff --git a/branches/paradiseo-moeo-1.0/src/moeoCrowdingDistanceDiversityAssignment.h b/branches/paradiseo-moeo-1.0/src/moeoCrowdingDistanceDiversityAssignment.h index aa8eec28e..0d60bfa04 100755 --- a/branches/paradiseo-moeo-1.0/src/moeoCrowdingDistanceDiversityAssignment.h +++ b/branches/paradiseo-moeo-1.0/src/moeoCrowdingDistanceDiversityAssignment.h @@ -40,6 +40,15 @@ public: } + /** + * Returns a very small value that can be used to avoid extreme cases (where the min bound == the max bound) + */ + double tiny() const + { + return 1e-6; + } + + /** * Computes diversity values for every solution contained in the population _pop * @param _pop the population @@ -81,34 +90,81 @@ private: */ void setDistances (eoPop < MOEOT > & _pop) { + unsigned a,b; double min, max, distance; unsigned nObjectives = MOEOT::ObjectiveVector::nObjectives(); - // set diversity to 0 + // set diversity to 0 for every individual for (unsigned i=0; i<_pop.size(); i++) { - _pop[i].diversity(0); + _pop[i].diversity(0.0); } - // for each objective - for (unsigned obj=0; obj fitnessComparator; + std::sort(_pop.begin(), _pop.end(), fitnessComparator); + // compute the crowding distance values for every individual "front" by "front" (front : from a to b) + a = 0; // the front starts at a + while (a < _pop.size()) { - // comparator - moeoOneObjectiveComparator < MOEOT > comp(obj); - // sort - std::sort(_pop.begin(), _pop.end(), comp); - // min & max - min = _pop[0].objectiveVector()[obj]; - max = _pop[_pop.size()-1].objectiveVector()[obj]; - // set the diversity value to infiny for min and max - _pop[0].diversity(inf()); - _pop[_pop.size()-1].diversity(inf()); - for (unsigned i=1; i<_pop.size()-1; i++) + b = lastIndex(_pop,a); // the front ends at b + // if there is 2 or less individuals in the front... + if ((b-a) < 2) { - distance = (_pop[i+1].objectiveVector()[obj] - _pop[i-1].objectiveVector()[obj]) / (max-min); - _pop[i].diversity(_pop[i].diversity() + distance); + for (unsigned i=a; i<=b; i++) + { + _pop[i].diversity(inf()); + } } + // else... + else + { + // for each objective + for (unsigned obj=0; obj objComp(obj); + std::sort(_pop.begin()+a, _pop.begin()+b+1, objComp); + // min & max + min = _pop[b].objectiveVector()[obj]; + max = _pop[a].objectiveVector()[obj]; + // avoid extreme case + if (min == max) + { + min -= tiny(); + max += tiny(); + } + // set the diversity value to infiny for min and max + _pop[a].diversity(inf()); + _pop[b].diversity(inf()); + // set the diversity values for the other individuals + for (unsigned i=a+1; i & _pop, unsigned _start) + { + unsigned i=_start; + while ( (i<_pop.size()-1) && (_pop[i].fitness()==_pop[i+1].fitness()) ) + { + i++; + } + return i; + } + + }; #endif /*MOEOCROWDINGDISTANCEDIVERSITYASSIGNMENT_H_*/