32 #ifndef GRT_PARTICLE_SWARM_OPTIMIZATION_HEADER
33 #define GRT_PARTICLE_SWARM_OPTIMIZATION_HEADER
35 #include "../../Util/GRTCommon.h"
36 #include "PSOParticle.h"
40 template<
class PARTICLE_TYPE,
class OBSERVATION_TYPE>
49 minImprovement = 1.0e-10;
51 maxNumIterNoChange = 10;
88 virtual bool init(
const unsigned int numParticles,
const unsigned int K,
const vector<double> &
xMin,
const vector<double> &
xMax,
const vector< double > &
propagationModel){
92 if( K != xMin.size() || K != xMax.size() )
return false;
112 typename vector< PARTICLE_TYPE >::iterator pIter =
particles.begin();
115 pIter->init(K,xMin,xMax);
137 typename vector< PARTICLE_TYPE >::iterator pIter =
particles.begin();
151 virtual bool search(OBSERVATION_TYPE &observation){
156 typename vector< PARTICLE_TYPE >::iterator pIter;
159 errorLog <<
"search(...) - Particle propagation failed!" << endl;
164 unsigned int iterCounter = 0;
165 unsigned int numIterNoChangeCounter = 0;
166 double currentMaxima = 0;
167 double lastMaxima = 0;
169 bool keepSearching =
true;
174 while( keepSearching ){
180 delta = fabs( currentMaxima - lastMaxima );
181 lastMaxima = currentMaxima;
183 if( delta < minImprovement ){
184 if( ++numIterNoChangeCounter >= maxNumIterNoChange ){
185 keepSearching =
false;
186 infoLog <<
"search(...) - Reached no change limit, stopping search at iteration: " << iterCounter <<
" with a change of: " << fabs( currentMaxima - lastMaxima ) << endl;
188 }
else numIterNoChangeCounter = 0;
190 if( ++iterCounter == maxIter ){
191 keepSearching =
false;
192 infoLog <<
"search(...) - Max iteration reached, stopping search at iteration: " << iterCounter << endl;
213 unsigned int index = 0;
214 unsigned int bestIndex = 0;
215 double currentBestMaxima = 0;
217 typename vector< PARTICLE_TYPE >::iterator pIter;
223 epsilon = pIter->evaluate(observation);
226 if( epsilon > currentBestMaxima ){
227 currentBestMaxima = epsilon;
245 return currentBestMaxima;
265 double minImprovement;
266 unsigned int maxIter;
267 unsigned int maxNumIterNoChange;
284 #endif //GRT_PARTICLE_SWARM_OPTIMIZATION_HEADER
vector< vector< double > > globalBestXHistory
A buffer to keep track of the search history.
vector< double > globalBestX
The state vector of the particle with the best cost.
bool initialized
A flag to indicate if the PSO algorithm has been initialized.
vector< double > xMin
The minimum range of the state space.
bool setPropagationModel(const vector< double > &propagationModel)
vector< double > finalX
The final estimate.
virtual ~ParticleSwarmOptimization()
PARTICLE_TYPE & operator[](const unsigned int &index)
vector< vector< PARTICLE_TYPE > > iterHistory
A buffer to keep track of the search history.
ParticleSwarmOptimization()
vector< PARTICLE_TYPE > particles
A vector containing all the particles used for the search.
int getRandomNumberInt(int minRange, int maxRange)
double globalBestCost
The current global best cost over all the particles.
vector< double > propagationModel
The propagation model used to update each particle.
vector< double > xMax
The maximum range of the state space.
virtual bool init(const unsigned int numParticles, const unsigned int K, const vector< double > &xMin, const vector< double > &xMax, const vector< double > &propagationModel)
unsigned int K
The size of the particles state vector.
virtual double searchIteration(OBSERVATION_TYPE &observation)
virtual bool search(OBSERVATION_TYPE &observation)