In vehicle routing problems (VRPs) sometimes not all stops can be served by every vehicle. The router engine offers the Attribute option to add a compatibility check. For most cases the Filter
option gives enough flexibility while maintaining simplicity. However, some problems may need to check for general compatibility but also have access to the current solution's route. In this case you can use the FilterWithRoute
option.
Filters are used to prevent assigning stops to a vehicle that they are incompatible with under any circumstance. However, if a stop is suitable for a vehicle according to the given filters, assigning it to a vehicle might not result in a valid solution in the current solution. This is checked and limited by the given constraints
, e.g. capacities or custom constraints. This means filters
are used to exclude stops from being assigned to certain vehicles, while Constraints make sure that when stops are being inserted into a route, the the route is still feasible.
The router
engine provides the Filter
and FilterWithRoute
options to add a custom filter.
Example
The aim of this example is to define a function to create a custom filter using the Filter
and FilterWithRoute
options. 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).
Filter
The Filter
option works by defining a compatibility function which checks if a vehicle is compatible with a stop. Often the Filter
option is combined with the custom constraints option. It is possible to use the Filter
option multiple times to add multiple filters.
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. We create a function to prohibit assigning the stops Arashiyama Bamboo Forest
and Kinkaku-ji
to vehicle v2
then pass into the Filter
option.
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).
The solution should look similar to this one:
You can see that the stops Arashiyama Bamboo Forest
and Kinkaku-ji
are assigned to vehicle v1
because they are not compatible with v2
, due to the custom filter.
Filter with route
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. That limits the number of assigned stops to a vehicle to a fixed number of 3 using the FilterWithRoute
option. This is done by filtering out vehicles that may not be assigned additional stops due to the number of stops that are already in the vehicle's route.
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).
The solution should look similar to this one:
You can see that the stop Arashiyama Bamboo Forest
is not assigned because the maximum number of stops per route has been already reached for both vehicles v1
and v2
.