据我所知,3-Opt Heuristic涉及从图表中删除三条边,并再添加三条边以重新完成游览。然而,我已经看过许多论文提到当三个边缘被移除时,只有两种可能的方法来重新组合巡回演出 - 这对我来说没有意义。
例如,this论文说:
3-opt算法以类似的方式工作,但我们删除了三个,而不是删除两个边。这意味着我们有两种方法可以将三个路径重新连接到有效的tour1中。 3-opt移动实际上可以被视为两次或三次2-opt移动。
但是,我计算了8种不同的方式来重新连接游览(如果在删除边缘之前不计算顺序,则为7)。我在这里错过了什么?
另外,如果可能,有人可以将我链接到3-opt算法吗?我只是想更好地理解它,但我还没有遇到任何问题。我找到的所有资源只是说“删除三条边,重新连接它们”。就是这样。
答案 0 :(得分:3)
我同意脚注将其减少到四种方式,而不是两种方式。
在下面的代码中,您可以传入排列p
,它将执行3-opt,这是四种可能之一。如果您也通过了broad=False
,它将允许任何8个(包括身份)。
但是,我非常欢迎对此代码进行审核,或者在线提供任何其他实施的链接。
请注意,此代码并未强制我们剪切的边缘最初是非连续的。应该吗?
另请注意,这只是移动,它没有执行尝试所有移动和选择最佳移动的完整例程。
def three_opt(p, broad=False):
"""In the broad sense, 3-opt means choosing any three edges ab, cd
and ef and chopping them, and then reconnecting (such that the
result is still a complete tour). There are eight ways of doing
it. One is the identity, 3 are 2-opt moves (because either ab, cd,
or ef is reconnected), and 4 are 3-opt moves (in the narrower
sense)."""
n = len(p)
# choose 3 unique edges defined by their first node
a, c, e = random.sample(range(n+1), 3)
# without loss of generality, sort
a, c, e = sorted([a, c, e])
b, d, f = a+1, c+1, e+1
if broad == True:
which = random.randint(0, 7) # allow any of the 8
else:
which = random.choice([3, 4, 5, 6]) # allow only strict 3-opt
# in the following slices, the nodes abcdef are referred to by
# name. x:y:-1 means step backwards. anything like c+1 or d-1
# refers to c or d, but to include the item itself, we use the +1
# or -1 in the slice
if which == 0:
sol = p[:a+1] + p[b:c+1] + p[d:e+1] + p[f:] # identity
elif which == 1:
sol = p[:a+1] + p[b:c+1] + p[e:d-1:-1] + p[f:] # 2-opt
elif which == 2:
sol = p[:a+1] + p[c:b-1:-1] + p[d:e+1] + p[f:] # 2-opt
elif which == 3:
sol = p[:a+1] + p[c:b-1:-1] + p[e:d-1:-1] + p[f:] # 3-opt
elif which == 4:
sol = p[:a+1] + p[d:e+1] + p[b:c+1] + p[f:] # 3-opt
elif which == 5:
sol = p[:a+1] + p[d:e+1] + p[c:b-1:-1] + p[f:] # 3-opt
elif which == 6:
sol = p[:a+1] + p[e:d-1:-1] + p[b:c+1] + p[f:] # 3-opt
elif which == 7:
sol = p[:a+1] + p[e:d-1:-1] + p[c:b-1:-1] + p[f:] # 2-opt
return sol
答案 1 :(得分:0)
在页面底部,它阐明了它没有计算连接与单个2-OPT移动相同。 如果我没有错,那就用四种方式减少它。其中三个看起来像图4旋转,但我不会说它们是相同的,一般来说。