-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_objective_expression.go
89 lines (72 loc) · 2.14 KB
/
model_objective_expression.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
// © 2019-present nextmv.io inc
package nextroute
// ExpressionObjective is an objective that uses an expression to calculate an
// objective.
type ExpressionObjective interface {
ModelObjective
// Expression returns the expression that is used to calculate the
// objective.
Expression() ModelExpression
}
// NewExpressionObjective is the implementation of sdk.NewExpressionObjective.
func NewExpressionObjective(e ModelExpression) ExpressionObjective {
return &expressionObjectiveImpl{
expression: e,
index: NewModelExpressionIndex(),
}
}
// expressionObjectiveImpl implements the ExpressionObjective
// interface.
type expressionObjectiveImpl struct {
expression ModelExpression
index int
}
func (e *expressionObjectiveImpl) Expression() ModelExpression {
return e.expression
}
func (e *expressionObjectiveImpl) Index() int {
return e.index
}
func (e *expressionObjectiveImpl) Value(solution Solution) float64 {
score := 0.0
for _, r := range solution.Vehicles() {
score += r.Last().CumulativeValue(e.expression)
}
return score
}
func (e *expressionObjectiveImpl) EstimateDeltaValue(
move SolutionMoveStops,
) float64 {
moveImpl := move.(*solutionMoveStopsImpl)
vehicle := moveImpl.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()
value := 0.0
first := true
var previousSolutionStop SolutionStop
generator := newSolutionStopGenerator(*moveImpl, false, false)
defer generator.release()
for solutionStop, ok := generator.next(); ok; solutionStop, ok = generator.next() {
if first {
previousSolutionStop = solutionStop
first = false
continue
}
value += e.expression.Value(
vehicleType,
previousSolutionStop.ModelStop(),
solutionStop.ModelStop(),
)
previousSolutionStop = solutionStop
}
nextmove, _ := moveImpl.next()
previousMove, _ := moveImpl.previous()
currentValue := nextmove.CumulativeValue(e.expression) -
previousMove.CumulativeValue(e.expression)
return value - currentValue
}
func (e *expressionObjectiveImpl) ModelExpressions() ModelExpressions {
return ModelExpressions{e.expression}
}
func (e *expressionObjectiveImpl) String() string {
return "expression_objective"
}