1. 辅助函数
Node算子用来存储搜索树的状态。其中level等于path的长度,path是当前节点已经访问过的vertex清单,bound则是当前的lb。
这里的bound函数是一种启发式方法,等于当前路径的总长度,再加上往后走两步的最小值。
struct Node
level::Int
path::Vector{Int64}
bound::Int
end
function totaldist(adj_mat::Array{Int64,2},t::Vector{Int64} )
n = length(t)
sum([adj_mat[t[i],t[i+1]] for i in 1:n-1])+adj_mat[t[n],t[1]]
end
function bound(adj_mat::Array{Int64,2}, path::Vector{Int64} )
_bound = 0
n = size(adj_mat)[1]
determined, last = path[1:end-1], path[end]
remain = setdiff(1:n,path)
for i in 1:length(path)-1;_bound += adj_mat[path[i],path[i + 1]];end
_bound += minimum([adj_mat[last,i] for i in remain])
p = [path[1];remain]
for r in remain
_bound+=minimum([adj_mat[r,i] for i in setdiff(p,r)])
end
return _bound
end;
2. 分枝定界代码
这里用priorityQueue存储节点,用Queue也是一样的。
分枝条件为bound<ub,往下搜索所有没有探访过的节点,使用函数setdiff(1:n,v.path)。当然这

文章介绍了利用分枝定界法求解旅行商问题(TSP)的算法,包括辅助函数如总距离计算和边界函数,以及核心的分枝定界代码。算法通过优先级队列存储节点,并基于启发式边界函数进行剪枝。在达到一定深度或找到更优解时更新上界,并使用最小生成树优化下界。随着点的数量增加,算法性能会下降,因为TSP是一个NP-hard问题。
最低0.47元/天 解锁文章
660

被折叠的 条评论
为什么被折叠?



