Features

Distance matrix

A how-to guide for using distance 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 distance_matrix (with speed) and duration_matrix are both specified, the duration_matrix takes precedence.

Field nameRequiredData typeSI UnitDefined onExampleConfigurable via defaults
distance_matrixNoarray of array of floatmetersinput{"distance_matrix": [[1.23, 4.56], [7.89, 0.12]]}

By default, the distance between two locations is calculated using the Haversine formula. To override this behavior, you can specify a distance matrix in the input that provides the distance of going from location A to B, using the distance_matrix feature. The travel duration between locations is obtained by factoring the distance and the speed (specified in meters/second) of each vehicle.

When using the distance_matrix feature, the speed of each vehicle must be specified.

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

The distance_matrix is an array of arrays of float. The units for the values are meters. 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 distance 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 distance in meters 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 distance of going from location A to B may be different than the distance of going from location B to A.

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

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

{
  "distance_matrix": [
    [0, 4300, 6700, 0, 0, 0, 0],
    [4300, 0, 4500, 0, 0, 0, 0],
    [6700, 4500, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 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",
      "speed": 10
    },
    {
      "id": "v2",
      "speed": 20
    }
  ]
}
Copy

Page last updated