gpt4 book ai didi

python - 运输优化(PuLP)

转载 作者:行者123 更新时间:2023-11-30 22:19:02 25 4
gpt4 key购买 nike

在 PuLP 的传输优化问题中:

from pulp import *
Warehouses = ["A","B"]

# Creates a dictionary for the number of units of supply for each supply node
supply = {"A": 1000,
"B": 4000}

# Creates a list of all demand nodes
Bars = ["1", "2", "3", "4", "5"]

# Creates a dictionary for the number of units of demand for each demand node
demand = {"1": 500,
"2": 900,
"3": 1800,
"4": 200,
"5": 700}
# Creates a list of costs of each transportation path
costs = [ #Bars
#1 2 3 4 5
[2,4,5,2,1],#A Warehouses
[3,1,3,2,3] #B
]
# Creates the prob variable to contain the problem data
prob = LpProblem("Beer Distribution Problem",LpMinimize)
# Creates a list of tuples containing all the possible routes for transport
Routes = [(w,b) for w in Warehouses for b in Bars]
# A dictionary called route_vars is created to contain the referenced variables (the routes)
route_vars = LpVariable.dicts("Route",(Warehouses,Bars),0,None,LpInteger)

运行时,以下代码:

# The objective function is added to prob first
prob += lpSum([route_vars[w][b]*costs[w][b] for (w,b) in Routes]), "Sum of Transporting Costs"

我收到以下错误:

TypeError: list indices must be integers, not str

最佳答案

替换

costs = [   #Bars
#1 2 3 4 5
[2,4,5,2,1],#A Warehouses
[3,1,3,2,3] #B
]

costs = { "A" : {"1" : 2, "2" : 4, "3" : 5, "4" : 2, "5" : 1 },
"B" : {"1" : 3, "2" : 1, "3" : 3, "4" : 2, "5" : 3 }}

Pulp 正在询问 costs["A"]["2"] 而不是 costs[0][1]

关于python - 运输优化(PuLP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49218316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com