Skip to main content

how to write user cuts to an lp file

Answered

Comments

5 comments

  • Simranjit Kaur
    Gurobi Staff Gurobi Staff

    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
  • Jose Vindel
    Gurobi-versary
    Thought Leader
    Investigator

    Thank you so much Simran!

    0
  • Jose Vindel
    Gurobi-versary
    Thought Leader
    Investigator

    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,y

    I 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
  • Simranjit Kaur
    Gurobi Staff Gurobi Staff

    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,y

    Regards,

    Simran

    0
  • Jose Vindel
    Gurobi-versary
    Thought Leader
    Investigator

    many thanks!!

    0

Please sign in to leave a comment.