moeoEasyEA constructors corrected and tested

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1267 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
liefooga 2008-11-27 13:53:01 +00:00
commit 1309736161
4 changed files with 81 additions and 117 deletions

View file

@ -1,7 +1,7 @@
/* /*
* <moeoEasyEA.h> * <moeoEasyEA.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
* (C) OPAC Team, LIFL, 2002-2007 * (C) OPAC Team, LIFL, 2002-2008
* *
* Arnaud Liefooghe * Arnaud Liefooghe
* *
@ -56,11 +56,11 @@
*/ */
template < class MOEOT > template < class MOEOT >
class moeoEasyEA: public moeoEA < MOEOT > class moeoEasyEA: public moeoEA < MOEOT >
{ {
public: public:
/** /**
* Ctor taking a breed and merge. * Ctor taking a breed.
* @param _continuator the stopping criteria * @param _continuator the stopping criteria
* @param _eval the evaluation functions * @param _eval the evaluation functions
* @param _breed the breeder * @param _breed the breeder
@ -69,16 +69,13 @@ class moeoEasyEA: public moeoEA < MOEOT >
* @param _diversityEval the diversity evaluation scheme * @param _diversityEval the diversity evaluation scheme
* @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process * @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process
*/ */
moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoBreed < MOEOT > & _breed, moeoReplacement < MOEOT > & _replace, moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoBreed < MOEOT > & _breed, moeoReplacement < MOEOT > & _replace, moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) :
moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) continuator(_continuator), eval (_eval), loopEval(_eval), popEval(loopEval), selectTransform(dummySelect, dummyTransform), breed(_breed), replace(_replace), fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
:
continuator(_continuator), eval (_eval), loopEval(_eval), popEval(loopEval), selectTransform(dummySelect, dummyTransform), breed(_breed), mergeReduce(dummyMerge, dummyReduce), replace(_replace),
fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
{} {}
/** /**
* Ctor taking a breed, a merge and a eoPopEval. * Ctor taking a breed and a popEval.
* @param _continuator the stopping criteria * @param _continuator the stopping criteria
* @param _popEval the evaluation functions for the whole population * @param _popEval the evaluation functions for the whole population
* @param _breed the breeder * @param _breed the breeder
@ -87,35 +84,13 @@ class moeoEasyEA: public moeoEA < MOEOT >
* @param _diversityEval the diversity evaluation scheme * @param _diversityEval the diversity evaluation scheme
* @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process * @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process
*/ */
moeoEasyEA(eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoBreed < MOEOT > & _breed, moeoReplacement < MOEOT > & _replace, moeoEasyEA(eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoBreed < MOEOT > & _breed, moeoReplacement < MOEOT > & _replace, moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) :
moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) continuator(_continuator), eval(dummyEval), loopEval(dummyEval), popEval(_popEval), selectTransform(dummySelect, dummyTransform), breed(_breed), replace(_replace), fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
:
continuator(_continuator), eval (dummyEval), loopEval(dummyEval), popEval(_popEval), selectTransform(dummySelect, dummyTransform), breed(_breed), mergeReduce(dummyMerge, dummyReduce), replace(_replace),
fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
{} {}
/** /**
* Ctor taking a breed, a merge and a reduce. * Ctor taking a select and a transform.
* @param _continuator the stopping criteria
* @param _eval the evaluation functions
* @param _breed the breeder
* @param _merge the merge scheme
* @param _reduce the reduce scheme
* @param _fitnessEval the fitness evaluation scheme
* @param _diversityEval the diversity evaluation scheme
* @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process
*/
moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoBreed < MOEOT > & _breed, eoMerge < MOEOT > & _merge, eoReduce< MOEOT > & _reduce,
moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false)
:
continuator(_continuator), eval(_eval), loopEval(_eval), popEval(loopEval), selectTransform(dummySelect, dummyTransform), breed(_breed), mergeReduce(_merge,_reduce), replace(mergeReduce),
fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
{}
/**
* Ctor taking a select, a transform and a replacement.
* @param _continuator the stopping criteria * @param _continuator the stopping criteria
* @param _eval the evaluation functions * @param _eval the evaluation functions
* @param _select the selection scheme * @param _select the selection scheme
@ -125,31 +100,24 @@ class moeoEasyEA: public moeoEA < MOEOT >
* @param _diversityEval the diversity evaluation scheme * @param _diversityEval the diversity evaluation scheme
* @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process * @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process
*/ */
moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoSelect < MOEOT > & _select, eoTransform < MOEOT > & _transform, moeoReplacement < MOEOT > & _replace, moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoSelect < MOEOT > & _select, eoTransform < MOEOT > & _transform, moeoReplacement < MOEOT > & _replace, moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) :
moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) continuator(_continuator), eval(_eval), loopEval(_eval), popEval(loopEval), selectTransform(_select, _transform), breed(selectTransform), replace(_replace), fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
:
continuator(_continuator), eval(_eval), loopEval(_eval), popEval(loopEval), selectTransform(_select, _transform), breed(selectTransform), mergeReduce(dummyMerge, dummyReduce), replace(_replace),
fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
{} {}
/** /**
* Ctor taking a select, a transform, a merge and a reduce. * Ctor taking a select, a transform.and a popEval
* @param _continuator the stopping criteria * @param _continuator the stopping criteria
* @param _eval the evaluation functions * @param _eval the evaluation functions
* @param _select the selection scheme * @param _select the selection scheme
* @param _transform the tranformation scheme * @param _transform the tranformation scheme
* @param _merge the merge scheme * @param _replace the replacement strategy
* @param _reduce the reduce scheme
* @param _fitnessEval the fitness evaluation scheme * @param _fitnessEval the fitness evaluation scheme
* @param _diversityEval the diversity evaluation scheme * @param _diversityEval the diversity evaluation scheme
* @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process * @param _evalFitAndDivBeforeSelection put this parameter to 'true' if you want to re-evalue the fitness and the diversity of the population before the selection process
*/ */
moeoEasyEA(eoContinue < MOEOT > & _continuator, eoEvalFunc < MOEOT > & _eval, eoSelect < MOEOT > & _select, eoTransform < MOEOT > & _transform, eoMerge < MOEOT > & _merge, eoReduce< MOEOT > & _reduce, moeoEasyEA(eoContinue < MOEOT > & _continuator, eoPopEvalFunc < MOEOT > & _popEval, eoSelect < MOEOT > & _select, eoTransform < MOEOT > & _transform, moeoReplacement < MOEOT > & _replace, moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) :
moeoFitnessAssignment < MOEOT > & _fitnessEval, moeoDiversityAssignment < MOEOT > & _diversityEval, bool _evalFitAndDivBeforeSelection = false) continuator(_continuator), eval(dummyEval), loopEval(dummyEval), popEval(_popEval), selectTransform(_select, _transform), breed(selectTransform), replace(_replace), fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
:
continuator(_continuator), eval(_eval), loopEval(_eval), popEval(loopEval), selectTransform(_select, _transform), breed(selectTransform), mergeReduce(_merge,_reduce), replace(mergeReduce),
fitnessEval(_fitnessEval), diversityEval(_diversityEval), evalFitAndDivBeforeSelection(_evalFitAndDivBeforeSelection)
{} {}
@ -159,67 +127,82 @@ class moeoEasyEA: public moeoEA < MOEOT >
*/ */
virtual void operator()(eoPop < MOEOT > & _pop) virtual void operator()(eoPop < MOEOT > & _pop)
{ {
eoPop < MOEOT > offspring, empty_pop; eoPop < MOEOT > offspring, empty_pop;
popEval(empty_pop, _pop); // A first eval of pop. popEval(empty_pop, _pop); // A first eval of pop.
bool firstTime = true; bool firstTime = true;
do do
{ {
try try
{ {
unsigned int pSize = _pop.size(); unsigned int pSize = _pop.size();
offspring.clear(); // new offspring offspring.clear(); // new offspring
// fitness and diversity assignment (if you want to or if it is the first generation) // fitness and diversity assignment (if you want to or if it is the first generation)
//std::cout << "eval ou first time" << std::endl; if (evalFitAndDivBeforeSelection || firstTime)
if (evalFitAndDivBeforeSelection || firstTime)
{ {
firstTime = false; firstTime = false;
//std::cout << "fitness eval" << std::endl; //std::cout << "fitness eval" << std::endl;
fitnessEval(_pop); fitnessEval(_pop);
//std::cout << "diversity eval" << std::endl; //std::cout << "diversity eval" << std::endl;
diversityEval(_pop); diversityEval(_pop);
} }
//std::cout << "breed" << std::endl; breed(_pop, offspring);
breed(_pop, offspring); popEval(_pop, offspring); // eval of parents + offspring if necessary
//std::cout << "pop eval" << std::endl; replace(_pop, offspring); // after replace, the new pop. is in _pop
popEval(_pop, offspring); // eval of parents + offspring if necessary if (pSize > _pop.size())
//std::cout << "replace" << std::endl;
replace(_pop, offspring); // after replace, the new pop. is in _pop
if (pSize > _pop.size())
{ {
throw std::runtime_error("Population shrinking!"); throw std::runtime_error("Population shrinking!");
} }
else if (pSize < _pop.size()) else if (pSize < _pop.size())
{ {
throw std::runtime_error("Population growing!"); throw std::runtime_error("Population growing!");
} }
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::string s = e.what(); std::string s = e.what();
s.append( " in moeoEasyEA"); s.append( " in moeoEasyEA");
throw std::runtime_error( s ); throw std::runtime_error( s );
} }
} }
while (continuator(_pop)); while (continuator(_pop));
} }
protected: protected:
/** the stopping criteria */ /** the stopping criteria */
eoContinue < MOEOT > & continuator; eoContinue < MOEOT > & continuator;
/** a dummy eval */
class eoDummyEval : public eoEvalFunc < MOEOT >
{
public:
void operator()(MOEOT &) {}
}
dummyEval;
/** the evaluation functions */ /** the evaluation functions */
eoEvalFunc < MOEOT > & eval; eoEvalFunc < MOEOT > & eval;
/** to evaluate the whole population */ /** to evaluate the whole population */
eoPopLoopEval < MOEOT > loopEval; eoPopLoopEval < MOEOT > loopEval;
/** to evaluate the whole population */ /** to evaluate the whole population */
eoPopEvalFunc < MOEOT > & popEval; eoPopEvalFunc < MOEOT > & popEval;
/** dummy select */
class DummySelect : public eoSelect < MOEOT >
{
public :
void operator()(const eoPop<MOEOT>&, eoPop<MOEOT>&) {}
}
dummySelect;
/** breed: a select followed by a transform */ /** breed: a select followed by a transform */
eoSelectTransform < MOEOT > selectTransform; eoSelectTransform < MOEOT > selectTransform;
/** dummy transform */
class DummyTransform : public eoTransform < MOEOT >
{
public :
void operator()(eoPop<MOEOT>&) {}
}
dummyTransform;
/** the breeder */ /** the breeder */
eoBreed < MOEOT > & breed; eoBreed < MOEOT > & breed;
/** replacement: a merge followed by a reduce */
eoMergeReduce < MOEOT > mergeReduce;
/** the replacment strategy */ /** the replacment strategy */
moeoReplacement < MOEOT > & replace; moeoReplacement < MOEOT > & replace;
/** the fitness assignment strategy */ /** the fitness assignment strategy */
@ -228,35 +211,7 @@ class moeoEasyEA: public moeoEA < MOEOT >
moeoDiversityAssignment < MOEOT > & diversityEval; moeoDiversityAssignment < MOEOT > & diversityEval;
/** if this parameter is set to 'true', the fitness and the diversity of the whole population will be re-evaluated before the selection process */ /** if this parameter is set to 'true', the fitness and the diversity of the whole population will be re-evaluated before the selection process */
bool evalFitAndDivBeforeSelection; bool evalFitAndDivBeforeSelection;
/** a dummy eval */
class eoDummyEval : public eoEvalFunc < MOEOT >
{
public: /** the dummy functor */
void operator()(MOEOT &)
{}
}
dummyEval;
/** a dummy select */
class eoDummySelect : public eoSelect < MOEOT >
{
public: /** the dummy functor */
void operator()(const eoPop < MOEOT > &, eoPop < MOEOT > &)
{}
}
dummySelect;
/** a dummy transform */
class eoDummyTransform : public eoTransform < MOEOT >
{
public: /** the dummy functor */
void operator()(eoPop < MOEOT > &)
{}
}
dummyTransform;
/** a dummy merge */
eoNoElitism < MOEOT > dummyMerge;
/** a dummy reduce */
eoTruncate < MOEOT > dummyReduce;
}; };
#endif /*MOEOEASYEA_H_*/ #endif /*MOEOEASYEA_H_*/

