diversity assignment corrected in moeoFrontByFrontCrowdingDiversityAssignment.h

This commit is contained in:
Arnaud Liefooghe 2016-04-19 17:34:39 +02:00
commit c0bf207fc1

View file

@ -1,38 +1,40 @@
/* /*
* <moeoFrontByFrontCrowdingDiversityAssignment.h> * <moeoFrontByFrontCrowdingDiversityAssignment.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2009 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2009
* (C) OPAC Team, LIFL, 2002-2009 * (C) OPAC Team, LIFL, 2002-2009
* *
* Arnaud Liefooghe, Waldo Cancino * Arnaud Liefooghe, Waldo Cancino
* *
* This software is governed by the CeCILL license under French law and * April 2016: Bug fix in the sorting of population (corrected thanks to Martin Stehlik)
* abiding by the rules of distribution of free software. You can use, *
* modify and/ or redistribute the software under the terms of the CeCILL * This software is governed by the CeCILL license under French law and
* license as circulated by CEA, CNRS and INRIA at the following URL * abiding by the rules of distribution of free software. You can use,
* "http://www.cecill.info". * modify and/ or redistribute the software under the terms of the CeCILL
* * license as circulated by CEA, CNRS and INRIA at the following URL
* As a counterpart to the access to the source code and rights to copy, * "http://www.cecill.info".
* modify and redistribute granted by the license, users are provided only *
* with a limited warranty and the software's author, the holder of the * As a counterpart to the access to the source code and rights to copy,
* economic rights, and the successive licensors have only limited liability. * modify and redistribute granted by the license, users are provided only
* * with a limited warranty and the software's author, the holder of the
* In this respect, the user's attention is drawn to the risks associated * economic rights, and the successive licensors have only limited liability.
* with loading, using, modifying and/or developing or reproducing the *
* software by the user in light of its specific status of free software, * In this respect, the user's attention is drawn to the risks associated
* that may mean that it is complicated to manipulate, and that also * with loading, using, modifying and/or developing or reproducing the
* therefore means that it is reserved for developers and experienced * software by the user in light of its specific status of free software,
* professionals having in-depth computer knowledge. Users are therefore * that may mean that it is complicated to manipulate, and that also
* encouraged to load and test the software's suitability as regards their * therefore means that it is reserved for developers and experienced
* requirements in conditions enabling the security of their systems and/or * professionals having in-depth computer knowledge. Users are therefore
* data to be ensured and, more generally, to use and operate it in the * encouraged to load and test the software's suitability as regards their
* same conditions as regards security. * requirements in conditions enabling the security of their systems and/or
* The fact that you are presently reading this means that you have had * data to be ensured and, more generally, to use and operate it in the
* knowledge of the CeCILL license and that you accept its terms. * same conditions as regards security.
* * The fact that you are presently reading this means that you have had
* ParadisEO WebSite : http://paradiseo.gforge.inria.fr * knowledge of the CeCILL license and that you accept its terms.
* Contact: paradiseo-help@lists.gforge.inria.fr *
* * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
*/ * Contact: paradiseo-help@lists.gforge.inria.fr
*
*/
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT2_H_ #ifndef MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT2_H_
@ -50,8 +52,8 @@
*/ */
template < class MOEOT > template < class MOEOT >
class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversityAssignment < MOEOT > class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversityAssignment < MOEOT >
{ {
public: public:
/** the objective vector type of the solutions */ /** the objective vector type of the solutions */
typedef typename MOEOT::ObjectiveVector ObjectiveVector; typedef typename MOEOT::ObjectiveVector ObjectiveVector;
@ -66,10 +68,10 @@ class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversity
*/ */
void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
{ {
std::cout << "WARNING : updateByDeleting not implemented in moeoFrontByFrontCrowdingDistanceDiversityAssignment" << std::endl; std::cout << "WARNING : updateByDeleting not implemented in moeoFrontByFrontCrowdingDistanceDiversityAssignment" << std::endl;
} }
private: private:
using moeoCrowdingDiversityAssignment < MOEOT >::inf; using moeoCrowdingDiversityAssignment < MOEOT >::inf;
using moeoCrowdingDiversityAssignment < MOEOT >::tiny; using moeoCrowdingDiversityAssignment < MOEOT >::tiny;
@ -81,73 +83,76 @@ class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversity
void setDistances (eoPop <MOEOT> & _pop) void setDistances (eoPop <MOEOT> & _pop)
{ {
unsigned int a,b; unsigned int a,b;
double min, max, distance; double min, max, distance;
unsigned int nObjectives = MOEOT::ObjectiveVector::nObjectives(); unsigned int nObjectives = MOEOT::ObjectiveVector::nObjectives();
// set diversity to 0 for every individual // set diversity to 0 for every individual
for (unsigned int i=0; i<_pop.size(); i++) for (unsigned int i=0; i<_pop.size(); i++)
{ {
_pop[i].diversity(0.0); _pop[i].diversity(0.0);
} }
// sort the whole pop according to fitness values // sort the whole pop according to fitness values
moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator; moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator;
std::vector<MOEOT *> sortedptrpop; std::vector<MOEOT *> sortedptrpop;
sortedptrpop.resize(_pop.size()); sortedptrpop.resize(_pop.size());
// due to intensive sort operations for this diversity assignment, // due to intensive sort operations for this diversity assignment,
// it is more efficient to perform sorts using only pointers to the // it is more efficient to perform sorts using only pointers to the
// population members in order to avoid copy of individuals // population members in order to avoid copy of individuals
for(unsigned int i=0; i< _pop.size(); i++) sortedptrpop[i] = & (_pop[i]); for(unsigned int i=0; i< _pop.size(); i++) sortedptrpop[i] = & (_pop[i]);
//sort the pointers to population members //sort the pointers to population members
moeoPtrComparator<MOEOT> cmp2( fitnessComparator); moeoPtrComparator<MOEOT> cmp2( fitnessComparator);
std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2); std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2);
// compute the crowding distance values for every individual "front" by "front" (front : from a to b) // compute the crowding distance values for every individual "front" by "front" (front : from a to b)
a = 0; // the front starts at a a = 0; // the front starts at a
while (a < _pop.size()) while (a < _pop.size())
{ {
b = lastIndex(sortedptrpop,a); // the front ends at b b = lastIndex(sortedptrpop,a); // the front ends at b
//b = lastIndex(_pop,a); // the front ends at b //b = lastIndex(_pop,a); // the front ends at b
// if there is less than 2 individuals in the front... // if there is less than 2 individuals in the front...
if ((b-a) < 2) if ((b-a) < 2)
{ {
for (unsigned int i=a; i<=b; i++) for (unsigned int i=a; i<=b; i++)
{ {
sortedptrpop[i]->diversity(inf()); sortedptrpop[i]->diversity(inf());
//_pop[i].diversity(inf()); //_pop[i].diversity(inf());
} }
} }
// else... // else...
else else
{ {
// for each objective // for each objective
for (unsigned int obj=0; obj<nObjectives; obj++) for (unsigned int obj=0; obj<nObjectives; obj++)
{ {
// sort in the descending order using the values of the objective 'obj' // sort in the descending order using the values of the objective 'obj'
moeoOneObjectiveComparator < MOEOT > objComp(obj); moeoOneObjectiveComparator < MOEOT > objComp(obj);
moeoPtrComparator<MOEOT> cmp2( objComp ); moeoPtrComparator<MOEOT> cmp2( objComp );
std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2); // erroneous part
// min & max // std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2);
min = (sortedptrpop[b])->objectiveVector()[obj]; // correct version
max = (sortedptrpop[a])->objectiveVector()[obj]; std::sort(sortedptrpop.begin()+a, sortedptrpop.begin()+b+1, cmp2);
// min & max
min = (sortedptrpop[b])->objectiveVector()[obj];
max = (sortedptrpop[a])->objectiveVector()[obj];
// avoid extreme case // avoid extreme case
if (min == max) if (min == max)
{ {
min -= tiny(); min -= tiny();
max += tiny(); max += tiny();
} }
// set the diversity value to infiny for min and max // set the diversity value to infiny for min and max
sortedptrpop[a]->diversity(inf()); sortedptrpop[a]->diversity(inf());
sortedptrpop[b]->diversity(inf()); sortedptrpop[b]->diversity(inf());
// set the diversity values for the other individuals // set the diversity values for the other individuals
for (unsigned int i=a+1; i<b; i++) for (unsigned int i=a+1; i<b; i++)
{ {
distance = ( sortedptrpop[i-1]->objectiveVector()[obj] - sortedptrpop[i+1]->objectiveVector()[obj] ) / (max-min); distance = ( sortedptrpop[i-1]->objectiveVector()[obj] - sortedptrpop[i+1]->objectiveVector()[obj] ) / (max-min);
sortedptrpop[i]->diversity(sortedptrpop[i]->diversity() + distance); sortedptrpop[i]->diversity(sortedptrpop[i]->diversity() + distance);
} }
} }
} }
// go to the next front // go to the next front
a = b+1; a = b+1;
} }
} }
@ -161,15 +166,15 @@ class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversity
unsigned int lastIndex (std::vector<MOEOT *> & _pop, unsigned int _start) unsigned int lastIndex (std::vector<MOEOT *> & _pop, unsigned int _start)
{ {
unsigned int i=_start; unsigned int i=_start;
while ( (i<_pop.size()-1) && (_pop[i]->fitness()==_pop[i+1]->fitness()) ) while ( (i<_pop.size()-1) && (_pop[i]->fitness()==_pop[i+1]->fitness()) )
{ {
i++; i++;
} }
return i; return i;
} }
}; };
#endif /*MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT_H_*/ #endif /*MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT_H_*/