diff --git a/eo/src/eo b/eo/src/eo index be7596fd..b7dbe157 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -162,6 +162,7 @@ // velocities #include #include +#include #include #include #include diff --git a/eo/src/eoEasyEA.h b/eo/src/eoEasyEA.h index 89ef790f..b599efed 100644 --- a/eo/src/eoEasyEA.h +++ b/eo/src/eoEasyEA.h @@ -60,184 +60,219 @@ Change (MS, July 3. 2001): Note: it looks ugly only because we wanted to authorize many different constructors. Please only look at the operator() and there shall be light */ -template class eoEasyEA: public eoAlgo { -public: - - /** Ctor taking a breed and merge */ - eoEasyEA( - eoContinue& _continuator, - eoEvalFunc& _eval, - eoBreed& _breed, - eoReplacement& _replace - ) : continuator(_continuator), - eval (_eval), - loopEval(_eval), - popEval(loopEval), - selectTransform(dummySelect, dummyTransform), - breed(_breed), - mergeReduce(dummyMerge, dummyReduce), - replace(_replace) - {} - - /* - eoEasyEA(eoContinue & _continuator, - eoPopEvalFunc & _pop_eval, - eoBreed & _breed, - eoReplacement & _replace - ) : - continuator (_continuator), - eval (dummyEval), - loopEval(dummyEval), - popEval (_pop_eval), - selectTransform (dummySelect, dummyTransform), - breed (_breed), - mergeReduce (dummyMerge, dummyReduce), - replace (_replace) { - - } - */ - - /** NEW Ctor taking a breed and merge and an eoPopEval */ - eoEasyEA( - eoContinue& _continuator, - eoPopEvalFunc& _eval, - eoBreed& _breed, - eoReplacement& _replace - ) : continuator(_continuator), - eval (dummyEval), - loopEval(dummyEval), - popEval(_eval), - selectTransform(dummySelect, dummyTransform), - breed(_breed), - mergeReduce(dummyMerge, dummyReduce), - replace(_replace) - {} - - /// Ctor eoBreed, eoMerge and eoReduce. - eoEasyEA( - eoContinue& _continuator, - eoEvalFunc& _eval, - eoBreed& _breed, - eoMerge& _merge, - eoReduce& _reduce - ) : continuator(_continuator), - eval (_eval), - loopEval(_eval), - popEval(loopEval), - selectTransform(dummySelect, dummyTransform), - breed(_breed), - mergeReduce(_merge, _reduce), - replace(mergeReduce) - {} - - /// Ctor eoSelect, eoTransform, and eoReplacement - eoEasyEA( - eoContinue& _continuator, - eoEvalFunc& _eval, - eoSelect& _select, - eoTransform& _transform, - eoReplacement& _replace - ) : continuator(_continuator), - eval (_eval), - loopEval(_eval), - popEval(loopEval), - selectTransform(_select, _transform), - breed(selectTransform), - mergeReduce(dummyMerge, dummyReduce), - replace(_replace) - {} - - /// Ctor eoSelect, eoTransform, eoMerge and eoReduce. - eoEasyEA( - eoContinue& _continuator, - eoEvalFunc& _eval, - eoSelect& _select, - eoTransform& _transform, - eoMerge& _merge, - eoReduce& _reduce - ) : continuator(_continuator), - eval (_eval), - loopEval(_eval), - popEval(loopEval), - selectTransform(_select, _transform), - breed(selectTransform), - mergeReduce(_merge, _reduce), - replace(mergeReduce) - {} - - - - - /// Apply a few generation of evolution to the population. - virtual void operator()(eoPop& _pop) +template class eoEasyEA: public eoAlgo { - eoPop offspring, empty_pop; + public: - popEval(empty_pop, _pop); // A first eval of pop. + /** Ctor taking a breed and merge */ + eoEasyEA( + eoContinue& _continuator, + eoEvalFunc& _eval, + eoBreed& _breed, + eoReplacement& _replace + ) : continuator(_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + selectTransform(dummySelect, dummyTransform), + breed(_breed), + mergeReduce(dummyMerge, dummyReduce), + replace(_replace) + {} - do + /* + eoEasyEA(eoContinue & _continuator, + eoPopEvalFunc & _pop_eval, + eoBreed & _breed, + eoReplacement & _replace + ) : + continuator (_continuator), + eval (dummyEval), + loopEval(dummyEval), + popEval (_pop_eval), + selectTransform (dummySelect, dummyTransform), + breed (_breed), + mergeReduce (dummyMerge, dummyReduce), + replace (_replace) { + + } + */ + + /** NEW Ctor taking a breed and merge and an eoPopEval */ + eoEasyEA( + eoContinue& _continuator, + eoPopEvalFunc& _eval, + eoBreed& _breed, + eoReplacement& _replace + ) : continuator(_continuator), + eval (dummyEval), + loopEval(dummyEval), + popEval(_eval), + selectTransform(dummySelect, dummyTransform), + breed(_breed), + mergeReduce(dummyMerge, dummyReduce), + replace(_replace) + {} + + + /// Ctor eoSelect, eoTransform, eoReplacement and an eoPopEval + eoEasyEA( + eoContinue& _continuator, + eoPopEvalFunc& _eval, + eoSelect& _select, + eoTransform& _transform, + eoReplacement& _replace + ) : continuator(_continuator), + eval (dummyEval), + loopEval(dummyEval), + popEval(_eval), + selectTransform(_select, _transform), + breed(selectTransform), + mergeReduce(dummyMerge, dummyReduce), + replace(_replace) + {} + + /// Ctor eoBreed, eoMerge and eoReduce. + eoEasyEA( + eoContinue& _continuator, + eoEvalFunc& _eval, + eoBreed& _breed, + eoMerge& _merge, + eoReduce& _reduce + ) : continuator(_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + selectTransform(dummySelect, dummyTransform), + breed(_breed), + mergeReduce(_merge, _reduce), + replace(mergeReduce) + {} + + /// Ctor eoSelect, eoTransform, and eoReplacement + eoEasyEA( + eoContinue& _continuator, + eoEvalFunc& _eval, + eoSelect& _select, + eoTransform& _transform, + eoReplacement& _replace + ) : continuator(_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + selectTransform(_select, _transform), + breed(selectTransform), + mergeReduce(dummyMerge, dummyReduce), + replace(_replace) + {} + + /// Ctor eoSelect, eoTransform, eoMerge and eoReduce. + eoEasyEA( + eoContinue& _continuator, + eoEvalFunc& _eval, + eoSelect& _select, + eoTransform& _transform, + eoMerge& _merge, + eoReduce& _reduce + ) : continuator(_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + selectTransform(_select, _transform), + breed(selectTransform), + mergeReduce(_merge, _reduce), + replace(mergeReduce) + {} + + + + + /// Apply a few generation of evolution to the population. + virtual void operator()(eoPop& _pop) { - try - { - unsigned pSize = _pop.size(); - offspring.clear(); // new offspring + eoPop offspring, empty_pop; - breed(_pop, offspring); + popEval(empty_pop, _pop); // A first eval of pop. - popEval(_pop, offspring); // eval of parents + offspring if necessary - - replace(_pop, offspring); // after replace, the new pop. is in _pop + do + { + try + { + unsigned pSize = _pop.size(); + offspring.clear(); // new offspring - if (pSize > _pop.size()) - throw std::runtime_error("Population shrinking!"); - else if (pSize < _pop.size()) - throw std::runtime_error("Population growing!"); + breed(_pop, offspring); - } - catch (std::exception& e) - { - std::string s = e.what(); - s.append( " in eoEasyEA"); - throw std::runtime_error( s ); - } - } while ( continuator( _pop ) ); - } + popEval(_pop, offspring); // eval of parents + offspring if necessary -protected : + replace(_pop, offspring); // after replace, the new pop. is in _pop - // If selectTransform needs not be used, dummySelect and dummyTransform are used - // to instantiate it. + if (pSize > _pop.size()) + throw std::runtime_error("Population shrinking!"); + else if (pSize < _pop.size()) + throw std::runtime_error("Population growing!"); + + } + catch (std::exception& e) + { + std::string s = e.what(); + s.append( " in eoEasyEA"); + throw std::runtime_error( s ); + } + } + while ( continuator( _pop ) ); + } + + protected : + + // If selectTransform needs not be used, dummySelect and dummyTransform are used + // to instantiate it. class eoDummySelect : public eoSelect - { public : void operator()(const eoPop&, eoPop&) {} } dummySelect; + { + public : + void operator()(const eoPop&, eoPop&) + {} + } + dummySelect; - class eoDummyTransform : public eoTransform - { public : void operator()(eoPop&) {} } dummyTransform; + class eoDummyTransform : public eoTransform + { + public : + void operator()(eoPop&) + {} + } + dummyTransform; class eoDummyEval : public eoEvalFunc - {public: void operator()(EOT &) {} } dummyEval; + { + public: + void operator()(EOT &) + {} + } + dummyEval; - eoContinue& continuator; + eoContinue& continuator; - eoEvalFunc & eval ; - eoPopLoopEval loopEval; + eoEvalFunc & eval ; + eoPopLoopEval loopEval; - eoPopEvalFunc& popEval; + eoPopEvalFunc& popEval; - eoSelectTransform selectTransform; - eoBreed& breed; + eoSelectTransform selectTransform; + eoBreed& breed; - // If mergeReduce needs not be used, dummyMerge and dummyReduce are used - // to instantiate it. - eoNoElitism dummyMerge; - eoTruncate dummyReduce; + // If mergeReduce needs not be used, dummyMerge and dummyReduce are used + // to instantiate it. + eoNoElitism dummyMerge; + eoTruncate dummyReduce; - eoMergeReduce mergeReduce; - eoReplacement& replace; + eoMergeReduce mergeReduce; + eoReplacement& replace; - // Friend classes - friend class eoIslandsEasyEA ; - friend class eoDistEvalEasyEA ; -}; + // Friend classes + friend class eoIslandsEasyEA ; + friend class eoDistEvalEasyEA ; + }; //----------------------------------------------------------------------------- diff --git a/eo/src/eoInitializer.h b/eo/src/eoInitializer.h index d1f14793..f0c905de 100644 --- a/eo/src/eoInitializer.h +++ b/eo/src/eoInitializer.h @@ -31,19 +31,22 @@ #include #include #include +#include /* * Abstract class for initialization of algorithm PSO */ template class eoInitializerBase : public eoFunctorBase -{ -public : + { + public : - virtual ~eoInitializerBase() {} + virtual ~eoInitializerBase() + {} - virtual void operator()(){}; -}; + virtual void operator()() + {}; + }; /** Base (name) class for Initialization of algorithm PSO @@ -51,8 +54,8 @@ public : @see eoInitializerBase eoUF apply */ template class eoInitializer : public eoInitializerBase -{ -public: + { + public: //! Constructor //! @param _proc Evaluation function @@ -60,25 +63,54 @@ public: //! @param _initBest Initialization of the best //! @param _pop Population eoInitializer( - eoUF& _proc, - eoVelocityInit < POT > &_initVelo, - eoParticleBestInit &_initBest, - eoPop < POT > &_pop - ) : proc(_proc), initVelo(_initVelo), initBest(_initBest) + eoUF& _proc, + eoVelocityInit < POT > &_initVelo, + eoParticleBestInit &_initBest, + eoTopology &_topology, + eoPop < POT > &_pop + ) : proc(_proc), procPara(dummyEval), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop) { - apply(proc, _pop); - apply < POT > (initVelo, _pop); - apply < POT > (initBest, _pop); + /* apply(proc, _pop); + apply < POT > (initVelo, _pop); + apply < POT > (initBest, _pop); + topology.setup(_pop);*/ } + //! Constructor for parallel evaluation + //! @param _proc Evaluation function + //! @param _initVelo Initialization of the velocity + //! @param _initBest Initialization of the best + //! @param _pop Population + eoInitializer( + eoPopEvalFunc & _proc, + eoVelocityInit < POT > &_initVelo, + eoParticleBestInit &_initBest, + eoTopology &_topology, + eoPop < POT > &_pop + ) : proc(dummy), procPara(_proc), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop) + {} + + //! Give the name of the class //! @return The name of the class virtual std::string className (void) const - { + { return "eoInitializer"; + } + + + + virtual void operator () (/*eoPop < POT > &_pop*/) + { + eoPop empty_pop; + apply(proc, pop); + procPara(empty_pop, pop); + apply < POT > (initVelo, pop); + apply < POT > (initBest, pop); + topology.setup(pop); } -private : + private : /* @param proc First evaluation @@ -86,10 +118,28 @@ private : @param initBest Initialization of the best */ + eoPop < POT > & pop; eoUF& proc; + eoPopEvalFunc & procPara; eoVelocityInit < POT > & initVelo; eoParticleBestInit & initBest; -}; + eoTopology & topology; + class eoDummyEval : public eoPopEvalFunc + { + public: + void operator()(eoPop &,eoPop &_pop) + {} + } + dummyEval; + class eoDummy : public eoUF + { + public: + void operator()(POT &) + {} + + } + dummy; + }; #endif diff --git a/eo/src/eoIntegerVelocity.h b/eo/src/eoIntegerVelocity.h index 1624be07..76f7161a 100644 --- a/eo/src/eoIntegerVelocity.h +++ b/eo/src/eoIntegerVelocity.h @@ -18,7 +18,8 @@ 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: clive.canape@inria.fr + Contact: thomas.legrand@lifl.fr + clive.canape@inria.fr */ //----------------------------------------------------------------------------- @@ -35,8 +36,9 @@ //----------------------------------------------------------------------------- -/** Standard velocity performer for particle swarm optimization. Derivated from abstract eoVelocity, -* At step t: v(t+1)= int (v(t) + c1*r1* ( xbest(t)-x(t) ) + c2*r2* ( gbest(t) - x(t) )) +/** Integer velocity performer for particle swarm optimization. Derivated from abstract eoVelocity, +* At step t: v(t+1)= c1 * v(t) + c2 * r2 * ( xbest(t)-x(t) ) + c3 * r3 * ( gbest(t) - x(t) ) +* v(t) is an INT for any time step * (ci given and Ri chosen at random in [0;1]). */ template < class POT > class eoIntegerVelocity:public eoVelocity < POT > @@ -51,22 +53,25 @@ public: /** Full constructor: Bounds and bound modifier required * @param _topology - The topology to get the global/local/other best - * @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - The first learning factor quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType + * @param _c2 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c3 - The third 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). * @param _gen - The eo random generator, default=rng */ eoIntegerVelocity (eoTopology < POT > & _topology, - const VelocityType & _c1, - const VelocityType & _c2 , + const VelocityType & _c1, + const VelocityType & _c2, + const VelocityType & _c3, eoRealVectorBounds & _bounds, eoRealBoundModifier & _bndsModifier, eoRng & _gen = rng): topology(_topology), c1 (_c1), c2 (_c2), + c3 (_c3), bounds(_bounds), bndsModifier(_bndsModifier), gen(_gen){} @@ -74,8 +79,9 @@ public: /** Constructor: No bound updater required <-> fixed bounds * @param _topology - The topology to get the global/local/other best - * @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - The first learning factor quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType + * @param _c2 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c3 - The third 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 @@ -83,29 +89,33 @@ public: eoIntegerVelocity (eoTopology < POT > & _topology, const VelocityType & _c1, const VelocityType & _c2, + const VelocityType & _c3, eoRealVectorBounds & _bounds, eoRng & _gen = rng): topology(_topology), c1 (_c1), c2 (_c2), + c3 (_c3), bounds(_bounds), bndsModifier(dummyModifier), gen(_gen){} /** Constructor: Neither bounds nor bound updater required <-> free velocity - * @param _topology - The topology to get the global/local/other best - * @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - The first learning factor quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType + * @param _c2 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c3 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _gen - The eo random generator, default=rng */ eoIntegerVelocity (eoTopology < POT > & _topology, const VelocityType & _c1, const VelocityType & _c2, + const VelocityType & _c3, eoRng & _gen = rng): topology(_topology), - c1 (_c1), + c1 (_c1), c2 (_c2), + c3 (_c3), bounds(*(new eoRealVectorNoBounds(0))), bndsModifier(dummyModifier), gen(_gen) @@ -120,14 +130,14 @@ public: */ void operator () (POT & _po,unsigned _indice) { - VelocityType r1; VelocityType r2; + VelocityType r3; VelocityType newVelocity; // cast the learning factors to VelocityType - r1 = (VelocityType) rng.uniform (1) * c1; r2 = (VelocityType) rng.uniform (1) * c2; + r3 = (VelocityType) rng.uniform (1) * c3; // need to resize the bounds even if there are dummy because of "isBounded" call bounds.adjust_size(_po.size()); @@ -135,7 +145,7 @@ public: // assign the new velocities for (unsigned j = 0; j < _po.size (); j++) { - newVelocity= round(_po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) + r2 * (topology.best (_indice)[j] - _po[j])); + newVelocity= round (c1 * _po.velocities[j] + r2 * (_po.bestPositions[j] - _po[j]) + r3 * (topology.best (_indice)[j] - _po[j])); /* check bounds */ if (bounds.isMinBounded(j)) @@ -154,8 +164,8 @@ public: { topology.updateNeighborhood(_po,_indice); } - - //! eoTopology getTopology + + //! eoTopology getTopology //! @return topology eoTopology & getTopology () @@ -165,8 +175,9 @@ public: protected: eoTopology < POT > & topology; - const VelocityType & c1; // learning factor 1 - const VelocityType & c2; // learning factor 2 + const VelocityType & c1; // social/cognitive coefficient + const VelocityType & c2; // social/cognitive coefficient + const VelocityType & c3; // social/cognitive coefficient eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type. eoRealBoundModifier & bndsModifier; diff --git a/eo/src/eoParticleFullInitializer.h b/eo/src/eoParticleFullInitializer.h new file mode 100644 index 00000000..d800db5b --- /dev/null +++ b/eo/src/eoParticleFullInitializer.h @@ -0,0 +1,149 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoParticleFullInitializer.h +// (c) OPAC Team, INRIA, 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: clive.canape@inria.fr + + + */ +//----------------------------------------------------------------------------- + +#ifndef _eoParticleFullInitializer_H +#define _eoParticleFullInitializer_H + +#include +#include +#include +#include +#include + + +/* + * Abstract class for initialization of algorithm PSO + */ +template class eoInitializerBase : public eoFunctorBase + { + public : + + virtual ~eoInitializerBase() + {} + + virtual void operator()() + {}; + }; + +/** + Base (name) class for Initialization of algorithm PSO + + @see eoInitializerBase eoUF apply +*/ +template class eoParticleInitializer : public eoInitializerBase + { + public: + + //! Constructor + //! @param _proc Evaluation function + //! @param _initVelo Initialization of the velocity + //! @param _initBest Initialization of the best + //! @param _pop Population + eoParticleFullInitializer( + eoUF& _proc, + eoVelocityInit < POT > &_initVelo, + eoParticleBestInit &_initBest, + eoTopology &_topology, + eoPop < POT > &_pop + ) : proc(_proc), procPara(dummyEval), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop) {} + + + //! Constructor for parallel evaluation + //! @param _proc Evaluation function + //! @param _initVelo Initialization of the velocity + //! @param _initBest Initialization of the best + //! @param _pop Population + eoParticleFullInitializer( + eoPopEvalFunc & _proc, + eoVelocityInit < POT > &_initVelo, + eoParticleBestInit &_initBest, + eoTopology &_topology, + eoPop < POT > &_pop + ) : proc(dummy), procPara(_proc), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop) + {} + + + //! Give the name of the class + //! @return The name of the class + virtual std::string className (void) const + { + return "eoInitializer"; + } + + + + virtual void operator () () + { + eoPop empty_pop; + + // evaluates using either the "sequential" evaluator ... + apply(proc, pop); + + // ... or the parallel one + procPara(empty_pop, pop); + + // no matter what is the eval operator, initializes the velocities and the particle's best + apply < POT > (initVelo, pop); + apply < POT > (initBest, pop); + + // finally setup the topology. We have now all we need to do so. + topology.setup(pop); + } + + 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; + eoTopology & topology; + class eoDummyEval : public eoPopEvalFunc + { + public: + void operator()(eoPop &,eoPop &_pop) + {} + } + dummyEval; + class eoDummy : public eoUF + { + public: + void operator()(POT &) + {} + + } + dummy; + }; +#endif /*_eoParticleFullInitializer_H*/ + + + diff --git a/eo/src/eoRingTopology.h b/eo/src/eoRingTopology.h index 0dff3147..d1046bd6 100644 --- a/eo/src/eoRingTopology.h +++ b/eo/src/eoRingTopology.h @@ -5,7 +5,8 @@ // (c) INRIA Futurs DOLPHIN 2007 /* Clive Canape - + Thomas Legrand + 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 @@ -34,9 +35,10 @@ /** - * Topology dedicated to "globest best" strategy for particle swarm optimization. - * All the particles of the swarm belong to the same and only social neighborhood. - * The global best is stored as a protected member and updated by using the "updateNeighborhood" method. + * Static ring topology for particle swarm optimization. + * The neighborhoods are built using a ring based on each particle's indice and + * do not change for all the time steps. Only the best particle in each neighborhood is + * potentially updated thanks to the "updateNeighborhood" method. */ template < class POT > class eoRingTopology:public eoTopology { @@ -44,14 +46,15 @@ template < class POT > class eoRingTopology:public eoTopology public: /** - * The only Ctor. No parameter required. + * The only Ctor. + * @param _neighborhoodSize - The size of each neighborhood. */ eoRingTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){} /** - * Builds the only neighborhood that contains all the particles of the given population. - * Also initializes the global best particle with the best particle of the given population. + * Builds the neighborhoods using a ring strategy based on the particle indices. + * Also initializes the best particle of each neighborhood. * @param _pop - The population used to build the only neighborhood. * @return */ @@ -87,7 +90,7 @@ public: } /** - * Retrieve the neighboorhood of a particle. + * Retrieves the neighboorhood of a particle. * @return _indice - The particle indice (in the population) */ unsigned retrieveNeighborhoodByIndice(unsigned _indice) @@ -97,8 +100,8 @@ public: /** - * Update the best fitness of the given particle if it's better. - * Also replace the global best by the given particle if it's better. + * Updates the best fitness of the given particle and + * potentially replaces the local best the given particle it's better. * @param _po - The particle to update * @param _indice - The indice of the given particle in the population */ @@ -123,7 +126,7 @@ public: /** - * Return the global best particle. + * Returns the best particle belonging to the neighborhood of the given particle. * @param _indice - The indice of a particle in the population * @return POT & - The best particle in the neighborhood of the particle whose indice is _indice */ diff --git a/eo/src/eoStandardVelocity.h b/eo/src/eoStandardVelocity.h index 314aacd9..71530879 100644 --- a/eo/src/eoStandardVelocity.h +++ b/eo/src/eoStandardVelocity.h @@ -37,7 +37,7 @@ /** Standard velocity performer for particle swarm optimization. Derivated from abstract eoVelocity, -* At step t: v(t+1)= v(t) + c1*r1* ( xbest(t)-x(t) ) + c2*r2* ( gbest(t) - x(t) ) +* At step t: v(t+1)= c1 * v(t) + c2 * r2 * ( xbest(t)-x(t) ) + c3 * r3 * ( gbest(t) - x(t) ) * (ci given and Ri chosen at random in [0;1]). */ template < class POT > class eoStandardVelocity:public eoVelocity < POT > @@ -52,22 +52,25 @@ public: /** Full constructor: Bounds and bound modifier required * @param _topology - The topology to get the global/local/other best - * @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - The first learning factor quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType + * @param _c2 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c3 - The third 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). * @param _gen - The eo random generator, default=rng */ eoStandardVelocity (eoTopology < POT > & _topology, - const VelocityType & _c1, - const VelocityType & _c2 , + const VelocityType & _c1, + const VelocityType & _c2, + const VelocityType & _c3, eoRealVectorBounds & _bounds, eoRealBoundModifier & _bndsModifier, eoRng & _gen = rng): topology(_topology), c1 (_c1), c2 (_c2), + c3 (_c3), bounds(_bounds), bndsModifier(_bndsModifier), gen(_gen){} @@ -75,8 +78,9 @@ public: /** Constructor: No bound updater required <-> fixed bounds * @param _topology - The topology to get the global/local/other best - * @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - The first learning factor quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType + * @param _c2 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c3 - The third 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 @@ -84,29 +88,33 @@ public: eoStandardVelocity (eoTopology < POT > & _topology, const VelocityType & _c1, const VelocityType & _c2, + const VelocityType & _c3, eoRealVectorBounds & _bounds, eoRng & _gen = rng): topology(_topology), c1 (_c1), c2 (_c2), + c3 (_c3), bounds(_bounds), bndsModifier(dummyModifier), gen(_gen){} /** Constructor: Neither bounds nor bound updater required <-> free velocity - * @param _topology - The topology to get the global/local/other best - * @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType - * @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType + * @param _c1 - The first learning factor quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType + * @param _c2 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType + * @param _c3 - The third 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, const VelocityType & _c1, const VelocityType & _c2, + const VelocityType & _c3, eoRng & _gen = rng): topology(_topology), - c1 (_c1), + c1 (_c1), c2 (_c2), + c3 (_c3), bounds(*(new eoRealVectorNoBounds(0))), bndsModifier(dummyModifier), gen(_gen) @@ -121,14 +129,14 @@ public: */ void operator () (POT & _po,unsigned _indice) { - VelocityType r1; VelocityType r2; + VelocityType r3; VelocityType newVelocity; // cast the learning factors to VelocityType - r1 = (VelocityType) rng.uniform (1) * c1; r2 = (VelocityType) rng.uniform (1) * c2; + r3 = (VelocityType) rng.uniform (1) * c3; // need to resize the bounds even if there are dummy because of "isBounded" call bounds.adjust_size(_po.size()); @@ -136,7 +144,7 @@ public: // assign the new velocities for (unsigned j = 0; j < _po.size (); j++) { - newVelocity= _po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) + r2 * (topology.best (_indice)[j] - _po[j]); + newVelocity= c1 * _po.velocities[j] + r2 * (_po.bestPositions[j] - _po[j]) + r3 * (topology.best (_indice)[j] - _po[j]); /* check bounds */ if (bounds.isMinBounded(j)) @@ -166,8 +174,9 @@ public: protected: eoTopology < POT > & topology; - const VelocityType & c1; // learning factor 1 - const VelocityType & c2; // learning factor 2 + const VelocityType & c1; // social/cognitive coefficient + const VelocityType & c2; // social/cognitive coefficient + const VelocityType & c3; // social/cognitive coefficient eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type. eoRealBoundModifier & bndsModifier; diff --git a/eo/src/eoStarTopology.h b/eo/src/eoStarTopology.h index 5f935cc3..db1c6f7b 100644 --- a/eo/src/eoStarTopology.h +++ b/eo/src/eoStarTopology.h @@ -35,7 +35,7 @@ /** * Topology dedicated to "globest best" strategy for particle swarm optimization. * All the particles of the swarm belong to the same and only social neighborhood. - * The global best is stored as a protected member and updated by using the "updateNeighborhood" method. + * The global best is stored and updated using the eoSocialNeighborhood. */ template < class POT > class eoStarTopology:public eoTopology { diff --git a/eo/src/eoSyncEasyPSO.h b/eo/src/eoSyncEasyPSO.h index 02cec6be..c474fdf9 100644 --- a/eo/src/eoSyncEasyPSO.h +++ b/eo/src/eoSyncEasyPSO.h @@ -44,8 +44,8 @@ * -- update the neighborhoods */ template < class POT > class eoSyncEasyPSO:public eoPSO < POT > -{ -public: + { + public: /** Full constructor * @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system @@ -55,16 +55,19 @@ public: * to modify the positions according to the velocities */ eoSyncEasyPSO ( - eoContinue < POT > &_continuator, - eoEvalFunc < POT > &_eval, - eoVelocity < POT > &_velocity, - eoFlight < POT > &_flight): - continuator (_continuator), - eval (_eval), - loopEval(_eval), - popEval(loopEval), - velocity (_velocity), - flight (_flight){} + eoInitializer &_init, + eoContinue < POT > &_continuator, + eoEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity, + eoFlight < POT > &_flight): + init(_init), + continuator (_continuator), + eval (_eval), + loopEval(_eval), + popEval(loopEval), + velocity (_velocity), + flight (_flight) + {} /** Constructor without eoFlight. For special cases when the flight is performed withing the velocity. @@ -73,54 +76,82 @@ public: * @param _velocity - An eoVelocity that defines how to compute the velocities */ eoSyncEasyPSO ( - eoContinue < POT > &_continuator, - eoEvalFunc < POT > &_eval, - eoVelocity < POT > &_velocity): - continuator (_continuator), - eval (_eval), - loopEval(_eval), - popEval(loopEval), - velocity (_velocity), - flight (dummyFlight){} - + eoInitializer &_init, + eoContinue < POT > &_continuator, + eoEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity): + init(_init), + 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 ( + eoInitializer &_init, + eoContinue < POT > &_continuator, + eoPopEvalFunc < POT > &_eval, + eoVelocity < POT > &_velocity, + eoFlight &_flight): + init(_init), + 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) { - try + + try { - // just to use a loop eval - eoPop empty_pop; + init(); + // just to use a loop eval + eoPop empty_pop; - do + do { - // perform velocity evaluation - velocity.apply (_pop); - // apply the flight - flight.apply (_pop); + // perform velocity evaluation + velocity.apply (_pop); - // evaluate the position (with a loop eval, empty_swarm IS USELESS) - loopEval(empty_pop,_pop); + // apply the flight + flight.apply (_pop); - // update the topology (particle and local/global best(s)) - velocity.updateNeighborhood(_pop); + // evaluate the position (with a loop eval, empty_swarm IS USELESS) + popEval(empty_pop,_pop); + // update the topology (particle and local/global best(s)) + velocity.updateNeighborhood(_pop); - }while (continuator (_pop)); + } + while (continuator (_pop)); } - catch (std::exception & e) + catch (std::exception & e) { - std::string s = e.what (); - s.append (" in eoSyncEasyPSO"); - throw std::runtime_error (s); + std::string s = e.what (); + s.append (" in eoSyncEasyPSO"); + throw std::runtime_error (s); } } -private: + private: + + eoInitializer &init; eoContinue < POT > &continuator; eoEvalFunc < POT > &eval; @@ -132,7 +163,17 @@ private: // 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; + + }; #endif /*_EOSYNCEASYPSO_H*/ diff --git a/eo/src/eoTopology.h b/eo/src/eoTopology.h index d62e6a2a..131aa8e6 100644 --- a/eo/src/eoTopology.h +++ b/eo/src/eoTopology.h @@ -18,7 +18,7 @@ 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 + Contact: thomas.legrand@inria.fr clive.canape@inria.fr */ //----------------------------------------------------------------------------- @@ -32,43 +32,46 @@ /** - * Define the interface for a swarm optimization topology. + * Defines the interface of a swarm topology. Can be static (usually the case for the social topologies) + * or dynamic. It's the same interface for both social and physical topologies. ("social" topology means + * social-neighborhood-based toplogy and so on ...) */ template < class POT > class eoTopology:public eoPop < POT > { public: /** - * Build the neighborhoods contained in the topology. + * Builds the neighborhoods contained in the topology. */ virtual void setup(const eoPop &)=0; /** - * Update the neighborhood of the given particle and its indice in the population + * Updates the neighborhood of the given particle and its indice in the population */ virtual void updateNeighborhood(POT & ,unsigned)=0; /** - * Update the neighborhood of the given particle thanks to a whole population (used for distributed or synchronous PSO) + * Updates the neighborhood of the given particle thanks to a whole population (used for distributed or synchronous PSO) */ virtual void updateNeighborhood(eoPop < POT > &_pop) { for (unsigned i = 0; i < _pop.size (); i++) - { updateNeighborhood(_pop[i],i); - } } /** - * Build the neighborhoods contained in the topology. + * Builds the neighborhoods contained in the topology. */ virtual POT & best (unsigned ) = 0; - /* - * Return the global best - */ + /* + * 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 globalBest,tmp; @@ -88,8 +91,7 @@ public: } /** - * Build the neighborhoods contained in the topology. - * @param _pop - The population ton share between the neighborhood(s) + * Prints the neighborhoods contained in the topology. */ virtual void printOn(){} }; diff --git a/eo/src/eoVelocityInit.h b/eo/src/eoVelocityInit.h index de98d3d9..a8000d6d 100644 --- a/eo/src/eoVelocityInit.h +++ b/eo/src/eoVelocityInit.h @@ -44,16 +44,14 @@ public: /** -* Provide a particle initialized thanks to the eoVelocityInit object given. +* Provides a particle initialized thanks to the eoVelocityInit object given. */ template < class POT > class eoVelocityInitGenerator:public eoF < POT > { public: /** Ctor from a plain eoVelocityInit */ - eoVelocityInitGenerator (eoVelocityInit < POT > &_init):init (_init) - { - } + eoVelocityInitGenerator (eoVelocityInit < POT > &_init):init (_init){} virtual POT operator () () { @@ -61,6 +59,7 @@ public: init (p); return (p); } + private: eoVelocityInit < POT > &init; }; diff --git a/eo/test/t-eoEasyPSO.cpp b/eo/test/t-eoEasyPSO.cpp index d8abc3e7..d5c2f3db 100644 --- a/eo/test/t-eoEasyPSO.cpp +++ b/eo/test/t-eoEasyPSO.cpp @@ -69,7 +69,7 @@ int main() eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5); // velocity - eoStandardVelocity velocity (topology,1.6,2,bnds); + eoStandardVelocity velocity (topology,1,1.6,2,bnds); // flight eoStandardFlight flight;