python - PuLP:目标函数:在循环中添加多个 lpSum

标签 python linear-programming pulp objective-function

我正在尝试使用 PuLp 来解决不同元素(铁、 Mercurial ......)的混合问题。但我需要最大化限制条件的利用率,而不是最大化/最小化一些利润/成本。所以在 Excel 中我有这样的东西(伪代码):

max Sum (for each Element: (Sumproduct([DecisionVariables] * [Values]) / [MaximumAllowedValueForThisElement]))

我从未使用过这样的目标函数,但它似乎在 Excel 中有效。

现在我想在 PuLP 中模拟同样的问题。我想我需要的是这样的想法:

for Element in ELEMENTS:
    prob += lpSum(DecisionVariable[Concentrate]*dic[Element][Concentrate]/ MaxAmount[Element] for Concentrate in CONCENTRATES)

其中 ELEMENTS 是包含所有元素的列表,CONCENTRATES 是列表 0 到 100 之间的值,dic[Element][Concentrate] 存储每个元素及其所有浓缩物的值。

现在,使用上面的代码,目标函数在每个循环中都会被覆盖。我不需要覆盖旧的目标函数,而是需要像append()或类似的东西来将每个loops=lpSums添加到我的prob变量?class?

总的来说,我对编程相当陌生,我想我的问题更多地与我缺乏 python 编程技能有关,而不是我(也缺乏 :D)PuLP 技能。但我在 PuLP documentation 中找不到任何内容,至少我无法将其连接到任何东西。

编辑:包括一个小表格来展示问题:

+------------------------------+-------------------------------------------+----+------------------------------+---------------+----------------------+---------------+---------------+-------------------------------+
|       Utilization [%]        |       Sumproduct[Quantity] = [LHS]        |    | Constrains[Quantity] = [RHS] |  Concentrate  |    Element 1 [%]     | Element 2 [%] | Element 3 [%] | Decision Variables [Quantity] |
+------------------------------+-------------------------------------------+----+------------------------------+---------------+----------------------+---------------+---------------+-------------------------------+
| u1 = z1 / MaxAmount Element1 | z1 = Col Element1 * Col Decison Variables | <= | MaxAmount Element1           | Concentrate 1 | % Element 1 in Con 1 |               |               | X1                            |
| u2 = z2 / MaxAmount Element2 | z2 = Col Element2 * Col Decison Variables | <= | MaxAmount Elemen2            | Concentrate 2 | % Element 1 in Con 2 |               |               | X2                            |
| u3 = z3 / MaxAmount Element3 | z3 = Col Element3 * Col Decison Variables | <= | MaxAmount Elemen3            | Concentrate 3 | % Element 1 in Con 3 |               |               | X3                            |
+------------------------------+-------------------------------------------+----+------------------------------+---------------+----------------------+---------------+---------------+-------------------------------+

“元素 2”和“元素 3”列存储与“元素 1”列相同的信息:浓缩物 1/2/3 中相应元素的百分比份额。

目标函数是最大化所有利用率的总和 (u1+u2+u3)。因此,我试图确定每种浓缩物应该使用多少,以便尽可能多地利用每种元素的给定限制。回到我的 PuLp 代码,我认为我能够将“u1”的等效项添加到我的 PuLp“LpProblem 类”中,但我不知道如何将多个这些 LpSum 添加到我的“LpProblem 类”中环形。

最佳答案

这是一个版本,其中包含用于说明目的的虚拟数据。看看这是否对您有帮助。

import pulp
from pulp import *

ELEMENTS = ['Iron', 'Mercury', 'Silver']


Max_Per_Elem = {'Iron': 35, 
         'Mercury': 17, 
         'Silver': 28
               }

# A dictionary of the Iron percent in each of the CONCs
IronPercent = {'CONC_1': 20, 'CONC_2': 10, 'CONC_3': 25}

# A dictionary of the Hg percent in each of the CONCs
MercPercent = {'CONC_1': 15, 'CONC_2': 18, 'CONC_3': 12}

# A dictionary of the Silver percent in each of the CONCs
SilverPercent = {'CONC_1': 30,  'CONC_2': 40, 'CONC_3': 20}

CONCENTRATE_DIC = {'Iron': IronPercent,
              'Mercury': MercPercent,
              'Silver': SilverPercent              
              }

# Creates a list of Decision Variables
concs = ['CONC_1', 'CONC_2', 'CONC_3']

现在,我们准备调用 puLP 函数。

conc_vars = LpVariable.dicts("Util", concs, 0, 1.0)

# Create the 'prob' variable to contain the problem data
prob = LpProblem("Elements Concentration Problem", LpMaximize)

# The objective function
prob += lpSum([conc_vars[i] for i in concs]), "Total Utilization is maximized"

for elem in ELEMENTS:
    prob += lpSum([CONCENTRATE_DIC[elem][i]/Max_Per_Elem[elem] * conc_vars[i] for i in concs]) <= Max_Per_Elem[elem]/100, elem+"Percent"

要进行验证,您可以打印 prob 以查看其外观:

Elements Concentration Problem:
MAXIMIZE
1*Util_CONC_1 + 1*Util_CONC_2 + 1*Util_CONC_3 + 0
SUBJECT TO
IronPercent: 0.571428571429 Util_CONC_1 + 0.285714285714 Util_CONC_2
 + 0.714285714286 Util_CONC_3 <= 0.35

MercuryPercent: 0.882352941176 Util_CONC_1 + 1.05882352941 Util_CONC_2
 + 0.705882352941 Util_CONC_3 <= 0.17

SilverPercent: 1.07142857143 Util_CONC_1 + 1.42857142857 Util_CONC_2
 + 0.714285714286 Util_CONC_3 <= 0.28

VARIABLES
Util_CONC_1 <= 1 Continuous
Util_CONC_2 <= 1 Continuous
Util_CONC_3 <= 1 Continuous

一旦您对公式感到满意,就解决问题。

prob.writeLP("ElemUtiliztionModel.lp")
prob.solve()
print("Status:", LpStatus[prob.status])
for v in prob.variables():
    print(v.name, "=", v.varValue)

获取,

Status: Optimal
Util_CONC_1 = 0.0
Util_CONC_2 = 0.0
Util_CONC_3 = 0.24083333

希望能帮助您前进。

关于python - PuLP:目标函数:在循环中添加多个 lpSum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51438018/

相关文章:

python - 指纹识别系统

python - Alembic 自动生成空的 Flask-SQLAlchemy 迁移

python - PuLP 中的线性整数优化

python - 了解 Python/PuLP 代码的片段

python - 在后台执行命令

python 如何在每次循环迭代时生成新的随机数?

python - 求解大数的模线性同余

matlab - 是什么原因导致错误 "Row ' c140 8' infeasible, all entries at implied bounds."?我怎样才能在matlab中显示它?

python - 如何防止 PuLP 和 python 出现不可行错误?

python - If-Then-ElseIf-Then 在混合整数线性规划中