How to distinguish between branching-solutions and heuristic-solutions inside a MIP callback?
回答済みDear Gurobi team,
I have implemented a callback using the python API to log each new found MIP incumbent and the corresponding runtime:
def logIncumbents(model, where):
# MIP solution callback
if where == GRB.Callback.MIPSOL:
if hasattr(model, '_IncObjVal'):
model._IncObjVal += [model.cbGet(GRB.Callback.MIPSOL_OBJ)]
model._IncRuntime += [model.cbGet(GRB.Callback.RUNTIME)]
else:
model._IncObjVal = [model.cbGet(GRB.Callback.MIPSOL_OBJ)]
model._IncRuntime = [model.cbGet(GRB.Callback.RUNTIME)]
Similar to the MIP logs, I'd like to indicate inside the callback whether the solution was found by a MIP heuristic or by branching. Unfortunately, I couldn't find a suited callback code. Is there any way to achieve this inside the callback? :-)
I could do it by parsing the generated log file for lines starting with 'H' and '*', but I still hope there's a way to do this inside the callback method.
Best, Jonathan
-
正式なコメント
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?. -
Dear Jonathan,
It is not possible to get the information whether a solution has been found via a heuristic or branching. Currently, the best way is to use the MESSAGE callback to scan the printed LOG message for 'H' and '*' as you already suggested.
Best regards,
Jaromił1 -
Hi Jaromił,
thanks a lot for the hint with the MESSAGE callback! For anyone who stumbles across this post, here's how it works (Python >= 3.8 required):
def logIncumbents(model, where):
# MIP solution callback
if where == GRB.Callback.MIPSOL:
if hasattr(model, '_IncObjVal'):
model._IncObjVal += [model.cbGet(GRB.Callback.MIPSOL_OBJ)]
model._IncRuntime += [model.cbGet(GRB.Callback.RUNTIME)]
else:
model._IncObjVal = [model.cbGet(GRB.Callback.MIPSOL_OBJ)]
model._IncRuntime = [model.cbGet(GRB.Callback.RUNTIME)]
if where == GRB.Callback.MESSAGE:
if (c := model.cbGet(GRB.Callback.MSG_STRING)[0]) in ['H', '*']:
if hasattr(model, '_IncFoundBy'):
model._IncFoundBy += c
else:
model._IncFoundBy = cBest, Jonathan
1 -
Hello Jonathan,
I came across your solution, I find it quite useful. I would like to ask you, in your script. the variable:
model._IncObjVal
is where you update the value of the incumbent. Is that correct? I would like to update my incumbent value as well with the best current obj value but I am still struggling with how to set up the initial value. Can you query the value from the model attributes?
0
投稿コメントは受け付けていません。
コメント
4件のコメント