2016-09-08 25 views
0

遷移コスト行列のノードから他のすべてのノード(ここにある関数のフォルダ内のDPA関数http://uk.mathworks.com/matlabcentral/fileexchange/39034-finding-optimal-path-on-a-terrain)への最適なパスを見つけるスクリプトがあります。最適なパス関数の高速化

私が抱えている問題は、この機能が私の必要とするものですが、16641 x 16641のマトリックス(129 * 129 x 129 * 129)で実行する必要があるということです。私は現在それを実行していますが、予想どおりの年齢を取っています(現在はまだ2時間で稼働しています)。私は最後に機能全体を掲示するつもりですが、誰かが機能に次のような変更を加えるとかなり速く動くかどうかを明らかにすることができるのだろうかと思っていました。私は、この過剰な時間を実行するプロセスが、

for fromNode = 1 : m 
     for toNode = 1 : m 
      aij = P(toNode, fromNode); 
      dj = aij + prevStageCostMat(fromNode); 
      if dj < stageCostMat(toNode) 
       stageCostMat(toNode) = dj; 
       predMat(toNode, stage) = fromNode; 
      end   
     end % end toNode 
    end % end fromNode 

ここでm = 16641であると信じています。関数は、正しい結果を出すかどうかにかかわらず、すべてのノードを伝播しなければなりません(関数が何をしているのかを正しく理解していますか?)、あるいは計算を高速化する方法はありますか?

主な機能のページには、誰もが私に私は非常に感謝(とあなたが任意のより多くの明確化が必要な場合、私は理解して私に知らせてくださいと思います任意の洞察力を与えることができればここで

% Keep in mind that DPA propagates through all the nodes. Basically DPA 
% tries to find optimal path from one nodes to all nodes 

は、フル機能であると言います私がこれを混乱させて説明したならば)、ありがとう!

function [stageCostMat, predMat, converged] = dpa(P, startNode, endNode, maxIteration) 

% This is the function that will perform the DPA. 
% 
% Assume we have n number of nodes. P matrix is the transition cost matrix 
% with dimension of n x n(square matrix). P(toNode, fromNode) shows 
% transition cost from fromNode to toNode. 
% 
% stageCostMat shows the cost at each node for current iteration. 
% stageCostMat(c) = current stage cost matrix at node c. 
% 
% predMat shows parent/predecessor node of each node for every stage. 
% predMat(c, s): parent of node c during stage s. 
% 
% [email protected] 
% 17.11.2012 
% ------------------------------------------------------------------------- 

% Is the algortihm converged? 
converged = 0; 

% Cost matrix is a square matrix, m = n 
[m, n] = size(P); 

% Assume we will probably converge after n stages 
stageCostMat = ones(1, m) * inf; 

% Initial cost, no initial cost 
stageCostMat(startNode) = 0; 

% Predecessor matrix to trace back the optimum path, we will record parent of 
% each node on each iteration 
predMat = zeros(m, maxIteration); 

% Stage-by-stage, we move from start node to terminal node 
for stage = 2 : maxIteration  

    % Find connection from any nodes to any nodes, keep the smaller cost 
    prevStageCostMat = stageCostMat; 
    stageCostMat = ones(1, m) * inf; 

    for fromNode = 1 : m 
     for toNode = 1 : m 
      aij = P(toNode, fromNode); 
      dj = aij + prevStageCostMat(fromNode); 
      if dj < stageCostMat(toNode) 
       stageCostMat(toNode) = dj; 
       predMat(toNode, stage) = fromNode; 
      end   
     end % end toNode 
    end % end fromNode 

    % Termination 
%  if (stageCostMat == prevStageCostMat) 
%   converged = 1; 
%   break; 
%  end 

    if (predMat(endNode, stage) == endNode) || (predMat(endNode, stage-1) > 0) && (predMat(endNode, stage) == predMat(endNode, stage-1)) 
     converged = 1; 
     break; 
    end 

end 

predMat = predMat(:, 1:stage); 
+0

おそらくこれをスピードアップするにはいくつかのことがありますが、これは主にあなたのマトリックスに依存しています。ループは可能なすべての接続(1:mから1:m)をチェックしますが、これはおそらく減らすことができます。行列Pはどのくらい疎ですか? – Finn

+0

さて、Pのすべての値は対角にあるセルと対角線の上にあるセルとその直下のセルを除いて「Inf」です –

+0

素晴らしい!その場合、内側のforループでthorugh(m-1):(m + 1)からループするだけで、すべての「Inf」計算が何とか生成されないので、計算を1%未満に減らします。それをあなた自身に適応させることができますか? (P、startNode、endNode、maxIteration) – Finn

答えて

0

少し進んだ。 toNodeforNodeの現在位置に関連して、あなたはtoNodeは、行列の次元を超えてしまうケースをかわす必要があります:

for fromNode = 1 : m 
    %relativ to fromNode bc its always on the diagonal 
    for toNode = (fromNode-1) : (fromNode+1) 
     %for the first and alse fromNode toNode would be out of the array limits 
     if toNode==0 | toNode==m+1 
      continue 
     end 
     aij = P(toNode, fromNode); 
     dj = aij + prevStageCostMat(fromNode); 
     if dj < stageCostMat(toNode) 
      stageCostMat(toNode) = dj; 
      predMat(toNode, stage) = fromNode; 
     end 
    end % end toNode 
end % end fromNode 

私はあなたが古いループから解決策を持っているデータでこれを試着することをお勧めいたしますでしょうソウルションがまだ同じかどうかを確認してください。 16641回のループのうち3回だけを計算するため、時間を大幅に短縮する必要があります。

+0

このスクリプトはうまく動作しますが、タスク全体に必要な次のスクリプトが実行されたときにエラーが発生します(これについて議論するために私的な会話を開いたければわかりません) –

+0

確か。しかし、私はもう今日はここにいないだろう。だから明日はいかがですか? – Finn

+0

私はプライベートチャットを開始する方法があまりにも分かりませんが、それは私とうまく動作します! –

関連する問題