Skip to content

Commit 6d59f4d

Browse files
committed
2018-12-1
1 parent 7d63528 commit 6d59f4d

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

bsocore.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Date: 2018-11-15 created, 2018-12-1 updated
33
// BSO CORE
44

5-
65
#include <iostream>
76
#include <vector>
87
#include <math.h>
@@ -17,7 +16,10 @@
1716
using namespace std;
1817
#define INF 1000000000;
1918

20-
// Gnerate an uniform distribution random variable in [0, 1]
19+
20+
/*
21+
* Gnerate an uniform distribution random variable in [0, 1]
22+
*/
2123
double uniRand()
2224
{
2325
default_random_engine e;
@@ -27,6 +29,11 @@ double uniRand()
2729
return u(e);
2830
}
2931

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+
*/
3037
vector<Point> randomSet(double min, double max, int m, int n)
3138
{
3239
vector<Point> po;
@@ -44,6 +51,11 @@ vector<Point> randomSet(double min, double max, int m, int n)
4451
return po;
4552
}
4653

54+
/* Generate a random value sample points
55+
* [min, max]: the uniform distribution domain
56+
* n: Point value dimension
57+
* id: Point id
58+
*/
4759
Point randomPoint(double min, double max, int n, int id)
4860
{
4961
vector<double> content;
@@ -57,6 +69,10 @@ Point randomPoint(double min, double max, int n, int id)
5769
return point;
5870
}
5971

72+
/* Sample point multiply with a number (value * coef)
73+
* coef:
74+
* origin:
75+
*/
6076
Point pointMtply(double coef, Point origin)
6177
{
6278
int dim = origin.getDimension();
@@ -69,6 +85,10 @@ Point pointMtply(double coef, Point origin)
6985
return ans;
7086
}
7187

88+
/* Sample point multiply with each other (value * value)
89+
* p1:
90+
* p2:
91+
*/
7292
Point pointAdd(Point p1, Point p2)
7393
{
7494
int dim = p1.getDimension();
@@ -97,7 +117,7 @@ int main(int argc, char *argv[])
97117
const int max_iteration = 250; // Pick any deviations you want
98118

99119
const int max_run = 5; // Pick any laps you want
100-
Function fun;
120+
Function Fun;
101121

102122
// Runs off laps from here
103123
for (int idx = 0; idx < max_run; idx++)
@@ -120,7 +140,7 @@ int main(int argc, char *argv[])
120140
// Evaluate the fitness of n ideas
121141
for (int times = 0; times < np; times++)
122142
{
123-
fit_popu[times] = fun.fun(popu[times]);
143+
fit_popu[times] = Fun.fun(popu[times]);
124144
}
125145

126146
int n_iteration = 0;
@@ -139,7 +159,6 @@ int main(int argc, char *argv[])
139159
{
140160
fit_values[clu] = INF; // Assign a initial big fitness value as best fitness
141161
number_in_cluster[clu] = kmeans.getCluster(clu).getSize();
142-
//cout << "number_in_cluster: clu: " << clu << " value: " << number_in_cluster[clu] << endl;
143162
}
144163

145164
for (int times = 0; times < np; times++)
@@ -157,11 +176,9 @@ int main(int argc, char *argv[])
157176

158177
// Check with every cluster
159178
acculate_num_cluster[0] = 0;
160-
//cout << "acculate_num_cluster: " << 0 << " value: " << acculate_num_cluster[0] << endl;
161179
for (int times = 1; times < nc; times++)
162180
{
163181
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;
165182
}
166183

167184
// Evaluate the individuals
@@ -230,6 +247,7 @@ int main(int argc, char *argv[])
230247
indi_1 = acculate_num_cluster[c1] + floor(uniRand() * number_in_cluster[c1]);
231248
c2 = floor(uniRand() * nc);
232249
indi_2 = acculate_num_cluster[c2] + floor(uniRand() * number_in_cluster[c2]);
250+
233251
double tem = uniRand();
234252
if (uniRand() < 0.5)
235253
{
@@ -247,7 +265,7 @@ int main(int argc, char *argv[])
247265
normal_distribution<double> n(0, 1);
248266
indi_temp = pointAdd(indi_temp, pointMtply(n(e), stepSize));
249267

250-
double fv = fun.fun(indi_temp);
268+
double fv = Fun.fun(indi_temp);
251269

252270
if (fv < fit_popu[times])
253271
{
@@ -267,15 +285,14 @@ int main(int argc, char *argv[])
267285
double min = INF;
268286
for (int k = 0; k < fit_values.size(); k++)
269287
{
270-
//cout << "fv: " << fit_values[k] << endl;
271288
if (min > fit_values[k]) min = fit_values[k];
272289
}
273290
best_fitness[n_iteration] = min;
274291

275292
n_iteration++;
276293
}
277294

278-
cout << "bestfitness: ";
295+
cout << "Best Fitness: ";
279296
for (int succ = 0; succ < best_fitness.size(); succ++)
280297
{
281298
cout << best_fitness[succ] << " ";

function.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
// Authors: Siqing Ma
2+
// Date: 2018-11-15 created, 2018-12-1 updated
3+
// BSO FUNCTION
4+
5+
16
#include <iostream>
27
#include <vector>
38
#include <math.h>
49
#include <stdlib.h>
510
#include <time.h>
611
#include <algorithm>
7-
#include <string>
812
#include "kmeans.h"
913
#include "function.h"
10-
#include <stack>
1114

1215

1316
using namespace std;
1417

15-
Function::Function()
16-
{
17-
18-
}
18+
Function::Function() {}
1919

2020
double Function::fun(Point point)
2121
{
22-
//cout << "fun: " << endl;
2322
double z = 0;
2423
for (int i = 0; i < 10; i++)
2524
{

kmeans.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Authors: Siqing Ma
2+
// Date: 2018-11-15 created, 2018-12-1 updated
3+
// Kmeans algorithm
4+
// Distance mode: "cityblock"
5+
// EmptyAction mode: "singleton"
6+
17
#include <iostream>
28
#include <vector>
39
#include <math.h>
@@ -7,11 +13,6 @@
713
#include <string>
814
#include "kmeans.h"
915
using namespace std;
10-
/**
11-
Created:2018-11-17, Updated:2018-11-17
12-
Author: Siqing Ma
13-
Version: V0.1T
14-
**/
1516

1617
Point::Point(int id_point, vector<double>& values)
1718
{
@@ -105,6 +106,7 @@ map<int, Point> Cluster::getPoints()
105106
}
106107

107108

109+
108110
KMeans::KMeans(int nc, int np, int nd, int max_iterations, vector<Point> centers)
109111
{
110112
this->nc = nc;
@@ -114,7 +116,6 @@ KMeans::KMeans(int nc, int np, int nd, int max_iterations, vector<Point> centers
114116
this->init_centres = centers;
115117
}
116118

117-
// return ID of nearest center
118119
int KMeans::getNearestCentreId(Point point)
119120
{
120121
double sum = 0.0, min_dist;

0 commit comments

Comments
 (0)