2

I am using Google OR tools to solve a simple vehicle routing problem in Python. I want to plot the solution that the solver returns in the way similar to the Google tutorial: Google OR Tools Vehicle Routing Problem Tutorial Solution

This is the code I'm using from the tutorial:

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))

and this is the solution I am getting:

Route for vehicle 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 Distance of the route: 530m

Route for vehicle 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 Distance of the route: 621m

Route for vehicle 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 Distance of the route: 614m

Route for vehicle 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 Distance of the route: 620m

How can I plot this solution similar to the image?

JT-12
  • 23
  • 6

2 Answers2

1

This image is a generated svg using a python script. You can find the source here: https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/docs/routing_svg.py

which is call by the shell script: https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/docs/generate_svg.sh

ps: Don't hesitate to ask on our discord (link in the project README.md) for further details...

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
Mizux
  • 8,222
  • 7
  • 32
  • 48
  • Thank you! But my vrp problem has no constraints except number of vehicles = 4, should I just make all the other constraints in the data model like time windows and capacity as 0? @Mizux – JT-12 Aug 13 '20 at 07:18
0

It is using the program here:

https://github.com/google/or-tools/blob/stable/examples/python/cvrptw_plot.py

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22