我想做类似的事情:
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'
答案 0 :(得分:0)
这是在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)