moeoFrontByFrontCrowdingDiversityAssignment.h

00001 /*
00002 * <moeoFrontByFrontCrowdingDiversityAssignment.h>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
00004 * (C) OPAC Team, LIFL, 2002-2007
00005 *
00006 * Arnaud Liefooghe
00007 *
00008 * This software is governed by the CeCILL license under French law and
00009 * abiding by the rules of distribution of free software.  You can  use,
00010 * modify and/ or redistribute the software under the terms of the CeCILL
00011 * license as circulated by CEA, CNRS and INRIA at the following URL
00012 * "http://www.cecill.info".
00013 *
00014 * As a counterpart to the access to the source code and  rights to copy,
00015 * modify and redistribute granted by the license, users are provided only
00016 * with a limited warranty  and the software's author,  the holder of the
00017 * economic rights,  and the successive licensors  have only  limited liability.
00018 *
00019 * In this respect, the user's attention is drawn to the risks associated
00020 * with loading,  using,  modifying and/or developing or reproducing the
00021 * software by the user in light of its specific status of free software,
00022 * that may mean  that it is complicated to manipulate,  and  that  also
00023 * therefore means  that it is reserved for developers  and  experienced
00024 * professionals having in-depth computer knowledge. Users are therefore
00025 * encouraged to load and test the software's suitability as regards their
00026 * requirements in conditions enabling the security of their systems and/or
00027 * data to be ensured and,  more generally, to use and operate it in the
00028 * same conditions as regards security.
00029 * The fact that you are presently reading this means that you have had
00030 * knowledge of the CeCILL license and that you accept its terms.
00031 *
00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00033 * Contact: paradiseo-help@lists.gforge.inria.fr
00034 *
00035 */
00036 //-----------------------------------------------------------------------------
00037 
00038 #ifndef MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT_H_
00039 #define MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT_H_
00040 
00041 #include <diversity/moeoCrowdingDiversityAssignment.h>
00042 #include <comparator/moeoFitnessThenDiversityComparator.h>
00043 
00049 template < class MOEOT >
00050 class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversityAssignment < MOEOT >
00051   {
00052   public:
00053 
00055     typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00056 
00057 
00065     void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
00066     {
00067       std::cout << "WARNING : updateByDeleting not implemented in moeoFrontByFrontCrowdingDistanceDiversityAssignment" << std::endl;
00068     }
00069 
00070 
00071   private:
00072 
00073     using moeoCrowdingDiversityAssignment < MOEOT >::inf;
00074     using moeoCrowdingDiversityAssignment < MOEOT >::tiny;
00075 
00080     void setDistances (eoPop < MOEOT > & _pop)
00081     {
00082       unsigned int a,b;
00083       double min, max, distance;
00084       unsigned int nObjectives = MOEOT::ObjectiveVector::nObjectives();
00085       // set diversity to 0 for every individual
00086       for (unsigned int i=0; i<_pop.size(); i++)
00087         {
00088           _pop[i].diversity(0.0);
00089         }
00090       // sort the whole pop according to fitness values
00091       moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator;
00092       std::sort(_pop.begin(), _pop.end(), fitnessComparator);
00093       // compute the crowding distance values for every individual "front" by "front" (front : from a to b)
00094       a = 0;                                    // the front starts at a
00095       while (a < _pop.size())
00096         {
00097           b = lastIndex(_pop,a);        // the front ends at b
00098           // if there is less than 2 individuals in the front...
00099           if ((b-a) < 2)
00100             {
00101               for (unsigned int i=a; i<=b; i++)
00102                 {
00103                   _pop[i].diversity(inf());
00104                 }
00105             }
00106           // else...
00107           else
00108             {
00109               // for each objective
00110               for (unsigned int obj=0; obj<nObjectives; obj++)
00111                 {
00112                   // sort in the descending order using the values of the objective 'obj'
00113                   moeoOneObjectiveComparator < MOEOT > objComp(obj);
00114                   std::sort(_pop.begin()+a, _pop.begin()+b+1, objComp);
00115                   // min & max
00116                   min = _pop[b].objectiveVector()[obj];
00117                   max = _pop[a].objectiveVector()[obj];
00118                   // avoid extreme case
00119                   if (min == max)
00120                     {
00121                       min -= tiny();
00122                       max += tiny();
00123                     }
00124                   // set the diversity value to infiny for min and max
00125                   _pop[a].diversity(inf());
00126                   _pop[b].diversity(inf());
00127                   // set the diversity values for the other individuals
00128                   for (unsigned int i=a+1; i<b; i++)
00129                     {
00130                       distance = (_pop[i-1].objectiveVector()[obj] - _pop[i+1].objectiveVector()[obj]) / (max-min);
00131                       _pop[i].diversity(_pop[i].diversity() + distance);
00132                     }
00133                 }
00134             }
00135           // go to the next front
00136           a = b+1;
00137         }
00138     }
00139 
00140 
00146     unsigned int lastIndex (eoPop < MOEOT > & _pop, unsigned int _start)
00147     {
00148       unsigned int i=_start;
00149       while ( (i<_pop.size()-1) && (_pop[i].fitness()==_pop[i+1].fitness()) )
00150         {
00151           i++;
00152         }
00153       return i;
00154     }
00155 
00156   };
00157 
00158 #endif /*MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT_H_*/

Generated on Thu Mar 13 15:54:07 2008 for ParadisEO-MOEO-MultiObjectiveEvolvingObjects by  doxygen 1.4.7