简体   繁体   English

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

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

I have this Toy factory LP :我有这个玩具厂 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. How should I set a minimal production value of 10 for each product ?我应该如何为每个产品设置 10 的最小生产值?

  2. How should I write this in a more elegant manner, in case I have 200 products ?如果我有 200 个产品,我应该如何以更优雅的方式写这个?

  3. Is the Lp correct ? Lp 正确吗?

The minimal production constraint :最小生产约束:

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

simply means ( in fact, car is an "amount of cars" and bicycle is also 'an amount of bicycles'... Maybe the variables names are not so nice ...)简单的意思(实际上,汽车是“汽车数量”,自行车也是“自行车数量”......也许变量名称不太好......)

prob += car + bicycle >= 10

or或者

prob += x1 + x2 >= 10

But it doesn't work as expected ...但它没有按预期工作......

If x[p] is the number of units produced for a product p in P , then you can just add a constraint of the form:如果x[p]是为p in P的产品p in P生产的单位数,那么您可以添加以下形式的约束:

x[p] >= 10  forall p in P

Translated in code:翻译成代码:

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

With your constraint有你的约束

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

what you are saying is that you want a total of at least 10 as production value over all the items.您的意思是您希望所有项目的总产值至少为 10。

Note also that your variables are fractional at the moment and that you might want to use Integer ones.另请注意,您的变量目前是小数,您可能希望使用整数。

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

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