pso.h
Go to the documentation of this file.
1 
23 #ifndef PSO_H_
24 #define PSO_H_
25 
26 #include <iostream>
27 #include <string>
28 #include <time.h>
29 #include <stdlib.h>
30 #include <vector>
31 #include <stack>
32 #include <map>
33 #include <math.h>
34 #include <set>
35 #include <queue>
36 #include <algorithm>
37 
38 #include <fstream>
39 #include <sstream>
40 
41 namespace pso{
42 
43  class Coordinate {
44  public:
45  float x;
46  float y;
48  {
49  x=0.0f;
50  y=.0f;
51  }
52 
53  Coordinate(float x,float y)
54  {
55  this->x=x;
56  this->y=y;
57  }
58  };
59 
60  class Particle{
61  //friend:
62  // std::ostream& operator<<(std::ostream &output,const Particle &right);
63 
64  public:
65  Particle(float x,float y)
66  {
67  Xmax=30.0f;
68  Xmin=0.0f;
69  Ymax=30.0f;
70  Ymin=0.0f;
71  Vxmax=Xmax-Xmin;
72  Vxmin=0-Vxmax;
73  Vymax=Ymax-Ymin;
74  Vymin=0-Vymax;
75 
76  c1=2.0f;
77  c2=2.0f;
78 
79  c.x=x;
80  c.y=y;
81  //p=100.0f;
82  p=pow(c.x-10.0f,2)+pow(c.y-20.0f,2);
83  Vx=(Xmax-Xmin)/8.0f;
84  Vy=(Xmax-Xmin)/8.0f;
85 
86  pBest.x=x;
87  pBest.y=y;
88  }
89 
90  void setP()
91  {
92  float temp=pow(c.x-10.0f,2)+pow(c.y-20.0f,2);
93  if(temp<p)
94  {
95  p=temp;
96  //pBest.x=c.x;
97  //pBest.y=c.y;
98  pBest=c;
99  }
100  }
101 
102  float getP()const
103  {
104  return p;
105  }
106 
108  {
109  return pBest;
110  }
111 
112  void setV(Coordinate gBest,float w)
113  {
114  float r1,r2;
115  r1=rand()/(float)RAND_MAX;
116  r2=rand()/(float)RAND_MAX;
117  Vx=w*Vx+c1*r1*(pBest.x-c.x)+c2*r2*(gBest.x-c.x);
118  if(Vx>Vxmax)
119  Vx=Vxmax;
120  else if(Vx<Vxmin)
121  Vx=Vxmin;
122  Vy=w*Vy+c1*r1*(pBest.y-c.y)+c2*r2*(gBest.y-c.y);
123  if(Vy>Vxmax)
124  Vy=Vxmax;
125  else if(Vy<Vxmin)
126  Vy=Vxmin;
127  }
128 
129  float getVx()const
130  {
131  return Vx;
132  }
133 
134  float getVy()const
135  {
136  return Vy;
137  }
138 
140  {
141  c.x=c.x+Vx;
142  if(c.x>Xmax)
143  c.x=Xmax;
144  else if(c.x<Xmin)
145  c.x=Xmin;
146  c.y=c.y+Vy;
147  if(c.y>Ymax)
148  c.y=Ymax;
149  else if(c.y<Ymin)
150  c.y=Ymin;
151  }
152 
153  float getX()const
154  {
155  return c.x;
156  }
157 
158  float getY()const
159  {
160  return c.y;
161  }
162  private:
164  float p;
166  float Vx;
167  float Vy;
168  float Xmax,Xmin;
169  float Ymax,Ymin;
170  float Vxmax,Vxmin;
171  float Vymax,Vymin;
172  float c1,c2;
173  };
174 };
175 #endif
float Vy
Definition: pso.h:167
void setCoordinate()
Definition: pso.h:139
Definition: pso.h:41
float c2
Definition: pso.h:172
f
float getVy() const
Definition: pso.h:134
float Vx
Definition: pso.h:166
void setV(Coordinate gBest, float w)
Definition: pso.h:112
float getP() const
Definition: pso.h:102
float Ymin
Definition: pso.h:169
Coordinate c
Definition: pso.h:163
Coordinate getPBest() const
Definition: pso.h:107
Coordinate pBest
Definition: pso.h:165
float y
Definition: pso.h:46
Coordinate(float x, float y)
Definition: pso.h:53
Coordinate()
Definition: pso.h:47
float Vymin
Definition: pso.h:171
float x
Definition: pso.h:45
void setP()
Definition: pso.h:90
Particle(float x, float y)
Definition: pso.h:65
float getY() const
Definition: pso.h:158
float Xmin
Definition: pso.h:168
float p
Definition: pso.h:164
float getX() const
Definition: pso.h:153
float Vxmin
Definition: pso.h:170
float getVx() const
Definition: pso.h:129


pso
Author(s):
autogenerated on Mon Jun 10 2019 14:02:26