how to write user cuts to an lp file
AnsweredHello,
I would like to save my user cuts and lazy cuts into an lp file. my cuts are inserted via callbacks. I tried just to save the complete model using:
model.write(filename.lp)
However, the lp file doesn't have the lazy cuts I am adding to my model. Do I have to write model.write(filename.lp) inside the callback?
def lazycallbacks(model, where):
if where == GRB.MIPSOL:
# v = model.cbGetSolution(model._vars)
# y = model.cbGetSolution(model._varsy)
# kset = [i for i,value in enumerate(y) if value <=.5]
# if some conditions:
# model.cbLazy(value <= v + M(sum(y))
# print(f"{value} <= v + M*(sum{['y[%d]'%g for g in kset]}))
m.write(f"{value} <= v + M*(sum{['y[%d]'%g for g in kset]}))
if where == GRB.MIPNODE:
# v = model.cbGetSolution(model._vars)
# y = model.cbGetSolution(model._varsy)
# kset = [i for i,value in enumerate(y) if value <=.5]
# if some conditions:
# model.cbLazy(value <= v + M(sum(y))
# print(f"{value} <= v + M*(sum{['y[%d]'%g for g in kset]}))
m = gp.Model()
# add vars
# add constrs
m.optimize(lazycallbacks)
m.write('filename.lp')
I'd appreciate the guidance!

Hi,
Unfortunately, it is not possible to write lazy constraints or user cuts added via callback with Model.write() function directly. However, you can workaround this by tracking the lazy constraints added to the model during optimization and adding them using model.addConstr() method after the optimization is finished. Please see below for an example.
import gurobipy as gp
from gurobipy import GRB
def lazy(model, where):
if where == GRB.Callback.MIPSOL:
xX, yX= model.cbGetSolution([x, y])
if xX == 1:
# add lazy constraint x == 0
expr = x
sense = "="
rhs = 0
model.cbLazy(expr == rhs)
lazyconstrs.append((expr,sense,rhs))
model = gp.Model()
model.params.LazyConstraints = 1
#add variables
x = model.addVar(vtype=gp.GRB.BINARY, name="x")
y = model.addVar(vtype=gp.GRB.BINARY, name="y")
#add constraints
model.addConstr(x+y == 1)
#empty list to track lazy constraints added in the model
lazyconstrs = []
model.optimize(lazy)
#add lazy constraints in the model to print in the lp file
for l in range(len(lazyconstrs)):
if lazyconstrs[l][1] == "=":
c = model.addConstr( lazyconstrs[l][0] == lazyconstrs[l][2])
c.Lazy = 1
model.write("test.lp")Regards,
Simran
0 
Thank you so much Simran!
0 
Hello Simran,
A further question, after I add the constraints to the model to be printed in the lp file. do I have to update the model to solve it? in my model I have a structure similar to this one
def myfunc(lp:bool=False):
def lazy(model, where):
# bunch of steps
lazycts.append((expr,sense,rhs))
m = gp.Model()
v = m.addVars(range(5),vtype=GRB.CONTINUOUS,name='v')
y = m.addVars(rangw(5),vtype=GRB.BINARY, name='y')
# set my objective
# add my contraints
lazycts = []
m.optimize(lazy)
if lp:
for l in range(len(lazycts)):
# conditions to add the lazycts
c.Lazy = 1
m.write(file.lp)
if m.status == GRB.OPTIMAL:
v = [m.getVarByName('v[%d]'%g).x for g in range(5)]
y = [m.getVarByName('y[%d]'%g).x for g in range(5)]
# other conditions for m.status
return v,yI get an error of local variable v referenced before assignment. This only seems to happen when I add the lazycts to the model's lp. But, if I omit the step when I add them to the lp the function returns the values with no problem. Just wondering if I have to update the model once I add the lazy cts to the lp and solve from there.
Best
0 
Hi,
The model.write() call implicitly updates the model and processes all pending model modifications.
In your example, when you add the lazy constraints using model.addConstr() and write the lp file, the model gets modified and updated, and the solution information from the previous optimize() call can no longer be retrieved, and hence you see the error.
To fix this, please retrieve the solution information before adding the lazy constraints to the model and writing the lp file.
m.optimize()
if m.status == GRB.OPTIMAL: v = [m.getVarByName('v[%d]'%g).x for g in range(5)] y = [m.getVarByName('y[%d]'%g).x for g in range(5)]
# other conditions for m.status
if lp:
for l in range(len(lazycts)):
# conditions to add the lazycts
c.Lazy = 1
m.write(file.lp)
return v,yRegards,
Simran
0 
many thanks!!
0
Please sign in to leave a comment.
Comments
5 comments