HiGHS icon indicating copy to clipboard operation
HiGHS copied to clipboard

Getting incumbent solution in callback (highspy)

Open sertalpbilal opened this issue 1 year ago • 1 comments

I am trying to read the incumbent solution in a user callback whenever an improvement is made in a MIP model. Here is what I have:

import highspy
import numpy as np

hscb = highspy.cb
h = highspy.Highs()
h.readModel('test.mps')

def myfunc(callback_type,
    message,
    data_out,
    data_in,
    user_callback_data):
    if callback_type == hscb.HighsCallbackType.kCallbackMipImprovingSolution:
        print(data_out.mip_solution)

h.setCallback(myfunc, None)
h.startCallback(hscb.HighsCallbackType.kCallbackMipImprovingSolution)
h.run()

It seems like the "mip_solution" is just a numpy array with dimension of 3. I was trying to investigate why I can't get the solution and have seen this comment here https://github.com/ERGO-Code/HiGHS/blob/5ce7a27531a7f4166ee5a8343169a1014febb41a/src/highs_bindings.cpp#L1179

So, I was wondering if there is either a way to get the current solution by fixing the issue in this file, or at least a way to export the current solution to a file.

Thanks -Sertalp

sertalpbilal avatar Jul 26 '24 03:07 sertalpbilal

This is related to #1857 raised by @thell

Hello!

Consider the following options being passed to HiGHS:

"mip_improving_solution_save=on", "mip_improving_solution_file=highs_improved_solution_{max_cost}_c.sol",

I can see that they are being accepted because a highs_improved_solution file was created and saved the first of the improved solutions.

That was ~6.5hrs ago. Since then over a dozen improvements have taken place yet none of them got saved. Much to my dismay because I just hit ctrl-c to copy something but my focus was on my terminal 🤦‍♂️ The real sad part was that I had realized this was taking place and the ctrl-c I used was to copy the options pasted above for this ticket. Anyhow, after getting down to a gap of 1.72% and 75.01% explored in those 6.5hrs I really would like to have seen the last incumbent.

jajhall avatar Jul 26 '24 20:07 jajhall