-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathjMonarchButterflyOptimization.m
157 lines (147 loc) · 3.8 KB
/
jMonarchButterflyOptimization.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
%[2015]-"Monarch butterfly optimization"
% (9/12/2020)
function MBO = jMonarchButterflyOptimization(feat,label,opts)
% Parameters
lb = 0;
ub = 1;
thres = 0.5;
peri = 1.2; % migration period
p = 5/12; % ratio
Smax = 1; % maximum step
BAR = 5/12; % butterfly adjusting rate
num_land1 = 4; % number of butterflies in land 1
beta = 1.5; % levy component
if isfield(opts,'T'), max_Iter = opts.T; end
if isfield(opts,'N'), N = opts.N; end
if isfield(opts,'peri'), peri = opts.peri; end
if isfield(opts,'p'), p = opts.p; end
if isfield(opts,'Smax'), Smax = opts.Smax; end
if isfield(opts,'BAR'), BAR = opts.BAR; end
if isfield(opts,'beta'), beta = opts.beta; end
if isfield(opts,'N1'), num_land1 = opts.N1; 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);
% Global best update
if fit(i) < fitG
fitG = fit(i);
Xgb = X(i,:);
end
end
% Pre
Xnew = zeros(N,dim);
Fnew = zeros(1,N);
curve = zeros(1,max_Iter);
curve(1) = fitG;
t = 2;
% Iterations
while t <= max_Iter
% Sort butterfly
[fit, idx] = sort(fit,'ascend');
X = X(idx,:);
% Weight factor (8)
alpha = Smax / (t ^ 2);
% {1} First land: Migration operation
for i = 1:num_land1
for d = 1:dim
% Random number (2)
r = rand() * peri;
if r <= p
% Random select a butterfly in land 1
r1 = randi([1,num_land1]);
% Update position (1)
Xnew(i,d) = X(r1,d);
else
% Random select a butterfly in land 2
r2 = randi([num_land1 + 1, N]);
% Update position (3)
Xnew(i,d) = X(r2,d);
end
end
% Boundary
XB = Xnew(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
Xnew(i,:) = XB;
end
% {2} Second land: Butterly adjusting operation
for i = num_land1 + 1 : N
% Levy distribution (7)
dx = jLevyDistribution(beta,dim);
for d = 1:dim
if rand() <= p
% Position update (4)
Xnew(i,d) = Xgb(d);
else
% Random select a butterfly in land 2
r3 = randi([num_land1 + 1, N]);
% Update position (5)
Xnew(i,d) = X(r3,d);
% Butterfly adjusting (6)
if rand () > BAR
Xnew(i,d) = Xnew(i,d) + alpha * (dx(d) - 0.5);
end
end
end
% Boundary
XB = Xnew(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
Xnew(i,:) = XB;
end
% {3} Combine population
for i = 1:N
% Fitness
Fnew(i) = fun(feat,label,(Xnew(i,:) > thres),opts);
% Global best update
if Fnew(i) < fitG
fitG = Fnew(i);
Xgb = Xnew(i,:);
end
end
% Merge & Select best N solutions
XX = [X; Xnew];
FF = [fit, Fnew];
[FF, idx] = sort(FF,'ascend');
X = XX(idx(1:N),:);
fit = FF(1:N);
% Save
curve(t) = fitG;
fprintf('\nIteration %d Best (MBO)= %f',t,curve(t))
t = t + 1;
end
% Select features
Pos = 1:dim;
Sf = Pos((Xgb > thres) == 1);
sFeat = feat(:,Sf);
% Store results
MBO.sf = Sf;
MBO.ff = sFeat;
MBO.nf = length(Sf);
MBO.c = curve;
MBO.f = feat;
MBO.l = label;
end
%// Levy Flight //
function LF = jLevyDistribution(beta,dim)
% Sigma
nume = gamma(1 + beta) * sin(pi * beta / 2);
deno = gamma((1 + beta) / 2) * beta * 2 ^ ((beta - 1) / 2);
sigma = (nume / deno) ^ (1 / beta);
% Parameter u & v
u = randn(1,dim) * sigma;
v = randn(1,dim);
% Step
step = u ./ abs(v) .^ (1 / beta);
LF = step;
end