Adding capacities to vehicles is a common task in vehicle routing problems (VRPs). This is known as the capacitated vehicle routing problem (CVRP). Stops have quantities associated to them that decrease or increase a vehicle's capacity. For example, a fuel-delivery truck can only hold a certain volume of fuel or a meal-delivery courier can only deliver three orders simultaneously, to keep them from losing freshness. In the case of sourcing, stops increase the vehicle's capacity since the vehicle is only performing pickups.
The router engine provides the Capacity
option to configure a capacity constraint for the routing problem. This is done by specifying the stops' quantities and the vehicles' capacities. A stop's quantity:
- decreases a vehicle's capacity if it is negative;
- increases a vehicle's capacity if it is positive.
The Capacity
option may be used multiple times if various dimensions need to be limited (for example, limiting weight and volume).
Example
The aim of this example is to add a quantity demanded to stops and a limited capacity to vehicles. The introductory router example is used as a base, where routes are created to visit seven landmarks in Kyoto using two vehicles.
Save the following information in an input.json
file (see input and output for more information on working with input files).
Code
The following program uses the CLI Runner to obtain a solution and requires access to the Nextmv code repository on GitHub. To request access, please contact support@nextmv.io.
To proceed with running the example, create a main.go
file and use the code snippet below.
To execute the example, specify the path to the input.json
file using command-line flags and use jq to extract the solution state (see runners for more information on building and running programs).
Solution
The solution should look similar to this one:
You can see that the capacity of each vehicle is not exceeded and the stops are assigned in a fashion that minimizes the route distance.