-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_latest.go
332 lines (278 loc) · 8.14 KB
/
model_latest.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
// © 2019-present nextmv.io inc
package nextroute
import (
"math"
"time"
)
// LatestEnd is a construct that can be added to the model as a constraint or
// as an objective. The latest end of a stop is the latest time a stop can end
// at the location of the stop.
type LatestEnd interface {
ConstraintReporter
ModelConstraint
ModelObjective
// Latest returns the latest end time expression which defines the latest
// end of a stop.
Latest() StopTimeExpression
// Lateness returns the lateness of a stop. The lateness is the difference
// between the actual end and its target end time.
Lateness(stop SolutionStop) float64
// SetFactor adds a factor with which a deviating stop is multiplied. This
// is only taken into account if the construct is used as an objective.
SetFactor(factor float64, stop ModelStop) error
// Factor returns the multiplication factor for the given stop expression.
Factor(stop ModelStop) float64
}
// LatestStart is a construct that can be added to the model as a constraint or
// as an objective. The latest start of a stop is the latest time a stop can
// start at the location of the stop.
type LatestStart interface {
ConstraintReporter
ModelConstraint
ModelObjective
// Latest returns the latest start expression which defines the latest
// start of a stop.
Latest() StopTimeExpression
// Lateness returns the lateness of a stop. The lateness is the difference
// between the actual start and its target start time.
Lateness(stop SolutionStop) float64
// SetFactor adds a factor with which a deviating stop is multiplied. This
// is only taken into account if the construct is used as an objective.
SetFactor(factor float64, stop ModelStop) error
// Factor returns the multiplication factor for the given stop expression.
Factor(stop ModelStop) float64
}
// LatestArrival is a construct that can be added to the model as a constraint
// or as an objective. The latest arrival of a stop is the latest time a stop
// can arrive at the location of the stop.
type LatestArrival interface {
ConstraintReporter
ModelConstraint
ModelObjective
// Latest returns the latest arrival expression which defines the latest
// arrival of a stop.
Latest() StopTimeExpression
// Lateness returns the lateness of a stop. The lateness is the difference
// between the actual arrival and its target arrival time.
Lateness(stop SolutionStop) float64
// SetFactor adds a factor with which a deviating stop is multiplied. This
// is only taken into account if the construct is used as an objective.
SetFactor(factor float64, stop ModelStop) error
// Factor returns the multiplication factor for the given stop expression.
Factor(stop ModelStop) float64
}
// NewLatestEnd returns a new LatestEnd construct.
func NewLatestEnd(
latestEnd StopTimeExpression,
) (LatestEnd, error) {
return &latestImpl{
modelConstraintImpl: newModelConstraintImpl(
"late_end_penalty",
ModelExpressions{},
),
latest: latestEnd,
latenessFactor: NewStopExpression("lateness_penalty_factor", 1.0),
temporalReference: OnEnd,
}, nil
}
// NewLatestStart returns a new LatestStart construct.
func NewLatestStart(
latestStart StopTimeExpression,
) (LatestStart, error) {
return &latestImpl{
modelConstraintImpl: newModelConstraintImpl(
"late_start_penalty",
ModelExpressions{},
),
latest: latestStart,
latenessFactor: NewStopExpression("lateness_penalty_factor", 1.0),
temporalReference: OnStart,
}, nil
}
// NewLatestArrival returns a new LatestArrival construct.
func NewLatestArrival(
latest StopTimeExpression,
) (LatestArrival, error) {
return &latestImpl{
modelConstraintImpl: newModelConstraintImpl(
"late_arrival_penalty",
ModelExpressions{},
),
latest: latest,
latenessFactor: NewStopExpression("lateness_penalty_factor", 1.0),
temporalReference: OnArrival,
}, nil
}
type latestImpl struct {
latest StopTimeExpression
latenessFactor StopExpression
modelConstraintImpl
temporalReference TemporalReference
}
func (l *latestImpl) SetFactor(factor float64, stop ModelStop) error {
if factor >= 0 {
return l.latenessFactor.SetValue(stop, factor)
}
return nil
}
func (l *latestImpl) Factor(stop ModelStop) float64 {
return l.latenessFactor.Value(nil, nil, stop)
}
func (l *latestImpl) ReportConstraint(stop SolutionStop) map[string]any {
var t time.Time
switch l.temporalReference {
case OnArrival:
t = stop.Arrival()
case OnStart:
t = stop.Start()
case OnEnd:
t = stop.End()
}
return map[string]any{
"latest": l.latest.Value(nil, nil, stop.ModelStop()),
"start": t,
}
}
func (l *latestImpl) String() string {
return l.name
}
func (l *latestImpl) Latest() StopTimeExpression {
return l.latest
}
func (l *latestImpl) EstimationCost() Cost {
return LinearStop
}
func (l *latestImpl) Lateness(stop SolutionStop) float64 {
latest := l.latest.Value(nil, nil, stop.ModelStop())
reference := 0.
switch l.temporalReference {
case OnArrival:
reference = stop.ArrivalValue()
case OnStart:
reference = stop.StartValue()
case OnEnd:
reference = stop.EndValue()
}
return math.Max(0, reference-latest)
}
func (l *latestImpl) Value(s Solution) float64 {
solution := s.(*solutionImpl)
value := 0.0
for _, vehicle := range solution.vehicles {
solutionStop := vehicle.First().Next()
lastSolutionStop := vehicle.Last()
for {
latenessFactor := l.latenessFactor.Value(
nil,
nil,
solutionStop.ModelStop(),
)
value += l.Lateness(solutionStop) * latenessFactor
if solutionStop == lastSolutionStop {
break
}
solutionStop = solutionStop.Next()
}
}
return value
}
func (l *latestImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
score, hint := l.estimateDeltaScore(
move.(*solutionMoveStopsImpl),
true,
)
return score != 0.0, hint.(*stopPositionHintImpl)
}
func (l *latestImpl) EstimateDeltaValue(
move SolutionMoveStops,
) float64 {
score, _ := l.estimateDeltaScore(
move.(*solutionMoveStopsImpl),
false,
)
return score
}
func (l *latestImpl) estimateDeltaScore(
move *solutionMoveStopsImpl,
asConstraint bool,
) (deltaScore float64, stopPositionsHint StopPositionsHint) {
vehicle := move.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()
deltaScore = 0.0
first := true
arrival, start, end := 0.0, 0.0, 0.0
previousStop := vehicle.First().ModelStop()
generator := newSolutionStopGenerator(*move, false, true)
defer generator.release()
for solutionStop, ok := generator.next(); ok; solutionStop, ok = generator.next() {
if first {
previousStop = solutionStop.ModelStop()
end = solutionStop.EndValue()
first = false
continue
}
modelStop := solutionStop.ModelStop()
_, arrival, start, end = vehicleType.TemporalValues(
end,
previousStop,
modelStop,
)
previousStop = modelStop
reference, currentReference := 0.0, 0.0
switch l.temporalReference {
case OnArrival:
reference = arrival
currentReference = solutionStop.ArrivalValue()
case OnStart:
reference = start
currentReference = solutionStop.StartValue()
case OnEnd:
reference = end
currentReference = solutionStop.EndValue()
}
latest := l.latest.Value(nil, nil, modelStop)
if reference <= latest {
continue
}
if asConstraint {
return 1.0, constNoPositionsHint
}
factor := l.latenessFactor.Value(nil, nil, modelStop)
violation := (reference - latest) * factor
deltaScore += violation
if !solutionStop.IsPlanned() {
continue
}
currentScore := 0.0
if currentReference > latest {
currentScore = (currentReference - latest) * factor
}
deltaScore -= currentScore
}
return deltaScore, constNoPositionsHint
}
func (l *latestImpl) DoesStopHaveViolations(s SolutionStop) bool {
stop := s
if !stop.
vehicle().
ModelVehicle().
VehicleType().
TravelDurationExpression().
SatisfiesTriangleInequality() {
latest := l.latest.Value(nil, nil, stop.modelStop())
switch l.temporalReference {
case OnArrival:
return stop.ArrivalValue() > latest
case OnStart:
return stop.StartValue() > latest
case OnEnd:
return stop.EndValue() > latest
}
}
return false
}
func (l *latestImpl) IsTemporal() bool {
return true
}