GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
PSOParticle.h
1 /*
2  GRT MIT License
3  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6  and associated documentation files (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all copies or substantial
12  portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef GRT_PSO_PARTICLE_HEADER
22 #define GRT_PSO_PARTICLE_HEADER
23 
24 #include "../../Util/GRTCommon.h"
25 
26 namespace GRT{
27 
28 template <class OBSERVATION_TYPE>
30 
31 public:
36  initialized = false;
37  }
38 
46  PSOParticle(const unsigned int K,const vector<double> &xMin,const vector<double> &xMax){
47  init(K,xMin,xMax);
48  }
49 
55  PSOParticle(const PSOParticle &rhs){
56  *this = rhs;
57  }
58 
62  virtual ~PSOParticle(){}
63 
70 
71  if( this != &rhs ){
72  this->K = rhs.K;
73  this->w = rhs.w;
74  this->c1 = rhs.c1;
75  this->c2 = rhs.c2;
76  this->localBestCost = rhs.localBestCost;
77  this->x = rhs.x;
78  this->v = rhs.v;
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;
84  }
85  return *this;
86 
87  }
88 
97  virtual bool init(const unsigned int K,const vector<double> &xMin,const vector<double> &xMax){
98 
99  initialized = false;
100 
101  if( K != xMin.size() || K != xMax.size() ){
102  return false;
103  }
104 
105  this->K = K;
106  this->xMin = xMin;
107  this->xMax = xMax;
108  w = 1.0;
109  c1 = 2.0;
110  c2 = 2.0;
111  x.resize(K,0);
112  v.resize(K,0);
113 
114  initialized = true;
115 
116  //Reset the state vector
117  return reset();
118  }
119 
129  virtual bool propagate(const vector< double > &model){
130 
131  if( !initialized ) return false;
132 
133  if( model.size() != K ) return false;
134 
135  //Update the current position
136  for(unsigned int k=0; k<K; k++){
137  x[k] += random.getRandomNumberGauss(0,model[k]);
138  }
139 
140  //Decrease the local best cost
141  localBestCost *= 0.9;
142 
143  return true;
144  }
145 
152  virtual bool update(const vector< double > &globalBestX){
153 
154  if( !initialized ) return false;
155 
156  double r1 = 0;
157  double r2 = 0;
158  double vMax = 0;
159  for(unsigned int k=0; k<K; k++){
160  r1 = random.getRandomNumberUniform(0.0,1.0);
161  r2 = random.getRandomNumberUniform(0.0,1.0);
162 
163  //Update the velocity
164  v[k] = ( w * v[k] ) + ( c1 * r1 * (localBestX[k]-x[k]) ) + ( c2 * r2 * (globalBestX[k]-x[k]) );
165 
166  //Velocity Clamping
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; }
170 
171  //Position Update
172  x[k] = x[k] + v[k];
173 
174  //Update the xMax xMin values
175  if( x[k] > xMax[k] ){ xMax[k] = x[k]; }
176  else if( x[k] < xMin[k] ){ xMin[k] = x[k]; }
177  }
178 
179  return true;
180  }
181 
191  virtual double evaluate(OBSERVATION_TYPE &observation){
192  if( !initialized ) return -1;
193 
194  if( observation.size() != K ) return -1;
195 
196  //A cost of 1.0 is best, 0 is the worse
197  double cost = 0;
198  for(UINT i=0; i<K; i++){
199  cost += SQR(x[i]-observation[i]);
200  }
201  cost += 0.0001;
202  cost = 1.0/(cost*cost);
203 
204  //Check to see if we have found a local maxima, if so store the values of x that achieved it
205  if( cost > localBestCost ){
206  localBestCost = cost;
207  localBestX = x;
208  }
209  return cost;
210  }
211 
217  virtual bool reset(){
218 
219  if( !initialized ) return false;
220 
221  for(unsigned int k=0; k<K; k++){
222  x[k] = random.getRandomNumberUniform(xMin[k],xMax[k]);
223  v[k] = random.getRandomNumberUniform(-1.0,1);
224  }
225 
226  //Set the local best state vector to the current x state vector
227  localBestX = x;
228  localBestCost = 0;
229 
230  return true;
231  }
232 
239  bool setRandomSeed(unsigned long long seed){
240  random.setSeed( seed );
241  return true;
242  }
243 
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)) ) );
254  }
255 
264  inline double gauss(double x,double mu,double sigma){
265  return exp( - ( SQR(x-mu)/(2.0*SQR(sigma)) ) );
266  }
267 
274  inline double SQR(double x){ return x*x; }
275 
276  bool initialized;
277  unsigned int K;
278  double w;
279  double c1;
280  double c2;
281  double localBestCost;
282  vector< double > x;
283  vector< double > v;
284  vector< double > localBestX;
285  vector< double > xMin;
286  vector< double > xMax;
287  Random random;
288 
289 };
290 
291 }
292 
293 #endif //GRT_PSO_PARTICLE_HEADER
294 
double getRandomNumberGauss(double mu=0.0, double sigma=1.0)
Definition: Random.h:208
virtual bool reset()
Definition: PSOParticle.h:217
Definition: AdaBoost.cpp:25
PSOParticle(const PSOParticle &rhs)
Definition: PSOParticle.h:55
void setSeed(unsigned long long seed=0)
Definition: Random.h:67
virtual bool propagate(const vector< double > &model)
Definition: PSOParticle.h:129
virtual bool init(const unsigned int K, const vector< double > &xMin, const vector< double > &xMax)
Definition: PSOParticle.h:97
PSOParticle & operator=(const PSOParticle &rhs)
Definition: PSOParticle.h:69
virtual bool update(const vector< double > &globalBestX)
Definition: PSOParticle.h:152
double gauss(double x, double mu, double sigma)
Definition: PSOParticle.h:264
virtual double evaluate(OBSERVATION_TYPE &observation)
Definition: PSOParticle.h:191
double getRandomNumberUniform(double minRange=0.0, double maxRange=1.0)
Definition: Random.h:197
bool setRandomSeed(unsigned long long seed)
Definition: PSOParticle.h:239
double SQR(double x)
Definition: PSOParticle.h:274
PSOParticle(const unsigned int K, const vector< double > &xMin, const vector< double > &xMax)
Definition: PSOParticle.h:46
double normal(double x, double mu, double sigma)
Definition: PSOParticle.h:252
virtual ~PSOParticle()
Definition: PSOParticle.h:62