Vehicle Routing Problem with Time Windows
Overview
Many vehicle routing problems involve scheduling visits to customers who are only available during specific time windows. These problems are known as vehicle routing problems with time windows (VRPTWs).
VRPTW Example
On this page, we’ll walk through an example that shows how to solve a VRPTW. Since the problem involves time windows, the data include a time matrix, which contains the travel times between locations (rather than a distance matrix as in previous examples).
The diagram below shows the locations to visit in blue and the depot in black. The time windows are shown above each location. See Location coordinates in the VRP section for more details about how the locations are defined.
The goal is to minimize the total travel time of the vehicles.
Solving the VRPTW example with OR-Tools
The following sections describe how to solve the VRPTW example with OR-Tools.
Create the data
The following function creates the data for the problem.
Python
The data consists of:
- data[‘time_matrix’] : An array of travel times between locations. Note that this differs from previous examples, which use a distance matrix. If all vehicles travel at the same speed, you will get the same solution if you use a distance matrix or a time matrix, since travel distances are a constant multiple of travel times.
- data[‘time_windows’] : An array of time windows for the locations, which you can think of as requested times for a visit. Vehicles must visit a location within its time window.
- data[‘num_vehicles’] : The number of vehicles in the fleet.
- data[‘depot’] : The index of the depot.
The data consists of:
- time_matrix : An array of travel times between locations. Note that this differs from previous examples, which use a distance matrix. If all vehicles travel at the same speed, you will get the same solution if you use a distance matrix or a time matrix, since travel distances are a constant multiple of travel times.
- time_windows : An array of time windows for the locations, which you can think of as requested times for a visit. Vehicles must visit a location within its time window.
- num_vehicles : The number of vehicles in the fleet.
- depot : The index of the depot.
The data consists of:
- timeMatrix : An array of travel times between locations. Note that this differs from previous examples, which use a distance matrix. If all vehicles travel at the same speed, you will get the same solution if you use a distance matrix or a time matrix, since travel distances are a constant multiple of travel times.
- timeWindows : An array of time windows for the locations, which you can think of as requested times for a visit. Vehicles must visit a location within its time window.
- vehicleNumber : The number of vehicles in the fleet.
- depot : The index of the depot.
The data consists of:
- TimeMatrix : An array of travel times between locations. Note that this differs from previous examples, which use a distance matrix. If all vehicles travel at the same speed, you will get the same solution if you use a distance matrix or a time matrix, since travel distances are a constant multiple of travel times.
- TimeWindows : An array of time windows for the locations, which you can think of as requested times for a visit. Vehicles must visit a location within its time window.
- VehicleNumber : The number of vehicles in the fleet.
- Depot : The index of the depot.
Time callback
The following function creates the time callback and passes it to the solver. It also sets the arc costs, which define the cost of travel, to be the travel times between locations.
Using OptaPlanner to solve a Vehicle Routing Problem with Time Windows
Hello OptaPlanner community. I am developing a Rest API to plan the routes of a fleet of vehicles. Looking for a tool that would help me I found Optaplanner, and I have seen that it has several success stories. In a first stage I made a planning taking into account the fastest distance and the capacity of the vehicle to cover all its visits. And I got the results I expected. Now I’m planning for time windows of visits and deposits, but I’m not successful yet.
R1- I have a fleet of vehicles. Each vehicle has a capacity and its deposit and this deposit has a window of time. From the example of OptaPlanner for VRP I have only made a variation on the capacity that I handle as a float. As I understand it, all the vehicles in the OptaPlanner example are moved for a single depot. In my case, each vehicle has its own depot, each vehicle has its own fixed depot, and it is possible that several vehicles have the same depot.
R2- I have the visits (delivery services). Each visit has a demand and a window of time. From the example of OptaPlanner for VRP I have only made one modification regarding the demand that I handle it as a type «float».
In this process of adding this variant with TW to my routing problem I have some doubts and problems since I have not obtained a viable solution to my problem by applying TW:
1- I understand that I do not need to make modifications to the OptaPlanner example so that each vehicle cannot transport more items than its capacity. I only need to adjust the constrint provider so that the calculation is on float. I would like to know if I am right ? and on the other hand How I can manage the capacities and demands with dimensions?, in OptaPlanner it is a number but I need to manage it as volume and weight.
In the OptaPlanner domain I modified the variables «capacity» from vehicle and «demand» from visit, both to type «float».
2- In the OptaPlanner example I understand that the TW is a long that multiplies by a thousand, but I do not know if this long expresses an hour or date, or if it is just the hour converted into minutes and multiplied by a thousand. What I am doing is converting the TW to minutes and multiplied by a thousand, for example if it is 8am, the ready time is a log equal to ‘480000’. In the case of the service duration, I do not multiply it by 1000, I always treat it as 10 minutes. Am I doing the conversion correctly? , is this the long that OptaPlanner expects?
3- I understand that using the example of OptaPlanner for time windows I can solve this requirement (R2), without making variations, however for some reason that I can not find is not giving me back a feasible solution. It returns me for example: time spent (5000), best score (-3313987hard/-4156887soft).
I have thought that the error could be in the conversion of the dates of the time window or maybe some hard constraint that I lack, because the arrival times of the visits do not adapt to the time windows defined for visits nor for deposits.
For example: I have 4 visits with time windows, 2 in the morning (visit 2 and visit 4) and 2 in the afternoon (visit 1 and visit 3). I have two vehicles, vehicle 1 leaves a depot 1 that has a time window in a morning schedule and the other vehicle leaves a depot 2 that has a time window in an afternoon schedule. So I expect vehicle 1 to conduct the visits that have a time window in the morning and vehicle 2 to conduct the visits that have a time window in the afternoon: [vehicle 1:
I must be doing something very wrong, but I can’t find where, not only does it not meet the TW of the deposit, but the arrival times of each visit exceed the defined TW. I don’t understand why I get such big arrival times, that even exceed the defined limit for 1 day (all arrival times are over 1440000 = 1400min = 24 = 12am), that is, they arrived after this time.
This is the result I have obtained: score (-3313987hard/-4156887soft)
Route 1 referring to the route followed by vehicle 1 Vehicle 1
Route 2 referring to the route followed by vehicle 2 Vehicle 2
This is my code, it can give you a better context. This is my VariableListerner for updating the shadow variable ‘arrival time’. I have not made any modifications, however the arrival times returned to me for each visit do not comply with the TW.
And this service is where I build the domain entities from the data stored in the database (find). This TimeWindowedVehicleRoutingSolution is the one I use in the solver.
Then I create the solver, start the optimization and finally save the best:
Any help on where the error is will be welcome. I am starting with Optaplanner, please any comments will be very helpful. Thank you very much!
Sorry about the calligraphy, English is not my language.