can i use an indicator Constraint with Min/Max Constraint in Gurobi?
Answeredi've the following model for Gurobi:
m = Model()
x = {}
for k in range(K):
for i in range(V):
for j in range(V):
if i != j:
x[k,i,j] = m.addVar(vtype=GRB.BINARY, name="x(%s,%s,%s)"%(k,i,j))
a = {}
for k in range(K):
for i in range(V):
a[k,i] = m.addVar(ub=25, vtype= GRB.INTEGER, name= "a(%s,%s)"%(k,i))
and i'm trying to write a constraint using the indicator Constraint with Min/Max Constraint like this:
m.addConstrs((x[k,i,j] == 1) >> (a[k,j] == max_((a[k,i] + 5), 15)) for k in range(K) for i in range(V) for j in range(1,V) if i!=j)
but i end up getting this Error:
File "model.pxi", line 3070, in gurobipy.Model.addConstrs
File "model.pxi", line 2951, in gurobipy.Model.addConstr
File "model.pxi", line 3592, in gurobipy.Model.addGenConstrIndicator
File "linexpr.pxi", line 461, in gurobipy.LinExpr.__sub__
TypeError: unsupported operand type(s) for *: 'int' and 'GenExpr'
i've also tried a different variants to write this constraint like:
for k in range(K):
for i in range(V):
for j in range(1,V):
if i!=j:
m.addConstr((x[k,i,j] == 1) >> ((a[k,j]) == max_((a[k,i] + 5), 15), "tww(%s)"%(i))
#or
m.addGenConstrIndicator(x[k,i,j], True, a[k,j], GRB.EQUAL, max_((a[k,i] + 5), 15))
but also end up getting the same error as before or this one:
m.addGenConstrIndicator(x[k,i,j], True, a[k,j], GRB.EQUAL, max_((a[k,i] + 5), 15))
File "model.pxi", line 3588, in gurobipy.Model.addGenConstrIndicator
gurobipy.GurobiError: Invalid rhs argument for general constraint of indicator type
is there something wrong that i'm doing or it just can't be done this way?
any help will be appreciated

The conclusion of an indicator constraint must be a linear constraint. But you can easily get around this by introducing another set of variables that are defined identically to `a`; let's call them `b`. Then you would write
m.addConstrs((x[k,i,j] == 1) >> (a[k,j] == b[k,j]) for k in range(K) for i in range(V) for j in range(1,V) if i!=j)
m.addConstrs(b[k,j] == max_((a[k,i] + 5), 15) for k in range(K) for i in range(V) for j in range(1,V) if i!=j)

thank you for your help that was very helpful.
after i worked with your suggestion, i end up getting the Error:TypeError: object of type 'gurobipy.LinExpr' has no len()
after digging in the internet, i found that this Error was caused by the max_() function. I can only pass a list of variables as arguments for this function and not a LinExpr as i do.
(thanks to Robert Luce for his answer on google group https://groups.google.com/forum/#!topic/gurobi/oO3d2ItJiqs )
thank you again for your help Tobias Achterberg
Please sign in to leave a comment.
Comments
2 comments