1#include "AlgParticleSwarmOptimization.h"
4ParticleSwarmOptimization::ParticleSwarmOptimization()
7 name_ =
"ParticleSwarmOptimization";
9 areGenotypesAdded_ =
false;
13 selBestOp =
static_cast<SelectionOperatorP
> (
new SelBestOp);
20 registerParameter(state,
"weightType", (voidP)
new InertiaWeightType(CONSTANT), ECF::INT,
21 "weight type update: 0 - constant, 1 - time dependant (based on max generations)");
23 "initial inertia weight (either constant or time dependant)");
25 "max particle velocity");
27 "should the algorithm respect the bounds defined in the genotype or not (default: 0)");
34 selBestOp->initialize(state);
37 m_weightType = *((InertiaWeightType*) weightType.get());
40 m_weight = *((
double*) weight.get());
43 m_maxV = *((
double*) maxV.get());
46 if(m_weightType == TIME_VARIANT) {
47 if(state->getRegistry()->isModified(
"term.maxgen")) {
49 m_maxIter = *(boost::static_pointer_cast<int>( state->getRegistry()->getEntry(
"term.maxgen") ));
52 ECF_LOG_ERROR(state,
"Error: term.maxgen has to be specified in order to use time variant inertia eight in PSO algorithm");
59 GenotypeP activeGenotype = state->getGenotypes()[0];
60 RealValueGenotypeP rv = boost::dynamic_pointer_cast<RealValueGenotype> (activeGenotype);
61 if(!rv || state->getGenotypes().size() > 1) {
62 ECF_LOG_ERROR(state,
"Error: PSO algorithm accepts only a RealValueGenotype derived genotype! (FloatingPoint or Binary)");
66 voidP sptr = state->getGenotypes()[0]->getParameterValue(state,
"dimension");
67 uint numDimension = *((uint*) sptr.get());
72 sptr = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
73 lbound_ = *((
double*) sptr.get());
75 sptr = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
76 ubound_ = *((
double*) sptr.get());
79 if(areGenotypesAdded_)
82 FloatingPointP flpoint[4];
83 for(uint iGen = 1; iGen < 4; iGen++) {
86 state->setGenotype(flpoint[iGen]);
91 flpoint[iGen]->setParameterValue(state,
"dimension", (voidP)
new uint(numDimension));
94 flpoint[iGen]->setParameterValue(state,
"lbound", (voidP)
new double(0));
95 flpoint[iGen]->setParameterValue(state,
"ubound", (voidP)
new double(1));
97 ECF_LOG(state, 1,
"PSO algorithm: added 3 FloatingPoint genotypes (particle velocity, best-so-far postition, best-so-far fitness value)");
100 areGenotypesAdded_ =
true;
122 for( uint i = 0; i < deme->getSize(); i++ ) {
123 IndividualP particle = deme->at(i);
126 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(3));
127 double &particlePbestFitness = flp->realValue[0];
128 double fitness = particle->fitness->getValue();
130 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(0));
131 std::vector< double > &positions = flp->realValue;
133 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(2));
134 std::vector< double > &pbestx = flp->realValue;
137 if( fitness < particlePbestFitness ) {
138 particlePbestFitness = fitness;
141 for( uint j = 0;j<pbestx.size();j++ ) {
142 pbestx[j] = positions[j];
149 for( uint i = 0; i < deme->getSize(); i++ ) {
150 IndividualP particle = deme->at(i);
152 IndividualP bestParticle = selBestOp->select( *deme );
154 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(0));
155 std::vector< double > &positions = flp->realValue;
157 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(1));
158 std::vector< double > &velocities = flp->realValue;
160 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(2));
161 std::vector< double > &pbestx = flp->realValue;
166 switch( m_weightType )
170 weight_up = ( m_weight - 0.4 ) * ( m_maxIter - state->getGenerationNo() ) / m_maxIter + 0.4;
176 weight_up = m_weight;
180 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (bestParticle->getGenotype(2));
181 std::vector< double > &bestParticlesPbestx = flp->realValue;
182 for( uint j = 0; j < velocities.size(); j++ ) {
185 velocity = weight_up * velocities[j] +
186 2 * (rand()/(float)RAND_MAX) * (pbestx[j] - positions[j]) +
187 2 * (rand()/(float)RAND_MAX) * (bestParticlesPbestx[j] - positions[j]);
188 if( velocity > m_maxV ) velocity = m_maxV;
189 velocities[j] = velocity;
191 positions[j] += velocities[j];
196 if(positions[j] < lbound_)
197 positions[j] = lbound_;
198 if(positions[j] > ubound_)
199 positions[j] = ubound_;
std::string name_
algorithm name
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Helper function: register a single parameter with the system.
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
FloatingPoint class - implements genotype as a vector of floating point values.
bool setParameterValue(StateP state, std::string name, voidP value)
Write single parameter value to Registry.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
bool bounded_
constrained or not
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
Best individual selection operator.