From 4ad79a91487d784dda89198bf8b03b4ced59c705 Mon Sep 17 00:00:00 2001 From: tlegrand Date: Tue, 4 Mar 2008 14:01:29 +0000 Subject: [PATCH] Added new lesson (6) dedicated to the PSO. Also changed a few things into the PSO-dedicated components (constructors) --- eo/src/PO.h | 2 +- eo/src/eo | 1 - eo/src/eoDummyFlight.h | 51 -------- eo/src/eoEasyPSO.h | 68 +++++++++-- eo/src/eoInitializer.h | 7 +- eo/src/eoSyncEasyPSO.h | 108 +++++++++++++--- eo/test/CMakeLists.txt | 1 + eo/test/ChangeLog | 15 ++- eo/test/Makefile.am | 5 +- eo/test/t-eoEasyPSO.cpp | 47 ++++--- eo/test/t-eoSyncEasyPSO.cpp | 104 ++++++++++++++++ eo/tutorial/CMakeLists.txt | 2 +- eo/tutorial/Lesson6/BinaryPSO.cpp | 182 +++++++++++++++++++++++++++ eo/tutorial/Lesson6/CMakeLists.txt | 58 +++++++++ eo/tutorial/Lesson6/Makefile.am | 19 +++ eo/tutorial/Lesson6/Makefile.simple | 31 +++++ eo/tutorial/Lesson6/RealPSO.cpp | 183 ++++++++++++++++++++++++++++ 17 files changed, 770 insertions(+), 114 deletions(-) delete mode 100644 eo/src/eoDummyFlight.h create mode 100644 eo/test/t-eoSyncEasyPSO.cpp create mode 100644 eo/tutorial/Lesson6/BinaryPSO.cpp create mode 100644 eo/tutorial/Lesson6/CMakeLists.txt create mode 100644 eo/tutorial/Lesson6/Makefile.am create mode 100644 eo/tutorial/Lesson6/Makefile.simple create mode 100644 eo/tutorial/Lesson6/RealPSO.cpp diff --git a/eo/src/PO.h b/eo/src/PO.h index 1c886ef6..2cac3d86 100644 --- a/eo/src/PO.h +++ b/eo/src/PO.h @@ -140,7 +140,7 @@ public: * Write object. Called printOn since it prints the object _on_ a stream. * @param _os A std::ostream. */ - virtual void printOn(std::ostream& _os) const { _os << bestFitness <<' ' ;} + virtual void printOn(std::ostream& _os) const { _os << bestFitness << ' ' ;} /** diff --git a/eo/src/eo b/eo/src/eo index b7dbe157..4ed64f9b 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -172,7 +172,6 @@ #include #include #include -#include #include #include diff --git a/eo/src/eoDummyFlight.h b/eo/src/eoDummyFlight.h deleted file mode 100644 index a1940315..00000000 --- a/eo/src/eoDummyFlight.h +++ /dev/null @@ -1,51 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoDummyFlight.h -// (c) OPAC 2007 -/* - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - Contact: thomas.legrand@lifl.fr - */ -//----------------------------------------------------------------------------- - -#ifndef EODUMMYFLIGHT_H -#define EODUMMYFLIGHT_H - -//----------------------------------------------------------------------------- -#include -//----------------------------------------------------------------------------- - - -/** -* A dummy flight that does nothing ! -*/ -template < class POT > class eoDummyFlight:public eoFlight < POT > -{ - -public: - - // Ctor - eoDummyFlight () {} - - /** - * Dummy move: do nothing - */ - void operator () (POT & _po1) {} -}; - - -#endif /*EODUMMYFLIGHT_H */ diff --git a/eo/src/eoEasyPSO.h b/eo/src/eoEasyPSO.h index a6805491..a73f5d17 100644 --- a/eo/src/eoEasyPSO.h +++ b/eo/src/eoEasyPSO.h @@ -30,7 +30,6 @@ #include #include #include -#include //----------------------------------------------------------------------------- /** An easy-to-use particle swarm algorithm; you can use any particle, @@ -47,7 +46,7 @@ template < class POT > class eoEasyPSO:public eoPSO < POT > public: /** Full constructor - * @param _init - An eoInitializer that initializes the topology, velocity, best particle(s) + * @param _init - An eoInitializerBase that initializes the topology, velocity, best particle(s) * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system * @param _eval - An eoEvalFunc: the evaluation performer * @param _velocity - An eoVelocity that defines how to compute the velocities @@ -55,7 +54,7 @@ public: * to modify the positions according to the velocities */ eoEasyPSO ( - eoInitializer &_init, + eoInitializerBase &_init, eoContinue < POT > &_continuator, eoEvalFunc < POT > &_eval, eoVelocity < POT > &_velocity, @@ -69,13 +68,13 @@ public: /** Constructor without eoFlight. For special cases when the flight is performed withing the velocity. - * @param _init - An eoInitializer that initializes the topology, velocity, best particle(s) + * @param _init - An eoInitializerBase that initializes the topology, velocity, best particle(s) * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system * @param _eval - An eoEvalFunc: the evaluation performer * @param _velocity - An eoVelocity that defines how to compute the velocities */ eoEasyPSO ( - eoInitializer &_init, + eoInitializerBase &_init, eoContinue < POT > &_continuator, eoEvalFunc < POT > &_eval, eoVelocity < POT > &_velocity): @@ -86,6 +85,43 @@ public: flight (dummyFlight) {} + + /* Constructor without eoInitializerBase. Assume the initialization is done before running the algorithm + * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system + * @param _eval - An eoEvalFunc: the evaluation performer + * @param _velocity - An eoVelocity that defines how to compute the velocities + * @param _flight - An eoFlight that defines how to make the particle flying: that means how + * to modify the positions according to the velocities + */ + eoEasyPSO ( + eoContinue < POT > &_continuator, + eoEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity, + eoFlight < POT > &_flight): + init(dummyInit), + continuator (_continuator), + eval (_eval), + velocity (_velocity), + flight (_flight) + {} + + + /** Constructor without eoFlight nor eoInitializerBase. For special cases when the flight is performed withing the velocity. + * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system + * @param _eval - An eoEvalFunc: the evaluation performer + * @param _velocity - An eoVelocity that defines how to compute the velocities + */ + eoEasyPSO ( + eoContinue < POT > &_continuator, + eoEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity): + init(dummyInit), + continuator (_continuator), + eval (_eval), + velocity (_velocity), + flight (dummyFlight) + {} + /// Apply a few iteration of flight to the population (=swarm). virtual void operator () (eoPop < POT > &_pop) { @@ -124,15 +160,29 @@ public: } -private: - eoInitializer &init; +protected: + eoInitializerBase &init; eoContinue < POT > &continuator; eoEvalFunc < POT > &eval; eoVelocity < POT > &velocity; eoFlight < POT > &flight; - // if the flight does not need to be used, use the dummy flight instance - eoDummyFlight dummyFlight; + // if the flight does not need to be used, use the dummy flight instance + class eoDummyFlight:public eoFlight < POT > + { + public: + eoDummyFlight () {} + void operator () (POT & _po) {} + }dummyFlight; + + // if the initializer does not need to be used, use the dummy one instead + class eoDummyInitializer:public eoInitializerBase < POT > + { + public: + eoDummyInitializer () {} + void operator () (POT & _po) {} + }dummyInit; + }; diff --git a/eo/src/eoInitializer.h b/eo/src/eoInitializer.h index 70315536..3900ffe4 100644 --- a/eo/src/eoInitializer.h +++ b/eo/src/eoInitializer.h @@ -111,13 +111,14 @@ private : @param proc First evaluation @param initVelo Initialization of the velocity @param initBest Initialization of the best - */ - eoPop < POT > & pop; + */ eoUF& proc; - eoPopEvalFunc & procPara; eoVelocityInit < POT > & initVelo; eoParticleBestInit & initBest; + eoPopEvalFunc & procPara; eoTopology & topology; + eoPop < POT > & pop; + class eoDummyEval : public eoPopEvalFunc { public: diff --git a/eo/src/eoSyncEasyPSO.h b/eo/src/eoSyncEasyPSO.h index ef2338b3..6c4a91f7 100644 --- a/eo/src/eoSyncEasyPSO.h +++ b/eo/src/eoSyncEasyPSO.h @@ -31,7 +31,6 @@ #include #include #include -#include //----------------------------------------------------------------------------- /** An easy-to-use synchronous particle swarm algorithm; you can use any particle, @@ -48,7 +47,7 @@ template < class POT > class eoSyncEasyPSO:public eoPSO < POT > public: /** Full constructor - * @param _init - An eoInitializer that initializes the topology, velocity, best particle(s) + * @param _init - An eoInitializerBase that initializes the topology, velocity, best particle(s) * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system * @param _eval - An eoEvalFunc: the evaluation performer * @param _velocity - An eoVelocity that defines how to compute the velocities @@ -56,7 +55,7 @@ public: * to modify the positions according to the velocities */ eoSyncEasyPSO ( - eoInitializer &_init, + eoInitializerBase &_init, eoContinue < POT > &_continuator, eoEvalFunc < POT > &_eval, eoVelocity < POT > &_velocity, @@ -72,13 +71,13 @@ public: /** Constructor without eoFlight. For special cases when the flight is performed withing the velocity. - * @param _init - An eoInitializer that initializes the topology, velocity, best particle(s) + * @param _init - An eoInitializerBase that initializes the topology, velocity, best particle(s) * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system * @param _eval - An eoEvalFunc: the evaluation performer * @param _velocity - An eoVelocity that defines how to compute the velocities */ eoSyncEasyPSO ( - eoInitializer &_init, + eoInitializerBase &_init, eoContinue < POT > &_continuator, eoEvalFunc < POT > &_eval, eoVelocity < POT > &_velocity): @@ -92,14 +91,14 @@ public: {} /** Full constructor - Can be used in parallel - * @param _init - An eoInitializer that initializes the topology, velocity, best particle(s) + * @param _init - An eoInitializerBase that initializes the topology, velocity, best particle(s) * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system * @param _eval - An eoPopEvalFunc * @param _velocity - An eoVelocity that defines how to compute the velocities * @param _flight - An eoFlight */ eoSyncEasyPSO ( - eoInitializer &_init, + eoInitializerBase &_init, eoContinue < POT > &_continuator, eoPopEvalFunc < POT > &_eval, eoVelocity < POT > &_velocity, @@ -114,6 +113,66 @@ public: {} + /** Another constructor without initializer + * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system + * @param _eval - An eoEvalFunc: the evaluation performer + * @param _velocity - An eoVelocity that defines how to compute the velocities + * @param _flight - An eoFlight that defines how to make the particle flying: that means how + * to modify the positions according to the velocities + */ + eoSyncEasyPSO ( + eoContinue < POT > &_continuator, + eoEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity, + eoFlight < POT > &_flight): + init(dummyInit), + continuator (_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + velocity (_velocity), + flight (_flight) + {} + + + /** Constructor without eoFlight nor eoInitializer. For special cases when the flight is performed withing the velocity. + * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system + * @param _eval - An eoEvalFunc: the evaluation performer + * @param _velocity - An eoVelocity that defines how to compute the velocities + */ + eoSyncEasyPSO ( + eoContinue < POT > &_continuator, + eoEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity): + init(dummyInit), + continuator (_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + velocity (_velocity), + flight (dummyFlight) + {} + + /** Full constructor - Can be used in parallel + * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system + * @param _eval - An eoPopEvalFunc + * @param _velocity - An eoVelocity that defines how to compute the velocities + * @param _flight - An eoFlight + */ + eoSyncEasyPSO ( + eoContinue < POT > &_continuator, + eoPopEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity, + eoFlight &_flight): + init(dummyInit), + continuator (_continuator), + eval (dummyEval), + loopEval(dummyEval), + popEval(_eval), + velocity (_velocity), + flight (_flight) + {} + /// Apply a few iteration of flight to the population (=swarm). virtual void operator () (eoPop < POT > &_pop) { @@ -122,6 +181,7 @@ public: { // initializes the topology, velocity, best particle(s) init(); + // just to use a loop eval eoPop empty_pop; @@ -154,7 +214,7 @@ public: private: - eoInitializer &init; + eoInitializerBase &init; eoContinue < POT > &continuator; eoEvalFunc < POT > &eval; @@ -164,18 +224,30 @@ private: eoVelocity < POT > &velocity; eoFlight < POT > &flight; - // if the flight does not need to be used, use the dummy flight instance - eoDummyFlight dummyFlight; - // if the eval does not need to be used, use the dummy eval instance -class eoDummyEval : public eoEvalFunc - { - public: - void operator()(POT &) - {} - } - dummyEval; + class eoDummyEval : public eoEvalFunc + { + public: + void operator()(POT &) + {} + } + dummyEval; + class eoDummyFlight:public eoFlight < POT > + { + public: + eoDummyFlight () {} + void operator () (POT & _po) {} + }dummyFlight; + + // if the initializer does not need to be used, use the dummy one instead + class eoDummyInitializer:public eoInitializerBase < POT > + { + public: + eoDummyInitializer () {} + void operator () (POT & _po) {} + }dummyInit; + }; diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index 334c79ae..886a1434 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -76,6 +76,7 @@ SET (TEST_LIST t-eoParetoFitness t-eoShiftMutation t-eoTwoOptMutation t-eoRingTopology + t-eoSyncEasyPSO # t-eoFrontSorter # t-eoEpsMOEA ) diff --git a/eo/test/ChangeLog b/eo/test/ChangeLog index 0c1377b3..0167b10a 100644 --- a/eo/test/ChangeLog +++ b/eo/test/ChangeLog @@ -16,14 +16,21 @@ avoid compiler warnings. -2006-07-02 Thomas Legrand +2006-07-02 Thomas Legrand - * test/t-eoEasyPSO.cpp: add PSO test + * test/t-eoEasyPSO.cpp: added PSO test - * test/Makefile.am: add PSO test + * test/Makefile.am: added PSO test + + +2006-02-27 Thomas Legrand + + * test/t-eoSyncEasyPSO.cpp: added synchronous PSO test + * test/t-eoEasyPSO.cpp: customized PSO test (initialization) + * test/Makefile.am: added synchronous PSO test * Local Variables: * coding: iso-8859-1 * mode: flyspell * fill-column: 80 - * End: + * End: \ No newline at end of file diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 1099b2f9..80b87bde 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -50,7 +50,8 @@ check_PROGRAMS = t-eoParetoFitness \ t-eoSwapMutation \ t-eoShiftMutation \ t-eoTwoOptMutation \ - t-eoRingTopology + t-eoRingTopology \ + t-eoSyncEasyPSO TESTS = $(check_PROGRAMS) \ run_tests # This script can be used to check command-line arguments @@ -114,4 +115,4 @@ t_eoSwapMutation_SOURCES = t-eoSwapMutation.cpp t_eoShiftMutation_SOURCES = t-eoShiftMutation.cpp t_eoTwoOptMutation_SOURCES = t-eoTwoOptMutation.cpp t_eoRingTopology_SOURCES = t-eoRingTopology.cpp - +t_eoSyncEasyPSO_SOURCES = t-eoSyncEasyPSO.cpp diff --git a/eo/test/t-eoEasyPSO.cpp b/eo/test/t-eoEasyPSO.cpp index 31c9abcd..16890093 100644 --- a/eo/test/t-eoEasyPSO.cpp +++ b/eo/test/t-eoEasyPSO.cpp @@ -48,23 +48,17 @@ int main() // local best init eoFirstIsBestInit < Particle > localInit; - - // perform initialization + // perform position initialization pop.append (POP_SIZE, random); - apply < Particle > (eval, pop); - apply < Particle > (veloRandom, pop); - apply < Particle > (localInit, pop); - - - std::cout << "population:" << std::endl; - for (i = 0; i < pop.size(); ++i) - std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; - - + // topology eoLinearTopology topology(NEIGHBORHOOD_SIZE); - topology.setup(pop); + // the full initializer + eoInitializer init(eval,veloRandom,localInit,topology,pop); + init(); + + // bounds eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5); @@ -75,19 +69,26 @@ int main() eoStandardFlight flight; // Terminators - eoGenContinue genCont (50); - - // the full initializer - eoInitializer init(eval,veloRandom,localInit,topology,pop); - + eoGenContinue genCont1 (50); + eoGenContinue genCont2 (50); + // PS flight - eoEasyPSO pso(init,genCont, eval, velocity, flight); - + eoEasyPSO pso1(genCont1, eval, velocity, flight); + eoEasyPSO pso2(init,genCont2, eval, velocity, flight); + // flight try { - pso(pop); + pso1(pop); + std::cout << "FINAL POPULATION AFTER PSO n°1:" << std::endl; + for (i = 0; i < pop.size(); ++i) + std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; + + pso2(pop); + std::cout << "FINAL POPULATION AFTER PSO n°2:" << std::endl; + for (i = 0; i < pop.size(); ++i) + std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; } catch (std::exception& e) { @@ -95,9 +96,7 @@ int main() exit(EXIT_FAILURE); } - std::cout << "pop" << std::endl; - for (i = 0; i < pop.size(); ++i) - std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; + return 0; } diff --git a/eo/test/t-eoSyncEasyPSO.cpp b/eo/test/t-eoSyncEasyPSO.cpp new file mode 100644 index 00000000..f9acba24 --- /dev/null +++ b/eo/test/t-eoSyncEasyPSO.cpp @@ -0,0 +1,104 @@ +//----------------------------------------------------------------------------- +// t-eoEasySyncPSO.cpp +//----------------------------------------------------------------------------- + +#ifndef __GNUG__ +// to avoid long name warnings +#pragma warning(disable:4786) +#endif // __GNUG__ + +#include + +//----------------------------------------------------------------------------- +typedef eoMinimizingFitness FitT; +typedef eoRealParticle < FitT > Particle; +//----------------------------------------------------------------------------- + +// the objective function +double real_value (const Particle & _particle) +{ + double sum = 0; + for (unsigned i = 0; i < _particle.size ()-1; i++) + sum += pow(_particle[i],2); + return (sum); +} + + +int main() +{ + const unsigned int VEC_SIZE = 2; + const unsigned int POP_SIZE = 20; + const unsigned int NEIGHBORHOOD_SIZE= 5; + unsigned i; + + // the population: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( real_value ); + + // position init + eoUniformGenerator < double >uGen (-3, 3); + eoInitFixedLength < Particle > random (VEC_SIZE, uGen); + + // velocity init + eoUniformGenerator < double >sGen (-2, 2); + eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen); + + // local best init + eoFirstIsBestInit < Particle > localInit; + + // perform position initialization + pop.append (POP_SIZE, random); + + // topology + eoLinearTopology topology(NEIGHBORHOOD_SIZE); + + // the full initializer + eoInitializer init(eval,veloRandom,localInit,topology,pop); + init(); + + + // bounds + eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5); + + // velocity + eoStandardVelocity velocity (topology,1,1.6,2,bnds); + + // flight + eoStandardFlight flight; + + // Terminators + eoGenContinue genCont1 (50); + eoGenContinue genCont2 (50); + + // PS flight + eoSyncEasyPSO pso1(genCont1, eval, velocity, flight); + + eoSyncEasyPSO pso2(init,genCont2, eval, velocity, flight); + + // flight + try + { + pso1(pop); + std::cout << "FINAL POPULATION AFTER SYNC PSO n°1:" << std::endl; + for (i = 0; i < pop.size(); ++i) + std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; + + pso2(pop); + std::cout << "FINAL POPULATION AFTER SYNC PSO n°2:" << std::endl; + for (i = 0; i < pop.size(); ++i) + std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; + } + catch (std::exception& e) + { + std::cout << "exception: " << e.what() << std::endl;; + exit(EXIT_FAILURE); + } + + + + return 0; +} + +//----------------------------------------------------------------------------- diff --git a/eo/tutorial/CMakeLists.txt b/eo/tutorial/CMakeLists.txt index 1633bc85..842421e4 100644 --- a/eo/tutorial/CMakeLists.txt +++ b/eo/tutorial/CMakeLists.txt @@ -4,6 +4,6 @@ ### 1) Where must cmake go now ? ###################################################################################### -SUBDIRS(Lesson1 Lesson2 Lesson3 Lesson4 Lesson5) +SUBDIRS(Lesson1 Lesson2 Lesson3 Lesson4 Lesson5 Lesson6) ###################################################################################### diff --git a/eo/tutorial/Lesson6/BinaryPSO.cpp b/eo/tutorial/Lesson6/BinaryPSO.cpp new file mode 100644 index 00000000..f87509d3 --- /dev/null +++ b/eo/tutorial/Lesson6/BinaryPSO.cpp @@ -0,0 +1,182 @@ +//----------------------------------------------------------------------------- +// BinaryPSO.cpp +//----------------------------------------------------------------------------- +//* +// An instance of a VERY simple Real-coded binary Particle Swarm Optimization Algorithm +// +//----------------------------------------------------------------------------- +#include +#include +#include + +#include + +// Use functions from namespace std +using namespace std; + +//----------------------------------------------------------------------------- +typedef eoMinimizingFitness FitT; +typedef eoBitParticle < FitT > Particle; +//----------------------------------------------------------------------------- + + +// EVALFUNC +//----------------------------------------------------------------------------- +// Just a simple function that takes binary value of a chromosome and sets +// the fitness +double binary_value (const Particle & _particle) +{ + double sum = 0; + for (unsigned i = 0; i < _particle.size(); i++) + sum +=_particle[i]; + return (sum); +} + + + +void main_function(int argc, char **argv) +{ +// PARAMETRES + // all parameters are hard-coded! + const unsigned int SEED = 42; // seed for random number generator + + const unsigned int MAX_GEN=500; + const unsigned int VEC_SIZE = 10; + const unsigned int POP_SIZE = 20; + const unsigned int NEIGHBORHOOD_SIZE= 3; + + const double VELOCITY_INIT_MIN= -1; + const double VELOCITY_INIT_MAX= 1; + + const double VELOCITY_MIN= -1.5; + const double VELOCITY_MAX= 1.5; + + const double INERTIA= 1; + const double LEARNING_FACTOR1= 1.7; + const double LEARNING_FACTOR2= 2.3; + + +////////////////////////// +// RANDOM SEED +////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + + +/// SWARM + // population <=> swarm + eoPop pop; + +/// EVALUATION + // Evaluation: from a plain C++ fn to an EvalFunc Object + eoEvalFuncPtr eval( binary_value ); + + +/////////////// +/// TOPOLOGY +////////////// + // ring topology + eoRingTopology topology(NEIGHBORHOOD_SIZE); + + +///////////////////// +// INITIALIZATION +//////////////////// + // position initialization + eoUniformGenerator uGen; + eoInitFixedLength < Particle > random (VEC_SIZE, uGen); + pop.append (POP_SIZE, random); + + // velocities initialization component + eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX); + eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen); + + // first best position initialization component + eoFirstIsBestInit < Particle > localInit; + + // Create an eoInitialier that: + // - performs a first evaluation of the particles + // - initializes the velocities + // - the first best positions of each particle + // - setups the topology + eoInitializer fullInit(eval,veloRandom,localInit,topology,pop); + + // Full initialization here to be able to print the initial population + // Else: give the "init" component in the eoEasyPSO constructor + fullInit(); + +///////////// +// OUTPUT +//////////// +// sort pop before printing it! + pop.sort(); + + // Print (sorted) the initial population (raw printout) + cout << "INITIAL POPULATION:" << endl; + for (unsigned i = 0; i < pop.size(); ++i) + cout << "\t best fit=" << pop[i] << endl; + + +/////////////// +/// VELOCITY +////////////// + // Create the bounds for the velocity not go to far away + eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX); + + // the velocity itself that needs the topology and a few constants + eoStandardVelocity velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds); + + +/////////////// +/// FLIGHT +////////////// + // Binary flight based on sigmoid function + eoSigBinaryFlight flight; + + +//////////////////////// +/// STOPPING CRITERIA +/////////////////////// + // the algo will run for MAX_GEN iterations + eoGenContinue genCont (MAX_GEN); + + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + // standard PSO requires + // stopping criteria, evaluation,velocity, flight + + eoEasyPSO pso(genCont, eval, velocity, flight); + + // Apply the algo to the swarm - that's it! + pso(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL POPULATION:" << endl; + for (unsigned i = 0; i < pop.size(); ++i) + cout << "\t best fit=" << pop[i] << endl; + +} + + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} +//----------------------------------------------------------------------------- diff --git a/eo/tutorial/Lesson6/CMakeLists.txt b/eo/tutorial/Lesson6/CMakeLists.txt new file mode 100644 index 00000000..17f368f9 --- /dev/null +++ b/eo/tutorial/Lesson6/CMakeLists.txt @@ -0,0 +1,58 @@ + +###################################################################################### +### 1) Include the sources +###################################################################################### + +INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src) + +###################################################################################### + + +###################################################################################### +### 2) Specify where CMake can find the libraries +###################################################################################### + +IF(NOT WIN32 OR CYGWIN) + LINK_DIRECTORIES(${EO_BINARY_DIR}/lib) +ENDIF(NOT WIN32 OR CYGWIN) + +# especially for Visual Studio +IF(WIN32 AND NOT CYGWIN) + LINK_DIRECTORIES(${EO_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE}) +ENDIF(WIN32 AND NOT CYGWIN) + +###################################################################################### + + +###################################################################################### +### 3) Define your targets +###################################################################################### + +ADD_EXECUTABLE(BinaryPSO BinaryPSO.cpp) +ADD_EXECUTABLE(RealPSO RealPSO.cpp) + +###################################################################################### + + +###################################################################################### +### 4) Optionnal +###################################################################################### + +SET(BINARYPSO_VERSION ${GLOBAL_VERSION}) +SET_TARGET_PROPERTIES(BinaryPSO PROPERTIES VERSION "${BINARYPSO_VERSION}") + +SET(REALPSO_VERSION ${GLOBAL_VERSION}) +SET_TARGET_PROPERTIES(RealPSO PROPERTIES VERSION "${REALPSO_VERSION}") + +###################################################################################### + + +###################################################################################### +### 5) Link the librairies for the targets +###################################################################################### + +TARGET_LINK_LIBRARIES(BinaryPSO eo eoutils) +TARGET_LINK_LIBRARIES(RealPSO eo eoutils) + +###################################################################################### + diff --git a/eo/tutorial/Lesson6/Makefile.am b/eo/tutorial/Lesson6/Makefile.am new file mode 100644 index 00000000..460fae7a --- /dev/null +++ b/eo/tutorial/Lesson6/Makefile.am @@ -0,0 +1,19 @@ + +noinst_PROGRAMS = BinaryPSO RealPSO + + +SecondBitEA_SOURCES = BinaryPSO.cpp + +SecondRealEA_SOURCES = RealPSO.cpp + + +noinst_HEADERS = + +extra_DIST = Makefile.simple + + +LDADD = -L$(top_builddir)/src + +LIBS = -leoutils -leo + +INCLUDES = -I$(top_srcdir)/src diff --git a/eo/tutorial/Lesson6/Makefile.simple b/eo/tutorial/Lesson6/Makefile.simple new file mode 100644 index 00000000..d9b5b64f --- /dev/null +++ b/eo/tutorial/Lesson6/Makefile.simple @@ -0,0 +1,31 @@ +### This Makefile is part of the tutorial of the EO library +# Unlike other Makefiles in EO, it is not using the automake/autoconf +# so that it stays easy to understant (you are in the tutorial, remember!) +# MS, Oct. 2002 + +# if you use this Makefile as a starting point for another application +# you might need to modify the following +DIR_EO = ../../src + +.SUFFIXES: .cpp + +# Warning: $(CXX) in Linux (RedHat and Mandrake at least) is g++ +# However, if you are using this Makefile within xemacs, +# and have problems with the interpretation of the output (and its colors) +# then you should use c++ instead (make CXX=c++ will do) + +.cpp: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -pg -o $@ $*.cpp +#$(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a + +.cpp.o: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -c -pg $*.cpp + +PSO = BinaryPSO RealPSO + +ALL = $(PSO) + +lesson6 : $(PSO) + +all : $(ALL) + +clean : + @/bin/rm $(ALL) *.o *.sav *.xg *.status *~ diff --git a/eo/tutorial/Lesson6/RealPSO.cpp b/eo/tutorial/Lesson6/RealPSO.cpp new file mode 100644 index 00000000..77d1167c --- /dev/null +++ b/eo/tutorial/Lesson6/RealPSO.cpp @@ -0,0 +1,183 @@ +//----------------------------------------------------------------------------- +// RealPSO.cpp +//----------------------------------------------------------------------------- +//* +// An instance of a VERY simple Real-coded Particle Swarm Optimization Algorithm +// +//----------------------------------------------------------------------------- +#include +#include +#include + +#include + +// Use functions from namespace std +using namespace std; + +//----------------------------------------------------------------------------- +typedef eoMinimizingFitness FitT; +typedef eoRealParticle < FitT > Particle; +//----------------------------------------------------------------------------- + + +// EVALFUNC +//----------------------------------------------------------------------------- +// a simple fitness function that computes the euclidian norm of a real vector +FitT real_value (const Particle & _particle) +{ + double sum = 0; + for (unsigned i = 0; i < _particle.size(); i++) + sum += pow(_particle[i],2); + return (sqrt(sum)); +} + + + +void main_function(int argc, char **argv) +{ +// PARAMETRES + // all parameters are hard-coded! + const unsigned int SEED = 42; // seed for random number generator + + const unsigned int MAX_GEN=100; + const unsigned int VEC_SIZE = 2; + const unsigned int POP_SIZE = 20; + const unsigned int NEIGHBORHOOD_SIZE= 5; + + const double POS_INIT_MIN= -2; + const double POS_INIT_MAX= 2; + + const double VELOCITY_INIT_MIN= -1; + const double VELOCITY_INIT_MAX= 1; + + const double VELOCITY_MIN= -1.5; + const double VELOCITY_MAX= 1.5; + + const double INERTIA= 1; + const double LEARNING_FACTOR1= 1.7; + const double LEARNING_FACTOR2= 2.3; + +////////////////////////// +// RANDOM SEED +////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + + +/// SWARM + // population <=> swarm + eoPop pop; + +/// EVALUATION + // Evaluation: from a plain C++ fn to an EvalFunc Object + eoEvalFuncPtr eval( real_value ); + + +/////////////// +/// TOPOLOGY +////////////// + // linear topology + eoLinearTopology topology(NEIGHBORHOOD_SIZE); + + +///////////////////// +// INITIALIZATION +//////////////////// + // position initialization + eoUniformGenerator < double >uGen (POS_INIT_MIN, POS_INIT_MAX); + eoInitFixedLength < Particle > random (VEC_SIZE, uGen); + pop.append (POP_SIZE, random); + + // velocities initialization component + eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX); + eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen); + + // first best position initialization component + eoFirstIsBestInit < Particle > localInit; + + // Create an eoInitialier that: + // - performs a first evaluation of the particles + // - initializes the velocities + // - the first best positions of each particle + // - setups the topology + eoInitializer fullInit(eval,veloRandom,localInit,topology,pop); + + // Full initialization here to be able to print the initial population + // Else: give the "init" component in the eoEasyPSO constructor + fullInit(); + +///////////// +// OUTPUT +//////////// +// sort pop before printing it! + pop.sort(); + + // Print (sorted) the initial population (raw printout) + cout << "INITIAL POPULATION:" << endl; + for (unsigned i = 0; i < pop.size(); ++i) + cout << "\t best fit=" << pop[i] << endl; + + +/////////////// +/// VELOCITY +////////////// + // Create the bounds for the velocity not go to far away + eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX); + + // the velocity itself that needs the topology and a few constants + eoStandardVelocity velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds); + + +/////////////// +/// FLIGHT +////////////// + // flight + eoStandardFlight flight; + + +//////////////////////// +/// STOPPING CRITERIA +/////////////////////// + // the algo will run for MAX_GEN iterations + eoGenContinue genCont (MAX_GEN); + + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + // standard PSO requires + // stopping criteria, evaluation,velocity, flight + + eoEasyPSO pso(genCont, eval, velocity, flight); + + // Apply the algo to the swarm - that's it! + pso(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL POPULATION:" << endl; + for (unsigned i = 0; i < pop.size(); ++i) + cout << "\t best fit=" << pop[i] << endl; + +} + + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} +//-----------------------------------------------------------------------------