Repository navigation
Fix solutions submitted from the mipUserSolution callback being discarded - #57
Merged
Merged
Conversation
Highs_js_setCallback hands the JS trampoline a C snapshot of HiGHS's HighsCallbackInput and copies it back after the callback returns. Highs_setCallbackSolution, Highs_setCallbackSparseSolution and Highs_repairCallbackSolution set user_has_solution on the live object through cbdata, not on the snapshot, so the copy-back reset the flag and HiGHS silently ignored every solution submitted from the mipUserSolution callback. OR the live flag into the snapshot before copying it back, and add a test that submits a known feasible solution and checks that it is the incumbent at the next query. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018RSNwPPM83ADX2WnKrtEKq
Collaborator
|
Thank you for the report and for the fix! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
I was trying to speed up a MIP solve by handing HiGHS a good solution from the
mipUserSolutioncallback (type 9), but it never took it.event.setSolution(vector),event.setSolution({indices, values})andevent.repairSolution()all return{status: 0}, yet the incumbents come out exactly the same as a run where I submit nothing, even when my solution is better. If I pass the same vector as a start withmodel.setSolution({colValue})beforerun(), HiGHS takes it as its first incumbent, so the solution itself is fine.Repro
I built a 300×10 binary knapsack, got a good solution (objective 11388) from a 2 s solve, then ran it again from scratch and submitted that solution at the first
mipUserSolutioncall. With the published 1.15.3 build:B, C and D follow A's incumbents exactly (0 → 99 → 11168 → … → 11388). The short version:
Cause
Highs_js_setCallbackinsrc/highs_js_bridge.cppcopies HiGHS's liveHighsCallbackInputinto a localHighsCallbackDataIn, calls the JS callback with that copy, and then copies it back over the live object (*data_in = c_data_in).The problem is that
Highs_setCallbackSolutionand the other two functions don't touch the copy. They go throughdata_in->cbdatato the live object and setuser_has_solution = truethere (HiGHS/highs/interfaces/highs_c_api.cpp:1604-1630,HiGHS/highs/lp_data/HighsCallback.cpp:164-181). The copy-back (HighsCallback.cpp:156-162) then sets the flag back to 0 from the copy, so by the timeHighsMipSolverData::queryExternalSolutionlooks at it (HiGHS/highs/mip/HighsMipSolverData.cpp:2769) it's false and the solution is dropped without a word.event.interrupt()works only because the JS writesuser_interruptstraight into the copy.HiGHS's own C wrapper (
Highs::setCallback(HighsCCallbackType, void*),Highs.cpp:2639-2651) does the same copy-back, so plain C API users hit this too; I reported that as ERGO-Code/HiGHS#3381. highs-js doesn't use that wrapper, so this PR fixes highs-js on its own with the HiGHS it bundles now.Fix
Two lines in the bridge: keep the live object's
user_has_solutionin the copy before copying it back.user_interruptstill goes through the copy as before, a callback that sets the C flag itself still works, and a failedrepairSolution()(which sets the live flag back to false) is still respected. Nothing changes in the JS runtime, the exported function list,types.d.tsor the API manifest.With the fix, HiGHS's log shows my solution as its first incumbent (
X= user solution):Test
I added "a solution submitted from the mipUserSolution callback becomes the incumbent" to
tests/extended/callbacks.test.cjs. It builds a 200×10 binary knapsack, submits a greedy solution (objective 7309) at the firstmipUserSolutioncall, and checks thatmip_primal_boundat the second call is at least 7309, that every improving solution HiGHS reports after that beats it, and that the final answer is at least as good. It interrupts after the second call, so it runs in about half a second. (HiGHS doesn't report user solutions through themipImprovingSolutioncallback on purpose, which is why the test checks the primal bound.)the primal bound at the second firing (99) should be at least the submitted objective (7309)node tests/test.jsand all 81 extended tests.I used an AI assistant (Claude) to help track this down and write the fix and test, and I checked the results by running them as above.