From 2a330ee94d74e6cd7625747ecc7329a6de26e9d3 Mon Sep 17 00:00:00 2001 From: wcancino Date: Fri, 16 Jan 2009 15:19:55 +0000 Subject: [PATCH] This new files defines a modified copies of Paradiseo existant classes. So, once approved, they will be part of the main distribution (I hope :) git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1348 331e1502-861f-0410-8da2-ba01fb791d7f --- .../PhyloMOEA/moeoElitistReplacement2.h | 177 ++++++++++++++++ ...FrontByFrontCrowdingDiversityAssignment2.h | 173 ++++++++++++++++ .../PhyloMOEA/PhyloMOEA/moeoNSGAII2.h | 190 ++++++++++++++++++ 3 files changed, 540 insertions(+) create mode 100644 contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h create mode 100644 contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h create mode 100644 contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h new file mode 100644 index 000000000..4a4e6ae52 --- /dev/null +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h @@ -0,0 +1,177 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 +* (C) OPAC Team, LIFL, 2002-2007 +* +* Arnaud Liefooghe +* +* 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 +* +*/ +//----------------------------------------------------------------------------- + +#ifndef MOEOELITISTREPLACEMENT2_H_ +#define MOEOELITISTREPLACEMENT2_H_ + +#include +#include +#include +#include +#include +#include +#include + +/** + * Elitist replacement strategy that consists in keeping the N best individuals. + */ +template < class MOEOT > class moeoElitistReplacement2:public moeoReplacement < MOEOT > + { + public: + + /** + * Full constructor. + * @param _fitnessAssignment the fitness assignment strategy + * @param _diversityAssignment the diversity assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoElitistReplacement2 (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (_comparator) + {} + + + /** + * Constructor without comparator. A moeoFitThenDivComparator is used as default. + * @param _fitnessAssignment the fitness assignment strategy + * @param _diversityAssignment the diversity assignment strategy + */ + moeoElitistReplacement2 (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (defaultComparator) + {} + + + /** + * Constructor without moeoDiversityAssignement. A dummy diversity is used as default. + * @param _fitnessAssignment the fitness assignment strategy + * @param _comparator the comparator (used to compare 2 individuals) + */ + moeoElitistReplacement2 (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoComparator < MOEOT > & _comparator) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (_comparator) + {} + + + /** + * Constructor without moeoDiversityAssignement nor moeoComparator. + * A moeoFitThenDivComparator and a dummy diversity are used as default. + * @param _fitnessAssignment the fitness assignment strategy + */ + moeoElitistReplacement2 (moeoFitnessAssignment < MOEOT > & _fitnessAssignment) : + fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (defaultComparator) + {} + + + /** + * Replaces the first population by adding the individuals of the second one, sorting with a moeoComparator and resizing the whole population obtained. + * @param _parents the population composed of the parents (the population you want to replace) + * @param _offspring the offspring population + */ + void operator () (eoPop < MOEOT > &_parents, eoPop < MOEOT > &_offspring) + { + unsigned int sz = _parents.size (); + // merges offspring and parents into a global population + _parents.reserve (_parents.size () + _offspring.size ()); + std::copy (_offspring.begin (), _offspring.end (), back_inserter (_parents)); + // evaluates the fitness and the diversity of this global population + fitnessAssignment (_parents); + // diversity assignment does several pop sorts, so it is better to performs + // these operations on pointers to avoid copy of individuals + + diversityAssignment(_parents); + // sorts the whole population + // the delindex contains the ordered index of the population according to the comparator + // again, we only sort the index of population instead of population + std::vector delindex; + vectorSortIndex( _parents, delindex, comparator); + //std::sort( delindex.begin(), delindex.end(), comparatorindex(_parents, delindex, comparator)); + // now, in order to rezise of population we remove the populations whose index + // are in the high of delindex + std::sort(delindex.begin()+sz, delindex.end(), std::greater()); + // finally, resize this global population + reduce_population( _parents, delindex,sz); + //_parents.resize (sz); + // and clear the offspring population + _offspring.clear (); + } + + void reduce_population ( eoPop < MOEOT > &_pop, std::vector &index, unsigned int size) + { + for(int j=size; j< index.size(); j++) + { + if( index[j] < index.size()-1) + _pop[ index[j] ] = _pop.back(); + _pop.pop_back(); + } + }; + + protected: + + /** the fitness assignment strategy */ + moeoFitnessAssignment < MOEOT > & fitnessAssignment; + /** the diversity assignment strategy */ + moeoDiversityAssignment < MOEOT > & diversityAssignment; + /** a dummy diversity assignment can be used as default */ + //moeoComparator & comparator; + moeoDummyDiversityAssignment < MOEOT > defaultDiversity; + /** a fitness then diversity comparator can be used as default */ + moeoFitnessThenDiversityComparator < MOEOT > defaultComparator; + /** this object is used to compare solutions in order to sort the population */ + class Cmp + { + public: + /** + * Ctor. + * @param _comp the comparator + */ + Cmp(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(_moeo2,_moeo1); + } + private: + /** the comparator */ + moeoComparator < MOEOT > & comp; + } + comparator; + }; + +#endif /*MOEOELITISTREPLACEMENT_H_ */ diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h new file mode 100644 index 000000000..d6c9b2d8b --- /dev/null +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h @@ -0,0 +1,173 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 +* (C) OPAC Team, LIFL, 2002-2007 +* +* Arnaud Liefooghe +* +* 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 +* +*/ +//----------------------------------------------------------------------------- + +#ifndef MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT2_H_ +#define MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT2_H_ + +#include +#include +#include + + +/** + * Diversity assignment sheme based on crowding proposed in: + * K. Deb, A. Pratap, S. Agarwal, T. Meyarivan, "A Fast and Elitist Multi-Objective Genetic Algorithm: NSGA-II", IEEE Transactions on Evolutionary Computation, vol. 6, no. 2 (2002). + * Tis strategy assigns diversity values FRONT BY FRONT. It is, for instance, used in NSGA-II. + */ +template < class MOEOT > +class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversityAssignment < MOEOT > + { + public: + + /** the objective vector type of the solutions */ + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + + /** + * @warning NOT IMPLEMENTED, DO NOTHING ! + * Updates the diversity values of the whole population _pop by taking the deletion of the objective vector _objVec into account. + * @param _pop the population + * @param _objVec the objective vector + * @warning NOT IMPLEMENTED, DO NOTHING ! + */ + void updateByDeleting(eoPop < MOEOT > & _pop, ObjectiveVector & _objVec) + { + std::cout << "WARNING : updateByDeleting not implemented in moeoFrontByFrontCrowdingDistanceDiversityAssignment" << std::endl; + } + + private: + + using moeoCrowdingDiversityAssignment < MOEOT >::inf; + using moeoCrowdingDiversityAssignment < MOEOT >::tiny; + + /** + * Sets the distance values + * @param _pop the population + */ + + void setDistances (eoPop & _pop) + { + unsigned int a,b; + double min, max, distance; + unsigned int nObjectives = MOEOT::ObjectiveVector::nObjectives(); + // set diversity to 0 for every individual + for (unsigned int i=0; i<_pop.size(); i++) + { + _pop[i].diversity(0.0); + } + // sort the whole pop according to fitness values + moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator; + std::vector sortedpop; + sortedpop.resize(_pop.size()); + std::transform( _pop.begin(), _pop.end(), sortedpop.begin(), typename eoPop::Ref()); + //struct eoPop::Ref ref; + moeoPtrComparator cmp1( fitnessComparator); + std::sort(sortedpop.begin(), sortedpop.end(), cmp1); + //std::sort(_pop.begin(), _pop.end(), fitnessComparator); + // compute the crowding distance values for every individual "front" by "front" (front : from a to b) + a = 0; // the front starts at a + while (a < _pop.size()) + { + b = lastIndex(sortedpop,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 ((b-a) < 2) + { + for (unsigned int i=a; i<=b; i++) + { + ((MOEOT *)sortedpop[i])->diversity(inf()); + //_pop[i].diversity(inf()); + } + } + // else... + else + { + // for each objective + for (unsigned int obj=0; obj objComp(obj); + moeoPtrComparator cmp2( objComp ); + std::sort(sortedpop.begin(), sortedpop.end(), cmp2); + // min & max + min = (sortedpop[b])->objectiveVector()[obj]; + max = (sortedpop[a])->objectiveVector()[obj]; + + // avoid extreme case + if (min == max) + { + min -= tiny(); + max += tiny(); + } + // set the diversity value to infiny for min and max + ((MOEOT *)sortedpop[a])->diversity(inf()); + ((MOEOT *)sortedpop[b])->diversity(inf()); + // set the diversity values for the other individuals + for (unsigned int i=a+1; iobjectiveVector()[obj] - (sortedpop[i+1])->objectiveVector()[obj]) / (max-min); + ((MOEOT *)sortedpop[i])->diversity(sortedpop[i]->diversity() + distance); + } + } + } + // go to the next front + a = b+1; + } + } + + + + /** + * Returns the index of the last individual having the same fitness value than _pop[_start] + * @param _pop the population + * @param _start the index to start from + */ + + unsigned int lastIndex (std::vector & _pop, unsigned int _start) + { + unsigned int i=_start; + while ( (i<_pop.size()-1) && (_pop[i]->fitness()==_pop[i+1]->fitness()) ) + { + i++; + } + return i; + } + + + }; + +#endif /*MOEOFRONTBYFRONTCROWDINGDIVERSITYASSIGNMENT_H_*/ diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h new file mode 100644 index 000000000..2a6b353d4 --- /dev/null +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h @@ -0,0 +1,190 @@ +/* +* +* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 +* (C) OPAC Team, LIFL, 2002-2007 +* +* Arnaud Liefooghe +* +* 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 +* +*/ +//----------------------------------------------------------------------------- + +#ifndef MOEONSGAII2_H_ +#define MOEONSGAII2_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include + +/** + * NSGA-II (Non-dominated Sorting Genetic Algorithm II) as described in: + * Deb, K., S. Agrawal, A. Pratap, and T. Meyarivan : "A fast elitist non-dominated sorting genetic algorithm for multi-objective optimization: NSGA-II". + * In IEEE Transactions on Evolutionary Computation, Vol. 6, No 2, pp 182-197 (April 2002). + * This class builds the NSGA-II algorithm only by using the fine-grained components of the ParadisEO-MOEO framework. + */ +template < class MOEOT > +class moeoNSGAII2: public moeoEA < MOEOT > + { + public: + + /** + * Simple ctor with a eoGenOp. + * @param _maxGen number of generations before stopping + * @param _eval evaluation function + * @param _op variation operator + */ + moeoNSGAII2 (unsigned int _maxGen, eoEvalFunc < MOEOT > & _eval, eoGenOp < MOEOT > & _op) : + defaultGenContinuator(_maxGen), continuator(defaultGenContinuator), popEval(_eval), select(2), + replace(fitnessAssignment, diversityAssignment), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), + genBreed(select, _op), breed(genBreed) + {} + + + /** + * Simple ctor with a eoTransform. + * @param _maxGen number of generations before stopping + * @param _eval evaluation function + * @param _op variation operator + */ + moeoNSGAII2 (unsigned int _maxGen, eoEvalFunc < MOEOT > & _eval, eoTransform < MOEOT > & _op) : + defaultGenContinuator(_maxGen), continuator(defaultGenContinuator), popEval(_eval), select(2), + replace(fitnessAssignment, diversityAssignment), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), + genBreed(select, _op), breed(genBreed) + {} + + + /** + * Ctor with a crossover, a mutation and their corresponding rates. + * @param _maxGen number of generations before stopping + * @param _eval evaluation function + * @param _crossover crossover + * @param _pCross crossover probability + * @param _mutation mutation + * @param _pMut mutation probability + */ + moeoNSGAII2 (unsigned int _maxGen, eoEvalFunc < MOEOT > & _eval, eoQuadOp < MOEOT > & _crossover, double _pCross, eoMonOp < MOEOT > & _mutation, double _pMut) : + defaultGenContinuator(_maxGen), continuator(defaultGenContinuator), popEval(_eval), select (2), + replace (fitnessAssignment, diversityAssignment), defaultSGAGenOp(_crossover, _pCross, _mutation, _pMut), + genBreed (select, defaultSGAGenOp), breed (genBreed) + {} + + + /** + * Ctor with a continuator (instead of _maxGen) and a eoGenOp. + * @param _continuator stopping criteria + * @param _eval evaluation function + * @param _op variation operator + */ + moeoNSGAII2 (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoGenOp < MOEOT > & _op) : + defaultGenContinuator(0), continuator(_continuator), popEval(_eval), select(2), + replace(fitnessAssignment, diversityAssignment), defaultSGAGenOp(defaultQuadOp, 1.0, defaultMonOp, 1.0), + genBreed(select, _op), breed(genBreed) + {} + + + /** + * Ctor with a continuator (instead of _maxGen) and a eoTransform. + * @param _continuator stopping criteria + * @param _eval evaluation function + * @param _op variation operator + */ + moeoNSGAII2 (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoTransform < MOEOT > & _op) : + continuator(_continuator), popEval(_eval), select(2), + replace(fitnessAssignment, diversityAssignment), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), + genBreed(select, _op), breed(genBreed) + {} + + + /** + * Apply a few generation of evolution to the population _pop until the stopping criteria is verified. + * @param _pop the population + */ + virtual void operator () (eoPop < MOEOT > &_pop) + { + eoPop < MOEOT > offspring, empty_pop; + popEval (empty_pop, _pop); // a first eval of _pop + // evaluate fitness and diversity + fitnessAssignment(_pop); + diversityAssignment(_pop); + do + { + // generate offspring, worths are recalculated if necessary + breed (_pop, offspring); + // eval of offspring + popEval (_pop, offspring); + // after replace, the new pop is in _pop. Worths are recalculated if necessary + replace (_pop, offspring); + } + while (continuator (_pop)); + } + + + protected: + + /** a continuator based on the number of generations (used as default) */ + eoGenContinue < MOEOT > defaultGenContinuator; + /** stopping criteria */ + eoContinue < MOEOT > & continuator; + /** evaluation function used to evaluate the whole population */ + eoPopLoopEval < MOEOT > popEval; + /** binary tournament selection */ + moeoDetTournamentSelect < MOEOT > select; + /** fitness assignment used in NSGA-II */ + moeoFastNonDominatedSortingFitnessAssignment < MOEOT > fitnessAssignment; + /** diversity assignment used in NSGA-II */ + moeoFrontByFrontCrowdingDiversityAssignment2 < MOEOT > diversityAssignment; + /** elitist replacement */ + moeoElitistReplacement2 < MOEOT > replace; + /** a default crossover */ + eoQuadCloneOp < MOEOT > defaultQuadOp; + /** a default mutation */ + eoMonCloneOp < MOEOT > defaultMonOp; + /** an object for genetic operators (used as default) */ + eoSGAGenOp < MOEOT > defaultSGAGenOp; + /** general breeder */ + eoGeneralBreeder < MOEOT > genBreed; + /** breeder */ + eoBreed < MOEOT > & breed; + + }; + +#endif /*MOEONSGAII_H_*/