Help with CVRP Reload #2442
-
ortools-discuss: https://groups.google.com/g/or-tools-discuss/c/-0uZTwB1S9M/m/ol3AQcUmAAAJ I'm trying to use OR-Tools' Routing Solver to solve a Multi Trip Capacitated VRP. What I need is
So the vehicles should pick up the goods from each node until the capacity is filled. Then go to "unloading location", unload all their weight and keep collecting the demand from nodes until a time limit is reached or all the goods are collected. Then return back to the depot. CVRP Reload Example seems very close but in my case, at the end of the route vehicles should visit the unloading location before the depot. In other words a vehicle can not go to the depot (starting, ending location) with load. Example:
I'm fairly new to or-tools, and trying to figure it out. Can you help me if you have any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
This is not an issue. Moving to discussions. |
Beta Was this translation helpful? Give feedback.
-
So, I thought I can come with a count_dimension to set a preceding constraint (unload before the last depot). Yet, I wasn't able to implement the idea. So, the count_dimension function is commented out and I tried to add new dummy locations: _locations = [
(4, 4), # depot 0
(4, 4), # depot 1
(4, 4), # depot 2
(4, 4), # depot 3
(4, 4), # depot 4
(4, 4), # depot 5
(2, 3), # unload depot_prime
(2, 3), # unload depot_second
(2, 3), # unload depot_fourth
(2, 3), # unload depot_fourth
(2, 3), # unload depot_fifth
....] I tried to end the routes at 3, 4, 5 which are just dummy depots.
This one returns an error: Process finished with exit code -1073741819 (0xC0000005) It works fine when I set the vehicles start from 0, 1, 2 and end at 0, 1, 2. Returns a result at least. Demands of these nodes (depots and unload depots) are: _demand = [0, 0, 0, 0, 0, 0,
-_capacity, #6
-_capacity, #7
-_capacity, #8
-_capacity, #9
-_capacity, #10,
...] Here is the whole code, it's basically cvrp_reload I'm trying to make modifications: from functools import partial
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
pickup_location_index = 11
###########################
# Problem Data Definition #
###########################
def create_data_model():
"""Stores the data for the problem"""
data = {}
_capacity = 15
# Locations in block unit
_locations = [
(4, 4), # depot
(4, 4), # depot
(4, 4), # depot
(4, 4), # depot
(4, 4), # depot
(4, 4), # depot
(2, 3), # unload depot_prime
(2, 3), # unload depot_second
(2, 3), # unload depot_fourth
(2, 3), # unload depot_fourth
(2, 3), # unload depot_fifth
(2, 0),
(8, 0), # locations to visit
(0, 1),
(1, 1),
(5, 2),
(7, 2),
(3, 3),
(6, 3),
(5, 5),
(8, 5),
(1, 6),
(2, 6),
(3, 7),
(6, 7),
(0, 8),
(7, 8)
]
# Compute locations in meters using the block dimension defined as follow
# Manhattan average block: 750ft x 264ft -> 228m x 80m
# here we use: 114m x 80m city block
# src: https://nyti.ms/2GDoRIe 'NY Times: Know Your distance'
data['locations'] = [(l[0] * 114, l[1] * 80) for l in _locations]
data['num_locations'] = len(data['locations'])
data['demands'] = \
[0,
0, 0, 0, 0, 0,
-_capacity, #6
-_capacity, #7
-_capacity, #8
-_capacity, #9
-_capacity, #10
3, 3, # 1, 2
3, 4, # 3, 4
3, 4, # 5, 6
8, 8, # 7, 8
3, 3, # 9,10
3, 3, # 11,12
4, 4, # 13, 14
8, 8] # 15, 16
data['time_per_demand_unit'] = 5 # 5 minutes/unit
data['time_windows'] = \
[(0, 0), # depot
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(0, 1000),
(75, 850), (75, 850), # 1, 2
(60, 700), (45, 550), # 3, 4
(0, 800), (50, 600), # 5, 6
(0, 1000), (10, 200), # 7, 8
(0, 1000), (75, 850), # 9, 10
(85, 950), (5, 150), # 11, 12
(15, 250), (10, 200), # 13, 14
(45, 550), (30, 400)] # 15, 16
data['num_vehicles'] = 3
data['vehicle_capacity'] = _capacity
data['vehicle_max_distance'] = 10_000
data['vehicle_max_time'] = 1_500
data[
'vehicle_speed'] = 5 * 60 / 3.6 # Travel speed: 5km/h to convert in m/min
data['depot'] = 0
return data
#######################
# Problem Constraints #
#######################
def manhattan_distance(position_1, position_2):
"""Computes the Manhattan distance between two points"""
return (abs(position_1[0] - position_2[0]) +
abs(position_1[1] - position_2[1]))
def create_distance_evaluator(data):
"""Creates callback to return distance between points."""
_distances = {}
# precompute distance between location to have distance callback in O(1)
for from_node in range(data['num_locations']):
_distances[from_node] = {}
for to_node in range(data['num_locations']):
if from_node == to_node:
_distances[from_node][to_node] = 0
# Forbid start/end/reload node to be consecutive.
elif from_node in range(6) and to_node in range(6):
_distances[from_node][to_node] = data['vehicle_max_distance']
else:
_distances[from_node][to_node] = (manhattan_distance(
data['locations'][from_node], data['locations'][to_node]))
def distance_evaluator(manager, from_node, to_node):
"""Returns the manhattan distance between the two nodes"""
return _distances[manager.IndexToNode(from_node)][manager.IndexToNode(
to_node)]
return distance_evaluator
def add_count_dimension(routing, manager, data):
count_dimension_name = 'Count'
routing.AddConstantDimensionWithSlack(1, 10**8, 10**8, True, count_dimension_name)
count_dimension = routing.GetDimensionOrDie(count_dimension_name)
A_index = manager.NodeToIndex(0)
B_index = manager.NodeToIndex(1)
C_index = manager.NodeToIndex(2)
A1_index = manager.NodeToIndex(6)
A2_index = manager.NodeToIndex(7)
A3_index = manager.NodeToIndex(8)
routing.solver().Add(routing.VehicleVar(A_index) == routing.VehicleVar(B_index))
routing.solver().Add(count_dimension.CumulVar(A_index) <= count_dimension.CumulVar(B_index))
# routing.solver().Add(routing.VehicleVar(1) == routing.VehicleVar(11))
# routing.solver().Add(routing.VehicleVar(17) == routing.VehicleVar(18))
def add_distance_dimension(routing, manager, data, distance_evaluator_index):
"""Add Global Span constraint"""
distance = 'Distance'
routing.AddDimension(
distance_evaluator_index,
0, # null slack
data['vehicle_max_distance'], # maximum distance per vehicle
True, # start cumul to zero
distance)
distance_dimension = routing.GetDimensionOrDie(distance)
# Try to minimize the max distance among vehicles.
# /!\ It doesn't mean the standard deviation is minimized
distance_dimension.SetGlobalSpanCostCoefficient(100)
def create_demand_evaluator(data):
"""Creates callback to get demands at each location."""
_demands = data['demands']
def demand_evaluator(manager, from_node):
"""Returns the demand of the current node"""
return _demands[manager.IndexToNode(from_node)]
return demand_evaluator
def add_capacity_constraints(routing, manager, data, demand_evaluator_index):
"""Adds capacity constraint"""
vehicle_capacity = data['vehicle_capacity']
capacity = 'Capacity'
routing.AddDimension(
demand_evaluator_index,
vehicle_capacity,
vehicle_capacity,
True, # start cumul to zero
capacity)
# Add Slack for reseting to zero unload depot nodes.
# e.g. vehicle with load 10/15 arrives at node 1 (depot unload)
# so we have CumulVar = 10(current load) + -15(unload) + 5(slack) = 0.
capacity_dimension = routing.GetDimensionOrDie(capacity)
# Allow to drop reloading nodes with zero cost.
for node_index in range(pickup_location_index):
index = manager.NodeToIndex(node_index)
routing.AddDisjunction([node_index], 0)
# Allow to drop regular node with a cost.
for node_index in range(pickup_location_index, len(data['demands'])):
index = manager.NodeToIndex(node_index)
capacity_dimension.SlackVar(index).SetValue(0)
routing.AddDisjunction([node_index], 100_000)
def create_time_evaluator(data):
"""Creates callback to get total times between locations."""
def service_time(data, node):
"""Gets the service time for the specified location."""
return abs(data['demands'][node]) * data['time_per_demand_unit']
def travel_time(data, from_node, to_node):
"""Gets the travel times between two locations."""
if from_node == to_node:
travel_time = 0
else:
travel_time = manhattan_distance(
data['locations'][from_node], data['locations'][to_node]) / data['vehicle_speed']
return travel_time
_total_time = {}
# precompute total time to have time callback in O(1)
for from_node in range(data['num_locations']):
_total_time[from_node] = {}
for to_node in range(data['num_locations']):
if from_node == to_node:
_total_time[from_node][to_node] = 0
else:
_total_time[from_node][to_node] = int(
service_time(data, from_node) + travel_time(
data, from_node, to_node))
def time_evaluator(manager, from_node, to_node):
"""Returns the total time between the two nodes"""
return _total_time[manager.IndexToNode(from_node)][manager.IndexToNode(
to_node)]
return time_evaluator
def add_time_window_constraints(routing, manager, data, time_evaluator):
"""Add Time windows constraint"""
time = 'Time'
max_time = data['vehicle_max_time']
routing.AddDimension(
time_evaluator,
max_time, # allow waiting time
max_time, # maximum time per vehicle
False, # don't force start cumul to zero since we are giving TW to start nodes
time)
time_dimension = routing.GetDimensionOrDie(time)
# Add time window constraints for each location except depot
# and 'copy' the slack var in the solution object (aka Assignment) to print it
for location_idx, time_window in enumerate(data['time_windows']):
if location_idx == 0:
continue
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
routing.AddToAssignment(time_dimension.SlackVar(index))
# Add time window constraints for each vehicle start node
# and 'copy' the slack var in the solution object (aka Assignment) to print it
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(data['time_windows'][0][0],
data['time_windows'][0][1])
routing.AddToAssignment(time_dimension.SlackVar(index))
# Warning: Slack var is not defined for vehicle's end node
#routing.AddToAssignment(time_dimension.SlackVar(self.routing.End(vehicle_id)))
###########
# Printer #
###########
def print_solution(data, manager, routing, assignment): # pylint:disable=too-many-locals
"""Prints assignment on console"""
print(f'Objective: {assignment.ObjectiveValue()}')
total_distance = 0
total_load = 0
total_time = 0
capacity_dimension = routing.GetDimensionOrDie('Capacity')
time_dimension = routing.GetDimensionOrDie('Time')
dropped = []
for node in range(pickup_location_index, routing.nodes()):
index = manager.NodeToIndex(node)
if assignment.Value(routing.NextVar(index)) == index:
dropped.append(node)
print(f'dropped orders: {dropped}')
dropped = []
for reload in range(1, pickup_location_index):
index = manager.NodeToIndex(reload)
if assignment.Value(routing.NextVar(index)) == index:
dropped.append(reload)
print(f'dropped reload stations: {dropped}')
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = f'Route for vehicle {vehicle_id}:\n'
distance = 0
while not routing.IsEnd(index):
load_var = capacity_dimension.CumulVar(index)
time_var = time_dimension.CumulVar(index)
plan_output += ' {0} Load({1}) Time({2},{3}) ->'.format(
manager.IndexToNode(index),
assignment.Value(load_var),
assignment.Min(time_var), assignment.Max(time_var))
previous_index = index
index = assignment.Value(routing.NextVar(index))
distance += routing.GetArcCostForVehicle(previous_index, index,
vehicle_id)
load_var = capacity_dimension.CumulVar(index)
time_var = time_dimension.CumulVar(index)
plan_output += ' {0} Load({1}) Time({2},{3})\n'.format(
manager.IndexToNode(index),
assignment.Value(load_var),
assignment.Min(time_var), assignment.Max(time_var))
plan_output += f'Distance of the route: {distance}m\n'
plan_output += f'Load of the route: {assignment.Value(load_var)}\n'
plan_output += f'Time of the route: {assignment.Value(time_var)}min\n'
print(plan_output)
total_distance += distance
total_load += assignment.Value(load_var)
total_time += assignment.Value(time_var)
print('Total Distance of all routes: {}m'.format(total_distance))
print('Total Load of all routes: {}'.format(total_load))
print('Total Time of all routes: {}min'.format(total_time))
# for i in range(data['num_locations']):
# print(f'Node {i}:')
# print(routing.VehicleVar(i))
# print('------')
########
# Main #
########
def main():
"""Entry point of the program"""
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
data['num_vehicles'], [0, 1, 2], [0, 1, 2])
# Create Routing Model
routing = pywrapcp.RoutingModel(manager)
# Define weight of each edge
distance_evaluator_index = routing.RegisterTransitCallback(
partial(create_distance_evaluator(data), manager))
routing.SetArcCostEvaluatorOfAllVehicles(distance_evaluator_index)
# Add Distance constraint to minimize the longuest route
add_distance_dimension(routing, manager, data, distance_evaluator_index)
# Add Capacity constraint
demand_evaluator_index = routing.RegisterUnaryTransitCallback(
partial(create_demand_evaluator(data), manager))
add_capacity_constraints(routing, manager, data, demand_evaluator_index)
# add_count_dimension(routing, manager, data)
# Add Time Window constraint
time_evaluator_index = routing.RegisterTransitCallback(
partial(create_time_evaluator(data), manager))
add_time_window_constraints(routing, manager, data, time_evaluator_index)
# Setting first solution heuristic (cheapest addition).
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) # pylint: disable=no-member
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
search_parameters.time_limit.FromSeconds(3)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
if solution:
print_solution(data, manager, routing, solution)
else:
print("No solution found !")
if __name__ == '__main__':
main() This is the result: Objective: 317544
dropped orders: []
dropped reload stations: [3, 4, 5]
Route for vehicle 0:
0 Load(0) Time(0,0) -> 19 Load(0) Time(2,10) -> 18 Load(3) Time(20,28) -> 9 Load(11) Time(65,73) -> 22 Load(0) Time(142,150) -> 21 Load(3) Time(158,513) -> 13 Load(6) Time(179,534) -> 14 Load(9) Time(195,550) -> 10 Load(13) Time(218,774) -> 6 Load(0) Time(293,849) -> 7 Load(0) Time(368,924) -> 17 Load(0) Time(444,1000) -> 0 Load(8) Time(486,1500)
Distance of the route: 2488m
Load of the route: 8
Time of the route: 486min
Route for vehicle 1:
1 Load(0) Time(0,0) -> 24 Load(0) Time(10,200) -> 26 Load(4) Time(32,400) -> 20 Load(12) Time(76,850) -> 1 Load(15) Time(97,1500)
Distance of the route: 1552m
Load of the route: 15
Time of the route: 97min
Route for vehicle 2:
2 Load(0) Time(0,0) -> 15 Load(0) Time(3,43) -> 16 Load(3) Time(50,60) -> 12 Load(7) Time(75,83) -> 11 Load(10) Time(98,106) -> 8 Load(13) Time(115,123) -> 25 Load(0) Time(197,205) -> 23 Load(8) Time(242,250) -> 2 Load(12) Time(266,1500)
Distance of the route: 3104m
Load of the route: 12
Time of the route: 266min
Total Distance of all routes: 7144m
Total Load of all routes: 35
Total Time of all routes: 849min |
Beta Was this translation helpful? Give feedback.
-
Simply add # Vehicles must be empty upon arrival
capacity_dimension = routing.GetDimensionOrDie("Capacity")
for v in range(manager.GetNumberOfVehicles()):
print(f"vehicle {v}")
end = routing.End(v)
#routing.solver().Add(capacity_dimension.CumulVar(end) == 0) # see comment below
capacity_dimension.SetCumulVarSoftUpperBound(end, 0, 100_000) possible output: ./2442_unload.py
...
I0417 23:53:02.181640 17437 search.cc:260] Solution #317 (372696, objective maximum = 4838552, time = 2969 ms, branches = 3223, failures = 940, depth = 33, MakeInactiveOperator, neighbors = 265820, filtered neighbors = 317, accepted neighbors = 317, memory used = 14.93 MB, limit = 99%)
I0417 23:53:20.290527 17437 search.cc:260] Solution #318 (469816, objective minimum = 372696, objective maximum = 4838552, time = 2987 ms, branches = 3239, failures = 945, depth = 33, MakeInactiveOperator, neighbors = 267395, filtered neighbors = 318, accepted neighbors = 318, memory used = 14.93 MB, limit = 99%)
I0417 23:53:21.045410 17437 search.cc:260] Solution #319 (469816, objective minimum = 372696, objective maximum = 4838552, time = 2988 ms, branches = 3247, failures = 947, depth = 33, MakeActiveOperator, neighbors = 267415, filtered neighbors = 319, accepted neighbors = 319, memory used = 14.93 MB, limit = 99%)
I0417 23:53:22.304931 17437 search.cc:260] Solution #320 (372696, objective maximum = 4838552, time = 2989 ms, branches = 3253, failures = 949, depth = 33, MakeActiveOperator, neighbors = 267464, filtered neighbors = 320, accepted neighbors = 320, memory used = 14.93 MB, limit = 99%)
I0417 23:53:30.987548 17437 search.cc:260] Finished search tree (time = 2998 ms, branches = 3259, failures = 982, neighbors = 268318, filtered neighbors = 320, accepted neigbors = 320, memory used = 14.93 MB)
I0417 23:53:31.046630 17437 search.cc:260] End search (time = 2998 ms, branches = 3259, failures = 982, memory used = 14.93 MB, speed = 1087 branches/s)
Objective: 372696
dropped orders: [25]
dropped reload stations: [3, 5]
Route for vehicle 0:
0 Load(0) Time(0,0) -> 20 Load(0) Time(75,506) -> 12 Load(3) Time(94,525) -> 14 Load(6) Time(119,550) -> 13 Load(10) Time(140,700) -> 8 Load(13) Time(159,1000) -> 0 Load(0) Time(237,1500)
Distance of the route: 2624m
Load of the route: 0
Time of the route: 237min
Route for vehicle 1:
1 Load(0) Time(0,0) -> 19 Load(0) Time(2,182) -> 24 Load(3) Time(20,200) -> 26 Load(7) Time(42,400) -> 4 Load(15) Time(89,770) -> 7 Load(15) Time(92,773) -> 11 Load(0) Time(169,850) -> 17 Load(3) Time(188,959) -> 10 Load(11) Time(229,1000) -> 1 Load(0) Time(307,1500)
Distance of the route: 2648m
Load of the route: 0
Time of the route: 307min
Route for vehicle 2:
2 Load(0) Time(0,0) -> 23 Load(0) Time(15,63) -> 22 Load(4) Time(37,85) -> 21 Load(7) Time(85,101) -> 9 Load(10) Time(104,120) -> 18 Load(0) Time(184,200) -> 16 Load(8) Time(226,600) -> 15 Load(12) Time(248,800) -> 6 Load(15) Time(268,1000) -> 2 Load(0) Time(346,1500)
Distance of the route: 2624m
Load of the route: 0
Time of the route: 346min
Total Distance of all routes: 7896m
Total Load of all routes: 0
Total Time of all routes: 890min note: I use the Soft constraint since otherwise the solver prefer to drop all nodes and never manage to escape from this solution search space point. |
Beta Was this translation helpful? Give feedback.
Simply add
possible output:
./2442_unload.py ... I0417 23:53:02.181640 17437 search.cc:260] Solution #317 (372696, objective maximum = 4838552, time = 2969 ms, branches = 3223, failures = 940, depth = 33, MakeInactiveOperator, neighbors = 265820, filtered neighbors = 317, accepted neighbors = 317, memory used = 14.93 MB, …