-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_objective_expression_test.go
159 lines (144 loc) · 3.58 KB
/
model_objective_expression_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// © 2019-present nextmv.io inc
package nextroute_test
import (
"testing"
"github.com/nextmv-io/nextroute"
)
// Simply test that we can add a new expression objective to the model.
func TestAddExpressionObjective(t *testing.T) {
model, err := createModel(
input(
vehicleTypes("truck"),
[]Vehicle{
vehicles(
"truck",
depot(),
1,
)[0],
},
planSingleStops(),
planPairSequences(),
),
)
if err != nil {
t.Error(err)
}
if len(model.Objective().Terms()) != 0 {
t.Error("model objective should be empty")
}
expressions := len(model.Expressions())
e := nextroute.NewConstantExpression("test", 1.0)
objective := nextroute.NewExpressionObjective(e)
_, err = model.Objective().NewTerm(1.0, objective)
if err != nil {
t.Error(err)
}
if len(model.Objective().Terms()) != 1 {
t.Error("model objective should have an objective")
}
if registered, ok := objective.(nextroute.RegisteredModelExpressions); ok {
if len(registered.ModelExpressions()) != 1 {
t.Error("objective should have an expression")
}
}
if len(model.Expressions())-expressions != 1 {
t.Error("expressions should increase by 1")
}
}
// This test simulates a move on a solution and checks that the objective is
// being measured correctly based on a constant expression.
func TestExpressionObjective_EstimateDeltaValue(t *testing.T) {
model, err := createModel(
input(
vehicleTypes("truck", "car", "bike"),
[]Vehicle{
vehicles(
"truck",
depot(),
1,
)[0],
},
planSingleStops(),
planPairSequences(),
),
)
if err != nil {
t.Error(err)
}
// Use a constant expression for simplicity, so the insertion cost of a
// plan unit is always the number of stops times this constant.
expressionValue := 666.999
e := nextroute.NewConstantExpression("test", expressionValue)
objective := nextroute.NewExpressionObjective(e)
_, err = model.Objective().NewTerm(1.0, objective)
if err != nil {
t.Error(err)
}
solution, err := nextroute.NewSolution(model)
if err != nil {
t.Error(err)
}
planUnits := model.PlanStopsUnits()
position, err := nextroute.NewStopPosition(
solution.Vehicles()[0].First(),
solution.SolutionPlanStopsUnit(planUnits[0]).SolutionStops()[0],
solution.Vehicles()[0].Last(),
)
if err != nil {
t.Fatal(err)
}
m1, err := nextroute.NewMoveStops(
solution.SolutionPlanStopsUnit(planUnits[0]),
[]nextroute.StopPosition{position},
)
if err != nil {
t.Fatal(err)
}
position1, err := nextroute.NewStopPosition(
solution.Vehicles()[0].First(),
solution.SolutionPlanStopsUnit(planUnits[3]).SolutionStops()[0],
solution.SolutionPlanStopsUnit(planUnits[3]).SolutionStops()[1],
)
if err != nil {
t.Fatal(err)
}
position2, err := nextroute.NewStopPosition(
solution.SolutionPlanStopsUnit(planUnits[3]).SolutionStops()[0],
solution.SolutionPlanStopsUnit(planUnits[3]).SolutionStops()[1],
solution.Vehicles()[0].Last(),
)
if err != nil {
t.Fatal(err)
}
m2, err := nextroute.NewMoveStops(
solution.SolutionPlanStopsUnit(planUnits[3]),
[]nextroute.StopPosition{position1, position2},
)
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
move nextroute.SolutionMoveStops
want float64
}{
{
name: "single stop added increments value by 1",
move: m1,
want: 1 * expressionValue,
},
{
name: "sequence of 2 stops added increments value by 2",
move: m2,
want: 2 * expressionValue,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := objective.EstimateDeltaValue(tt.move)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}