You are viewing Nextmv legacy docs. ⚡️ Go to latest docs ⚡️

Router

Alternates

You will learn how to use the Alternates option with a practical example.

In vehicle routing problems (VRPs) you may want your drivers to have a break at specific locations that are especially suitable for this. The router engine provides the Alternates option to configure such stops for each vehicle. The router will then add exactly one of those stops optimally to the vehicle's route.

The Alternates option is often combined with the Windows and the Services option to enforce a driver's break during the given time window for the specified "service time".

Example

The aim of this example is to define alternate stops (or breaks) for a vehicle. The introductory router example is used as a base, where routes are created to visit seven landmarks in Kyoto using two vehicles. Note, this example also uses the Capacity option for illustrative purposes.

alternates-input

Save the following information in an input.json file (see input and output for more information on working with input files).

{
  "stops": [
    {
      "id": "Fushimi Inari Taisha",
      "position": { "lon": 135.772695, "lat": 34.967146 }
    },
    {
      "id": "Kiyomizu-dera",
      "position": { "lon": 135.78506, "lat": 34.994857 }
    },
    {
      "id": "Nijō Castle",
      "position": { "lon": 135.748134, "lat": 35.014239 }
    },
    {
      "id": "Kyoto Imperial Palace",
      "position": { "lon": 135.762057, "lat": 35.025431 }
    },
    {
      "id": "Gionmachi",
      "position": { "lon": 135.775682, "lat": 35.002457 }
    },
    {
      "id": "Kinkaku-ji",
      "position": { "lon": 135.728898, "lat": 35.039705 }
    },
    {
      "id": "Arashiyama Bamboo Forest",
      "position": { "lon": 135.672009, "lat": 35.017209 }
    }
  ],
  "vehicles": ["v1", "v2"],
  "alternate_stops": [
    {
      "id": "v1",
      "stops": ["Nijō Castle", "Kiyomizu-dera"]
    },
    {
      "id": "v2",
      "stops": ["Nijō Castle", "Kiyomizu-dera"]
    }
  ],
  "quantities": [-1, -1, -1, -1, -1, 0, 0],
  "capacities": [3, 3]
}
Copy

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.

package main

import (
    "github.com/nextmv-io/code/engines/route"
    "github.com/nextmv-io/code/hop/run/cli"
    "github.com/nextmv-io/code/hop/solve"
)

// Struct to read from JSON in.
type input struct {
    Stops      []route.Stop      `json:"stops,omitempty"`
    Vehicles   []string          `json:"vehicles,omitempty"`
    Alternates []route.Alternate `json:"alternate_stops,omitempty"`
    Quantities []int             `json:"quantities,omitempty"`
    Capacities []int             `json:"capacities,omitempty"`
}

// Use the CLI runner to solve a Vehicle Routing Problem.
func main() {
    f := func(i input, opt solve.Options) (solve.Solver, error) {
        router, err := route.NewRouter(
            i.Stops,
            i.Vehicles,
            route.Alternates(i.Alternates),
            route.Capacity(i.Quantities, i.Capacities),
        )
        if err != nil {
            return nil, err
        }

        return router.Solver(opt)
    }

    cli.Run(f)
}
Copy

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).

go run main.go -hop.runner.input.path input.json | jq .state
Copy

Solution

The solution should look similar to this one:

{
  "unassigned": [],
  "vehicles": [
    {
      "id": "v1",
      "route": [
        {
          "id": "Kyoto Imperial Palace",
          "position": {
            "lon": 135.762057,
            "lat": 35.025431
          }
        },
        {
          "id": "Nijō Castle",
          "position": {
            "lon": 135.748134,
            "lat": 35.014239
          }
        },
        {
          "id": "Kinkaku-ji",
          "position": {
            "lon": 135.728898,
            "lat": 35.039705
          }
        },
        {
          "id": "Arashiyama Bamboo Forest",
          "position": {
            "lon": 135.672009,
            "lat": 35.017209
          }
        }
      ],
      "route_duration": 1085
    },
    {
      "id": "v2",
      "route": [
        {
          "id": "Gionmachi",
          "position": {
            "lon": 135.775682,
            "lat": 35.002457
          }
        },
        {
          "id": "Kiyomizu-dera",
          "position": {
            "lon": 135.78506,
            "lat": 34.994857
          }
        },
        {
          "id": "Fushimi Inari Taisha",
          "position": {
            "lon": 135.772695,
            "lat": 34.967146
          }
        }
      ],
      "route_duration": 448
    }
  ]
}
Copy

You can see that only one alternate stop was assigned to each vehicle. In this example, Nijō Castle was assigned to v1 and Kiyomizu-dera was assigned to v2.

alternates-with-capacity-output

Page last updated

Go to on-page nav menu