Features

Duration matrix

A how-to guide for using duration matrices with vehicle routing.

Nextmv provides a hosted map data service as a map data cloud feature based on OSRM if you wish to use a pre-calculated distance matrix. The service is currently free to use with any paid plans or trials and will be available only as a paid add-on in the future. The service can be configured via the input file and can be used with either the Nextmv routing app or platform.

This feature is configurable via .json input, without the need for code customization, and is available for both the Marketplace app and Platform. You can find a list of all available features here.

This how-to guide assumes you already completed the get started with vehicle routing tutorial.

When duration_matrix and distance_matrix (with speed) are both specified, the duration_matrix takes precedence.

Field nameRequiredData typeSI UnitDefined onExampleConfigurable via defaults
duration_matrixNoarray of array of floatsecondsinput{"duration_matrix": [[1.23, 4.56], [7.89, 0.12]]}

By default, the duration of traveling between two locations is calculated using the Haversine formula and factoring the speed of each vehicle. To override this behavior, you can specify a duration matrix in the input that provides the duration of going from location A to B, using the duration_matrix feature.

The duration_matrix is useful when you want to use a different travel duration function, or when you want to use a pre-calculated duration matrix. For example, you may have your own mapping service that provides the travel duration between locations.

The duration_matrix is an array of arrays of float. The units for the values are seconds. The matrix must be square, and its size is:

n + 2m
Copy

Where:

  • n: the number of stops.
  • m: the number of vehicles.

If you’re using Nextmv Maps, the max size for the duration matrix is 2,000 x 2,000. If you’re using your own matrix there is no size limit other than the 100 MB file size limit for the input file. The size follows that each float value is the duration in seconds of traveling from location at index i to location at index j. Indices must be given in the following order:

[
  stop-1, ..., stop-n,
  vehicle-1-start-location, vehicle-1-end-location, ..., vehicle-m-start-location, vehicle-m-end-location
]
Copy

The matrix may be asymmetric, meaning that the duration of going from location A to B may be different than the duration of going from location B to A.

Here is an example input defining a complete duration matrix. In this example, the duration to travel from "Fushimi Inari Taisha" to "Kiyomizu-dera" and viceversa is 720 seconds. Vehicle locations are omitted, given that the duration of going to/from them is zero.

A sample output obtained after solving the problem is also shown.

{
  "duration_matrix": [
    [0, 720, 1020, 0, 0],
    [720, 0, 660, 0, 0],
    [1020, 660, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
  ],
  "stops": [
    {
      "id": "Fushimi Inari Taisha",
      "location": { "lon": 135.772695, "lat": 34.967146 }
    },
    {
      "id": "Kiyomizu-dera",
      "location": { "lon": 135.78506, "lat": 34.994857 }
    },
    {
      "id": "Nijō Castle",
      "location": { "lon": 135.748134, "lat": 35.014239 }
    }
  ],
  "vehicles": [
    {
      "id": "v1"
    }
  ]
}
Copy

Page last updated