繁体   English   中英

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

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

我有这个玩具厂 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"

简单的意思(实际上,汽车是“汽车数量”,自行车也是“自行车数量”......也许变量名称不太好......)

prob += car + bicycle >= 10

或者

prob += x1 + x2 >= 10

但它没有按预期工作......

如果x[p]是为p in 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。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM