gurobipy中的反向指标约束 [英] inverted indicator constraint in gurobipy

查看:39
本文介绍了gurobipy中的反向指标约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是gurobipy的初学者.我想添加一个反向指标约束.

I am a beginner in gurobipy. I would like to add an inverted indicator constraint.

指标约束什么都不是,但取决于二进制变量约束是否成立.在gurobipy中,它写为

Indicator constraint is nothing but depending on a binary variable a constraint does or does not hold. In gurobipy this is written as

model.addConstr((x == 1) >> (y + z <= 5))

其中x是二进制变量,y和z是整数变量.该语句表明,如果x为True,则约束y + z< = 5成立.

where x is a binary variable, y and z are integer variables. This statement says that if x is True then the constraint y+z <= 5 holds.

但是我想要这样一个反向约束.如果y + z< = 5,则x ==1.但是gurobi不允许语句的lhs部分为不等式.它只能是等于常数(0或1)的二进制变量.

But I would like to have an inverted constraint like this. If y+z <= 5 then x == 1. But gurobi does not allow the lhs part of the statement to be an inequality. It can only be a binary variable equal to a constant (0 or 1).

所以倒排的语句会引发错误.

So the inverted statement throws an error.

model.addConstr((y + z <= 5) >> (x == 1))

有什么想法如何在gurobipy中重写这样的条件约束吗?!

Any ideas how to rewrite such a conditional constraint in gurobipy?!

推荐答案

含义

y+z ≤ 5  ⇒  x = 1

可以改写为:

x = 0  ⇒  y+z ≥ 6

这可以直接实现为指标约束.

This can be directly implemented as an indicator constraint.

这是基于命题逻辑的.这称为换位:

This is based on propositional logic. This is called transposition:

A ⇒ B
⇔
not B ⇒ not A 

所以理论上我们有

y+z ≤ 5  ⇒  x = 1
⇔
x = 0  ⇒  y+z > 5

如果x和y是整数,我们可以说 x = 0⇒y + z≥6 如果它们是连续变量,则可以这样做: x = 0⇒y + z≥5.0001 (实际上,我会这样做: x = 0⇒y + z≥5 ,并且在 y + z = 5 时保持模棱两可).

If x and y are integers we can say x = 0 ⇒ y+z ≥ 6 If they are continuous variables you could do: x = 0 ⇒ y+z ≥ 5.0001 (in practice I would do: x = 0 ⇒ y+z ≥ 5 and keep things ambiguous at y+z = 5).

当使用指标约束时,这是一种标准技巧.似乎并不是每个人都知道或赞赏这一点.

This is kind of a standard trick when using indicator constraints. It seems not everyone is aware of or appreciates this.

这篇关于gurobipy中的反向指标约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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