簡體   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