Configure 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 [email protected].
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:
Go
1
package main
2
3
import (
4
"github.com/nextmv-io/code/engines/route"
5
"github.com/nextmv-io/code/hop/run/cli"
6
"github.com/nextmv-io/code/hop/solve"
7
)
8
9
// Struct to read from JSON in.
10
type input struct {
11
Stops []route.Stop `json:"stops,omitempty"`
12
Vehicles []string `json:"vehicles,omitempty"`
13
Starts []route.Position `json:"starts,omitempty"`
14
Ends []route.Position `json:"ends,omitempty"`
15
}
16
17
// Use the CLI runner to solve a Vehicle Routing Problem.
18
func main() {
19
f := func(i input, opt solve.Options) (solve.Solver, error) {
20
router, err := route.NewRouter(
21
i.Stops,
22
i.Vehicles,
23
route.Starts(i.Starts),
24
// route.Ends(i.Ends),
25
)
26
if err != nil {
27
return nil, err
28
}
29
// Set the ending locations for each vehicle using the Options function.
30
if err := router.Options(route.Ends(i.Ends)); err != nil {
31
return nil, err
32
}
33
34
return router.Solver(opt)
35
}
36
37
cli.Run(f)
38
}
Copied!
Running the code above will result in the same solution as before.
Export as PDF
Copy link
Contents
Example