Callback for nonconvex QCP/MIQCP
AnsweredHi folks,
Does gurobi support callback for QCP/MIQCP. I tried the following small example in python and it does not seem to work?
Thanks in advance.
from gurobipy import *
model = Model("NewModel")
model.setParam("nonConvex", 2)
x = model.addVar(name="x")
y = model.addVar(name="y")
z = model.addVar(name="z")
# Set objective: maximize x
model.setObjective(1.0*x, GRB.MAXIMIZE)
# Add linear constraint: x + y + z <= 10
model.addConstr(x + y + z <= 10, "c0")
# Add bilinear inequality constraint: x * y <= 2
model.addConstr(x*y <= 2, "bilinear0")
# Add bilinear equality constraint: x * z + y * z == 1
model.addConstr(x*z + y*z == 1, "bilinear1")
def mycallback(model, where):
if where == GRB.Callback.MIPSOL:
sol = model.cbGetSolution([model._vars[0], model._vars[1]])
if sol[0] + sol[1] > 1.1:
model.cbLazy(model._vars[0] + model._vars[1] <= 1)
model._vars = model.getVars()
model.optimize(mycallback)
0
-
Hi,
There are two issues with your code:
Firstly, because of Gurobi's lazy update approach, model.getVars() returns an empty list at this point. This is one of the rare occasions where you need to call model.update() first.
Secondly, if you want to add lazy constraints through a callback, you need to set the LazyConstraints parameter to 1.
1 -
Thanks. It works
0
Please sign in to leave a comment.
Comments
2 comments