Skip to content

Commit 450a2f2

Browse files
committed
Complete first draft
1 parent 0a2d45d commit 450a2f2

File tree

5 files changed

+121
-34
lines changed

5 files changed

+121
-34
lines changed

bsomine.cpp renamed to bsocore.cpp

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#include <string>
88
#include <random>
99
#include "kmeans.h"
10+
#include "function.h"
1011
#define INF 100000000;
1112
using namespace std;
12-
double randa()
13+
double uniRand()
1314
{
1415
default_random_engine e;
1516
random_device r;
@@ -18,7 +19,7 @@ double randa()
1819
return u(e);
1920
}
2021

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)
2223
{
2324
vector<Point> po;
2425
for (int i = 0; i < m; i++)
@@ -38,7 +39,7 @@ vector<Point> mat_random(double min, double max, int m, int n)
3839
return po;
3940
}
4041

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)
4243
{
4344
vector<double> content;
4445
for (int j = 0; j < n; j++)
@@ -54,6 +55,31 @@ Point po_random(double min, double max, int m, int n, int id)
5455

5556
}
5657

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+
5783

5884
int main(int argc, char *argv[])
5985
{
@@ -62,37 +88,38 @@ int main(int argc, char *argv[])
6288
const int np = 48; // Number of population
6389
const int nd = 24; // Number of dimension
6490
const int nc = 3; // Number of cluster
65-
// Ideas limits
91+
// Point values limits
6692
const double rang_l = -5.12;
6793
const double rang_r = 5.12;
6894
// Pick any deviations you want
6995
const int max_iteration = 8;
7096
// Pick any laps you want
7197
const int max_run = 1;
98+
Function fun;
7299

73100
// Runs off laps from here
74101
for (int idx = 0; idx < max_run; idx++)
75102
{
76103

77104
// 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);
80107

81108
vector<double> prob(nc);
82109
vector<double> best(nc);
83110

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);
86113
vector<double> best_fitness(max_iteration);
87114

88115
vector<double> fit_popu(np);
89116
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);
91118

92119
// Evaluate the fitness of n ideas
93120
for (int times = 0; times < np; times++)
94121
{
95-
fit_popu[times] = 1; // Add the evaluation function!!!!!!!!
122+
fit_popu[times] = fun.fun(po[idx]); // Add the evaluation function!!!!!!!!
96123
}
97124

98125
int n_iteration = 0;
@@ -158,10 +185,10 @@ int main(int argc, char *argv[])
158185

159186

160187
// Select one cluster center to be replaced by a randomly generated center
161-
if (randa() < 0.2)
188+
if (uniRand() < 0.2)
162189
{
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);
165192
}
166193

167194
// Calculate cluster probabilities based on number of individuals in each cluster
@@ -178,48 +205,71 @@ int main(int argc, char *argv[])
178205
// Generate new individuals
179206
for (int times = 0; times < np; times++)
180207
{
181-
double r_1 = randa();
208+
double r_1 = uniRand();
182209
if (r_1 < 0.8)
183210
{
184-
double r = randa();
211+
double r = uniRand();
185212
for (int idj = 0; idj < nc; idj++)
186213
{
187214
if (r < prob[idj])
188215
{
189-
if (randa() < 0.4)
216+
if (uniRand() < 0.4)
190217
{
191-
in_te = centers[idj];
218+
indi_temp = centers[idj];
192219
}
193220
else
194221
{
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];
197224
}
198225
break;
199226
}
200227
}
201228
}
202229
else
203230
{
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)
210237
{
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]));
212243
}
213244
}
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));
219247

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));
220253

254+
double fv = fun.fun(indi_temp); //添加评价函数
221255

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+
222270
n_iteration++;
271+
// record the best fitness in each iteration
272+
best_fitness[n_iteration] = *min_element(fit_values.begin(), fit_values.end());
223273
}
224274
}
225275

function.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
#include <time.h>
6+
#include <algorithm>
7+
#include <string>
8+
#include "kmeans.h"
9+
#include "function.h"
10+
using namespace std;
11+
12+
Function::Function()
13+
{
14+
15+
}
16+
17+
double Function::fun(Point point)
18+
{
19+
return 1;
20+
}

function.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <vector>
4+
#include <math.h>
5+
#include <stdlib.h>
6+
#include <time.h>
7+
#include <algorithm>
8+
#include <string>
9+
#include "kmeans.h"
10+
using namespace std;
11+
12+
class Function
13+
{
14+
public:
15+
Function();
16+
double fun(Point point);
17+
};

kmeans.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ int Point::getDimension()
5151
return nd;
5252
}
5353

54-
void Point::addValue(double value)
54+
void Point::setValue(int index,double value)
5555
{
56-
values.push_back(value);
56+
values[index] = value;
5757
}
5858

5959

kmeans.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Point
2828
int getCluster();
2929
double getValue(int index);
3030
int getDimension();
31-
void addValue(double value);
31+
void setValue(int index, double value);
3232
};
3333

3434
class Cluster

0 commit comments

Comments
 (0)