added pso initializer+ integerVelocity
This commit is contained in:
parent
2767fadf90
commit
0280487348
2 changed files with 278 additions and 0 deletions
96
eo/src/eoInitializer.h
Normal file
96
eo/src/eoInitializer.h
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInitializer.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 _eoInitializer_H
|
||||
#define _eoInitializer_H
|
||||
|
||||
#include <utils/eoRealVectorBounds.h>
|
||||
#include <eoVelocityInit.h>
|
||||
#include <eoPop.h>
|
||||
#include <eoParticleBestInit.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 eoInitializer : public eoInitializerBase <POT>
|
||||
{
|
||||
public:
|
||||
|
||||
//! Constructor
|
||||
//! @param _proc Evaluation function
|
||||
//! @param _initVelo Initialization of the velocity
|
||||
//! @param _initBest Initialization of the best
|
||||
//! @param _pop Population
|
||||
eoInitializer(
|
||||
eoUF<POT&, void>& _proc,
|
||||
eoVelocityInit < POT > &_initVelo,
|
||||
eoParticleBestInit <POT> &_initBest,
|
||||
eoPop < POT > &_pop
|
||||
) : proc(_proc), initVelo(_initVelo), initBest(_initBest)
|
||||
{
|
||||
apply(proc, _pop);
|
||||
apply < POT > (initVelo, _pop);
|
||||
apply < POT > (initBest, _pop);
|
||||
}
|
||||
|
||||
//! Give the name of the class
|
||||
//! @return The name of the class
|
||||
virtual std::string className (void) const
|
||||
{
|
||||
return "eoInitializer";
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
/*
|
||||
@param proc First evaluation
|
||||
@param initVelo Initialization of the velocity
|
||||
@param initBest Initialization of the best
|
||||
|
||||
*/
|
||||
eoUF<POT&, void>& proc;
|
||||
eoVelocityInit < POT > & initVelo;
|
||||
eoParticleBestInit <POT> & initBest;
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
182
eo/src/eoIntegerVelocity.h
Normal file
182
eo/src/eoIntegerVelocity.h
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoIntegerVelocity.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: clive.canape@inria.fr
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef EOINTEGERVELOCITY_H
|
||||
#define EOINTEGERVELOCITY_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <eoFunctor.h>
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoPop.h>
|
||||
#include <utils/eoRealVectorBounds.h>
|
||||
#include <eoRealBoundModifier.h>
|
||||
#include <eoTopology.h>
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
/** Standard velocity performer for particle swarm optimization. Derivated from abstract eoVelocity,
|
||||
* At step t: v(t+1)= int (v(t) + c1*r1* ( xbest(t)-x(t) ) + c2*r2* ( gbest(t) - x(t) ))
|
||||
* (ci given and Ri chosen at random in [0;1]).
|
||||
*/
|
||||
template < class POT > class eoIntegerVelocity:public eoVelocity < POT >
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
/*
|
||||
* Each element for the velocity evaluation is expected to be of type VelocityType.
|
||||
*/
|
||||
typedef typename POT::ParticleVelocityType VelocityType;
|
||||
|
||||
/** Full constructor: Bounds and bound modifier required
|
||||
* @param _topology - The topology to get the global/local/other best
|
||||
* @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType
|
||||
* @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType
|
||||
* @param _bounds - An eoRealBaseVectorBounds: real bounds for real velocities.
|
||||
* If the velocities are not real, they won't be bounded by default. Should have a eoBounds ?
|
||||
* @param _boundsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
|
||||
* @param _gen - The eo random generator, default=rng
|
||||
*/
|
||||
eoIntegerVelocity (eoTopology < POT > & _topology,
|
||||
const VelocityType & _c1,
|
||||
const VelocityType & _c2 ,
|
||||
eoRealVectorBounds & _bounds,
|
||||
eoRealBoundModifier & _bndsModifier,
|
||||
eoRng & _gen = rng):
|
||||
topology(_topology),
|
||||
c1 (_c1),
|
||||
c2 (_c2),
|
||||
bounds(_bounds),
|
||||
bndsModifier(_bndsModifier),
|
||||
gen(_gen){}
|
||||
|
||||
|
||||
/** Constructor: No bound updater required <-> fixed bounds
|
||||
* @param _topology - The topology to get the global/local/other best
|
||||
* @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType
|
||||
* @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType
|
||||
* @param _bounds - An eoRealBaseVectorBounds: real bounds for real velocities.
|
||||
* If the velocities are not real, they won't be bounded by default. Should have a eoBounds ?
|
||||
* @param _gen - The eo random generator, default=rng
|
||||
*/
|
||||
eoIntegerVelocity (eoTopology < POT > & _topology,
|
||||
const VelocityType & _c1,
|
||||
const VelocityType & _c2,
|
||||
eoRealVectorBounds & _bounds,
|
||||
eoRng & _gen = rng):
|
||||
topology(_topology),
|
||||
c1 (_c1),
|
||||
c2 (_c2),
|
||||
bounds(_bounds),
|
||||
bndsModifier(dummyModifier),
|
||||
gen(_gen){}
|
||||
|
||||
|
||||
/** Constructor: Neither bounds nor bound updater required <-> free velocity
|
||||
* @param _topology - The topology to get the global/local/other best
|
||||
* @param _c1 - The first learning factor used for the particle's best. Type must be POT::ParticleVelocityType
|
||||
* @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType
|
||||
* @param _gen - The eo random generator, default=rng
|
||||
*/
|
||||
eoIntegerVelocity (eoTopology < POT > & _topology,
|
||||
const VelocityType & _c1,
|
||||
const VelocityType & _c2,
|
||||
eoRng & _gen = rng):
|
||||
topology(_topology),
|
||||
c1 (_c1),
|
||||
c2 (_c2),
|
||||
bounds(*(new eoRealVectorNoBounds(0))),
|
||||
bndsModifier(dummyModifier),
|
||||
gen(_gen)
|
||||
{}
|
||||
|
||||
|
||||
/**
|
||||
* Evaluate the new velocities of the given particle. Need an indice to identify the particle
|
||||
* into the topology.
|
||||
* @param _po - A particle
|
||||
* @param _indice - The indice (into the topology) of the given particle
|
||||
*/
|
||||
void operator () (POT & _po,unsigned _indice)
|
||||
{
|
||||
VelocityType r1;
|
||||
VelocityType r2;
|
||||
|
||||
VelocityType newVelocity;
|
||||
|
||||
// cast the learning factors to VelocityType
|
||||
r1 = (VelocityType) rng.uniform (1) * c1;
|
||||
r2 = (VelocityType) rng.uniform (1) * c2;
|
||||
|
||||
// need to resize the bounds even if there are dummy because of "isBounded" call
|
||||
bounds.adjust_size(_po.size());
|
||||
|
||||
// assign the new velocities
|
||||
for (unsigned j = 0; j < _po.size (); j++)
|
||||
{
|
||||
newVelocity= round(_po.velocities[j] + r1 * (_po.bestPositions[j] - _po[j]) + r2 * (topology.best (_indice)[j] - _po[j]));
|
||||
|
||||
/* check bounds */
|
||||
if (bounds.isMinBounded(j))
|
||||
newVelocity=std::max(newVelocity,bounds.minimum(j));
|
||||
if (bounds.isMaxBounded(j))
|
||||
newVelocity=std::min(newVelocity,bounds.maximum(j));
|
||||
|
||||
_po.velocities[j]=newVelocity;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the neighborhood.
|
||||
*/
|
||||
void updateNeighborhood(POT & _po,unsigned _indice)
|
||||
{
|
||||
topology.updateNeighborhood(_po,_indice);
|
||||
}
|
||||
|
||||
//! eoTopology<POT> getTopology
|
||||
//! @return topology
|
||||
|
||||
eoTopology<POT> & getTopology ()
|
||||
{
|
||||
return topology;
|
||||
}
|
||||
|
||||
protected:
|
||||
eoTopology < POT > & topology;
|
||||
const VelocityType & c1; // learning factor 1
|
||||
const VelocityType & c2; // learning factor 2
|
||||
|
||||
eoRealVectorBounds bounds; // REAL bounds even if the velocity could be of another type.
|
||||
eoRealBoundModifier & bndsModifier;
|
||||
|
||||
eoRng & gen; // the random generator
|
||||
|
||||
// If the bound modifier doesn't need to be used, use the dummy instance
|
||||
eoDummyRealBoundModifier dummyModifier;
|
||||
};
|
||||
|
||||
|
||||
#endif /*EOINTEGERVELOCITY_H */
|
||||
|
||||
Reference in a new issue