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
#include <eoVelocity.h>
#include <eoStandardVelocity.h>
#include <eoIntegerVelocity.h>
#include <eoConstrictedVelocity.h>
#include <eoFixedInertiaWeightedVelocity.h>
#include <eoVariableInertiaWeightedVelocity.h>

View file

@ -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 EOT> class eoEasyEA: public eoAlgo<EOT> {
public:
/** Ctor taking a breed and merge */
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _replace
) : continuator(_continuator),
eval (_eval),
loopEval(_eval),
popEval(loopEval),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
/*
eoEasyEA(eoContinue <EOT> & _continuator,
eoPopEvalFunc <EOT> & _pop_eval,
eoBreed <EOT> & _breed,
eoReplacement <EOT> & _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<EOT>& _continuator,
eoPopEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _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<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoMerge<EOT>& _merge,
eoReduce<EOT>& _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<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoReplacement<EOT>& _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<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoMerge<EOT>& _merge,
eoReduce<EOT>& _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<EOT>& _pop)
template<class EOT> class eoEasyEA: public eoAlgo<EOT>
{
eoPop<EOT> offspring, empty_pop;
public:
popEval(empty_pop, _pop); // A first eval of pop.
/** Ctor taking a breed and merge */
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _replace
) : continuator(_continuator),
eval (_eval),
loopEval(_eval),
popEval(loopEval),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{}
do
/*
eoEasyEA(eoContinue <EOT> & _continuator,
eoPopEvalFunc <EOT> & _pop_eval,
eoBreed <EOT> & _breed,
eoReplacement <EOT> & _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<EOT>& _continuator,
eoPopEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _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<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.
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoMerge<EOT>& _merge,
eoReduce<EOT>& _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<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoReplacement<EOT>& _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<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoSelect<EOT>& _select,
eoTransform<EOT>& _transform,
eoMerge<EOT>& _merge,
eoReduce<EOT>& _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<EOT>& _pop)
{
try
{
unsigned pSize = _pop.size();
offspring.clear(); // new offspring
eoPop<EOT> 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<EOT>
{ public : void operator()(const eoPop<EOT>&, eoPop<EOT>&) {} } dummySelect;
{
public :
void operator()(const eoPop<EOT>&, eoPop<EOT>&)
{}
}
dummySelect;
class eoDummyTransform : public eoTransform<EOT>
{ public : void operator()(eoPop<EOT>&) {} } dummyTransform;
class eoDummyTransform : public eoTransform<EOT>
{
public :
void operator()(eoPop<EOT>&)
{}
}
dummyTransform;
class eoDummyEval : public eoEvalFunc<EOT>
{public: void operator()(EOT &) {} } dummyEval;
{
public:
void operator()(EOT &)
{}
}
dummyEval;
eoContinue<EOT>& continuator;
eoContinue<EOT>& continuator;
eoEvalFunc <EOT> & eval ;
eoPopLoopEval<EOT> loopEval;
eoEvalFunc <EOT> & eval ;
eoPopLoopEval<EOT> loopEval;
eoPopEvalFunc<EOT>& popEval;
eoPopEvalFunc<EOT>& popEval;
eoSelectTransform<EOT> selectTransform;
eoBreed<EOT>& breed;
eoSelectTransform<EOT> selectTransform;
eoBreed<EOT>& breed;
// If mergeReduce needs not be used, dummyMerge and dummyReduce are used
// to instantiate it.
eoNoElitism<EOT> dummyMerge;
eoTruncate<EOT> dummyReduce;
// If mergeReduce needs not be used, dummyMerge and dummyReduce are used
// to instantiate it.
eoNoElitism<EOT> dummyMerge;
eoTruncate<EOT> dummyReduce;
eoMergeReduce<EOT> mergeReduce;
eoReplacement<EOT>& replace;
eoMergeReduce<EOT> mergeReduce;
eoReplacement<EOT>& replace;
// Friend classes
friend class eoIslandsEasyEA <EOT> ;
friend class eoDistEvalEasyEA <EOT> ;
};
// Friend classes
friend class eoIslandsEasyEA <EOT> ;
friend class eoDistEvalEasyEA <EOT> ;
};
//-----------------------------------------------------------------------------

View file

@ -31,19 +31,22 @@
#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 :
{
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 POT> class eoInitializer : public eoInitializerBase <POT>
{
public:
{
public:
//! Constructor
//! @param _proc Evaluation function
@ -60,25 +63,54 @@ public:
//! @param _initBest Initialization of the best
//! @param _pop Population
eoInitializer(
eoUF<POT&, void>& _proc,
eoVelocityInit < POT > &_initVelo,
eoParticleBestInit <POT> &_initBest,
eoPop < POT > &_pop
) : proc(_proc), initVelo(_initVelo), initBest(_initBest)
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)
{
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 <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 > &_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 :
/*
@param proc First evaluation
@ -86,10 +118,28 @@ private :
@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

View file

@ -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<POT> getTopology
//! eoTopology<POT> getTopology
//! @return topology
eoTopology<POT> & 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;

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,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 <POT>
{
@ -44,14 +46,15 @@ template < class POT > class eoRingTopology:public eoTopology <POT>
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
*/

View file

@ -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;

View file

@ -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 <POT>
{

View file

@ -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 <POT> &_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 <POT> &_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 <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).
virtual void operator () (eoPop < POT > &_pop)
{
try
try
{
// just to use a loop eval
eoPop<POT> empty_pop;
init();
// just to use a loop eval
eoPop<POT> 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 <POT> &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<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;
};
#endif /*_EOSYNCEASYPSO_H*/

View file

@ -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<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;
/**
* 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<POT>& _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(){}
};

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 >
{
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;
};

View file

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