我使用了这个代码,得到了一个错误。你能帮忙吗?
错误:
TypeError Traceback (most recent call last)
Cell In[38], line 66
64 # Define decision variables
65 x = LpVariable.dicts("x", [(i, j) for i in store_locations for j in store_locations], lowBound=0, cat='Continuous')
---> 66 y = LpVariable.dicts("y", [(i, j) for i in store_locations for j in warehouse_locations], cat='Binary')
67 t = LpVariable.dicts("t", [(i) for i in store_locations], lowBound=0, cat='Continuous')
69 # Define the objective function
File ~AppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagespulppulp.py:418, in LpVariable.dicts(cls, name, indices, lowBound, upBound, cat, indexStart, indexs)
416 if len(indices) == 0:
417 for i in index:
--> 418 d[i] = LpVariable(
419 name % tuple(indexStart + [str(i)]), lowBound, upBound, cat
420 )
421 else:
422 for i in index:
TypeError: unhashable type: 'list'
这是我使用的代码
from pulp import *
# Store locations
store_locations = {
1: (54.13917237, 10.16301447),
2: (5.163117855, 18.37136899),
3: (58.10773412, 97.325872),
4: (16.60663876, 73.67529068),
5: (10.92151473, 26.90262604),
6: (40.2830426, 49.00596112),
7: (95.90679501, 31.25379214),
8: (38.80688537, 61.84963412),
9: (50.77362816, 41.71229884),
10: (16.12104517, 4.174152112),
11: (30.57252604, 25.66416139),
12: (88.65418483, 28.60340585),
13: (76.15833298, 52.06010201),
14: (57.99079029, 58.29016932),
15: (80.11801005, 80.74242101),
16: (90.87902761, 90.97690565),
17: (2.587207522, 45.80849397),
18: (74.04980053, 2.986131158),
19: (71.70911665, 37.25817058),
20: (22.37191714, 94.53074723),
21: (24.10112206, 11.06658123),
22: (42.27800052, 9.141860385),
23: (58.3280586, 76.15907608),
'W1': (71.25359844, 18.21268482),
'W2': (57.24219473, 82.66876117)
}
# Distance matrix
warehouse_locations = [
[0, 50, 87, 74, 46, 41, 47, 54, 32, 38, 28, 39, 47, 48, 75, 89, 63, 21, 32, 90, 30, 12, 51, 14],
[50, 0, 95, 56, 10, 47, 92, 55, 51, 18, 26, 84, 79, 66, 98, 112, 28, 71, 69, 78, 20, 38, 65, 52],
[87, 95, 0, 48, 85, 52, 76, 40, 56, 102, 77, 75, 49, 39, 28, 33, 76, 96, 62, 36, 93, 90, 25, 66],
[74, 56, 48, 0, 47, 34, 90, 25, 47, 70, 50, 85, 63, 44, 64, 76, 31, 91, 71, 54, 72, 84, 39, 56],
[46, 10, 85, 47, 0, 38, 84, 50, 45, 15, 22, 74, 69, 56, 88, 101, 22, 62, 60, 68, 13, 33, 57, 43],
[41, 47, 52, 34, 38, 0, 57, 21, 12, 53, 32, 64, 44, 31, 47, 61, 34, 60, 38, 16, 53, 47, 15, 17],
[47, 92, 76, 90, 84, 57, 0, 67, 73, 85, 68, 9, 28, 50, 28, 10, 73, 54, 63, 24, 83, 80, 44, 68],
[54, 55, 40, 25, 50, 21, 67, 0, 23, 54, 35, 51, 29, 16, 29, 44, 19, 69, 45, 23, 63, 59, 15, 34],
[32, 51, 56, 47, 45, 12, 73, 23, 0, 46, 25, 53, 31, 23, 47, 60, 33, 54, 27, 16, 46, 44, 6, 22],
[38, 18, 102, 70, 15, 53, 85, 54, 46, 0, 21, 79, 73, 61, 93, 107, 33, 85, 73, 81, 31, 47, 78, 67],
[28, 26, 77, 50, 20, 25, 66, 37, 26, 26, 0, 58, 53, 43, 74, 89, 34, 49, 43, 69],
[39, 84, 75, 85, 78, 52, 8, 60, 77, 77, 58, 0, 27, 43, 53, 62, 88, 29, 19, 69],
[47, 79, 49, 63, 70,36, 29, 39, 27, 77, 53, 27, 0],
[48, 66, 39, 44, 57, 20, 47, 20, 18, 68, 43, 43, 19, 0, 0],
[75, 98, 28, 64, 88, 51, 52, 45, 49, 100, 74, 53, 29, 32, 0, 0],
[89, 112, 33, 76, 102, 66, 60, 60, 64, 115, 89, 62, 42, 46, 15, 0],
[63, 28, 76, 31, 21, 38, 94, 40, 48, 44, 34, 88, 74, 57, 85, 99, 0],
[21, 71, 96, 91, 68, 57, 36, 69, 45, 58, 49, 29, 49, 58, 78, 90, 83, 0],
[32, 69, 62, 66, 62, 34, 25, 41, 21, 65, 43, 19, 15, 25, 44, 57, 70, 34, 0],
[90, 78, 36, 22, 69, 49, 97, 37, 60, 91, 69, 93, 69, 51, 59, 69, 53, 105, 76, 0],
[30, 20, 93, 63, 21, 41, 75, 53, 41, 11, 16, 67, 66, 58, 89, 104, 41, 51, 54, 83, 0],
[12, 38, 90, 69, 36, 40, 58, 53, 34, 27, 20, 50, 55, 52, 81, 95, 54, 32, 41, 88, 0],
[66, 79, 21, 42, 68, 33, 59, 24, 35, 83, 58, 56, 30, 18, 22, 36, 63, 75, 41, 40, 0],
[19, 66, 80, 78, 61, 44, 28, 54, 31, 57, 41, 20, 34, 42, 63, 75, 74, 15, 19, 91, 0],
[73, 83, 15, 42, 72, 38, 64, 28, 41, 89, 63, 63, 36, 24, 23, 35, 66, 81, 48, 37, 0, 0]
]
# Define the problem
prob = LpProblem("Store_Location_Problem", LpMinimize)
# Define decision variables
x = LpVariable.dicts("x", [(i, j) for i in store_locations for j in store_locations], lowBound=0, cat='Continuous')
y = LpVariable.dicts("y", [(i, j) for i in store_locations for j in warehouse_locations], cat='Binary')
t = LpVariable.dicts("t", [(i) for i in store_locations], lowBound=0, cat='Continuous')
# Define the objective function
prob += lpSum([distances[i-1][j-1]*x[(i,j)] for i in store_locations for j in store_locations])
# Define the constraints
for i in store_locations:
prob += lpSum([x[(i,j)] for j in store_locations]) == 1
for j in warehouse_locations:
prob += lpSum([y[(i,j)] for i in store_locations]) == 2
for i in store_locations:
for j in warehouse_locations:
prob += x[(i,j)] <= y[(i,j)]*100
for i in store_locations:
prob += lpSum([x[(i,j)] for j in store_locations]) - lpSum([x[(j,i)] for j in store_locations]) == 0
for j in warehouse_locations:
prob += lpSum([x[(i,j)] for i in store_locations]) - lpSum([x[(j,i)] for i in store_locations]) == 0
for i in store_locations:
prob += t[i] >= 0
prob += t[i] <= 300
for i in store_locations:
for j in store_locations:
if i != j:
prob += t[i] + distances[i-1][j-1] - t[j] <= 300*(1 - x[(i,j)])
# Solve the problem
prob.solve()
# Print the results
print("Status: ", LpStatus[prob.status])
print("Total Cost of Transportation = ", value(prob.objective))
for v in prob.variables():
if v.varValue > 0:
print(v.name, "=", v.varValue)
你的代码并不像你想象的那样。这个错误是完全正确的,看看输出:
# Store locations
store_locations = {
1: (54.13917237, 10.16301447),
2: (5.163117855, 18.37136899),
3: (58.10773412, 97.325872),
4: (16.60663876, 73.67529068),
5: (10.92151473, 26.90262604),
6: (40.2830426, 49.00596112),
7: (95.90679501, 31.25379214),
8: (38.80688537, 61.84963412),
9: (50.77362816, 41.71229884),
10: (16.12104517, 4.174152112),
11: (30.57252604, 25.66416139),
12: (88.65418483, 28.60340585),
13: (76.15833298, 52.06010201),
14: (57.99079029, 58.29016932),
15: (80.11801005, 80.74242101),
16: (90.87902761, 90.97690565),
17: (2.587207522, 45.80849397),
18: (74.04980053, 2.986131158),
19: (71.70911665, 37.25817058),
20: (22.37191714, 94.53074723),
21: (24.10112206, 11.06658123),
22: (42.27800052, 9.141860385),
23: (58.3280586, 76.15907608),
'W1': (71.25359844, 18.21268482),
'W2': (57.24219473, 82.66876117)
}
# Distance matrix
warehouse_locations = [
[0, 50, 87, 74, 46, 41, 47, 54, 32, 38, 28, 39, 47, 48, 75, 89, 63, 21, 32, 90, 30, 12, 51, 14],
[50, 0, 95, 56, 10, 47, 92, 55, 51, 18, 26, 84, 79, 66, 98, 112, 28, 71, 69, 78, 20, 38, 65, 52],
[87, 95, 0, 48, 85, 52, 76, 40, 56, 102, 77, 75, 49, 39, 28, 33, 76, 96, 62, 36, 93, 90, 25, 66],
[74, 56, 48, 0, 47, 34, 90, 25, 47, 70, 50, 85, 63, 44, 64, 76, 31, 91, 71, 54, 72, 84, 39, 56],
[46, 10, 85, 47, 0, 38, 84, 50, 45, 15, 22, 74, 69, 56, 88, 101, 22, 62, 60, 68, 13, 33, 57, 43],
[41, 47, 52, 34, 38, 0, 57, 21, 12, 53, 32, 64, 44, 31, 47, 61, 34, 60, 38, 16, 53, 47, 15, 17],
[47, 92, 76, 90, 84, 57, 0, 67, 73, 85, 68, 9, 28, 50, 28, 10, 73, 54, 63, 24, 83, 80, 44, 68],
[54, 55, 40, 25, 50, 21, 67, 0, 23, 54, 35, 51, 29, 16, 29, 44, 19, 69, 45, 23, 63, 59, 15, 34],
[32, 51, 56, 47, 45, 12, 73, 23, 0, 46, 25, 53, 31, 23, 47, 60, 33, 54, 27, 16, 46, 44, 6, 22],
[38, 18, 102, 70, 15, 53, 85, 54, 46, 0, 21, 79, 73, 61, 93, 107, 33, 85, 73, 81, 31, 47, 78, 67],
[28, 26, 77, 50, 20, 25, 66, 37, 26, 26, 0, 58, 53, 43, 74, 89, 34, 49, 43, 69],
[39, 84, 75, 85, 78, 52, 8, 60, 77, 77, 58, 0, 27, 43, 53, 62, 88, 29, 19, 69],
[47, 79, 49, 63, 70,36, 29, 39, 27, 77, 53, 27, 0],
[48, 66, 39, 44, 57, 20, 47, 20, 18, 68, 43, 43, 19, 0, 0],
[75, 98, 28, 64, 88, 51, 52, 45, 49, 100, 74, 53, 29, 32, 0, 0],
[89, 112, 33, 76, 102, 66, 60, 60, 64, 115, 89, 62, 42, 46, 15, 0],
[63, 28, 76, 31, 21, 38, 94, 40, 48, 44, 34, 88, 74, 57, 85, 99, 0],
[21, 71, 96, 91, 68, 57, 36, 69, 45, 58, 49, 29, 49, 58, 78, 90, 83, 0],
[32, 69, 62, 66, 62, 34, 25, 41, 21, 65, 43, 19, 15, 25, 44, 57, 70, 34, 0],
[90, 78, 36, 22, 69, 49, 97, 37, 60, 91, 69, 93, 69, 51, 59, 69, 53, 105, 76, 0],
[30, 20, 93, 63, 21, 41, 75, 53, 41, 11, 16, 67, 66, 58, 89, 104, 41, 51, 54, 83, 0],
[12, 38, 90, 69, 36, 40, 58, 53, 34, 27, 20, 50, 55, 52, 81, 95, 54, 32, 41, 88, 0],
[66, 79, 21, 42, 68, 33, 59, 24, 35, 83, 58, 56, 30, 18, 22, 36, 63, 75, 41, 40, 0],
[19, 66, 80, 78, 61, 44, 28, 54, 31, 57, 41, 20, 34, 42, 63, 75, 74, 15, 19, 91, 0],
[73, 83, 15, 42, 72, 38, 64, 28, 41, 89, 63, 63, 36, 24, 23, 35, 66, 81, 48, 37, 0, 0]
]
a = [(i, j) for i in store_locations for j in warehouse_locations]
print(a)
我不太清楚你到底想要什么,但第一个结果是(1, [0, 50, 87, 74, 46, 41, 47, 54, 32, 38, 28, 39, 47, 48, 75, 89, 63, 21, 32, 90, 30, 12, 51, 14])
,你不能在字典键中有一个列表。此外,标准库需要一个字符串列表,这与您提供给它的字符串列表相差甚远。
我认为你的数据结构设置不正确。没有关于warehouse_location
结构的信息。它是距离矩阵;但是从什么到什么并不清楚,也不清楚为什么行(假设)的长度不同。
所以,这可能需要修正
在导致错误的行(第66行)中,您正在传递"forwarehouse_locations
中的j
…"但是,warehouse_locations
是一个列表的列表,所以你在这里传递的是一个完整的列表(子列表之一),这是不合法的