can write() function export the lazyconstraints?
AnsweredHi,
I try to use write() function to export a model. During the solving process, some lazy constraints are added to the model, finally, when i use write() to print the final model, i cannot see the added lazy constraints with .lp file while the final solution is changed. If the write() funtion cannot export the changed model with lazy constraints, how can i see what lazy constraints are added?
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
model._var_x = model.getVarByName('x')
model._var_y = model.getVarByName('y')
model._var_z = model.getVarByName('z')
x = model.cbGetSolution(model._var_x)
y = model.cbGetSolution(model._var_y)
z = model.cbGetSolution(model._var_z)
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
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 -
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 -
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 -
Thank you very much. It helps me a lot.
0
Post is closed for comments.
Comments
5 comments