1
+ // Authors: Siqing Ma
2
+ // Date: 2018-11-15 created, 2018-12-1 updated
3
+ // BSO CORE
4
+
5
+
1
6
#include < iostream>
2
7
#include < vector>
3
8
#include < math.h>
8
13
#include < random>
9
14
#include " kmeans.h"
10
15
#include " function.h"
11
- # define INF 100000000 ;
16
+
12
17
using namespace std ;
18
+ #define INF 1000000000 ;
13
19
14
20
// Gnerate an uniform distribution random variable in [0, 1]
15
21
double uniRand ()
@@ -29,14 +35,11 @@ vector<Point> randomSet(double min, double max, int m, int n)
29
35
vector<double > content;
30
36
for (int j = 0 ; j < n; j++)
31
37
{
32
- default_random_engine e;
33
- random_device r;
34
- e.seed (r ());
38
+ default_random_engine e; random_device r; e.seed (r ());
35
39
uniform_real_distribution<double > u (min, max);
36
40
content.push_back (u (e));
37
41
}
38
- Point point (i, content);
39
- po.push_back (point);
42
+ Point point (i, content); po.push_back (point);
40
43
}
41
44
return po;
42
45
}
@@ -46,15 +49,12 @@ Point randomPoint(double min, double max, int n, int id)
46
49
vector<double > content;
47
50
for (int j = 0 ; j < n; j++)
48
51
{
49
- default_random_engine e;
50
- random_device r;
51
- e.seed (r ());
52
+ default_random_engine e; random_device r; e.seed (r ());
52
53
uniform_real_distribution<double > u (min, max);
53
54
content.push_back (u (e));
54
55
}
55
56
Point point (id, content);
56
57
return point;
57
-
58
58
}
59
59
60
60
Point pointMtply (double coef, Point origin)
@@ -90,33 +90,28 @@ int main(int argc, char *argv[])
90
90
const int np = 100 ; // Number of population
91
91
const int nd = 10 ; // Number of dimension
92
92
const int nc = 5 ; // Number of cluster
93
- // Point values limits
94
- const double rang_l = -5.12 ;
93
+
94
+ const double rang_l = -5.12 ; // Point values limits
95
95
const double rang_r = 5.12 ;
96
- // Pick any deviations you want
97
- const int max_iteration = 250 ;
98
- // Pick any laps you want
99
- const int max_run = 10 ;
96
+
97
+ const int max_iteration = 250 ; // Pick any deviations you want
98
+
99
+ const int max_run = 5 ; // Pick any laps you want
100
100
Function fun;
101
101
102
102
// Runs off laps from here
103
103
for (int idx = 0 ; idx < max_run; idx++)
104
104
{
105
-
106
105
// Genetate a random
107
106
vector<Point> popu = randomSet (rang_l, rang_r, np, nd);
108
107
vector<Point> popu_sort = randomSet (rang_l, rang_r, np, nd);
109
-
110
108
vector<double > prob (nc); // Initialize cluster probability
111
109
vector<double > best (nc); // Index of best individual in each cluster
112
-
113
-
114
110
vector<Point> centers = randomSet (rang_l, rang_r, nc, nd); // Randomly pick a best individual in each cluster
115
111
116
112
// initialize best individual - COPY in each cluster FOR the purpose of introduce random best
117
113
vector<Point> centers_copy = randomSet (rang_l, rang_r, nc, nd);
118
114
vector<double > best_fitness (max_iteration);
119
-
120
115
vector<double > fit_popu (np); // Store fitness value for each individual
121
116
vector<double > fit_popu_sorted (np); // Store fitness value for each sorted individual
122
117
Point indi_temp = randomPoint (rang_l, rang_r, nd, 0 ); // Store temperary individual
@@ -125,20 +120,19 @@ int main(int argc, char *argv[])
125
120
// Evaluate the fitness of n ideas
126
121
for (int times = 0 ; times < np; times++)
127
122
{
128
- fit_popu[times] = fun.fun (popu[times]); // Add the evaluation function!!!!!!!!
123
+ fit_popu[times] = fun.fun (popu[times]);
129
124
}
130
125
131
126
int n_iteration = 0 ;
132
127
while (n_iteration < max_iteration)
133
128
{
134
- // cout << "Iteration: " << n_iteration << endl;
129
+ cout << " Iteration: " << n_iteration << endl;
135
130
KMeans kmeans (nc, np, nd, 100 , centers);
136
131
kmeans.run (popu); // Cluster each population
137
132
138
-
139
133
vector<int > cluster = kmeans.getDependency (); // Containing cluster indices of each observation.
140
- vector<double > fit_values (nc);
141
- vector<int > number_in_cluster (nc); // The number of points in each cluster
134
+ vector<double > fit_values (nc, 0 );
135
+ vector<int > number_in_cluster (nc, 0 ); // The number of points in each cluster
142
136
143
137
// Initialize the fit_values and get the number in each cluster
144
138
for (int clu = 0 ; clu < nc; clu++)
@@ -158,26 +152,23 @@ int main(int argc, char *argv[])
158
152
}
159
153
}
160
154
161
- vector<int > counter_cluster (nc); // Initialization cluster counter
162
- vector<int > acculate_num_cluster (nc); // Accumulated number of individuals in previous clusters
155
+ vector<int > counter_cluster (nc, 0 ); // Initialization cluster counter
156
+ vector<int > acculate_num_cluster (nc, 0 ); // Accumulated number of individuals in previous clusters
163
157
164
158
// Check with every cluster
165
159
acculate_num_cluster[0 ] = 0 ;
166
160
// cout << "acculate_num_cluster: " << 0 << " value: " << acculate_num_cluster[0] << endl;
167
161
for (int times = 1 ; times < nc; times++)
168
162
{
169
- acculate_num_cluster[times] = acculate_num_cluster[times - 1 ] + number_in_cluster[times - 1 ] - 1 ;
163
+ acculate_num_cluster[times] = acculate_num_cluster[times - 1 ] + number_in_cluster[times - 1 ];
170
164
// cout << "acculate_num_cluster: " << times << " value: " << acculate_num_cluster[times] << endl;
171
165
}
172
166
173
-
174
-
175
167
// Evaluate the individuals
176
168
for (int times = 0 ; times < np; times++)
177
169
{
178
170
counter_cluster[cluster[times]]++;
179
171
int temldx = acculate_num_cluster[cluster[times]] + counter_cluster[cluster[times]];
180
- // cout << "times: " << times << " temldx: " << temldx << endl;
181
172
popu_sort[temldx - 1 ] = popu[times]; // The population of individuals sorted according to clusters
182
173
fit_popu_sorted[temldx - 1 ] = fit_popu[times]; // Fitness value for each sorted individual
183
174
}
@@ -190,15 +181,13 @@ int main(int argc, char *argv[])
190
181
// make a copy
191
182
centers_copy.assign (centers.begin (), centers.end ());
192
183
193
-
194
184
// Select one cluster center to be replaced by a randomly generated center
195
185
if (uniRand () < 0.2 )
196
186
{
197
187
int cenldx = floor (uniRand ()*nc);
198
188
centers[cenldx] = randomPoint (rang_l, rang_r, nd, cenldx);
199
189
}
200
190
201
-
202
191
// Calculate cluster probabilities based on number of individuals in each cluster
203
192
for (int times = 0 ; times < nc; times++)
204
193
{
@@ -210,7 +199,7 @@ int main(int argc, char *argv[])
210
199
}
211
200
212
201
213
- int indi_1;
202
+ int indi_1, indi_2, c1, c2 ;
214
203
// Generate new individuals
215
204
for (int times = 0 ; times < np; times++)
216
205
{
@@ -237,10 +226,10 @@ int main(int argc, char *argv[])
237
226
}
238
227
else
239
228
{
240
- int c1 = floor (uniRand () * nc);
229
+ c1 = floor (uniRand () * nc);
241
230
indi_1 = acculate_num_cluster[c1] + floor (uniRand () * number_in_cluster[c1]);
242
- int c2 = floor (uniRand () * nc);
243
- int indi_2 = acculate_num_cluster[c2] + floor (uniRand () * number_in_cluster[c2]);
231
+ c2 = floor (uniRand () * nc);
232
+ indi_2 = acculate_num_cluster[c2] + floor (uniRand () * number_in_cluster[c2]);
244
233
double tem = uniRand ();
245
234
if (uniRand () < 0.5 )
246
235
{
@@ -254,9 +243,7 @@ int main(int argc, char *argv[])
254
243
double coef = 1 / (1 + exp (-((0.5 *max_iteration - n_iteration) / 20 )));
255
244
Point stepSize = pointMtply (coef, randomPoint (0 , 1 , nd, 0 ));
256
245
257
- default_random_engine e;
258
- random_device r;
259
- e.seed (r ());
246
+ default_random_engine e; random_device r; e.seed (r ());
260
247
normal_distribution<double > n (0 , 1 );
261
248
indi_temp = pointAdd (indi_temp, pointMtply (n (e), stepSize));
262
249
@@ -270,14 +257,12 @@ int main(int argc, char *argv[])
270
257
}
271
258
}
272
259
273
-
274
260
for (int times = 0 ; times < nc; times++)
275
261
{
276
262
popu[best[times]] = centers_copy[times];
277
263
fit_popu[best[times]] = fit_values[times];
278
264
}
279
265
280
-
281
266
// record the best fitness in each iteration
282
267
double min = INF;
283
268
for (int k = 0 ; k < fit_values.size (); k++)
@@ -289,6 +274,7 @@ int main(int argc, char *argv[])
289
274
290
275
n_iteration++;
291
276
}
277
+
292
278
cout << " bestfitness: " ;
293
279
for (int succ = 0 ; succ < best_fitness.size (); succ++)
294
280
{
0 commit comments