简体   繁体   中英

“”“Diagonal”“” -1/+1 Matrix

I need to construct the tech cycle constraint matrix Aa and the right side ba. The aim is building the technology cycle matrices in order to solve the scheduling linear problem constrained by Ax<=b. In this case -1 and +1 in A refers to the coefficients of the constraints of the problem such as starting times and precedences

TC = [1,2,3,4,6,7;1,2,5,4,6,7;2,5,6,7,0,0];                               % Technology cycle
CT = [100,60,200,160,80,120;100,60,150,120,60,150;50,120,40,30,0,0];      % Cycle time

n_jb = size(TC,1);                      % number of jobs
n_op = sum(TC~=0,2);                    % number of operations for each job
N_op = sum(n_op);                       % total number of operations


c=1;                                    % indice for constraints in Aa
Op=1;                                   % counter for overall operation
n_tf = N_op - n_jb- sum(n_op==1);       % number of job transfer between machines (also number of tech cycle constraint numbers)
Aa   = zeros(n_tf,N_op);                % Constraint matrx for tech cycle 
ba   = zeros(n_tf,1);                   % The right vector of the constraint function: Aa*x<=ba

for j=1:n_jb

    if n_op(j)>1

        for op=1:n_op(j)-1
            Aa(c,Op)=-1;
            Aa(c,Op+1)=1;
            ba(c,1)=CT(j,op);  
            c=c+1;            
            Op=Op+1;
        end

    else
        Op=Op+1;
    end
    Op=Op+1;
end

The output, like Aa is 3 """diagonal""" -1/+1 matrices:

-1  1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   -1  1   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   -1  1   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   -1  1   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   -1  1   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   -1  1   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   -1  1   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   -1  1   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   -1  1   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   -1  1   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   -1  1   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   -1  1   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   -1  1

In order to be more precise in the following there is an image: 图片 showing the 3 different part of the matrix Aa . My question is : Is there a way to build the same this avoiding loops since A is not a 3x1 but will definitely become 30-50x1?

You can use diag to create the positive and negative ones. The second input to diag is to shift the diagonal to the side. In this case, 1 to the right.

Use cumsum to find the rows you want to remove. For n = [6, 6, 4] , you want to remove the 6th, 12th and 16th row.

n = [6, 6, 4];
cols = sum(n);
A = -eye(cols) + diag(ones(cols-1,1), 1);
A(cumsum(n),:) = []

A =    
    -1    1    0    0    0    0    0    0    0    0    0    0    0    0    0    0
     0   -1    1    0    0    0    0    0    0    0    0    0    0    0    0    0
     0    0   -1    1    0    0    0    0    0    0    0    0    0    0    0    0
     0    0    0   -1    1    0    0    0    0    0    0    0    0    0    0    0
     0    0    0    0   -1    1    0    0    0    0    0    0    0    0    0    0
     0    0    0    0    0    0   -1    1    0    0    0    0    0    0    0    0
     0    0    0    0    0    0    0   -1    1    0    0    0    0    0    0    0
     0    0    0    0    0    0    0    0   -1    1    0    0    0    0    0    0
     0    0    0    0    0    0    0    0    0   -1    1    0    0    0    0    0
     0    0    0    0    0    0    0    0    0    0   -1    1    0    0    0    0
     0    0    0    0    0    0    0    0    0    0    0    0   -1    1    0    0
     0    0    0    0    0    0    0    0    0    0    0    0    0   -1    1    0
     0    0    0    0    0    0    0    0    0    0    0    0    0    0   -1    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM