OR-tools consistently returns very sub-optimal TSP solution

前端 未结 1 1951
花落未央
花落未央 2021-02-05 21:31

Generating some random Gaussian coordinates, I noticed the TSP-solver returns horrible solutions, however it also returns the same horrible solution over and over again for the

1条回答
  •  无人及你
    2021-02-05 22:02

    I think the problem is with the distance-measure :). I can remember a kScalingFactor in the C-code samples from or-tools, which was used to scale up distances, and then round (by casting) them to integers: or-tools expects distances to be integers.

    Or course, distances between standard Gaussian random coordinates typically lie between 0 and maybe 2, hence most point pairs have the same distances when mapped to integers: garbage in, garbage out.

    I fixed it by simply multiplying and casting to integers (just to be sure swig won't interpreter the floats as integers):

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

    Then the results make a lot more sense:

    10 points:

    20 points:

    200 points:

    0 讨论(0)
提交回复
热议问题