From 68ec20fd66dacee459f05249ae5da99170b50652 Mon Sep 17 00:00:00 2001 From: wcancino Date: Fri, 23 Jan 2009 11:31:18 +0000 Subject: [PATCH] Modifies PhyloMOEA clases in order to compile to last Paradiseo-MOEO svn version git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1363 331e1502-861f-0410-8da2-ba01fb791d7f --- .../PhyloMOEA/PhyloMOEA/PhyloMOEA.cpp | 7 +- .../PhyloMOEA/PhyloMOEOPartitionStat.h | 2 +- .../PhyloMOEA/PhyloMOEA/PhyloMOEO_archive.h | 12 +- .../PhyloMOEA/moeoElitistReplacement2.h | 2 +- ...FrontByFrontCrowdingDiversityAssignment2.h | 18 +- .../PhyloMOEA/PhyloMOEA/moeoNSGAII2.h | 202 ++++++++++-------- 6 files changed, 136 insertions(+), 107 deletions(-) diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEA.cpp b/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEA.cpp index 2bc76ff6b..38bbf847f 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEA.cpp +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEA.cpp @@ -7,7 +7,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -165,7 +166,7 @@ int main(int argc, char *argv[]) cout << " done\n"; PhyloMOEOFinalSolutionsArchive finalsolutions; - finalsolutions.update(population); + finalsolutions.operator()(population); //remove_final_solutions( population ); // optimize remaining solutions @@ -202,7 +203,7 @@ int main(int argc, char *argv[]) cout << "\nCalculating Pareto-optimal Solutions..."; PhyloMOEOParetoSolutionsArchive paretosolutions; - paretosolutions.update(finalsolutions); + paretosolutions.operator()(finalsolutions); paretosolutions.save_scores(path + datafile + "_pareto_scores_" + expid + ".txt","#Pareto Solutions Scores"); paretosolutions.save_trees(path + datafile + "_pareto_trees_" + expid + ".txt"); cout << " done\n"; diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEOPartitionStat.h b/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEOPartitionStat.h index 0066181fd..1c419d5dd 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEOPartitionStat.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEOPartitionStat.h @@ -20,7 +20,7 @@ #ifndef PHYLOMOEOPARTITONSTAT_H_ #define PHYLOMOEOPARTITONSTAT_H_ -#include +#include #include //#include diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEO_archive.h b/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEO_archive.h index 96c652b09..8b352ff08 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEO_archive.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/PhyloMOEO_archive.h @@ -20,7 +20,7 @@ #ifndef PHYLOMOEO_ARCHIVE_H #define PHYLOMOEO_ARCHIVE_H -#include +#include typedef moeoArchive PhyloMOEOArchive; typedef PhyloMOEOArchive PhyloMOEOPFArchive; @@ -28,7 +28,7 @@ typedef PhyloMOEOArchive PhyloMOEOPFArchive; -class PhyloMOEOParetoSolutionsArchive:public moeoArchive +class PhyloMOEOParetoSolutionsArchive:public moeoUnboundedArchive { public: void save_trees( std::string filename, string title="" ) @@ -65,6 +65,7 @@ class PhyloMOEOParetoSolutionsArchive:public moeoArchive class PhyloMOEODummyArchive:public PhyloMOEOParetoSolutionsArchive { public: + void update(const eoPop < PhyloMOEO > & _pop) { std::copy(_pop.begin(), _pop.end(), back_inserter(*this) ); @@ -74,8 +75,15 @@ class PhyloMOEODummyArchive:public PhyloMOEOParetoSolutionsArchive class PhyloMOEOFinalSolutionsArchive: public PhyloMOEOParetoSolutionsArchive { public: + // overwrite the default operator + void operator()(const eoPop < PhyloMOEO > & _pop) { update(_pop); } + protected: + + + void update(const eoPop < PhyloMOEO > & _pop) { + std::copy(_pop.begin(), _pop.end(), back_inserter(*this) ); moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator; diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h index 2f7544126..cad98c676 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoElitistReplacement2.h @@ -44,7 +44,7 @@ #include #include #include -#include +#include /** * Elitist replacement strategy that consists in keeping the N best individuals. diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h index c033978ad..f5d5f56f4 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoFrontByFrontCrowdingDiversityAssignment2.h @@ -40,7 +40,7 @@ #include #include -#include +#include /** @@ -91,12 +91,12 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit } // sort the whole pop according to fitness values moeoFitnessThenDiversityComparator < MOEOT > fitnessComparator; - std::vector sortedptrpop; + std::vector sortedptrpop; sortedptrpop.resize(_pop.size()); // due to intensive sort operations for this diversity assignment, // it is more efficient to perform sorts using only pointers to the // population members in order to avoid copy of individuals - std::transform( _pop.begin(), _pop.end(), sortedptrpop.begin(), typename eoPop::Ref()); + for(int i=0; i< _pop.size(); i++) sortedptrpop[i] = & (_pop[i]); //sort the pointers to population members moeoPtrComparator cmp2( fitnessComparator); std::sort(sortedptrpop.begin(), sortedptrpop.end(), cmp2); @@ -111,7 +111,7 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit { for (unsigned int i=a; i<=b; i++) { - ((MOEOT *)sortedptrpop[i])->diversity(inf()); + sortedptrpop[i]->diversity(inf()); //_pop[i].diversity(inf()); } } @@ -136,13 +136,13 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit max += tiny(); } // set the diversity value to infiny for min and max - ((MOEOT *)sortedptrpop[a])->diversity(inf()); - ((MOEOT *)sortedptrpop[b])->diversity(inf()); + sortedptrpop[a]->diversity(inf()); + sortedptrpop[b]->diversity(inf()); // set the diversity values for the other individuals for (unsigned int i=a+1; iobjectiveVector()[obj] - (sortedptrpop[i+1])->objectiveVector()[obj]) / (max-min); - ((MOEOT *)sortedptrpop[i])->diversity(sortedptrpop[i]->diversity() + distance); + distance = ( sortedptrpop[i-1]->objectiveVector()[obj] - sortedptrpop[i+1]->objectiveVector()[obj] ) / (max-min); + sortedptrpop[i]->diversity(sortedptrpop[i]->diversity() + distance); } } } @@ -159,7 +159,7 @@ class moeoFrontByFrontCrowdingDiversityAssignment2 : public moeoCrowdingDiversit * @param _start the index to start from */ - unsigned int lastIndex (std::vector & _pop, unsigned int _start) + 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()) ) diff --git a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h index 2a6b353d4..06b00d732 100644 --- a/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h +++ b/contribution/branches/PhyloMOEA/PhyloMOEA/moeoNSGAII2.h @@ -1,7 +1,7 @@ /* * -* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 -* (C) OPAC Team, LIFL, 2002-2007 +* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 +* (C) OPAC Team, LIFL, 2002-2008 * * Arnaud Liefooghe * @@ -39,6 +39,7 @@ #define MOEONSGAII2_H_ #include +#include #include #include #include @@ -47,144 +48,163 @@ #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). + * NSGA-II (Non-dominated Sorting Genetic Algorithm II). + * Deb, K., S. Agrawal, A. Pratap, and T. Meyarivan. A fast elitist non-dominated sorting genetic algorithm for multi-objective optimization: NSGA-II. IEEE Transactions on Evolutionary Computation, Vol. 6, No 2, pp 182-197 (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) - {} - +{ +public: /** * 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 + * @param _maxGen maximum 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) + defaultGenContinuator(_maxGen), continuator(defaultGenContinuator), eval(_eval), defaultPopEval(_eval), popEval(defaultPopEval), select (2), selectMany(select,0.0), selectTransform(defaultSelect, defaultTransform), defaultSGAGenOp(_crossover, _pCross, _mutation, _pMut), genBreed (select, defaultSGAGenOp), breed (genBreed), replace (fitnessAssignment, diversityAssignment) {} /** - * 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. + * Ctor with a eoContinue and a eoGenOp. * @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) + * @param _op variation operators + */ + moeoNSGAII2 (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoGenOp < MOEOT > & _op) : + defaultGenContinuator(0), continuator(_continuator), eval(_eval), defaultPopEval(_eval), popEval(defaultPopEval), select(2), + selectMany(select,0.0), selectTransform(defaultSelect, defaultTransform), defaultSGAGenOp(defaultQuadOp, 1.0, defaultMonOp, 1.0), genBreed(select, _op), breed(genBreed), replace (fitnessAssignment, diversityAssignment) {} /** - * Apply a few generation of evolution to the population _pop until the stopping criteria is verified. + * Ctor with a eoContinue, a eoPopEval and a eoGenOp. + * @param _continuator stopping criteria + * @param _popEval population evaluation function + * @param _op variation operators + */ + moeoNSGAII2 (eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoGenOp < MOEOT > & _op) : + defaultGenContinuator(0), continuator(_continuator), eval(defaultEval), defaultPopEval(eval), popEval(_popEval), select(2), + selectMany(select,0.0), selectTransform(defaultSelect, defaultTransform), defaultSGAGenOp(defaultQuadOp, 1.0, defaultMonOp, 1.0), genBreed(select, _op), breed(genBreed), replace (fitnessAssignment, diversityAssignment) + {} + + + /** + * Ctor with a eoContinue and a eoTransform. + * @param _continuator stopping criteria + * @param _eval evaluation function + * @param _transform variation operator + */ + moeoNSGAII2 (eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoTransform < MOEOT > & _transform) : + defaultGenContinuator(0), continuator(_continuator), eval(_eval), defaultPopEval(_eval), popEval(defaultPopEval), + select(2), selectMany(select, 1.0), selectTransform(selectMany, _transform), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), genBreed(select, defaultSGAGenOp), breed(selectTransform), replace(fitnessAssignment, diversityAssignment) + {} + + + /** + * Ctor with a eoContinue, a eoPopEval and a eoTransform. + * @param _continuator stopping criteria + * @param _popEval population evaluation function + * @param _transform variation operator + */ + moeoNSGAII2 (eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoTransform < MOEOT > & _transform) : + defaultGenContinuator(0), continuator(_continuator), eval(defaultEval), defaultPopEval(eval), popEval(_popEval), + select(2), selectMany(select, 1.0), selectTransform(selectMany, _transform), defaultSGAGenOp(defaultQuadOp, 0.0, defaultMonOp, 0.0), genBreed(select, defaultSGAGenOp), breed(selectTransform), replace(fitnessAssignment, diversityAssignment) + {} + + + /** + * Apply a the algorithm to the population _pop until the stopping criteria is satified. * @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 + 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); + // 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)); + while (continuator (_pop)); } - protected: +protected: /** a continuator based on the number of generations (used as default) */ eoGenContinue < MOEOT > defaultGenContinuator; /** stopping criteria */ eoContinue < MOEOT > & continuator; + /** default eval */ + class DummyEval : public eoEvalFunc < MOEOT > + { + public: + void operator()(MOEOT &) {} + } + defaultEval; + /** evaluation function */ + eoEvalFunc < MOEOT > & eval; + /** default popEval */ + eoPopLoopEval < MOEOT > defaultPopEval; /** evaluation function used to evaluate the whole population */ - eoPopLoopEval < MOEOT > popEval; + eoPopEvalFunc < MOEOT > & popEval; + /** default select */ + class DummySelect : public eoSelect < MOEOT > + { + public : + void operator()(const eoPop&, eoPop&) {} + } + defaultSelect; /** 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; + /** default select many */ + eoSelectMany < MOEOT > selectMany; + /** select transform */ + eoSelectTransform < MOEOT > selectTransform; /** a default crossover */ eoQuadCloneOp < MOEOT > defaultQuadOp; /** a default mutation */ eoMonCloneOp < MOEOT > defaultMonOp; /** an object for genetic operators (used as default) */ eoSGAGenOp < MOEOT > defaultSGAGenOp; + /** default transform */ + class DummyTransform : public eoTransform < MOEOT > + { + public : + void operator()(eoPop&) {} + } + defaultTransform; /** general breeder */ eoGeneralBreeder < MOEOT > genBreed; /** breeder */ eoBreed < MOEOT > & breed; + /** fitness assignment used in NSGA */ + moeoDominanceDepthFitnessAssignment < MOEOT > fitnessAssignment; + /** diversity assignment used in NSGA-II */ + moeoFrontByFrontCrowdingDiversityAssignment2 < MOEOT > diversityAssignment; + /** elitist replacement */ + moeoElitistReplacement < MOEOT > replace; - }; +}; #endif /*MOEONSGAII_H_*/