# Index in position 2 exceeds array bounds (must not exceed 2)

32 views (last 30 days)
ShravanKumar Shivappa Masalvad on 20 Sep 2021 at 4:49
Commented: Mathieu NOE on 20 Sep 2021 at 11:27
format short
clear all
clc
%% INITIALIZING THE PARAMETERS
fun=@fun3bar;
N=300;
D=2;
lb=[0 0];
ub=[1 1];
itermax=100;
%%GENERATING THE INITIAL POPULATION
for i=1:N
for j=1:D
pos(i,j)=lb(j)+rand*(ub(j)-lb(j));
end
end
%%EVALUATING OBJECTIVE FUNCTION
fx=fun(pos);
%%INITIALIZE gBEST
[fminvalue,ind]=min(fx);
gbest=pos(ind,:);
gbest
%%GWO MAIN LOOP WILL START NOW
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);
%% Now let's calculate ALPHA Value i.e., FIRST BEST
[alpha,alphaind]=min(fx);
alphapos=pos1(alphaind,:);
Dalpha=abs(C1.*alphapos-x);
x1=alphapos-A1.*Dalpha;
pos1(alphaind,:)=[];
fx1=fun(pos1);
%% Now let's calculate BETA Value i.e., SECOND BEST
[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;
%%FINDING DELTA POSITION i.e., THIRD BEST
[delta,deltaind]=min(fx1);
deltapos=pos1(deltaind,:);
A3=2.*a.*rand(1,D)-a;
C3=2.*rand(1,D);
Ddelta=abs(C3.*deltapos-x);
x3=deltapos-A3.*Ddelta;
xnew=(x1+x2+x3)./3
%%%CHECK THE BOUNDARIES
xnew=max(xnew,lb);
xnew=min(xnew,ub);
fnew=fun(xnew);
%%%GREEDY SELECTION
if fnew<fx(i)
pos(i,:)=xnew;
fx(i,:)=fnew;
end
end
%%UPDATE GBEST (DESTINATION)
[fmin,find]=min(fx);
if fmin<Fgbest
Fgbest=fmin;
gbest=pos(find,:);
end
%%%%MEMORIZE THE BEST SOLUTION
[optval,optind]=min(fx);
BestFx(iter)=optval;
BestX(iter,:)=pos(optind,:);
%%%SHOW ITERATION INFORMATION
disp(['Iteration' num2str(iter):'Best Cost=' (num2str(BestFx(iter)))]); %#ok<BDSCA>
%%%PLOTTING THE RESULT
plot(BestFx,'Line Width',2);
xlabel('Iteration Number');
ylabel('Fitness Value')
title('Convergence Vs Iteration')
grid on
iter=iter+1
end
##### 2 CommentsShowHide 1 older comment
ShravanKumar Shivappa Masalvad on 20 Sep 2021 at 6:12
function out= fun3bar(x)
A1=x(:,1)
A2=x(:,2)
%%Let's write the objective fucntion
fx=(2.*sqrt(2).*A1+A2)*100;
%%% Now writing all inequality constraints here
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
%%% COMPUTE OBJECTIVE FUNCTION
out=fx+sum(penalty,2);

Mathieu NOE on 20 Sep 2021 at 8:42
hello
just fixed minor issues on following two lines
%%%SHOW ITERATION INFORMATION
disp(['Iteration : ' num2str(iter) ' ; Best Cost=' (num2str(BestFx(iter)))]);
%%%PLOTTING THE RESULT
plot(BestFx,'Linewidth',2);
and the code seems to work fine - no error throw at the command window so far
also terminated some lines where the ; was missing (speed up the simulation and do not fill the command window with intermediate results).
beside that , the fitness value does not seem to decrease a lot , but that's another topic... full code :
format short
clear all
clc
%% INITIALIZING THE PARAMETERS
fun=@fun3bar;
N=300;
D=2;
lb=[0 0];
ub=[1 1];
itermax=100;
%%GENERATING THE INITIAL POPULATION
for i=1:N
for j=1:D
pos(i,j)=lb(j)+rand*(ub(j)-lb(j));
end
end
%%EVALUATING OBJECTIVE FUNCTION
fx=fun(pos);
%%INITIALIZE gBEST
[fminvalue,ind]=min(fx);
gbest=pos(ind,:);
% gbest
%%GWO MAIN LOOP WILL START NOW
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);
%% Now let's calculate ALPHA Value i.e., FIRST BEST
[alpha,alphaind]=min(fx);
alphapos=pos1(alphaind,:);
Dalpha=abs(C1.*alphapos-x);
x1=alphapos-A1.*Dalpha;
pos1(alphaind,:)=[];
fx1=fun(pos1);
%% Now let's calculate BETA Value i.e., SECOND BEST
[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;
%%FINDING DELTA POSITION i.e., THIRD BEST
[delta,deltaind]=min(fx1);
deltapos=pos1(deltaind,:);
A3=2.*a.*rand(1,D)-a;
C3=2.*rand(1,D);
Ddelta=abs(C3.*deltapos-x);
x3=deltapos-A3.*Ddelta;
xnew=(x1+x2+x3)./3;
%%%CHECK THE BOUNDARIES
xnew=max(xnew,lb);
xnew=min(xnew,ub);
fnew=fun(xnew);
%%%GREEDY SELECTION
if fnew<fx(i)
pos(i,:)=xnew;
fx(i,:)=fnew;
end
end
%%UPDATE GBEST (DESTINATION)
[fmin,find]=min(fx);
if fmin<Fgbest
Fgbest=fmin;
gbest=pos(find,:);
end
%%%%MEMORIZE THE BEST SOLUTION
[optval,optind]=min(fx);
BestFx(iter)=optval;
BestX(iter,:)=pos(optind,:);
%%%SHOW ITERATION INFORMATION
disp(['Iteration : ' num2str(iter) ' ; Best Cost=' (num2str(BestFx(iter)))]);
%%%PLOTTING THE RESULT
plot(BestFx,'Linewidth',2);
xlabel('Iteration Number');
ylabel('Fitness Value')
title('Convergence Vs Iteration')
grid on
iter=iter+1;
end
function out= fun3bar(x)
A1=x(:,1);
A2=x(:,2);
%%Let's write the objective fucntion
fx=(2.*sqrt(2).*A1+A2)*100;
%%% Now writing all inequality constraints here
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
%%% COMPUTE OBJECTIVE FUNCTION
out=fx+sum(penalty,2);
end
##### 2 CommentsShowHide 1 older comment
Mathieu NOE on 20 Sep 2021 at 11:27
my pleasure !

R2018b

### Community Treasure Hunt

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

Start Hunting!