I’m trying to optimize the best routes VRP with OR-Tools. I couldn’t find the right function in the documentation.
CASE: Some customers only accept pickup trucks, some accept only trucks, some accept both trucks, pickup, and vans. There’s a single depot location and vehicles should carry orders to the right customers with accepted vehicles.
Those vehicles I have
Customers accept those vehicle types
These vehicles should be directed to the appropriate customers.
Do you have any thoughts or are there any or-tools function regarding this?
Advertisement
Answer
You can use the RoutingModel::VehicleVar(index)
Pseudo code in Python (using customer_id as node_id)
JavaScript
x
25
25
1
# Vehicles list
2
trucks = [1, 3, 6, 7, 9, 10]
3
vans = [4, 5]
4
pickups = [2, 8]
5
6
# location list with a tuple (location, truck, van pickup)
7
locations = [
8
(1, True, True, True), # C-01
9
(2, True, True, False), # C-02
10
(3, True, False, False), # C-03
11
(4, True, True, True), # C-04
12
13
]
14
15
for location, truck_allowed, van_allowed, pickup_allowed in locations:
16
index = manager.NodeToIndex(location)
17
allowed_vehicles = [] # you can add -1 iff the location can be dropped
18
if truck_allowed:
19
allowed_vehicles.extend(trucks)
20
if van_allowed:
21
allowed_vehicles.extend(vans)
22
if pickup_allowed:
23
allowed_vehicles.extend(pickups)
24
routing.VehicleVar(index).SetValues(allowed_vehicles)
25
side note: solver vehicle ID start at 0 but here I followed you vehicle_id convention starting at 1…