【发布时间】:2016-09-08 23:10:54
【问题描述】:
我有一个脚本,它可以找到从转换成本矩阵中的一个节点到所有其他节点的最佳路径(在此处找到的函数文件夹中的 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.
%
% manurung.auralius@gmail.com
% 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);
【问题讨论】:
-
可能有一些事情可以加快速度,但这主要取决于您的矩阵。循环检查每个可能的连接(从 1:m 到 1:m),这可能会减少。矩阵 P 有多稀疏?
-
好的,在 P 中,每个值都是
Inf,除了对角线上的单元格和紧邻对角线上方和下方的单元格 -
太棒了!在这种情况下,您只需要在内部 for 循环中从 (m-1):(m+1) 循环,并且您会将计算减少到不到 1%,因为所有
Inf计算无论如何都不会生成任何东西.你能适应自己吗?否则我们可以提供帮助,但我们需要以下示例数据:(P, startNode, endNode, maxIteration) -
所以实际上只是
for fromNode = 1 : mfor toNode = (m-1) : (m+1)aij = P(toNode, fromNode);dj = aij + prevStageCostMat(fromNode);if dj < stageCostMat(toNode)stageCostMat(toNode) = dj;predMat(toNode, stage) = fromNode;endend % end toNodeend % end toNodeend % end toNodeend % end toNode@987654337跨度>
标签: matlab