-5

你能给我建议我的代码来改进时间计算吗?因为这段代码会花费很多时间。有时我不会得到结果。我的案例是如何结合 RandomMatriceA [1 12] 和 RandomMatriceB [0 2]。

例子:

ProcessTime=[2 2 2 3 3] %number of consecutive value RandomMatriceA

RandomMatriceA=[1 2 3 4 5;
               3 4 2 1 5;
               1 4 2 3 5;
               5 1 2 3 4]

RandomMatriceB=[0 2 0 0 1;%humber of consecutive zeros
                0 0 0 1 1;
                1 2 1 0 0;
                0 0 1 1 1]

Result   =[1 1 0 0 2 2 3 3 4 4 4 0 5 0 0 0;
           3 3 4 4 4 2 2 0 1 1 0 5 5 5 0 0;
           0 1 1 0 0 4 4 4 0 2 2 3 3 5 5 5;   
           5 5 5 1 1 0 2 2 0 3 3 0 4 4 4 0]

NewMatric{1}=[1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
              0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0;
              0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0;
              0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0;

NewMatric{2}= just only identify value 2;
NewMatric{3}= for value 3  
NewMatric{4}= value 4;

实际上,我制作了以下代码来强制 NewMatric 中的每一列必须具有每列值 1 的总和等于 2。

如果你们中的某个人想给我建议以改善计算时间,非常感谢。

    clc
clear all
A=2;
B=12;
C=4;
ProcessTime= [ 11 11 11 11 4 4 4 4 4 4 4 2 ]; %Converting Matrice

RandomMatriceA=zeros(A,B,C);
RandomMatriceB=zeros(A,B,C);
SumRandomMatriceB=zeros(1,A,C);
ConvertMatriceA=zeros(A,B,C);
ProcessRandomMatriceA=zeros(A,B,C);
StartProcess=zeros(A,B,C);
ZerosCoordinate=zeros(A,B,C);
total=zeros(1,1,C);

%make first random matrice
for ii=1:C;    
    for a=1:A
        RandomMatriceA(a,:,ii)=randperm(B); %batasan tidak boleh satu kelompok melakukan lebih dari satu aktivitas dalam satu waktu
    end
end
%make second random matrice
for ii=1:C;
    RandomMatriceB(:,:,ii)=randint(A,B,[0 2]);
end

%Make sure sum all element per row in RandomMatriceB <=11
for ii=1:C;
    SumRandomMatriceB(1,:,ii)=sum(RandomMatriceB(:,:,ii),2); 
    for jj=1:A,
        while SumRandomMatriceB(1,jj,ii) > 11 
              RandomMatriceB(jj,:,ii)=randint(1,B,[0 2]);
              SumRandomMatriceB(1,:,ii)=sum(RandomMatriceB(:,:,ii),2);
        end
    end  
end

%After making RandomMatriceA and RandomMatriceB, then Make New Matrice
%which has size (8,85,10)
%To know varible of new matrice which is result of combining
%RandomMatriceA,RandomMatriceB and ProcessTime
for ii=1:C;
    for a=1:A,
        for b=1:B,
            ConvertMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii));%FirstVarible:Consecutive The Number of value in all element of RandomMatriceA
            ProcessRandomMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii))+RandomMatriceB(a,b,ii);
            EndProcess=cumsum(ProcessRandomMatriceA,2);%secondVaribale:to know in which column The Consecutive value RandomMatrice will be end
            StartProcess(a,b,ii)=EndProcess(a,b,ii)-(ProcessTime(RandomMatriceA(a,b,ii))-1);%ThirdVariable:to know in which column The Consecutive Value will be start
        end
    end
end

%To know in which column and row in new matrice will have zeros
 for ii=1:C,        
    for a=1:A;
        for b=1:B
            if RandomMatriceB (a,b,ii)~=0
               ZerosCoordinate(a,b,ii)=StartProcess(a,b,ii)-RandomMatriceB(a,b,ii);%To know in which column in new matrice will have value zeros
            else
               ZerosCoordinate(a,b,ii)=RandomMatriceB(a,b,ii);
            end
        end
    end
 end
%to force/make column 43th will be filled by zeros in every row.
for ii=1:C;
       while size(find(ZerosCoordinate(:,:,ii)==43),1)<A, %looping untuk mendapatkan hari libur diminggu ke 43
           for a=1:A;
              if numel(find(ZerosCoordinate(a,:,ii)==43))==1, %mengharuskan adanya libur di minggu ke 43
                 continue;
              else
                 %make sure sum of element new random row in RandomMatriceB
                 %<=11
                 newRow = randi([0 2],1,B);
                 sumnewRow=sum(newRow,2);
                 while sumnewRow >11,
                       newRow = randi([0 2],1,B);
                       sumnewRow=sum(newRow,2);
                 end
                 %replace newRow to row in RandomMatriceB
                 RandomMatriceB(a,:,ii)=newRow; %proses pergantian baris yang tidak sesuai dengan konstrain
                 %random row in RandomMatriceA
                 RandomMatriceA(a,:,ii)=randperm(B); %randoming baris kromosom yang tidak sesuai dengan konstrain
                 %Try to know variabel to make NewMatrice
                 for b=1:B;
                     ConvertMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii));
                     ProcessRandomMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii))+RandomMatriceB(a,b,ii);
                     EndProcess=cumsum(ProcessRandomMatriceA,2);
                     StartProcess(a,b,ii)=EndProcess(a,b,ii)-(ProcessTime(RandomMatriceA(a,b,ii))-1);
                 end
                 for b=1:B
                     %To know column and row which has value zeros
                    if RandomMatriceB(a,b,ii)~=0
                       ZerosCoordinate(a,b,ii)=StartProcess(a,b,ii)-RandomMatriceB(a,b,ii);
                    else
                       ZerosCoordinate(a,b,ii)=RandomMatriceB(a,b,ii); 
                    end
                end
             end
           end
       end
end

%Make New Matrice (8,85,10) by considering variable above 
max_row_length = max(StartProcess(:,B) + ConvertMatriceA(:, B) - 1); %to know row length
result = zeros(size(StartProcess, 1),max_row_length, size(StartProcess,3)); %preallocation size

for row = 1 : size(StartProcess, 1)
    for column = 1 : size(StartProcess, 2)
         for depth = 1 : size(StartProcess,3)
             s = StartProcess(row, column,depth);
             c = ConvertMatriceA(row, column,depth);
             v = RandomMatriceA(row,column,depth);
             result(row, s : s + c - 1,depth) = v;%this matrix is new matrix (8,85,10) which is result of combining RandomMatriceA,RandomMatriceB and ProcessTime
         end
     end
end

%Make A new Matrice per Value (B).
newMatrice = arrayfun(@(b) (result == b).*cumsum((result == b),2),nonzeros(unique(result)), 'UniformOutput', false);  %indikator pemakaian bagian

%to know how many number of value 1 per column in matrice
%bagian1,bagian2,bagian3 and bagian4
SumOnePerColumn=cell(1,4);
SumOnePerColumn{1}=sum(newMatrice{1}==1);
SumOnePerColumn{2}=sum(newMatrice{2}==1);
SumOnePerColumn{3}=sum(newMatrice{3}==1);
SumOnePerColumn{4}=sum(newMatrice{4}==1);

%to count how many column which has sum of value one per column ~=2&~=0,
gab=cell(1,4);
gabb{1}=sum(SumOnePerColumn{1}~=0 &SumOnePerColumn{1}~=2);
gabb{2}=sum(SumOnePerColumn{2}~=0 &SumOnePerColumn{2}~=2);
gabb{3}=sum(SumOnePerColumn{3}~=0 &SumOnePerColumn{3}~=2);
gabb{4}=sum(SumOnePerColumn{4}~=0 &SumOnePerColumn{4}~=2);


for yyy=1:C; 
total(1,1,yyy)= gabb{1}(1,1,yyy)+gabb{2}(1,1,yyy)+gabb{3}(1,1,yyy)+gabb{4}(1,1,yyy);%Total
end

for ii=1:C;
    if total(:,:,ii)==0;
        continue;
    else
       while total(:,:,ii)~=0;%to make total=0 by randoming RandomMatriceA and RandomMatriceB
             for a=1:A
                 RandomMatriceA(a,:,ii)=randperm(B); %batasan tidak boleh satu kelompok melakukan lebih dari satu aktivitas dalam satu waktu
             end
             %make second random matrice
             RandomMatriceB(:,:,ii)=randint(A,B,[0 2]);
             %Make sure sum all element per row in RandomMatriceB <=11
             SumRandomMatriceB(:,:,ii)=sum(RandomMatriceB(:,:,ii),2); 
             for jj=1:A,
                    while SumRandomMatriceB(1,jj,ii) > 11 
                    RandomMatriceB(jj,:,ii)=randint(1,B,[0 2]);
                    SumRandomMatriceB(1,:,ii)=sum(RandomMatriceB(:,:,ii),2);
                    end
             end  
             for a=1:A,
                 for b=1:B,
                      ConvertMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii));%FirstVarible:Consecutive The Number of value in all element of RandomMatriceA
                      ProcessRandomMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii))+RandomMatriceB(a,b,ii);
                      EndProcess=cumsum(ProcessRandomMatriceA,2);%secondVaribale:to know in which column The Consecutive value RandomMatrice will be end
                      StartProcess(a,b,ii)=EndProcess(a,b,ii)-(ProcessTime(RandomMatriceA(a,b,ii))-1);%ThirdVariable:to know in which column The Consecutive Value will be start
                 end
                 for bb=1:B
                     if RandomMatriceB (a,bb,ii)~=0
                        ZerosCoordinate(a,bb,ii)=StartProcess(a,bb,ii)-RandomMatriceB(a,bb,ii);%To know in which column in new matrice will have value zeros
                     else
                        ZerosCoordinate(a,bb,ii)=RandomMatriceB(a,bb,ii);
                     end
                 end
             end
       while size(find(ZerosCoordinate(:,:,ii)==43),1)<A, %looping untuk mendapatkan hari libur diminggu ke 43
           for a=1:A;
              if numel(find(ZerosCoordinate(a,:,ii)==43))==1, %mengharuskan adanya libur di minggu ke 43
                 continue;
              else
                 %make sure sum of element new random row in RandomMatriceB
                 %<=11
                 newRow = randi([0 2],1,B);
                 sumnewRow=sum(newRow,2);
                 while sumnewRow >11,
                       newRow = randi([0 2],1,B);
                       sumnewRow=sum(newRow,2);
                 end
                 %replace newRow to row in RandomMatriceB
                 RandomMatriceB(a,:,ii)=newRow; %proses pergantian baris yang tidak sesuai dengan konstrain
                 %random row in RandomMatriceA
                 RandomMatriceA(a,:,ii)=randperm(B); %randoming baris kromosom yang tidak sesuai dengan konstrain
                 %Try to know variabel to make NewMatrice
                 for b=1:B;
                     ConvertMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii));
                     ProcessRandomMatriceA(a,b,ii)=ProcessTime(RandomMatriceA(a,b,ii))+RandomMatriceB(a,b,ii);
                     EndProcess=cumsum(ProcessRandomMatriceA,2);
                     StartProcess(a,b,ii)=EndProcess(a,b,ii)-(ProcessTime(RandomMatriceA(a,b,ii))-1);
                 end
                 for b=1:B
                     %To know column and row which has value zeros
                    if RandomMatriceB(a,b,ii)~=0
                       ZerosCoordinate(a,b,ii)=StartProcess(a,b,ii)-RandomMatriceB(a,b,ii);
                    else
                       ZerosCoordinate(a,b,ii)=RandomMatriceB(a,b,ii); 
                    end
                end
             end
           end
       end
       %To make one combining matrice to replace combining matrice which
       %doesnt have total=0
       max_row= 85;
       DummyResult = zeros(A, max_row);
       for bbb= 1 : B
           for aaa= 1 : A
               su = StartProcess(aaa,bbb,ii);
               cu = ConvertMatriceA(aaa,bbb,ii);
               vu = RandomMatriceA(aaa,bbb,ii);
               DummyResult(aaa, su : su + cu - 1) = vu;
           end
       end


        indhasil= arrayfun(@(b) (DummyResult == b).*cumsum((DummyResult == b),2),nonzeros(unique(DummyResult)), 'UniformOutput', false);  %indikator pemakaian bagian

        SumOnePerColumnIndhasil=cell(1,4);
        SumOnePerColumnIndhasil{1}=sum(indhasil{1}==1);
        SumOnePerColumnIndhasil{2}=sum(indhasil{2}==1);
        SumOnePerColumnIndhasil{3}=sum(indhasil{3}==1);
        SumOnePerColumnIndhasil{4}=sum(indhasil{4}==1);

        %to know how many column that has not yet had
        %SumOnePerColumnIndhasil= 2 and 0.
        gabbh=cell(1,4);
        gabbh{1}=sum(SumOnePerColumnIndhasil{1}~=0 &SumOnePerColumnIndhasil{1}~=2);
        gabbh{2}=sum(SumOnePerColumnIndhasil{2}~=0 &SumOnePerColumnIndhasil{2}~=2);
        gabbh{3}=sum(SumOnePerColumnIndhasil{3}~=0 &SumOnePerColumnIndhasil{3}~=2);
        gabbh{4}=sum(SumOnePerColumnIndhasil{4}~=0 &SumOnePerColumnIndhasil{4}~=2);


        total(1,1,ii)=gabbh{1}+gabbh{2}+gabbh{3}+gabbh{4};
       end      
    end 
end
4

1 回答 1

6

在我的机器上,代码非常快,因为它会立即中止并出现错误;-)

一些问题:

  1. 第 25 行:没有randint()函数(我使用的是 Matlab R2011b)。

  2. 第 30 行:这SumRandomMatriceB(:,:,ii)=sum(RandomMatriceB(:,:,C),2);不起作用,因为 size(SumRandomMatriceB(:,:,ii)) == 1x12但是 size(sum(RandomMatriceB(:,:,1),2)) == 8 x 1,所以存在尺寸不匹配。

  3. 第 58 行:没有调用函数或矩阵libur

  4. 第 89 行:ProcessMatriceA从未创建过矩阵。

  5. 第 90 行:变量bb未清除。

我只浏览了你一半的代码,就发现了五个严重错误。老实说,我没有心情为您解决这些错误。但是,当您有运行版本的代码时,我建议您输入

profile viewer

在命令行中。它将调出分析器。只需告诉它代码的文件名,它就会显示非常漂亮的时序图和统计信息。在分析之后,您可以非常具体地询问代码的哪一部分速度很慢,如果可能的话,我很乐意提供解决方法。

于 2012-08-29T09:33:43.450 回答