corrected PSO dummy errors and completed documentation
This commit is contained in:
parent
4b99bc8bc9
commit
ef252cf7de
3 changed files with 121 additions and 114 deletions
|
|
@ -47,6 +47,7 @@ template < class POT > class eoEasyPSO:public eoPSO < POT >
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** Full constructor
|
/** Full constructor
|
||||||
|
* @param _init - An eoInitializer that initializes the topology, velocity, best particle(s)
|
||||||
* @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
|
||||||
* @param _eval - An eoEvalFunc: the evaluation performer
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
|
|
@ -54,37 +55,44 @@ public:
|
||||||
* to modify the positions according to the velocities
|
* to modify the positions according to the velocities
|
||||||
*/
|
*/
|
||||||
eoEasyPSO (
|
eoEasyPSO (
|
||||||
|
eoInitializer <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity,
|
eoVelocity < POT > &_velocity,
|
||||||
eoFlight < POT > &_flight):
|
eoFlight < POT > &_flight):
|
||||||
|
init(_init),
|
||||||
continuator (_continuator),
|
continuator (_continuator),
|
||||||
eval (_eval),
|
eval (_eval),
|
||||||
velocity (_velocity),
|
velocity (_velocity),
|
||||||
flight (_flight){}
|
flight (_flight)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
/** Constructor without eoFlight. For special cases when the flight is performed withing the velocity.
|
/** Constructor without eoFlight. For special cases when the flight is performed withing the velocity.
|
||||||
|
* @param _init - An eoInitializer that initializes the topology, velocity, best particle(s)
|
||||||
* @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
|
||||||
* @param _eval - An eoEvalFunc: the evaluation performer
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
*/
|
*/
|
||||||
eoEasyPSO (
|
eoEasyPSO (
|
||||||
|
eoInitializer <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity):
|
eoVelocity < POT > &_velocity):
|
||||||
|
init(_init),
|
||||||
continuator (_continuator),
|
continuator (_continuator),
|
||||||
eval (_eval),
|
eval (_eval),
|
||||||
velocity (_velocity),
|
velocity (_velocity),
|
||||||
flight (dummyFlight){}
|
flight (dummyFlight)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
/// 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
|
||||||
{
|
{
|
||||||
|
// initializes the topology, velocity, best particle(s)
|
||||||
|
init();
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
// loop over all the particles for the current iteration
|
// loop over all the particles for the current iteration
|
||||||
|
|
@ -103,7 +111,8 @@ public:
|
||||||
velocity.updateNeighborhood(_pop[idx],idx);
|
velocity.updateNeighborhood(_pop[idx],idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
}while (continuator (_pop));
|
}
|
||||||
|
while (continuator (_pop));
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (std::exception & e)
|
catch (std::exception & e)
|
||||||
|
|
@ -116,6 +125,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
eoInitializer <POT> &init;
|
||||||
eoContinue < POT > &continuator;
|
eoContinue < POT > &continuator;
|
||||||
eoEvalFunc < POT > &eval;
|
eoEvalFunc < POT > &eval;
|
||||||
eoVelocity < POT > &velocity;
|
eoVelocity < POT > &velocity;
|
||||||
|
|
|
||||||
|
|
@ -38,15 +38,15 @@
|
||||||
* 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
|
||||||
|
|
@ -54,8 +54,8 @@ template <class POT> class eoInitializerBase : public eoFunctorBase
|
||||||
@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
|
||||||
|
|
@ -63,18 +63,13 @@ template <class POT> class eoInitializer : public eoInitializerBase <POT>
|
||||||
//! @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,
|
||||||
eoTopology <POT> &_topology,
|
eoTopology <POT> &_topology,
|
||||||
eoPop < POT > &_pop
|
eoPop < POT > &_pop
|
||||||
) : proc(_proc), procPara(dummyEval), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop)
|
) : proc(_proc), procPara(dummyEval), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop)
|
||||||
{
|
{}
|
||||||
/* apply(proc, _pop);
|
|
||||||
apply < POT > (initVelo, _pop);
|
|
||||||
apply < POT > (initBest, _pop);
|
|
||||||
topology.setup(_pop);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
//! Constructor for parallel evaluation
|
//! Constructor for parallel evaluation
|
||||||
//! @param _proc Evaluation function
|
//! @param _proc Evaluation function
|
||||||
|
|
@ -82,11 +77,11 @@ template <class POT> class eoInitializer : public eoInitializerBase <POT>
|
||||||
//! @param _initBest Initialization of the best
|
//! @param _initBest Initialization of the best
|
||||||
//! @param _pop Population
|
//! @param _pop Population
|
||||||
eoInitializer(
|
eoInitializer(
|
||||||
eoPopEvalFunc <POT>& _proc,
|
eoPopEvalFunc <POT>& _proc,
|
||||||
eoVelocityInit < POT > &_initVelo,
|
eoVelocityInit < POT > &_initVelo,
|
||||||
eoParticleBestInit <POT> &_initBest,
|
eoParticleBestInit <POT> &_initBest,
|
||||||
eoTopology <POT> &_topology,
|
eoTopology <POT> &_topology,
|
||||||
eoPop < POT > &_pop
|
eoPop < POT > &_pop
|
||||||
) : proc(dummy), procPara(_proc), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop)
|
) : proc(dummy), procPara(_proc), initVelo(_initVelo), initBest(_initBest), topology(_topology), pop(_pop)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
@ -94,29 +89,28 @@ template <class POT> class eoInitializer : public eoInitializerBase <POT>
|
||||||
//! 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";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
virtual void operator () (/*eoPop < POT > &_pop*/)
|
|
||||||
{
|
{
|
||||||
eoPop<POT> empty_pop;
|
return "eoInitializer";
|
||||||
apply(proc, pop);
|
|
||||||
procPara(empty_pop, pop);
|
|
||||||
apply < POT > (initVelo, pop);
|
|
||||||
apply < POT > (initBest, pop);
|
|
||||||
topology.setup(pop);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private :
|
|
||||||
|
|
||||||
|
virtual void operator() ()
|
||||||
|
{
|
||||||
|
eoPop<POT> empty_pop;
|
||||||
|
apply(proc, pop);
|
||||||
|
procPara(empty_pop, pop);
|
||||||
|
apply < POT > (initVelo, pop);
|
||||||
|
apply < POT > (initBest, pop);
|
||||||
|
topology.setup(pop);
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@param proc First evaluation
|
@param proc First evaluation
|
||||||
@param initVelo Initialization of the velocity
|
@param initVelo Initialization of the velocity
|
||||||
@param initBest Initialization of the best
|
@param initBest Initialization of the best
|
||||||
|
|
||||||
*/
|
*/
|
||||||
eoPop < POT > & pop;
|
eoPop < POT > & pop;
|
||||||
eoUF<POT&, void>& proc;
|
eoUF<POT&, void>& proc;
|
||||||
|
|
@ -124,22 +118,22 @@ template <class POT> class eoInitializer : public eoInitializerBase <POT>
|
||||||
eoVelocityInit < POT > & initVelo;
|
eoVelocityInit < POT > & initVelo;
|
||||||
eoParticleBestInit <POT> & initBest;
|
eoParticleBestInit <POT> & initBest;
|
||||||
eoTopology <POT> & topology;
|
eoTopology <POT> & topology;
|
||||||
class eoDummyEval : public eoPopEvalFunc<POT>
|
class eoDummyEval : public eoPopEvalFunc<POT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void operator()(eoPop<POT> &,eoPop<POT> &_pop)
|
void operator()(eoPop<POT> &,eoPop<POT> &_pop)
|
||||||
{}
|
{}
|
||||||
}
|
}
|
||||||
dummyEval;
|
dummyEval;
|
||||||
class eoDummy : public eoUF<POT&, void>
|
class eoDummy : public eoUF<POT&, void>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void operator()(POT &)
|
void operator()(POT &)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
}
|
}
|
||||||
dummy;
|
dummy;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,11 @@
|
||||||
* -- 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 _init - An eoInitializer that initializes the topology, velocity, best particle(s)
|
||||||
* @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
|
||||||
* @param _eval - An eoEvalFunc: the evaluation performer
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
|
|
@ -55,102 +56,104 @@ template < class POT > class eoSyncEasyPSO:public eoPSO < POT >
|
||||||
* to modify the positions according to the velocities
|
* to modify the positions according to the velocities
|
||||||
*/
|
*/
|
||||||
eoSyncEasyPSO (
|
eoSyncEasyPSO (
|
||||||
eoInitializer <POT> &_init,
|
eoInitializer <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity,
|
eoVelocity < POT > &_velocity,
|
||||||
eoFlight < POT > &_flight):
|
eoFlight < POT > &_flight):
|
||||||
init(_init),
|
init(_init),
|
||||||
continuator (_continuator),
|
continuator (_continuator),
|
||||||
eval (_eval),
|
eval (_eval),
|
||||||
loopEval(_eval),
|
loopEval(_eval),
|
||||||
popEval(loopEval),
|
popEval(loopEval),
|
||||||
velocity (_velocity),
|
velocity (_velocity),
|
||||||
flight (_flight)
|
flight (_flight)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
/** Constructor without eoFlight. For special cases when the flight is performed withing the velocity.
|
/** Constructor without eoFlight. For special cases when the flight is performed withing the velocity.
|
||||||
|
* @param _init - An eoInitializer that initializes the topology, velocity, best particle(s)
|
||||||
* @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
|
||||||
* @param _eval - An eoEvalFunc: the evaluation performer
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
*/
|
*/
|
||||||
eoSyncEasyPSO (
|
eoSyncEasyPSO (
|
||||||
eoInitializer <POT> &_init,
|
eoInitializer <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity):
|
eoVelocity < POT > &_velocity):
|
||||||
init(_init),
|
init(_init),
|
||||||
continuator (_continuator),
|
continuator (_continuator),
|
||||||
eval (_eval),
|
eval (_eval),
|
||||||
loopEval(_eval),
|
loopEval(_eval),
|
||||||
popEval(loopEval),
|
popEval(loopEval),
|
||||||
velocity (_velocity),
|
velocity (_velocity),
|
||||||
flight (dummyFlight)
|
flight (dummyFlight)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/** Full constructor - Can be used in parallel
|
/** Full constructor - Can be used in parallel
|
||||||
|
* @param _init - An eoInitializer that initializes the topology, velocity, best particle(s)
|
||||||
* @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
|
||||||
* @param _eval - An eoPopEvalFunc
|
* @param _eval - An eoPopEvalFunc
|
||||||
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
* @param _flight - An eoFlight
|
* @param _flight - An eoFlight
|
||||||
*/
|
*/
|
||||||
eoSyncEasyPSO (
|
eoSyncEasyPSO (
|
||||||
eoInitializer <POT> &_init,
|
eoInitializer <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoPopEvalFunc < POT > &_eval,
|
eoPopEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity,
|
eoVelocity < POT > &_velocity,
|
||||||
eoFlight <POT> &_flight):
|
eoFlight <POT> &_flight):
|
||||||
init(_init),
|
init(_init),
|
||||||
continuator (_continuator),
|
continuator (_continuator),
|
||||||
eval (dummyEval),
|
eval (dummyEval),
|
||||||
loopEval(dummyEval),
|
loopEval(dummyEval),
|
||||||
popEval(_eval),
|
popEval(_eval),
|
||||||
velocity (_velocity),
|
velocity (_velocity),
|
||||||
flight (_flight)
|
flight (_flight)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
/// Apply a few iteration of flight to the population (=swarm).
|
/// Apply a few iteration of flight to the population (=swarm).
|
||||||
virtual void operator () (eoPop < POT > &_pop)
|
virtual void operator () (eoPop < POT > &_pop)
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
init();
|
// initializes the topology, velocity, best particle(s)
|
||||||
// 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);
|
||||||
|
|
||||||
// perform velocity evaluation
|
// apply the flight
|
||||||
velocity.apply (_pop);
|
flight.apply (_pop);
|
||||||
|
|
||||||
// apply the flight
|
// evaluate the position (with a loop eval, empty_swarm IS USELESS)
|
||||||
flight.apply (_pop);
|
popEval(empty_pop,_pop);
|
||||||
|
|
||||||
// evaluate the position (with a loop eval, empty_swarm IS USELESS)
|
// update the topology (particle and local/global best(s))
|
||||||
popEval(empty_pop,_pop);
|
velocity.updateNeighborhood(_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;
|
eoInitializer <POT> &init;
|
||||||
eoContinue < POT > &continuator;
|
eoContinue < POT > &continuator;
|
||||||
|
|
||||||
|
|
@ -163,17 +166,17 @@ template < class POT > class eoSyncEasyPSO:public eoPSO < POT >
|
||||||
|
|
||||||
// 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
|
// if the eval does not need to be used, use the dummy eval instance
|
||||||
class eoDummyEval : public eoEvalFunc<POT>
|
class eoDummyEval : public eoEvalFunc<POT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void operator()(POT &)
|
void operator()(POT &)
|
||||||
{}
|
{}
|
||||||
}
|
}
|
||||||
dummyEval;
|
dummyEval;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /*_EOSYNCEASYPSO_H*/
|
#endif /*_EOSYNCEASYPSO_H*/
|
||||||
|
|
|
||||||
Reference in a new issue