I want to stop my LP when |primal obj - dual obj| <= 1.and want decision variable values and optimal solution values.
AnsweredBelow code was suggested to me by an expert , however it terminates once it reaches a gap = 1 and i am unable to get decision variable values and optimal solution.
import sys
import gurobipy as gp
from gurobipy import GRB def mycallback(model, where): if where == GRB.Callback.BARRIER:
# Barrier callback
itcnt = model.cbGet(GRB.Callback.BARRIER_ITRCNT)
primobj = model.cbGet(GRB.Callback.BARRIER_PRIMOBJ)
dualobj = model.cbGet(GRB.Callback.BARRIER_DUALOBJ)
priminf = model.cbGet(GRB.Callback.BARRIER_PRIMINF)
dualinf = model.cbGet(GRB.Callback.BARRIER_DUALINF)
cmpl = model.cbGet(GRB.Callback.BARRIER_COMPL)
if abs(primobj - dualobj) <= 1:
print("Custom gap reached")
model.terminate()
print("%d %g %g %g %g %g" % (itcnt, primobj, dualobj, priminf, dualinf, cmpl)) if len(sys.argv) < 2:
print("Usage: callback.py filename")
sys.exit(0)
# Read model from file
model = gp.read(sys.argv[1])
model.params.Method = 2 # fix to barrier
model.optimize(mycallback)
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 try Gurobot, our chatbot interface offering instant, expert-level support.
Post is closed for comments.
Comments
2 comments