Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
JingweiToo committed Dec 20, 2020
1 parent 56f81e9 commit b956ab4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 80 deletions.
63 changes: 32 additions & 31 deletions Main.m
@@ -1,44 +1,45 @@
%-------------------------------------------------------------------------%
% Binary Differential Evolution (BDE) source codes demo version %
% %
% Programmer: Jingwei Too %
% %
% E-Mail: jamesjames868@gmail.com %
%-------------------------------------------------------------------------%


%---Input------------------------------------------------------------------
% feat: feature vector (instances x features)
% label: labelling
% N: Number of vectors
% T: Maximum number of generations
% CR: Crossover rate
%---Output-----------------------------------------------------------------
% sFeat: Selected features (instances x features)
% Sf: Selected feature index
% Nf: Number of selected features
% curve: Convergence curve
%--------------------------------------------------------------------------
%-------------------------------------------------------------------%
% Binary Differential Evolution (BDE) demo version %
%-------------------------------------------------------------------%


%---Input------------------------------------------------------------
% feat : feature vector (instances x features)
% label : label vector (instances x 1)
% N : Number of solutions
% max_Iter : Maximum number of iterations
% CR : Crossover rate

%---Output-----------------------------------------------------------
% sFeat : Selected features (instances x features)
% Sf : Selected feature index
% Nf : Number of selected features
% curve : Convergence curve
%--------------------------------------------------------------------


%% Binary Differential Evolution
clc, clear, close
% Benchmark data set
load ionosphere.mat;
load ionosphere.mat;

% Set 20% data as validation set
ho=0.2;
ho = 0.2;
% Hold-out method
HO=cvpartition(label,'HoldOut',ho,'Stratify',false);
HO = cvpartition(label,'HoldOut',ho,'Stratify',false);

% Parameter setting
N=10; T=100; CR=0.9;
N = 10;
max_Iter = 100;
CR = 0.9;
% Binary Differential Evolution
[sFeat,Sf,Nf,curve]=jBDE(feat,label,N,T,CR,HO);
% Plot convergence curve
figure(); plot(1:T,curve); xlabel('Number of generations');
ylabel('Fitness Value'); title('BDE'); grid on;


[sFeat,Sf,Nf,curve] = jBDE(feat,label,N,max_Iter,CR,HO);

% Plot convergence curve
plot(1:max_Iter,curve);
xlabel('Number of generations');
ylabel('Fitness Value');
title('BDE'); grid on;



84 changes: 50 additions & 34 deletions jBDE.m
@@ -1,60 +1,76 @@
function [sFeat,Sf,Nf,curve]=jBDE(feat,label,N,T,CR,HO)
function [sFeat,Sf,Nf,curve] = jBDE(feat,label,N,max_Iter,CR,HO)

fun=@jFitnessFunction;
D=size(feat,2); X=zeros(N,D);
for i=1:N
for d=1:D
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;
X(i,d) = 1;
end
end
end
fit=zeros(1,N); fitG=inf;
for i=1:N
fit(i)=fun(feat,label,X(i,:),HO);
fit = zeros(1,N);
fitG = inf;
for i = 1:N
fit(i) = fun(feat,label,X(i,:),HO);
if fit(i) < fitG
fitG=fit(i); Xgb=X(i,:);
fitG = fit(i);
Xgb = X(i,:);
end
end
curve=inf; Xnew=zeros(N,D); t=1;
%---Iterations start-------------------------------------------------------
while t <= T
for i=1:N
R=randperm(N); R(R==i)=[];
r1=R(1); r2=R(2); r3=R(3);
jrand=randi([1,D]);
for d=1:D
if X(r1,d)==X(r2,d)
diffV=0;
MV = zeros(N,dim);
Xnew = zeros(N,dim);

curve = inf;
t = 1;
%---Iterations start--------------------------------------------------
while t <= max_Iter
for i = 1:N
R = randperm(N); R(R == i) = [];
r1 = R(1);
r2 = R(2);
r3 = R(3);
for d = 1:dim
if X(r1,d) == X(r2,d)
diffV = 0;
else
diffV=X(r1,d);
diffV = X(r1,d);
end
if diffV==1
MV=1;
if diffV == 1
MV(i,d) = 1;
else
MV=X(r3,d);
MV(i,d) = X(r3,d);
end
if d==jrand || rand() <= CR
Xnew(i,d)=MV;
end
jrand = randi([1,dim]);
for d = 1:dim
if rand() <= CR || d == jrand
Xnew(i,d) = MV(i,d);
else
Xnew(i,d)=X(i,d);
Xnew(i,d) = X(i,d);
end
end
end
for i=1:N
Fnew=fun(feat,label,Xnew(i,:),HO);
for i = 1:N
Fnew = fun(feat,label,Xnew(i,:),HO);
if Fnew <= fit(i)
X(i,:)=Xnew(i,:); fit(i)=Fnew;
X(i,:) = Xnew(i,:);
fit(i) = Fnew;
end
if fit(i) < fitG
fitG=fit(i); Xgb=X(i,:);
fitG = fit(i);
Xgb = X(i,:);
end
end
curve(t)=fitG;
curve(t) = fitG;
fprintf('\nIteration %d Best (BDE)= %f',t,curve(t))
t=t+1;
t = t + 1;
end
Pos=1:D; Sf=Pos(Xgb==1); Nf=length(Sf); sFeat=feat(:,Sf);
Pos = 1:dim;
Sf = Pos(Xgb == 1);
Nf = length(Sf);
sFeat = feat(:,Sf);
end


36 changes: 21 additions & 15 deletions jFitnessFunction.m
@@ -1,27 +1,33 @@
% Notation: This fitness function is for demonstration

function fitness=jFitnessFunction(feat,label,X,HO)
if sum(X==1)==0
fitness=1;
function cost = jFitnessFunction(feat,label,X,HO)
if sum(X == 1) == 0
cost = 1;
else
fitness=jwrapperKNN(feat(:,X==1),label,HO);
cost = jwrapperKNN(feat(:, X == 1),label,HO);
end
end


function ER=jwrapperKNN(sFeat,label,HO)
function error = jwrapperKNN(sFeat,label,HO)
%---// Parameter setting for k-value of KNN //
k=5;
xtrain=sFeat(HO.training==1,:); ytrain=label(HO.training==1);
xvalid=sFeat(HO.test==1,:); yvalid=label(HO.test==1);
Model=fitcknn(xtrain,ytrain,'NumNeighbors',k);
pred=predict(Model,xvalid);
N=length(yvalid); correct=0;
for i=1:N
k = 5;

xtrain = sFeat(HO.training == 1,:);
ytrain = label(HO.training == 1);
xvalid = sFeat(HO.test == 1,:);
yvalid = label(HO.test == 1);

Model = fitcknn(xtrain,ytrain,'NumNeighbors',k);
pred = predict(Model,xvalid);
num_valid = length(yvalid);
correct = 0;
for i = 1:num_valid
if isequal(yvalid(i),pred(i))
correct=correct+1;
correct = correct + 1;
end
end
Acc=correct/N;
ER=1-Acc;
Acc = correct / num_valid;
error = 1 - Acc;
end

0 comments on commit b956ab4

Please sign in to comment.