필터 지우기
필터 지우기

Problem with index in position 1 exceeding array bounds in PSO algorithm

조회 수: 1 (최근 30일)
Hello guys,
I have a particle swarm optimization code in Mtalab that should find the local minima of a function with two variables. As of now the code is not running because I am getting the error: Index in position 1 exceeds array bounds.
Any thoughts on how to fix this? (I know this is a usuall beginner error but I am just that, a beginner)
I also have another question, anyone knows how to do the following in a if loop or such? I want to multily the weight w with a factor close to 1 such that the multpilication occurs with every iteration till the weight hits a lower limit of 0.3 and then it won't go below that.
This is is my script (I have another scrip for the main objective function):
x_min=[-5,-5];
x_max=[5,5];
n=100;
m=2;
c1=2;
c2=2;
w_max=1.4;
w_min=0.3;
B=0.95;
v_max=5;
Max_iter=100;
for i=1:n
for j=1:m
pos(i,j)=x_min(i,j)+rand().*(x_max(i,j) - x_min(i,j));
end
end
vel=pos;
%function evaluation
out=fun(pos);
pos_best_val=out;
pos_best=pos;
[fminval,index]=min(out);
glo_best=pos_best(index,:);
X=pos;
Out=fun(X);
k=1;
w=w_max;
while k <= Maxiter
w(k)=w_max*B;
if w<w_min
w=w_min;
end
%Update values and positions
new=find(out<=pos_best_val);
pos_best(new,:)=X(new,:);
pos_best_val=out(new);
[f_best_val,ind1]=min(pos_best_val);
if f_best_val<=f_min_val
f_min_value=f_best_val;
glo_best=pos_best(ind1,:);
end
for i=1:n
for j=1:m
vel(i,j)=w.*vel(i,j)+c1.*rand().*(pos_best(i,j)-pos(i,j)) + c2.*rand().*(glo_best(1,j)-pos(i,j)); %updating velocity
pos(i,j)=vel(i,j) + pos(i,j); %updating position
%Handling boundary conditions
if pos(i,j)<x_min(j)
pos(i,j)=x_min(j);
elseif pos(i,j)>x_max(j)
pos(i,j)=x_max(j);
%Velocity condition
if vel(i,j)> v_max
break
end
end
end
end
k=k+1
end
Minima(run)=fun(glo_best);
Minima_pos(run,:)=glo_best;

답변 (1개)

the cyclist
the cyclist 2022년 8월 21일
편집: the cyclist 2022년 8월 21일
x_min and x_max are 1x2 vectors, but in the loop you try to access a second row of those matrices, which do not exist.
I'm not sure what you intend to happen there, so I can't give advice.

카테고리

Help CenterFile Exchange에서 Particle Swarm에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by