Stop selector
You will learn how to use the Selector option with a practical example.
By default the router engine looks in the list of stops yet to be assigned and selects the one with the lowest index. However, it may be useful to select the next stop to be inserted into a route differently, which means that the default behaviour needs to overridden.
The router engine provides the Selector option to set up a custom selector for stops. This is done by writing a function that selects the next stop or even multiple stops.

Example

The router example is used as a base, where routes are created to visit seven landmarks in Kyoto using two vehicles and a score for each stop. This time, we define a function in code, not in the input file, to create a custom selector.
Save the following information in an input.json file (see input and output for more information on working with input files).
JSON
1
{
2
"stops": [
3
{
4
"id": "Fushimi Inari Taisha",
5
"position": { "lon": 135.772695, "lat": 34.967146 }
6
},
7
{
8
"id": "Kiyomizu-dera",
9
"position": { "lon": 135.78506, "lat": 34.994857 }
10
},
11
{
12
"id": "Nijō Castle",
13
"position": { "lon": 135.748134, "lat": 35.014239 }
14
},
15
{
16
"id": "Kyoto Imperial Palace",
17
"position": { "lon": 135.762057, "lat": 35.025431 }
18
},
19
{
20
"id": "Gionmachi",
21
"position": { "lon": 135.775682, "lat": 35.002457 }
22
},
23
{
24
"id": "Kinkaku-ji",
25
"position": { "lon": 135.728898, "lat": 35.039705 }
26
},
27
{
28
"id": "Arashiyama Bamboo Forest",
29
"position": { "lon": 135.672009, "lat": 35.017209 }
30
}
31
],
32
"vehicles": ["v1", "v2"],
33
"score": [5, 4, 6, 7, 3, 2, 1]
34
}
Copied!

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 [email protected].
To proceed with running the example, create a main.go file and use the code snippet below. We create a function to select the next stop based on a score which we then pass into the Selector option.
Go
1
package main
2
3
import (
4
"github.com/nextmv-io/code/engines/route"
5
fleetEngine "github.com/nextmv-io/code/engines/route/fleet"
6
"github.com/nextmv-io/code/hop/model"
7
"github.com/nextmv-io/code/hop/run/cli"
8
"github.com/nextmv-io/code/hop/solve"
9
)
10
11
// Struct to read from JSON in.
12
type input struct {
13
Stops []route.Stop `json:"stops,omitempty"`
14
Vehicles []string `json:"vehicles,omitempty"`
15
Score []int `json:"score,omitempty"`
16
}
17
18
// Use the CLI runner to solve a Vehicle Routing Problem.
19
func main() {
20
f := func(i input, opt solve.Options) (solve.Solver, error) {
21
// Define a location selector. This location selector looks for the highest
22
// score of a stop among the not yet assigned stops and returns it, wrapped
23
// in an IntDomain
24
selector := func(s fleetEngine.State) model.IntDomain {
25
index := -1
26
highestScore := 0
27
for _, l := range s.Locations().Slice() {
28
if i.Score[l] > highestScore {
29
index = l
30
highestScore = i.Score[l]
31
}
32
}
33
if index != -1 {
34
return model.Domain(model.Range(index, index))
35
}
36
return model.Domain()
37
}
38
router, err := route.NewRouter(
39
i.Stops,
40
i.Vehicles,
41
route.Selector(selector),
42
)
43
if err != nil {
44
return nil, err
45
}
46
47
return router.Solver(opt)
48
}
49
50
cli.Run(f)
51
}
Copied!
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).
Bash
1
go run main.go -hop.runner.input.path input.json | jq .state
Copied!

Solution

The solution should look similar to this one:
JSON
1
{
2
"unassigned": [],
3
"vehicles": [
4
{
5
"id": "v1",
6
"route": [
7
{
8
"id": "Kinkaku-ji",
9
"position": {
10
"lon": 135.728898,
11
"lat": 35.039705
12
}
13
},
14
{
15
"id": "Nijō Castle",
16
"position": {
17
"lon": 135.748134,
18
"lat": 35.014239
19
}
20
},
21
{
22
"id": "Kyoto Imperial Palace",
23
"position": {
24
"lon": 135.762057,
25
"lat": 35.025431
26
}
27
},
28
{
29
"id": "Gionmachi",
30
"position": {
31
"lon": 135.775682,
32
"lat": 35.002457
33
}
34
},
35
{
36
"id": "Kiyomizu-dera",
37
"position": {
38
"lon": 135.78506,
39
"lat": 34.994857
40
}
41
},
42
{
43
"id": "Fushimi Inari Taisha",
44
"position": {
45
"lon": 135.772695,
46
"lat": 34.967146
47
}
48
}
49
],
50
"route_duration": 1242
51
},
52
{
53
"id": "v2",
54
"route": [
55
{
56
"id": "Arashiyama Bamboo Forest",
57
"position": {
58
"lon": 135.672009,
59
"lat": 35.017209
60
}
61
}
62
],
63
"route_duration": 0
64
}
65
]
66
}
Copied!
To find this solution, the highest priority stops were selected and assigned first.
Export as PDF
Copy link