微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以使用带有部分距离矩阵的 TSP 的 OR 工具对于大量节点吗?

如何解决我可以使用带有部分距离矩阵的 TSP 的 OR 工具对于大量节点吗?

我正在尝试使用 OR-tools 解决 tsp 之类的问题,例如 80,000 个节点,问题是,我需要一个占用大量内存的巨大距离矩阵,因此它不可行,我没有得到解决方案.

所以:

  1. 是否可以选择在 or-tools 中使用部分距离矩阵?
  2. 如果没有,有没有办法改进我的代码
  3. 是否有另一个外部求解器可以在 Python 中完成此任务?
    import math
    from collections import namedtuple
    import random
    import time
    from collections import namedtuple
    from sklearn.metrics.pairwise import euclidean_distances
    import numpy as np
    import numba
    from scipy.spatial import distance_matrix
    from sklearn.metrics.pairwise import euclidean_distances
    from math import sqrt


    Point = namedtuple("Point",['x','y'])

    def solve_it(input_data):
       # Modify this code to run your optimization algorithm
       global POINTS
       # parse the input
       lines = input_data.split('\n')

    nodeCount = int(lines[0])

    points = []
    for i in range(1,nodeCount+1):
        line = lines[i]
        parts = line.split()
        points.append(Point(float(parts[0]),float(parts[1])))

   

    #2.routing with or tools

    def dist_matrix(nodeCount,points):
        data=[]
        for k in range(len(points)):
            data.append([int(points[k].x),int(points[k].y)])
        
        D=euclidean_distances(data,data)
        return D


    def create_data_model(D):
        """Stores the data for the problem."""
        data = {}
        data['distance_matrix'] = D # yapf: disable
        data['num_vehicles'] = 1
        data['depot'] = 0
        return data

    def print_solution(manager,routing,solution):
        index = routing.Start(0)
        plan_output = []#Route for vehicle 0:\n'
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output.append(manager.IndexToNode(index))
            index = solution.Value(routing.Nextvar(index))
        return plan_output

    def or_main(nodeCount,points):
        from ortools.constraint_solver import routing_enums_pb2
        from ortools.constraint_solver import pywrapcp

        """Entry point of the program."""
        # Instantiate the data problem.
        global sol
        D=dist_matrix(nodeCount,points)
        data = create_data_model(D)

        # Create the routing index manager.
        manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),data['num_vehicles'],data['depot'])

        # Create Routing Model.
        routing = pywrapcp.RoutingModel(manager)

        def distance_callback(from_index,to_index):
            """Returns the distance between the two nodes."""
            # Convert from routing variable Index to distance matrix NodeIndex.
            from_node = manager.IndexToNode(from_index)
            to_node = manager.IndexToNode(to_index)
            return data['distance_matrix'][from_node][to_node]

        transit_callback_index = routing.RegisterTransitCallback(distance_callback)

        # Define cost of each arc.
        routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

        # Setting first solution heuristic.
        search_parameters = pywrapcp.DefaultRoutingSearchParameters()
        search_parameters.local_search_Metaheuristic = (
            routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
        k = 100
        if nodeCount <= 100:
            k = 30
        elif 100 <= nodeCount <= 1000:
            k = 300
        elif nodeCount > 1000:
            k = 17000
        search_parameters.time_limit.seconds =k
        search_parameters.log_search = True


        # Solve the problem.
        solution = routing.solveWithParameters(search_parameters)

        # #print solution on console.
        if solution:
            sol=print_solution(manager,solution)
        return sol

    
    ######################################################################
    
    solution=or_main(nodeCount,points)
    

    # calculate the length of the tour
    obj = length(points[solution[-1]],points[solution[0]])
    for index in range(0,nodeCount-1):
        obj += length(points[solution[index]],points[solution[index+1]])

    # prepare the solution in the specified output format
    output_data = '%.2f' % obj + ' ' + str(0) + '\n'
    output_data += ' '.join(map(str,solution))

    return output_data




    if __name__ == '__main__':
      import sys
      if len(sys.argv) > 1:
          file_location = sys.argv[1].strip()
          with open(file_location,'r') as input_data_file:
            input_data = input_data_file.read()
        #print(solve_it(input_data))
      else:
          print('This test requires an input file.  Please select one from the data directory. (i.e. python solver.py ./data/tsp_51_1)')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。