如何将条件二进制变量添加到 GUROBI

问题描述

我正在使用 Gurobi 作为求解器并使用 Python 作为接口进行数学建模。在我的模型中,我想创建一个二元变量,但如果 a>=1 则允许该变量取值为 1,否则为 0,其中 a 是模型中的另一个决策变量。

二进制变量应如下图所示:-

enter image description here

解决方法

总而言之,您希望对影响进行建模:

  x>=1 => y=1
  x=0  => y=0
  x >= 0 (non-negative variable)
  y ∈ {0,1}  (binary variable)      

有不同的方法可以做到这一点。

请注意,我们可以反转含义:

  y=0 => x=0
  y=1 => x>=1

指标约束

  y=0 => x=0
  y=1 => x>=1
 

可以直接实现为指标约束。见https://www.gurobi.com/documentation/9.1/refman/py_model_agc_indicator.html

Big-M 方法

  x <= M*y   (this will do y=0 => x=0)
  x >= y     (this will do y=1 => x>=1) 

这里的常数 M 是 x 的上界。如果 M 很大,最好使用指标约束。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...