-
Notifications
You must be signed in to change notification settings - Fork 7
/
jBGWO2.m
79 lines (74 loc) · 1.79 KB
/
jBGWO2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function [sFeat,Sf,Nf,curve] = jBGWO2(feat,label,N,max_Iter,HO)
fun = @jFitnessFunction;
dim = size(feat,2);
X = zeros(N,dim);
for i = 1:N
for d = 1:dim
if rand() > 0.5
X(i,d) = 1;
end
end
end
fit = zeros(1,N);
for i = 1:N
fit(i) = fun(feat,label,X(i,:),HO);
end
[~, idx] = sort(fit,'ascend');
Xalpha = X(idx(1),:);
Xbeta = X(idx(2),:);
Xdelta = X(idx(3),:);
Falpha = fit(idx(1));
Fbeta = fit(idx(2));
Fdelta = fit(idx(3));
curve = inf;
t = 1;
%---Iterations start-------------------------------------------------
while t <= max_Iter
a = 2 - 2 * (t / max_Iter);
for i = 1:N
for d = 1:dim
C1 = 2 * rand();
C2 = 2 * rand();
C3 = 2 * rand();
Dalpha = abs(C1 * Xalpha(d) - X(i,d));
Dbeta = abs(C2 * Xbeta(d) - X(i,d));
Ddelta = abs(C3 * Xdelta(d) - X(i,d));
A1 = 2 * a * rand() - a;
A2 = 2 * a * rand() - a;
A3 = 2 * a * rand() - a;
X1 = Xalpha(d) - A1 * Dalpha;
X2 = Xbeta(d) - A2 * Dbeta;
X3 = Xdelta(d) - A3 * Ddelta;
Xn = (X1 + X2 + X3) / 3;
TF = 1 / (1 + exp(-10 * (Xn - 0.5)));
if TF >= rand()
X(i,d) = 1;
else
X(i,d) = 0;
end
end
end
for i = 1:N
fit(i) = fun(feat,label,X(i,:),HO);
if fit(i) < Falpha
Falpha = fit(i);
Xalpha = X(i,:);
end
if fit(i) < Fbeta && fit(i) > Falpha
Fbeta = fit(i);
Xbeta = X(i,:);
end
if fit(i) < Fdelta && fit(i) > Falpha && fit(i) > Fbeta
Fdelta = fit(i);
Xdelta = X(i,:);
end
end
curve(t) = Falpha;
fprintf('\nIteration %d Best (BGWO2)= %f',t,curve(t))
t = t + 1;
end
Pos = 1:dim;
Sf = Pos(Xalpha == 1);
Nf = length(Sf);
sFeat = feat(:,Sf);
end