-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_constraint_maximum_travel_duration.go
103 lines (82 loc) · 2.76 KB
/
model_constraint_maximum_travel_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
// © 2019-present nextmv.io inc
package nextroute
// MaximumTravelDurationConstraint is a constraint that limits the
// total travel duration of a vehicle.
type MaximumTravelDurationConstraint interface {
ModelConstraint
// Maximum returns the maximum expression which defines the maximum
// travel duration of a vehicle type.
Maximum() VehicleTypeDurationExpression
}
// NewMaximumTravelDurationConstraint returns a new
// MaximumTravelDurationConstraint.
func NewMaximumTravelDurationConstraint(
maximum VehicleTypeDurationExpression,
) (MaximumTravelDurationConstraint, error) {
return &maximumTravelDurationConstraintImpl{
modelConstraintImpl: newModelConstraintImpl(
"maximum_travel_duration",
ModelExpressions{},
),
maximum: maximum,
}, nil
}
type maximumTravelDurationConstraintImpl struct {
maximum VehicleTypeDurationExpression
modelConstraintImpl
}
func (l *maximumTravelDurationConstraintImpl) String() string {
return l.name
}
func (l *maximumTravelDurationConstraintImpl) EstimationCost() Cost {
return Constant
}
func (l *maximumTravelDurationConstraintImpl) Maximum() VehicleTypeDurationExpression {
return l.maximum
}
func (l *maximumTravelDurationConstraintImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
moveImpl := move.(*solutionMoveStopsImpl)
vehicle := moveImpl.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()
isDependentOnTime := vehicleType.TravelDurationExpression().IsDependentOnTime()
previous, _ := moveImpl.previous()
cumulativeDurationAtStart := previous.CumulativeTravelDurationValue()
maximum := l.maximum.Value(vehicleType, nil, nil)
value := 0.0
generator := newSolutionStopGenerator(
*moveImpl,
false,
isDependentOnTime,
)
defer generator.release()
previousStop, _ := generator.next()
departure := previousStop.EndValue()
for solutionStop, ok := generator.next(); ok; solutionStop, ok = generator.next() {
travelDuration, _, _, end := vehicleType.TemporalValues(
departure,
previousStop.ModelStop(),
solutionStop.ModelStop(),
)
value += travelDuration
if value+cumulativeDurationAtStart > maximum {
return true, constNoPositionsHint
}
previousStop = solutionStop
departure = end
}
next, _ := moveImpl.next()
delta := value - next.CumulativeTravelDurationValue()
if vehicle.Last().CumulativeTravelDurationValue()+delta > maximum {
return true, constNoPositionsHint
}
return false, constNoPositionsHint
}
func (l *maximumTravelDurationConstraintImpl) DoesVehicleHaveViolations(vehicle SolutionVehicle) bool {
return vehicle.Last().CumulativeTravelDurationValue() >
l.maximum.Value(vehicle.ModelVehicle().VehicleType(), nil, nil)
}
func (l *maximumTravelDurationConstraintImpl) IsTemporal() bool {
return true
}