How to get around the " Attempted to access para(2); index out of bounds because numel(para)=1." error that appears in the following code?
9 views (last 30 days)
Show older comments
I have a code which has three parts in it viz. the main PSO code, the function and the Simulink block. On running the main PSO code, I am getting the following error
Attempted to access para(2); index out of bounds because numel(para)=1.
Error in sofunc2 (line 6)
r2 = para(2)
Error in sosmc_sch1_c2 (line 55)
obj_value(i)=sofunc2(current_position{:,i});
The main PSO code is as below
tic
clear all
close all
clc
% Initializing variables
S=10; % no of swarm particles
d=3; % dimension of swarm particles
c1=2; % self confidence parameter
c2=1; % swarm confidence parameter
C=1; % constriction factor
LB=[0 0 0]; %lower bounds of variables
UB=[1 1 1]; %upper bounds of variables
wmax=0.9; % maximum inertia weight
wmin=0.4; % minimum inertia weight
Xmax=1; % maximum position
iter=1; % initial iteration
R1=rand();
R2=rand();
tolerance=1;
Maxiter=40; % maximum number of iteration
maxrun=5;
dt=1;
particle_best=cell(1,S);
particle_best_value=ones(1,S)*1E5;
global_best_value=1E5;
global_best=zeros(1,d);
obj_value=zeros(1,S); % Objective function value of particles
%%%%%%%%
current_position=cell(1,S); % particle position
velocity=cell(1,S); % particle velocity
dummy=zeros(1,length(d)); % dummy list
% Initialize particle position and velocity
for run=1:maxrun
run
for i=1:S
for j=1:d
dummy(j)=round(LB(j)+rand()*(UB(j)-LB(j)));
end
current_position{i}=dummy;
velocity{i}=current_position{i}/dt;
end
while iter <= Maxiter && tolerance > 0.01
for i=1:S
for j=1:d
if current_position{i}<LB(j) %%handling boundary conditions
current_position{i}=LB(j);
elseif current_position{i}>UB(j)
current_position{i}=UB(j);
end
end
end
for i=1:S
obj_value(i)=sofunc2(current_position{:,i});
end
for i=1:S
if obj_value(i) < particle_best_value(i) % finding best local
particle_best{i}= current_position{i};
particle_best_value(i)=obj_value(i);
end
end
[fmin,index]=min(obj_value); % finding out the best particle
ffmin(iter,run)=fmin; % storing best fitness
ffite(run)=iter; % storing iteration count
if min(obj_value)< global_best_value % finding best global
global_best=current_position{obj_value==min(obj_value)}; % updating gbest and best fitness
global_best_value=min(obj_value);
fmin0=global_best_value;
end
for i=1:S
w=wmax-((wmax-wmin)/Maxiter)*iter;
velocity{i}=C*(w*velocity{i}+c1*R1*(particle_best{i}-current_position{i})+c2*R2*(global_best-current_position{i}));
current_position{i}=current_position{i}+velocity{i};
end
% calculating tolerance
if iter>20;
tolerance=abs(ffmin(iter-20,run)-fmin0)
end
% displaying iterative results
if iter==1
disp(fprintf('Iteration Best particle Objective fun'));
end
disp(fprintf('%8g %8g %8.4f',iter,index,fmin0));
iter=iter+1;
subplot(1,2,1);
plot(current_position{i}(1),'rO')
xlim([-5*Xmax 5*Xmax])
ylim([-5*Xmax 5*Xmax])
title('Particles instant location')
grid on
hold on;
hold off;
subplot(1,2,2);
plot(iter,global_best_value,'bO')
grid on
hold on
xlim([0 Maxiter]);
title('Best Particle value histogram');
xlabel('iteration no')
ylabel('global best value')
% pause(0.05)
% disp(['BEST PARTICLE VALUE >> ' num2str(global_best_value)])
end
% pso algorithm-----------------------------------------------------end
global_best;
fvalue=global_best_value;
fff(run)=fvalue;
rgbest(run,:)=global_best;
disp(sprintf('--------------------------------------'));
% disp(['BEST PARTICLE POSITION >> ' num2str(global_best)])
end
% pso main program------------------------------------------------------end
disp(sprintf('\n'));
disp(sprintf('*********************************************************'));
disp(sprintf('Final Results-----------------------------'));
[bestfun,bestrun]=min(fff)
best_variables=rgbest(bestrun,:)
disp(sprintf('*********************************************************'));
toc
% PSO convergence characteristic
plot(ffmin(1:ffite(bestrun),bestrun),'-k');
xlabel('Iteration');
ylabel('Fitness function value');
title('PSO convergence characteristic')
%####################################
The function is made in a seperate matlab file as shown below
function F = sofunc2(para)
% Track the output of optsim to a signal of 1
% Variables a1 and a2 are shared with RUNTRACKLSQ
c2 = para(1)
r2 = para(2)
W2 = para(3)
disp(sprintf('The value of parameters c2= %3.0f, r2= %3.0f, W2= %3.0f', para(1),para(2),para(3)));
% Compute function value
simopt = simset('solver','ode1','SrcWorkspace','current','DstWorkspace','current'); % Initialize sim options
[tout,xout,yout] = sim('sosmc_c2',[0 200],simopt);
% e=yout-1 ; % compute the error
% sys_overshoot=max(yout)-2 % compute the overshoot
if para(1)<0 || para(1)>5
penalty=500;
elseif para(2)<0 || para(2)>5
penalty=500;
elseif para(3)<0 || para(3)>5
penalty=500;
else
penalty=0;
end
A=5; %B=1;C=5;
F=int_abs_error*A+penalty
end
The simulink block (attached) contains the model whose parameters are being optimized with the PSO code. All the three parts need to be kept in the same folder for the purpose of running. Please help me figure out what is wrong with it and how to tackle it.
1 Comment
Mangesh modak
on 12 Mar 2017
i think first you should check that current_position is cell and lb and ub are matrices
Answers (0)
See Also
Categories
Find more on Particle Swarm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!