Skip to content

Commit b7062c0

Browse files
authored
Add files via upload
1 parent e40ecef commit b7062c0

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

Main.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
%---------------------------------------------------------------------%
2+
% Whale Optimization Algorithm (WOA) source codes demo version %
3+
%---------------------------------------------------------------------%
4+
5+
6+
%---Inputs-----------------------------------------------------------
7+
% feat : feature vector ( Instances x Features )
8+
% label : label vector ( Instances x 1 )
9+
% N : Number of whales
10+
% max_Iter : Maximum number of iterations
11+
% b : Constant
12+
13+
%---Output-----------------------------------------------------------
14+
% sFeat : Selected features (instances x features)
15+
% Sf : Selected feature index
16+
% Nf : Number of selected features
17+
% curve : Convergence curve
18+
%--------------------------------------------------------------------
19+
20+
21+
%% Whale Optimization Algorithm
22+
clc, clear, close;
23+
% Benchmark data set
24+
load ionosphere.mat;
25+
26+
% Set 20% data as validation set
27+
ho=0.2;
28+
% Hold-out method
29+
HO=cvpartition(label,'HoldOut',ho);
30+
31+
% Parameter setting
32+
N = 10;
33+
max_Iter = 100;
34+
35+
% Whale Optimization Algorithm
36+
[sFeat,Sf,Nf,curve] = jWOA(feat,label,N,max_Iter,HO);
37+
38+
% Accuracy
39+
Acc = jKNN(sFeat,label,HO);
40+
fprintf('\n Accuracy: %g %%',Acc); fprintf('\n');
41+
42+
% Plot convergence curve
43+
plot(1:max_Iter,curve);
44+
xlabel('Number of Iterations');
45+
ylabel('Fitness Value');
46+
title('WOA'); grid on;
47+
48+
49+
50+

ionosphere.mat

57.1 KB
Binary file not shown.

jFitnessFunction.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
% Notation: This fitness function is for demonstration
2+
3+
function cost = jFitnessFunction(feat,label,X,HO)
4+
if sum(X == 1) == 0
5+
cost = 1;
6+
else
7+
cost = jwrapperKNN(feat(:, X == 1),label,HO);
8+
end
9+
end
10+
11+
12+
function error = jwrapperKNN(sFeat,label,HO)
13+
%---// Parameter setting for k-value of KNN //
14+
k = 5;
15+
xtrain = sFeat(HO.training == 1,:);
16+
ytrain = label(HO.training == 1);
17+
xvalid = sFeat(HO.test == 1,:);
18+
yvalid = label(HO.test == 1);
19+
Model = fitcknn(xtrain,ytrain,'NumNeighbors',k);
20+
pred = predict(Model,xvalid);
21+
num_valid = length(yvalid);
22+
correct = 0;
23+
for i = 1:num_valid
24+
if isequal(yvalid(i),pred(i))
25+
correct = correct + 1;
26+
end
27+
end
28+
Acc = correct / num_valid;
29+
error = 1 - Acc;
30+
end
31+

jKNN.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
function Acc = jKNN(feat,label,HO)
3+
%---// Parameter setting for k-value of KNN //
4+
k=5;
5+
6+
xtrain = feat(HO.training==1,:); ytrain = label(HO.training==1);
7+
xvalid = feat(HO.test==1,:); yvalid = label(HO.test==1);
8+
Model = fitcknn(xtrain,ytrain,'NumNeighbors',k);
9+
ypred = predict(Model,xvalid);
10+
11+
num_valid = length(yvalid);
12+
correct = 0;
13+
for i = 1:num_valid
14+
if isequal(yvalid(i),ypred(i))
15+
correct = correct + 1;
16+
end
17+
end
18+
Acc = 100 * (correct / num_valid);
19+
end
20+

jWOA.m

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
function [sFeat,Sf,Nf,curve] = jWOA(feat,label,N,max_Iter,HO)
3+
% Parameters
4+
lb = 0;
5+
ub = 1;
6+
b = 1;
7+
8+
fun = @jFitnessFunction;
9+
dim = size(feat,2);
10+
X = zeros(N,dim);
11+
for i = 1:N
12+
for d = 1:dim
13+
X(i,d) = lb + (ub - lb) * rand();
14+
end
15+
end
16+
fit = zeros(1,N);
17+
fitG = inf;
18+
for i = 1:N
19+
fit(i) = fun(feat,label,(X(i,:) > 0.5),HO);
20+
if fit(i) < fitG
21+
fitG = fit(i);
22+
Xgb = X(i,:);
23+
end
24+
end
25+
26+
curve = inf;
27+
t = 1;
28+
%---Iteration start---------------------------------------------------
29+
while t <= max_Iter
30+
a = 2 - t * (2 / max_Iter);
31+
for i = 1:N
32+
A = 2 * a * rand() - a;
33+
C = 2 * rand();
34+
p = rand();
35+
l = -1 + 2 * rand();
36+
if p < 0.5
37+
if abs(A) < 1
38+
for d = 1:dim
39+
Dx = abs(C * Xgb(d) - X(i,d));
40+
X(i,d) = Xgb(d) - A * Dx;
41+
end
42+
elseif abs(A) >= 1
43+
for d = 1:dim
44+
k = randi([1,N]);
45+
Dx = abs(C * X(k,d) - X(i,d));
46+
X(i,d) = X(k,d) - A * Dx;
47+
end
48+
end
49+
elseif p >= 0.5
50+
for d = 1:dim
51+
dist = abs(Xgb(d) - X(i,d));
52+
X(i,d) = dist * exp(b * l) * cos(2 * pi * l) + Xgb(d);
53+
end
54+
end
55+
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
56+
X(i,:) = XB;
57+
end
58+
for i = 1:N
59+
fit(i) = fun(feat,label,(X(i,:) > 0.5),HO);
60+
if fit(i) < fitG
61+
fitG = fit(i);
62+
Xgb = X(i,:);
63+
end
64+
end
65+
curve(t) = fitG;
66+
fprintf('\nIteration %d Best (WOA)= %f',t,curve(t))
67+
t = t + 1;
68+
end
69+
Pos = 1:dim;
70+
Sf = Pos((Xgb > 0.5) == 1);
71+
Nf = length(Sf);
72+
sFeat = feat(:,Sf);
73+
end
74+
75+
76+
77+

0 commit comments

Comments
 (0)