MATLAB Answers

How to find where a very long code is stuck?

11 views (last 30 days)
Cantor Set
Cantor Set on 19 Feb 2020
Edited: Adam Danz on 20 Feb 2020
This code is a part of coding a mutiobjective genetic algorithm, the algorithm for this code is as follows:
  1. Initialization: Q=Ro; ND= ϕ, Ro is the initial population
  2. Randomly select a row x of Q, and set Q=Q-{x} [here I mean remove the row x from Q], RK= ϕ , R= ϕ
  3. while ( Q ϕ)
  • for each y in Q do,
  • if x dominates y, then remove y from Q and update Q {Q=Q-{y}}
  • elseif, y dominates x then update x=y; and update Q by removing y from Q, Q=Q-{y}, RK=RK R then update R= ϕ [but since they are matrices so I use RK=[RK;R]; ]
  • else, R= R {y} [I used R=[R;Y]; ], update Q by removing the row y from it
  • end(if)
  • end(do)
4. Take all the rows y in RK which are not dominated by x and put them in a matrix RKK
7. If the number of rows in Q >1 then go to step 2, otherwise set ND=[ND;Q]; stop
in the code:
output=FD( x,y,Nvar,perci,Lb,Ub); is a function I wrote takes the rows/individuals x,y and returns 2 by Nvar*perci matrix named output, where the first row is the non-dominated individual and the second is the dominated one
Remark: In FD, 2 individuals/rows x,y has 3 outputs either x dominates y or y dominates x or no one dominates the other
in the first 2 cases FD returns: a matrix, 2 by Nvar*perci matrix named output, where the first row is the non-dominated individual and the second is the dominated one, in the third case it returns a matrix: output or 2 rows and Nvar*perci cols where each element in it is INF
I hope someone can help me because I am a beginner in MATLAB.
Nind=100; %number of individuals to begin with
Nvar=1; %number of the decision variables
perci=10; %number of bits of every decision variable
Ro=crtbp(Nind,Nvar*perci); %random matrix of 0 and 1
%each element in it, is either 0 or 1
%crtbp is a function from GA toolbox
iii=1; DOM=[]; ranks=[]; Lb=-100; Ub=100;
while ~isempty(Ro)
Q=Ro; carQ=size(Q,1);
while carQ>1
Q=Q(2:end,1:end); %remove x from Q
RK=[]; R=[]; RKK=[];
while ~isempty(Q) %while Q is not empty
y=Q(1,:); %let y be the first row in Q
output=FD( x,y,Nvar,perci,Lb,Ub); %check the dominance between y and x
if norm(x- output(1,1:end))==0 %if x dom y
%remove y from Q;
elseif norm(y-output(1,1:end))==0 %if y dom x
%remove y from Q
RK=[RK;R]; R=[];
%remove y from Q
if ~isempty(RK)
for j=1:size(RK,1)
if norm(x-output(1,1:end))~=0 %x~=output(1,1:end)
QSS=[RKK;R]; %%the code gets stuck here%%%
if carQ>1
UND=unique(ND, 'rows', 'stable');
%assigning ranks
Matlab tell me that the error at the line I referred to but I don't understand why it tells me:
QSS is 1x10 double, RKK 0X0 empty double and R is 2x10 double
and that Q is empty 0x10 double!
Why? and what does it mean?
EDIT: The code sometimes work and sometimes it gets stuck, so maybe there is something computational that happens
because every time crtbp generates a different random binary matrix Ro
QSS=[RKK;R]; %it points that the problem is here
And it says that RKK is empty 0 x 0 double
QSS is empty 1 x 10 double
R is 2 x 10 double and that Q is 0 x 0 empty
Q is a matrix which changes its size through the loop


Walter Roberson
Walter Roberson on 20 Feb 2020
When you click pause, it stops at the beginning of the next line of code. But that does not mean that that next line of code is the one that takes a long time; it could be the previous line of code, or it could be that you just happened to pause while it was executing one of the short fast lines while it was executing a section that has some slow lines.
the cyclist
the cyclist on 20 Feb 2020
One good way to debug a problem like this is to type
dbstop if error
and then run your code. The debugger will stop at the exact line that is causing the error. Then you can examine the state of your workspace, to see which other variables might causing the problem, or which iteration of a loop you are on, etc.
After you are done debugging, you'll likely want to type
dbclear if error
to exit this mode of debugging.
See this documentation for more details on using the debugger.
Adam Danz
Adam Danz on 20 Feb 2020
Some methods to locate bottlenecks are
  • use profile to time the function and produce a breakdown of execution times
  • Within the inner most loop print the value of all loop iterations such as fprintf('i=%d, j=%d, k=%d\n', i,j,k)
  • Step through the code in debug mode
  • Strategically place tic/toc pairs throughout your code

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!