From 8880c5c1d419d73bf9b81c6916e372534399206c Mon Sep 17 00:00:00 2001 From: jhumeau Date: Wed, 28 May 2008 13:00:22 +0000 Subject: [PATCH] new conception of archives: moeoArchive became abstract, some new archives extends it. git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1188 331e1502-861f-0410-8da2-ba01fb791d7f --- .../paradiseo-moeo/src/archive/moeoArchive.h | 99 ++--- .../src/archive/moeoBoundedArchive.h | 92 +++++ .../src/archive/moeoFixedSizeArchive.h | 93 +++++ .../src/archive/moeoSPEA2Archive.h | 353 ++++++++++++++++++ .../src/archive/moeoUnboundedArchive.h | 142 +++++++ 5 files changed, 705 insertions(+), 74 deletions(-) create mode 100644 trunk/paradiseo-moeo/src/archive/moeoBoundedArchive.h create mode 100644 trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h create mode 100644 trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h create mode 100644 trunk/paradiseo-moeo/src/archive/moeoUnboundedArchive.h diff --git a/trunk/paradiseo-moeo/src/archive/moeoArchive.h b/trunk/paradiseo-moeo/src/archive/moeoArchive.h index fcfe1bc7a..7da115229 100644 --- a/trunk/paradiseo-moeo/src/archive/moeoArchive.h +++ b/trunk/paradiseo-moeo/src/archive/moeoArchive.h @@ -47,15 +47,11 @@ */ template < class MOEOT > class moeoArchive : public eoPop < MOEOT > - { - public: +{ +public: using eoPop < MOEOT > :: size; - using eoPop < MOEOT > :: resize; using eoPop < MOEOT > :: operator[]; - using eoPop < MOEOT > :: back; - using eoPop < MOEOT > :: pop_back; - /** * The type of an objective vector for a solution @@ -84,17 +80,17 @@ class moeoArchive : public eoPop < MOEOT > * @param _objectiveVector the objective vector to compare with the current archive */ bool dominates (const ObjectiveVector & _objectiveVector) const - { + { for (unsigned int i = 0; i * @param _objectiveVector the objective vector to compare with the current archive */ bool contains (const ObjectiveVector & _objectiveVector) const - { + { for (unsigned int i = 0; i & _pop) - { - for (unsigned int i=0; i<_pop.size(); i++) - { - update(_pop[i]); - } - } - + virtual void operator()(const eoPop < MOEOT > & _pop) = 0; /** * Returns true if the current archive contains the same objective vectors than the given archive _arch @@ -176,31 +128,30 @@ class moeoArchive : public eoPop < MOEOT > */ bool equals (const moeoArchive < MOEOT > & _arch) { - for (unsigned int i=0; i & comparator; /** A moeoObjectiveVectorComparator based on Pareto dominance (used as default) */ moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; - }; +}; #endif /*MOEOARCHIVE_H_ */ diff --git a/trunk/paradiseo-moeo/src/archive/moeoBoundedArchive.h b/trunk/paradiseo-moeo/src/archive/moeoBoundedArchive.h new file mode 100644 index 000000000..848b2d86f --- /dev/null +++ b/trunk/paradiseo-moeo/src/archive/moeoBoundedArchive.h @@ -0,0 +1,92 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Lille-Nord Europe, 2006-2008 +* (C) OPAC Team, LIFL, 2002-2008 +* +* Arnaud Liefooghe +* Jeremie Humeau +* +* This software is governed by the CeCILL license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL +* license as circulated by CEA, CNRS and INRIA at the following URL +* "http://www.cecill.info". +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited liability. +* +* In this respect, the user's attention is drawn to the risks associated +* with loading, using, modifying and/or developing or reproducing the +* software by the user in light of its specific status of free software, +* that may mean that it is complicated to manipulate, and that also +* therefore means that it is reserved for developers and experienced +* professionals having in-depth computer knowledge. Users are therefore +* encouraged to load and test the software's suitability as regards their +* requirements in conditions enabling the security of their systems and/or +* data to be ensured and, more generally, to use and operate it in the +* same conditions as regards security. +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL license and that you accept its terms. +* +* ParadisEO WebSite : http://paradiseo.gforge.inria.fr +* Contact: paradiseo-help@lists.gforge.inria.fr +* +*/ +//----------------------------------------------------------------------------- +// moeoBoundedArchive.h +//----------------------------------------------------------------------------- +#ifndef MOEOBOUNDEDARCHIVE_H_ +#define MOEOBOUNDEDARCHIVE_H_ + +#include + +/** + * An archive is a secondary population that stores non-dominated solutions. + */ +template < class MOEOT > +class moeoBoundedArchive : public moeoArchive < MOEOT > +{ +public: + + /** + * The type of an objective vector for a solution + */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Default ctor. + * The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance + */ + moeoBoundedArchive() : moeoArchive < MOEOT >() {} + + + /** + * Ctor + * @param _comparator the moeoObjectiveVectorComparator used to compare solutions + */ + moeoBoundedArchive(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 + */ + virtual void operator()(const eoPop < MOEOT > & _pop)=0; + +}; + +#endif /*MOEOBOUNDEDARCHIVE_H_*/ diff --git a/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h b/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h new file mode 100644 index 000000000..8cf005f06 --- /dev/null +++ b/trunk/paradiseo-moeo/src/archive/moeoFixedSizeArchive.h @@ -0,0 +1,93 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Lille-Nord Europe, 2006-2008 +* (C) OPAC Team, LIFL, 2002-2008 +* +* Arnaud Liefooghe +* Jeremie Humeau +* +* This software is governed by the CeCILL license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL +* license as circulated by CEA, CNRS and INRIA at the following URL +* "http://www.cecill.info". +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited liability. +* +* In this respect, the user's attention is drawn to the risks associated +* with loading, using, modifying and/or developing or reproducing the +* software by the user in light of its specific status of free software, +* that may mean that it is complicated to manipulate, and that also +* therefore means that it is reserved for developers and experienced +* professionals having in-depth computer knowledge. Users are therefore +* encouraged to load and test the software's suitability as regards their +* requirements in conditions enabling the security of their systems and/or +* data to be ensured and, more generally, to use and operate it in the +* same conditions as regards security. +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL license and that you accept its terms. +* +* ParadisEO WebSite : http://paradiseo.gforge.inria.fr +* Contact: paradiseo-help@lists.gforge.inria.fr +* +*/ +//----------------------------------------------------------------------------- +// moeoFixedSizeArchive.h +//----------------------------------------------------------------------------- + +#ifndef MOEOFIXEDSIZEARCHIVE_H_ +#define MOEOFIXEDSIZEARCHIVE_H_ + +#include + +/** + * An FixedSizeArchive is a secondary population that stores non-dominated solutions whith a fixed size. + */ +template < class MOEOT > +class moeoFixedSizeArchive : public moeoArchive < MOEOT > +{ +public: + + /** + * The type of an objective vector for a solution + */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Default ctor. + * The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance + */ + moeoFixedSizeArchive() : moeoArchive < MOEOT >() {} + + + /** + * Ctor + * @param _comparator the moeoObjectiveVectorComparator used to compare solutions + */ + 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 + */ + virtual void operator()(const eoPop < MOEOT > & _pop)=0; + +}; + +#endif /*MOEOFIXEDSIZEARCHIVE_H_*/ diff --git a/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h b/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h new file mode 100644 index 000000000..3ead99918 --- /dev/null +++ b/trunk/paradiseo-moeo/src/archive/moeoSPEA2Archive.h @@ -0,0 +1,353 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Lille-Nord Europe, 2006-2008 +* (C) OPAC Team, LIFL, 2002-2008 +* +* Arnaud Liefooghe +* Jeremie Humeau +* +* This software is governed by the CeCILL license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL +* license as circulated by CEA, CNRS and INRIA at the following URL +* "http://www.cecill.info". +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited liability. +* +* In this respect, the user's attention is drawn to the risks associated +* with loading, using, modifying and/or developing or reproducing the +* software by the user in light of its specific status of free software, +* that may mean that it is complicated to manipulate, and that also +* therefore means that it is reserved for developers and experienced +* professionals having in-depth computer knowledge. Users are therefore +* encouraged to load and test the software's suitability as regards their +* requirements in conditions enabling the security of their systems and/or +* data to be ensured and, more generally, to use and operate it in the +* same conditions as regards security. +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL license and that you accept its terms. +* +* ParadisEO WebSite : http://paradiseo.gforge.inria.fr +* Contact: paradiseo-help@lists.gforge.inria.fr +* +*/ +//----------------------------------------------------------------------------- +// moeoSPEA2Archive.cpp +//----------------------------------------------------------------------------- + +#ifndef MOEOSPEA2ARCHIVE_H_ +#define MOEOSPEA2ARCHIVE_H_ + +#include +#include +#include +#include + +/** + * An archive is a secondary population that stores non-dominated solutions. + */ +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; + + /** + * 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) + */ + 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 + * @param _maxSize the size of archive (must be smaller or egal to the population size) + */ + 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 + * @param _maxSize the size of archive (must be smaller or egal to the population size) + */ + 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 + * @param _maxSize the size of archive (must be smaller or egal to the population size) + */ + 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 + * @param _dist the distance used + * @param _comparator the functor used to compare objective vectors + * @param _maxSize the size of archive (must be smaller or egal to the population size) + */ + 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) { + 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) { + 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++) { + copy_pop[i].index=i; + copy_pop[i].fitness=_pop[i].fitness(); + copy_pop[i].diversity=_pop[i].diversity(); + } + + //Sort this vector in decrease order of fitness+diversity + 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) { + foo= std::min(_pop.size(), maxSize-borne); + + for (i=0; i< foo ; i++) { + push_back(_pop[copy_pop[i].index]); + borne++; + } + + } + 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) { + unsigned int inf=j; + unsigned int p; + unsigned int k=0; + unsigned int l=0; + double tmp=0; + unsigned int tmp2=0; + + //search bounds of copy_pop where are the goods elements + while ((copy_pop[j].fitness > -1) && (j < copy_pop.size())) + j++; + + 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; k=borne) ) + tmp=distance(operator[](k), _pop[copy_pop[l-borne+inf].index]); + else + tmp=distance(_pop[copy_pop[k-borne+inf].index], _pop[copy_pop[l-borne+inf].index]); + + matrice[k].push_back(std::pair(l,tmp)); + matrice[l].push_back(std::pair(k,tmp)); + } + } + } + + for (k=0; k(-1,k)); + } + + //sort the lines of the matrice between us (by shortest distance) + std::sort(matrice.begin(),matrice.end(), CmpVector()); + + //vectors and iterators used to replace some archive element by some pop element + std::vector notkeeped; + std::vector keeped; + std::vector< std::vector< std::pair > >::iterator matrice_it=matrice.begin(); + std::vector< std::pair >::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 (_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) { + 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())) { + if ((*it1).second < (*it2).second) + return true; + else if ((*it1).second > (*it2).second) + return false; + it1++; + it2++; + } + return true; + } + }; + + /** + * Comparator of two pair : compare the second pair's value + */ + 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 + */ + class Wrapper + { + public: + /** + * Ctor. + * @param _comp the comparator + */ + Wrapper(moeoComparator < MOEOT > & _comp) : comp(_comp) + {} + /** + * Returns true if _moeo1 is greater than _moeo2 according to the comparator + * _moeo1 the first individual + * _moeo2 the first individual + */ + bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2) + { + return comp(_moeo1,_moeo2); + } + private: + /** the comparator */ + moeoComparator < MOEOT > & comp; + } + 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 new file mode 100644 index 000000000..7cf08170b --- /dev/null +++ b/trunk/paradiseo-moeo/src/archive/moeoUnboundedArchive.h @@ -0,0 +1,142 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Lille-Nord Europe, 2006-2008 +* (C) OPAC Team, LIFL, 2002-2008 +* +* Arnaud Liefooghe +* Jeremie Humeau +* +* This software is governed by the CeCILL license under French law and +* abiding by the rules of distribution of free software. You can use, +* modify and/ or redistribute the software under the terms of the CeCILL +* license as circulated by CEA, CNRS and INRIA at the following URL +* "http://www.cecill.info". +* +* As a counterpart to the access to the source code and rights to copy, +* modify and redistribute granted by the license, users are provided only +* with a limited warranty and the software's author, the holder of the +* economic rights, and the successive licensors have only limited liability. +* +* In this respect, the user's attention is drawn to the risks associated +* with loading, using, modifying and/or developing or reproducing the +* software by the user in light of its specific status of free software, +* that may mean that it is complicated to manipulate, and that also +* therefore means that it is reserved for developers and experienced +* professionals having in-depth computer knowledge. Users are therefore +* encouraged to load and test the software's suitability as regards their +* requirements in conditions enabling the security of their systems and/or +* data to be ensured and, more generally, to use and operate it in the +* same conditions as regards security. +* The fact that you are presently reading this means that you have had +* knowledge of the CeCILL license and that you accept its terms. +* +* ParadisEO WebSite : http://paradiseo.gforge.inria.fr +* Contact: paradiseo-help@lists.gforge.inria.fr +* +*/ +//----------------------------------------------------------------------------- +// moeoUnboundedArchive.h +//----------------------------------------------------------------------------- +#ifndef MOEOUNBOUNDEDARCHIVE_H_ +#define MOEOUNBOUNDEDARCHIVE_H_ + +#include + +/** + * An archive is a secondary population that stores 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; + + + /** + * The type of an objective vector for a solution + */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * Default ctor. + * The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance + */ + moeoUnboundedArchive() : moeoArchive < MOEOT >() {} + + + /** + * Ctor + * @param _comparator the moeoObjectiveVectorComparator used to compare solutions + */ + 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 + */ + void operator()(const MOEOT & _moeo) + { + // first step: removing the dominated solutions from the archive + for (unsigned int j=0; j & _pop) + { + for (unsigned int i=0; i<_pop.size(); i++) + { + (*this)(_pop[i]); + } + } + +}; + +#endif /*MOEOUNBOUNDEDARCHIVE_H_*/