corrected PSO dummy errors and completed documentation

This commit is contained in:
tlegrand 2008-01-08 14:50:13 +00:00
commit 4b99bc8bc9
12 changed files with 581 additions and 281 deletions

View file

@ -162,6 +162,7 @@
// velocities // velocities
#include <eoVelocity.h> #include <eoVelocity.h>
#include <eoStandardVelocity.h> #include <eoStandardVelocity.h>
#include <eoIntegerVelocity.h>
#include <eoConstrictedVelocity.h> #include <eoConstrictedVelocity.h>
#include <eoFixedInertiaWeightedVelocity.h> #include <eoFixedInertiaWeightedVelocity.h>
#include <eoVariableInertiaWeightedVelocity.h> #include <eoVariableInertiaWeightedVelocity.h>

View file

@ -60,7 +60,8 @@ Change (MS, July 3. 2001):
Note: it looks ugly only because we wanted to authorize many different Note: it looks ugly only because we wanted to authorize many different
constructors. Please only look at the operator() and there shall be light constructors. Please only look at the operator() and there shall be light
*/ */
template<class EOT> class eoEasyEA: public eoAlgo<EOT> { template<class EOT> class eoEasyEA: public eoAlgo<EOT>
{
public: public:
/** Ctor taking a breed and merge */ /** Ctor taking a breed and merge */
@ -113,6 +114,24 @@ public:
replace(_replace) replace(_replace)
{} {}
/// Ctor eoSelect, eoTransform, eoReplacement and an eoPopEval
eoEasyEA(
eoContinue<EOT>& _continuator,
eoPopEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoReplacement<EOT>& _replace
) : continuator(_continuator),
eval (dummyEval),
loopEval(dummyEval),
popEval(_eval),
selectTransform(_select, _transform),
breed(selectTransform),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
/// Ctor eoBreed, eoMerge and eoReduce. /// Ctor eoBreed, eoMerge and eoReduce.
eoEasyEA( eoEasyEA(
eoContinue<EOT>& _continuator, eoContinue<EOT>& _continuator,
@ -200,7 +219,8 @@ public:
s.append( " in eoEasyEA"); s.append( " in eoEasyEA");
throw std::runtime_error( s ); throw std::runtime_error( s );
} }
} while ( continuator( _pop ) ); }
while ( continuator( _pop ) );
} }
protected : protected :
@ -208,13 +228,28 @@ protected :
// If selectTransform needs not be used, dummySelect and dummyTransform are used // If selectTransform needs not be used, dummySelect and dummyTransform are used
// to instantiate it. // to instantiate it.
class eoDummySelect : public eoSelect<EOT> class eoDummySelect : public eoSelect<EOT>
{ public : void operator()(const eoPop<EOT>&, eoPop<EOT>&) {} } dummySelect; {
public :
void operator()(const eoPop<EOT>&, eoPop<EOT>&)
{}
}
dummySelect;
class eoDummyTransform : public eoTransform<EOT> class eoDummyTransform : public eoTransform<EOT>
{ public : void operator()(eoPop<EOT>&) {} } dummyTransform; {
public :
void operator()(eoPop<EOT>&)
{}
}
dummyTransform;
class eoDummyEval : public eoEvalFunc<EOT> class eoDummyEval : public eoEvalFunc<EOT>
{public: void operator()(EOT &) {} } dummyEval; {
public:
void operator()(EOT &)
{}
}
dummyEval;
eoContinue<EOT>& continuator; eoContinue<EOT>& continuator;

View file

