-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_constraint_maximum_duration.go
106 lines (83 loc) · 2.58 KB
/
model_constraint_maximum_duration.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
// © 2019-present nextmv.io inc
package nextroute
import (
"fmt"
)
// MaximumDurationConstraint is a constraint that limits the
// duration of a vehicle.
type MaximumDurationConstraint interface {
ModelConstraint
// Maximum returns the maximum expression which defines the maximum
// duration of a vehicle type.
Maximum() VehicleTypeDurationExpression
}
// NewMaximumDurationConstraint returns a new MaximumDurationConstraint.
func NewMaximumDurationConstraint(
maximum VehicleTypeDurationExpression,
) (MaximumDurationConstraint, error) {
return &maximumDurationConstraintImpl{
modelConstraintImpl: newModelConstraintImpl(
"maximum_duration",
ModelExpressions{},
),
maximum: maximum,
}, nil
}
type maximumDurationConstraintImpl struct {
maximum VehicleTypeDurationExpression
modelConstraintImpl
}
func (l *maximumDurationConstraintImpl) String() string {
return fmt.Sprintf("MaximumDuration '%v', maxima: %v",
l.name,
l.maximum,
)
}
func (l *maximumDurationConstraintImpl) EstimationCost() Cost {
return Constant
}
func (l *maximumDurationConstraintImpl) Maximum() VehicleTypeDurationExpression {
return l.maximum
}
func (l *maximumDurationConstraintImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
moveImpl := move.(*solutionMoveStopsImpl)
vehicle := moveImpl.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()
dependentOnTime := vehicleType.TravelDurationExpression().IsDependentOnTime()
maximumValue := l.maximum.Value(vehicleType, nil, nil)
startValue := vehicle.First().StartValue()
previous, _ := moveImpl.previous()
endValue := previous.EndValue()
generator := newSolutionStopGenerator(
*moveImpl,
false,
dependentOnTime,
)
defer generator.release()
previousStop, _ := generator.next()
for solutionStop, ok := generator.next(); ok; solutionStop, ok = generator.next() {
_, _, _, endValue = vehicleType.TemporalValues(
endValue,
previousStop.ModelStop(),
solutionStop.ModelStop(),
)
if endValue-startValue > maximumValue {
return true, constNoPositionsHint
}
previousStop = solutionStop
}
deltaEnd := endValue - previousStop.EndValue() - previousStop.SlackValue()
if vehicle.DurationValue()+deltaEnd > maximumValue {
return true, constNoPositionsHint
}
return false, constNoPositionsHint
}
func (l *maximumDurationConstraintImpl) DoesVehicleHaveViolations(vehicle SolutionVehicle) bool {
return vehicle.DurationValue() >
l.maximum.Value(vehicle.ModelVehicle().VehicleType(), nil, nil)
}
func (l *maximumDurationConstraintImpl) IsTemporal() bool {
return true
}