cbCut - Variable not in model
AnsweredHi, I was trying to add a user-cut through callback but encountered an error that I cannot resolve. There was always an GurobiError returned saying 'Variable not in model' due to the last 'model.cbCut' command while the print function in the previous line has no issue to find the variable gamma
def add_valid_cut(model, where):
if where == GRB.Callback.MIPNODE:
status = model.cbGet(GRB.Callback.MIPNODE_STATUS)
if status == GRB.OPTIMAL:
print('valid ineqaulities added')
rel = model.cbGetNodeRel(gamma)
for base in train_drone_bases:
if rel[base, 1] > 1 - rel[base, 2]:
print(base, 'valid cut added')
print(gamma[1,1], 'gamma', gamma[1,2])
model.cbCut((gamma[1,1] <= 1 - gamma[1,2]))
0
-
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?. -
It is not possible to access variables \(\texttt{gamma}\) from inside your callback, because the model at this point doesn't know them. You have to save these variables as a private model object as is done in, e.g., the tsp.py example.
So your code should read something like
def add_valid_cut(model, where):
if where == GRB.Callback.MIPNODE:
status = model.cbGet(GRB.Callback.MIPNODE_STATUS)
if status == GRB.OPTIMAL:
print('valid ineqaulities added')
rel = model.cbGetNodeRel(model._gamma)
for base in train_drone_bases:
if rel[base, 1] > 1 - rel[base, 2]:
print(base, 'valid cut added')
print(model._gamma[1,1], 'gamma', model._gamma[1,2])
model.cbCut((model._gamma[1,1] <= 1 - model._gamma[1,2]))
model = Model("myModel")
gamma = model.addVars(...)
model._gamma = gamma
[...]
model.optimize(add_valid_cut)Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments