1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoExtendedVelocity.h
// (c) INRIA Dolphin 2008
/*
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@inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef eoExtendedVelocity_H
#define eoExtendedVelocity_H
//-----------------------------------------------------------------------------
#include <eoFunctor.h>
#include <utils/eoRNG.h>
#include <eoPop.h>
#include <utils/eoRealVectorBounds.h>
#include <eoRealBoundModifier.h>
#include <eoTopology.h>
//-----------------------------------------------------------------------------
/** Extended 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 * ( lbest(t) - x(t) ) + c3 * r3 * ( gbest(t) - x(t) )
* It includes both a "topology" best and a global best in the social knowledge. Each topology
* provides a method to retrieve the global best <=> the best of all the neighborhood the topology contains.
*
* @ingroup Variators
*/
template < class POT > class eoExtendedVelocity: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
* @param _w - The weight factor.
* @param _c1 - Learning factor used for the particle's best. Type must be POT::ParticleVelocityType
* @param _c2 - Learning factor used for the local best
* @param _c3 - Learning factor used for the global best
* @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 _bndsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
* @param _gen - The eo random generator, default=rng
*/
eoExtendedVelocity (eoTopology < POT > & _topology,
const VelocityType & _w,
const VelocityType & _c1,
const VelocityType & _c2,
const VelocityType & _c3,
eoRealVectorBounds & _bounds,
eoRealBoundModifier & _bndsModifier,
eoRng & _gen = rng):
topology(_topology),
omega (_w),
c1 (_c1),
c2 (_c2),
c3 (_c3),
bounds(_bounds),
bndsModifier(_bndsModifier),
gen(_gen){}
/** Constructor: No bound updater required <-> fixed bounds
* @param _topology - The topology
* @param _w - The weight factor.
* @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType
* @param _c2 - The third learning factor used for the local best. Type must be POT::ParticleVelocityType
* @param _c3 - Learning factor used for the global best
* @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
*/
eoExtendedVelocity (eoTopology < POT > & _topology,
const VelocityType & _w,
const VelocityType & _c1,
const VelocityType & _c2,
const VelocityType & _c3,
eoRealVectorBounds & _bounds,
eoRng & _gen = rng):
topology(_topology),
omega (_w),
c1 (_c1),
c2 (_c2),
c3 (_c3),
bounds(_bounds),
bndsModifier(dummyModifier),
gen(_gen){}
/** Constructor: Neither bounds nor bound updater required <-> free velocity
* @param _topology - The topology
* @param _w - The weight factor.
* @param _c1 - The second learning factor used for the particle's best. Type must be POT::ParticleVelocityType
* @param _c2 - The third learning factor used for the local best. Type must be POT::ParticleVelocityType
* @param _c3 - Learning factor used for the global best
* @param _gen - The eo random generator, default=rng
*/
eoExtendedVelocity (eoTopology < POT > & _topology,
const VelocityType & _w,
const VelocityType & _c1,
const VelocityType & _c2,
const VelocityType & _c3,
eoRng & _gen = rng):
topology(_topology),
omega (_w),
c1 (_c1),
c2 (_c2),
c3 (_c3),
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 r3;
VelocityType newVelocity;
// cast the learning factors to VelocityType
r1 = (VelocityType) rng.uniform (1) * c1;
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
bounds.adjust_size(_po.size());
// assign the new velocities
for (unsigned j = 0; j < _po.size (); j++)
{
newVelocity= omega * _po.velocities[j]
+ r1 * (_po.bestPositions[j] - _po[j])
+ r2 * (topology.best (_indice)[j] - _po[j])
+ r3 * (topology.globalBest()[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 & omega; // social/cognitive coefficient
const VelocityType & c1;
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.
eoRealBoundModifier & bndsModifier;
eoRng & gen; // the random generator
// If the bound modifier doesn't need to be used, use the dummy instance
eoDummyRealBoundModifier dummyModifier;
};
/** @todo this example does not appear in the doc for an unknown reason
* @example t-eoExtendedVelocity.cpp
* Example of a test program using this class:
*/
#endif /*eoExtendedVelocity_H */
|