Callback function does not remember previous lazy constraints?
AnsweredHello,
I wrote a callback function to apply a variation of the Lshaped method (which is a benders like decomposition). At each feasible solution I solve a subproblem and on two conditions I apply a lazy cut. The cut should be applied to the master problem, so to all pending nodes. Now if I apply my callback to a very simple problem, then I see that if in the first feasible solution the conditions are satisfied, and so a lazy cut is added. Then the second feasible solution the program finds and thus enters the callback function with does not satisfy the previously added lazy cut?
My callback function looks like:
def my_callback(model, where):
global first_callback
if where == GRB.Callback.MIPSOL:
valx = model.cbGetSolution(model._vars_x)
valzeta = model.cbGetSolution(model.getVarByName('zeta'))
if first_callback:
valzeta = float('inf')
first_callback=False
LL = Stage2par(valx,S,cases) #solves the subproblem, returns a float
z = valx + LL
if z < model._best_obj: #condition 1
model._best_obj = z # Update best solution so far
model._best_x = valx
if valzeta < LL: #condition 2
model.cbLazy(x>=1)
first_callback = True
model = gp.Model("Demand_test")
model.modelSense = GRB.MINIMIZE
model.setParam(GRB.Param.LazyConstraints, 1)
x = model.addVar(name='x', lb=0.0, ub=1.0, vtype = GRB.BINARY)
zeta = model.addVar(name='zeta', lb=0.0, vtype = GRB.CONTINUOUS)
model.setObjective(x+zeta)
# Set the callback function
model._vars_x = x
model._best_obj = float('inf')
model._best_x = None
model.optimize(my_callback)

Hi Ans,
Added (lazy) cut information is only propagated to all the solver components at specific synchronization points, especially when running multiple threads in parallel. Therefore, you may observe a solution in the lazy callback that does not respect previously added cuts. You need to consider each lazy callback independently without relying on previous calls. This might even result in adding the same lazy cut multiple times.
Best regards,
Mario0
Please sign in to leave a comment.
Comments
1 comment