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

Sdk

Options

You will learn how to use the Options function with a practical example.

The router engine is configured through a list of options that are passed in to the NewRouter function. The opts ...Option argument implies that any number of options can be passed, or none at all. Sometimes, not all the options needed to configure the router are available at the time it is instantiated. The Options function allows the router to be configured at any time after it has been declared.

The router engine's overview page provides a list of all options that can be configured, including those for:

  • adding constraints,
  • setting vehicle properties, and
  • customizing the solver.

In this tutorial, the Options function is showcased using vehicle's start and end locations.

Example

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.

The code snippet used to configure a vehicle's start and end locations can be modified to pass the vehicle end locations using the Options function, as opposed to configuring this option as an argument for the NewRouter function:

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"`
    Starts   []route.Position `json:"starts,omitempty"`
    Ends     []route.Position `json:"ends,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.Starts(i.Starts),
            // route.Ends(i.Ends),
        )
        if err != nil {
            return nil, err
        }
        // Set the ending locations for each vehicle using the Options function.
        if err := router.Options(route.Ends(i.Ends)); err != nil {
            return nil, err
        }

        return router.Solver(opt)
    }

    cli.Run(f)
}
Copy

Running the code above will result in the same solution as before.

Page last updated

Go to on-page nav menu