From 78a798d169fad5043fb98e4648192b6163f0e638 Mon Sep 17 00:00:00 2001 From: tlegrand Date: Fri, 18 Apr 2008 08:50:22 +0000 Subject: [PATCH] added eoExtendedvelocity (+doc + test) that takes both the local best and the global of a PSO topology into account. Also updated the doc tags for the other topologies and velocities --- eo/src/eo | 3 +- eo/src/eoLinearTopology.h | 16 ++++--- eo/src/eoStandardVelocity.h | 18 ++++---- eo/src/eoTopology.h | 35 ++++----------- eo/test/CMakeLists.txt | 1 + eo/test/t-eoExtendedVelocity.cpp | 76 ++++++++++++++++++++++++++++++++ eo/test/t-eoRingTopology.cpp | 7 ++- 7 files changed, 110 insertions(+), 46 deletions(-) create mode 100644 eo/test/t-eoExtendedVelocity.cpp diff --git a/eo/src/eo b/eo/src/eo index 0d79e6f1..cd28e848 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -161,7 +161,8 @@ // velocities #include -#include +#include +#include #include #include #include diff --git a/eo/src/eoLinearTopology.h b/eo/src/eoLinearTopology.h index 21854c18..4c61bf00 100644 --- a/eo/src/eoLinearTopology.h +++ b/eo/src/eoLinearTopology.h @@ -162,17 +162,19 @@ public: return (neighborhoods[theGoodNhbd].best()); } - /* + + /* * Return the global best of the topology - */ - - virtual POT & globalBest(const eoPop& _pop) + */ + virtual POT & globalBest() { - unsigned howManyNeighborhood=_pop.size()/ neighborhoodSize; POT gBest,tmp; - unsigned indGlobalBest=0; + unsigned indGlobalBest=0; + if(neighborhoods.size()==1) + return neighborhoods[0].best(); + gBest=neighborhoods[0].best(); - for(unsigned i=1;i class eoStandardVelocity:public eoVelocity < POT > { @@ -53,8 +54,8 @@ public: /** Full constructor: Bounds and bound modifier required * @param _topology - The topology to get the global/local/other best * @param _w - The weight factor. - * @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c2 - Learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _bounds - An eoRealBaseVectorBounds: real bounds for real velocities. * If the velocities are not real, they won't be bounded by default. Should have a eoBounds ? * @param _boundsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only). @@ -79,8 +80,8 @@ public: /** Constructor: No bound updater required <-> fixed bounds * @param _topology - The topology to get the global/local/other best * @param _w - The weight factor. - * @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c2 - Learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _bounds - An eoRealBaseVectorBounds: real bounds for real velocities. * If the velocities are not real, they won't be bounded by default. Should have a eoBounds ? * @param _gen - The eo random generator, default=rng @@ -101,9 +102,10 @@ public: /** Constructor: Neither bounds nor bound updater required <-> free velocity + * @param _topology - The topology to get the global/local/other best * @param _w - The weight factor. - * @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c2 - Learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _gen - The eo random generator, default=rng */ eoStandardVelocity (eoTopology < POT > & _topology, diff --git a/eo/src/eoTopology.h b/eo/src/eoTopology.h index ca1395a9..827064fe 100644 --- a/eo/src/eoTopology.h +++ b/eo/src/eoTopology.h @@ -59,36 +59,19 @@ public: for (unsigned i = 0; i < _pop.size (); i++) updateNeighborhood(_pop[i],i); } + /** * Builds the neighborhoods contained in the topology. */ - virtual POT & best (unsigned ) = 0; - - - /* - * Returns the global best particle of the given population. - * Even if the extended topology does not define a global best, - * it should always be possible to get it by searching in all the neighborhoods. - * This method is virtual in order not to have to define it in all the extended topologies. - */ - virtual POT & globalBest(const eoPop& _pop) - { - POT gBest,tmp; - unsigned indGlobalBest=0; - gBest=best(0); - for(unsigned i=1;i<_pop.size();i++) - { - tmp=best(i); - if(gBest.best() < tmp.best()) - { - gBest=tmp; - indGlobalBest=i; - } - - } - return best(indGlobalBest); - } + virtual POT & best (unsigned ) = 0; + + + /* + * Return the global best of the topology + */ + virtual POT & globalBest(){} + /** * Prints the neighborhoods contained in the topology. diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index 0cb39405..07279bec 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -71,6 +71,7 @@ SET (TEST_LIST t-eoParetoFitness t-eoRingTopology t-eoSyncEasyPSO t-eoOrderXover + t-eoExtendedVelocity # t-eoFrontSorter # t-eoEpsMOEA ) diff --git a/eo/test/t-eoExtendedVelocity.cpp b/eo/test/t-eoExtendedVelocity.cpp new file mode 100644 index 00000000..c019bf69 --- /dev/null +++ b/eo/test/t-eoExtendedVelocity.cpp @@ -0,0 +1,76 @@ +//----------------------------------------------------------------------------- +// t-eoExtendedVelocity.cpp +//----------------------------------------------------------------------------- + + +#include + +typedef eoRealParticle < double > Particle; + +//Evaluation function +double f (const Particle & _particle) +{ + double sum = 0; + for (unsigned i = 0; i < _particle.size (); i++) + sum += pow(_particle[i],2); + return (-sum); +} + +int main_function(int argc, char **argv) +{ + const unsigned POP_SIZE = 6, VEC_SIZE = 2, NEIGHBORHOOD_SIZE=2; + + // the population: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( f ); + + // position + velocity + best init + eoUniformGenerator < double >uGen (-3, 3); + eoInitFixedLength < Particle > random (VEC_SIZE, uGen); + eoUniformGenerator < double >sGen (-2, 2); + eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen); + eoFirstIsBestInit < Particle > localInit; + pop.append (POP_SIZE, random); + + // topology + eoLinearTopology topology(NEIGHBORHOOD_SIZE); + eoInitializer init(eval,veloRandom,localInit,topology,pop); + init(); + + // velocity + eoExtendedVelocity velocity (topology,1,1,1,1); + + // the test itself + for (unsigned int i = 0; i < POP_SIZE; ++i) + { + std::cout << " Initial particle n°" << i << " velocity: " << std::endl; + for (unsigned int j = 0; j < VEC_SIZE; ++j) + std::cout << " v" << j << "=" << pop[i].velocities[j] << std::endl; + } + + for (unsigned int i = 0; i < POP_SIZE; ++i) + velocity (pop[i],i); + + for (unsigned int i = 0; i < POP_SIZE; ++i) + { + std::cout << " Final particle n°" << i << " velocity: " << std::endl; + for (unsigned int j = 0; j < VEC_SIZE; ++j) + std::cout << " v" << j << "=" << pop[i].velocities[j] << std::endl; + } + +} + +int main(int argc, char **argv) +{ + try + { + main_function(argc, argv); + } + catch(std::exception& e) + { + std::cout << "Exception: " << e.what() << " in t-eoExtendedVelocity" << std::endl; + } + +} diff --git a/eo/test/t-eoRingTopology.cpp b/eo/test/t-eoRingTopology.cpp index 9fd1a47c..e7d7488d 100644 --- a/eo/test/t-eoRingTopology.cpp +++ b/eo/test/t-eoRingTopology.cpp @@ -16,7 +16,7 @@ double f (const Indi & _indi) return (-sum); } -int the_main(int argc, char **argv) +int main_function(int argc, char **argv) { //Parameters const unsigned int VEC_SIZE = 2; @@ -31,7 +31,6 @@ int the_main(int argc, char **argv) eoUniformGenerator < double >sGen (-1., 1.); eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen); eoFirstIsBestInit < Indi > localInit; - eoStandardFlight < Indi > flight; eoPop < Indi > pop; pop.append (POP_SIZE, random); apply(eval, pop); @@ -58,11 +57,11 @@ int main(int argc, char **argv) { try { - the_main(argc, argv); + main_function(argc, argv); } catch(std::exception& e) { - std::cout << "Exception: " << e.what() << std::endl; + std::cout << "Exception: " << e.what() << " in t-eoRingTopology" << std::endl; } }