Python 纸浆Lp:can';t到达变量
我正在用Python纸浆构建一个lp:Python 纸浆Lp:can';t到达变量,python,pulp,Python,Pulp,我正在用Python纸浆构建一个lp: # Import the PuLP lib from pulp import * # Products list products = ["car", "cycle"] profit = {"car": 8, "cycle": 12} uses = { "Plastic": [2.0, 4.0], "Wood": [1.0, 1.
# Import the PuLP lib
from pulp import *
# Products list
products = ["car", "cycle"]
profit = {"car": 8, "cycle": 12}
uses = {
"Plastic": [2.0, 4.0],
"Wood": [1.0, 1.0],
"Steel": [1.0, 2.0]
}
# Problem variables
x = LpVariable.dicts("products ", products , 0)
# Maximise profit
prob += lpSum([profit[i] * x[i] for i in products ]), "Maximise" // This is working
prob += lpSum([uses[0][i] * x[i] for i in products]) <= 142 ,"MaxPlasticStock" // This is not working
prob.solve()
我也不能打印:(products[x])或使用任何类似于x[0]的LP变量,我发现这很奇怪:
KeyError: 0.
另一个错误,我无法调用任何决策变量,尝试调用“循环”:
如果我像这样更改最后一行,lp运行良好(但约束当然不起作用):
prob+=lpSum([x[i]表示产品中的i])类型错误:不可损坏类型:“dict”使用
是一个dict,因此您需要使用它的一个键来访问它包含的值列表:塑料
,木材
或钢
我如何调用x[0]您不能。在您的代码中,x
由列表产品
索引。因此元素x[0]
不存在。相反,您需要查找元素x[“car”]
。您也可以将其称为x[products[0]]
KeyError: 0.
prob += x[1] * 5 <= 142 ,"MaxPlasticStock"
KeyError: 1
prob += lpSum([x[i] for i in products]) <= 142 ,"MaxPlasticStock"
plasticAmount = {car : 2.0, cycle : 4.0 }
prob += lpSum([plasticAmount[i] * x[i] for i in products]) <= 142 ,"MaxPlasticStock"