我需要用线性规划分析很多大小问题,但我的 PuLP 代码不能正常工作。
from pulp import *
prob = LpProblem("Lot_Sizing_Problem", LpMinimize)
period = []
for i in range(H):
period.append(i)
order = LpVariable.dicts('order', period, lowBound = 0, upBound = 1, cat='integer'),
orderamount = LpVariable.dicts('orderamount', period, lowBound = 0, cat='integer'),
inventory = LpVariable.dicts('orderamount', period, lowBound = 0, cat='integer'),
prob += lpSum([A*period[i]+h*(inventory[i]+orderamount[i]-D[i]) for i in period])
prob += (inventory[i+1] for i in period) == inventory[i]+orderamount[i]-D[i]
prob += (orderamount[i] for i in period) <= M*order[i]
prob += (orderamount[i] for i in period) == D[i] - inventory[i]
prob.solve()
我不断收到此错误:
类型错误:+ 不支持的操作数类型:“dict”和“dict”
这些是一些要输入的示例值,结果应该是 102.0:
这些是值的含义:(时间范围:H,持有成本:h,固定设置
成本:A,产能:C,需求 list :D,大M:M)
H=10
h=0.5
A=10
C=18
D=[12,10,13,14,13,15,17,20,19,14]
M=C
谢谢!
最佳答案
有不少错误。
from pulp import *
prob = LpProblem("Lot_Sizing_Problem", LpMinimize)
H=10
h=0.5
A=10
C=18
D=[12,10,13,14,13,15,17,20,19,14]
M=100
period = list(range(H))
order = LpVariable.dicts('order', period, lowBound = 0, upBound = 1, cat='integer')
orderamount = LpVariable.dicts('orderamount', period, lowBound = 0, cat='integer')
inventory = LpVariable.dicts('inventory', period, lowBound = 0, cat='integer')
prob += lpSum([A*period[i]+h*(inventory[i]+orderamount[i]-D[i]) for i in period])
for i in period:
if i<H-1:
prob += inventory[i+1] == inventory[i]+orderamount[i]-D[i]
prob += orderamount[i] <= M*order[i]
prob += orderamount[i] == D[i] - inventory[i]
prob.solve()
请注意,使用 dicts 进行整数索引不是一种正常的方法。所以,我可能会写:
order = [LpVariable(...) for i in period]
等等
关于python - 我的 PuLP (使用线性编程的批量大小)代码中不断出现错误,出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60540953/