问题最有趣的一点是,我并不总是在运行代码时遇到错误(我在5次中得到3次)。确切的错误消息在下面给出
“尝试访问para(2);索引越界,因为numel(para)= 1。
sofunc2出错(第6行) r2 = para(2)
sosmc_sch1_c2中的错误(第55行) obj_value(ⅰ)= sofunc2(CURRENT_POSITION {:,I}); “
代码如下:
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=[2 2 2]; %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)*1E10;
global_best_value=1E10;
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=10*(global_best(1)-1)^2+20*(global_best(2)-2)^2+30*(global_best(3)-3)^2;
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')
%##########################################################################
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
代码中有另一部分涉及名为'sosmc_c2'的simulink块。但是,该框图没有问题,并且可以与同一程序的其他实例平滑运行。我想知道在这种特殊情况下究竟是什么导致了错误,我该如何解决呢。
答案 0 :(得分:0)
我认为你可能正在调用函数,错误的参数:
obj_value(i)=sofunc2(current_position{i});
而不是current_position{:,i}
。
据我所知,current_position
是单元格的列向量,而不是矩阵。我无法完全运行代码,因为我错过了sim
和simset
。
另一个错误,由'daren shan'指出
检查您正在比较细胞和载体的边界条件。
for i=1:S
for j=1:d
if current_position{i}(j)<LB(j)
current_position{i}(j)=LB(j);
elseif current_position{i}(j)>UB(j)
current_position{i}(j)=UB(j);
end
end
end
以前发生的事情是,如果整个向量current_position{i}
小于或大于LB
/ UB
,则向量将被单个值替换。
答案 1 :(得分:0)
这是错误: - |
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
你正在比较1x3矩阵的当前位置和LB(j)这是一个“单元素矩阵”!!!!