Subscript indices must either be real positive integers or logicals.

2 Ansichten (letzte 30 Tage)
Uros Jovanovic
Uros Jovanovic am 10 Jul. 2017
Kommentiert: Jan am 17 Jul. 2017
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 Kommentar
Adam
Adam am 10 Jul. 2017
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.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Walter Roberson
Walter Roberson am 10 Jul. 2017
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 Kommentare
Walter Roberson
Walter Roberson am 10 Jul. 2017
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 am 13 Jul. 2017
Bearbeitet: Uros Jovanovic am 13 Jul. 2017
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

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 10 Jul. 2017
  3 Kommentare
Image Analyst
Image Analyst am 13 Jul. 2017
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 am 14 Jul. 2017
Thanks, I corrected it and it still doesn't work, any thoughts perhaps, please?

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 13 Jul. 2017
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 Kommentare
Walter Roberson
Walter Roberson am 14 Jul. 2017
If you want [-1, 0, 1] to be the possible random outcomes then use randi([[-1 1], 1, VarSize)
Jan
Jan am 17 Jul. 2017
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by