【问题标题】:How to graph Google OR Tools Vehicle Routing Problem Solution?如何绘制谷歌或工具车辆路线问题解决方案?
【发布时间】:2020-12-02 03:29:34
【问题描述】:

我正在使用 Google OR 工具在 Python 中解决一个简单的车辆路线问题。我想以类似于 Google 教程的方式绘制求解器返回的解决方案: Google OR Tools Vehicle Routing Problem Tutorial Solution

这是我在教程中使用的代码:

def print_solution(data, manager, routing, solution):
"""Prints solution on console."""
max_route_distance = 0
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 = solution.Value(routing.NextVar(index))
        route_distance += routing.GetArcCostForVehicle(
            previous_index, index, vehicle_id)
    plan_output += '{}\n'.format(manager.IndexToNode(index))
    plan_output += 'Distance of the route: {}m\n'.format(route_distance)
    print(plan_output)
    max_route_distance = max(route_distance, max_route_distance)
print('Maximum of the route distances: {}m'.format(max_route_distance))

这是我得到的解决方案:

车辆 0 的路线: 0 -> 93 -> 92 -> 91 -> 53 -> 56 -> 52 -> 51 -> 61 -> 62 -> 63 -> 64 -> 65 -> 68 -> 67 -> 66 -> 70 - > 69 -> 100 -> 99 -> 98 -> 97 -> 96 -> 94 -> 95 -> 0 路线距离:530m

车辆 1 的路线: 0 -> 4 -> 5 -> 10 -> 9 -> 90 -> 89 -> 83 -> 82 -> 81 -> 87 -> 41 -> 44 -> 47 -> 49 -> 50 -> 14 - > 17 -> 19 -> 18 -> 20 -> 22 -> 23 -> 26 -> 31 -> 33 -> 0 路线距离:621m

车辆 2 的路线: 0 -> 1 -> 2 -> 7 -> 8 -> 86 -> 88 -> 85 -> 84 -> 59 -> 60 -> 79 -> 76 -> 77 -> 74 -> 71 -> 72 - > 73 -> 75 -> 78 -> 80 -> 58 -> 57 -> 55 -> 54 -> 0 路线距离:614m

车辆 3 的路线: 0 -> 3 -> 6 -> 43 -> 42 -> 46 -> 45 -> 48 -> 11 -> 12 -> 15 -> 13 -> 16 -> 25 -> 27 -> 29 -> 28 - > 30 -> 35 -> 36 -> 40 -> 37 -> 39 -> 38 -> 34 -> 32 -> 24 -> 21 -> 0 路线距离:620m

如何绘制与图像相似的解决方案?

【问题讨论】:

    标签: python matplotlib or-tools vehicle-routing


    【解决方案1】:

    此图像是使用 python 脚本生成的 svg。 你可以在这里找到源代码: https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/doc/routing_svg.py

    由 shell 脚本调用: https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/doc/generate_svg.sh

    ps:请不要犹豫,询问我们的不和谐(项目 README.md 中的链接)以获取更多详细信息...

    【讨论】:

    • 谢谢!但是我的 vrp 问题除了车辆数量 = 4 之外没有其他限制,我是否应该将数据模型中的所有其他限制(如时间窗口和容量)设为 0? @Mizux
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 2021-11-22
    • 2021-05-15
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多