View file

@ -50,6 +50,7 @@
#include <algo/moeoLS.h> #include <algo/moeoLS.h>
#include <algo/moeoNSGA.h> #include <algo/moeoNSGA.h>
#include <algo/moeoNSGAII.h> #include <algo/moeoNSGAII.h>
#include <algo/moeoSEEA.h>
#include <algo/moeoSPEA2.h> #include <algo/moeoSPEA2.h>
#include <archive/moeoArchive.h> #include <archive/moeoArchive.h>

View file

@ -50,6 +50,7 @@ SET (TEST_LIST
t-moeoIBEA t-moeoIBEA
t-moeoNSGA t-moeoNSGA
t-moeoNSGAII t-moeoNSGAII
t-moeoSEEA
t-moeoMax3Obj t-moeoMax3Obj
t-moeoEasyEA t-moeoEasyEA
t-moeoDominanceCountFitnessAssignment t-moeoDominanceCountFitnessAssignment

View file

@ -1,7 +1,7 @@
/* /*
* <t-moeoEasyEA.cpp> * <t-moeoEasyEA.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
* (C) OPAC Team, LIFL, 2002-2007 * (C) OPAC Team, LIFL, 2002-2008
* *
* Arnaud Liefooghe * Arnaud Liefooghe
* *
@ -88,11 +88,14 @@ int main()
std::cout << "[moeoEasyEA]" << std::endl; std::cout << "[moeoEasyEA]" << std::endl;
TestEval eval; TestEval eval;
eoPopLoopEval <Solution> loopEval(eval);
eoPopEvalFunc <Solution> & popEval(loopEval);
eoQuadCloneOp < Solution > xover; eoQuadCloneOp < Solution > xover;
eoUniformMutation < Solution > mutation(0.05); eoUniformMutation < Solution > mutation(0.05);
eoSequentialOp < Solution > op; eoSequentialOp < Solution > op;
op.add(xover, 1.0); op.add(xover, 1.0);
op.add(mutation, 1.0); op.add(mutation, 1.0);
eoSGATransform < Solution > transform(xover, 1.0, mutation, 1.0);
eoRealVectorBounds bounds(1, 1.0, 2.0); eoRealVectorBounds bounds(1, 1.0, 2.0);
eoRealInitBounded < Solution > init(bounds); eoRealInitBounded < Solution > init(bounds);
eoPop < Solution > pop(20, init); eoPop < Solution > pop(20, init);
@ -100,15 +103,19 @@ int main()
moeoFastNonDominatedSortingFitnessAssignment < Solution > fitnessAssignment; moeoFastNonDominatedSortingFitnessAssignment < Solution > fitnessAssignment;
moeoFrontByFrontCrowdingDiversityAssignment < Solution > diversityAssignment; moeoFrontByFrontCrowdingDiversityAssignment < Solution > diversityAssignment;
moeoFitnessThenDiversityComparator < Solution > comparator; moeoFitnessThenDiversityComparator < Solution > comparator;
moeoDetTournamentSelect < Solution > select(comparator, 2); moeoDetTournamentSelect < Solution > selectOne(comparator, 2);
moeoElitistReplacement < Solution > replace(fitnessAssignment, diversityAssignment, comparator); moeoElitistReplacement < Solution > replace(fitnessAssignment, diversityAssignment, comparator);
eoGeneralBreeder < Solution > breed(select, op); eoGeneralBreeder < Solution > breed(selectOne, op);
eoSelectMany < Solution > select(selectOne, 1.0);
// build EA // build EA
moeoEasyEA < Solution > algo (term, eval, breed, replace, fitnessAssignment, diversityAssignment); moeoEasyEA < Solution > algo1 (term, eval, breed, replace, fitnessAssignment, diversityAssignment);
moeoEasyEA < Solution > algo2 (term, popEval, breed, replace, fitnessAssignment, diversityAssignment);
moeoEasyEA < Solution > algo3 (term, eval, select, transform, replace, fitnessAssignment, diversityAssignment);
moeoEasyEA < Solution > algo4 (term, popEval, select, transform, replace, fitnessAssignment, diversityAssignment);
// run the algo // run the algo
algo(pop); algo4(pop);
// final pop // final pop
std::cout << "Final population" << std::endl; std::cout << "Final population" << std::endl;