或者工具旅行推销员Python算法没有给出正确组织的路线

或者工具旅行推销员Python算法没有给出正确组织的路线,python,Python,尝试测试示例中的代码,使用单个车辆在2D数组中采用预先制作的距离矩阵,而不是创建字典。预先制作的距离矩阵将点排列在一个大圆中,试图“强制”它生成一条通过点的单圈路径,然后扰乱阵列内节点的“顺序”(保持节点之间的距离正确) 然而,工具输出只是以相反的顺序遍历数组,没有任何形式的组织或优化。除了更改以防止错误,以及使用2D矩阵而不是字典之外,我看不到有任何更改破坏了此设置 from ortools.constraint_solver import routing_enums_pb2 from ort

尝试测试示例中的代码,使用单个车辆在2D数组中采用预先制作的距离矩阵,而不是创建字典。预先制作的距离矩阵将点排列在一个大圆中,试图“强制”它生成一条通过点的单圈路径,然后扰乱阵列内节点的“顺序”(保持节点之间的距离正确)

然而,工具输出只是以相反的顺序遍历数组,没有任何形式的组织或优化。除了更改以防止错误,以及使用2D矩阵而不是字典之外,我看不到有任何更改破坏了此设置

from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp



def print_solution(manager, routing, solution):
    """Prints solution on console."""
    index = routing.Start(0)
    plan_output = 'Route for vehicle:\n'
    route_distance = 0
    while not routing.IsEnd(index):
        plan_output += ' {'+str((manager.IndexToNode(index)))+'} ->'
        previous_index = index
        index = solution.Value(routing.NextVar(index))
        route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
    plan_output += " End."
    print(plan_output)


def TSP_algorithm(data):
    """Entry point of the program."""

    # Create the routing index manager.
    manager = pywrapcp.RoutingIndexManager(len(data),
                                           1, 0)

    # 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[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.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

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

    # Print solution on console.
    if solution:
        print_solution(manager, routing, solution)

    return manager, routing, solution
对于大小为40的数组,输出仅为“0->40->39->38->…”

不确定是否遗漏了某些内容,或者是否未正确导入数据,或者
distance\u callback
中的数据是否只能是特定的数据类型