纸浆求解器:如何使用循环设置最小变量生产

Pulp solver :How to set a minimal variable production using a loop

我有这张 Toy factory 的唱片 :

# 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是一个"amount of cars",bicycle也是'an amount of bicycles'...可能变量名不太好听...)

prob += car + bicycle >= 10

prob += x1 + x2 >= 10

但它并没有像预期的那样工作...

如果 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。

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