"""对角线""" -1 / + 1矩阵

时间:2014-12-12 10:49:36

标签: matlab matrix

我需要构建技术周期约束矩阵Aa和右侧ba。目标是构建技术循环矩阵以便解决由Ax< = b约束的调度线性问题。在这种情况下,A中的-1和+1表示问题约束的系数,例如开始时间和优先级

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

输出,如Aa是3“”“对角线”“”-1 / + 1矩阵:

-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

为了更精确地在下面有一个图像:image显示矩阵Aa的3个不同部分。 我的问题是:有没有办法建立相同的避免循环,因为A不是3x1但肯定会变成30-50x1?

1 个答案:

答案 0 :(得分:3)

您可以使用diag创建正面和负面的内容。 diag的第二个输入是将对角线移到侧面。在这种情况下,1在右边。

使用cumsum查找要删除的行。对于n = [6, 6, 4],您要删除第6行,第12行和第16行。

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
相关问题