Skip to main content

can write() function export the lazyconstraints?

Answered

Comments

4 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi,

    The write() function does not write out lazy constraints which are added via a callback function. In order to write out lazy constraints you would have to either define all possible lazy constraint via the addConstr() method and set the Lazy attribute or track which lazy constraints have been added in your callback function and after the optimization process is finished, add them manually to your model via addConstr() and the finally use write().

    By the way, the example of function "cbGetSolution" (https://www.gurobi.com/documentation/9.1/refman/py_model_cbgetsolution.html)only gives how to get all variables using model.getVars() during solving process. However, if I have variables named 'x','y','z' and so on, how can i get them by their names, i tried this, but fails

    I just tested this and was not able to reproduce your issue. I used the tsp.py example and changed the lines 22-24 to

    if where == GRB.Callback.MIPSOL:
    # make a list of edges selected in the solution
    x = model.getVarByName("e[1,0]")
    print(model.cbGetSolution(x))
    vals = model.cbGetSolution(model._vars)
    [...]

    Best regards,
    Jaromił

    1
  • yansong bai
    Gurobi-versary
    Conversationalist
    Curious

    Hi, Jaromił

    Thank you for your prompt reply. 

    import numpy as np
    import gurobipy as gp
    from gurobipy import GRB
    model = gp.Model('test')
    x = model.addVars(1,4,vtype=GRB.BINARY,name='x')
    y = model.addVars(1,2,vtype=GRB.CONTINUOUS,name='y')
    model.addConstr(x[0,0]<=0)
    model.addConstr(y[0,0]<=1)
    model.addConstr(y[0,1]>=2)
    model.Params.LazyConstraints = 1
    model.setObjective(x[0,0]+x[0,1]+x[0,2]+x[0,3]-y[0,0]+y[0,1],GRB.MINIMIZE)
    model.optimize(mycallback)

    def mycallback(model,where):
    if where == GRB.Callback.MIPSOL:
    vars = model.getVarByName('x')
    x = model.cbGetSolution(vars)
    if x[0]==0:
    model.cbLazy(vars[1]>=1)

     Getting variables by name from callback above does not seem to work.

    x = model.cbGetSolution(vars)
    File "src\gurobipy\model.pxi", line 6066, in gurobipy.Model.cbGetSolution
    File "src\gurobipy\model.pxi", line 6018, in gurobipy.Model._map_coldata_to_vars
    TypeError: object of type 'NoneType' has no len()

     

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi,

    You see this error because there is no variable called 'x' in your model. When you use the addVars method, Gurobi generates unique names for each created variables depending on the indices you provide. In your case the variable names are

    x[0,0],x[0,1],x[0,2],x[0,3]

    In order to access the \(\texttt{x}\) variables, you have to save them as a private object in the model and then access them in the callback

    import numpy as np
    import gurobipy as gp
    from gurobipy import GRB

    def mycallback(model,where):
    if where == GRB.Callback.MIPSOL:
    x = model.cbGetSolution(model._x)
    if x[0,0]==0:
    model.cbLazy(model._x[0,1] >= 1)

    model = gp.Model('test')
    x = model.addVars(1,4,vtype=GRB.BINARY,name='x')
    y = model.addVars(1,2,vtype=GRB.CONTINUOUS,name='y')
    model.addConstr(x[0,0]<=0)
    model.addConstr(y[0,0]<=1)
    model.addConstr(y[0,1]>=2)
    model.Params.LazyConstraints = 1
    model.setObjective(x[0,0]+x[0,1]+x[0,2]+x[0,3]-y[0,0]+y[0,1],GRB.MINIMIZE)
    model._x = x
    model.optimize(mycallback)

    Best regards,
    Jaromił

    0
  • yansong bai
    Gurobi-versary
    Conversationalist
    Curious

    Thank you very much. It helps me a lot.

    0

Please sign in to leave a comment.