While loop for genetic algorithm optimization variables

13 views (last 30 days)
Hello matlab experts ;),
I would like to search a matrix (Prec_Mat) filled with 0 and 1. Based on this matrix (Prec_Mat), a new matrix (Z) needs to be filled so that in matrix (Z) exactly one entry per row and exactly one entry per column contains a 1, like you see below.
To create the matrix (Z) I would like to switch the content of two arrays called (y_row) and (y_col), applying a GA-algorithm with integer constraints by using the matlab optimization toolbox.
To do so, I created a lower bound [1] and upper bound [6] for all variables of (y_row) and (y_col).
Could you please help me with these "while"-conditions? Is it possible to force the algorithmn by these "while"-constraints to generate a valid solution for matrix (Z)? Do you have other solutions for this problem?
Prec_Mat = [1 1 1 1 1 0;
1 1 0 1 1 1;
1 0 1 1 1 1;
1 1 1 1 1 0;
1 1 1 1 1 0;
0 1 1 1 1 1];
[Job_Num, ~] = size(Prec_Mat);
Z = zeros(Job_Num);
while any(sum(Z,1)~=1) || any(sum(Z,2)~=1)
for i = 1:Job_Num
%y_row =[1 5 3 6 4 2]; % This is only for testing the while condition
%y_col =[1 2 3 4 5 6];
y_row = [y(1) y(2) y(3) y(4) y(5) y(6)]; % I would like to switch between these values with integer constraints until the while conditions are true
y_col = [y(7) y(8) y(9) y(10) y(11) y(12)]; % these two arrays are created as optimization variables with the GA - optimization toolbox
if Prec_Mat(y_row(i),y_col(i)) == 1
Z(y_row(i),y_col(i)) = 1;
end
end
end
% This is an example of the Z matrix based on y_row =[1 5 3 6 4 2] and y_col =[1 2 3 4 5 6]
% with exactly with one entry per row and exactly one entry per column containing a 1 for (Z)
% Z = [1 0 0 0 0 0;
% 0 0 0 0 0 1;
% 0 0 1 0 0 0;
% 0 0 0 0 1 0;
% 0 1 0 0 0 0;
% 0 0 0 1 0 0];
% @ this point I generate a job order based on the solution in matrix Z
for j = 1:Job_Num
for i = 1:Job_Num
if Z(i,j) == 1
job_order(j,1) = i;
end
end
end
  1 Comment
Walter Roberson
Walter Roberson on 12 Feb 2021
Z = zeros(Job_Num);
You started it all zero
while sum(Z,1) == 1
Something that is all zero cannot have a sum along any path that is 1. There are no 1's in an all-zero matrix.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Feb 2021
You edited your code, but did not repair this issue.
Your test sum(Z,1)==1 is a vector test because sum() of a 2D array along one of the dimensions is a vector along the other dimension. When you are testing a vector in while or if, the test is considered to succeed provided that all of the values being tested are non-zero (true). Your while would therefore end as soon as there was any column that had no ones at all, or any column that had more than one one. Your loops would continue as long as the sum of every row and every column was one, and so you can be sure that either the row sum or the column sum will not be 1 when you leave the while loops. But that is the opposite of what you want: you want to continue until you find row sum and column sum both 1.
while any(sum(Z,1)~=1) || any(sum(Z,2)~=1)
single loop for the termination test.
This assumes that the body of the loop does something useful that is not the same between loop iterations. Which is a problem with your current code, as the body of the loop does exactly the same thing every iteration.
  1 Comment
Paul Haase
Paul Haase on 13 Feb 2021
Thank you very much for your help and the tip about updating the code. The "while" loop works perfectly! Unfortunatly I have just noticed that the algorithm is not generating new variables in the arrays of (y_row) & (y_col) until the "while"-condition is met. To do so I changed the array of (y_row) & (y_col) as follows:
y_row = [y(1) y(2) y(3) y(4) y(5) y(6)];
y_col = [y(7) y(8) y(9) y(10) y(11) y(12)];
I updated this, too.
Do you know how to implement this "while" condition, so that only populations result that satisfy this condition? Or do you think it is possible that the population changes in the run until the condition is met? If this is not working, would you implement it in a completely different way? Do you have any suggestions for me, please?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!