From 17f068a70039a2bfa1cf69b9db63664af246be51 Mon Sep 17 00:00:00 2001 From: Jingwei Too <30698779+JingweiToo@users.noreply.github.com> Date: Sat, 19 Dec 2020 22:27:39 +0800 Subject: [PATCH] Add files via upload --- Main.m | 51 ++++++------ jBHHO.m | 192 +++++++++++++++++++++++++-------------------- jFitnessFunction.m | 35 +++++---- 3 files changed, 155 insertions(+), 123 deletions(-) diff --git a/Main.m b/Main.m index ab18575..9fc83fa 100644 --- a/Main.m +++ b/Main.m @@ -1,42 +1,43 @@ -%-------------------------------------------------------------------------% -% Binary Harris Hawk Optimization (BHHO) source codes demo version % -% % -% Programmer: Jingwei Too % -% % -% E-Mail: jamesjames868@gmail.com % -%-------------------------------------------------------------------------% +%-------------------------------------------------------------------% +% Binary Harris Hawk Optimization (BHHO) demo version % +%-------------------------------------------------------------------% -%---Inputs----------------------------------------------------------------- -% feat: features -% label: labelling -% N: Number of hawks -% T: Maximum number of iterations -%---Outputs---------------------------------------------------------------- -% sFeat: Selected features -% Sf: Selected feature index -% Nf: Number of selected features -% curve: Convergence curve -%-------------------------------------------------------------------------- +%---Inputs----------------------------------------------------------- +% feat : feature vector (instances x features) +% label : label vector (instance x 1) +% N : Number of hawks +% max_Iter : Maximum number of iterations +%---Outputs---------------------------------------------------------- +% sFeat : Selected features +% Sf : Selected feature index +% Nf : Number of selected features +% curve : Convergence curve +%-------------------------------------------------------------------- %% Binary Harris Hawk Optimization clc, clear, close; % Benchmark data set 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; +N = 10; +max_Iter = 100; % Binary Harris Hawk Optimization -[sFeat,Sf,Nf,curve]=jBHHO(feat,label,N,T,HO); -% Plot convergence curve -figure(); plot(1:T,curve); xlabel('Number of iterations'); -ylabel('Fitness Value'); title('BHHO'); grid on; +[sFeat,Sf,Nf,curve] = jBHHO(feat,label,N,max_Iter,HO); +% Plot convergence curve +plot(1:max_Iter,curve); +xlabel('Number of iterations'); +ylabel('Fitness Value'); +title('BHHO'); grid on; diff --git a/jBHHO.m b/jBHHO.m index 09cbf11..916f714 100644 --- a/jBHHO.m +++ b/jBHHO.m @@ -1,144 +1,170 @@ -function [sFeat,Sf,Nf,curve]=jBHHO(feat,label,N,T,HO) +function [sFeat,Sf,Nf,curve] = jBHHO(feat,label,N,max_Iter,HO) -fun=@jFitnessFunction; -D=size(feat,2); X=zeros(N,D); -for i=1:N - for d=1:D +beta = 1.5; +ub = 1; +lb = 0; + +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 -fitR=inf; fit=zeros(1,N); Y=zeros(1,D); Z=zeros(1,D); -beta=1.5; ub=1; lb=0; t=1; curve=inf; -%---Iteration start------------------------------------------------------- -while t <= T - for i=1:N - fit(i)=fun(feat,label,X(i,:),HO); +fitR = inf; +fit = zeros(1,N); +Y = zeros(1,dim); +Z = zeros(1,dim); + +curve = inf; +t = 1; +%---Iteration start------------------------------------------------- +while t <= max_Iter + for i = 1:N + fit(i) = fun(feat,label,X(i,:),HO); if fit(i) < fitR - fitR=fit(i); Xrb=X(i,:); + fitR = fit(i); + Xrb = X(i,:); end end - Xm=mean(X,1); - for i=1:N - E0=-1+2*rand(); - E=2*E0*(1-(t/T)); + X_mu = mean(X,1); + for i = 1:N + E0 = -1 + 2 * rand(); + E = 2 * E0 * (1 - (t / max_Iter)); if abs(E) >= 1 - q=rand(); + q = rand(); if q >= 0.5 - k=randi([1,N]); r1=rand(); r2=rand(); - for d=1:D - Xn=X(k,d)-r1*abs(X(k,d)-2*r2*X(i,d)); - S=1/(1+exp(-Xn)); + k = randi([1,N]); + r1 = rand(); + r2 = rand(); + for d = 1:dim + Xn = X(k,d) - r1 * abs(X(k,d) - 2 * r2 * X(i,d)); + S = 1 / (1 + exp(-Xn)); if rand() < S - X(i,d)=1; - else - X(i,d)=0; + X(i,d)= 1; + else + X(i,d) = 0; end end elseif q < 0.5 - r3=rand(); r4=rand(); - for d=1:D - Xn=(Xrb(d)-Xm(d))-r3*(lb+r4*(ub-lb)); - S=1/(1+exp(-Xn)); + r3 = rand(); + r4 = rand(); + for d = 1:dim + Xn = (Xrb(d) - X_mu(d)) - r3 * (lb + r4 * (ub - lb)); + S = 1 / (1 + exp(-Xn)); if rand() < S - X(i,d)=1; + X(i,d) = 1; else - X(i,d)=0; + X(i,d) = 0; end end end elseif abs(E) < 1 - J=2*(1-rand()); r=rand(); - if r >= 0.5 && abs(E) >= 0.5 - for d=1:D - DX=Xrb(d)-X(i,d); - Xn=DX-E*abs(J*Xrb(d)-X(i,d)); - S=1/(1+exp(-Xn)); + J = 2 * (1 - rand()); + r = rand(); + if r >= 0.5 && abs(E) >= 0.5 + for d = 1:dim + DX = Xrb(d) - X(i,d); + Xn = DX - E * abs(J * Xrb(d) - X(i,d)); + S = 1 / (1 + exp(-Xn)); if rand() < S - X(i,d)=1; + X(i,d) = 1; else - X(i,d)=0; + X(i,d) = 0; end end - elseif r >= 0.5 && abs(E) < 0.5 - for d=1:D - DX=Xrb(d)-X(i,d); - Xn=Xrb(d)-E*abs(DX); - S=1/(1+exp(-Xn)); + elseif r >= 0.5 && abs(E) < 0.5 + for d = 1:dim + DX = Xrb(d) - X(i,d); + Xn = Xrb(d) - E * abs(DX); + S = 1 / (1 + exp(-Xn)); if rand() < S - X(i,d)=1; + X(i,d) = 1; else - X(i,d)=0; + X(i,d) = 0; end end - elseif r < 0.5 && abs(E) >= 0.5 - LF=jLevyDistribution(beta,D); - for d=1:D - Yn=Xrb(d)-E*abs(J*Xrb(d)-X(i,d)); - S=1/(1+exp(-Yn)); + elseif r < 0.5 && abs(E) >= 0.5 + LF = jLevyDistribution(beta,dim); + for d = 1:dim + Yn = Xrb(d) - E * abs(J * Xrb(d) - X(i,d)); + S = 1 / (1 + exp(-Yn)); if rand() < S - Y(d)=1; + Y(d) = 1; else - Y(d)=0; + Y(d) = 0; end - Zn=Y(d)+rand()*LF(d); - S=1/(1+exp(-Zn)); + Zn = Y(d) + rand() * LF(d); + S = 1 / (1 + exp(-Zn)); if rand() < S - Z(d)=1; + Z(d) = 1; else - Z(d)=0; + Z(d) = 0; end end - fitY=fun(feat,label,Y,HO); fitZ=fun(feat,label,Z,HO); + fitY = fun(feat,label,Y,HO); + fitZ = fun(feat,label,Z,HO); if fitY <= fit(i) - fit(i)=fitY; X(i,:)=Y; + fit(i) = fitY; + X(i,:) = Y; end if fitZ <= fit(i) - fit(i)=fitZ; X(i,:)=Z; + fit(i) = fitZ; + X(i,:) = Z; end - elseif r < 0.5 && abs(E) < 0.5 - LF=jLevyDistribution(beta,D); - for d=1:D - Yn=Xrb(d)-E*abs(J*Xrb(d)-Xm(d)); - S=1/(1+exp(-Yn)); + elseif r < 0.5 && abs(E) < 0.5 + LF = jLevyDistribution(beta,dim); + for d = 1:dim + Yn = Xrb(d) - E * abs(J * Xrb(d) - X_mu(d)); + S = 1 / (1 + exp(-Yn)); if rand() < S - Y(d)=1; + Y(d) = 1; else - Y(d)=0; + Y(d) = 0; end - Zn=Y(d)+rand()*LF(d); - S=1/(1+exp(-Zn)); + Zn = Y(d) + rand() * LF(d); + S = 1 / (1 + exp(-Zn)); if rand() < S - Z(d)=1; + Z(d) = 1; else - Z(d)=0; + Z(d) = 0; end end - fitY=fun(feat,label,Y,HO); fitZ=fun(feat,label,Z,HO); + fitY = fun(feat,label,Y,HO); + fitZ = fun(feat,label,Z,HO); if fitY <= fit(i) - fit(i)=fitY; X(i,:)=Y; + fit(i) = fitY; + X(i,:) = Y; end if fitZ <= fit(i) - fit(i)=fitZ; X(i,:)=Z; + fit(i) = fitZ; + X(i,:) = Z; end end end end - curve(t)=fitR; + curve(t) = fitR; fprintf('\nIteration %d Best (BHHO)= %f',t,curve(t)) - t=t+1; + t = t + 1; end -Pos=1:D; Sf=Pos(Xrb==1); Nf=length(Sf); sFeat=feat(:,Sf); +Pos = 1:dim; +Sf = Pos(Xrb == 1); +Nf = length(Sf); +sFeat = feat(:,Sf); end -function LF=jLevyDistribution(beta,D) -nume=gamma(1+beta)*sin(pi*beta/2); -deno=gamma((1+beta)/2)*beta*2^((beta-1)/2); -sigma=(nume/deno)^(1/beta); -u=randn(1,D)*sigma; v=randn(1,D); -step=u./abs(v).^(1/beta); LF=0.01*step; +function LF = jLevyDistribution(beta,dim) +nume = gamma(1 + beta) * sin(pi * beta / 2); +deno = gamma((1 + beta) / 2) * beta * 2 ^ ((beta - 1) / 2); +sigma = (nume / deno) ^ (1 / beta); +u = randn(1,dim) * sigma; +v = randn(1,dim); +step = u ./ abs(v) .^ (1 / beta); +LF = 0.01 * step; end diff --git a/jFitnessFunction.m b/jFitnessFunction.m index 0514503..025bf3f 100644 --- a/jFitnessFunction.m +++ b/jFitnessFunction.m @@ -1,28 +1,33 @@ % Notation: This fitness function is for demonstration -function fitness=jFitnessFunction(feat,label,X,HO) -if sum(X==1)==0 - fitness=inf; +function cost = jFitnessFunction(feat,label,X,HO) +if sum(X == 1) == 0 + cost = inf; 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