如何用Google OR工具绘制车辆路径问题解决方案?

2024-02-17

我正在使用 Google OR 工具来解决 Python 中的简单车辆路径问题。我想以类似于 Google 教程的方式绘制求解器返回的解决方案:Google OR Tools 车辆路径问题教程解决方案 https://i.stack.imgur.com/Ojp9R.png

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

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 脚本生成的 svg。 您可以在这里找到来源:https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/docs/routing_svg.py https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/docs/routing_svg.py

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何用Google OR工具绘制车辆路径问题解决方案? 的相关文章

随机推荐