简体   繁体   English

如何在 PuLP 中使用变量作为除数

[英]How to use a variable as a divisor in PuLP

I was trying to resolve a LP problem with a constraint that is calculated by dividing variable A by variable B.我试图通过将变量 A 除以变量 B 来计算约束来解决 LP 问题。

The simple version of the problem is as below:问题的简单版本如下:

  1. The product is made by two materials (A and B)该产品由两种材料(A 和 B)制成

  2. % of A should be greater than 50% A 的 % 应大于 50%

  3. % of B should be less than 40% B 的百分比应小于 40%

  4. Total amount of A and B are 100 A和B的总金额为100

Objective: What's the minimum amount of A?目标:A 的最少数量是多少?

The code is like:代码是这样的:

from pulp import *

prob = LpProblem('Simple problem', LpMinimize)
x = LpVariable('x', 0, None, 'Integer')
y = LpVariable('y', 0, None, 'Integer')
prob += x
prob += x / (x + y) > 0.5  # <== Where the error happens
prob += y / (x + y) < 0.4
prob += x + y == 100
prob.solve()

print 'Result: %s' % LpStatus[prob.status]
print 'Amount of A: %s' % value(prob.objective)

However I'm getting an error message saying:但是我收到一条错误消息说:

TypeError: Expressions cannot be divided by a non-constant expression类型错误:表达式不能被非常量表达式分割

It looks like PuLP does not support variable as divisor.看起来 PuLP 不支持变量作为除数。 https://github.com/coin-or/pulp/blob/master/src/pulp/pulp.py#L800 https://github.com/coin-or/pulp/blob/master/src/pulp/pulp.py#L800

Any idea?任何的想法? If PuLP is not the right library to use, I'm happy to switch to any library that fits in.如果 PuLP 不是适合使用的库,我很乐意切换到任何适合的库。

UPDATE 27 Nov 2015 2015 年 11 月 27 日更新

For some reason, the sample above does not make sense (not working as expected).出于某种原因,上面的示例没有意义(未按预期工作)。 I am very newbie to this library.我是这个图书馆的新手。 Maybe it's not the right one to solve my problem at all.也许它根本就不是解决我问题的正确方法。 So if any one has suggestions of other libraries, it'd be appreciated.因此,如果有人对其他图书馆有建议,我们将不胜感激。

BTW, Koen Peters's advice below is great.顺便说一句,下面 Koen Peters 的建议很棒。 The error is gone after taking his advice.采纳他的建议后,错误就消失了。 Thank you.谢谢你。

Linear Programming doesn't understand divisions, hence the error :) You have to reformulate it so that the division is formulated linearly.线性规划不理解除法,因此出现错误:) 您必须重新制定它,以便线性制定除法。 In this case:在这种情况下:

prob += x / (x + y) > 0.5  
prob += y / (x + y) < 0.4

is equivalent to:相当于:

prob += x > 0.5 * (x + y)
prob += y < 0.4 * (x + y)

Which are linear constraints.哪些是线性约束。 Good luck!祝你好运!

I felt like zero shouldn't be allowed in my solution — and I included a variable that was the sum of x and y (think you're referring to it as A ).我觉得在我的解决方案中不应该允许零 - 我包含了一个变量,它是xy的总和(认为您将其称为A )。

from pulp import LpProblem, LpStatus, LpVariable
from pulp import LpMinimize, LpMaximize, lpSum, value

# I feel like zero shouldn't be allowed for lower bound...
x = LpVariable("x", lowBound=1, cat="Integer")
y = LpVariable("y", lowBound=1, cat="Integer")
z = LpVariable("z", lowBound=1, cat="Integer")

model = LpProblem("Divison problem", LpMinimize)

model += x
model += z == x + y
model += z == 100

# Rather than division, we can use multiplication
model += x >= z * 0.5
model += y <= z * 0.4

model.solve()

print(LpStatus[model.status])

print(f"""
x = {int(x.varValue):>3}  #  60
y = {int(y.varValue):>3}  #  40
z = {int(z.varValue):>3}  # 100
""")

And if而如果

prob += 5/x + 13/y > 0.5

not working:不工作:

prob += 5*y + 13*x > 0.5*x*y
TypeError: Non-constant expressions cannot be multiplied

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

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