21 #ifndef GRT_PSO_PARTICLE_HEADER 22 #define GRT_PSO_PARTICLE_HEADER 24 #include "../../Util/GRTCommon.h" 28 template <
class OBSERVATION_TYPE>
76 this->localBestCost = rhs.localBestCost;
79 this->xMin = rhs.xMin;
80 this->xMax = rhs.xMax;
81 this->localBestX = rhs.localBestX;
82 this->initialized = rhs.initialized;
83 this->random = rhs.random;
101 if( K != xMin.size() || K != xMax.size() ){
131 if( !initialized )
return false;
133 if( model.size() != K )
return false;
136 for(
unsigned int k=0; k<K; k++){
141 localBestCost *= 0.9;
154 if( !initialized )
return false;
159 for(
unsigned int k=0; k<K; k++){
164 v[k] = ( w * v[k] ) + ( c1 * r1 * (localBestX[k]-x[k]) ) + ( c2 * r2 * (globalBestX[k]-x[k]) );
167 vMax = 0.1 * ((xMax[k]-xMin[k])/2.0);
168 if( v[k] > vMax ){ v[k] = vMax; }
169 else if( v[k] < -vMax ){ v[k] = -vMax; }
175 if( x[k] > xMax[k] ){ xMax[k] = x[k]; }
176 else if( x[k] < xMin[k] ){ xMin[k] = x[k]; }
191 virtual Float
evaluate(OBSERVATION_TYPE &observation){
192 if( !initialized )
return -1;
194 if( observation.size() != K )
return -1;
198 for(UINT i=0; i<K; i++){
199 cost +=
SQR(x[i]-observation[i]);
202 cost = 1.0/(cost*cost);
205 if( cost > localBestCost ){
206 localBestCost = cost;
219 if( !initialized )
return false;
221 for(
unsigned int k=0; k<K; k++){
252 inline Float
normal(Float x,Float mu,Float sigma){
253 return ( 1.0/(sigma*SQRT_TWO_PI) ) * exp( - (
SQR(x-mu)/(2.0*
SQR(sigma)) ) );
264 inline Float
gauss(Float x,Float mu,Float sigma){
265 return exp( - (
SQR(x-mu)/(2.0*
SQR(sigma)) ) );
274 inline Float
SQR(Float x){
return x*x; }
293 #endif //GRT_PSO_PARTICLE_HEADER Float gauss(Float x, Float mu, Float sigma)
virtual bool init(const unsigned int K, const VectorFloat &xMin, const VectorFloat &xMax)
This file contains the Random class, a useful wrapper for generating cross platform random functions...
virtual bool resize(const unsigned int size)
PSOParticle(const PSOParticle &rhs)
Float getRandomNumberGauss(Float mu=0.0, Float sigma=1.0)
PSOParticle & operator=(const PSOParticle &rhs)
virtual bool update(const VectorFloat &globalBestX)
virtual bool propagate(const VectorFloat &model)
Float normal(Float x, Float mu, Float sigma)
bool setRandomSeed(unsigned long long seed)
PSOParticle(const unsigned int K, const VectorFloat &xMin, const VectorFloat &xMax)
virtual Float evaluate(OBSERVATION_TYPE &observation)
bool setSeed(const unsigned long long seed=0)
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)