-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_constraint_maximum_stops.go
80 lines (66 loc) · 2.27 KB
/
model_constraint_maximum_stops.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
// © 2019-present nextmv.io inc
package nextroute
// MaximumStopsConstraint is a constraint that limits the maximum number of
// stops a vehicle type can have. The maximum number of stops is defined by
// the maximum stops expression. The first stop of a vehicle is not counted
// as a stop and the last stop of a vehicle is not counted as a stop.
type MaximumStopsConstraint interface {
ModelConstraint
// MaximumStops returns the maximum stops expression which defines the
// maximum number of stops a vehicle type can have.
MaximumStops() VehicleTypeExpression
}
// NewMaximumStopsConstraint returns a new MaximumStopsConstraint.
func NewMaximumStopsConstraint(
maximumStops VehicleTypeExpression,
) (MaximumStopsConstraint, error) {
return &maximumStopsConstraintImpl{
modelConstraintImpl: newModelConstraintImpl(
"maximum_stops",
ModelExpressions{},
),
maximumStops: maximumStops,
}, nil
}
type maximumStopsConstraintImpl struct {
maximumStops VehicleTypeExpression
maximumStopsByVehicleType []float64
modelConstraintImpl
}
func (l *maximumStopsConstraintImpl) Lock(model Model) error {
vehicleTypes := model.VehicleTypes()
l.maximumStopsByVehicleType = make([]float64, len(vehicleTypes))
for _, vehicleType := range vehicleTypes {
l.maximumStopsByVehicleType[vehicleType.Index()] = l.maximumStops.Value(
vehicleType,
nil,
nil,
)
}
return nil
}
func (l *maximumStopsConstraintImpl) String() string {
return l.name
}
func (l *maximumStopsConstraintImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
moveImpl := move.(*solutionMoveStopsImpl)
stopPositions := moveImpl.stopPositions
nrStopsToBeAddedToSolution := len(stopPositions)
beforeStop := stopPositions[len(stopPositions)-1].Next()
vehicle := beforeStop.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType().Index()
maximumStops := l.maximumStopsByVehicleType[vehicleType]
if float64(vehicle.NumberOfStops()+nrStopsToBeAddedToSolution) >
maximumStops {
return true, constSkipVehiclePositionsHint
}
return false, constNoPositionsHint
}
func (l *maximumStopsConstraintImpl) EstimationCost() Cost {
return Constant
}
func (l *maximumStopsConstraintImpl) MaximumStops() VehicleTypeExpression {
return l.maximumStops
}