-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGWO (working).cpp
338 lines (312 loc) · 9.35 KB
/
GWO (working).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
334
335
336
337
338
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<cstdint>
#include<numeric>
#include <iomanip>
#include <random>
#include<limits>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
int len(int x[])
{
return (sizeof(x)/sizeof(x[0]));
}
int lenDouble(double x[])
{
return (sizeof(x)/sizeof(x[0]));
}
double *power(double x[], int y)
{
int lx=lenDouble(x);
double *xp= new double[lx];
//int xp[lx];
for(int i=0;i<lx;i++)
{
xp[i]=pow(x[i],y);
}
return (xp);
}
double prod(double a[])
{
int l=lenDouble(a);
double result;
for (int i=0;i<l;i++)
{
result=a[i]*result;
}
return result;
}
double arraySum(double a[])
{
int n = lenDouble(a);
double sum = 0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
return (sum);
}
double F1(double x[])
{
double fit=arraySum(power(x,2));
return fit;
}
double F2(double x[])
{
double fit=arraySum(x)+prod(x);
return fit;
}
//float GWO(string objf,float lb,float ub,float dim,
// float SearchAgents_no,float Max_iter);//obif, lb,ub,dim we will get from benchmark function not from optimizer
/*double *randomUniform(int lb1, int ub1, int length ) //for slicing the elements from array which are out of bound
{
const double lb=lb1;
const double ub=ub1;
std::random_device rd;
std::mt19937_64 mt(rd());
std::uniform_real_distribution<double> distribution(lb, ub);
double array[length];
for(int i = 0; i < length; i++)
{
double d = distribution(mt);
array[i] = d;
}
return (array);
}
double *prod(double a[], int p)
{
int l=lenDouble(a);
double resultArray[l];
for (int i=0;i<l;i++)
{
resultArray[i]=a[i]*p;
}
return resultArray;
}
double *add(double a[], int ad)
{
int l=lenDouble(a);
double resultArray[l];
for (int i=0; i<l;i++)
{
resultArray[i]=a[i]+ad;
}
return resultArray;
}*/
/*double *clamp(double positions[], int lb, int ub)
{
int l=lenDouble(positions);
int j=0;
double positionsI[l];
for (int i=0;i<l;i++)
{
if(double(lb)<=positions[i]<=double(ub))
{
positionsI[j]=positions[i];
j++;
}
}
return positionsI;
}*/
int GWO(int lb,int ub,int dim,int SearchAgents_no,int Max_iter)//obif, lb,ub,dim we will get from benchmark function not from optimizer
{
double Alpha_pos[dim];
double Alpha_score = std::numeric_limits<double>::infinity();
//float Alpha_score=std::numeric_limits<float>::infinity();
double Beta_pos[dim];
double Beta_score = std::numeric_limits<double>::infinity();
double Delta_pos[dim];
double Delta_score = std::numeric_limits<double>::infinity();
//initialize the positions of search agents
double Positions[SearchAgents_no][dim]; //initializing the array holding the Positions of wolves
// Positions=add(prod(randomUniform(0,1,AgentsDim),(ub-lb)),(lb)) -- Equation 1
/* ("ORIGINAL FORMULA") Positions=add(prod(randomUniform(0,1,AgentsDim),(ub-lb)),(lb)) "Implimented like below:-" */
//this block
std::random_device rd;
std::mt19937_64 mt(rd());
std::uniform_real_distribution<double> distribution(lb, ub);
double array[SearchAgents_no][dim];
for(int i = 0; i < SearchAgents_no; i++)
{
for (int j=0;j<dim;j++)
{
double d = distribution(mt);
array[i][j] = d;
}
}
//will perform the randomUniform function
/* ("NOW THE EQUATION SIMPLIFIESAD: ")Positions=add(prod(array,(ub-lb)),(lb)) "it is IMPLIMENTED like below:- "*/
//this block
//int l=lenDouble(array);
double productArray[SearchAgents_no][dim];
for (int i=0;i<SearchAgents_no;i++)
{
for(int j=0;j<dim;j++)
{
productArray[i][j]=array[i][j]*(ub-lb);
}
}
// will perform the prod function
/*("NOW EQUATION FURTHER SIMPLIFIES TO:- ") Positions=add(productArray,lb) "it will be calculated like:- "*/
//this block
//int lpA=lenDouble(productArray); //lpA- length of product array
double addArray[SearchAgents_no][dim];
for (int i=0; i<SearchAgents_no;i++)
{
for (int j=0;j<dim;j++)
{
addArray[i][j]=productArray[i][j]+lb;
}
}
//will perform add function in euation
//# Positions=addArray //randomUniform will return double values between 0 and 1
//delete[] Positions; //deleting the old Postions array and clearing memory //
//double Positions[lpA]; //creating a new Poitions array with different length //
for (int i=0;i<SearchAgents_no;i++) //coping elements from PositionsI to positions // assigning result of equation 1 to positions
{ //
for(int j=0;j<dim;j++)
{
Positions[i][j]=addArray[i][j]; //
}
} //
/*for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for
for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for
for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for
for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for */
double Convergence_curve[Max_iter];
// cout<<"\n--------------------------------------GWO is optimizing F1--------------------------\n";
//cout<<"GWO is optimizing \" "<<objf.__name__<<"\"";
for(int l=0;l<Max_iter;l++)
{
// cout<<" Inside for loop 1\n";
//Positions=clamp(Positions, lb, ub);
//int q=lenDouble(Positions);
/*int k=0;
double positionsI[SearchAgents_no][dim];
for (int i=0;i<SearchAgents_no;i++)
{
for (int j=0;j<dim;j++)
{
if(double(lb)<=Positions[i][j]<=double(ub))
{
positionsI[k]=Positions[i];
j++;
}
}
}
//delete[] Positions; //deleting the old Postions array and clearing memory
//double Positions[q]; //creating a new Poitions array with different length
for (int i=0;i<q;i++) //coping elements from PositionsI to Positions
{
Positions[i]=positionsI[i];
}*/
for(int i=0;i<SearchAgents_no;i++)
{
//cout<< "Inside for loop 2\n ";
//cout<<" in i<SearchAgents_no";
//Return back the search agents that go beyond the boundries of the search space
//Positions[i]=boost::algorithm::clamp(Positions[i], lb, ub);
// Or #include <algorithm> std::clamp(n, lower, upper);
//Calculate objective function for each search agent
double fitness=F1(Positions[i]);
/*
if (i==0)
{
double fitness=F1(Positions[i]); //objf(Positions[i]);
}
else if (i==1)
{
double fitness=F2(Positions[i]);
}
else
{
cout<< "done";
double fitness =0;
}*/
if(fitness<Alpha_score)
{
Alpha_score=fitness; //Update Alpha
//Alpha_pos=Positions[i];
for(int z=0;z<SearchAgents_no;z++)
{
Alpha_pos[z]=Positions[i][z];
}
}
if(fitness>Alpha_score && fitness<Beta_score)
{
Beta_score=fitness; //Update Beta
//Beta_pos=Positions[i];
for(int z=0;z<SearchAgents_no;z++)
{
Beta_pos[z]=Positions[i][z];
}
}
if(fitness>Alpha_score && fitness>Beta_score && fitness<Delta_score)
{
Delta_score=fitness; //Update Delta
//Delta_pos[i]=Positions[i];
for(int z=0;z<SearchAgents_no;z++)
{
Delta_pos[z]=Positions[i][z];
}
}
}
int a=2-1*((2)/Max_iter); //'a' decreases linearly from 2 to 0
//Update the position of search agents including omegas
for (int i=0;i<SearchAgents_no;i++)
{
// cout<<" Inside for loop 3 \n";
for (int j=0;j<dim;j++)
{
// cout<<" Inside for loop 3.1 \n";
int r1=rand()%2; //r1 is random number in [0,1]
int r2=rand()%2; //r1 is random number in [0,1]
int A1=2*a*r1-a; //Equation (3.3)
int C1=2*r2; //Equation (3.4)
int D_alpha=abs(C1*Alpha_pos[j]-Positions[i][j]);//Equation (3.5)-part 1
int X1=Alpha_pos[j]-A1*D_alpha; //Equation (3.6)-part 1
r1=rand()%2;
r2=rand()%2;
int A2=2*a*r1-a; //Equation (3.3)
int C2=2*r2; //Equation (3.4)
int D_beta=abs(C2*Beta_pos[j]-Positions[i][j]); //Equation (3.5)-part 2
int X2=Beta_pos[j]-A2*D_beta; //Equation (3.6)-part 2
r1=rand()%2;
r2=rand()%2;
int A3=2*a*r1-a; //Equation (3.3)
int C3=2*r2; //Equation (3.4)
int D_delta=abs(C2*Delta_pos[j]-Positions[i][j]); //Equation (3.5)-part 3
int X3=Delta_pos[j]-A3*D_delta; //Equation (3.6)-part 3
Positions[i][j]=(X1+X2+X3)/3; //Equation (3.7)
}
}
//Convergence_curve [l]=Alpha_score;
//printf("\n-----------------yoooooooo---------------\n");
//cout<<" \n in l<Max_iter\n";
cout<< "At iteration " << l << "the best fitness is " << Alpha_score<<"\n";
}
}
int main(int args, char* arg[])
{
/*bool benchmarkfunc[]={true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false
,false,false,false,false}; //length of benchmarkfunc array is: 23
//not required currently as we are directly passing F1 function name while calling GWO function
*/
int NumOfRuns=2;
int PopulationSize=50;
int Iterations=100;
//bool Export=false; for exporting results
//bool Flag=false;
for (int j=0;j<23;j++)
{
for(int k=0;k<NumOfRuns;k++)
{
GWO(-100,100,30,PopulationSize,Iterations);
//Flag=true;
}
}
}