How to find where a very long code is stuck?

15 ビュー (過去 30 日間)
Cantor Set
Cantor Set 2020 年 2 月 19 日
編集済み: Adam Danz 2020 年 2 月 20 日
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
5.ND=[ND;x];
6.Q=[RKK;R];
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.
Thanks
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[Fronts,ranks]=IMPARENA(Nvar,perci,Ro,Lb,Ub)
iii=1; DOM=[]; ranks=[]; Lb=-100; Ub=100;
while ~isempty(Ro)
Q=Ro; carQ=size(Q,1);
ND=[];
while carQ>1
x=Q(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;
Q(all(Q==repmat(y,size(Q,1),1),2),:)=[];
elseif norm(y-output(1,1:end))==0 %if y dom x
x=y;
%remove y from Q
Q(all(Q==repmat(y,size(Q,1),1),2),:)=[];
RK=[RK;R]; R=[];
else
R=[R;y];
%remove y from Q
Q(all(Q==repmat(y,size(Q,1),1),2),:)=[];
end
if ~isempty(RK)
for j=1:size(RK,1)
rr=RK(j,1:end);
output=FD(x,rr,Nvar,perci,Lb,Ub);
if norm(x-output(1,1:end))~=0 %x~=output(1,1:end)
RKK=[RKK;rr];
else
j=j+1;
end
end
end
ND=[ND;x];
QSS=[RKK;R]; %%the code gets stuck here%%%
Q=QSS;
carQ=size(Q,1);
if carQ>1
break;
end
end
end
ND=[ND;Q];
%%%%%%%%%%%%%%%%%%%%%%%%%
UND=unique(ND, 'rows', 'stable');
ND=UND;
%%%%%%%%%%%
DOM=[DOM;ND];
%%%%%%
%assigning ranks
rD=iii*ones(size(ND,1),1);
ranks=[ranks;rD];
iii=iii+1;
%%%%%%%%%%%%%%%
C=setdiff(Ro,ND,'rows');
Ro=C;
end
%%%%%%%%%END%%%%%%%%%%%%
Fronts=[DOM];
ranks;
end
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
Q=QSS;
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
  3 件のコメント
the cyclist
the cyclist 2020 年 2 月 20 日
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 2020 年 2 月 20 日
編集済み: Adam Danz 2020 年 2 月 20 日
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

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by