@ -31,6 +31,7 @@
#include <eoVelocityInit.h> #include <eoVelocityInit.h>
#include <eoPop.h> #include <eoPop.h>
#include <eoParticleBestInit.h> #include <eoParticleBestInit.h>
#include <eoTopology.h>
/* /*
@ -40,9 +41,11 @@ template <class POT> class eoInitializerBase : public eoFunctorBase
{ {
public : public :
virtual ~eoInitializerBase() {} virtual ~eoInitializerBase()
{}
virtual void operator()(){}; virtual void operator()()
{};
}; };
/** /**
@ -63,14 +66,31 @@ public:
eoUF<POT&, void>& _proc, eoUF<POT&, void>& _proc,
eoVelocityInit < POT > &_initVelo, eoVelocityInit < POT > &_initVelo,
eoParticleBestInit <POT> &_initBest, eoParticleBestInit <POT> &_initBest,
eoTopology <POT> &_topology,
eoPop < POT > &_pop eoPop < POT > &_pop
) : proc(_proc), initVelo(_initVelo), initBest(_initBest) ) : proc(_proc), procPara(dummyEval), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop)
{ {
apply(proc, _pop); /* apply(proc, _pop);
apply < POT > (initVelo, _pop); apply < POT > (initVelo, _pop);
apply < POT > (initBest, _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 <POT>& _proc,
eoVelocityInit < POT > &_initVelo,
eoParticleBestInit <POT> &_initBest,
eoTopology <POT> &_topology,
eoPop < POT > &_pop
) : proc(dummy), procPara(_proc), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop)
{}
//! Give the name of the class //! Give the name of the class
//! @return The name of the class //! @return The name of the class
virtual std::string className (void) const virtual std::string className (void) const
@ -78,6 +98,18 @@ public:
return "eoInitializer"; return "eoInitializer";
} }
virtual void operator () (/*eoPop < POT > &_pop*/)
{
eoPop<POT> empty_pop;
apply(proc, pop);
procPara(empty_pop, pop);
apply < POT > (initVelo, pop);
apply < POT > (initBest, pop);
topology.setup(pop);
}
private : private :
/* /*
@ -86,9 +118,27 @@ private :
@param initBest Initialization of the best @param initBest Initialization of the best
*/ */
eoPop < POT > & pop;
eoUF<POT&, void>& proc; eoUF<POT&, void>& proc;
eoPopEvalFunc <POT>& procPara;
eoVelocityInit < POT > & initVelo; eoVelocityInit < POT > & initVelo;
eoParticleBestInit <POT> & initBest; eoParticleBestInit <POT> & initBest;
eoTopology <POT> & topology;
class eoDummyEval : public eoPopEvalFunc<POT>
{
public:
void operator()(eoPop<POT> &,eoPop<POT> &_pop)
{}
}
dummyEval;
class eoDummy : public eoUF<POT&, void>
{
public:
void operator()(POT &)
{}
}
dummy;
}; };
#endif #endif

View file

