Skip to content

Commit 7d63528

Browse files
committed
2018-12-1
1 parent 61fdb2f commit 7d63528

File tree

2 files changed

+69
-109
lines changed

2 files changed

+69
-109
lines changed

bsocore.cpp

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Authors: Siqing Ma
2+
// Date: 2018-11-15 created, 2018-12-1 updated
3+
// BSO CORE
4+
5+
16
#include <iostream>
27
#include <vector>
38
#include <math.h>
@@ -8,8 +13,9 @@
813
#include <random>
914
#include "kmeans.h"
1015
#include "function.h"
11-
#define INF 100000000;
16+
1217
using namespace std;
18+
#define INF 1000000000;
1319

1420
// Gnerate an uniform distribution random variable in [0, 1]
1521
double uniRand()
@@ -29,14 +35,11 @@ vector<Point> randomSet(double min, double max, int m, int n)
2935
vector<double> content;
3036
for (int j = 0; j < n; j++)
3137
{
32-
default_random_engine e;
33-
random_device r;
34-
e.seed(r());
38+
default_random_engine e; random_device r; e.seed(r());
3539
uniform_real_distribution<double> u(min, max);
3640
content.push_back(u(e));
3741
}
38-
Point point(i, content);
39-
po.push_back(point);
42+
Point point(i, content); po.push_back(point);
4043
}
4144
return po;
4245
}
@@ -46,15 +49,12 @@ Point randomPoint(double min, double max, int n, int id)
4649
vector<double> content;
4750
for (int j = 0; j < n; j++)
4851
{
49-
default_random_engine e;
50-
random_device r;
51-
e.seed(r());
52+
default_random_engine e; random_device r; e.seed(r());
5253
uniform_real_distribution<double> u(min, max);
5354
content.push_back(u(e));
5455
}
5556
Point point(id, content);
5657
return point;
57-
5858
}
5959

6060
Point pointMtply(double coef, Point origin)
@@ -90,33 +90,28 @@ int main(int argc, char *argv[])
9090
const int np = 100; // Number of population
9191
const int nd = 10; // Number of dimension
9292
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
9595
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
100100
Function fun;
101101

102102
// Runs off laps from here
103103
for (int idx = 0; idx < max_run; idx++)
104104
{
105-
106105
// Genetate a random
107106
vector<Point> popu = randomSet(rang_l, rang_r, np, nd);
108107
vector<Point> popu_sort = randomSet(rang_l, rang_r, np, nd);
109-
110108
vector<double> prob(nc); // Initialize cluster probability
111109
vector<double> best(nc); // Index of best individual in each cluster
112-
113-
114110
vector<Point> centers = randomSet(rang_l, rang_r, nc, nd); // Randomly pick a best individual in each cluster
115111

116112
// initialize best individual - COPY in each cluster FOR the purpose of introduce random best
117113
vector<Point> centers_copy = randomSet(rang_l, rang_r, nc, nd);
118114
vector<double> best_fitness(max_iteration);
119-
120115
vector<double> fit_popu(np); // Store fitness value for each individual
121116
vector<double> fit_popu_sorted(np); // Store fitness value for each sorted individual
122117
Point indi_temp = randomPoint(rang_l, rang_r, nd, 0); // Store temperary individual
@@ -125,20 +120,19 @@ int main(int argc, char *argv[])
125120
// Evaluate the fitness of n ideas
126121
for (int times = 0; times < np; times++)
127122
{
128-
fit_popu[times] = fun.fun(popu[times]); // Add the evaluation function!!!!!!!!
123+
fit_popu[times] = fun.fun(popu[times]);
129124
}
130125

131126
int n_iteration = 0;
132127
while (n_iteration < max_iteration)
133128
{
134-
//cout << "Iteration: " << n_iteration << endl;
129+
cout << "Iteration: " << n_iteration << endl;
135130
KMeans kmeans(nc, np, nd, 100, centers);
136131
kmeans.run(popu); // Cluster each population
137132

138-
139133
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
142136

143137
// Initialize the fit_values and get the number in each cluster
144138
for (int clu = 0; clu < nc; clu++)
@@ -158,26 +152,23 @@ int main(int argc, char *argv[])
158152
}
159153
}
160154

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
163157

164158
// Check with every cluster
165159
acculate_num_cluster[0] = 0;
166160
//cout << "acculate_num_cluster: " << 0 << " value: " << acculate_num_cluster[0] << endl;
167161
for (int times = 1; times < nc; times++)
168162
{
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];
170164
//cout << "acculate_num_cluster: " << times << " value: " << acculate_num_cluster[times] << endl;
171165
}
172166

173-
174-
175167
// Evaluate the individuals
176168
for (int times = 0; times < np; times++)
177169
{
178170
counter_cluster[cluster[times]]++;
179171
int temldx = acculate_num_cluster[cluster[times]] + counter_cluster[cluster[times]];
180-
//cout << "times: " << times << " temldx: " << temldx << endl;
181172
popu_sort[temldx - 1] = popu[times]; // The population of individuals sorted according to clusters
182173
fit_popu_sorted[temldx - 1] = fit_popu[times]; // Fitness value for each sorted individual
183174
}
@@ -190,15 +181,13 @@ int main(int argc, char *argv[])
190181
// make a copy
191182
centers_copy.assign(centers.begin(), centers.end());
192183

193-
194184
// Select one cluster center to be replaced by a randomly generated center
195185
if (uniRand() < 0.2)
196186
{
197187
int cenldx = floor(uniRand()*nc);
198188
centers[cenldx] = randomPoint(rang_l, rang_r, nd, cenldx);
199189
}
200190

201-
202191
// Calculate cluster probabilities based on number of individuals in each cluster
203192
for (int times = 0; times < nc; times++)
204193
{
@@ -210,7 +199,7 @@ int main(int argc, char *argv[])
210199
}
211200

212201

213-
int indi_1;
202+
int indi_1, indi_2, c1, c2;
214203
// Generate new individuals
215204
for (int times = 0; times < np; times++)
216205
{
@@ -237,10 +226,10 @@ int main(int argc, char *argv[])
237226
}
238227
else
239228
{
240-
int c1 = floor(uniRand() * nc);
229+
c1 = floor(uniRand() * nc);
241230
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]);
244233
double tem = uniRand();
245234
if (uniRand() < 0.5)
246235
{
@@ -254,9 +243,7 @@ int main(int argc, char *argv[])
254243
double coef = 1 / (1 + exp(-((0.5*max_iteration - n_iteration) / 20)));
255244
Point stepSize = pointMtply(coef, randomPoint(0, 1, nd, 0));
256245

257-
default_random_engine e;
258-
random_device r;
259-
e.seed(r());
246+
default_random_engine e; random_device r; e.seed(r());
260247
normal_distribution<double> n(0, 1);
261248
indi_temp = pointAdd(indi_temp, pointMtply(n(e), stepSize));
262249

@@ -270,14 +257,12 @@ int main(int argc, char *argv[])
270257
}
271258
}
272259

273-
274260
for (int times = 0; times < nc; times++)
275261
{
276262
popu[best[times]] = centers_copy[times];
277263
fit_popu[best[times]] = fit_values[times];
278264
}
279265

280-
281266
// record the best fitness in each iteration
282267
double min = INF;
283268
for (int k = 0; k < fit_values.size(); k++)
@@ -289,6 +274,7 @@ int main(int argc, char *argv[])
289274

290275
n_iteration++;
291276
}
277+
292278
cout << "bestfitness: ";
293279
for (int succ = 0; succ < best_fitness.size(); succ++)
294280
{

0 commit comments

Comments
 (0)