Added new lesson (6) dedicated to the PSO. Also changed a few things into the PSO-dedicated components (constructors)
This commit is contained in:
parent
4834adbe5f
commit
4ad79a9148
17 changed files with 771 additions and 115 deletions
|
|
@ -140,7 +140,7 @@ public:
|
||||||
* Write object. Called printOn since it prints the object _on_ a stream.
|
* Write object. Called printOn since it prints the object _on_ a stream.
|
||||||
* @param _os A std::ostream.
|
* @param _os A std::ostream.
|
||||||
*/
|
*/
|
||||||
virtual void printOn(std::ostream& _os) const { _os << bestFitness <<' ' ;}
|
virtual void printOn(std::ostream& _os) const { _os << bestFitness << ' ' ;}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -172,7 +172,6 @@
|
||||||
#include <eoFlight.h>
|
#include <eoFlight.h>
|
||||||
#include <eoStandardFlight.h>
|
#include <eoStandardFlight.h>
|
||||||
#include <eoVelocityInit.h>
|
#include <eoVelocityInit.h>
|
||||||
#include <eoDummyFlight.h>
|
|
||||||
#include <eoBinaryFlight.h>
|
#include <eoBinaryFlight.h>
|
||||||
#include <eoSigBinaryFlight.h>
|
#include <eoSigBinaryFlight.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// eoDummyFlight.h
|
|
||||||
// (c) OPAC 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: thomas.legrand@lifl.fr
|
|
||||||
*/
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
#ifndef EODUMMYFLIGHT_H
|
|
||||||
#define EODUMMYFLIGHT_H
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
#include <eoFlight.h>
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A dummy flight that does nothing !
|
|
||||||
*/
|
|
||||||
template < class POT > class eoDummyFlight:public eoFlight < POT >
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
// Ctor
|
|
||||||
eoDummyFlight () {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dummy move: do nothing
|
|
||||||
*/
|
|
||||||
void operator () (POT & _po1) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif /*EODUMMYFLIGHT_H */
|
|
||||||
|
|
@ -30,7 +30,6 @@
|
||||||
#include <eoPSO.h>
|
#include <eoPSO.h>
|
||||||
#include <eoVelocity.h>
|
#include <eoVelocity.h>
|
||||||
#include <eoFlight.h>
|
#include <eoFlight.h>
|
||||||
#include <eoDummyFlight.h>
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
/** An easy-to-use particle swarm algorithm; you can use any particle,
|
/** An easy-to-use particle swarm algorithm; you can use any particle,
|
||||||
|
|
@ -47,7 +46,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 _init - An eoInitializerBase 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,7 +54,7 @@ public:
|
||||||
* to modify the positions according to the velocities
|
* to modify the positions according to the velocities
|
||||||
*/
|
*/
|
||||||
eoEasyPSO (
|
eoEasyPSO (
|
||||||
eoInitializer <POT> &_init,
|
eoInitializerBase <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity,
|
eoVelocity < POT > &_velocity,
|
||||||
|
|
@ -69,13 +68,13 @@ public:
|
||||||
|
|
||||||
|
|
||||||
/** 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 _init - An eoInitializerBase 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,
|
eoInitializerBase <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity):
|
eoVelocity < POT > &_velocity):
|
||||||
|
|
@ -86,6 +85,43 @@ public:
|
||||||
flight (dummyFlight)
|
flight (dummyFlight)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
/* Constructor without eoInitializerBase. Assume the initialization is done before running the algorithm
|
||||||
|
* @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system
|
||||||
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
|
* @param _flight - An eoFlight that defines how to make the particle flying: that means how
|
||||||
|
* to modify the positions according to the velocities
|
||||||
|
*/
|
||||||
|
eoEasyPSO (
|
||||||
|
eoContinue < POT > &_continuator,
|
||||||
|
eoEvalFunc < POT > &_eval,
|
||||||
|
eoVelocity < POT > &_velocity,
|
||||||
|
eoFlight < POT > &_flight):
|
||||||
|
init(dummyInit),
|
||||||
|
continuator (_continuator),
|
||||||
|
eval (_eval),
|
||||||
|
velocity (_velocity),
|
||||||
|
flight (_flight)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
/** Constructor without eoFlight nor eoInitializerBase. For special cases when the flight is performed withing the velocity.
|
||||||
|
* @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system
|
||||||
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
|
*/
|
||||||
|
eoEasyPSO (
|
||||||
|
eoContinue < POT > &_continuator,
|
||||||
|
eoEvalFunc < POT > &_eval,
|
||||||
|
eoVelocity < POT > &_velocity):
|
||||||
|
init(dummyInit),
|
||||||
|
continuator (_continuator),
|
||||||
|
eval (_eval),
|
||||||
|
velocity (_velocity),
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
|
@ -124,15 +160,29 @@ public:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
eoInitializer <POT> &init;
|
eoInitializerBase <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;
|
||||||
|
|
||||||
// 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;
|
class eoDummyFlight:public eoFlight < POT >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
eoDummyFlight () {}
|
||||||
|
void operator () (POT & _po) {}
|
||||||
|
}dummyFlight;
|
||||||
|
|
||||||
|
// if the initializer does not need to be used, use the dummy one instead
|
||||||
|
class eoDummyInitializer:public eoInitializerBase < POT >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
eoDummyInitializer () {}
|
||||||
|
void operator () (POT & _po) {}
|
||||||
|
}dummyInit;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,12 +112,13 @@ private :
|
||||||
@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;
|
|
||||||
eoUF<POT&, void>& proc;
|
eoUF<POT&, void>& proc;
|
||||||
eoPopEvalFunc <POT>& procPara;
|
|
||||||
eoVelocityInit < POT > & initVelo;
|
eoVelocityInit < POT > & initVelo;
|
||||||
eoParticleBestInit <POT> & initBest;
|
eoParticleBestInit <POT> & initBest;
|
||||||
|
eoPopEvalFunc <POT>& procPara;
|
||||||
eoTopology <POT> & topology;
|
eoTopology <POT> & topology;
|
||||||
|
eoPop < POT > & pop;
|
||||||
|
|
||||||
class eoDummyEval : public eoPopEvalFunc<POT>
|
class eoDummyEval : public eoPopEvalFunc<POT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,6 @@
|
||||||
#include <eoPSO.h>
|
#include <eoPSO.h>
|
||||||
#include <eoVelocity.h>
|
#include <eoVelocity.h>
|
||||||
#include <eoFlight.h>
|
#include <eoFlight.h>
|
||||||
#include <eoDummyFlight.h>
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
/** An easy-to-use synchronous particle swarm algorithm; you can use any particle,
|
/** An easy-to-use synchronous particle swarm algorithm; you can use any particle,
|
||||||
|
|
@ -48,7 +47,7 @@ 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 _init - An eoInitializerBase 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
|
||||||
|
|
@ -56,7 +55,7 @@ public:
|
||||||
* to modify the positions according to the velocities
|
* to modify the positions according to the velocities
|
||||||
*/
|
*/
|
||||||
eoSyncEasyPSO (
|
eoSyncEasyPSO (
|
||||||
eoInitializer <POT> &_init,
|
eoInitializerBase <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity,
|
eoVelocity < POT > &_velocity,
|
||||||
|
|
@ -72,13 +71,13 @@ public:
|
||||||
|
|
||||||
|
|
||||||
/** 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 _init - An eoInitializerBase 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,
|
eoInitializerBase <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoEvalFunc < POT > &_eval,
|
eoEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity):
|
eoVelocity < POT > &_velocity):
|
||||||
|
|
@ -92,14 +91,14 @@ public:
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/** 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 _init - An eoInitializerBase 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,
|
eoInitializerBase <POT> &_init,
|
||||||
eoContinue < POT > &_continuator,
|
eoContinue < POT > &_continuator,
|
||||||
eoPopEvalFunc < POT > &_eval,
|
eoPopEvalFunc < POT > &_eval,
|
||||||
eoVelocity < POT > &_velocity,
|
eoVelocity < POT > &_velocity,
|
||||||
|
|
@ -114,6 +113,66 @@ public:
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
/** Another constructor without initializer
|
||||||
|
* @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system
|
||||||
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
|
* @param _flight - An eoFlight that defines how to make the particle flying: that means how
|
||||||
|
* to modify the positions according to the velocities
|
||||||
|
*/
|
||||||
|
eoSyncEasyPSO (
|
||||||
|
eoContinue < POT > &_continuator,
|
||||||
|
eoEvalFunc < POT > &_eval,
|
||||||
|
eoVelocity < POT > &_velocity,
|
||||||
|
eoFlight < POT > &_flight):
|
||||||
|
init(dummyInit),
|
||||||
|
continuator (_continuator),
|
||||||
|
eval (_eval),
|
||||||
|
loopEval(_eval),
|
||||||
|
popEval(loopEval),
|
||||||
|
velocity (_velocity),
|
||||||
|
flight (_flight)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
/** Constructor without eoFlight nor eoInitializer. For special cases when the flight is performed withing the velocity.
|
||||||
|
* @param _continuator - An eoContinue that manages the stopping criterion and the checkpointing system
|
||||||
|
* @param _eval - An eoEvalFunc: the evaluation performer
|
||||||
|
* @param _velocity - An eoVelocity that defines how to compute the velocities
|
||||||
|
*/
|
||||||
|
eoSyncEasyPSO (
|
||||||
|
eoContinue < POT > &_continuator,
|
||||||
|
eoEvalFunc < POT > &_eval,
|
||||||
|
eoVelocity < POT > &_velocity):
|
||||||
|
init(dummyInit),
|
||||||
|
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 (
|
||||||
|
eoContinue < POT > &_continuator,
|
||||||
|
eoPopEvalFunc < POT > &_eval,
|
||||||
|
eoVelocity < POT > &_velocity,
|
||||||
|
eoFlight <POT> &_flight):
|
||||||
|
init(dummyInit),
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
|
@ -122,6 +181,7 @@ public:
|
||||||
{
|
{
|
||||||
// initializes the topology, velocity, best particle(s)
|
// initializes the topology, velocity, best particle(s)
|
||||||
init();
|
init();
|
||||||
|
|
||||||
// just to use a loop eval
|
// just to use a loop eval
|
||||||
eoPop<POT> empty_pop;
|
eoPop<POT> empty_pop;
|
||||||
|
|
||||||
|
|
@ -154,7 +214,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
eoInitializer <POT> &init;
|
eoInitializerBase <POT> &init;
|
||||||
eoContinue < POT > &continuator;
|
eoContinue < POT > &continuator;
|
||||||
|
|
||||||
eoEvalFunc < POT > &eval;
|
eoEvalFunc < POT > &eval;
|
||||||
|
|
@ -164,17 +224,29 @@ private:
|
||||||
eoVelocity < POT > &velocity;
|
eoVelocity < POT > &velocity;
|
||||||
eoFlight < POT > &flight;
|
eoFlight < POT > &flight;
|
||||||
|
|
||||||
// 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
|
// 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;
|
||||||
|
|
||||||
|
class eoDummyFlight:public eoFlight < POT >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
eoDummyFlight () {}
|
||||||
|
void operator () (POT & _po) {}
|
||||||
|
}dummyFlight;
|
||||||
|
|
||||||
|
// if the initializer does not need to be used, use the dummy one instead
|
||||||
|
class eoDummyInitializer:public eoInitializerBase < POT >
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
eoDummyInitializer () {}
|
||||||
|
void operator () (POT & _po) {}
|
||||||
|
}dummyInit;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,7 @@ SET (TEST_LIST t-eoParetoFitness
|
||||||
t-eoShiftMutation
|
t-eoShiftMutation
|
||||||
t-eoTwoOptMutation
|
t-eoTwoOptMutation
|
||||||
t-eoRingTopology
|
t-eoRingTopology
|
||||||
|
t-eoSyncEasyPSO
|
||||||
# t-eoFrontSorter
|
# t-eoFrontSorter
|
||||||
# t-eoEpsMOEA
|
# t-eoEpsMOEA
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,18 @@
|
||||||
avoid compiler warnings.
|
avoid compiler warnings.
|
||||||
|
|
||||||
|
|
||||||
2006-07-02 Thomas Legrand <thomas.legrand@lifl.fr>
|
2006-07-02 Thomas Legrand <thomas.legrand@inria.fr>
|
||||||
|
|
||||||
* test/t-eoEasyPSO.cpp: add PSO test
|
* test/t-eoEasyPSO.cpp: added PSO test
|
||||||
|
|
||||||
* test/Makefile.am: add PSO test
|
* test/Makefile.am: added PSO test
|
||||||
|
|
||||||
|
|
||||||
|
2006-02-27 Thomas Legrand <thomas.legrand@inria.fr>
|
||||||
|
|
||||||
|
* test/t-eoSyncEasyPSO.cpp: added synchronous PSO test
|
||||||
|
* test/t-eoEasyPSO.cpp: customized PSO test (initialization)
|
||||||
|
* test/Makefile.am: added synchronous PSO test
|
||||||
|
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* coding: iso-8859-1
|
* coding: iso-8859-1
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,8 @@ check_PROGRAMS = t-eoParetoFitness \
|
||||||
t-eoSwapMutation \
|
t-eoSwapMutation \
|
||||||
t-eoShiftMutation \
|
t-eoShiftMutation \
|
||||||
t-eoTwoOptMutation \
|
t-eoTwoOptMutation \
|
||||||
t-eoRingTopology
|
t-eoRingTopology \
|
||||||
|
t-eoSyncEasyPSO
|
||||||
|
|
||||||
TESTS = $(check_PROGRAMS) \
|
TESTS = $(check_PROGRAMS) \
|
||||||
run_tests # This script can be used to check command-line arguments
|
run_tests # This script can be used to check command-line arguments
|
||||||
|
|
@ -114,4 +115,4 @@ t_eoSwapMutation_SOURCES = t-eoSwapMutation.cpp
|
||||||
t_eoShiftMutation_SOURCES = t-eoShiftMutation.cpp
|
t_eoShiftMutation_SOURCES = t-eoShiftMutation.cpp
|
||||||
t_eoTwoOptMutation_SOURCES = t-eoTwoOptMutation.cpp
|
t_eoTwoOptMutation_SOURCES = t-eoTwoOptMutation.cpp
|
||||||
t_eoRingTopology_SOURCES = t-eoRingTopology.cpp
|
t_eoRingTopology_SOURCES = t-eoRingTopology.cpp
|
||||||
|
t_eoSyncEasyPSO_SOURCES = t-eoSyncEasyPSO.cpp
|
||||||
|
|
|
||||||
|
|
@ -48,22 +48,16 @@ int main()
|
||||||
// local best init
|
// local best init
|
||||||
eoFirstIsBestInit < Particle > localInit;
|
eoFirstIsBestInit < Particle > localInit;
|
||||||
|
|
||||||
|
// perform position initialization
|
||||||
// perform initialization
|
|
||||||
pop.append (POP_SIZE, random);
|
pop.append (POP_SIZE, random);
|
||||||
apply < Particle > (eval, pop);
|
|
||||||
apply < Particle > (veloRandom, pop);
|
|
||||||
apply < Particle > (localInit, pop);
|
|
||||||
|
|
||||||
|
|
||||||
std::cout << "population:" << std::endl;
|
|
||||||
for (i = 0; i < pop.size(); ++i)
|
|
||||||
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
|
||||||
|
|
||||||
|
|
||||||
// topology
|
// topology
|
||||||
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
||||||
topology.setup(pop);
|
|
||||||
|
// the full initializer
|
||||||
|
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
|
||||||
|
init();
|
||||||
|
|
||||||
|
|
||||||
// bounds
|
// bounds
|
||||||
eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
|
eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
|
||||||
|
|
@ -75,19 +69,26 @@ int main()
|
||||||
eoStandardFlight <Particle> flight;
|
eoStandardFlight <Particle> flight;
|
||||||
|
|
||||||
// Terminators
|
// Terminators
|
||||||
eoGenContinue <Particle> genCont (50);
|
eoGenContinue <Particle> genCont1 (50);
|
||||||
|
eoGenContinue <Particle> genCont2 (50);
|
||||||
// the full initializer
|
|
||||||
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
|
|
||||||
|
|
||||||
// PS flight
|
// PS flight
|
||||||
eoEasyPSO<Particle> pso(init,genCont, eval, velocity, flight);
|
eoEasyPSO<Particle> pso1(genCont1, eval, velocity, flight);
|
||||||
|
|
||||||
|
eoEasyPSO<Particle> pso2(init,genCont2, eval, velocity, flight);
|
||||||
|
|
||||||
// flight
|
// flight
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
pso(pop);
|
pso1(pop);
|
||||||
|
std::cout << "FINAL POPULATION AFTER PSO n°1:" << std::endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||||
|
|
||||||
|
pso2(pop);
|
||||||
|
std::cout << "FINAL POPULATION AFTER PSO n°2:" << std::endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
|
|
@ -95,9 +96,7 @@ int main()
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "pop" << std::endl;
|
|
||||||
for (i = 0; i < pop.size(); ++i)
|
|
||||||
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
104
eo/test/t-eoSyncEasyPSO.cpp
Normal file
104
eo/test/t-eoSyncEasyPSO.cpp
Normal file
|
|
@ -0,0 +1,104 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// t-eoEasySyncPSO.cpp
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef __GNUG__
|
||||||
|
// to avoid long name warnings
|
||||||
|
#pragma warning(disable:4786)
|
||||||
|
#endif // __GNUG__
|
||||||
|
|
||||||
|
#include <eo>
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
typedef eoMinimizingFitness FitT;
|
||||||
|
typedef eoRealParticle < FitT > Particle;
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// the objective function
|
||||||
|
double real_value (const Particle & _particle)
|
||||||
|
{
|
||||||
|
double sum = 0;
|
||||||
|
for (unsigned i = 0; i < _particle.size ()-1; i++)
|
||||||
|
sum += pow(_particle[i],2);
|
||||||
|
return (sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
const unsigned int VEC_SIZE = 2;
|
||||||
|
const unsigned int POP_SIZE = 20;
|
||||||
|
const unsigned int NEIGHBORHOOD_SIZE= 5;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
// the population:
|
||||||
|
eoPop<Particle> pop;
|
||||||
|
|
||||||
|
// Evaluation
|
||||||
|
eoEvalFuncPtr<Particle, double, const Particle& > eval( real_value );
|
||||||
|
|
||||||
|
// position init
|
||||||
|
eoUniformGenerator < double >uGen (-3, 3);
|
||||||
|
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
|
||||||
|
|
||||||
|
// velocity init
|
||||||
|
eoUniformGenerator < double >sGen (-2, 2);
|
||||||
|
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
|
||||||
|
|
||||||
|
// local best init
|
||||||
|
eoFirstIsBestInit < Particle > localInit;
|
||||||
|
|
||||||
|
// perform position initialization
|
||||||
|
pop.append (POP_SIZE, random);
|
||||||
|
|
||||||
|
// topology
|
||||||
|
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
||||||
|
|
||||||
|
// the full initializer
|
||||||
|
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
|
||||||
|
init();
|
||||||
|
|
||||||
|
|
||||||
|
// bounds
|
||||||
|
eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
|
||||||
|
|
||||||
|
// velocity
|
||||||
|
eoStandardVelocity <Particle> velocity (topology,1,1.6,2,bnds);
|
||||||
|
|
||||||
|
// flight
|
||||||
|
eoStandardFlight <Particle> flight;
|
||||||
|
|
||||||
|
// Terminators
|
||||||
|
eoGenContinue <Particle> genCont1 (50);
|
||||||
|
eoGenContinue <Particle> genCont2 (50);
|
||||||
|
|
||||||
|
// PS flight
|
||||||
|
eoSyncEasyPSO<Particle> pso1(genCont1, eval, velocity, flight);
|
||||||
|
|
||||||
|
eoSyncEasyPSO<Particle> pso2(init,genCont2, eval, velocity, flight);
|
||||||
|
|
||||||
|
// flight
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pso1(pop);
|
||||||
|
std::cout << "FINAL POPULATION AFTER SYNC PSO n°1:" << std::endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||||
|
|
||||||
|
pso2(pop);
|
||||||
|
std::cout << "FINAL POPULATION AFTER SYNC PSO n°2:" << std::endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "exception: " << e.what() << std::endl;;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
@ -4,6 +4,6 @@
|
||||||
### 1) Where must cmake go now ?
|
### 1) Where must cmake go now ?
|
||||||
######################################################################################
|
######################################################################################
|
||||||
|
|
||||||
SUBDIRS(Lesson1 Lesson2 Lesson3 Lesson4 Lesson5)
|
SUBDIRS(Lesson1 Lesson2 Lesson3 Lesson4 Lesson5 Lesson6)
|
||||||
|
|
||||||
######################################################################################
|
######################################################################################
|
||||||
|
|
|
||||||
182
eo/tutorial/Lesson6/BinaryPSO.cpp
Normal file
182
eo/tutorial/Lesson6/BinaryPSO.cpp
Normal file
|
|
@ -0,0 +1,182 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// BinaryPSO.cpp
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
//*
|
||||||
|
// An instance of a VERY simple Real-coded binary Particle Swarm Optimization Algorithm
|
||||||
|
//
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <eo>
|
||||||
|
|
||||||
|
// Use functions from namespace std
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
typedef eoMinimizingFitness FitT;
|
||||||
|
typedef eoBitParticle < FitT > Particle;
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// EVALFUNC
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Just a simple function that takes binary value of a chromosome and sets
|
||||||
|
// the fitness
|
||||||
|
double binary_value (const Particle & _particle)
|
||||||
|
{
|
||||||
|
double sum = 0;
|
||||||
|
for (unsigned i = 0; i < _particle.size(); i++)
|
||||||
|
sum +=_particle[i];
|
||||||
|
return (sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void main_function(int argc, char **argv)
|
||||||
|
{
|
||||||
|
// PARAMETRES
|
||||||
|
// all parameters are hard-coded!
|
||||||
|
const unsigned int SEED = 42; // seed for random number generator
|
||||||
|
|
||||||
|
const unsigned int MAX_GEN=500;
|
||||||
|
const unsigned int VEC_SIZE = 10;
|
||||||
|
const unsigned int POP_SIZE = 20;
|
||||||
|
const unsigned int NEIGHBORHOOD_SIZE= 3;
|
||||||
|
|
||||||
|
const double VELOCITY_INIT_MIN= -1;
|
||||||
|
const double VELOCITY_INIT_MAX= 1;
|
||||||
|
|
||||||
|
const double VELOCITY_MIN= -1.5;
|
||||||
|
const double VELOCITY_MAX= 1.5;
|
||||||
|
|
||||||
|
const double INERTIA= 1;
|
||||||
|
const double LEARNING_FACTOR1= 1.7;
|
||||||
|
const double LEARNING_FACTOR2= 2.3;
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////
|
||||||
|
// RANDOM SEED
|
||||||
|
//////////////////////////
|
||||||
|
//reproducible random seed: if you don't change SEED above,
|
||||||
|
// you'll aways get the same result, NOT a random run
|
||||||
|
rng.reseed(SEED);
|
||||||
|
|
||||||
|
|
||||||
|
/// SWARM
|
||||||
|
// population <=> swarm
|
||||||
|
eoPop<Particle> pop;
|
||||||
|
|
||||||
|
/// EVALUATION
|
||||||
|
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||||
|
eoEvalFuncPtr<Particle, double, const Particle& > eval( binary_value );
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// TOPOLOGY
|
||||||
|
//////////////
|
||||||
|
// ring topology
|
||||||
|
eoRingTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
// INITIALIZATION
|
||||||
|
////////////////////
|
||||||
|
// position initialization
|
||||||
|
eoUniformGenerator<bool> uGen;
|
||||||
|
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
|
||||||
|
pop.append (POP_SIZE, random);
|
||||||
|
|
||||||
|
// velocities initialization component
|
||||||
|
eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX);
|
||||||
|
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
|
||||||
|
|
||||||
|
// first best position initialization component
|
||||||
|
eoFirstIsBestInit < Particle > localInit;
|
||||||
|
|
||||||
|
// Create an eoInitialier that:
|
||||||
|
// - performs a first evaluation of the particles
|
||||||
|
// - initializes the velocities
|
||||||
|
// - the first best positions of each particle
|
||||||
|
// - setups the topology
|
||||||
|
eoInitializer <Particle> fullInit(eval,veloRandom,localInit,topology,pop);
|
||||||
|
|
||||||
|
// Full initialization here to be able to print the initial population
|
||||||
|
// Else: give the "init" component in the eoEasyPSO constructor
|
||||||
|
fullInit();
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// OUTPUT
|
||||||
|
////////////
|
||||||
|
// sort pop before printing it!
|
||||||
|
pop.sort();
|
||||||
|
|
||||||
|
// Print (sorted) the initial population (raw printout)
|
||||||
|
cout << "INITIAL POPULATION:" << endl;
|
||||||
|
for (unsigned i = 0; i < pop.size(); ++i)
|
||||||
|
cout << "\t best fit=" << pop[i] << endl;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// VELOCITY
|
||||||
|
//////////////
|
||||||
|
// Create the bounds for the velocity not go to far away
|
||||||
|
eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX);
|
||||||
|
|
||||||
|
// the velocity itself that needs the topology and a few constants
|
||||||
|
eoStandardVelocity <Particle> velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds);
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// FLIGHT
|
||||||
|
//////////////
|
||||||
|
// Binary flight based on sigmoid function
|
||||||
|
eoSigBinaryFlight <Particle> flight;
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
/// STOPPING CRITERIA
|
||||||
|
///////////////////////
|
||||||
|
// the algo will run for MAX_GEN iterations
|
||||||
|
eoGenContinue <Particle> genCont (MAX_GEN);
|
||||||
|
|
||||||
|
|
||||||
|
// GENERATION
|
||||||
|
/////////////////////////////////////////
|
||||||
|
// the algorithm
|
||||||
|
////////////////////////////////////////
|
||||||
|
// standard PSO requires
|
||||||
|
// stopping criteria, evaluation,velocity, flight
|
||||||
|
|
||||||
|
eoEasyPSO<Particle> pso(genCont, eval, velocity, flight);
|
||||||
|
|
||||||
|
// Apply the algo to the swarm - that's it!
|
||||||
|
pso(pop);
|
||||||
|
|
||||||
|
// OUTPUT
|
||||||
|
// Print (sorted) intial population
|
||||||
|
pop.sort();
|
||||||
|
cout << "FINAL POPULATION:" << endl;
|
||||||
|
for (unsigned i = 0; i < pop.size(); ++i)
|
||||||
|
cout << "\t best fit=" << pop[i] << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// A main that catches the exceptions
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
main_function(argc, argv);
|
||||||
|
}
|
||||||
|
catch(exception& e)
|
||||||
|
{
|
||||||
|
cout << "Exception: " << e.what() << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
58
eo/tutorial/Lesson6/CMakeLists.txt
Normal file
58
eo/tutorial/Lesson6/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
### 1) Include the sources
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src)
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
### 2) Specify where CMake can find the libraries
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
IF(NOT WIN32 OR CYGWIN)
|
||||||
|
LINK_DIRECTORIES(${EO_BINARY_DIR}/lib)
|
||||||
|
ENDIF(NOT WIN32 OR CYGWIN)
|
||||||
|
|
||||||
|
# especially for Visual Studio
|
||||||
|
IF(WIN32 AND NOT CYGWIN)
|
||||||
|
LINK_DIRECTORIES(${EO_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE})
|
||||||
|
ENDIF(WIN32 AND NOT CYGWIN)
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
### 3) Define your targets
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
ADD_EXECUTABLE(BinaryPSO BinaryPSO.cpp)
|
||||||
|
ADD_EXECUTABLE(RealPSO RealPSO.cpp)
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
### 4) Optionnal
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
SET(BINARYPSO_VERSION ${GLOBAL_VERSION})
|
||||||
|
SET_TARGET_PROPERTIES(BinaryPSO PROPERTIES VERSION "${BINARYPSO_VERSION}")
|
||||||
|
|
||||||
|
SET(REALPSO_VERSION ${GLOBAL_VERSION})
|
||||||
|
SET_TARGET_PROPERTIES(RealPSO PROPERTIES VERSION "${REALPSO_VERSION}")
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
### 5) Link the librairies for the targets
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
|
TARGET_LINK_LIBRARIES(BinaryPSO eo eoutils)
|
||||||
|
TARGET_LINK_LIBRARIES(RealPSO eo eoutils)
|
||||||
|
|
||||||
|
######################################################################################
|
||||||
|
|
||||||
19
eo/tutorial/Lesson6/Makefile.am
Normal file
19
eo/tutorial/Lesson6/Makefile.am
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
noinst_PROGRAMS = BinaryPSO RealPSO
|
||||||
|
|
||||||
|
|
||||||
|
SecondBitEA_SOURCES = BinaryPSO.cpp
|
||||||
|
|
||||||
|
SecondRealEA_SOURCES = RealPSO.cpp
|
||||||
|
|
||||||
|
|
||||||
|
noinst_HEADERS =
|
||||||
|
|
||||||
|
extra_DIST = Makefile.simple
|
||||||
|
|
||||||
|
|
||||||
|
LDADD = -L$(top_builddir)/src
|
||||||
|
|
||||||
|
LIBS = -leoutils -leo
|
||||||
|
|
||||||
|
INCLUDES = -I$(top_srcdir)/src
|
||||||
31
eo/tutorial/Lesson6/Makefile.simple
Normal file
31
eo/tutorial/Lesson6/Makefile.simple
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
### This Makefile is part of the tutorial of the EO library
|
||||||
|
# Unlike other Makefiles in EO, it is not using the automake/autoconf
|
||||||
|
# so that it stays easy to understant (you are in the tutorial, remember!)
|
||||||
|
# MS, Oct. 2002
|
||||||
|
|
||||||
|
# if you use this Makefile as a starting point for another application
|
||||||
|
# you might need to modify the following
|
||||||
|
DIR_EO = ../../src
|
||||||
|
|
||||||
|
.SUFFIXES: .cpp
|
||||||
|
|
||||||
|
# Warning: $(CXX) in Linux (RedHat and Mandrake at least) is g++
|
||||||
|
# However, if you are using this Makefile within xemacs,
|
||||||
|
# and have problems with the interpretation of the output (and its colors)
|
||||||
|
# then you should use c++ instead (make CXX=c++ will do)
|
||||||
|
|
||||||
|
.cpp: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -pg -o $@ $*.cpp
|
||||||
|
#$(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
||||||
|
|
||||||
|
.cpp.o: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -c -pg $*.cpp
|
||||||
|
|
||||||
|
PSO = BinaryPSO RealPSO
|
||||||
|
|
||||||
|
ALL = $(PSO)
|
||||||
|
|
||||||
|
lesson6 : $(PSO)
|
||||||
|
|
||||||
|
all : $(ALL)
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@/bin/rm $(ALL) *.o *.sav *.xg *.status *~
|
||||||
183
eo/tutorial/Lesson6/RealPSO.cpp
Normal file
183
eo/tutorial/Lesson6/RealPSO.cpp
Normal file
|
|
@ -0,0 +1,183 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// RealPSO.cpp
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
//*
|
||||||
|
// An instance of a VERY simple Real-coded Particle Swarm Optimization Algorithm
|
||||||
|
//
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <eo>
|
||||||
|
|
||||||
|
// Use functions from namespace std
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
typedef eoMinimizingFitness FitT;
|
||||||
|
typedef eoRealParticle < FitT > Particle;
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// EVALFUNC
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// a simple fitness function that computes the euclidian norm of a real vector
|
||||||
|
FitT real_value (const Particle & _particle)
|
||||||
|
{
|
||||||
|
double sum = 0;
|
||||||
|
for (unsigned i = 0; i < _particle.size(); i++)
|
||||||
|
sum += pow(_particle[i],2);
|
||||||
|
return (sqrt(sum));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void main_function(int argc, char **argv)
|
||||||
|
{
|
||||||
|
// PARAMETRES
|
||||||
|
// all parameters are hard-coded!
|
||||||
|
const unsigned int SEED = 42; // seed for random number generator
|
||||||
|
|
||||||
|
const unsigned int MAX_GEN=100;
|
||||||
|
const unsigned int VEC_SIZE = 2;
|
||||||
|
const unsigned int POP_SIZE = 20;
|
||||||
|
const unsigned int NEIGHBORHOOD_SIZE= 5;
|
||||||
|
|
||||||
|
const double POS_INIT_MIN= -2;
|
||||||
|
const double POS_INIT_MAX= 2;
|
||||||
|
|
||||||
|
const double VELOCITY_INIT_MIN= -1;
|
||||||
|
const double VELOCITY_INIT_MAX= 1;
|
||||||
|
|
||||||
|
const double VELOCITY_MIN= -1.5;
|
||||||
|
const double VELOCITY_MAX= 1.5;
|
||||||
|
|
||||||
|
const double INERTIA= 1;
|
||||||
|
const double LEARNING_FACTOR1= 1.7;
|
||||||
|
const double LEARNING_FACTOR2= 2.3;
|
||||||
|
|
||||||
|
//////////////////////////
|
||||||
|
// RANDOM SEED
|
||||||
|
//////////////////////////
|
||||||
|
//reproducible random seed: if you don't change SEED above,
|
||||||
|
// you'll aways get the same result, NOT a random run
|
||||||
|
rng.reseed(SEED);
|
||||||
|
|
||||||
|
|
||||||
|
/// SWARM
|
||||||
|
// population <=> swarm
|
||||||
|
eoPop<Particle> pop;
|
||||||
|
|
||||||
|
/// EVALUATION
|
||||||
|
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||||
|
eoEvalFuncPtr<Particle, FitT, const Particle& > eval( real_value );
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// TOPOLOGY
|
||||||
|
//////////////
|
||||||
|
// linear topology
|
||||||
|
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
// INITIALIZATION
|
||||||
|
////////////////////
|
||||||
|
// position initialization
|
||||||
|
eoUniformGenerator < double >uGen (POS_INIT_MIN, POS_INIT_MAX);
|
||||||
|
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
|
||||||
|
pop.append (POP_SIZE, random);
|
||||||
|
|
||||||
|
// velocities initialization component
|
||||||
|
eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX);
|
||||||
|
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
|
||||||
|
|
||||||
|
// first best position initialization component
|
||||||
|
eoFirstIsBestInit < Particle > localInit;
|
||||||
|
|
||||||
|
// Create an eoInitialier that:
|
||||||
|
// - performs a first evaluation of the particles
|
||||||
|
// - initializes the velocities
|
||||||
|
// - the first best positions of each particle
|
||||||
|
// - setups the topology
|
||||||
|
eoInitializer <Particle> fullInit(eval,veloRandom,localInit,topology,pop);
|
||||||
|
|
||||||
|
// Full initialization here to be able to print the initial population
|
||||||
|
// Else: give the "init" component in the eoEasyPSO constructor
|
||||||
|
fullInit();
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// OUTPUT
|
||||||
|
////////////
|
||||||
|
// sort pop before printing it!
|
||||||
|
pop.sort();
|
||||||
|
|
||||||
|
// Print (sorted) the initial population (raw printout)
|
||||||
|
cout << "INITIAL POPULATION:" << endl;
|
||||||
|
for (unsigned i = 0; i < pop.size(); ++i)
|
||||||
|
cout << "\t best fit=" << pop[i] << endl;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// VELOCITY
|
||||||
|
//////////////
|
||||||
|
// Create the bounds for the velocity not go to far away
|
||||||
|
eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX);
|
||||||
|
|
||||||
|
// the velocity itself that needs the topology and a few constants
|
||||||
|
eoStandardVelocity <Particle> velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds);
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// FLIGHT
|
||||||
|
//////////////
|
||||||
|
// flight
|
||||||
|
eoStandardFlight <Particle> flight;
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
/// STOPPING CRITERIA
|
||||||
|
///////////////////////
|
||||||
|
// the algo will run for MAX_GEN iterations
|
||||||
|
eoGenContinue <Particle> genCont (MAX_GEN);
|
||||||
|
|
||||||
|
|
||||||
|
// GENERATION
|
||||||
|
/////////////////////////////////////////
|
||||||
|
// the algorithm
|
||||||
|
////////////////////////////////////////
|
||||||
|
// standard PSO requires
|
||||||
|
// stopping criteria, evaluation,velocity, flight
|
||||||
|
|
||||||
|
eoEasyPSO<Particle> pso(genCont, eval, velocity, flight);
|
||||||
|
|
||||||
|
// Apply the algo to the swarm - that's it!
|
||||||
|
pso(pop);
|
||||||
|
|
||||||
|
// OUTPUT
|
||||||
|
// Print (sorted) intial population
|
||||||
|
pop.sort();
|
||||||
|
cout << "FINAL POPULATION:" << endl;
|
||||||
|
for (unsigned i = 0; i < pop.size(); ++i)
|
||||||
|
cout << "\t best fit=" << pop[i] << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// A main that catches the exceptions
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
main_function(argc, argv);
|
||||||
|
}
|
||||||
|
catch(exception& e)
|
||||||
|
{
|
||||||
|
cout << "Exception: " << e.what() << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
Reference in a new issue