-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathjMantaRayForagingOptimization.m
165 lines (157 loc) · 4.12 KB
/
jMantaRayForagingOptimization.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%[2020]-"Manta ray foraging optimization: An effective bio-inspired
%optimizer for engineering applications"
% (8/12/2020)
function MRFO = jMantaRayForagingOptimization(feat,label,opts)
% Parameters
lb = 0;
ub = 1;
thres = 0.5;
S = 2; % somersault factor
if isfield(opts,'N'), N = opts.N; end
if isfield(opts,'T'), max_Iter = opts.T; end
if isfield(opts,'S'), S = opts.S; 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 solution
if fit(i) < fitG
fitG = fit(i);
Xbest = X(i,:);
end
end
% Pre
Xnew = zeros(N,dim);
curve = zeros(1,max_Iter);
curve(1) = fitG;
t = 2;
% Iteration
while t <= max_Iter
for i = 1:N
% [Cyclone foraging]
if rand() < 0.5
if t / max_Iter < rand()
% Compute beta (5)
r1 = rand();
beta = 2 * exp(r1 * ((max_Iter - t + 1) / max_Iter)) * ...
(sin(2 * pi * r1));
for d = 1:dim
% Create random solution (6)
Xrand = lb + rand() * (ub - lb);
% First manta ray follow best food (7)
if i == 1
Xnew(i,d) = Xrand + rand() * (Xrand - X(i,d)) + ...
beta * (Xrand - X(i,d));
% Followers follew the front manta ray (7)
else
Xnew(i,d) = Xrand + rand() * (X(i-1,d) - X(i,d)) + ...
beta * (Xrand - X(i,d));
end
end
else
% Compute beta (5)
r1 = rand();
beta = 2 * exp(r1 * ((max_Iter - t + 1) / max_Iter)) * ...
(sin(2 * pi * r1));
for d = 1:dim
% First manta ray follow best food (4)
if i == 1
Xnew(i,d) = Xbest(d) + rand() * (Xbest(d) - X(i,d)) + ...
beta * (Xbest(d) - X(i,d));
% Followers follow the front manta ray (4)
else
Xnew(i,d) = Xbest(d) + rand() * (X(i-1,d) - X(i,d)) + ...
beta * (Xbest(d) - X(i,d));
end
end
end
% [Chain foraging]
else
for d = 1:dim
% Compute alpha (2)
r = rand();
alpha = 2 * r * sqrt(abs(log(r)));
% First manta ray follow best food (1)
if i == 1
Xnew(i,d) = X(i,d) + rand() * (Xbest(d) - X(i,d)) + ...
alpha * (Xbest(d) - X(i,d));
% Followers follew the front manta ray (1)
else
Xnew(i,d) = X(i,d) + rand() * (X(i-1,d) - X(i,d)) + ...
alpha * (Xbest(d) - X(i,d));
end
end
end
% Boundary
XB = Xnew(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
Xnew(i,:) = XB;
end
% Fitness
for i = 1:N
Fnew = fun(feat,label,(Xnew(i,:) > thres),opts);
% Greedy selection
if Fnew < fit(i)
fit(i) = Fnew;
X(i,:) = Xnew(i,:);
end
% Update best
if fit(i) < fitG
fitG = fit(i);
Xbest = X(i,:);
end
end
% [Somersault foraging]
for i = 1:N
% Manta ray update (8)
r2 = rand();
r3 = rand();
for d = 1:dim
Xnew(i,d) = X(i,d) + S * (r2 * Xbest(d) - r3 * X(i,d));
end
% Boundary
XB = Xnew(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
Xnew(i,:) = XB;
end
% Fitness
for i = 1:N
Fnew = fun(feat,label,(Xnew(i,:) > thres),opts);
% Greedy selection
if Fnew < fit(i)
fit(i) = Fnew;
X(i,:) = Xnew(i,:);
end
% Update best
if fit(i) < fitG
fitG = fit(i);
Xbest = X(i,:);
end
end
curve(t) = fitG;
fprintf('\nIteration %d Best (MRFO)= %f',t,curve(t))
t = t + 1;
end
% Select features based on selected index
Pos = 1:dim;
Sf = Pos((Xbest > thres) == 1);
sFeat = feat(:,Sf);
% Store results
MRFO.sf = Sf;
MRFO.ff = sFeat;
MRFO.nf = length(Sf);
MRFO.c = curve;
MRFO.f = feat;
MRFO.l = label;
end