-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Feature RequestMissing Feature/WrapperMissing Feature/WrapperSolver: RoutingUses the Routing library and the original CP solverUses the Routing library and the original CP solver
Milestone
Description
The routing with time windows example uses a print_solution() function that calculates the Time of the route as being the earliest arrival time of the vehicle to its depot i.e. assignment.Min(time_var):
or-tools/ortools/constraint_solver/samples/vrp_time_windows.py
Lines 72 to 93 in c9b7d58
| def print_solution(data, manager, routing, assignment): | |
| """Prints assignment on console.""" | |
| time_dimension = routing.GetDimensionOrDie('Time') | |
| total_time = 0 | |
| for vehicle_id in range(data['num_vehicles']): | |
| index = routing.Start(vehicle_id) | |
| plan_output = 'Route for vehicle {}:\n'.format(vehicle_id) | |
| while not routing.IsEnd(index): | |
| time_var = time_dimension.CumulVar(index) | |
| plan_output += '{0} Time({1},{2}) -> '.format( | |
| manager.IndexToNode(index), assignment.Min(time_var), | |
| assignment.Max(time_var)) | |
| index = assignment.Value(routing.NextVar(index)) | |
| time_var = time_dimension.CumulVar(index) | |
| plan_output += '{0} Time({1},{2})\n'.format( | |
| manager.IndexToNode(index), assignment.Min(time_var), | |
| assignment.Max(time_var)) | |
| plan_output += 'Time of the route: {}min\n'.format( | |
| assignment.Min(time_var)) | |
| print(plan_output) | |
| total_time += assignment.Min(time_var) | |
| print('Total time of all routes: {}min'.format(total_time)) |
If you change the time window for the depot in that example from (0,5) to (1,5) you'll find that the Time of the route is left unchanged even though the vehicles all leave an hour later.
Shouldn't print_solution() calculate time of the route using both the route starting time and route ending time? Maybe something naive like:
def print_solution(data, manager, routing, assignment):
"""Prints assignment on console."""
time_dimension = routing.GetDimensionOrDie('Time')
total_time = 0
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
start_time = None
while not routing.IsEnd(index):
time_var = time_dimension.CumulVar(index)
plan_output += '{0} Time({1},{2}) -> '.format(
manager.IndexToNode(index), assignment.Min(time_var),
assignment.Max(time_var))
index = assignment.Value(routing.NextVar(index))
if not start_time:
start_time = assignment.Min(time_var)
time_var = time_dimension.CumulVar(index)
plan_output += '{0} Time({1},{2})\n'.format(
manager.IndexToNode(index), assignment.Min(time_var),
assignment.Max(time_var))
plan_output += 'Time of the route: {}min\n'.format(
assignment.Min(time_var)-start_time)
print(plan_output)
total_time += assignment.Min(time_var)
print('Total time of all routes: {}min'.format(total_time))This also corrects the time of the route for cases where a vehicle is not used, e.g.:
Route for vehicle 15:
0 Time(1,1) -> 0 Time(1,1)
Time of the route: 0min
Metadata
Metadata
Assignees
Labels
Feature RequestMissing Feature/WrapperMissing Feature/WrapperSolver: RoutingUses the Routing library and the original CP solverUses the Routing library and the original CP solver