added eoExtendedvelocity (+doc + test) that takes both the local best and the global of a PSO topology into account. Also updated the doc tags for the other topologies and velocities

This commit is contained in:
tlegrand 2008-04-18 08:50:22 +00:00
commit 78a798d169
7 changed files with 110 additions and 46 deletions

View file

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

View file

@ -162,17 +162,19 @@ public:
return (neighborhoods[theGoodNhbd].best()); return (neighborhoods[theGoodNhbd].best());
} }
/* /*
* Return the global best of the topology * Return the global best of the topology
*/ */
virtual POT & globalBest()
virtual POT & globalBest(const eoPop<POT>& _pop)
{ {
unsigned howManyNeighborhood=_pop.size()/ neighborhoodSize;
POT gBest,tmp; POT gBest,tmp;
unsigned indGlobalBest=0; unsigned indGlobalBest=0;
if(neighborhoods.size()==1)
return neighborhoods[0].best();
gBest=neighborhoods[0].best(); gBest=neighborhoods[0].best();
for(unsigned i=1;i<howManyNeighborhood;i++) for(unsigned i=1;i<neighborhoods.size();i++)
{ {
tmp=neighborhoods[i].best(); tmp=neighborhoods[i].best();
if(gBest.best() < tmp.best()) if(gBest.best() < tmp.best())

View file

@ -37,8 +37,9 @@
/** 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)= w * v(t) + c1 * r1 * ( xbest(t)-x(t) ) + c2 * r2 * ( gbest(t) - x(t) ) * At step t: v(t+1)= w * v(t) + c1 * r1 * ( xbest(t)-x(t) ) + c2 * r2 * ( lbest(t) - x(t) )
* (ci given and Ri chosen at random in [0;1]). * lbest depends on the topology evolved, when using eoStarTopology, lbest corresponds to the
* global. Otherwise, lbest is a "local best", i.e the best in a neighborhood.
*/ */
template < class POT > class eoStandardVelocity:public eoVelocity < POT > template < class POT > class eoStandardVelocity:public eoVelocity < POT >
{ {
@ -53,8 +54,8 @@ 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 _w - The weight factor. * @param _w - The weight factor.
* @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType * @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType
* @param _c2 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _c2 - 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).
@ -79,8 +80,8 @@ 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 _w - The weight factor. * @param _w - The weight factor.
* @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType * @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType
* @param _c2 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _c2 - 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
@ -101,9 +102,10 @@ public:
/** 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 _w - The weight factor. * @param _w - The weight factor.
* @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType * @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType
* @param _c2 - The third learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType * @param _c2 - 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,

View file

@ -60,6 +60,7 @@ public:
updateNeighborhood(_pop[i],i); updateNeighborhood(_pop[i],i);
} }
/** /**
* Builds the neighborhoods contained in the topology. * Builds the neighborhoods contained in the topology.
*/ */
@ -67,28 +68,10 @@ public:
/* /*
* Returns the global best particle of the given population. * Return the global best of the topology
* 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(){}
{
POT gBest,tmp;
unsigned indGlobalBest=0;
gBest=best(0);
for(unsigned i=1;i<_pop.size();i++)
{
tmp=best(i);
if(gBest.best() < tmp.best())
{
gBest=tmp;
indGlobalBest=i;
}
}
return best(indGlobalBest);
}
/** /**
* Prints the neighborhoods contained in the topology. * Prints the neighborhoods contained in the topology.

View file

@ -71,6 +71,7 @@ SET (TEST_LIST t-eoParetoFitness
t-eoRingTopology t-eoRingTopology
t-eoSyncEasyPSO t-eoSyncEasyPSO
t-eoOrderXover t-eoOrderXover
t-eoExtendedVelocity
# t-eoFrontSorter # t-eoFrontSorter
# t-eoEpsMOEA # t-eoEpsMOEA
) )

View file

@ -0,0 +1,76 @@
//-----------------------------------------------------------------------------
// t-eoExtendedVelocity.cpp
//-----------------------------------------------------------------------------
#include <eo>
typedef eoRealParticle < double > Particle;
//Evaluation function
double f (const Particle & _particle)
{
double sum = 0;
for (unsigned i = 0; i < _particle.size (); i++)
sum += pow(_particle[i],2);
return (-sum);
}
int main_function(int argc, char **argv)
{
const unsigned POP_SIZE = 6, VEC_SIZE = 2, NEIGHBORHOOD_SIZE=2;
// the population:
eoPop<Particle> pop;
// Evaluation
eoEvalFuncPtr<Particle, double, const Particle& > eval( f );
// position + velocity + best init
eoUniformGenerator < double >uGen (-3, 3);
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
eoUniformGenerator < double >sGen (-2, 2);
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
eoFirstIsBestInit < Particle > localInit;
pop.append (POP_SIZE, random);
// topology
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
init();
// velocity
eoExtendedVelocity <Particle> velocity (topology,1,1,1,1);
// the test itself
for (unsigned int i = 0; i < POP_SIZE; ++i)
{
std::cout << " Initial particle n°" << i << " velocity: " << std::endl;
for (unsigned int j = 0; j < VEC_SIZE; ++j)
std::cout << " v" << j << "=" << pop[i].velocities[j] << std::endl;
}
for (unsigned int i = 0; i < POP_SIZE; ++i)
velocity (pop[i],i);
for (unsigned int i = 0; i < POP_SIZE; ++i)
{
std::cout << " Final particle n°" << i << " velocity: " << std::endl;
for (unsigned int j = 0; j < VEC_SIZE; ++j)
std::cout << " v" << j << "=" << pop[i].velocities[j] << std::endl;
}
}
int main(int argc, char **argv)
{
try
{
main_function(argc, argv);
}
catch(std::exception& e)
{
std::cout << "Exception: " << e.what() << " in t-eoExtendedVelocity" << std::endl;
}
}

View file

@ -16,7 +16,7 @@ double f (const Indi & _indi)
return (-sum); return (-sum);
} }
int the_main(int argc, char **argv) int main_function(int argc, char **argv)
{ {
//Parameters //Parameters
const unsigned int VEC_SIZE = 2; const unsigned int VEC_SIZE = 2;
@ -31,7 +31,6 @@ int the_main(int argc, char **argv)
eoUniformGenerator < double >sGen (-1., 1.); eoUniformGenerator < double >sGen (-1., 1.);
eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen); eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen);
eoFirstIsBestInit < Indi > localInit; eoFirstIsBestInit < Indi > localInit;
eoStandardFlight < Indi > flight;
eoPop < Indi > pop; eoPop < Indi > pop;
pop.append (POP_SIZE, random); pop.append (POP_SIZE, random);
apply(eval, pop); apply(eval, pop);
@ -58,11 +57,11 @@ int main(int argc, char **argv)
{ {
try try
{ {
the_main(argc, argv); main_function(argc, argv);
} }
catch(std::exception& e) catch(std::exception& e)
{ {
std::cout << "Exception: " << e.what() << std::endl; std::cout << "Exception: " << e.what() << " in t-eoRingTopology" << std::endl;
} }
} }