-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_AO_ESG.cs
276 lines (235 loc) · 8.7 KB
/
C_AO_ESG.cs
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
//+————————————————————————————————————————————————————————————————————————————+
//| C_AO_ESG |
//| Copyright 2007-2024, Andrey Dik |
//| Copyright 2007-2024, https://www.mql5.com/ru/users/joo |
//—————————————————————————————————————————————————————————————————————————————+
/*
Алгоритм оптимизации "Эволюция Социальных Групп" (Evolution Of Social Groups, ESG)
*/
public struct S_Group
{
public double[] cB;
public double fB;
public int sSize;
public double sRadius;
public void Init(int coords, int groupSize)
{
cB = new double[coords];
fB = double.MinValue;
sSize = groupSize;
}
}
public struct S_Agent
{
public double[] c; //coordinates
public double f; //fitness
public void Init(int coords)
{
c = new double[coords];
f = double.MinValue;
}
}
public class C_AO_ESG
{
public double[] cB = new double[1]; //best coordinates
public double fB; //FF of the best coordinates
public S_Agent[] a = new S_Agent[1]; //agents
public double[] rangeMax = new double[1];
public double[] rangeMin = new double[1];
public double[] rangeStep = new double[1];
private int coords;
private int popSize; //population size
private S_Group[] gr = new S_Group[1]; //group
private int groups; //number of groups
private double groupRadius; //group radius
private double expansionRatio; //expansion ratio
private double power; //power
private bool revision;
public void Init(int coordinatesNumberP, int populationSizeP, int groupsP, double groupRadiusP, double expansionRatioP, double powerP)
{
Random rand = new Random();
fB = double.MinValue;
revision = false;
coords = coordinatesNumberP;
popSize = populationSizeP;
groups = groupsP;
groupRadius = groupRadiusP;
expansionRatio = expansionRatioP;
power = powerP;
int[] partInSwarms = new int[groups];
int particles = popSize / groups;
for (int i = 0; i < groups; i++)
{
partInSwarms[i] = particles;
}
int lost = popSize - particles * groups;
if (lost > 0)
{
int pos = 0;
while (true)
{
partInSwarms[pos]++;
lost--;
pos++;
if (pos >= groups) pos = 0;
if (lost == 0) break;
}
}
rangeMax = new double[coords];
rangeMin = new double[coords];
rangeStep = new double[coords];
cB = new double[coords];
gr = new S_Group[groups];
for (int s = 0; s < groups; s++) gr[s].Init(coords, partInSwarms[s]);
a = new S_Agent[popSize];
for (int i = 0; i < popSize; i++) a[i].Init(coords);
}
public void Moving()
{
if (!revision)
{
int cnt = 0;
double coordinate = 0.0;
double radius = 0.0;
double min = 0.0;
double max = 0.0;
for (int s = 0; s < groups; s++)
{
gr[s].sRadius = groupRadius;
for (int c = 0; c < coords; c++)
{
coordinate = RNDfromCI(rangeMin[c], rangeMax[c]);
gr[s].cB[c] = SeInDiSp(coordinate, rangeMin[c], rangeMax[c], rangeStep[c]);
}
}
for (int s = 0; s < groups; s++)
{
for (int p = 0; p < gr[s].sSize; p++)
{
for (int c = 0; c < coords; c++)
{
radius = (rangeMax[c] - rangeMin[c]) * gr[s].sRadius;
min = gr[s].cB[c] - radius;
max = gr[s].cB[c] + radius;
if (min < rangeMin[c]) min = rangeMin[c];
if (max > rangeMax[c]) max = rangeMax[c];
coordinate = PowerDistribution(gr[s].cB[c], min, max, power);
a[cnt].c[c] = SeInDiSp(coordinate, rangeMin[c], rangeMax[c], rangeStep[c]);
}
cnt++;
}
}
revision = true;
}
}
public void Revision()
{
for (int i = 0; i < popSize; i++)
{
if (a[i].f > fB)
{
fB = a[i].f;
Array.Copy(a[i].c, cB, a[i].c.Length);
}
}
int cnt = 0;
bool impr = false;
for (int s = 0; s < groups; s++)
{
impr = false;
for (int p = 0; p < gr[s].sSize; p++)
{
if (a[cnt].f > gr[s].fB)
{
gr[s].fB = a[cnt].f;
Array.Copy(a[cnt].c, gr[s].cB, a[cnt].c.Length);
impr = true;
}
cnt++;
}
if (!impr) gr[s].sRadius *= expansionRatio;
else gr[s].sRadius = groupRadius;
if (gr[s].sRadius > 0.5) gr[s].sRadius = 0.5;
}
double coordinate = 0.0;
double radius = 0.0;
double min = 0.0;
double max = 0.0;
cnt = 0;
for (int s = 0; s < groups; s++)
{
for (int p = 0; p < gr[s].sSize; p++)
{
for (int c = 0; c < coords; c++)
{
if (RNDfromCI(0.0, 1.0) < 1.0)
{
radius = (rangeMax[c] - rangeMin[c]) * gr[s].sRadius;
min = gr[s].cB[c] - radius;
max = gr[s].cB[c] + radius;
if (min < rangeMin[c]) min = rangeMin[c];
if (max > rangeMax[c]) max = rangeMax[c];
coordinate = PowerDistribution(gr[s].cB[c], min, max, power);
a[cnt].c[c] = SeInDiSp(coordinate, rangeMin[c], rangeMax[c], rangeStep[c]);
}
}
cnt++;
}
}
cnt = 0;
for (int s = 0; s < groups; s++)
{
for (int c = 0; c < coords; c++)
{
int posSw = (int)RNDfromCI(0, groups);
if (posSw >= groups) posSw = groups - 1;
a[cnt].c[c] = gr[posSw].cB[c];
}
cnt += gr[s].sSize;
}
}
private double SeInDiSp(double In, double InMin, double InMax, double Step)
{
if (In <= InMin) return (InMin);
if (In >= InMax) return (InMax);
if (Step == 0.0) return (In);
else return (InMin + Step * Math.Round((In - InMin) / Step));
}
private double RNDfromCI(double min, double max)
{
if (min == max) return (min);
double Min, Max;
if (min > max)
{
Min = max;
Max = min;
}
else
{
Min = min;
Max = max;
}
Random rand = new Random();
return (double)(Min + ((Max - Min) * rand.NextDouble()));
}
private double Scale(double In, double InMIN, double InMAX, double OutMIN, double OutMAX, bool revers)
{
if (OutMIN == OutMAX) return (OutMIN);
if (InMIN == InMAX) return (double)((OutMIN + OutMAX) / 2.0);
else
{
if (In < InMIN) return revers ? OutMAX : OutMIN;
if (In > InMAX) return revers ? OutMIN : OutMAX;
double res = (((In - InMIN) * (OutMAX - OutMIN) / (InMAX - InMIN)) + OutMIN);
if (!revers) return res;
else return OutMAX - res;
}
}
private double PowerDistribution(double In, double outMin, double outMax, double p)
{
double rnd = RNDfromCI(-1.0, 1.0);
double r = Math.Pow(Math.Abs(rnd), p);
if (rnd >= 0.0) return In + Scale(r, 0.0, 1.0, 0.0, outMax - In, false);
else return In - Scale(r, 0.0, 1.0, 0.0, In - outMin, false);
}
}