@ -18,7 +18,8 @@
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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, /** Integer 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) )) * 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]). * (ci given and Ri chosen at random in [0;1]).
*/ */
template < class POT > class eoIntegerVelocity:public eoVelocity < POT > template < class POT > class eoIntegerVelocity:public eoVelocity < POT >
@ -51,8 +53,9 @@ public:
/** Full constructor: Bounds and bound modifier required /** Full constructor: Bounds and bound modifier required
* @param _topology - The topology to get the global/local/other best * @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 _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 local/global best(s). 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. * @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 ? * 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 _boundsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
@ -61,12 +64,14 @@ public:
eoIntegerVelocity (eoTopology < POT > & _topology, eoIntegerVelocity (eoTopology < POT > & _topology,
const VelocityType & _c1, const VelocityType & _c1,
const VelocityType & _c2, const VelocityType & _c2,
const VelocityType & _c3,
eoRealVectorBounds & _bounds, eoRealVectorBounds & _bounds,
eoRealBoundModifier & _bndsModifier, eoRealBoundModifier & _bndsModifier,
eoRng & _gen = rng): eoRng & _gen = rng):
topology(_topology), topology(_topology),
c1 (_c1), c1 (_c1),
c2 (_c2), c2 (_c2),
c3 (_c3),
bounds(_bounds), bounds(_bounds),
bndsModifier(_bndsModifier), bndsModifier(_bndsModifier),
gen(_gen){} gen(_gen){}
@ -74,8 +79,9 @@ public:
/** Constructor: No bound updater required <-> fixed bounds /** Constructor: No bound updater required <-> fixed bounds
* @param _topology - The topology to get the global/local/other best * @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 _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 local/global best(s). 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. * @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 ? * 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 * @param _gen - The eo random generator, default=rng
@ -83,29 +89,33 @@ public:
eoIntegerVelocity (eoTopology < POT > & _topology, eoIntegerVelocity (eoTopology < POT > & _topology,
const VelocityType & _c1, const VelocityType & _c1,
const VelocityType & _c2, const VelocityType & _c2,
const VelocityType & _c3,
eoRealVectorBounds & _bounds, eoRealVectorBounds & _bounds,
eoRng & _gen = rng): eoRng & _gen = rng):
topology(_topology), topology(_topology),
c1 (_c1), c1 (_c1),
c2 (_c2), c2 (_c2),
c3 (_c3),
bounds(_bounds), bounds(_bounds),
bndsModifier(dummyModifier), bndsModifier(dummyModifier),
gen(_gen){} gen(_gen){}
/** Constructor: Neither bounds nor bound updater required <-> free velocity /** 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 quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType
* @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 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 _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 * @param _gen - The eo random generator, default=rng
*/ */
eoIntegerVelocity (eoTopology < POT > & _topology, eoIntegerVelocity (eoTopology < POT > & _topology,
const VelocityType & _c1, const VelocityType & _c1,
const VelocityType & _c2, const VelocityType & _c2,
const VelocityType & _c3,
eoRng & _gen = rng): eoRng & _gen = rng):
topology(_topology), topology(_topology),
c1 (_c1), c1 (_c1),
c2 (_c2), c2 (_c2),
c3 (_c3),
bounds(*(new eoRealVectorNoBounds(0))), bounds(*(new eoRealVectorNoBounds(0))),
bndsModifier(dummyModifier), bndsModifier(dummyModifier),
gen(_gen) gen(_gen)
@ -120,14 +130,14 @@ public:
*/ */
void operator () (POT & _po,unsigned _indice) void operator () (POT & _po,unsigned _indice)
{ {
VelocityType r1;
VelocityType r2; VelocityType r2;
VelocityType r3;
VelocityType newVelocity; VelocityType newVelocity;
// cast the learning factors to VelocityType // cast the learning factors to VelocityType
r1 = (VelocityType) rng.uniform (1) * c1;
r2 = (VelocityType) rng.uniform (1) * c2; 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 // need to resize the bounds even if there are dummy because of "isBounded" call
bounds.adjust_size(_po.size()); bounds.adjust_size(_po.size());
@ -135,7 +145,7 @@ public:
// assign the new velocities // assign the new velocities
for (unsigned j = 0; j < _po.size (); j++) 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 */ /* check bounds */
if (bounds.isMinBounded(j)) if (bounds.isMinBounded(j))
@ -165,8 +175,9 @@ public:
protected: protected:
eoTopology < POT > & topology; eoTopology < POT > & topology;
const VelocityType & c1; // learning factor 1 const VelocityType & c1; // social/cognitive coefficient
const VelocityType & c2; // learning factor 2 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. eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type.
eoRealBoundModifier & bndsModifier; eoRealBoundModifier & bndsModifier;

View file

@ -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 <utils/eoRealVectorBounds.h>
#include <eoVelocityInit.h>
#include <eoPop.h>
#include <eoParticleBestInit.h>
#include <eoTopology.h>
/*
* Abstract class for initialization of algorithm PSO
*/
template <class POT> class eoInitializerBase : public eoFunctorBase
{
public :
virtual ~eoInitializerBase()
{}
virtual void operator()()
{};
};
/**
Base (name) class for Initialization of algorithm PSO
@see eoInitializerBase eoUF apply
*/
template <class POT> class eoParticleInitializer : public eoInitializerBase <POT>
{
public:
//! Constructor
//! @param _proc Evaluation function
//! @param _initVelo Initialization of the velocity
//! @param _initBest Initialization of the best
//! @param _pop Population
eoParticleFullInitializer(
eoUF<POT&, void>& _proc,
eoVelocityInit < POT > &_initVelo,
eoParticleBestInit <POT> &_initBest,
eoTopology <POT> &_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 <POT>& _proc,
eoVelocityInit < POT > &_initVelo,
eoParticleBestInit <POT> &_initBest,
eoTopology <POT> &_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> 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<POT&, void>& proc;
eoPopEvalFunc <POT>& procPara;
eoVelocityInit < POT > & initVelo;
eoParticleBestInit <POT> & initBest;
eoTopology <POT> & topology;
class eoDummyEval : public eoPopEvalFunc<POT>
{
public:
void operator()(eoPop<POT> &,eoPop<POT> &_pop)
{}
}
dummyEval;
class eoDummy : public eoUF<POT&, void>
{
public:
void operator()(POT &)
{}
}
dummy;
};
#endif /*_eoParticleFullInitializer_H*/

View file

@ -5,6 +5,7 @@
// (c) INRIA Futurs DOLPHIN 2007 // (c) INRIA Futurs DOLPHIN 2007
/* /*
Clive Canape Clive Canape
Thomas Legrand
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -34,9 +35,10 @@
/** /**
* Topology dedicated to "globest best" strategy for particle swarm optimization. * Static ring topology for particle swarm optimization.
* All the particles of the swarm belong to the same and only social neighborhood. * The neighborhoods are built using a ring based on each particle's indice and
* The global best is stored as a protected member and updated by using the "updateNeighborhood" method. * 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 <POT> template < class POT > class eoRingTopology:public eoTopology <POT>
{ {
@ -44,14 +46,15 @@ template < class POT > class eoRingTopology:public eoTopology <POT>
public: public:
/** /**
* The only Ctor. No parameter required. * The only Ctor.
* @param _neighborhoodSize - The size of each neighborhood.
*/ */
eoRingTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){} eoRingTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){}
/** /**
* Builds the only neighborhood that contains all the particles of the given population. * Builds the neighborhoods using a ring strategy based on the particle indices.
* Also initializes the global best particle with the best particle of the given population. * Also initializes the best particle of each neighborhood.
* @param _pop - The population used to build the only neighborhood. * @param _pop - The population used to build the only neighborhood.
* @return * @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) * @return _indice - The particle indice (in the population)
*/ */
unsigned retrieveNeighborhoodByIndice(unsigned _indice) unsigned retrieveNeighborhoodByIndice(unsigned _indice)
@ -97,8 +100,8 @@ public:
/** /**
* Update the best fitness of the given particle if it's better. * Updates the best fitness of the given particle and
* Also replace the global best by the given particle if it's better. * potentially replaces the local best the given particle it's better.
* @param _po - The particle to update * @param _po - The particle to update
* @param _indice - The indice of the given particle in the population * @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 * @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 * @return POT & - The best particle in the neighborhood of the particle whose indice is _indice
*/ */

View file

@ -37,7 +37,7 @@
/** Standard velocity performer for particle swarm optimization. Derivated from abstract eoVelocity, /** 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]). * (ci given and Ri chosen at random in [0;1]).
*/ */
template < class POT > class eoStandardVelocity:public eoVelocity < POT > template < class POT > class eoStandardVelocity:public eoVelocity < POT >
@ -52,8 +52,9 @@ public:
/** Full constructor: Bounds and bound modifier required /** Full constructor: Bounds and bound modifier required
* @param _topology - The topology to get the global/local/other best * @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 _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 local/global best(s). 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. * @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 ? * 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 _boundsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
@ -62,12 +63,14 @@ public:
eoStandardVelocity (eoTopology < POT > & _topology, eoStandardVelocity (eoTopology < POT > & _topology,
const VelocityType & _c1, const VelocityType & _c1,
const VelocityType & _c2, const VelocityType & _c2,
const VelocityType & _c3,
eoRealVectorBounds & _bounds, eoRealVectorBounds & _bounds,
eoRealBoundModifier & _bndsModifier, eoRealBoundModifier & _bndsModifier,
eoRng & _gen = rng): eoRng & _gen = rng):
topology(_topology), topology(_topology),
c1 (_c1), c1 (_c1),
c2 (_c2), c2 (_c2),
c3 (_c3),
bounds(_bounds), bounds(_bounds),
bndsModifier(_bndsModifier), bndsModifier(_bndsModifier),
gen(_gen){} gen(_gen){}
@ -75,8 +78,9 @@ public:
/** Constructor: No bound updater required <-> fixed bounds /** Constructor: No bound updater required <-> fixed bounds
* @param _topology - The topology to get the global/local/other best * @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 _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 local/global best(s). 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. * @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 ? * 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 * @param _gen - The eo random generator, default=rng
@ -84,29 +88,33 @@ public:
eoStandardVelocity (eoTopology < POT > & _topology, eoStandardVelocity (eoTopology < POT > & _topology,
const VelocityType & _c1, const VelocityType & _c1,
const VelocityType & _c2, const VelocityType & _c2,
const VelocityType & _c3,
eoRealVectorBounds & _bounds, eoRealVectorBounds & _bounds,
eoRng & _gen = rng): eoRng & _gen = rng):
topology(_topology), topology(_topology),
c1 (_c1), c1 (_c1),
c2 (_c2), c2 (_c2),
c3 (_c3),
bounds(_bounds), bounds(_bounds),
bndsModifier(dummyModifier), bndsModifier(dummyModifier),
gen(_gen){} gen(_gen){}
/** Constructor: Neither bounds nor bound updater required <-> free velocity /** 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 quantify how much the particle trusts itself. Type must be POT::ParticleVelocityType
* @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 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 _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 * @param _gen - The eo random generator, default=rng
*/ */
eoStandardVelocity (eoTopology < POT > & _topology, eoStandardVelocity (eoTopology < POT > & _topology,
const VelocityType & _c1, const VelocityType & _c1,
const VelocityType & _c2, const VelocityType & _c2,
const VelocityType & _c3,
eoRng & _gen = rng): eoRng & _gen = rng):
topology(_topology), topology(_topology),
c1 (_c1), c1 (_c1),
c2 (_c2), c2 (_c2),
c3 (_c3),
bounds(*(new eoRealVectorNoBounds(0))), bounds(*(new eoRealVectorNoBounds(0))),
bndsModifier(dummyModifier), bndsModifier(dummyModifier),
gen(_gen) gen(_gen)
@ -121,14 +129,14 @@ public:
*/ */
void operator () (POT & _po,unsigned _indice) void operator () (POT & _po,unsigned _indice)
{ {
VelocityType r1;
VelocityType r2; VelocityType r2;
VelocityType r3;
VelocityType newVelocity; VelocityType newVelocity;
// cast the learning factors to VelocityType // cast the learning factors to VelocityType
r1 = (VelocityType) rng.uniform (1) * c1;
r2 = (VelocityType) rng.uniform (1) * c2; 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 // need to resize the bounds even if there are dummy because of "isBounded" call
bounds.adjust_size(_po.size()); bounds.adjust_size(_po.size());
@ -136,7 +144,7 @@ public:
// assign the new velocities // assign the new velocities
for (unsigned j = 0; j < _po.size (); j++) 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 */ /* check bounds */
if (bounds.isMinBounded(j)) if (bounds.isMinBounded(j))
@ -166,8 +174,9 @@ public:
protected: protected:
eoTopology < POT > & topology; eoTopology < POT > & topology;
const VelocityType & c1; // learning factor 1 const VelocityType & c1; // social/cognitive coefficient
const VelocityType & c2; // learning factor 2 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. eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type.
eoRealBoundModifier & bndsModifier; eoRealBoundModifier & bndsModifier;

View file

@ -35,7 +35,7 @@
/** /**
* Topology dedicated to "globest best" strategy for particle swarm optimization. * Topology dedicated to "globest best" strategy for particle swarm optimization.
* All the particles of the swarm belong to the same and only social neighborhood. * 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 <POT> template < class POT > class eoStarTopology:public eoTopology <POT>
{ {

View file

@ -55,16 +55,19 @@ public:
* to modify the positions according to the velocities * to modify the positions according to the velocities
*/ */
eoSyncEasyPSO ( eoSyncEasyPSO (
eoInitializer <POT> &_init,
eoContinue < POT > &_continuator, eoContinue < POT > &_continuator,
eoEvalFunc < POT > &_eval, eoEvalFunc < POT > &_eval,
eoVelocity < POT > &_velocity, eoVelocity < POT > &_velocity,
eoFlight < POT > &_flight): eoFlight < POT > &_flight):
init(_init),
continuator (_continuator), continuator (_continuator),
eval (_eval), eval (_eval),
loopEval(_eval), loopEval(_eval),
popEval(loopEval), popEval(loopEval),
velocity (_velocity), velocity (_velocity),
flight (_flight){} flight (_flight)
{}
/** Constructor without eoFlight. For special cases when the flight is performed withing the velocity. /** Constructor without eoFlight. For special cases when the flight is performed withing the velocity.
@ -73,28 +76,54 @@ public:
* @param _velocity - An eoVelocity that defines how to compute the velocities * @param _velocity - An eoVelocity that defines how to compute the velocities
*/ */
eoSyncEasyPSO ( eoSyncEasyPSO (
eoInitializer <POT> &_init,
eoContinue < POT > &_continuator, eoContinue < POT > &_continuator,
eoEvalFunc < POT > &_eval, eoEvalFunc < POT > &_eval,
eoVelocity < POT > &_velocity): eoVelocity < POT > &_velocity):
init(_init),
continuator (_continuator), continuator (_continuator),
eval (_eval), eval (_eval),
loopEval(_eval), loopEval(_eval),
popEval(loopEval), popEval(loopEval),
velocity (_velocity), velocity (_velocity),
flight (dummyFlight){} 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 <POT> &_init,
eoContinue < POT > &_continuator,
eoPopEvalFunc < POT > &_eval,
eoVelocity < POT > &_velocity,
eoFlight <POT> &_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). /// Apply a few iteration of flight to the population (=swarm).
virtual void operator () (eoPop < POT > &_pop) virtual void operator () (eoPop < POT > &_pop)
{ {
try try
{ {
init();
// just to use a loop eval // just to use a loop eval
eoPop<POT> empty_pop; eoPop<POT> empty_pop;
do do
{ {
// perform velocity evaluation // perform velocity evaluation
velocity.apply (_pop); velocity.apply (_pop);
@ -102,13 +131,13 @@ public:
flight.apply (_pop); flight.apply (_pop);
// evaluate the position (with a loop eval, empty_swarm IS USELESS) // evaluate the position (with a loop eval, empty_swarm IS USELESS)
loopEval(empty_pop,_pop); popEval(empty_pop,_pop);
// update the topology (particle and local/global best(s)) // update the topology (particle and local/global best(s))
velocity.updateNeighborhood(_pop); velocity.updateNeighborhood(_pop);
}
}while (continuator (_pop)); while (continuator (_pop));
} }
catch (std::exception & e) catch (std::exception & e)
@ -121,6 +150,8 @@ public:
} }
private: private:
eoInitializer <POT> &init;
eoContinue < POT > &continuator; eoContinue < POT > &continuator;
eoEvalFunc < POT > &eval; eoEvalFunc < POT > &eval;
@ -132,6 +163,16 @@ private:
// if the flight does not need to be used, use the dummy flight instance // if the flight does not need to be used, use the dummy flight instance
eoDummyFlight<POT> dummyFlight; eoDummyFlight<POT> dummyFlight;
// if the eval does not need to be used, use the dummy eval instance
class eoDummyEval : public eoEvalFunc<POT>
{
public:
void operator()(POT &)
{}
}
dummyEval;
}; };

