tutorial PEO modified

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@903 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
canape 2008-01-24 14:52:36 +00:00
commit 15db5e4fea
37 changed files with 512 additions and 832 deletions

View file

@ -21,13 +21,6 @@ ADD_CUSTOM_COMMAND(
ARGS -E copy_if_different
${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson3/schema.xml
${ParadisEO-PEO_BINARY_DIR}/tutorial/Lesson3)
ADD_CUSTOM_COMMAND(
TARGET install
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy_if_different
${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson3/Lesson3.pdf
${ParadisEO-PEO_BINARY_DIR}/tutorial/Lesson3)
######################################################################################
@ -36,7 +29,7 @@ ADD_CUSTOM_COMMAND(
### 1) Include the sources
######################################################################################
INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src ${MO_SRC_DIR}/src ${ParadisEO-PEO_SOURCE_DIR}/src)
INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src ${MOEO_SRC_DIR}/src ${MO_SRC_DIR}/src ${ParadisEO-PEO_SOURCE_DIR}/src)
######################################################################################

View file

@ -71,77 +71,72 @@ int main (int __argc, char *__argv[])
// First algorithm
/*****************************************************************************************/
eoGenContinue < Indi > genContPara (MAX_GEN);
eoCombinedContinue <Indi> continuatorPara (genContPara);
eoCheckPoint<Indi> checkpoint(continuatorPara);
peoEvalFunc<Indi> plainEval(f);
peoSeqPopEval< Indi > eval(plainEval); // Here, the evaluation is sequential
peoEvalFunc<Indi> mainEval( f );
peoPopEval <Indi> eval(mainEval);
eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX);
eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
eoRankingSelect<Indi> selectionStrategy;
eoSelectNumber<Indi> select(selectionStrategy,POP_SIZE);
eoSegmentCrossover<Indi> crossover;
eoUniformMutation<Indi> mutation(EPSILON);
eoSGATransform<Indi> transform(crossover,CROSS_RATE,mutation,MUT_RATE);
peoSeqTransform<Indi> eaTransform(transform); // Here, the transformation is sequential
eoUniformMutation<Indi> mutation(EPSILON);
peoTransform<Indi> transform(crossover,CROSS_RATE,mutation,MUT_RATE);
peoPop < Indi > pop;
pop.append (POP_SIZE, random);
eoPlusReplacement<Indi> replace;
eoPop < Indi > pop;
pop.append (POP_SIZE, random);
eoPeriodicContinue <Indi> mig_cont( MIG_FREQ ); // Migration occurs periodically
eoRandomSelect<Indi> mig_select_one; // Emigrants are randomly selected
eoSelectNumber<Indi> mig_select (mig_select_one,MIG_SIZE);
eoPlusReplacement<Indi> mig_replace; // Immigrants replace the worse individuals
eoRandomSelect<Indi> mig_select_one;
eoSelector <Indi, peoPop<Indi> > mig_select (mig_select_one,MIG_SIZE,pop);
eoReplace <Indi, peoPop<Indi> > mig_replace (replace,pop);
peoSyncIslandMig<Indi, peoPop<Indi> > mig(MIG_FREQ,mig_select,mig_replace,topology,pop,pop);
checkpoint.add(mig);
eoEasyEA< Indi > eaAlg( checkpoint, eval, select, transform, replace );
peoWrapper parallelEA( eaAlg, pop);
eval.setOwner(parallelEA);
transform.setOwner(parallelEA);
mig.setOwner(parallelEA);
/*****************************************************************************************/
// Second algorithm (on the same model but with others names)
/*****************************************************************************************/
eoGenContinue < Indi > genContPara2 (MAX_GEN);
eoCombinedContinue <Indi> continuatorPara2 (genContPara2);
eoCheckPoint<Indi> checkpoint2(continuatorPara2);
peoEvalFunc<Indi> plainEval2(f);
peoSeqPopEval< Indi > eval2(plainEval2);
peoEvalFunc<Indi> mainEval2( f );
peoPopEval <Indi> eval2(mainEval2);
eoUniformGenerator < double >uGen2 (INIT_POSITION_MIN, INIT_POSITION_MAX);
eoInitFixedLength < Indi > random2 (VEC_SIZE, uGen2);
eoRankingSelect<Indi> selectionStrategy2;
eoSelectNumber<Indi> select2(selectionStrategy2,POP_SIZE);
eoSegmentCrossover<Indi> crossover2;
eoUniformMutation<Indi> mutation2(EPSILON);
eoSGATransform<Indi> transform2(crossover2,CROSS_RATE,mutation2,MUT_RATE);
peoSeqTransform<Indi> eaTransform2(transform2);
eoUniformMutation<Indi> mutation2(EPSILON);
peoTransform<Indi> transform2(crossover2,CROSS_RATE,mutation2,MUT_RATE);
peoPop < Indi > pop2;
pop2.append (POP_SIZE, random2);
eoPlusReplacement<Indi> replace2;
eoPop < Indi > pop2;
pop2.append (POP_SIZE, random2);
eoPeriodicContinue <Indi> mig_cont2( MIG_FREQ );
eoRandomSelect<Indi> mig_select_one2;
eoSelectNumber<Indi> mig_select2 (mig_select_one2,MIG_SIZE);
eoPlusReplacement<Indi> mig_replace2;
eoSelector <Indi, peoPop<Indi> > mig_select2 (mig_select_one2,MIG_SIZE,pop2);
eoReplace <Indi, peoPop<Indi> > mig_replace2 (replace2,pop2);
peoSyncIslandMig<Indi, peoPop<Indi> > mig2(MIG_FREQ,mig_select2,mig_replace2,topology,pop2,pop2);
checkpoint2.add(mig2);
eoEasyEA< Indi > eaAlg2( checkpoint2, eval2, select2, transform2, replace2 );
peoWrapper parallelEA2( eaAlg2, pop2);
eval2.setOwner(parallelEA2);
transform2.setOwner(parallelEA2);
mig2.setOwner(parallelEA2);
/*****************************************************************************************/
// You can choose between :
//
// - Synchronous communication : peoSyncIslandMig<Indi> mig(MIG_FREQ,mig_select,mig_replace,topology,pop,pop);
// - Asynchronous communication : peoAsyncIslandMig<Indi> mig(mig_cont,mig_select,mig_replace,topology,pop,pop);
// With a grid, you should use an asynchronous communication
peoAsyncIslandMig<Indi> mig(mig_cont,mig_select,mig_replace,topology,pop,pop2);
checkpoint.add(mig);
peoAsyncIslandMig<Indi> mig2(mig_cont2,mig_select2,mig_replace2,topology,pop2,pop);
checkpoint2.add(mig2);
// Initialization of the algorithms
peoEA<Indi> Algo(checkpoint,eval,select,eaTransform,replace);
mig.setOwner(Algo);
Algo(pop);
peoEA<Indi> Algo2(checkpoint2,eval2,select2,eaTransform2,replace2);
mig2.setOwner(Algo2);
Algo2(pop2);
peo :: run();
peo :: finalize();
if (getNodeRank()==1)
{
pop.sort();
pop2.sort();
std::cout << "Final population 1 :\n" << pop << std::endl;
std::cout << "Final population 2 :\n" << pop2 << std::endl;
}

View file

@ -49,37 +49,31 @@ double f (const Indi & _indi)
int main (int __argc, char *__argv[])
{
// In this lesson, we define two algorithms of the PSO witch represents two islands.
// Obviously, you can define more algorithms.
// The parameters are common between the two algorithms.
/*****************************************************************************************/
peo :: init( __argc, __argv );
const unsigned int VEC_SIZE = 2;
const unsigned int POP_SIZE = 20;
const unsigned int VEC_SIZE = 2;
const unsigned int POP_SIZE = 20;
const unsigned int NEIGHBORHOOD_SIZE= 6;
const unsigned int MAX_GEN = 150;
const unsigned int MAX_GEN = 100;
const double INIT_POSITION_MIN = -2.0;
const double INIT_POSITION_MAX = 2.0;
const double INIT_POSITION_MAX = 2.0;
const double INIT_VELOCITY_MIN = -1.;
const double INIT_VELOCITY_MAX = 1.;
const unsigned int MIG_FREQ = 10;
const double omega = 1;
const double C1 = 0.5;
const double C2 = 2.;
// C3 is used for the calculation of one of the strategies of the island model.
const double C3 = 2.;
// MIG_FREQ define the frequency of the migration.
const unsigned int MIG_FREQ = 10; // The optimal value is 1 or 2 for the component peoPSOVelocity.
const double C2 = 2.;
rng.reseed (time(0));
/*****************************************************************************************/
// Define the topology of your island model
// Island model
RingTopology topologyMig;
// First algorithm
/*****************************************************************************************/
// First
eoGenContinue < Indi > genContPara (MAX_GEN);
eoCombinedContinue <Indi> continuatorPara (genContPara);
eoCheckPoint<Indi> checkpoint(continuatorPara);
peoEvalFunc<Indi, double, const Indi& > plainEval(f);
peoSeqPopEval< Indi > eval(plainEval); // Here, the evaluation is sequential !
peoPopEval< Indi > eval(plainEval);
eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX);
eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
eoUniformGenerator < double >sGen (INIT_VELOCITY_MIN, INIT_VELOCITY_MAX);
@ -87,27 +81,33 @@ int main (int __argc, char *__argv[])
eoFirstIsBestInit < Indi > localInit;
eoRealVectorBounds bndsFlight(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX);
eoStandardFlight < Indi > flight(bndsFlight);
eoPop < Indi > pop;
peoPop < Indi > pop;
pop.append (POP_SIZE, random);
peoInitializer <Indi> init(eval,veloRandom,localInit,pop);
eoLinearTopology<Indi> topology(NEIGHBORHOOD_SIZE);
eoRealVectorBounds bnds(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX);
eoStandardVelocity < Indi > velocity (topology,C1,C2,bnds);
eoGenContinue < Indi > genContPara (MAX_GEN);
eoCheckPoint<Indi> checkpoint(genContPara);
// Specific implementation for the island model
eoStandardVelocity < Indi > velocity (topology,omega,C1,C2,bnds);
eoInitializer <Indi> init(eval,veloRandom,localInit,topology,pop);
// Island model
eoPeriodicContinue< Indi > mig_cont( MIG_FREQ );
peoPSOSelect<Indi> mig_selec(topology);
eoSelectNumber< Indi > mig_select(mig_selec);
// If you want to use a replacement stategy : peoPSOReplacement<Indi> mig_replace;
// If you want to use a consideration of the migration in the calculation of the velocity : peoPSOVelocity<Indi> mig_replace(C3,velocity);
peoPSOReplacement<Indi> mig_replace;
/*****************************************************************************************/
peoWorstPositionReplacement<Indi> mig_replac;
// Specific implementation (peoData.h)
eoContinuator<Indi> cont(mig_cont, pop);
eoSelector <Indi, peoPop<Indi> > mig_select (mig_selec,1,pop);
eoReplace <Indi, peoPop<Indi> > mig_replace (mig_replac,pop);
// Second algorithm (on the same model but with others names)
/*****************************************************************************************/
// Second
eoGenContinue < Indi > genContPara2 (MAX_GEN);
eoCombinedContinue <Indi> continuatorPara2 (genContPara2);
eoCheckPoint<Indi> checkpoint2(continuatorPara2);
peoEvalFunc<Indi, double, const Indi& > plainEval2(f);
peoSeqPopEval< Indi > eval2(plainEval2);
peoPopEval< Indi > eval2(plainEval2);
eoUniformGenerator < double >uGen2 (INIT_POSITION_MIN, INIT_POSITION_MAX);
eoInitFixedLength < Indi > random2 (VEC_SIZE, uGen2);
eoUniformGenerator < double >sGen2 (INIT_VELOCITY_MIN, INIT_VELOCITY_MAX);
@ -115,38 +115,51 @@ int main (int __argc, char *__argv[])
eoFirstIsBestInit < Indi > localInit2;
eoRealVectorBounds bndsFlight2(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX);
eoStandardFlight < Indi > flight2(bndsFlight2);
eoPop < Indi > pop2;
peoPop < Indi > pop2;
pop2.append (POP_SIZE, random2);
peoInitializer <Indi> init2(eval2,veloRandom2,localInit2,pop2);
eoLinearTopology<Indi> topology2(NEIGHBORHOOD_SIZE);
eoRealVectorBounds bnds2(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX);
eoStandardVelocity < Indi > velocity2 (topology2,C1,C2,bnds2);
eoGenContinue < Indi > genContPara2 (MAX_GEN);
eoCheckPoint<Indi> checkpoint2(genContPara2);
eoStandardVelocity < Indi > velocity2 (topology2,omega,C1,C2,bnds2);
eoInitializer <Indi> init2(eval2,veloRandom2,localInit2,topology2,pop2);
// Island model
eoPeriodicContinue< Indi > mig_cont2( MIG_FREQ );
peoPSOSelect<Indi> mig_selec2(topology2);
eoSelectNumber< Indi > mig_select2(mig_selec2);
peoPSOReplacement<Indi> mig_replace2;
/*****************************************************************************************/
peoWorstPositionReplacement<Indi> mig_replac2;
// Define the communication between the islands
peoAsyncIslandMig< Indi > mig( mig_cont, mig_select, mig_replace, topologyMig, pop, pop);
// Specific implementation (peoData.h)
eoContinuator<Indi> cont2(mig_cont2,pop2);
eoSelector <Indi, peoPop<Indi> > mig_select2 (mig_selec2,1,pop2);
eoReplace <Indi, peoPop<Indi> > mig_replace2 (mig_replac2,pop2);
// Island model
peoAsyncIslandMig< Indi, peoPop<Indi> > mig(cont,mig_select, mig_replace, topologyMig, pop, pop);
checkpoint.add( mig );
peoAsyncIslandMig< Indi > mig2( mig_cont2, mig_select2, mig_replace2, topologyMig, pop2, pop2);
peoAsyncIslandMig< Indi, peoPop<Indi> > mig2(cont2,mig_select2, mig_replace2, topologyMig, pop2, pop2);
checkpoint2.add( mig2 );
// Initialization of the algorithms
peoPSO < Indi > psa(init,checkpoint, eval, velocity, flight);
mig.setOwner( psa );
psa(pop);
peoPSO < Indi > psa2(init2,checkpoint2, eval2, velocity2, flight2);
mig2.setOwner( psa2 );
psa2(pop2);
// Parallel algorithm
eoSyncEasyPSO <Indi> psa(init,checkpoint,eval, velocity, flight);
peoWrapper parallelPSO( psa, pop);
eval.setOwner(parallelPSO);
mig.setOwner(parallelPSO);
eoSyncEasyPSO <Indi> psa2(init2,checkpoint2,eval2, velocity2, flight2);
peoWrapper parallelPSO2( psa2, pop2);
eval2.setOwner(parallelPSO2);
mig2.setOwner(parallelPSO2);
peo :: run();
peo :: finalize();
if (getNodeRank()==1)
{
std::cout << "Population 1 :\n" << pop << std::endl;
std::cout << "Population 2 :\n" << pop2 << std::endl;
}
{
pop.sort();
pop2.sort();
std::cout << "Final population :\n" << pop << std::endl;
std::cout << "Final population :\n" << pop2 << std::endl;
}
}