diff --git a/trunk/paradiseo-peo/tutorial/CMakeLists.txt b/trunk/paradiseo-peo/tutorial/CMakeLists.txt index 4e492ccf2..75232bcca 100644 --- a/trunk/paradiseo-peo/tutorial/CMakeLists.txt +++ b/trunk/paradiseo-peo/tutorial/CMakeLists.txt @@ -11,6 +11,6 @@ SET(TSP_BINARY_DIR ${ParadisEO-PEO_BINARY_DIR}/tutorial/examples/tsp) ### 2) Where must cmake go now ? ###################################################################################### -SUBDIRS(examples Lesson1 Lesson2 Lesson3 Lesson4 Lesson5 Lesson6) +SUBDIRS(examples Lesson1 Lesson2 Lesson3 Lesson4 Lesson5 Lesson6 Wrapper) ###################################################################################### diff --git a/trunk/paradiseo-peo/tutorial/Wrapper/CMakeLists.txt b/trunk/paradiseo-peo/tutorial/Wrapper/CMakeLists.txt new file mode 100644 index 000000000..48214db22 --- /dev/null +++ b/trunk/paradiseo-peo/tutorial/Wrapper/CMakeLists.txt @@ -0,0 +1,92 @@ + +###################################################################################### +### 0) Set the compiler and define targets to easily run the lessons +###################################################################################### + +SET (CMAKE_CXX_COMPILER mpicxx) + +ADD_CUSTOM_TARGET(install DEPENDS ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Wrapper/param ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Wrapper/schema.xml) + +ADD_CUSTOM_COMMAND( + TARGET install + POST_BUILD + COMMAND ${CMAKE_COMMAND} + ARGS -E copy_if_different + ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Wrapper/param + ${ParadisEO-PEO_BINARY_DIR}/tutorial/Wrapper) +ADD_CUSTOM_COMMAND( + TARGET install + POST_BUILD + COMMAND ${CMAKE_COMMAND} + ARGS -E copy_if_different + ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Wrapper/schema.xml + ${ParadisEO-PEO_BINARY_DIR}/tutorial/Wrapper) + +###################################################################################### + + +###################################################################################### +### 1) Include the sources +###################################################################################### + +INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src ${MO_SRC_DIR}/src ${ParadisEO-PEO_SOURCE_DIR}/src ${TSP_SRC_DIR}) + +###################################################################################### + + +###################################################################################### +### 2) Specify where CMake can find the libraries (mandatory: before 3) ) +###################################################################################### + +IF(NOT WIN32 OR CYGWIN) + LINK_DIRECTORIES(${EO_BIN_DIR}/lib ${ParadisEO-PEO_BINARY_DIR}/lib ${TSP_BINARY_DIR}/lib) +ENDIF(NOT WIN32 OR CYGWIN) + +# especially for Visual Studio +IF(WIN32 AND NOT CYGWIN) + LINK_DIRECTORIES(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE} ${ParadisEO-PEO_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE} ${TSP_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE}) +ENDIF(WIN32 AND NOT CYGWIN) + +###################################################################################### + + +###################################################################################### +### 3) Define your target(s): just an executable here +###################################################################################### + +ADD_EXECUTABLE(example1 main1.cpp) +ADD_DEPENDENCIES(example1 peo rmc_mpi) +ADD_EXECUTABLE(example2 main2.cpp) +ADD_DEPENDENCIES(example2 peo rmc_mpi) +ADD_EXECUTABLE(example3 main3.cpp) +ADD_DEPENDENCIES(example3 peo rmc_mpi) +ADD_EXECUTABLE(example4 main4.cpp) +ADD_DEPENDENCIES(example4 peo rmc_mpi) +ADD_EXECUTABLE(example5 main5.cpp) +ADD_DEPENDENCIES(example5 tsp peo rmc_mpi) +ADD_EXECUTABLE(example6 main6.cpp) +ADD_DEPENDENCIES(example6 tsp peo rmc_mpi) + +###################################################################################### + + +###################################################################################### +### 4) Optionnal: define your target(s)'s version: no effect for windows +###################################################################################### + + +###################################################################################### + + +###################################################################################### +### 5) Link the librairies +###################################################################################### + +TARGET_LINK_LIBRARIES(example1 ${XML2_LIBS} peo rmc_mpi eo eoutils) +TARGET_LINK_LIBRARIES(example2 ${XML2_LIBS} peo rmc_mpi eo eoutils) +TARGET_LINK_LIBRARIES(example3 ${XML2_LIBS} peo rmc_mpi eo eoutils) +TARGET_LINK_LIBRARIES(example4 ${XML2_LIBS} peo rmc_mpi eo eoutils) +TARGET_LINK_LIBRARIES(example5 ${XML2_LIBS} tsp peo rmc_mpi eo eoutils) +TARGET_LINK_LIBRARIES(example6 ${XML2_LIBS} tsp peo rmc_mpi eo eoutils) + +###################################################################################### diff --git a/trunk/paradiseo-peo/tutorial/Wrapper/main1.cpp b/trunk/paradiseo-peo/tutorial/Wrapper/main1.cpp new file mode 100644 index 000000000..926930f44 --- /dev/null +++ b/trunk/paradiseo-peo/tutorial/Wrapper/main1.cpp @@ -0,0 +1,91 @@ +#include +#include +typedef eoReal Indi; + +double f (const Indi & _indi) +{ + double sum; + sum=_indi[1]-pow(_indi[0],2); + sum=100*pow(sum,2); + sum+=pow((1-_indi[0]),2); + return (-sum); +} + +int main( int __argc, char** __argv ) +{ + + + + peo :: init( __argc, __argv ); + +// Parameters + const unsigned int VEC_SIZE = 2; + const unsigned int POP_SIZE = 20; + const unsigned int MAX_GEN = 300; + const double INIT_POSITION_MIN = -2.0; + const double INIT_POSITION_MAX = 2.0; + const float CROSS_RATE = 0.8; + const double EPSILON = 0.01; + const float MUT_RATE = 0.3; + rng.reseed (time(0)); + + +// Algorithm + eoGenContinue < Indi > genContPara (MAX_GEN); + eoCombinedContinue continuatorPara (genContPara); + eoCheckPoint checkpoint(continuatorPara); + eoEvalFuncPtr< Indi > mainEval( f ); + eoEvalFuncCounter< Indi > eval(mainEval); + eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX); + eoInitFixedLength < Indi > random (VEC_SIZE, uGen); + eoRankingSelect selectionStrategy; + eoSelectNumber select(selectionStrategy,POP_SIZE); + eoSegmentCrossover crossover; + eoUniformMutation mutation(EPSILON); + eoSGATransform transform(crossover,CROSS_RATE,mutation,MUT_RATE); + peoSeqTransform eaTransform(transform); + eoPlusReplacement replace; + eoEasyEA< Indi > eaAlg( checkpoint, eval, select, transform, replace ); + +// Population + eoPop < Indi > pop; + pop.append (POP_SIZE, random); + +// Wrapper + peoParallelAlgorithmWrapper parallelEA( eaAlg, pop); + peo :: run(); + peo :: finalize(); + if (getNodeRank()==1) + std::cout << "Final population :\n" << pop << std::endl; + + +// Algorithm + eoGenContinue < Indi > genContPara2 (MAX_GEN); + eoCombinedContinue continuatorPara2 (genContPara2); + eoCheckPoint checkpoint2(continuatorPara2); + eoEvalFuncPtr< Indi > mainEval2( f ); + eoEvalFuncCounter< Indi > eval2(mainEval2); + eoUniformGenerator < double >uGen2 (INIT_POSITION_MIN, INIT_POSITION_MAX); + eoInitFixedLength < Indi > random2 (VEC_SIZE, uGen2); + eoRankingSelect selectionStrategy2; + eoSelectNumber select2(selectionStrategy2,POP_SIZE); + eoSegmentCrossover crossover2; + eoUniformMutation mutation2(EPSILON); + eoSGATransform transform2(crossover2,CROSS_RATE,mutation2,MUT_RATE); + peoSeqTransform eaTransform2(transform2); + eoPlusReplacement replace2; + eoEasyEA< Indi > eaAlg2( checkpoint2, eval2, select2, transform2, replace2 ); + +// Population + eoPop < Indi > pop2; + pop2.append (POP_SIZE, random2); + + +// Wrapper + peo :: init( __argc, __argv ); + peoParallelAlgorithmWrapper parallelEA2( eaAlg2, pop2); + peo :: run(); + peo :: finalize(); + if (getNodeRank()==1) + std::cout << "Final population 2 :\n" << pop2 << std::endl; +} diff --git a/trunk/paradiseo-peo/tutorial/Wrapper/main2.cpp b/trunk/paradiseo-peo/tutorial/Wrapper/main2.cpp new file mode 100644 index 000000000..778e6e382 --- /dev/null +++ b/trunk/paradiseo-peo/tutorial/Wrapper/main2.cpp @@ -0,0 +1,66 @@ +#include +#include +typedef eoReal Indi; + +double f (const Indi & _indi) +{ + double sum; + sum=_indi[1]-pow(_indi[0],2); + sum=100*pow(sum,2); + sum+=pow((1-_indi[0]),2); + return (-sum); +} + +int main( int __argc, char** __argv ) +{ + peo :: init( __argc, __argv ); + +// Parameters + const unsigned int VEC_SIZE = 2; + const unsigned int POP_SIZE = 20; + const unsigned int MAX_GEN = 300; + const double INIT_POSITION_MIN = -2.0; + const double INIT_POSITION_MAX = 2.0; + const float CROSS_RATE = 0.8; + const double EPSILON = 0.01; + const float MUT_RATE = 0.5; + rng.reseed (time(0)); + +// Algorithm + eoGenContinue < Indi > genContPara (MAX_GEN); + eoCombinedContinue continuatorPara (genContPara); + eoCheckPoint checkpoint(continuatorPara); + + //eoEvalFuncPtr< Indi > mainEval( f ); + //eoEvalFuncCounter< Indi > eval(mainEval); + peoEvalFunc mainEval( f ); + peoParaPopEval eval(mainEval); + + eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX); + eoInitFixedLength < Indi > random (VEC_SIZE, uGen); + eoRankingSelect selectionStrategy; + eoSelectNumber select(selectionStrategy,POP_SIZE); + eoSegmentCrossover crossover; + eoUniformMutation mutation(EPSILON); + + //eoSGATransform transform(crossover,CROSS_RATE,mutation,MUT_RATE); + //peoSeqTransform eaTransform(transform); + peoParaSGATransform transform(crossover,CROSS_RATE,mutation,MUT_RATE); + + eoPlusReplacement replace; + eoEasyEA< Indi > eaAlg( checkpoint, eval, select, transform, replace ); + +// Population*/ + eoPop < Indi > pop; + pop.append (POP_SIZE, random); + peoParallelAlgorithmWrapper parallelEA( eaAlg, pop); + eval.setOwner(parallelEA); + transform.setOwner(parallelEA); + peo :: run(); + peo :: finalize(); + + + + if (getNodeRank()==1) + std::cout << "Final population :\n" << pop << std::endl; +} diff --git a/trunk/paradiseo-peo/tutorial/Wrapper/main3.cpp b/trunk/paradiseo-peo/tutorial/Wrapper/main3.cpp new file mode 100644 index 000000000..b142ec7e7 --- /dev/null +++ b/trunk/paradiseo-peo/tutorial/Wrapper/main3.cpp @@ -0,0 +1,60 @@ +#include +#include + +typedef eoRealParticle < double >Indi; + +double f (const Indi & _indi) +{ + double sum; + sum=_indi[1]-pow(_indi[0],2); + sum=100*pow(sum,2); + sum+=pow((1-_indi[0]),2); + return (-sum); +} + +int main( int __argc, char** __argv ) +{ + + peo :: init( __argc, __argv ); + const unsigned int VEC_SIZE = 2; + const unsigned int POP_SIZE = 20; + const unsigned int NEIGHBORHOOD_SIZE= 6; + const unsigned int MAX_GEN = 300; + const double INIT_POSITION_MIN = -2.0; + const double INIT_POSITION_MAX = 2.0; + const double INIT_VELOCITY_MIN = -1.; + const double INIT_VELOCITY_MAX = 1.; + const double C1 = 0.5; + const double C2 = 2.; + rng.reseed (time(0)); + eoGenContinue < Indi > genContPara (MAX_GEN); + eoCombinedContinue continuatorPara (genContPara); + eoCheckPoint checkpoint(continuatorPara); + + peoEvalFunc plainEval(f); + peoParaPopEval< 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); + eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen); + eoFirstIsBestInit < Indi > localInit; + eoRealVectorBounds bndsFlight(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX); + eoStandardFlight < Indi > flight(bndsFlight); + eoPop < Indi > pop; + pop.append (POP_SIZE, random); + eoLinearTopology topology(NEIGHBORHOOD_SIZE); + eoRealVectorBounds bnds(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX); + eoStandardVelocity < Indi > velocity (topology,C1,C2,bnds); + eoInitializer init(eval,veloRandom,localInit,topology,pop); + +// Parallel algorithm + + eoSyncEasyPSO psa(init,checkpoint,eval, velocity, flight); + peoParallelAlgorithmWrapper parallelPSO( psa, pop); + eval.setOwner(parallelPSO); + peo :: run(); + peo :: finalize(); + if (getNodeRank()==1) + std::cout << "Final population :\n" << pop << std::endl; +} diff --git a/trunk/paradiseo-peo/tutorial/Wrapper/main4.cpp b/trunk/paradiseo-peo/tutorial/Wrapper/main4.cpp new file mode 100644 index 000000000..296f0163c --- /dev/null +++ b/trunk/paradiseo-peo/tutorial/Wrapper/main4.cpp @@ -0,0 +1,119 @@ +#include +#include + +typedef eoRealParticle < double >Indi; + +double f (const Indi & _indi) +{ + double sum; + sum=_indi[1]-pow(_indi[0],2); + sum=100*pow(sum,2); + sum+=pow((1-_indi[0]),2); + return (-sum); +} + +int main( int __argc, char** __argv ) +{ + + peo :: init( __argc, __argv ); + const unsigned int VEC_SIZE = 2; + const unsigned int POP_SIZE = 20; + const unsigned int NEIGHBORHOOD_SIZE= 6; + const unsigned int MAX_GEN = 100; + const double INIT_POSITION_MIN = -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 C1 = 0.5; + const double C2 = 2.; + rng.reseed (time(0)); + +// Island model + + RingTopology topologyMig; + +// First + eoGenContinue < Indi > genContPara (MAX_GEN); + eoCombinedContinue continuatorPara (genContPara); + eoCheckPoint checkpoint(continuatorPara); + peoEvalFunc plainEval(f); + peoParaPopEval< 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); + eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen); + eoFirstIsBestInit < Indi > localInit; + eoRealVectorBounds bndsFlight(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX); + eoStandardFlight < Indi > flight(bndsFlight); + eoPop < Indi > pop; + pop.append (POP_SIZE, random); + eoLinearTopology topology(NEIGHBORHOOD_SIZE); + eoRealVectorBounds bnds(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX); + eoStandardVelocity < Indi > velocity (topology,C1,C2,bnds); + eoInitializer init(eval,veloRandom,localInit,topology,pop); + +// Island model + + eoPeriodicContinue< Indi > mig_cont( MIG_FREQ ); + peoPSOSelect mig_selec(topology); + eoSelectNumber< Indi > mig_select(mig_selec); + peoPSOReplacement mig_replace; + + +// Second + + eoGenContinue < Indi > genContPara2 (MAX_GEN); + eoCombinedContinue continuatorPara2 (genContPara2); + eoCheckPoint checkpoint2(continuatorPara2); + peoEvalFunc plainEval2(f); + peoParaPopEval< 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); + eoVelocityInitFixedLength < Indi > veloRandom2 (VEC_SIZE, sGen2); + eoFirstIsBestInit < Indi > localInit2; + eoRealVectorBounds bndsFlight2(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX); + eoStandardFlight < Indi > flight2(bndsFlight2); + eoPop < Indi > pop2; + pop2.append (POP_SIZE, random2); + eoLinearTopology topology2(NEIGHBORHOOD_SIZE); + eoRealVectorBounds bnds2(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX); + eoStandardVelocity < Indi > velocity2 (topology2,C1,C2,bnds2); + eoInitializer init2(eval2,veloRandom2,localInit2,topology2,pop2); + +// Island model + + eoPeriodicContinue< Indi > mig_cont2( MIG_FREQ ); + peoPSOSelect mig_selec2(topology2); + eoSelectNumber< Indi > mig_select2(mig_selec2); + peoPSOReplacement mig_replace2; + + + +// Island model + + peoAsyncIslandMig< Indi > mig( mig_cont, mig_select, mig_replace, topologyMig, pop, pop); + checkpoint.add( mig ); + peoAsyncIslandMig< Indi > mig2( mig_cont2, mig_select2, mig_replace2, topologyMig, pop2, pop2); + checkpoint2.add( mig2 ); + + +// Parallel algorithm + + eoSyncEasyPSO psa(init,checkpoint,eval, velocity, flight); + peoParallelAlgorithmWrapper parallelPSO( psa, pop); + eval.setOwner(parallelPSO); + mig.setOwner(parallelPSO); + eoSyncEasyPSO psa2(init2,checkpoint2,eval2, velocity2, flight2); + peoParallelAlgorithmWrapper parallelPSO2( psa2, pop2); + eval2.setOwner(parallelPSO2); + mig2.setOwner(parallelPSO2); + peo :: run(); + peo :: finalize(); + if (getNodeRank()==1) + { + std::cout << "Final population :\n" << pop << std::endl; + std::cout << "Final population :\n" << pop2 << std::endl; + } +} diff --git a/trunk/paradiseo-peo/tutorial/Wrapper/main5.cpp b/trunk/paradiseo-peo/tutorial/Wrapper/main5.cpp new file mode 100644 index 000000000..b87f87e97 --- /dev/null +++ b/trunk/paradiseo-peo/tutorial/Wrapper/main5.cpp @@ -0,0 +1,77 @@ +#include "param.h" +#include "route_init.h" +#include "route_eval.h" +#include "order_xover.h" +#include "edge_xover.h" +#include "partial_mapped_xover.h" +#include "city_swap.h" +#include "part_route_eval.h" +#include "merge_route_eval.h" +#include "two_opt_init.h" +#include "two_opt_next.h" +#include "two_opt_incr_eval.h" + +#include + +#define POP_SIZE 10 +#define NUM_GEN 100 +#define CROSS_RATE 1.0 +#define MUT_RATE 0.01 + + +int main (int __argc, char * * __argv) +{ + + peo :: init (__argc, __argv); + loadParameters (__argc, __argv); + RouteInit route_init; + RouteEval full_eval; + OrderXover order_cross; + PartialMappedXover pm_cross; + EdgeXover edge_cross; + CitySwap city_swap_mut; + +// Initialization of the local search + TwoOptInit pmx_two_opt_init; + TwoOptNext pmx_two_opt_next; + TwoOptIncrEval pmx_two_opt_incr_eval; + moBestImprSelect pmx_two_opt_move_select; + moHC hc (pmx_two_opt_init, pmx_two_opt_next, pmx_two_opt_incr_eval, pmx_two_opt_move_select, full_eval); + +// EA + eoPop pop (POP_SIZE, route_init); + eoGenContinue cont (NUM_GEN); + eoCheckPoint checkpoint (cont); + peoSeqPopEval eval (full_eval); + eoStochTournamentSelect select_one; + eoSelectNumber select (select_one, POP_SIZE); + eoSGATransform transform (order_cross, CROSS_RATE, city_swap_mut, MUT_RATE); + peoSeqTransform para_transform (transform); + eoEPReplacement replace (2); + eoEasyEA< Route > eaAlg( checkpoint, eval, select, para_transform, replace ); + peoParallelAlgorithmWrapper parallelEA( eaAlg, pop); + peo :: run (); + peo :: finalize (); + + if (getNodeRank()==1) + { + std :: cout << "\nResult before the local search\n"; + for(unsigned i=0;i initParallelHC (hc); + peoParallelAlgorithmWrapper parallelHC (initParallelHC, pop); + initParallelHC.setOwner(parallelHC); + peo :: run( ); + peo :: finalize( ); + + if (getNodeRank()==1) + { + std :: cout << "\nResult after the local search\n"; + for(unsigned i=0;i + +#define POP_SIZE 10 +#define NUM_GEN 200 +#define CROSS_RATE 1.0 +#define MUT_RATE 0.1 + + +int main (int __argc, char * * __argv) +{ + + peo :: init (__argc, __argv); + loadParameters (__argc, __argv); + RouteInit route_init; + RouteEval full_eval; + OrderXover order_cross; + PartialMappedXover pm_cross; + EdgeXover edge_cross; + CitySwap city_swap_mut; + +// Local search + TwoOptInit pmx_two_opt_init; + TwoOptNext pmx_two_opt_next; + TwoOptIncrEval pmx_two_opt_incr_eval; + moBestImprSelect pmx_two_opt_move_select; + moHC hc (pmx_two_opt_init, pmx_two_opt_next, pmx_two_opt_incr_eval, pmx_two_opt_move_select, full_eval); + eoPop pop (POP_SIZE, route_init); + peoSynchronousMultiStart initParallelHC (hc); + peoParallelAlgorithmWrapper parallelHC (initParallelHC, pop); + initParallelHC.setOwner(parallelHC); + peo :: run( ); + peo :: finalize( ); + + if (getNodeRank()==1) + { + std :: cout << "\nResult before the EA\n"; + for(unsigned i=0;i cont (NUM_GEN); + eoCheckPoint checkpoint (cont); + peoSeqPopEval eval (full_eval); + eoStochTournamentSelect select_one; + eoSelectNumber select (select_one, POP_SIZE); + eoSGATransform transform (order_cross, CROSS_RATE, city_swap_mut, MUT_RATE); + peoSeqTransform para_transform (transform); + eoEPReplacement replace (2); + eoEasyEA< Route > eaAlg( checkpoint, eval, select, para_transform, replace ); + peoParallelAlgorithmWrapper parallelEA( eaAlg, pop); + peo :: run (); + peo :: finalize (); + + if (getNodeRank()==1) + { + std :: cout << "\nResult after the EA\n"; + for(unsigned i=0;i + + + + + + + 1 + 2 + + + + + + +