Vehicle initialization costs
You will learn how to use the InitializationCosts option with a practical example.
Adding initialization costs to vehicles is a common task in vehicle routing problems (VRPs). The activation or initialization of a vehicle can be useful if a vehicle isn't permanently in the fleet, but has to be rented for example. If this vehicle isn't needed in a solution, then the rental cost can be saved. The router engine provides the InitializationCosts option to configure the costs per vehicle that are added to the value function when a vehicle is used. This is done by specifying the vehicles' initialization costs.

Example

The aim of this example is to add initialization costs to vehicles. The introductory router example is used as a base, where routes are created to visit seven landmarks in Kyoto using two vehicles. In this example, v1 has an expensive initialization cost associated with it, while v2 does not.
initialization-costs-input
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
"initialization_costs": [100000, 0]
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.
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
InitializationCosts []float64 `json:"initialization_costs,omitempty"`
14
}
15
16
// Use the CLI runner to solve a Vehicle Routing Problem.
17
func main() {
18
f := func(i input, opt solve.Options) (solve.Solver, error) {
19
router, err := route.NewRouter(
20
i.Stops,
21
i.Vehicles,
22
route.InitializationCosts(i.InitializationCosts),
23
)
24
if err != nil {
25
return nil, err
26
}
27
28
return router.Solver(opt)
29
}
30
31
cli.Run(f)
32
}
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
"route_duration": 0
8
},
9
{
10
"id": "v2",
11
"route": [
12
{
13
"id": "Fushimi Inari Taisha",
14
"position": {
15
"lon": 135.772695,
16
"lat": 34.967146
17
}
18
},
19
{
20
"id": "Kiyomizu-dera",
21
"position": {
22
"lon": 135.78506,
23
"lat": 34.994857
24
}
25
},
26
{
27
"id": "Gionmachi",
28
"position": {
29
"lon": 135.775682,
30
"lat": 35.002457
31
}
32
},
33
{
34
"id": "Kyoto Imperial Palace",
35
"position": {
36
"lon": 135.762057,
37
"lat": 35.025431
38
}
39
},
40
{
41
"id": "Nijō Castle",
42
"position": {
43
"lon": 135.748134,
44
"lat": 35.014239
45
}
46
},
47
{
48
"id": "Kinkaku-ji",
49
"position": {
50
"lon": 135.728898,
51
"lat": 35.039705
52
}
53
},
54
{
55
"id": "Arashiyama Bamboo Forest",
56
"position": {
57
"lon": 135.672009,
58
"lat": 35.017209
59
}
60
}
61
],
62
"route_duration": 1818
63
}
64
]
65
}
Copied!
Given that v1 is the most expensive vehicle to operate, all stops are assigned to v2.
initialization-costs-output
Export as PDF
Copy link