Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3.  
  4. def distance(point1, point2):
  5.     """
  6.    Returns the Euclidean distance of two points in the Cartesian Plane.
  7.  
  8.    >>> distance([3,4],[0,0])
  9.    5.0
  10.    >>> distance([3,6],[10,6])
  11.    7.0
  12.    """
  13.     return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) ** 0.5
  14.  
  15.  
  16. def total_distance(points):
  17.     """
  18.    Returns the length of the path passing throught
  19.    all the points in the given order.
  20.  
  21.    >>> total_distance([[1,2],[4,6]])
  22.    5.0
  23.    >>> total_distance([[3,6],[7,6],[12,6]])
  24.    9.0
  25.    """
  26.     return sum([distance(point, points[index + 1]) for index, point in enumerate(points[:-1])])
  27.  
  28.  
  29. def travelling_salesman(points, start=None):
  30.     """
  31.    Finds the shortest route to visit all the cities by bruteforce.
  32.    Time complexity is O(N!), so never use on long lists.
  33.  
  34.    >>> travelling_salesman([[0,0],[10,0],[6,0]])
  35.    ([0, 0], [6, 0], [10, 0])
  36.    >>> travelling_salesman([[0,0],[6,0],[2,3],[3,7],[0.5,9],[3,5],[9,1]])
  37.    ([0, 0], [6, 0], [9, 1], [2, 3], [3, 5], [3, 7], [0.5, 9])
  38.    """
  39.     if start is None:
  40.         start = points[0]
  41.     return min([perm for perm in permutations(points) if perm[0] == start], key=total_distance)
  42.  
  43.  
  44. def optimized_travelling_salesman(points, start=None):
  45.     """
  46.    As solving the problem in the brute force way is too slow,
  47.    this function implements a simple heuristic: always
  48.    go to the nearest city.
  49.  
  50.    Even if this algoritmh is extremely simple, it works pretty well
  51.    giving a solution only about 25% longer than the optimal one (cit. Wikipedia),
  52.    and runs very fast in O(N^2) time complexity.
  53.  
  54.    >>> optimized_travelling_salesman([[i,j] for i in range(5) for j in range(5)])
  55.    [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 4], [1, 3], [1, 2], [1, 1], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 4], [3, 3], [3, 2], [3, 1], [3, 0], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]
  56.    >>> optimized_travelling_salesman([[0,0],[10,0],[6,0]])
  57.    [[0, 0], [6, 0], [10, 0]]
  58.    """
  59.     if start is None:
  60.         start = points[0]
  61.     must_visit = points
  62.     path = [start]
  63.     must_visit.remove(start)
  64.     while must_visit:
  65.         nearest = min(must_visit, key=lambda x: distance(path[-1], x))
  66.         path.append(nearest)
  67.         must_visit.remove(nearest)
  68.     return path
  69.  
  70.  
  71. def main():
  72.     doctest.testmod()
  73.     points = [[0, 0], [1, 5.7], [2, 3], [3, 7],
  74.               [0.5, 9], [3, 5], [9, 1], [10, 5]]
  75.     print("""The minimum distance to visit all the following points: {}
  76. starting at {} is {}.
  77.  
  78. The optimized algoritmh yields a path long {}.""".format(
  79.         tuple(points),
  80.         points[0],
  81.         total_distance(travelling_salesman(points)),
  82.         total_distance(optimized_travelling_salesman(points))))
  83.  
  84.  
  85. if __name__ == "__main__":
  86.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement