【发布时间】:2020-01-20 02:16:47
【问题描述】:
我有这个玩具厂 LP:
# Import the PuLP lib
from pulp import *
# Products list
products = ["car", "bicycle"]
#Profit per product in $
profit = {"car": 8, "bicycle": 12}
# Used resources per product in kgs
plasticAmount = {"car": 2, "bicycle": 4}
woodAmount = {"car": 1, "bicycle": 1}
steelAmount = {"car": 3, "bicycle": 2}
# Setting Problem variables dictionary
x = LpVariable.dicts("products ", products , 0)
# The Objective function : Maximising profit in $
prob += lpSum([profit[i] * x[i] for i in products ]), "Maximise"
# Total Stock amount Constraints in kgs
prob += lpSum([plasticAmount[i] * x[i] for i in products]) <= 142 ,"MaxPlastic"
prob += lpSum([woodAmount [i] * x[i] for i in products]) <= 117 ,"MaxWood"
prob += lpSum([steelAmount[i] * x[i] for i in products]) <= 124 ,"MaxSteel"
# This constraints is not working : Minimal production amount should be at least 10 on each products ( need at least 10 Cars and 10 bicycles)
prob += lpSum([x[i] for i in produits]) >= 10 ,"MinProdObjs"
我应该如何为每个产品设置 10 的最小产值?
如果我有 200 种产品,我应该如何以更优雅的方式写这个?
Lp 是否正确?
最小生产约束:
prob += lpSum([x[i] for i in produits]) >= 10 ,"MinProdObjs"
简单的意思是(实际上,car 是“数量的汽车”,而自行车也是“数量的自行车”......也许变量名称不太好......)
prob += car + bicycle >= 10
或
prob += x1 + x2 >= 10
但它并没有按预期工作......
【问题讨论】:
-
代码中指定的
minimal production value变量在哪里? -
prob += lpSum([x[i] for i in produits]) >= 10 ,"MinProdObjs" 它是 10 ,没有变量,暂时...
-
那行对我来说没有多大意义。我不知道您正在使用的库,但是您正在向
prob添加一些内容,然后将其与一行中的元组进行比较?看起来很不对劲。您没有收到该行的错误吗? -
这是纸浆线性编程库。
-
@alec_djinn:这对于
pulp来说似乎很正常。除了它在语法上仍然正确。