-
Notifications
You must be signed in to change notification settings - Fork 0
/
pso.cpp
333 lines (278 loc) · 5.41 KB
/
pso.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <cstring>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <ctime>
#include <array>
using namespace std;
//Khoi tao cac gia tri mac dinh
const int Max_Interation = 200;
const int Population_Size = 5;
const int Variable_Count = 2;
//const int x_min = -5;
//const int x_max = 5;
//const int v_min = 0;
//const int v_max = 1;
double X[Population_Size][Variable_Count]; /*= {
{3.1472, -4.0246},
{4.0579, -2.2150},
{-3.7301, 0.4688},
{4.1338, 4.5751},
{1.3236, 4.6489}
};*/
double V[Population_Size][Variable_Count]; /*= {
{0.2194, 0.2449},
{0.1908, 0.2228},
{0.3828, 0.3232},
{0.3976, 0.3547},
{0.0934, 0.3773}
};*/
double Fx[Population_Size];
double Fx_old[Population_Size];
double Fx_new[Population_Size];
double Pbest[Population_Size][Variable_Count];
double Gbest[Population_Size];
double Randoom(int min, int max){
double r = min + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(max-min)));
return r ;
}
double func(double x1, double x2)
{
return pow(x1,2) - x1*x2 + pow(x2,2) + 2*x1 + 4*x2 + 3;
}
void initialize()
{
for(int i=0; i<Population_Size; i++)
{
for(int j=0; j<Variable_Count; j++)
{
X[i][j] = Randoom(-10, 10);
V[i][j] = Randoom(0, 1) ;
Pbest[i][j] = X[i][j];
}
}
}
void out_initialize()
{
cout<<"\nX{}\tGia tri X trong khoang [-5,5] :"<<endl;
//cout<<" x1\t\tx2"<<endl;
for(int i=0; i<Population_Size; i++)
{
cout<<"\tCon chim "<<i<<": ";
for(int j=0; j<Variable_Count; j++)
{
cout<<"[ "<<X[i][j]<<" ]\t";
}
cout<<endl;
}
cout<<"\nV{}\tGia tri V trong khoang [0,1] :"<<endl;
//cout<<" v1\t\tv2"<<endl;
for(int i=0; i<Population_Size; i++)
{
cout<<"\tCon chim "<<i<<": ";
for(int j=0; j<Variable_Count; j++)
{
cout<<"[ "<<V[i][j]<<" ]\t";
}
cout<<endl;
}
}
void f()
{
for (int i=0; i<Population_Size; i++)
{
Fx_new[i] = func(X[i][0], X[i][1]);
Fx[i] = Fx_new[i];
}
}
double gbest()
{
//double minF = Fx_new[0];
double maxF = Fx_new[0];
int pos = 0;
for (int i=0; i<Population_Size; i++)
{
/*
if (minF > Fx_new[i])
{
minF = Fx_new[i];
pos = i;
}
*/
if (maxF < Fx_new[i])
{
maxF = Fx_new[i];
pos = i;
}
}
for (int j=0; j<Variable_Count; j++)
{
Gbest[j] = X[pos][j];
}
return maxF;
}
void out_Pbest()
{
cout<<"\nPbest{}\tGia tri Pbest:"<<endl;
//cout<<" x1\t\t x2"<<endl;
for(int i=0; i<Population_Size; i++)
{
cout<<"\tCon chim "<<i<<": ";
for(int j=0; j<Variable_Count; j++)
{
cout<<"[ "<<Pbest[i][j]<<" ]\t";
}
cout<<endl;
}
}
void out_Gbest()
{
cout<<"\nGbest{} => Gia tri GBest: ";
for(int i=0; i<Variable_Count; i++)
{
cout<<"[ "<<Gbest[i]<<" ]\t";
}
}
void out_F()
{
cout<<"\nf{}\tGia tri cua ham f(x):"<<endl;
cout<<"\t\t f(x)"<<endl;
for(int i=0; i<Population_Size; i++)
{
cout<<"\tCon chim "<<i<<":\t";
cout<<"[ "<<Fx_new[i]<<" ]\t";
cout<<endl;
}
}
void update_V()
{
double r1 = 0.5949; //Randoom(0,1);
double r2 = 0.0855; //Randoom(0,1);
double Vnew[Population_Size][Variable_Count];
//Cap nhat V
for (int i=0; i<Population_Size; i++)
{
for (int j=0; j<Variable_Count; j++)
{
Vnew[i][j] = 0.9*V[i][j] + 1.5*r1*(Pbest[i][j] - X[i][j]) + 1.5*r2*(Gbest[j] - X[i][j]);
}
}
//Cap nhat
for (int i=0; i<Population_Size; i++)
{
for (int j=0; j<Variable_Count; j++)
{
V[i][j] = Vnew[i][j];
}
}
}
void update_X()
{
double Xnew[Population_Size][Variable_Count];
//Cap nhat X
for (int i=0; i<Population_Size; i++)
{
for (int j=0; j<Variable_Count; j++)
{
Xnew[i][j] = X[i][j] + V[i][j];
}
}
for (int i=0; i<Population_Size; i++)
{
for (int j=0; j<Variable_Count; j++)
{
if(Xnew[i][j]>10)
{
Xnew[i][j] = 10.00000;
}
else if(Xnew[i][j]<-10)
{
Xnew[i][j] = -10.00000;
}
}
}
//Cap nhat
for (int i=0; i<Population_Size; i++)
{
for (int j=0; j<Variable_Count; j++)
{
X[i][j] = Xnew[i][j];
}
}
}
void update_FGP()
{
int pos = 0;
//double Pbest_new[Population_Size][Variable_Count];
//double Gbest_new[Population_Size];
//Cap nhat gia tri ham muc tieu f(x)
for (int i=0; i<Population_Size; i++)
{
Fx_old[i] = Fx[i];
}
for (int i=0; i<Population_Size; i++)
{
Fx_new[i] = func(X[i][0], X[i][1]);
Fx[i] = Fx_new[i];
}
//Cap nhat Gbest
gbest();
/*
//Cap nhat Pbest max
for (int i=0; i<Population_Size; i++)
{
if (Fx_new[i] > Fx_old[i])
{
pos = i;
for (int j=0; j<Variable_Count; j++)
{
Pbest[pos][j] = X[pos][j];
}
}
}
*/
//Cap nhat Pbest min
for (int i=0; i<Population_Size; i++)
{
if (Fx_new[i] > Fx_old[i])
{
pos = i;
for (int j=0; j<Variable_Count; j++)
{
Pbest[pos][j] = X[pos][j];
}
}
}
}
void PSO()
{
cout<<"\t\t\t******************";
cout<<"\n\t\t\t* *";
cout<<"\n\t\t\t* THUAT TOAN PSO *";
cout<<"\n\t\t\t* *";
cout<<"\n\t\t\t******************\n\n";
int inter = 0;
cout<<"\tTim max f(x) = (x1)^2 - (x1)*(x2) + (x2)^2 + 2*(x1) + 4*(x2) + 3";
initialize();
while(inter < Max_Interation)
{
f();
gbest();
cout<<"\n\n\n=============================================================> ";
cout<<"Lan lap thu "<<inter<<": ";
out_initialize();
out_F();
out_Pbest();
out_Gbest();
cout<<"=> max f(x) = "<<gbest()<<"\t";
update_V();
update_X();
update_FGP();
inter += 1;
}
}
int main()
{
PSO();
return 0;
}