MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

Why is this code stuck?

1 view (last 30 days)
Cantor Set
Cantor Set on 20 Feb 2020
Closed: Stephen Cobeldick on 20 Feb 2020
This code is a part of coding a mutiobjective genetic algorithm, the algorithm for this code is the following:
  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

  3 Comments

Walter Roberson
Walter Roberson on 20 Feb 2020
This should be posted as part of https://www.mathworks.com/matlabcentral/answers/506504-how-to-find-where-a-very-long-code-is-stuck and you should indicate what the input values are so people can test.
People will also need function FD
Walter Roberson
Walter Roberson on 20 Feb 2020
if norm(x- output(1,1:end))==0 %if x dom y
What if that is never satisfied because of round-off error in the calculation?
Cantor Set
Cantor Set on 20 Feb 2020
The original statement in the algorithm says that if x which is a a row vector where each element is either zero or one is the same as output(1,:) which is also a row vector of zero and one then excute the following succeeding commands but I don't how to tell matlab this so I thought if the norm of the difference between 2 binary vectors is zero then they must be the same, both vectors are of the same length.
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

Answers (0)

Community Treasure Hunt

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

Start Hunting!