在Gurobi中添加x<=y==z作为约束

2024-05-01 21:34:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一些类似的事情:

model.addConstr( (x <= y) == z )其中z是二进制约束。换句话说:z如果x小于y,则应为1,否则为0。你知道吗

Gurobi崩溃的原因: model.addConstr(z == (x <= y)) File "model.pxi", line 2966, in gurobipy.Model.addConstr (../../src/python/gurobipy.c:88191) File "linexpr.pxi", line 461, in gurobipy.LinExpr.__sub__ (../../src/python/gurobipy.c:34910) TypeError: unsupported operand type(s) for *: 'int' and 'TempConstr'


Tags: insrcmodelline二进制原因事情file
1条回答
网友
1楼 · 发布于 2024-05-01 21:34:48

在x和y是完全连续的GurobiVar实例的情况下,您可以这样做。Big M method是解决这类问题的公认方法。你知道吗

一个简单的例子:

from gurobipy import Model, GRB

m = Model()
x = m.addVar(lb=-GRB.INFINITY)
y = m.addVar(lb=-GRB.INFINITY)
z = m.addVar(vtype=GRB.BINARY)

# To be numerically stable, the big M has to be
# a similar order of magnitude as the absolute value
# of the difference between x and y
BIG_M = 10000

# Force z to become 1 if y is larger than x
m.addConstr(BIG_M * z >= y - x)

# Force z to be zero if y is smaller than x
m.addConstr(BIG_M * (1 - z) >= x - y)

相关问题 更多 >