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

2024-09-29 21:21:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个玩具工厂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是一个“车的数量”,bicycle也是一个“自行车的数量”…也许变量的名称不太好…)

prob += car + bicycle >= 10

prob += x1 + x2 >= 10

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


Tags: inforproductcaramountproductsperprofit
1条回答
网友
1楼 · 发布于 2024-09-29 21:21:18

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

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

相关问题 更多 >

    热门问题