Skip to content

print_solution() "time of the route" does not take into account route starting time #1179

@ConstantinoSchillebeeckxSimpleRose

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

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/WrapperSolver: RoutingUses the Routing library and the original CP solver

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions