From fc4166a40e50495362af1f86c2c91980d0ae6d55 Mon Sep 17 00:00:00 2001 From: liefooga Date: Thu, 26 Jun 2008 09:13:04 +0000 Subject: [PATCH] doc added git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1206 331e1502-861f-0410-8da2-ba01fb791d7f --- .../paradiseo-moeo/src/archive/moeoArchive.h | 7 +- .../src/archive/moeoFixedSizeArchive.h | 11 +- .../src/archive/moeoSPEA2Archive.h | 161 +++++++++++------- .../src/archive/moeoUnboundedArchive.h | 19 +-- 4 files changed, 115 insertions(+), 83 deletions(-) diff --git a/trunk/paradiseo-moeo/src/archive/moeoArchive.h b/trunk/paradiseo-moeo/src/archive/moeoArchive.h index 7da115229..689d9c873 100644 --- a/trunk/paradiseo-moeo/src/archive/moeoArchive.h +++ b/trunk/paradiseo-moeo/src/archive/moeoArchive.h @@ -43,7 +43,8 @@ #include /** - * An archive is a secondary population that stores non-dominated solutions. + * Abstract class for representing an archive ; + * an archive is a secondary population that stores non-dominated solutions. */ template < class MOEOT > class moeoArchive : public eoPop < MOEOT > @@ -53,6 +54,7 @@ public: using eoPop < MOEOT > :: size; using eoPop < MOEOT > :: operator[]; + /** * The type of an objective vector for a solution */ @@ -116,12 +118,14 @@ public: */ virtual void operator()(const MOEOT & _moeo) = 0; + /** * Updates the archive with a given population _pop * @param _pop the given population */ virtual void operator()(const eoPop < MOEOT > & _pop) = 0; + /** * Returns true if the current archive contains the same objective vectors than the given archive _arch * @param _arch the given archive @@ -145,6 +149,7 @@ public: return true; } + private: /** The moeoObjectiveVectorComparator used to compare solutions */ diff --git a/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h b/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h index 8cf005f06..e87c4385f 100644 --- a/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h +++ b/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h @@ -42,9 +42,12 @@ #define MOEOFIXEDSIZEARCHIVE_H_ #include +#include +#include /** - * An FixedSizeArchive is a secondary population that stores non-dominated solutions whith a fixed size. + * Abstract class for representing a fixed size archive ; + * a fixed size archive is an archive containing a given number of solutions. */ template < class MOEOT > class moeoFixedSizeArchive : public moeoArchive < MOEOT > @@ -71,17 +74,13 @@ public: moeoFixedSizeArchive(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator) : moeoArchive < MOEOT >( _comparator) {} - /** - * Returns true if the current archive dominates _objectiveVector according to the moeoObjectiveVectorComparator given in the constructor - * @param _objectiveVector the objective vector to compare with the current archive - */ - /** * Updates the archive with a given individual _moeo * @param _moeo the given individual */ virtual void operator()(const MOEOT & _moeo)=0; + /** * Updates the archive with a given population _pop * @param _pop the given population diff --git a/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h b/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h index 3ead99918..8e249008d 100644 --- a/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h +++ b/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h @@ -41,40 +41,50 @@ #ifndef MOEOSPEA2ARCHIVE_H_ #define MOEOSPEA2ARCHIVE_H_ -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include /** - * An archive is a secondary population that stores non-dominated solutions. + * This class represents a bounded archive as defined in the SPEA2 algorithm. + * E. Zitzler, M. Laumanns, and L. Thiele. SPEA2: Improving the Strength Pareto Evolutionary Algorithm. Technical Report 103, + * Computer Engineering and Networks Laboratory (TIK), ETH Zurich, Zurich, Switzerland, 2001. */ template < class MOEOT > class moeoSPEA2Archive : public moeoFixedSizeArchive < MOEOT > { public: - using eoPop < MOEOT > :: size; - using eoPop < MOEOT > :: resize; - using eoPop < MOEOT > :: operator[]; - using eoPop < MOEOT > :: back; - using eoPop < MOEOT > :: pop_back; - using eoPop < MOEOT > :: push_back; - using eoPop < MOEOT > :: begin; - using eoPop < MOEOT > :: end; + using moeoFixedSizeArchive < MOEOT > :: size; + using moeoFixedSizeArchive < MOEOT > :: resize; + using moeoFixedSizeArchive < MOEOT > :: operator[]; + using moeoFixedSizeArchive < MOEOT > :: back; + using moeoFixedSizeArchive < MOEOT > :: pop_back; + using moeoFixedSizeArchive < MOEOT > :: push_back; + using moeoFixedSizeArchive < MOEOT > :: begin; + using moeoFixedSizeArchive < MOEOT > :: end; + /** * The type of an objective vector for a solution */ typedef typename MOEOT::ObjectiveVector ObjectiveVector; + /** * Default ctor. - * @param _maxSize the size of archive (must be smaller or egal to the population size) + * @param _maxSize the size of archive (must be smaller or equal to the population size) */ moeoSPEA2Archive(unsigned int _maxSize=100): moeoFixedSizeArchive < MOEOT >(), maxSize(_maxSize), borne(0), indiComparator(defaultComparator), distance(defaultDistance) {} + /** * Ctor where you can choose your own moeoDistance * @param _dist the distance used @@ -83,6 +93,7 @@ public: moeoSPEA2Archive(moeoDistance & _dist, unsigned int _maxSize=100): moeoFixedSizeArchive < MOEOT >(), maxSize(_maxSize), borne(0), indiComparator(defaultComparator), distance(_dist) {} + /** * Ctor where you can choose your own moeoObjectiveVectorComparator * @param _comparator the functor used to compare objective vectors @@ -91,6 +102,7 @@ public: moeoSPEA2Archive(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator, unsigned int _maxSize=100): moeoFixedSizeArchive < MOEOT >(_comparator), maxSize(_maxSize), borne(0), indiComparator(defaultComparator), distance(defaultDistance) {} + /** * Ctor where you can choose your own moeoComparator * @param _indiComparator the functor used to compare MOEOT @@ -99,6 +111,7 @@ public: moeoSPEA2Archive(moeoComparator & _indiComparator, unsigned int _maxSize=100): moeoFixedSizeArchive < MOEOT >(), maxSize(_maxSize), borne(0), indiComparator(_indiComparator), distance(defaultDistance) {} + /** * Ctor where you can choose your own moeoComparator, moeoDistance and moeoObjectiveVectorComparator * @param _indiComparator the functor used to compare MOEOT @@ -109,35 +122,32 @@ public: moeoSPEA2Archive(moeoComparator & _indiComparator, moeoDistance & _dist, moeoObjectiveVectorComparator < ObjectiveVector > & _comparator, unsigned int _maxSize=100) : moeoFixedSizeArchive < MOEOT >(_comparator), maxSize(_maxSize), borne(0), indiComparator(_indiComparator), distance(_dist) {} + /** * Updates the archive with a given individual _moeo * @param _moeo the given individual */ - void operator()(const MOEOT & _moeo) { + void operator()(const MOEOT & _moeo) + { eoPop < MOEOT > pop_tmp; pop_tmp.push_back(_moeo); operator()(pop_tmp); } + /** * Updates the archive with a given population _pop * @param _pop the given population */ - void operator()(const eoPop < MOEOT > & _pop) { + void operator()(const eoPop < MOEOT > & _pop) + { unsigned int i=0; unsigned int foo=0; - /*std::cout << "\n\narchive avant: "; - for(unsigned k=0; k copy_pop(_pop.size()); - for (i;i<_pop.size(); i++) { + for (i;i<_pop.size(); i++) + { copy_pop[i].index=i; copy_pop[i].fitness=_pop[i].fitness(); copy_pop[i].diversity=_pop[i].diversity(); @@ -147,23 +157,26 @@ public: std::sort(copy_pop.begin(), copy_pop.end(), Cmp()); //If the archive is empty, put in the best elements of the pop - if (borne < maxSize) { + if (borne < maxSize) + { foo= std::min(_pop.size(), maxSize-borne); - - for (i=0; i< foo ; i++) { + for (i=0; i< foo ; i++) + { push_back(_pop[copy_pop[i].index]); borne++; } } - else { + else + { unsigned int j=0; //Sort the archive std::sort(begin(), end(), indiComparator); i=0; //While we have a better element in pop than the worst <= -1 in the archive, replace the worst(of archive) by the best(of pop) - while ( (i -1) { + if (copy_pop[j].fitness > -1) + { unsigned int inf=j; unsigned int p; unsigned int k=0; @@ -186,14 +200,15 @@ public: p=j-inf; - //std::cout << "p: " << p << ", j: " << j << ",inf: " << inf << "\n"; - std::vector< std::vector< std::pair > > matrice(borne+p); //Build the distance matrice(vector of vector) between each keeped elements - if (borne+p>0) { - for (k=0; k0) + { + for (k=0; k=borne) ) @@ -207,7 +222,8 @@ public: } } - for (k=0; k >::iterator it; //search elements of the archive to delete - for (k=0; k= borne) keeped.push_back(tmp2); } //replace some archive element by some pop element - for (k=0; k defaultComparator; + /** distance */ + moeoDistance & distance; + /** default distance */ + moeoEuclideanDistance < MOEOT > defaultDistance; + + /** * Structure needs to copy informations of the pop in order to sort it */ - struct refpop { + struct refpop + { unsigned index; double fitness; double diversity; }; + /** * Comparator of struct refpop : compare fitness+divesity */ - struct Cmp { - bool operator()(const struct refpop& _a, const struct refpop& _b) { + struct Cmp + { + bool operator()(const struct refpop& _a, const struct refpop& _b) + { return ( (_a.diversity + _a.fitness) > (_b.diversity + _b.fitness) ); } }; + /** * Comparator of two vector of pair * Compare the second pair's value of the first element vector, if equals compare the next element vector... */ - struct CmpVector { - bool operator()( const std::vector< std::pair >& _a, const std::vector< std::pair >& _b) { + struct CmpVector + { + bool operator()( const std::vector< std::pair >& _a, const std::vector< std::pair >& _b) + { std::vector< std::pair >::const_iterator it1= _a.begin(); std::vector< std::pair >::const_iterator it2= _b.begin(); - while ( (it1 != _a.end()) && (it2 != _b.end())) { + while ( (it1 != _a.end()) && (it2 != _b.end())) + { if ((*it1).second < (*it2).second) return true; else if ((*it1).second > (*it2).second) @@ -301,15 +339,19 @@ private : } }; + /** * Comparator of two pair : compare the second pair's value */ - struct CmpPair { - bool operator()(const std::pair& _a, const std::pair& _b) { + struct CmpPair + { + bool operator()(const std::pair& _a, const std::pair& _b) + { return _a.second < _b.second; } }; + /** * Wrapper which allow to used an moeoComparator in std::sort * @param _comp the comparator to used @@ -321,8 +363,7 @@ private : * Ctor. * @param _comp the comparator */ - Wrapper(moeoComparator < MOEOT > & _comp) : comp(_comp) - {} + Wrapper(moeoComparator < MOEOT > & _comp) : comp(_comp) {} /** * Returns true if _moeo1 is greater than _moeo2 according to the comparator * _moeo1 the first individual @@ -338,16 +379,6 @@ private : } indiComparator; - /** archive max size */ - unsigned int maxSize; - /** archive size */ - unsigned int borne; - /** default moeoComparator*/ - moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; - /** distance */ - moeoDistance & distance; - /** default distance */ - moeoEuclideanDistance < MOEOT > defaultDistance; }; #endif /*MOEOSPEA2ARCHIVE_H_*/ diff --git a/trunk/paradiseo-moeo/src/archive/moeoUnboundedArchive.h b/trunk/paradiseo-moeo/src/archive/moeoUnboundedArchive.h index 7cf08170b..88625a594 100644 --- a/trunk/paradiseo-moeo/src/archive/moeoUnboundedArchive.h +++ b/trunk/paradiseo-moeo/src/archive/moeoUnboundedArchive.h @@ -41,20 +41,22 @@ #define MOEOUNBOUNDEDARCHIVE_H_ #include +#include +#include /** - * An archive is a secondary population that stores non-dominated solutions. + * An unbounded archive is an archive storing an unbounded number of non-dominated solutions. */ template < class MOEOT > class moeoUnboundedArchive : public moeoArchive < MOEOT > { public: - using eoPop < MOEOT > :: size; - using eoPop < MOEOT > :: resize; - using eoPop < MOEOT > :: operator[]; - using eoPop < MOEOT > :: back; - using eoPop < MOEOT > :: pop_back; + using moeoArchive < MOEOT > :: size; + using moeoArchive < MOEOT > :: resize; + using moeoArchive < MOEOT > :: operator[]; + using moeoArchive < MOEOT > :: back; + using moeoArchive < MOEOT > :: pop_back; /** @@ -77,11 +79,6 @@ public: moeoUnboundedArchive(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator) : moeoArchive < MOEOT >(_comparator) {} - /** - * Returns true if the current archive dominates _objectiveVector according to the moeoObjectiveVectorComparator given in the constructor - * @param _objectiveVector the objective vector to compare with the current archive - */ - /** * Updates the archive with a given individual _moeo * @param _moeo the given individual