OR 工具始终 returns 非常次优的 TSP 解决方案

OR-tools consistently returns very sub-optimal TSP solution

生成一些随机高斯坐标,我注意到 TSP 求解器 return 的解决方案很糟糕,但是对于相同的输入它也 return 一遍又一遍地使用相同的可怕解决方案。

鉴于此代码:

import numpy
import math
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2

import matplotlib
%matplotlib inline
from matplotlib import pyplot, pylab
pylab.rcParams['figure.figsize'] = 20, 10


n_points = 200

orders = numpy.random.randn(n_points, 2)
coordinates = orders.tolist()

class Distance:
    def __init__(self, coords):
        self.coords = coords

    def distance(self, x, y):
        return math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)

    def __call__(self, x, y):
        return self.distance(self.coords[x], self.coords[y])

distance = Distance(coordinates)

search_parameters = pywrapcp.RoutingModel.DefaultSearchParameters()
search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.LOCAL_CHEAPEST_ARC)

search_parameters.local_search_metaheuristic = routing_enums_pb2.LocalSearchMetaheuristic.TABU_SEARCH


routing = pywrapcp.RoutingModel(len(coordinates), 1)
routing.SetArcCostEvaluatorOfAllVehicles(distance)   

routing.SetDepot(0)
solver = routing.solver()
routing.CloseModel() # the documentation is a bit unclear on whether this is needed

assignment = routing.SolveWithParameters(search_parameters)

nodes = []
index = routing.Start(0)
while not routing.IsEnd(index):
    nodes.append(routing.IndexToNode(index))
    index = assignment.Value(routing.NextVar(index))

nodes.append(0)
for (a, b) in zip(nodes, nodes[1:]):
    a, b = coordinates[a], coordinates[b]
    pyplot.plot([a[0], b[0]], [a[1], b[1]], 'r' )

例如,10分我得到一个很好的解决方案:

对于20更糟糕的是,一些明显的优化仍然存在(其中一个只需要交换两个点。

对于 200 人来说,这太可怕了:

我想知道上面的代码是否真的做了一些 LNS,或者只是 return 的初始值,特别是因为大多数 first_solution_strategy 选项建议确定性初始化。

为什么上面的 TSP 求解器 return 对相同的数据有一致的解决方案,即使禁忌搜索和模拟退火等是随机的。还有,为什么200分的方案这么差?

我在 SearchParameters 中尝试了几个选项,尤其是在 local_search_operators 中启用 'use_...' 字段。这没有效果,同样非常次优的解决方案被 returned.

我认为问题在于距离测量:)。我记得 or-tools 的 C 代码示例中的 kScalingFactor,它用于放大距离,然后将它们四舍五入(通过转换)为整数:or-tools 期望距离为整数。

当然,标准高斯随机坐标之间的距离通常介于 0 和 2 之间,因此大多数点对在映射到整数时具有相同的距离:垃圾输入,垃圾输出。

我通过简单地乘以并转换为整数来修复它(只是为了确保 swig 不会将浮点数解释为整数):

# ...
def distance(self, x, y):
    return int(10000 * math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2))
# ...

那么结果就更有意义了:

10 点:

20分:

200 点: