How do you write an objective function for a shortest path problem?

3 ビュー (過去 30 日間)
Xi Wen Lim
Xi Wen Lim 2016 年 3 月 28 日
回答済み: Walter Roberson 2016 年 3 月 28 日
Can anyone help me with this please? Not sure how to write one.
I need to write a function sort of like this:
function z=fobj(u)
%%d-dimensional sphere function sum_j=1^d (u_j-1)^2.
% with a minimum at (1,1, ...., 1);
z=sum((u-1).^2);
end
But I need one to solve a shortest path problem. I have 49 nodes and I want to minimize the distance taken to travel between 2 selected nodes. Can someone help me please? I'd really really appreciate it.

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 3 月 28 日
Your inputs to the objective function should be indices. Your objective function should use the indices to index the distance table.
sum( distances( sub2ind( size(distances), x(1:end-1), x(2:end) ) )

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by