Index exceeds matrix dimensions.
2 views (last 30 days)
Show older comments
Dear sirs
i have this error with PSO code below any one can help me
the error is
Index exceeds matrix dimensions.
Error in ofun (line 92)
f=alph1*(of)^2+alph2*(DT-beta(2)*(DT- abs(DT)))^2; % fitness function
Error in Untitled4 (line 32)
f0{i}=ofun(x0(i,:));
the code is
%Save the following codes in MATLAB script file (*.m) and save as ofun.m.
%----------------------------------------------------------------------------------------------------------------------------------start
function f=ofun(x) % objective function (minimization)
of =2.633*x(1)+2.992*x(2)+3.134*x(3)+3.678*x(4)+3.620*x(5)+2.948*x(6)+1.607*x(7)+2.952*x(8)+3.348*x(9)+3.680*x(10)+3.774*x(11)+2.995*x(12)+3.237*x(13)+1.608*x(14);
% if there is no constraints then comments all c0 lines below
c =[];
CTI =0.3;
A(1) =2.633*x(1); B(1) =4.112*x(6); %first row
A(2) =2.992*x(2); B(2) =4.870*x(1); %second row
A(3) =2.992*x(2); B(3) =2.178*x(7); %third row
A(4) =3.134*x(3); B(4) =3.857*x(2); %fourth row
A(5) =3.678*x(4); B(5) =3.989*x(3); %fifth row
A(6) =3.620*x(5); B(6) =4.979*x(4); %sixth row
A(7) =2.948*x(6); B(7) =5.756*x(5); %seventh row
A(8) =1.607*x(7); B(8) =5.826*x(5); %eighth row
A(9) =2.948*x(6); B(9) =2.150*x(14); %nighth row
A(10)=1.607*x(7); B(10)=6.920*x(13); %tenth row
A(11)=2.952*x(8); B(11)=2.144*x(7); %eleventh row
A(12)=2.952*x(8); B(12)=5.365*x(9); %tweleventh row
A(13)=3.348*x(9); B(13)=4.863*x(10); %thirteenth row
A(14)=3.680*x(10); B(14)=5.073*x(11); %fourteenth row
A(15)=3.774*x(11); B(15)=3.774*x(12); %fiveteenth row
A(16)=2.995*x(12); B(16)=6.920*x(13); %sixteenth row
A(17)=2.995*x(12); B(17)=2.151*x(14); %seventennth row
A(18)=3.237*x(13); B(18)=4.288*x(8); %eighteenth row
A(19)=1.608*x(14); B(19)=4.870*x(1); %nighnteenth row
A(20)=1.608*x(14); B(20)=5.365*x(9); % twenty row
for I = 1:length (A)
for J = 1:length (B)
DT(I,J) = B(J)-A(I)-CTI;
if DT(I,J)>=0
D(I,J)=0;
else
D(I,J)= B(J)-A(I)+CTI;
end
end
end
c0(1)= B(1) -A(1)-CTI;
c0(2)= B(2) -A(2)-CTI;
c0(3)= B(3) -A(3)-CTI;
c0(4)= B(4) -A(4)-CTI;
c0(5)= B(5) -A(5)-CTI;
c0(6)= B(6) -A(6)-CTI;
c0(7)= B(7) -A(7)-CTI;
c0(8)= B(8) -A(8)-CTI;
c0(9)= B(9) -A(9)-CTI;
c0(10)= B(10)-A(10)-CTI;
c0(11)= B(11)-A(11)-CTI;
c0(12)= B(12)-A(12)-CTI;
c0(13)= B(13)-A(13)-CTI;
c0(14)= B(14)-A(14)-CTI;
c0(15)= B(15)-A(15)-CTI;
c0(16)= B(16)-A(16)-CTI;
c0(17)= B(17)-A(17)-CTI;
c0(18)= B(18)-A(18)-CTI;
c0(19)= B(19)-A(19)-CTI;
c0(20)= B(20)-A(20)-CTI;
for d = 1: length(c0)
if c0(d)>=0
c(d)=0;
else
c(d)=1;
end
end
alph1=1;
alph2=2;
beta = 100;
f=alph1*(of)^2+alph2*(DT-beta(2)*(DT- abs(DT)))^2; % fitness function
%---------------------------------------------------------------------------------------------end
%---------------------------------------------------------------------------------------------------------------------------------start
tic
clc
clear all
close all
rng default
LB=[0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05]; % lower bounds of variables
UB=[1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1]; % upper bounds of variables
% pso parameters values
m=14; % number of variables
n=100; % population size
wmax=0.9; % inertia weight
wmin=0.4; % inertia weight
c1=2; % acceleration factor
c2=2; % acceleration factor
% pso main program----------------------------------------------------start
maxite=500; % set maximum number of iteration
maxrun=10; % set maximum number of runs need to be
for run=1:maxrun
run
% pso initialization----------------------------------------------start
for i=1:n
for j=1:m
x0(i,j)=round(LB(j)+rand()*(UB(j)-LB(j)));
end
end
x=x0; % initial population
v=0.1*x0; % initial velocity
f0=cell(1,n); %preallocation
for i=1:n
f0{i}=ofun(x0(i,:));
end
[fmin0,index0]=min(f0{i});
pbest=x0; % initial pbest
gbest=x0(index0,:); % initial gbest
% pso initialization-----------------------------------------------end
% pso algorithm---------------------------------------------------start
ite=1;
tolerance=1;
while ite<=maxite
tolerance>1e-12
w=wmax-(wmax-wmin)*ite/maxite; % update inertial weight
% pso velocity updates
for i=1:n
for j=1:m
v(i,j)=w*v(i,j)+c1*rand()*(pbest(i,j)-x(i,j))...
+c2*rand()*(gbest(1,j)-x(i,j));
end
end
% pso position update
for i=1:n
for j=1:m
x(i,j)=x(i,j)+v(i,j);
end
end
% handling boundary violations
for i=1:n
for j=1:m
if x(i,j)<LB(j)
x(i,j)=LB(j);
elseif x(i,j)>UB(j)
x(i,j)=UB(j);
end
end
end
% evaluating fitness
f=cell(1,50); %preallocation
for i=1:n
f{i}=ofun(x(i,:));
end
% updating pbest and fitness
for i=1:n
if f{i}<f0{i}
pbest(i,:)=x(i,:);
f0{i}=f{i};
end
end
[fmin,index]=min(f0{i});
% finding out the best particle
ffmin(ite,run)=fmin(1); % storing best fitness
ffite(run)=ite; % storing iteration count
% updating gbest and best fitness
if fmin<fmin0
gbest=pbest(index,:);
fmin0=fmin;
end
% calculating tolerance
if ite>100;
tolerance=abs(ffmin(ite-100,run)-fmin0);
end
% displaying iterative results
if ite==1
fprintf('Iteration Best particle Objective fun\n');
end
fprintf('%8g %8g %8.4f\n',ite,index,fmin0);
ite=ite+1;
end
% pso algorithm---------------------------------------------------end
gbest
fvalue=2.633*x(1)+2.992*x(2)+3.134*x(3)+3.678*x(4)+3.620*x(5)+2.948*x(6)+1.607*x(7)+2.952*x(8)+3.348*x(9)+3.680*x(10)+3.774*x(11)+2.995*x(12)+3.237*x(13)+1.608*x(14);
fff(run)=fvalue
rgbest(run,:)=gbest;
fprintf('--------------------------------------\n');
end
% pso main program------------------------------------------------------end
fprintf('\n\n');
fprintf('*********************************************************\n');
fprintf('Final Results-----------------------------\n');
[bestfun,bestrun]=min(fff)
best_variables=rgbest(bestrun,:)
fprintf('*********************************************************\n');
toc
% PSO convergence characteristic
plot(ffmin(1:ffite(bestrun),bestrun),'-k');
xlabel('Iteration');
ylabel('Fitness function value');
title('PSO convergence characteristic')
% checking for coordination time interval (CTI)
CTI1 = -2.633*best_variables(1) + 4.112*best_variables(6)
CTI2 = +4.870*best_variables(1) - 2.992*best_variables(2)
CTI3 = -2.992*best_variables(2) + 2.178*best_variables(7)
CTI4 = +3.857*best_variables(2) - 3.134*best_variables(3)
CTI5 = +3.989*best_variables(3) - 3.678*best_variables(4)
CTI6 = +4.979*best_variables(4) - 3.620*best_variables(5)
CTI7 = +5.756*best_variables(5) - 2.948*best_variables(6)
CTI8 = +5.826*best_variables(5) - 1.607*best_variables(7)
CTI9 = -2.948*best_variables(6) + 2.150*best_variables(14)
CTI10= -1.607*best_variables(7) + 6.920*best_variables(13)
CTI11= +2.144*best_variables(7) - 2.952*best_variables(8)
CTI12= -2.952*best_variables(8) + 5.365*best_variables(9)
CTI13= -3.348*best_variables(9) + 4.863*best_variables(10)
CTI14= -3.680*best_variables(10)+ 5.073*best_variables(11)
CTI15= -3.774*best_variables(11)+ 3.774*best_variables(12)
CTI16= -2.995*best_variables(12)+ 6.920*best_variables(13)
CTI17= -2.995*best_variables(12)+ 2.151*best_variables(14)
CTI18= +4.288*best_variables(8) - 3.237*best_variables(13)
CTI19= +4.870*best_variables(1) - 1.608*best_variables(14)
CTI20= +5.365*best_variables(9) - 1.608*best_variables(14)
%##########################################--------------------------end
0 Comments
Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!