2
2
// Date: 2018-11-15 created, 2018-12-1 updated
3
3
// BSO CORE
4
4
5
-
6
5
#include < iostream>
7
6
#include < vector>
8
7
#include < math.h>
17
16
using namespace std ;
18
17
#define INF 1000000000 ;
19
18
20
- // Gnerate an uniform distribution random variable in [0, 1]
19
+
20
+ /*
21
+ * Gnerate an uniform distribution random variable in [0, 1]
22
+ */
21
23
double uniRand ()
22
24
{
23
25
default_random_engine e;
@@ -27,6 +29,11 @@ double uniRand()
27
29
return u (e);
28
30
}
29
31
32
+ /* Generate a set of random value sample points
33
+ * [min, max]: the uniform distribution domain
34
+ * m: Sample point number
35
+ * n: Single point value dimension
36
+ */
30
37
vector<Point> randomSet (double min, double max, int m, int n)
31
38
{
32
39
vector<Point> po;
@@ -44,6 +51,11 @@ vector<Point> randomSet(double min, double max, int m, int n)
44
51
return po;
45
52
}
46
53
54
+ /* Generate a random value sample points
55
+ * [min, max]: the uniform distribution domain
56
+ * n: Point value dimension
57
+ * id: Point id
58
+ */
47
59
Point randomPoint (double min, double max, int n, int id)
48
60
{
49
61
vector<double > content;
@@ -57,6 +69,10 @@ Point randomPoint(double min, double max, int n, int id)
57
69
return point;
58
70
}
59
71
72
+ /* Sample point multiply with a number (value * coef)
73
+ * coef:
74
+ * origin:
75
+ */
60
76
Point pointMtply (double coef, Point origin)
61
77
{
62
78
int dim = origin.getDimension ();
@@ -69,6 +85,10 @@ Point pointMtply(double coef, Point origin)
69
85
return ans;
70
86
}
71
87
88
+ /* Sample point multiply with each other (value * value)
89
+ * p1:
90
+ * p2:
91
+ */
72
92
Point pointAdd (Point p1, Point p2)
73
93
{
74
94
int dim = p1.getDimension ();
@@ -97,7 +117,7 @@ int main(int argc, char *argv[])
97
117
const int max_iteration = 250 ; // Pick any deviations you want
98
118
99
119
const int max_run = 5 ; // Pick any laps you want
100
- Function fun ;
120
+ Function Fun ;
101
121
102
122
// Runs off laps from here
103
123
for (int idx = 0 ; idx < max_run; idx++)
@@ -120,7 +140,7 @@ int main(int argc, char *argv[])
120
140
// Evaluate the fitness of n ideas
121
141
for (int times = 0 ; times < np; times++)
122
142
{
123
- fit_popu[times] = fun .fun (popu[times]);
143
+ fit_popu[times] = Fun .fun (popu[times]);
124
144
}
125
145
126
146
int n_iteration = 0 ;
@@ -139,7 +159,6 @@ int main(int argc, char *argv[])
139
159
{
140
160
fit_values[clu] = INF; // Assign a initial big fitness value as best fitness
141
161
number_in_cluster[clu] = kmeans.getCluster (clu).getSize ();
142
- // cout << "number_in_cluster: clu: " << clu << " value: " << number_in_cluster[clu] << endl;
143
162
}
144
163
145
164
for (int times = 0 ; times < np; times++)
@@ -157,11 +176,9 @@ int main(int argc, char *argv[])
157
176
158
177
// Check with every cluster
159
178
acculate_num_cluster[0 ] = 0 ;
160
- // cout << "acculate_num_cluster: " << 0 << " value: " << acculate_num_cluster[0] << endl;
161
179
for (int times = 1 ; times < nc; times++)
162
180
{
163
181
acculate_num_cluster[times] = acculate_num_cluster[times - 1 ] + number_in_cluster[times - 1 ];
164
- // cout << "acculate_num_cluster: " << times << " value: " << acculate_num_cluster[times] << endl;
165
182
}
166
183
167
184
// Evaluate the individuals
@@ -230,6 +247,7 @@ int main(int argc, char *argv[])
230
247
indi_1 = acculate_num_cluster[c1] + floor (uniRand () * number_in_cluster[c1]);
231
248
c2 = floor (uniRand () * nc);
232
249
indi_2 = acculate_num_cluster[c2] + floor (uniRand () * number_in_cluster[c2]);
250
+
233
251
double tem = uniRand ();
234
252
if (uniRand () < 0.5 )
235
253
{
@@ -247,7 +265,7 @@ int main(int argc, char *argv[])
247
265
normal_distribution<double > n (0 , 1 );
248
266
indi_temp = pointAdd (indi_temp, pointMtply (n (e), stepSize));
249
267
250
- double fv = fun .fun (indi_temp);
268
+ double fv = Fun .fun (indi_temp);
251
269
252
270
if (fv < fit_popu[times])
253
271
{
@@ -267,15 +285,14 @@ int main(int argc, char *argv[])
267
285
double min = INF;
268
286
for (int k = 0 ; k < fit_values.size (); k++)
269
287
{
270
- // cout << "fv: " << fit_values[k] << endl;
271
288
if (min > fit_values[k]) min = fit_values[k];
272
289
}
273
290
best_fitness[n_iteration] = min;
274
291
275
292
n_iteration++;
276
293
}
277
294
278
- cout << " bestfitness : " ;
295
+ cout << " Best Fitness : " ;
279
296
for (int succ = 0 ; succ < best_fitness.size (); succ++)
280
297
{
281
298
cout << best_fitness[succ] << " " ;
0 commit comments