Benders Cuts in Branch and Bound Tree
AnsweredHello,
I am solving a two stage stochastic model in which the first stage has integer variables. I am using the Benders cuts and adding them to the MIP nodes of the branch and bound tree.
This is the callback code I am using. Function GetPIs get the MIPSOL and returns the vector E and scalar e to generate the benders cuts, and callback adds the cut by cbCut().
The issue is that the callback does not work. I mean, I have put a print in the callback to see the value of generated E and e, but they never show up in the log. Did I write the callback code correctly?
Thank you in advance for the help.
Farzane
def GetPIs(X_values):
PIs = {}
for scen in SP:
vars = SP[scen].getVars()
for x in Xkeys:
vars[x].UB = X_values[x]
vars[x].LB = X_values[x]
SP[scen].update()
SP[scen].optimize()
if SP[scen].status == 2:
constrs = SP[scen].getConstrs()
PIs[scen] = {c: constrs[c].Pi for c in range(len(constrs))}
pir = {scen: Probs[scen] * sum(PIs[scen][row] * rVectors[scen][row] for row in rVectors[scen].keys())
for scen in Probs.keys()}
e = sum(pir.values())
E = {scen: {x: 0 for x in Xkeys} for scen in Probs.keys()}
for scen in Probs.keys():
for key in TMatrices[scen].keys():
#print(key)
E[scen][key[1]] += PIs[scen][key[0]] * TMatrices[scen][key]
E = {x: sum(E[scen][x] * Probs[scen] for scen in Probs.keys()) for x in Xkeys}
return E, e
def mycallback(model, where):
if where == gp.GRB.Callback.MIPSOL:
X = model.cbGetNodeRel(model._vars)
# Get solutions in subproblems, calculate e and E
E, e = GetPIs(X)
print(E, e)
# Add a cut based on the solution # For example, adding a simple cut:
model.cbCut(model._vars[-1] + sum(model._vars[x] * E[x] for x in Xkeys) >= e)
master = gp.read('Models/Master.mps', env=env)
master._vars = master.getVars()
master.optimize(mycallback)
-
You need to call
MIPNODE
insteadMIPSOL
. In addition, note that for method Model.cbGetNodeRel() you need to check whetherGRB.Callback.MIPNODE_STATUS
is equal toGRB.OPTIMAL
, see the example usage in Model.cbGetNodeRel().0
Please sign in to leave a comment.
Comments
1 comment