【问题标题】:Pulp solver :How to set a minimal variable production using a loop纸浆求解器:如何使用循环设置最小变量生产
【发布时间】: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"
  1. 我应该如何为每个产品设置 10 的最小产值?

  2. 如果我有 200 种产品,我应该如何以更优雅的方式写这个?

  3. 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 来说似乎很正常。除了它在语法上仍然正确。

标签: python pulp


【解决方案1】:

如果x[p] 是为产品p in P 生产的单位数,那么您只需添加以下形式的约束:

x[p] >= 10  forall p in P

代码翻译:

for p in products:
   prob += x[p] >= 10, f"min production units for product {p}"

有你的约束

prob += lpSum([x[i] for i in produits]) >= 10 ,"MinProdObjs"

您的意思是,您希望所有项目的总产值至少为 10。

另请注意,您的变量目前是小数,您可能希望使用整数。

【讨论】:

  • 你好 abc !我无法表达我对您的友好回答的感激之情,我会仔细阅读并在今晚尝试一下!我真的需要你的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2016-02-29
相关资源
最近更新 更多