View file

@ -18,7 +18,7 @@
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 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 > template < class POT > class eoTopology:public eoPop < POT >
{ {
public: public:
/** /**
* Build the neighborhoods contained in the topology. * Builds the neighborhoods contained in the topology.
*/ */
virtual void setup(const eoPop<POT> &)=0; virtual void setup(const eoPop<POT> &)=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; 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) virtual void updateNeighborhood(eoPop < POT > &_pop)
{ {
for (unsigned i = 0; i < _pop.size (); i++) for (unsigned i = 0; i < _pop.size (); i++)
{
updateNeighborhood(_pop[i],i); updateNeighborhood(_pop[i],i);
} }
}
/** /**
* Build the neighborhoods contained in the topology. * Builds the neighborhoods contained in the topology.
*/ */
virtual POT & best (unsigned ) = 0; 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<POT>& _pop) virtual POT & globalBest(const eoPop<POT>& _pop)
{ {
POT globalBest,tmp; POT globalBest,tmp;
@ -88,8 +91,7 @@ public:
} }
/** /**
* Build the neighborhoods contained in the topology. * Prints the neighborhoods contained in the topology.
* @param _pop - The population ton share between the neighborhood(s)
*/ */
virtual void printOn(){} virtual void printOn(){}
}; };

View file

@ -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 > template < class POT > class eoVelocityInitGenerator:public eoF < POT >
{ {
public: public:
/** Ctor from a plain eoVelocityInit */ /** Ctor from a plain eoVelocityInit */
eoVelocityInitGenerator (eoVelocityInit < POT > &_init):init (_init) eoVelocityInitGenerator (eoVelocityInit < POT > &_init):init (_init){}
{
}
virtual POT operator () () virtual POT operator () ()
{ {
@ -61,6 +59,7 @@ public:
init (p); init (p);
return (p); return (p);
} }
private: private:
eoVelocityInit < POT > &init; eoVelocityInit < POT > &init;
}; };

View file

@ -69,7 +69,7 @@ int main()
eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5); eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
// velocity // velocity
eoStandardVelocity <Particle> velocity (topology,1.6,2,bnds); eoStandardVelocity <Particle> velocity (topology,1,1.6,2,bnds);
// flight // flight
eoStandardFlight <Particle> flight; eoStandardFlight <Particle> flight;