如何在 docplex python API 中定义指标约束?

how to define an indicator constraint in docplex python API?

我想在 python 中使用 docplex 对整数规划示例进行建模。对于指标约束,我有这个等式(X 是一个二元变量):

我写了这段代码:

for i1,i2,i3 in P:
    mdl.add_indicator_constraints(x[(i,j,k)] for i,j,k in ijk if i==i1 and j==i2 and k==i3)==0

我不知道我是否使用了正确的命令来定义这个指标。当我 运行 程序时,我得到这个错误:

    cpx_indvars.append(logct.binary_var.safe_index)

AttributeError: 'Var' object has no attribute 'binary_var'

您的约束看起来不像指标约束。指示器约束看起来像 "if x=1 then ..." 或 "if x=0 then ...",其中 x 是一个二进制变量。您的约束中似乎没有 "then" 部分。

如果您只想将变量固定为 0,则不必使用指示符约束。只需添加一个常规约束:

mld.add_linear_constraints(x[(i,j,k)] == 0 for i,j,k in ijk if i==i1 and j==i2 and k==i3)