-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution_move_stops.go
638 lines (557 loc) · 18.2 KB
/
solution_move_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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// © 2019-present nextmv.io inc
package nextroute
import (
"context"
"fmt"
"github.com/nextmv-io/nextroute/common"
)
// Move is a type alias for SolutionMoveStops. It is used to make the
// custom constraints and observers backward compatible.
type Move = SolutionMoveStops
// SolutionMoveStops is a move in a solution. A move is a change in the solution that
// can be executed. A move can be executed which may or may not result in a
// change in the solution. A move can be asked if it is executable, it is not
// executable if it is incomplete or if it is not executable because the move is
// not allowed. A move can be asked if it is an improvement, it is an
// improvement if it is executable and the move has a value less than zero.
// A move describes the change in the solution. The change in the solution
// is described by the stop positions. The stop positions describe for each
// stop in the associated units where in the existing solution the stop
// is supposed to be placed. The stop positions are ordered by the order
// of the stops in the unit. The first stop in the unit is the first
// stop in the stop positions. The last stop in the unit is the last
// stop in the stop positions.
type SolutionMoveStops interface {
SolutionMove
// Previous returns previous stop of the first to be planned
// stop if it would be planned. Previous is the same stop as the
// previous stop of the first stop position.
Previous() SolutionStop
// Next returns the next stop of the last to be planned
// stop if it would be planned. Next is the same stop as the
// next stop of the last stop position.
Next() SolutionStop
// PlanStopsUnit returns the [SolutionPlanStopsUnit] that is affected by the move.
PlanStopsUnit() SolutionPlanStopsUnit
// StopPositions returns the [StopPositions] that define the move and
// how it will change the solution.
StopPositions() StopPositions
// StopPositionAt returns the stop position at the given index. The index
// is the index is based on the positions in the move. Get the length of
// the stop positions from [StopPositionsLength].
StopPositionAt(index int) StopPosition
// StopPositionsLength returns the length of the stop positions.
StopPositionsLength() int
// Vehicle returns the vehicle, if known, that is affected by the move. If
// not known, nil is returned.
Vehicle() SolutionVehicle
// Solution returns the solution that is affected by the move.
Solution() Solution
}
// StopPosition is the definition of the change in the solution for a
// specific stop. The change is defined by a Next and a Stop. The
// Next is a stop which is already part of the solution (it is planned)
// and the Stop is a stop which is not yet part of the solution (it is not
// planned). A stop position states that the stop should be moved from the
// unplanned set to the planned set by positioning it directly before the
// Next.
type StopPosition struct {
solution *solutionImpl
previousStopIndex int
stopIndex int
nextStopIndex int
}
// StopPositions is a slice of stop positions.
type StopPositions []StopPosition
func newNotExecutableSolutionMoveStops(planUnit *solutionPlanStopsUnitImpl) *solutionMoveStopsImpl {
return &solutionMoveStopsImpl{
planUnit: planUnit,
valueSeen: 1,
allowed: false,
}
}
type solutionMoveStopsImpl struct {
planUnit *solutionPlanStopsUnitImpl
stopPositions []StopPosition
valueSeen int
value float64
allowed bool
}
// reset resets the move to its initial state.
// We assume the planUnit stays the same.
func (m *solutionMoveStopsImpl) reset() {
m.stopPositions = m.stopPositions[:0]
m.allowed = false
m.value = 0.0
m.valueSeen = 1
}
// replaceBy replaces the move by the new move.
// We assume the planunit and solution stays the same.
func (m *solutionMoveStopsImpl) replaceBy(newStop *solutionMoveStopsImpl, newValueSeen int) {
m.reset()
m.value = newStop.value
m.valueSeen = newValueSeen
m.allowed = newStop.allowed
m.stopPositions = append(m.stopPositions, newStop.stopPositions...)
}
func (m *solutionMoveStopsImpl) String() string {
return fmt.Sprintf("move{%v, vehicle=%v, %v, valueSeen=%v, value=%v, allowed=%v}",
m.planUnit,
m.Vehicle().Index(),
m.stopPositions,
m.valueSeen,
m.value,
m.allowed,
)
}
func (m *solutionMoveStopsImpl) Solution() Solution {
return m.planUnit.solution()
}
func (m *solutionMoveStopsImpl) Vehicle() SolutionVehicle {
if len(m.stopPositions) == 0 {
return SolutionVehicle{}
}
return m.stopPositions[len(m.stopPositions)-1].Next().Vehicle()
}
func (m *solutionMoveStopsImpl) vehicle() SolutionVehicle {
return m.stopPositions[len(m.stopPositions)-1].Next().vehicle()
}
func (m *solutionMoveStopsImpl) Next() SolutionStop {
if next, ok := m.next(); ok {
return next
}
return SolutionStop{}
}
func (m *solutionMoveStopsImpl) next() (SolutionStop, bool) {
if len(m.stopPositions) == 0 {
return SolutionStop{}, false
}
return m.stopPositions[len(m.stopPositions)-1].Next(), true
}
func (m *solutionMoveStopsImpl) Previous() SolutionStop {
previous, ok := m.previous()
if !ok {
return SolutionStop{}
}
return previous
}
func (m *solutionMoveStopsImpl) previous() (SolutionStop, bool) {
if len(m.stopPositions) == 0 {
return SolutionStop{}, false
}
return m.stopPositions[0].Previous(), true
}
func (m *solutionMoveStopsImpl) Execute(_ context.Context) (bool, error) {
if !m.IsExecutable() {
return false, nil
}
m.planUnit.solution().model.OnPlan(m)
if _, isElementOfPlanUnitsUnit := m.planUnit.ModelPlanUnit().PlanUnitsUnit(); !isElementOfPlanUnitsUnit {
m.planUnit.solution().unPlannedPlanUnits.remove(m.planUnit)
m.planUnit.solution().plannedPlanUnits.add(m.planUnit)
}
startPropagate, err := m.attach()
if err != nil {
return false, err
}
constraint, _, err := m.planUnit.solution().isFeasible(startPropagate, true)
if err != nil {
return false, err
}
if constraint != nil {
m.planUnit.solution().model.OnPlanFailed(m, constraint)
if _, isElementOfPlanUnitsUnit := m.planUnit.ModelPlanUnit().PlanUnitsUnit(); !isElementOfPlanUnitsUnit {
m.planUnit.solution().unPlannedPlanUnits.add(m.planUnit)
m.planUnit.solution().plannedPlanUnits.remove(m.planUnit)
}
for _, position := range m.stopPositions {
position.Stop().detach()
}
constraint, _, err := m.planUnit.solution().isFeasible(startPropagate, true)
if err != nil {
return false, err
}
if constraint != nil {
return false, fmt.Errorf(
"undoing failed solutionMoveStopsImpl %v failed: %v",
m, constraint,
)
}
return false, nil
}
m.planUnit.solution().model.OnPlanSucceeded(m)
return true, nil
}
func (m *solutionMoveStopsImpl) attach() (int, error) {
startPropagate := -1
for i := len(m.stopPositions) - 1; i >= 0; i-- {
stopPosition := m.stopPositions[i]
m.planUnit.solutionStops[i] = stopPosition.Stop()
beforeStop := stopPosition.Next()
if stopPosition.Stop().IsPlanned() {
return -1, fmt.Errorf(
"stop %v is already planned",
stopPosition.Stop(),
)
}
if beforeStop.IsFirst() {
return -1, fmt.Errorf(
"nextStop %v is first",
beforeStop,
)
}
startPropagate = stopPosition.Stop().attach(
beforeStop.PreviousIndex(),
)
}
return startPropagate, nil
}
func (m *solutionMoveStopsImpl) PlanUnit() SolutionPlanUnit {
return m.planUnit
}
func (m *solutionMoveStopsImpl) PlanStopsUnit() SolutionPlanStopsUnit {
if m.planUnit == nil {
return nil
}
return m.planUnit
}
func (m *solutionMoveStopsImpl) Value() float64 {
return m.value
}
func (m *solutionMoveStopsImpl) ValueSeen() int {
return m.valueSeen
}
func (m solutionMoveStopsImpl) IncrementValueSeen(inc int) SolutionMove {
m.valueSeen += inc
return &m
}
func (m *solutionMoveStopsImpl) StopPositions() StopPositions {
stopPositions := make(StopPositions, len(m.stopPositions))
copy(stopPositions, m.stopPositions)
return stopPositions
}
func (m *solutionMoveStopsImpl) StopPositionAt(index int) StopPosition {
return m.stopPositions[index]
}
func (m *solutionMoveStopsImpl) StopPositionsLength() int {
return len(m.stopPositions)
}
func (m *solutionMoveStopsImpl) IsExecutable() bool {
return m.stopPositions != nil &&
!m.planUnit.IsPlanned() &&
m.allowed &&
!m.planUnit.IsFixed()
}
func (m *solutionMoveStopsImpl) IsImprovement() bool {
return m.IsExecutable() && m.value < 0
}
func (m *solutionMoveStopsImpl) TakeBest(that SolutionMove) SolutionMove {
if !that.IsExecutable() {
return m
}
if !m.IsExecutable() {
return that
}
if m.value > that.Value() {
return that
}
if m.value < that.Value() {
return m
}
if m.planUnit.solution().random.Intn(m.ValueSeen()+that.ValueSeen()) == 0 {
m.valueSeen++
return m
}
return that.IncrementValueSeen(m.ValueSeen())
}
// deltaStopTravelDurationValue computes the sume of deltaStopDurationValue()
// and deltaTravelDurationValue() in one pass.
// This is more efficient than calling the two functions separately.
// But only call it if the travel time is not dependent on time.
func (m *solutionMoveStopsImpl) deltaStopTravelDurationValue(
vehicleType ModelVehicleType,
) float64 {
if len(m.stopPositions) == 0 || m.stopPositions[0].Stop().IsPlanned() {
return 0
}
deltaStopDurationValue := 0.0
travelDuration := 0.0
vehicleTravelDuration := vehicleType.TravelDurationExpression()
vehicleDuration := vehicleType.DurationExpression()
for _, stopPosition := range m.stopPositions {
modelStop := stopPosition.Stop().ModelStop()
nextStop := stopPosition.Next().ModelStop()
previousStop := stopPosition.Previous().ModelStop()
if stopPosition.Next().IsPlanned() {
deltaStopDurationValue -= stopPosition.Next().DurationValue()
travelDuration -= stopPosition.Next().TravelDurationValue()
travelDuration += vehicleTravelDuration.Value(
vehicleType,
modelStop,
nextStop,
)
}
deltaStopDurationValue += vehicleDuration.Value(
vehicleType,
previousStop,
modelStop,
)
deltaStopDurationValue += vehicleDuration.Value(
vehicleType,
modelStop,
nextStop,
)
travelDuration += vehicleTravelDuration.Value(
vehicleType,
previousStop,
modelStop,
)
}
return deltaStopDurationValue + travelDuration
}
func (m *solutionMoveStopsImpl) deltaTravelDurationValue() float64 {
if len(m.stopPositions) == 0 || m.stopPositions[0].Stop().IsPlanned() {
return 0
}
vehicle := m.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()
isDependentOnTime := vehicleType.TravelDurationExpression().IsDependentOnTime()
if isDependentOnTime {
if len(m.stopPositions) == 1 {
solutionStop := m.stopPositions[0].Stop()
previousStop, _ := m.previous()
departure := previousStop.EndValue()
fromDuration, _, _, _ := vehicleType.TemporalValues(
departure,
previousStop.ModelStop(),
solutionStop.ModelStop(),
)
nextStop, _ := m.next()
toDuration, _, _, _ := vehicleType.TemporalValues(
departure,
solutionStop.ModelStop(),
nextStop.ModelStop(),
)
return fromDuration + toDuration - nextStop.TravelDurationValue()
}
newTravelDuration := 0.0
generator := newSolutionStopGenerator(
*m,
false,
isDependentOnTime,
)
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(),
)
newTravelDuration += travelDuration
previousStop = solutionStop
departure = end
}
currentTravelDuration := previousStop.CumulativeTravelDurationValue() -
m.Previous().CumulativeTravelDurationValue()
return newTravelDuration - currentTravelDuration
}
travelDuration := 0.0
for _, stopPosition := range m.stopPositions {
modelStop := stopPosition.Stop().ModelStop()
if stopPosition.Next().IsPlanned() {
travelDuration -= stopPosition.Next().TravelDurationValue()
travelDuration += vehicleType.TravelDurationExpression().Value(
vehicleType,
modelStop,
stopPosition.Next().ModelStop(),
)
}
travelDuration += vehicleType.TravelDurationExpression().Value(
vehicleType,
stopPosition.Previous().ModelStop(),
modelStop,
)
}
return travelDuration
}
// NewMoveStops creates a new move and checks if the move is allowed and if so
// estimates the delta score.
func NewMoveStops(
planUnit SolutionPlanStopsUnit,
stopPositions StopPositions,
) (SolutionMoveStops, error) {
return newMoveStops(planUnit, stopPositions, true)
}
// newMoveStops creates a new move.
func newMoveStops(
planUnit SolutionPlanStopsUnit,
stopPositions StopPositions,
checkConstraintsAndEstimateDeltaScore bool,
) (SolutionMoveStops, error) {
if planUnit == nil {
return nil, fmt.Errorf("planUnit is nil")
}
if len(stopPositions) != len(planUnit.SolutionStops()) {
return nil, fmt.Errorf(
"stopPositions length %v must be equal to planUnit.SolutionStops length %v",
len(stopPositions),
len(planUnit.SolutionStops()),
)
}
if !stopPositions[0].Previous().IsPlanned() {
return nil, fmt.Errorf("previous stop %s of first stop position must be planned",
stopPositions[0].Previous().ModelStop().ID(),
)
}
if !stopPositions[len(stopPositions)-1].Next().IsPlanned() {
return nil, fmt.Errorf("next stop %s of last stop positions must be planned",
stopPositions[len(stopPositions)-1].Next().ModelStop().ID(),
)
}
stops := common.Map(stopPositions, func(i StopPosition) ModelStop {
return i.Stop().ModelStop()
})
allowed, err := planUnit.ModelPlanStopsUnit().DirectedAcyclicGraph().IsAllowed(stops)
if err != nil {
return nil, err
}
if !allowed {
return nil, fmt.Errorf(
"move is not allowed, " +
"the stops are in a sequence violating the Directed Acyclic Graph of the plan unit",
)
}
vehicle := stopPositions[0].Previous().vehicle()
lastPlannedPreviousStop := stopPositions[0].Previous()
position := stopPositions[0].Previous().Position()
for index, sp := range stopPositions {
stopPosition := sp
if stopPosition.Stop().PlanStopsUnit() != planUnit {
return nil,
fmt.Errorf(
"stop %v of stop position %v is not part of the provided plan unit",
stopPosition.Stop().ModelStop().ID(),
index,
)
}
if stopPosition.Stop().IsPlanned() {
return nil,
fmt.Errorf(
"stop %v of stop position %v is already planned",
stopPosition.Stop().ModelStop().ID(),
index,
)
}
if stopPosition.Previous().IsPlanned() {
if stopPosition.Previous().Position() < position {
return nil,
fmt.Errorf("previous stop %s of stop position %v is planned at position %v,"+
" which is before or at the last planned previous stop %s at position %v",
stopPosition.Previous().ModelStop().ID(),
index,
stopPosition.Previous().Position(),
lastPlannedPreviousStop.ModelStop().ID(),
lastPlannedPreviousStop.Position(),
)
}
position = stopPosition.Previous().Position()
lastPlannedPreviousStop = stopPosition.Previous()
}
if stopPosition.Next().IsPlanned() {
if stopPosition.Next().Position() < position {
return nil,
fmt.Errorf("next stop %s of stop position %v is planned at position %v,"+
" which is before or at the last planned previous stop %s at position %v",
stopPosition.Next().ModelStop().ID(),
index,
stopPosition.Next().Position(),
lastPlannedPreviousStop.ModelStop().ID(),
lastPlannedPreviousStop.Position(),
)
}
position = stopPosition.Next().Position()
}
if stopPosition.Next().IsPlanned() && !stopPosition.Previous().IsPlanned() {
if lastPlannedPreviousStop.Position() != stopPosition.Next().Position()-1 {
return nil,
fmt.Errorf("stop positions are not allowed, planned previous stop %v is not adjacent"+
" to the planned next stop %v of the next stop position",
lastPlannedPreviousStop.ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
)
}
}
if stopPosition.Next().IsPlanned() && stopPosition.Previous().IsPlanned() {
if stopPosition.Next().Position() != stopPosition.Previous().Position()+1 {
return nil,
fmt.Errorf("stop positions are not allowed, planned previous stop %v is not adjacent"+
" to the planned next stop %v of stop position %v",
stopPosition.Previous().ModelStop().ID(),
stopPosition.Next().ModelStop().ID(),
index,
)
}
}
if !stopPosition.Previous().IsPlanned() {
if stopPositions[index-1].Stop() != stopPosition.Previous() {
return nil,
fmt.Errorf("the previous stop %s of stop position %v"+
" must be the stop %s of the previous stop position %v if it is unplanned",
stopPosition.Previous().ModelStop().ID(),
index,
stopPositions[index-1].Stop().ModelStop().ID(),
index-1,
)
}
}
if !stopPosition.Next().IsPlanned() {
if stopPositions[index+1].Stop() != stopPosition.Next() {
return nil,
fmt.Errorf("the next stop %s of stop position %v"+
" must be the stop %s of the next stop position %v if it is unplanned",
stopPosition.Next().ModelStop().ID(),
index,
stopPositions[index+1].Stop().ModelStop().ID(),
index+1,
)
}
}
if stopPosition.Previous().IsPlanned() && stopPosition.Previous().vehicle().index != vehicle.index {
return nil,
fmt.Errorf(
"planned previous stop %v of stop position %v vehicle mismatch: %v != %v",
stopPosition.Previous().ModelStop().ID(),
index,
stopPosition.Previous().vehicle().ModelVehicle().ID(),
vehicle.ModelVehicle().ID(),
)
}
if stopPosition.Next().IsPlanned() && stopPosition.Next().vehicle().index != vehicle.index {
return nil,
fmt.Errorf(
"planned next stop %v of stop position %v vehicle mismatch: %v != %v",
stopPosition.Next().ModelStop().ID(),
index,
stopPosition.Next().vehicle().ModelVehicle().ID(),
vehicle.ModelVehicle().ID(),
)
}
}
stopPositionsImpl := make([]StopPosition, len(stopPositions))
copy(stopPositionsImpl, stopPositions)
move := &solutionMoveStopsImpl{
planUnit: planUnit.(*solutionPlanStopsUnitImpl),
stopPositions: stopPositionsImpl,
value: 0.0,
valueSeen: 0,
allowed: true,
}
if checkConstraintsAndEstimateDeltaScore {
value, allowed, _ := planUnit.(*solutionPlanStopsUnitImpl).solution().checkConstraintsAndEstimateDeltaScore(move)
move.value = value
move.allowed = allowed
move.valueSeen = 1
}
return move, nil
}