moeoSharingDiversityAssignment.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // moeoSharingDiversityAssignment.h
00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
00006 /*
00007     This library...
00008 
00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
00010  */
00011 //-----------------------------------------------------------------------------
00012 
00013 #ifndef MOEOSHARINGDIVERSITYASSIGNMENT_H_
00014 #define MOEOSHARINGDIVERSITYASSIGNMENT_H_
00015 
00016 #include <eoPop.h>
00017 #include <comparator/moeoDiversityThenFitnessComparator.h>
00018 #include <distance/moeoDistance.h>
00019 #include <distance/moeoDistanceMatrix.h>
00020 #include <distance/moeoEuclideanDistance.h>
00021 #include <diversity/moeoDiversityAssignment.h>
00022 
00027 template < class MOEOT >
00028 class moeoSharingDiversityAssignment : public moeoDiversityAssignment < MOEOT >
00029 {
00030 public:
00031 
00033     typedef typename MOEOT::ObjectiveVector ObjectiveVector;
00034 
00035 
00042     moeoSharingDiversityAssignment(moeoDistance<MOEOT,double> & _distance, double _nicheSize = 0.5, double _alpha = 1.0) : distance(_distance), nicheSize(_nicheSize), alpha(_alpha)
00043     {}
00044 
00045 
00051     moeoSharingDiversityAssignment(double _nicheSize = 0.5, double _alpha = 1.0) : distance(defaultDistance), nicheSize(_nicheSize), alpha(_alpha)
00052     {}
00053 
00054 
00059     void operator()(eoPop < MOEOT > & _pop)
00060     {
00061         // 1 - set simuilarities
00062         setSimilarities(_pop);
00063         // 2 - a higher diversity is better, so the values need to be inverted
00064         moeoDiversityThenFitnessComparator < MOEOT > divComparator;
00065         double max = std::max_element(_pop.begin(), _pop.end(), divComparator)->diversity();
00066         for (unsigned int i=0 ; i<_pop.size() ; i++)
00067         {
00068             _pop[i].diversity(max - _pop[i].diversity());
00069         }
00070     }
00071 
00072 
00080     void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec)
00081     {
00082         std::cout << "WARNING : updateByDeleting not implemented in moeoSharingDiversityAssignment" << std::endl;
00083     }
00084 
00085 
00086 protected:
00087 
00089     moeoDistance < MOEOT , double > & distance;
00091     moeoEuclideanDistance < MOEOT > defaultDistance;
00093     double nicheSize;
00095     double alpha;
00096 
00097 
00102     virtual void setSimilarities(eoPop < MOEOT > & _pop)
00103     {
00104         // compute distances between every individuals
00105         moeoDistanceMatrix < MOEOT , double > dMatrix (_pop.size(), distance);
00106         dMatrix(_pop);
00107         // compute similarities
00108         double sum;
00109         for (unsigned int i=0; i<_pop.size(); i++)
00110         {
00111             sum = 0.0;
00112             for (unsigned int j=0; j<_pop.size(); j++)
00113             {
00114                 sum += sh(dMatrix[i][j]);
00115             }
00116             _pop[i].diversity(sum);
00117         }
00118     }
00119 
00120 
00125     double sh(double _dist)
00126     {
00127         double result;
00128         if (_dist < nicheSize)
00129         {
00130             result = 1.0 - pow(_dist / nicheSize, alpha);
00131         }
00132         else
00133         {
00134             result = 0.0;
00135         }
00136         return result;
00137     }
00138 
00139 };
00140 
00141 
00142 #endif /*MOEOSHARINGDIVERSITYASSIGNMENT_H_*/

Generated on Tue Jun 26 15:13:04 2007 for ParadisEO-MOEO by  doxygen 1.4.7