使用PuLP进行线性优化,对变量附加条件 [英] Linear optimization with PuLP, additional condition on variables

查看:180
本文介绍了使用PuLP进行线性优化,对变量附加条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用纸浆解决Python中的整数线性优化问题. 我解决了基本问题,现在我必须添加其他约束. 是否有人可以通过逻辑指标帮助我添加条件? 逻辑限制是:如果A> 20

,则B> 5

这是我的代码:

from pulp import *

prob = LpProblem("The Optimization Problem", LpMaximize)
A = LpVariable("A", 0, 100)
B = LpVariable("B", 0, 200)
C = LpVariable("C", 0, 100)
R1 = LpVariable("R1", 0)
R2 = LpVariable("R2", 0)

R1 = 0.1 * A + 0.2 * B + 0.075 * C
R2 = 0.05 * A + 0.1 * B + 0.05 * C

prob += 2 * A + 3 * B + 2.55 * C - 0.6 * R1 - 0.8 * R2
prob += 0.5 * A + 0.8 * B + C <= 100, "T1"
prob += 0.8 * A + 0.6 * B + 0.2 * C <= 100, "T2"


prob.writeLP("OptimizationModel.lp")

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

解决方案

我认为您可以在f中创建一个二进制"on-off"变量,这是正确的选择.

f = LpVariable('f',0,1,cat='Integer')

如果我理解正确,只要A> 20,就需要B> 5.

然后,您需要调整代码,只要A大于20,就将f设置为1.

由于f为二进制,因此您可以执行以下操作:

prob+= f>= (A-20)/80
prob+= B>= 6*f

在第一行中,对于从0到19的A值,(A-20)/80将为负,当A为20时将为零.这将确保对于这些A值,f为零. /p>

当f为零时,第二个约束仅暗示B必须至少为零,这始终是其下界.

但是,如果A为21或更高,即A> 20,则(A-20)/80变为正数,但绝不大于1(稍后将对此进行详细说明).然后,只要A为21或更高,这将迫使f至少为1.由于f只能为1或0,因此f设置为1.

这将导致第二个约束,在B等于1时(即A大于20时)将B强制至少为6.简而言之,每当f等于1时B大于5(即当A大于2时). 20.

希望这会有所帮助!请告诉我它是否无效.我本人一直在处理puLP问题,并使用这种方法编写了一些约束.

注意: 我们用81除以确保(A-20)/80始终不大于1.例如,如果A为21,则(A-20)/80等于1/80.由于A只能大到100,因此(A-20)/80只能大到(100-20)/80,即1.如果我们将其更改为(A-20)/X,则X在这里是小于(A-20)最大值的任何其他值,则分数(A-20)/X可以大于1.由于f为二进制(1或0),则约束f> = (A-20)/X表示我们实际上将迫使A小于其他情况.

I have to solve an integer linear optimization problem in Python with pulp. I solved the basic problem and now I have to add additional constraints. Is anybody who can help me with adding the condition with a logical indicator? The logical restriction is: B>5 if A>20

Here is my code:

from pulp import *

prob = LpProblem("The Optimization Problem", LpMaximize)
A = LpVariable("A", 0, 100)
B = LpVariable("B", 0, 200)
C = LpVariable("C", 0, 100)
R1 = LpVariable("R1", 0)
R2 = LpVariable("R2", 0)

R1 = 0.1 * A + 0.2 * B + 0.075 * C
R2 = 0.05 * A + 0.1 * B + 0.05 * C

prob += 2 * A + 3 * B + 2.55 * C - 0.6 * R1 - 0.8 * R2
prob += 0.5 * A + 0.8 * B + C <= 100, "T1"
prob += 0.8 * A + 0.6 * B + 0.2 * C <= 100, "T2"


prob.writeLP("OptimizationModel.lp")

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

解决方案

I think you are on the right track with creating a binary 'on-off' variable in f.

f = LpVariable('f',0,1,cat='Integer')

If I understand correctly, you need B to be > 5 as long as A is > 20, right?

Then what you need is to tweak your code such f is set to 1 as long as A is more than 20.

Since f is binary, you can do this:

prob+= f>= (A-20)/80
prob+= B>= 6*f

In the first line, (A-20)/80 will be negative for values of A from 0 to 19, and will be zero when A is 20. This will ensure that f is zero for these values of A.

When f is zero, the second constraint just implies B has to be at least zero, which is its lower bound anyway.

If, however, A is 21 and above ie A>20, then (A-20)/80 becomes positive, but never greater than 1 (more on this later). This then forces f to be at least 1 whenever A is 21 and above. Since f can only be 1 or 0, f then is set to 1.

This will result in the second constraint forcing B to at least 6 whenever B is 1, which is whenever A is greater than 20. In short, B is more than 5 whenever f is 1, which is whenever A is greater than 20.

Hope this helps! Please let me know if it doesn't work. I've been working on a puLP problem myself, and used this method to write a few of my constraints.

Note: We divide by 81 to ensure that (A-20)/80 always never bigger than one. for example, if A is 21, then (A-20)/80 evaluates to 1/80. Since A can only be as large as 100, then (A-20)/80 can therefore only ever be as large As (100-20)/80 ie 1. If we changed it to (A-20)/X, where X here is any other value below the largest value of (A-20), then the fraction (A-20)/X can be more than 1. And since f is binary (1 or 0), then the constraint f>= (A-20)/X would mean we would actually be forcing A to be smaller than it would be otherwise.

这篇关于使用PuLP进行线性优化,对变量附加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