7
7
#include < string>
8
8
#include < random>
9
9
#include " kmeans.h"
10
+ #include " function.h"
10
11
#define INF 100000000 ;
11
12
using namespace std ;
12
- double randa ()
13
+ double uniRand ()
13
14
{
14
15
default_random_engine e;
15
16
random_device r;
@@ -18,7 +19,7 @@ double randa()
18
19
return u (e);
19
20
}
20
21
21
- vector<Point> mat_random (double min, double max, int m, int n)
22
+ vector<Point> matRandom (double min, double max, int m, int n)
22
23
{
23
24
vector<Point> po;
24
25
for (int i = 0 ; i < m; i++)
@@ -38,7 +39,7 @@ vector<Point> mat_random(double min, double max, int m, int n)
38
39
return po;
39
40
}
40
41
41
- Point po_random (double min, double max, int m , int n, int id)
42
+ Point pointRandom (double min, double max, int n, int id)
42
43
{
43
44
vector<double > content;
44
45
for (int j = 0 ; j < n; j++)
@@ -54,6 +55,31 @@ Point po_random(double min, double max, int m, int n, int id)
54
55
55
56
}
56
57
58
+ Point pointMtply (double coef, Point origin)
59
+ {
60
+ int dim = origin.getDimension ();
61
+ vector<double > newvel (dim);
62
+ for (int i = 0 ; i < dim; i++)
63
+ {
64
+ newvel[i] = coef * origin.getValue (i);
65
+ }
66
+ Point ans (-1 , newvel); // 记得重置id值
67
+ return ans;
68
+ }
69
+
70
+ Point pointAdd (Point p1, Point p2)
71
+ {
72
+ int dim = p1.getDimension ();
73
+ vector<double > newvel (dim);
74
+
75
+ for (int i = 0 ; i < dim; i++)
76
+ {
77
+ newvel[i] = p1.getValue (i) + p2.getValue (i);
78
+ }
79
+ Point ans (-1 , newvel); // 记得重置id值
80
+ return ans;
81
+ }
82
+
57
83
58
84
int main (int argc, char *argv[])
59
85
{
@@ -62,37 +88,38 @@ int main(int argc, char *argv[])
62
88
const int np = 48 ; // Number of population
63
89
const int nd = 24 ; // Number of dimension
64
90
const int nc = 3 ; // Number of cluster
65
- // Ideas limits
91
+ // Point values limits
66
92
const double rang_l = -5.12 ;
67
93
const double rang_r = 5.12 ;
68
94
// Pick any deviations you want
69
95
const int max_iteration = 8 ;
70
96
// Pick any laps you want
71
97
const int max_run = 1 ;
98
+ Function fun;
72
99
73
100
// Runs off laps from here
74
101
for (int idx = 0 ; idx < max_run; idx++)
75
102
{
76
103
77
104
// Genetate a random
78
- vector<Point> po = mat_random (rang_l, rang_r, np, nd);
79
- vector<Point> po_sort = mat_random (rang_l, rang_r, np, nd);
105
+ vector<Point> po = matRandom (rang_l, rang_r, np, nd);
106
+ vector<Point> po_sort = matRandom (rang_l, rang_r, np, nd);
80
107
81
108
vector<double > prob (nc);
82
109
vector<double > best (nc);
83
110
84
- vector<Point> centers = mat_random (rang_l, rang_r, nc, nd);
85
- vector<Point> centers_copy = mat_random (rang_l, rang_r, nc, nd);
111
+ vector<Point> centers = matRandom (rang_l, rang_r, nc, nd);
112
+ vector<Point> centers_copy = matRandom (rang_l, rang_r, nc, nd);
86
113
vector<double > best_fitness (max_iteration);
87
114
88
115
vector<double > fit_popu (np);
89
116
vector<double > fit_popu_sorted (np);
90
- Point in_te = po_random (rang_l, rang_r, nc , nd, 0 );
117
+ Point indi_temp = pointRandom (rang_l, rang_r, nd, 0 );
91
118
92
119
// Evaluate the fitness of n ideas
93
120
for (int times = 0 ; times < np; times++)
94
121
{
95
- fit_popu[times] = 1 ; // Add the evaluation function!!!!!!!!
122
+ fit_popu[times] = fun. fun (po[idx]) ; // Add the evaluation function!!!!!!!!
96
123
}
97
124
98
125
int n_iteration = 0 ;
@@ -158,10 +185,10 @@ int main(int argc, char *argv[])
158
185
159
186
160
187
// Select one cluster center to be replaced by a randomly generated center
161
- if (randa () < 0.2 )
188
+ if (uniRand () < 0.2 )
162
189
{
163
- int cenldx = floor (randa ()*nc);
164
- centers[cenldx] = po_random (rang_l, rang_r, nc , nd, cenldx);
190
+ int cenldx = floor (uniRand ()*nc);
191
+ centers[cenldx] = pointRandom (rang_l, rang_r, nd, cenldx);
165
192
}
166
193
167
194
// Calculate cluster probabilities based on number of individuals in each cluster
@@ -178,48 +205,71 @@ int main(int argc, char *argv[])
178
205
// Generate new individuals
179
206
for (int times = 0 ; times < np; times++)
180
207
{
181
- double r_1 = randa ();
208
+ double r_1 = uniRand ();
182
209
if (r_1 < 0.8 )
183
210
{
184
- double r = randa ();
211
+ double r = uniRand ();
185
212
for (int idj = 0 ; idj < nc; idj++)
186
213
{
187
214
if (r < prob[idj])
188
215
{
189
- if (randa () < 0.4 )
216
+ if (uniRand () < 0.4 )
190
217
{
191
- in_te = centers[idj];
218
+ indi_temp = centers[idj];
192
219
}
193
220
else
194
221
{
195
- indi_1 = acculate_num_cluster[idj] + floor (randa () * number_in_cluster[idj]);
196
- in_te = po_sort[indi_1];
222
+ indi_1 = acculate_num_cluster[idj] + floor (uniRand () * number_in_cluster[idj]);
223
+ indi_temp = po_sort[indi_1];
197
224
}
198
225
break ;
199
226
}
200
227
}
201
228
}
202
229
else
203
230
{
204
- int c1 = floor (randa () * nc);
205
- indi_1 = acculate_num_cluster[c1] + ceil (randa () * number_in_cluster[c1]);
206
- int c2 = floor (randa () * nc);
207
- int indi_2 = acculate_num_cluster[c2] + ceil (randa () * number_in_cluster[c2]);
208
- double tem = randa ();
209
- if (randa () < 0.5 )
231
+ int c1 = floor (uniRand () * nc);
232
+ indi_1 = acculate_num_cluster[c1] + ceil (uniRand () * number_in_cluster[c1]);
233
+ int c2 = floor (uniRand () * nc);
234
+ int indi_2 = acculate_num_cluster[c2] + ceil (uniRand () * number_in_cluster[c2]);
235
+ double tem = uniRand ();
236
+ if (uniRand () < 0.5 )
210
237
{
211
- // 写一个能将一个样本点里值全部乘系数的函数。
238
+ indi_temp = pointAdd (pointMtply (tem, centers[c1]), pointMtply ((1 -tem), centers[c2]));
239
+ }
240
+ else
241
+ {
242
+ indi_temp = pointAdd (pointMtply (tem, po_sort[indi_1]), pointMtply ((1 - tem), po_sort[indi_2]));
212
243
}
213
244
}
214
-
215
-
216
- }
217
-
218
-
245
+ double coef = 1 / (1 + exp (-((0.5 *max_iteration - n_iteration) / 20 )));
246
+ Point stepSize = pointMtply (coef, pointRandom (0 , 1 , nd, 0 ));
219
247
248
+ default_random_engine e;
249
+ random_device r;
250
+ e.seed (r ());
251
+ normal_distribution<double > n (0 , 1 );
252
+ indi_temp = pointAdd (indi_temp, pointMtply (n (e), stepSize));
220
253
254
+ double fv = fun.fun (indi_temp); // 添加评价函数
221
255
256
+ if (fv < fit_popu[times])
257
+ {
258
+ fit_popu[times] = fv;
259
+ indi_temp.setID (times);
260
+ po[times] = indi_temp;
261
+ }
262
+ }
263
+
264
+ for (int times = 0 ; times < nc; times++)
265
+ {
266
+ po[best[times]] = centers_copy[times];
267
+ fit_popu[best[times]] = fit_values[times];
268
+ }
269
+
222
270
n_iteration++;
271
+ // record the best fitness in each iteration
272
+ best_fitness[n_iteration] = *min_element (fit_values.begin (), fit_values.end ());
223
273
}
224
274
}
225
275
0 commit comments