Subscript indices must either be real positive integers or logicals.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Adam
il 10 Lug 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.
Risposte (3)
Walter Roberson
il 10 Lug 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 Commenti
Walter Roberson
il 10 Lug 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.
Image Analyst
il 10 Lug 2017
3 Commenti
Image Analyst
il 13 Lug 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.
Jan
il 13 Lug 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 Commenti
Walter Roberson
il 14 Lug 2017
If you want [-1, 0, 1] to be the possible random outcomes then use randi([[-1 1], 1, VarSize)
Jan
il 17 Lug 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?
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!