-
Notifications
You must be signed in to change notification settings - Fork 36
/
jWeightedSuperpositionAttraction.m
147 lines (141 loc) · 3.38 KB
/
jWeightedSuperpositionAttraction.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
%[2017]-"Weighted Superposition Attraction (WSA): A swarm
%intelligence algorithm for optimization problems – Part 1:
%Unconstrained optimization"
% (8/12/2020)
function WSA = jWeightedSuperpositionAttraction(feat,label,opts)
% Parameters
lb = 0;
ub = 1;
thres = 0.5;
tau = 0.8; % constant
sl = 0.035; % step length
phi = 0.001; % constant
lambda = 0.75; % constant
if isfield(opts,'T'), max_Iter = opts.T; end
if isfield(opts,'N'), N = opts.N; end
if isfield(opts,'tau'), tau = opts.tau; end
if isfield(opts,'sl'), sl = opts.sl; end
if isfield(opts,'phi'), phi = opts.phi; end
if isfield(opts,'lambda'), lambda = opts.lambda; end
if isfield(opts,'thres'), thres = opts.thres; end
% Objective function
fun = @jFitnessFunction;
% Number of dimensions
dim = size(feat,2);
% Initial
X = zeros(N,dim);
for i = 1:N
for d = 1:dim
X(i,d) = lb + (ub - lb) * rand();
end
end
% Fitness
fit = zeros(1,N);
fitG = inf;
for i = 1:N
fit(i) = fun(feat,label,(X(i,:) > thres),opts);
% Best update
if fit(i) < fitG
fitG = fit(i);
Xgb = X(i,:);
end
end
% Pre
curve = zeros(1,max_Iter);
curve(1) = fitG;
t = 2;
% Iterations
while t <= max_Iter
% Rank solution based on fitness
[fit, idx] = sort(fit,'ascend');
X = X(idx,:);
% {1} Target point determination: Figure 2
w = zeros(1,N);
Xtar = zeros(1,dim);
for i = 1:N
% Assign weight based on rank
w(i) = i ^ (-1 * tau);
% Create target
for d = 1:dim
Xtar(d) = Xtar(d) + X(i,d) * w(i);
end
end
% Boundary
Xtar(Xtar > ub) = ub;
Xtar(Xtar < lb) = lb;
% Fitness
fitT = fun(feat,label,(Xtar > thres),opts);
% Best update
if fitT < fitG
fitG = fitT;
Xgb = Xtar;
end
% {2} Compute search direction: Figure 4
gap = zeros(N,dim);
direct = zeros(N,dim);
for i = 1:N
if fit(i) >= fitT
for d = 1:dim
% Compute gap
gap(i,d) = Xtar(d) - X(i,d);
% Compute direction
direct(i,d) = sign(gap(i,d));
end
elseif fit(i) < fitT
if rand() < exp(fit(i) - fitT)
for d = 1:dim
% Compute gap
gap(i,d) = Xtar(d) - X(i,d);
% Compute direction
direct(i,d) = sign(gap(i,d));
end
else
for d = 1:dim
% Compute direction
direct(i,d) = sign(-1 + (1 + 1) * rand());
end
end
end
end
% Compute step sizing function (2)
if rand() <= lambda
sl = sl - exp(t / (t - 1)) * phi * sl;
else
sl = sl + exp(t / (t - 1)) * phi * sl;
end
% {3} Neighbor generation: Figure 7
for i = 1:N
for d = 1:dim
% Update (1)
X(i,d) = X(i,d) + sl * direct(i,d) * abs(X(i,d));
end
% Boundary
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X(i,:) = XB;
end
% Fitness
for i = 1:N
% Fitness
fit(i) = fun(feat,label,(X(i,:) > thres),opts);
% Best update
if fit(i) < fitG
fitG = fit(i);
Xgb = X(i,:);
end
end
curve(t) = fitG;
fprintf('\nIteration %d Best (WSA)= %f',t,curve(t))
t = t + 1;
end
% Select features
Pos = 1:dim;
Sf = Pos((Xgb > thres) == 1);
sFeat = feat(:,Sf);
% Store results
WSA.sf = Sf;
WSA.ff = sFeat;
WSA.nf = length(Sf);
WSA.c = curve;
WSA.f = feat;
WSA.l = label;
end