corrected PSO dummy errors and completed documentation
This commit is contained in:
parent
b8da2db21f
commit
4b99bc8bc9
12 changed files with 581 additions and 281 deletions
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -60,184 +60,219 @@ 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 */
|
||||||
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(
|
eoEasyEA(
|
||||||
eoContinue<EOT>& _continuator,
|
eoContinue<EOT>& _continuator,
|
||||||
eoEvalFunc<EOT>& _eval,
|
eoEvalFunc<EOT>& _eval,
|
||||||
eoBreed<EOT>& _breed,
|
eoBreed<EOT>& _breed,
|
||||||
eoMerge<EOT>& _merge,
|
eoReplacement<EOT>& _replace
|
||||||
eoReduce<EOT>& _reduce
|
) : continuator(_continuator),
|
||||||
) : continuator(_continuator),
|
eval (_eval),
|
||||||
eval (_eval),
|
loopEval(_eval),
|
||||||
loopEval(_eval),
|
popEval(loopEval),
|
||||||
popEval(loopEval),
|
selectTransform(dummySelect, dummyTransform),
|
||||||
selectTransform(dummySelect, dummyTransform),
|
breed(_breed),
|
||||||
breed(_breed),
|
mergeReduce(dummyMerge, dummyReduce),
|
||||||
mergeReduce(_merge, _reduce),
|
replace(_replace)
|
||||||
replace(mergeReduce)
|
{}
|
||||||
{}
|
|
||||||
|
|
||||||
/// Ctor eoSelect, eoTransform, and eoReplacement
|
/*
|
||||||
|
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(
|
eoEasyEA(
|
||||||
eoContinue<EOT>& _continuator,
|
eoContinue<EOT>& _continuator,
|
||||||
eoEvalFunc<EOT>& _eval,
|
eoPopEvalFunc<EOT>& _eval,
|
||||||
eoSelect<EOT>& _select,
|
eoBreed<EOT>& _breed,
|
||||||
eoTransform<EOT>& _transform,
|
eoReplacement<EOT>& _replace
|
||||||
eoReplacement<EOT>& _replace
|
) : continuator(_continuator),
|
||||||
) : continuator(_continuator),
|
eval (dummyEval),
|
||||||
eval (_eval),
|
loopEval(dummyEval),
|
||||||
loopEval(_eval),
|
popEval(_eval),
|
||||||
popEval(loopEval),
|
selectTransform(dummySelect, dummyTransform),
|
||||||
selectTransform(_select, _transform),
|
breed(_breed),
|
||||||
breed(selectTransform),
|
mergeReduce(dummyMerge, dummyReduce),
|
||||||
mergeReduce(dummyMerge, dummyReduce),
|
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.
|
||||||
|
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.
|
/// Ctor eoSelect, eoTransform, eoMerge and eoReduce.
|
||||||
eoEasyEA(
|
eoEasyEA(
|
||||||
eoContinue<EOT>& _continuator,
|
eoContinue<EOT>& _continuator,
|
||||||
eoEvalFunc<EOT>& _eval,
|
eoEvalFunc<EOT>& _eval,
|
||||||
eoSelect<EOT>& _select,
|
eoSelect<EOT>& _select,
|
||||||
eoTransform<EOT>& _transform,
|
eoTransform<EOT>& _transform,
|
||||||
eoMerge<EOT>& _merge,
|
eoMerge<EOT>& _merge,
|
||||||
eoReduce<EOT>& _reduce
|
eoReduce<EOT>& _reduce
|
||||||
) : continuator(_continuator),
|
) : continuator(_continuator),
|
||||||
eval (_eval),
|
eval (_eval),
|
||||||
loopEval(_eval),
|
loopEval(_eval),
|
||||||
popEval(loopEval),
|
popEval(loopEval),
|
||||||
selectTransform(_select, _transform),
|
selectTransform(_select, _transform),
|
||||||
breed(selectTransform),
|
breed(selectTransform),
|
||||||
mergeReduce(_merge, _reduce),
|
mergeReduce(_merge, _reduce),
|
||||||
replace(mergeReduce)
|
replace(mergeReduce)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Apply a few generation of evolution to the population.
|
/// Apply a few generation of evolution to the population.
|
||||||
virtual void operator()(eoPop<EOT>& _pop)
|
virtual void operator()(eoPop<EOT>& _pop)
|
||||||
{
|
|
||||||
eoPop<EOT> offspring, empty_pop;
|
|
||||||
|
|
||||||
popEval(empty_pop, _pop); // A first eval of pop.
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
try
|
eoPop<EOT> offspring, empty_pop;
|
||||||
{
|
|
||||||
unsigned pSize = _pop.size();
|
|
||||||
offspring.clear(); // new offspring
|
|
||||||
|
|
||||||
breed(_pop, offspring);
|
popEval(empty_pop, _pop); // A first eval of pop.
|
||||||
|
|
||||||
popEval(_pop, offspring); // eval of parents + offspring if necessary
|
do
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
unsigned pSize = _pop.size();
|
||||||
|
offspring.clear(); // new offspring
|
||||||
|
|
||||||
replace(_pop, offspring); // after replace, the new pop. is in _pop
|
breed(_pop, offspring);
|
||||||
|
|
||||||
if (pSize > _pop.size())
|
popEval(_pop, offspring); // eval of parents + offspring if necessary
|
||||||
throw std::runtime_error("Population shrinking!");
|
|
||||||
else if (pSize < _pop.size())
|
|
||||||
throw std::runtime_error("Population growing!");
|
|
||||||
|
|
||||||
}
|
replace(_pop, offspring); // after replace, the new pop. is in _pop
|
||||||
catch (std::exception& e)
|
|
||||||
{
|
|
||||||
std::string s = e.what();
|
|
||||||
s.append( " in eoEasyEA");
|
|
||||||
throw std::runtime_error( s );
|
|
||||||
}
|
|
||||||
} while ( continuator( _pop ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
protected :
|
if (pSize > _pop.size())
|
||||||
|
throw std::runtime_error("Population shrinking!");
|
||||||
|
else if (pSize < _pop.size())
|
||||||
|
throw std::runtime_error("Population growing!");
|
||||||
|
|
||||||
// If selectTransform needs not be used, dummySelect and dummyTransform are used
|
}
|
||||||
// to instantiate it.
|
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>
|
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;
|
||||||
|
|
||||||
eoEvalFunc <EOT> & eval ;
|
eoEvalFunc <EOT> & eval ;
|
||||||
eoPopLoopEval<EOT> loopEval;
|
eoPopLoopEval<EOT> loopEval;
|
||||||
|
|
||||||
eoPopEvalFunc<EOT>& popEval;
|
eoPopEvalFunc<EOT>& popEval;
|
||||||
|
|
||||||
eoSelectTransform<EOT> selectTransform;
|
eoSelectTransform<EOT> selectTransform;
|
||||||
eoBreed<EOT>& breed;
|
eoBreed<EOT>& breed;
|
||||||
|
|
||||||
// If mergeReduce needs not be used, dummyMerge and dummyReduce are used
|
// If mergeReduce needs not be used, dummyMerge and dummyReduce are used
|
||||||
// to instantiate it.
|
// to instantiate it.
|
||||||
eoNoElitism<EOT> dummyMerge;
|
eoNoElitism<EOT> dummyMerge;
|
||||||
eoTruncate<EOT> dummyReduce;
|
eoTruncate<EOT> dummyReduce;
|
||||||
|
|
||||||
eoMergeReduce<EOT> mergeReduce;
|
eoMergeReduce<EOT> mergeReduce;
|
||||||
eoReplacement<EOT>& replace;
|
eoReplacement<EOT>& replace;
|
||||||
|
|
||||||
// Friend classes
|
// Friend classes
|
||||||
friend class eoIslandsEasyEA <EOT> ;
|
friend class eoIslandsEasyEA <EOT> ;
|
||||||
friend class eoDistEvalEasyEA <EOT> ;
|
friend class eoDistEvalEasyEA <EOT> ;
|
||||||
};
|
};
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,19 +31,22 @@
|
||||||
#include <eoVelocityInit.h>
|
#include <eoVelocityInit.h>
|
||||||
#include <eoPop.h>
|
#include <eoPop.h>
|
||||||
#include <eoParticleBestInit.h>
|
#include <eoParticleBestInit.h>
|
||||||
|
#include <eoTopology.h>
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Abstract class for initialization of algorithm PSO
|
* Abstract class for initialization of algorithm PSO
|
||||||
*/
|
*/
|
||||||
template <class POT> class eoInitializerBase : public eoFunctorBase
|
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
|
Base (name) class for Initialization of algorithm PSO
|
||||||
|
|
@ -51,8 +54,8 @@ public :
|
||||||
@see eoInitializerBase eoUF apply
|
@see eoInitializerBase eoUF apply
|
||||||
*/
|
*/
|
||||||
template <class POT> class eoInitializer : public eoInitializerBase <POT>
|
template <class POT> class eoInitializer : public eoInitializerBase <POT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//! Constructor
|
//! Constructor
|
||||||
//! @param _proc Evaluation function
|
//! @param _proc Evaluation function
|
||||||
|
|
@ -60,25 +63,54 @@ public:
|
||||||
//! @param _initBest Initialization of the best
|
//! @param _initBest Initialization of the best
|
||||||
//! @param _pop Population
|
//! @param _pop Population
|
||||||
eoInitializer(
|
eoInitializer(
|
||||||
eoUF<POT&, void>& _proc,
|
eoUF<POT&, void>& _proc,
|
||||||
eoVelocityInit < POT > &_initVelo,
|
eoVelocityInit < POT > &_initVelo,
|
||||||
eoParticleBestInit <POT> &_initBest,
|
eoParticleBestInit <POT> &_initBest,
|
||||||
eoPop < POT > &_pop
|
eoTopology <POT> &_topology,
|
||||||
) : proc(_proc), initVelo(_initVelo), initBest(_initBest)
|
eoPop < POT > &_pop
|
||||||
|
) : 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
|
||||||
{
|
{
|
||||||
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 :
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@param proc First evaluation
|
@param proc First evaluation
|
||||||
|
|
@ -86,10 +118,28 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,22 +53,25 @@ 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).
|
||||||
* @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,
|
||||||
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))
|
||||||
|
|
@ -155,7 +165,7 @@ public:
|
||||||
topology.updateNeighborhood(_po,_indice);
|
topology.updateNeighborhood(_po,_indice);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! eoTopology<POT> getTopology
|
//! eoTopology<POT> getTopology
|
||||||
//! @return topology
|
//! @return topology
|
||||||
|
|
||||||
eoTopology<POT> & getTopology ()
|
eoTopology<POT> & getTopology ()
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
149
eo/src/eoParticleFullInitializer.h
Normal file
149
eo/src/eoParticleFullInitializer.h
Normal 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*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -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,22 +52,25 @@ 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).
|
||||||
* @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,
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,8 @@
|
||||||
* -- update the neighborhoods
|
* -- update the neighborhoods
|
||||||
*/
|
*/
|
||||||
template < class POT > class eoSyncEasyPSO:public eoPSO < POT >
|
template < class POT > class eoSyncEasyPSO:public eoPSO < POT >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** Full constructor
|
/** Full constructor
|
||||||
* @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system
|
* @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
|
* to modify the positions according to the velocities
|
||||||
*/
|
*/
|
||||||
eoSyncEasyPSO (
|
eoSyncEasyPSO (
|
||||||
eoContinue < POT > &_continuator,
|
eoInitializer <POT> &_init,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoContinue < POT > &_continuator,
|
||||||
eoVelocity < POT > &_velocity,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoFlight < POT > &_flight):
|
eoVelocity < POT > &_velocity,
|
||||||
continuator (_continuator),
|
eoFlight < POT > &_flight):
|
||||||
eval (_eval),
|
init(_init),
|
||||||
loopEval(_eval),
|
continuator (_continuator),
|
||||||
popEval(loopEval),
|
eval (_eval),
|
||||||
velocity (_velocity),
|
loopEval(_eval),
|
||||||
flight (_flight){}
|
popEval(loopEval),
|
||||||
|
velocity (_velocity),
|
||||||
|
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,54 +76,82 @@ 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 (
|
||||||
eoContinue < POT > &_continuator,
|
eoInitializer <POT> &_init,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoContinue < POT > &_continuator,
|
||||||
eoVelocity < POT > &_velocity):
|
eoEvalFunc < POT > &_eval,
|
||||||
continuator (_continuator),
|
eoVelocity < POT > &_velocity):
|
||||||
eval (_eval),
|
init(_init),
|
||||||
loopEval(_eval),
|
continuator (_continuator),
|
||||||
popEval(loopEval),
|
eval (_eval),
|
||||||
velocity (_velocity),
|
loopEval(_eval),
|
||||||
flight (dummyFlight){}
|
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).
|
/// Apply a few iteration of flight to the population (=swarm).
|
||||||
virtual void operator () (eoPop < POT > &_pop)
|
virtual void operator () (eoPop < POT > &_pop)
|
||||||
{
|
{
|
||||||
try
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// just to use a loop eval
|
init();
|
||||||
eoPop<POT> empty_pop;
|
// just to use a loop eval
|
||||||
|
eoPop<POT> empty_pop;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
// perform velocity evaluation
|
|
||||||
velocity.apply (_pop);
|
|
||||||
|
|
||||||
// apply the flight
|
// perform velocity evaluation
|
||||||
flight.apply (_pop);
|
velocity.apply (_pop);
|
||||||
|
|
||||||
// evaluate the position (with a loop eval, empty_swarm IS USELESS)
|
// apply the flight
|
||||||
loopEval(empty_pop,_pop);
|
flight.apply (_pop);
|
||||||
|
|
||||||
// update the topology (particle and local/global best(s))
|
// evaluate the position (with a loop eval, empty_swarm IS USELESS)
|
||||||
velocity.updateNeighborhood(_pop);
|
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 ();
|
std::string s = e.what ();
|
||||||
s.append (" in eoSyncEasyPSO");
|
s.append (" in eoSyncEasyPSO");
|
||||||
throw std::runtime_error (s);
|
throw std::runtime_error (s);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
eoInitializer <POT> &init;
|
||||||
eoContinue < POT > &continuator;
|
eoContinue < POT > &continuator;
|
||||||
|
|
||||||
eoEvalFunc < POT > &eval;
|
eoEvalFunc < POT > &eval;
|
||||||
|
|
@ -132,7 +163,17 @@ 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;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /*_EOSYNCEASYPSO_H*/
|
#endif /*_EOSYNCEASYPSO_H*/
|
||||||
|
|
|
||||||
|
|
@ -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(){}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Reference in a new issue