Subscript indices must either be real positive integers or logicals.

1 次查看(过去 30 天)
Hi everyone,
I am trying to implement bee algorithm for my project. I have a function that goes like this:
function [L]=myTourLength(tour,model)
n=numel(tour);
tour=[tour tour(1)];
L=0;
for i=1:n
L=L+model.D(tour(i),tour(i+1));
end
end
and I call it in my other script, but this error turns up at this part of the code:
for it=1:MaxIt
% Recruited Bees
for i=1:nPop
% Choose k randomly, not equal to i
K=[1:i-1 i+1:nPop];
k=K(randi([1 numel(K)]));
% Define Acceleration Coeff.
phi=a*unifrnd(-1,+1,VarSize);
% New Bee Position
newbee.Tour=pop(i).Tour+phi.*(pop(i).Tour-pop(k).Tour);
% Evaluation
newbee.Cost=CostFunction(pop(i).Tour); %>>>
% Comparision
if newbee.Cost<=pop(i).Cost
pop(i)=newbee;
else
C(i)=C(i)+1;
end
end
Specifically at line with the >>>, where I evaluate the solution, and it reports an error to the function myTourLenght to line inside the for loop (where L is calculated) w. Any thoughts, please?
  1 个评论
Adam
Adam 2017-7-10
Use the Stop on errors setting in the Breakpoints menu of the editor. Finding the source of these errors is generally trivial once you are stopped in the code at the iteration causing the error.

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2017-7-10
Insufficient information. The most likely is that the model.D that you do not show the definition of is a matrix, and some element of pop(i).Tour is 0 or negative or not an integer.
You do not show us the value for a in building phi
By the way, why are you constructing newbie.Tour as different than pop(i).Tour, but then calculating the cost of pop(i).Tour ? It would seem to make more sense to calculate the cost of newbie.Tour ?
  3 个评论
Walter Roberson
Walter Roberson 2017-7-10
How did you initialize pop(i).Tour ?
It is not obvious to us that pop(i).Tour can never be less than pop(k).Tour so it is not obvious that (pop(i).Tour-pop(k).Tour) cannot be negative.
Suppose for example that pop(i).Tour is 3 at one point and pop(k).Tour is 7 at that point. Then pop(i).Tour-pop(k).Tour would be -4. Now suppose phi is +1 so phi times that is still -4. Then pop(i).Tour+phi.*(pop(i).Tour-pop(k).Tour) would be 3+(-4) which would be -1 . Then inside myTourLength you have model.D(tour(i),tour(i+1)) which could be model.D(-1,something) . If model.D is a matrix rather than a function, that would be an attempt to index the array at a negative value.
Uros Jovanovic
Uros Jovanovic 2017-7-13
编辑:Uros Jovanovic 2017-7-13
I looked at the code and I figured out that the problem in 'tour' inside the myTourLenght function, it takes non integer values in for loop. Here is most of the code, the pop(i) is initialized in the for loop:
time=[1 2];
cost=[2 3];
n=numel(time);
d=zeros(n,n);
for i=1:n-1
for j=i+1:n
d(i,j)=sqrt((time(i)-time(j))^2+(cost(i)-cost(j))^2);
d(j,i)=d(i,j);
end
end
model.n=n;
model.time=time;
model.cost=cost;
model.d=d;
%problem definition
CostFunction=@(tour) myTourLength(tour,model);
nVar=model.n;
%parameters for bees
MaxIt=25; % Maximum Number of Iterations
nPop=20; % Population Size (Colony Size)
nOnlooker=nPop; % Number of Onlooker Bees
L=round(0.6*nVar*nPop); % Abandonment Limit Parameter (Trial Limit)
a=1; % Acceleration Coefficient Upper Bound
VarSize=[1 nVar]; % Decision Variables Matrix Size
VarMin=-10; % Decision Variables Lower Bound
VarMax= 10;
BestCost=zeros(MaxIt,1); % Array to Hold Best Cost Values
%Initialization
%Empty bee
empty_bee.Tour=[];
empty_bee.Cost=[];
% Initialize Population Array
pop=repmat(empty_bee,nPop,1);
% Initialize Best Solution Ever Found
BestSol.Cost=inf;
% Create Initial Population
for i=1:nPop
pop(i).Tour=randi([1,nVar]);
pop(i).Cost=CostFunction(pop(i).Tour);
if pop(i).Cost<=BestSol.Cost
BestSol=pop(i);
end
end
% Abandonment Counter
C=zeros(nPop,1);
% Array to Hold Best Cost Values
BestCost=zeros(MaxIt,1);
Don't know if you can do any trick here to make it work or not, but really appreciate your effort

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-7-10
  3 个评论
Image Analyst
Image Analyst 2017-7-13
In your code, you do this:
model.d=d;
However your error message says
Error in myTourLength (line 9) L=L+model.D(tour(i),tour(i+1));
"model" has a "d" field, but not a "D" field. MATLAB is case sensitive. Pick one or the other: d or D.
Uros Jovanovic
Uros Jovanovic 2017-7-14
Thanks, I corrected it and it still doesn't work, any thoughts perhaps, please?

请先登录,再进行评论。


Jan
Jan 2017-7-13
Start with posting a copy of the complete error message. This is very helpful in the forum.
Then find out, which indexing is concerned:
newbee.Cost = CostFunction(pop(i).Tour);
While i is a valid index, no other index operation is performed here. Except if you have defined "CostFunction" as an array and the output of pop(i).Tour) is not a valid integer. So please use teh debugger to exmine this. Type this in the command window
dbstop if error
(or use the corresponding menu mentioned by Adam). Then run the code again until it stops now check this:
i
pop(i).Tour
which CostFunction -all % The topmost matters
CostFunction(pop(i).Tour)
newbee
What do you get as output?
  11 个评论
Walter Roberson
Walter Roberson 2017-7-14
If you want [-1, 0, 1] to be the possible random outcomes then use randi([[-1 1], 1, VarSize)
Jan
Jan 2017-7-17
newbee.Tour should be a row vector, a tour/path which newbee takes
This is not clear yet: How is "tour" defined? What values does this vector contain? Are these the current angles of the trajectory agains the North direction in floating point notation? Or a [-1,0,1] for moving to the left, straight ot to the right?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by