'TypeError: list indices must be integers or slices, not list (Starting ending points as lists)
My inputs are like this, i tried to make starting and ending points to control the routing from a point a --> (special scenario of my case: routing is from location 'a' to point 'a')
I try to get a routing with capacity , distance and time windows constraints, at this level, if i execute the code, I visualise the error bellow:
''TypeError: list indices must be integers or slices, not list ''
data['time_matrix'] = [
[0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7],
[6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14],
[9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9],
[8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16],
[7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14],
[3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8],
[6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5],
[2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10],
[3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6],
[2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5],
[6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4],
[6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10],
[4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8],
[4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6],
[5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2],
[9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9],
[7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0],
]
data['time_windows'] = [
(0, 5), # depot
(7, 12), # 1
(10, 15), # 2
(16, 18), # 3
(10, 13), # 4
(0, 5), # 5
(5, 10), # 6
(0, 4), # 7
(5, 10), # 8
(0, 3), # 9
(10, 16), # 10
(10, 15), # 11
(0, 5), # 12
(5, 10), # 13
(7, 8), # 14
(10, 15), # 15
(11, 15), # 16
]
data['num_vehicles'] = 4
data['demands'] = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8]
data['vehicle_capacities'] = [15, 15, 15, 15]
data['depot'] = [ 0, 0, 0, 0]
data['ends']= [ 5, 5, 5, 5]
My code is :
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(
data['time_windows'][depot_idx][0],
data['time_windows'][depot_idx][1])
# Add time window constraints for each location except depot.
for location_idx, time_window in enumerate(data['time_windows']):
if location_idx == data['depot']:
continue
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
And when i execute the code it gives me this :
<ipython-input-10-8bb55ac15980> in main()
47 index = routing.Start(vehicle_id)
48 time_dimension.CumulVar(index).SetRange(
---> 49 data['time_windows'][depot_idx][0],
50 data['time_windows'][depot_idx][1])
51
TypeError: list indices must be integers or slices, not list
Can anyone please tell me where and what it is the problem, because I tried to make "depot_idx" as arrays but in vain ?
Solution 1:[1]
You're trying to access a list item by giving another list (depot_idx is a list):
depot_idx = data['depot'] = [ 0, 0, 0, 0]
For accessing items in a list you need to use integers or slices that are representing the indexes you want to access. In your case you need to pass an integer because your trying to access then the first element of the item (index 0):
data['time_windows'][YOUR_INTEGER][0]
Solution 2:[2]
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(
data['time_windows'][depot_idx][0],
data['time_windows'][depot_idx][1])
- Here,
depot_idxis a list. - You mismatch index and node index
so this should work:
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
start_index = routing.Start(vehicle_id) # solver index space
start_node = depot_idx[vehicle_id] # your index space or
# start_node = manager.IndexToNode(start_index)
time_dimension.CumulVar(start_index).SetRange(
data['time_windows'][start_node][0],
data['time_windows'][start_node][1])
side note: Here you have manager.IndexToNode(start_index) == start_node BUT the opposite is undefined aka you can't use manager.NodeToIndex(start_node) since the result is ambiguous (i.e. not a single integer) actually the result should be [routing.Start(v) for v in range(data['num_vehicles'])] but since API should return an integer NodeToIndex() is undefined for start/end nodes...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
| Solution 2 | Mizux |
