21 #ifndef GRT_PSO_PARTICLE_HEADER
22 #define GRT_PSO_PARTICLE_HEADER
24 #include "../../Util/GRTCommon.h"
28 template <
class OBSERVATION_TYPE>
46 PSOParticle(
const unsigned int K,
const vector<double> &xMin,
const vector<double> &xMax){
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;
97 virtual bool init(
const unsigned int K,
const vector<double> &xMin,
const vector<double> &xMax){
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;
152 virtual bool update(
const vector< double > &globalBestX){
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 double 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 double normal(
double x,
double mu,
double sigma){
253 return ( 1.0/(sigma*SQRT_TWO_PI) ) * exp( - (
SQR(x-mu)/(2.0*
SQR(sigma)) ) );
264 inline double gauss(
double x,
double mu,
double sigma){
265 return exp( - (
SQR(x-mu)/(2.0*
SQR(sigma)) ) );
274 inline double SQR(
double x){
return x*x; }
281 double localBestCost;
284 vector< double > localBestX;
285 vector< double > xMin;
286 vector< double > xMax;
293 #endif //GRT_PSO_PARTICLE_HEADER
double getRandomNumberGauss(double mu=0.0, double sigma=1.0)
PSOParticle(const PSOParticle &rhs)
void setSeed(unsigned long long seed=0)
virtual bool propagate(const vector< double > &model)
virtual bool init(const unsigned int K, const vector< double > &xMin, const vector< double > &xMax)
PSOParticle & operator=(const PSOParticle &rhs)
virtual bool update(const vector< double > &globalBestX)
double gauss(double x, double mu, double sigma)
virtual double evaluate(OBSERVATION_TYPE &observation)
double getRandomNumberUniform(double minRange=0.0, double maxRange=1.0)
bool setRandomSeed(unsigned long long seed)
PSOParticle(const unsigned int K, const vector< double > &xMin, const vector< double > &xMax)
double normal(double x, double mu, double sigma)