While loop for genetic algorithm optimization variables
Show older comments
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
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.
Accepted Answer
More Answers (0)
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!