Closed Mizux closed 6 years ago
We must use "index" not "node_index" for accessing/setting SlackVar/CumulVar
see modification in def add_time_window_constraints(routing, data, time_evaluator):
def add_time_window_constraints(routing, data, time_evaluator):
"""Add Global Span constraint"""
time = "Time"
horizon = 120
routing.AddDimension(
time_evaluator,
horizon, # allow waiting time
horizon, # maximum time per vehicle
True, # start cumul to zero
time)
time_dimension = routing.GetDimensionOrDie(time)
for location_idx, time_window in enumerate(data.time_windows):
if location_idx == 0:
continue
time_dimension.CumulVar(routing.NodeToIndex(location_idx)).SetRange(time_window[0], time_window[1])
routing.AddToAssignment(time_dimension.SlackVar(routing.NodeToIndex(location_idx)))
for vehicle_id in xrange(data.num_vehicles):
routing.AddToAssignment(time_dimension.SlackVar(routing.Start(vehicle_id)))
# SlackVar is not defined for vehicle ends
#routing.AddToAssignment(time_dimension.SlackVar(routing.End(vehicle_id)))
output
./test.py
vehicle: 0
idx:0 node_idx:0
slack bound: False
idx:12 node_idx:12
slack bound: False
idx:13 node_idx:13
slack bound: False
idx:15 node_idx:15
slack bound: False
idx:11 node_idx:11
slack bound: False
Route for vehicle 0:
0 Load(0) Time(0,0) Slack(1,9) -> 12 Load(0) Time(5,13) Slack(0,8) -> 13 Load(2) Time(17,25) Slack(0,10) -> 15 Load(6) Time(45,52) Slack(0,7) -> 11 Load(14) Time(88,95) Slack(0,21) -> 0 Load(15) Time(99,120)
Distance of the route: 1780m
Load of the route: 15
Time of the route: 99min
vehicle: 1
idx:17 node_idx:0
slack bound: False
idx:5 node_idx:5
slack bound: False
idx:8 node_idx:8
slack bound: False
idx:6 node_idx:6
slack bound: False
idx:2 node_idx:2
slack bound: False
Route for vehicle 1:
0 Load(0) Time(0,0) Slack(0,3) -> 5 Load(0) Time(3,6) Slack(0,3) -> 8 Load(2) Time(15,18) Slack(0,3) -> 6 Load(10) Time(57,60) Slack(0,5) -> 2 Load(14) Time(80,85) Slack(0,26) -> 0 Load(15) Time(94,120)
Distance of the route: 1712m
Load of the route: 15
Time of the route: 94min
vehicle: 2
idx:18 node_idx:0
slack bound: False
idx:7 node_idx:7
slack bound: False
idx:4 node_idx:4
slack bound: False
idx:3 node_idx:3 slack bound: False
idx:1 node_idx:1
slack bound: False
Route for vehicle 2:
0 Load(0) Time(0,0) Slack(0,3) -> 7 Load(0) Time(2,5) Slack(0,3) -> 4 Load(8) Time(46,49) Slack(0,3) -> 3 Load(12) Time(67,70) Slack(0,5) -> 1 Load(14) Time(80,85) Slack(0,29) -> 0 Load(15) Time(91,120)
Distance of the route: 1552m
Load of the route: 15
Time of the route: 91min
vehicle: 3
idx:19 node_idx:0
slack bound: False
idx:9 node_idx:9
slack bound: False
idx:14 node_idx:14
slack bound: False
idx:16 node_idx:16
slack bound: False
idx:10 node_idx:10
slack bound: False
Route for vehicle 3:
0 Load(0) Time(0,0) Slack(0,8) -> 9 Load(0) Time(2,10) Slack(0,8) -> 14 Load(1) Time(10,18) Slack(0,8) -> 16 Load(5) Time(32,40) Slack(0,9) -> 10 Load(13) Time(76,85) Slack(0,28) -> 0 Load(15) Time(92,120)
Distance of the route: 1552m
Load of the route: 15
Time of the route: 92min
Total Distance of all routes: 6596m
Total Time of all routes: 376min
-> Need to patch all examples and optimization site documentation !
It seems our code example can't be use to access SlackVar since we mismatch nodeIndex and Index.
Spotted here https://stackoverflow.com/questions/50569820/vrptw-how-to-handle-time-windows-and-slacks-for-the-special-depot-node
Test to reproduce it. Please take a look at
def add_time_window_constraints(routing, data, time_evaluator):
functionoutput: