-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_maximum.go
438 lines (356 loc) · 11.3 KB
/
model_maximum.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// © 2019-present nextmv.io inc
package nextroute
import (
"fmt"
"math"
)
// Maximum can be used as a constraint or an objective that limits the maximum
// cumulative value can be assigned to a vehicle type. The maximum cumulative
// value is defined by the expression and the maximum value is defined by the
// maximum expression.
type Maximum interface {
ModelConstraint
ModelObjective
// Expression returns the expression which is used to calculate the
// cumulative value of each stop which is required to stay below the
// maximum value and above zero.
Expression() ModelExpression
// Maximum returns the maximum expression which defines the maximum
// cumulative value that can be assigned to a vehicle type.
Maximum() VehicleTypeExpression
// PenaltyOffset returns the penalty offset. Penalty offset is used to
// offset the penalty. The penalty offset is added to the penalty if there
// is at least one violation.
PenaltyOffset() float64
// SetPenaltyOffset sets the penalty offset. Penalty offset is used to
// offset the penalty. The penalty offset is added to the penalty if there
// is at least one violation. The default penalty offset is 0.0 and it can
// be changed by this method and must be positive.
SetPenaltyOffset(penaltyOffset float64) error
}
// NewMaximum creates a new maximum construct which can be used as constraint
// or as objective.
func NewMaximum(
expression ModelExpression,
maximum VehicleTypeExpression,
) (Maximum, error) {
return &maximumImpl{
modelConstraintImpl: newModelConstraintImpl(
"maximum",
ModelExpressions{expression},
),
maximum: maximum,
penaltyOffset: 0.0,
}, nil
}
type maximumImpl struct {
maximum VehicleTypeExpression
deltas []float64
modelConstraintImpl
// hasNegativeValues is true if the expression has negative values.
// This is used to optimize the estimation cost.
hasNegativeValues bool
// hasPositiveValues is true if the expression has positive values.
// This is used to optimize the estimation cost.
hasPositiveValues bool
hasConstantExpression bool
hasStopExpressionAndNoNegativeValues bool
resourceExpression ModelExpression
maximumByVehicleType []float64
penaltyOffset float64
hasNoEffect []bool
}
func (l *maximumImpl) PenaltyOffset() float64 {
return l.penaltyOffset
}
func (l *maximumImpl) SetPenaltyOffset(penaltyOffset float64) error {
if penaltyOffset < 0.0 {
return fmt.Errorf(
"maximum objective, penalty offset must be positive, it can not be %f",
penaltyOffset,
)
}
l.penaltyOffset = penaltyOffset
return nil
}
func (l *maximumImpl) Lock(model Model) error {
l.hasNegativeValues = l.Expression().HasNegativeValues()
l.hasPositiveValues = l.Expression().HasPositiveValues()
if _, ok := l.Expression().(ConstantExpression); ok {
l.hasConstantExpression = true
}
if _, ok := l.Expression().(StopExpression); ok &&
!l.hasNegativeValues {
l.hasStopExpressionAndNoNegativeValues = true
}
l.resourceExpression = l.expressions[0]
vehicleTypes := model.VehicleTypes()
l.maximumByVehicleType = make([]float64, len(vehicleTypes))
for _, vehicleType := range vehicleTypes {
l.maximumByVehicleType[vehicleType.Index()] = l.maximum.Value(
vehicleType,
nil,
nil,
)
}
planUnits := model.PlanStopsUnits()
l.hasNoEffect = make([]bool, len(planUnits))
if !l.hasStopExpressionAndNoNegativeValues {
return nil
}
l.deltas = make([]float64, len(planUnits))
for _, planUnit := range planUnits {
delta := 0.0
hasNoEffect := true
for _, stop := range planUnit.Stops() {
value := l.Expression().Value(nil, nil, stop)
delta += value
if value != 0 {
hasNoEffect = false
}
}
l.deltas[planUnit.Index()] = delta
l.hasNoEffect[planUnit.Index()] = hasNoEffect
}
return nil
}
func (l *maximumImpl) String() string {
return l.name
}
func (l *maximumImpl) ID() string {
return l.name
}
func (l *maximumImpl) SetID(id string) {
l.name = id
}
func (l *maximumImpl) EstimationCost() Cost {
if l.hasNegativeValues && !l.hasPositiveValues {
return Constant
}
if l.hasConstantExpression {
return Constant
}
if l.hasStopExpressionAndNoNegativeValues {
return Constant
}
return LinearStop
}
func (l *maximumImpl) Expression() ModelExpression {
return l.expressions[0]
}
func (l *maximumImpl) Maximum() VehicleTypeExpression {
return l.maximum
}
func (l *maximumImpl) DoesStopHaveViolations(s SolutionStop) bool {
stop := s
// We check if the cumulative value is below zero or above the maximum.
// If there are stops with negative values, the cumulative value can be
// below zero. Un-planning can result in a cumulative value below zero
// therefore we need to check for this after un-planning.
cumulativeValue := stop.CumulativeValue(l.Expression())
maximum := l.maximum.Value(
stop.vehicle().ModelVehicle().VehicleType(),
nil,
nil,
)
return cumulativeValue > maximum || cumulativeValue < 0.0
}
func (l *maximumImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
moveImpl := move.(*solutionMoveStopsImpl)
if l.hasNoEffect[moveImpl.planUnit.modelPlanStopsUnit.Index()] {
return false, constNoPositionsHint
}
// All contributions to the level are negative, no need to check
// it will always be below the implied minimum level of zero.
if l.hasNegativeValues && !l.hasPositiveValues {
return true, constSkipVehiclePositionsHint
}
vehicle := moveImpl.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()
maximum := l.maximumByVehicleType[vehicleType.Index()]
expression := l.resourceExpression
if l.hasConstantExpression {
value := expression.Value(nil, nil, nil)
if value > maximum || value < 0 {
return true, constSkipVehiclePositionsHint
}
return false, constNoPositionsHint
}
// All contributions to the level are positive, it is sufficient to check
// if the delta level as a result of the move is not exceeding the maximum
// level at the end of the vehicle. We can only do this if the expression
// is a stop expression.
if l.hasStopExpressionAndNoNegativeValues {
cumulativeValue := vehicle.Last().CumulativeValue(expression)
if cumulativeValue+l.deltas[moveImpl.planUnit.modelPlanStopsUnit.Index()] > maximum {
return true, constSkipVehiclePositionsHint
}
return false, constNoPositionsHint
}
generator := newSolutionStopGenerator(*moveImpl, false, false)
defer generator.release()
previousStop, _ := generator.next()
previousModelStop := previousStop.ModelStop()
level := previousStop.CumulativeValue(expression)
for solutionStop, ok := generator.next(); ok; solutionStop, ok = generator.next() {
modelStop := solutionStop.ModelStop()
level += expression.Value(
vehicleType,
previousModelStop,
modelStop,
)
if level > maximum || level < 0 {
return true, constNoPositionsHint
}
previousStop = solutionStop
previousModelStop = modelStop
}
if !l.hasNegativeValues {
violated := level-previousStop.CumulativeValue(l.Expression())+
vehicle.Last().CumulativeValue(l.Expression()) > maximum
return violated, constNoPositionsHint
}
stop, _ := moveImpl.next()
if stop.CumulativeValue(expression) != level {
stop = stop.Next()
for !stop.IsLast() {
level += stop.Value(expression)
if level > maximum || level < 0 {
// TODO we can hint the move has to be past this stop
return true, constNoPositionsHint
}
stop = stop.Next()
}
}
return false, constNoPositionsHint
}
type maximumObjectiveDate struct {
hasViolation bool
}
func (m *maximumObjectiveDate) Copy() Copier {
return &maximumObjectiveDate{
hasViolation: m.hasViolation,
}
}
func (l *maximumImpl) UpdateObjectiveStopData(
solutionStop SolutionStop,
) (Copier, error) {
if solutionStop.IsFirst() {
return &maximumObjectiveDate{
hasViolation: false,
}, nil
}
hasViolation := solutionStop.Previous().ObjectiveData(l).(*maximumObjectiveDate).hasViolation
if !hasViolation {
maximum := l.maximumByVehicleType[solutionStop.Vehicle().ModelVehicle().VehicleType().Index()]
value := solutionStop.CumulativeValue(l.resourceExpression)
if value > maximum || value < 0 {
hasViolation = true
}
}
return &maximumObjectiveDate{
hasViolation: hasViolation,
}, nil
}
func (l *maximumImpl) EstimateDeltaValue(
move SolutionMoveStops,
) (deltaValue float64) {
moveImpl := move.(*solutionMoveStopsImpl)
if l.hasNoEffect[moveImpl.planUnit.modelPlanStopsUnit.Index()] {
return 0.0
}
vehicle := moveImpl.vehicle()
hasViolation := vehicle.Last().ObjectiveData(l).(*maximumObjectiveDate).hasViolation
vehicleType := vehicle.ModelVehicle().VehicleType()
maximum := l.maximumByVehicleType[vehicleType.Index()]
if l.hasConstantExpression {
value := l.resourceExpression.Value(nil, nil, nil)
if value > maximum {
return value - maximum + l.penaltyOffset
}
if value < 0 {
return math.Abs(value) + l.penaltyOffset
}
return 0.0
}
// All contributions to the level are positive, it is sufficient to check
// if the delta level as a result of the move is not exceeding the maximum
// level at the end of the vehicle. We can only do this if the expression
// is a stop expression.
if l.hasStopExpressionAndNoNegativeValues {
cumulativeValue := vehicle.Last().CumulativeValue(l.resourceExpression)
returnValue := 0.0
excess := cumulativeValue + l.deltas[moveImpl.planUnit.modelPlanStopsUnit.Index()] - maximum
if excess > 0 {
if !hasViolation {
returnValue += l.penaltyOffset
}
returnValue += excess
}
return returnValue
}
estimateDeltaValue := 0.0
generator := newSolutionStopGenerator(*moveImpl, false, true)
defer generator.release()
previousStop, _ := generator.next()
level := previousStop.CumulativeValue(l.resourceExpression)
for solutionStop, ok := generator.next(); ok; solutionStop, ok = generator.next() {
modelStop := solutionStop.ModelStop()
level += l.resourceExpression.Value(
vehicleType,
previousStop.ModelStop(),
modelStop,
)
if level > maximum || level < 0 {
deltaViolation := level - maximum
if solutionStop.IsPlanned() {
deltaViolation -= solutionStop.CumulativeValue(l.resourceExpression)
}
if deltaViolation > 0. {
estimateDeltaValue += deltaViolation
if !hasViolation {
estimateDeltaValue += l.penaltyOffset
hasViolation = true
}
}
}
if solutionStop == moveImpl.Next() {
if level <= solutionStop.CumulativeValue(l.resourceExpression) {
break
}
}
previousStop = solutionStop
}
return estimateDeltaValue
}
func (l *maximumImpl) Value(
solution Solution,
) (value float64) {
solutionImp := solution.(*solutionImpl)
score := 0.0
for _, vehicle := range solutionImp.vehicles {
vehicleType := vehicle.ModelVehicle().VehicleType()
maximum := l.maximumByVehicleType[vehicleType.Index()]
if l.hasStopExpressionAndNoNegativeValues {
cumulativeValue := vehicle.Last().CumulativeValue(l.resourceExpression)
excess := cumulativeValue - maximum
if excess > 0 {
score += excess
}
continue
}
for _, solutionStop := range vehicle.SolutionStops() {
solutionStop.CumulativeValue(l.resourceExpression)
excess := solutionStop.CumulativeValue(l.resourceExpression) - maximum
if excess > 0 {
score += excess
}
}
}
if score > 0 {
score += l.penaltyOffset
}
return score
}