Subscript indices must either be real positive integers or logicals.

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 Comment

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.

Sign in to comment.

Answers (3)

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 Comments

Thanks for the answer, sorry for not showing the value of a.
a=1, so when you have that you can calculate phi. Value of L is 0, don't know how to fix that. Model is 1x1 struck with 4 fields.
You mean just to put newbee.Cost=CostFunction(newbee.Tour), instead of newbee.Cost=CostFunction(pop(i).Tour), where the evaluation happens?
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.
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

Sign in to comment.

3 Comments

I already read this, did not help, if you can provide some other answer, please, that would be nice
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.
Thanks, I corrected it and it still doesn't work, any thoughts perhaps, please?

Sign in to comment.

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 Comments

The index problem is likely happening inside CostFunction in the reference to Model.D()
when I run the code, this is the entire error that pops up:
Reference to non-existent field 'D'.
Error in myTourLength (line 9) L=L+model.D(tour(i),tour(i+1));
Error in novepcele>@(tour)myTourLength(tour,model)
Error in novepcele (line 48) pop(i).Cost=CostFunction(pop(i).Tour); Also, you can see how I defined the cost function as:
CostFunction=@(tour) myTourLength(tour,model);
You can see most of the code above. I used dbstop if error and it points out to the line newbee.cost=CostFunction(newbee.Tour)
When I run:
i
pop(i).Tour
which CostFunction -all % The topmost matters
CostFunction(pop(i).Tour)
newbee
I get:
i =
1
Undefined variable "pop" or class "pop".
9 L=L+model.D(tour(i),tour(i+1));
'CostFunction' not found.
Undefined variable "pop" or class "pop".
Undefined function or variable 'newbee'.
Any thoughts? I really appreciate it
Your code defines a field named 'd' for models. Your tour cost function looks for a field named 'D' .
Thanks, I corrected it, but still shows the same error message:
Subscript indices must either be real positive integers or logicals.
Error in myTourLength (line 9)
L=L+model.D(tour(i),tour(i+1));
Error in novepcele>@(tour)myTourLength(tour,model)
Error in novepcele (line 77)
newbee.Cost=CostFunction(pop(i).Tour);
The problem is, as I can see it, the "tour" part that has non integer values instead of integer ones. When I run similar code that I wrote for ant colony, it works, the tour part has integer values, but here in case of bees it doesn't. Any suggestions, please?
Only you know how to interpret those values. To get rid of the error you can simply wrap the tour values in a round(...) instruction, but whether that is an acceptable fix or just hiding a problem from earlier in the code where non-integer results should not have been created only you really know.
The values of tour are defined here:
% Define Acceleration Coeff.
phi=a*unifrnd(-1,+1,VarSize);
% New Bee Position
newbee.Tour=pop(i).Tour+phi.*(pop(i).Tour-pop(k).Tour);
You cannot expect them to be integers. But you could round() them. But even then they could be <= 0 .
Yes, I see now, the problem are the values of the acceleration coefficient, specifically the part unifrnd(-1,1,VarSize) that returns non - integer values, I'll try to fix that somehow, to return integer values. Thank you very much
What function should I use to obtain integer values instead of these ones? Any suggestions? Thanks
@Uros: It depends on what you want as output. What is the meaning of the value of newbee.Tour? Perhaps an floating point value is correct, but the index operation later on is wrong and you can something else in the CostFunction. I don't think, that we have enough information to guess, how the code must be changed.
newbee.Tour should be the tour that the newbee is taking, the value should be a row vector. I corrected the previous error, but the program reports another error:
Index exceeds matrix dimensions.
Error in myTourLength (line 9)
L=L+model.D(Tour(i),Tour(i+1));
Error in novepcele>@(tour)myTourLength(tour,model)
Error in novepcele (line 77)
newbee.Cost=CostFunction(pop(i).Tour);
And the matrix dimensions are not okay, I now that, but how do I fix this now? Tour is a row vector now, but when the equation for L is ran it gives this message.
newbee.Tour should be a row vector, a tour/path which newbee takes
If you want [-1, 0, 1] to be the possible random outcomes then use randi([[-1 1], 1, VarSize)
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?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 10 Jul 2017

Commented:

Jan
on 17 Jul 2017

Community Treasure Hunt

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

Start Hunting!