# Grey Wolf optimizer in matlab

10 views (last 30 days)
okoth ochola on 29 Jan 2023
Answered: Steven Lord on 29 Jan 2023
Hi, I am trying to write matlab code for grey wolf optimization. I would want to use it to obtain wbl patramaters. So I have written a trial code but if i run it (in R2016a) I keep getting error in line 95, telling me this"Error: File: Gwo1.m Line: 95 Column: 4
All functions in a script must be closed with an 'end'."When I insert the end at the speficidied location, it's telling me this "Error: File: Gwo1.m Line: 94 Column: 4
Function definitions are not permitted in this context.". How can I solve this? Below is the code that I used, Thanks in advance.
Any help will be highly appreciated. Thank you.
%X=input('enter the values of X\n')
X = rand(35,2);
out = wbl(X)
Out of memory. The likely cause is an infinite recursion within the program.

Error in solution>wbl (line 55)
fx1=fun(pos);
function out = wbl(X)
A1=X(:,1);
A2=X(:,2);
%objective function
fx=(2*sqrt(2).*A1+A2).*100;
g(:,1)=2.*(sqrt(2).*A1+A2)./(sqrt(2).*A1.^2+2.*A1.*A2)-2;
g(:,2)=2.*A2./(sqrt(2).*A1.^2+2.*A1.*A2)-2;
g(:,3)=2./(A1 + sqrt(2).*A2)-2;
%define penalty term
pp=10^9;
for i=1:size(g,1)
for j=1:size(g,2)
if g(i,j)>0
penalty(i,j)= pp.*g(i,j);
else
penalty(i,j)=0;
end
end
end
out = fx+sum(penalty,2);
%the GWO main code
format short
fun = @wbl;
N=300;
D=2;
lb=[0 0];
ub=[1 1];
itermax=100;
%generating intial popilation size
for i=1:N
for j=1:D
pos(i,j)=lb(j)+rand.*(ub(j)-lb(j));
end
end
%Evaluation of objective function
[fminvalue,ind]=min(fx);
% GWO main loop
iter=1;
while iter<=itermax
Fgbest=fminvalue;
a=2-2*iter/itermax;
for i=1:N
X=pos(i,:);
pos1=pos;
A1=2.*a.*rand(1,D)-a;
C1=2.*rand(1,D);
[alpha, alphaind]=min(fx);
alphapos=pos1(alphaind,:);
Dalpha=abs(C1.*alphapos-X);
X1=alphapos-A1.*Dalpha;
pos1(alphaind,:)=[];
fx1=fun(pos);
%funding beta position
[bet,betind]=min(fx1);
betpos=pos1(betind,:);
A2=2.*a.*rand(1,D)-a;
C2=2.*rand(1,D);
Dbet=abs(C2.*betpos-X);
X2=betpos-A2.*Dbet;
pos1(betind,:)=[];
fx1=fun(pos1);
%Delta position
[delta,deltaind]=min(fx1);
de;tapos=pos1(deltaind,:);
A3=2.*a.*rand(1,D)-a;
C3=2.*rand(1,D);
Ddelta=abs(C3.*betpos-X);
X3=deltapos-A3.*Ddelta;
Xnew=(X1+X2+X3)./3;
%check bound
Xnew=max(Xnew,lb);
Xnew=min(Xnew,ub);
fnew=fun(Xnew);
%greedy slection
if fnew<fx(i)
pos(i,:)=Xnew;
fx(i,:)=fnew;
end
end
%Update Gbest
[fmin,find]=min(fx);
if fmin<Fgbest
Fgbest = fmin;
gbest=pos(find,:);
end
%memorize
[optval,optind]=min(fx);
BestFx(iter)=optval;
BestX(iter,:)=pos(optind,:);
%show iteration infomation
plot(BestFx, 'LineWidth',2);
iter=iter+1
end
out = BestX
end
Torsten on 29 Jan 2023
As you can see, the code above runs without this problem.

Steven Lord on 29 Jan 2023
The ability to define local functions in scripts was introduced in release R2016b.

### Categories

Find more on Support Vector Machine Regression in Help Center and File Exchange

R2016a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!