0

我现在正在尝试使用该网站的代码:

https://developers.google.com/optimization/routing/vrp

我只是做了一个小改动,当然它不再起作用了=D。

我所做的只是将数据从一个大字典(如这句话后面所示)更改为一个字典和一些列表(如代码中所示)。

  data = {}
        data['distance_matrix'] = [.....

    ]
    data['num_vehicles'] = 4
        data['depot'] = 0
        return data

当然,我将整个文档中的名称更改为新名称。

我在这里发布了所有更改的代码,因为我不太确定问题是否来自代码的不同部分。

错误是:

manager = pywrapcp.RoutingIndexManager(len(data_distance), data_num_vehicles, data_depot)

NameError:未定义名称“data_distance”

该程序现在不再识别列表和字典,我不知道为什么它根本不起作用。当我尝试在程序的任何其他部分打印列表时,python 找到它没有问题。我是个新手,非常感谢您的帮助。

问候,本

from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp



def create_data_model():
    """Stores the data for the problem."""
    data_distance = {
 #     BE   BR   DR  DÜ  HAM    MU
   #  [0]   [1]  [2]  [3] [4]  [5]  [
    "BE":[ 0, 315, 165, 477, 255, 504],
    "BR":[315,   0, 404, 248,  95, 582],
    "DR": [165, 404,   0, 485, 376, 359],
    "DÜ": [477, 248, 485,   0, 338, 486],
    "HAM":[255,  95, 376, 338,   0, 612],
    "MU":[ 506, 587, 350, 486, 612,   0]
    }
  # yapf: disable
    data_num_vehicles = 2
    data_depot = 0
    return(data_distance, data_num_vehicles, data_depot)

def print_solution(data_distance,  data_num_vehicles, data_depot, manager, routing, assignment):
    """Prints assignment on console."""
    max_route_distance = 0
    print(data_distance)
    print(data_num_vehicles)
    print(data_depot)
    for vehicle_id in range(data_num_vehicles):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += ' {} ->'.format(manager.IndexToNode(index))
            previous_index = index
            index = assignment.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
        plan_output += ' {}\n'.format(manager.IndexToNode(index))
        plan_output += 'Route distance: {}miles\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distance, max_route_distance)
    print("Maximum of the route distances: {}".format(max_route_distance))


def main():
    """Entry point of the program."""
    # Instantiate the data problem.
    data = create_data_model()

    # Create the routing index manager.
    manager = pywrapcp.RoutingIndexManager(len(data_distance), 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[from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)

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

    dimension_name = "Distance"
    routing.AddDimension(
            transit_callback_index,
            0,
            1050,
            True,
            dimension_name
            )

    # 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(data_distance, data_num_vehicles, data_depot, manager, routing, solution)


if __name__ == '__main__':
    main()
print(len(data_distance))
4

0 回答 0