Skip to content

Fix solutions submitted from the mipUserSolution callback being discarded - #57

Merged
lovasoa merged 1 commit into
ERGO-Code:mainfrom
DoumarCharles:fix-mip-user-solution
Oct 16, 2026
Merged

lovasoa merged 1 commit into
ERGO-Code:mainfrom
DoumarCharles:fix-mip-user-solution

Conversation

@DoumarCharles

@DoumarCharles DoumarCharles commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

I was trying to speed up a MIP solve by handing HiGHS a good solution from the mipUserSolution callback (type 9), but it never took it. event.setSolution(vector), event.setSolution({indices, values}) and event.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 with model.setSolution({colValue}) before run(), 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 mipUserSolution call. With the published 1.15.3 build:

Summary (submitted objective 11388):
  A no submission              : first improving incumbent = 0
  B callback setSolution       : incumbent at 2nd firing = 99, first improving incumbent = 0 -> IGNORED
  C callback setSolution (all) : incumbent at 2nd firing = 99, first improving incumbent = 0 -> IGNORED
  D callback sparse + repair   : incumbent at 2nd firing = 99, first improving incumbent = 0 -> IGNORED
  E start before run()         : first improving incumbent = 11388

B, C and D follow A's incumbents exactly (0 → 99 → 11168 → … → 11388). The short version:

const highs = await require("highs")({ print: console.log });  // print: to see HiGHS's log
const model = highs.createModel(knapsack);   // any MILP that takes a while
model.options.set({ output_flag: true });
let firings = 0;
model.run({
  [highs.constants.callbackType.mipUserSolution](event) {
    firings += 1;
    if (firings === 1) console.log(event.setSolution(knownGoodVector)); // { status: 0 }
    else console.log("incumbent:", event.data.mip_primal_bound);        // never the submitted objective
  },
});

Cause

Highs_js_setCallback in src/highs_js_bridge.cpp copies HiGHS's live HighsCallbackInput into a local HighsCallbackDataIn, 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_setCallbackSolution and the other two functions don't touch the copy. They go through data_in->cbdata to the live object and set user_has_solution = true there (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 time HighsMipSolverData::queryExternalSolution looks 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 writes user_interrupt straight 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_solution in the copy before copying it back.

if (data_in) {
  c_data_in.user_has_solution |= data_in->user_has_solution ? 1 : 0;
  *data_in = c_data_in;
}

user_interrupt still goes through the copy as before, a callback that sets the C flag itself still works, and a failed repairSolution() (which sets the live flag back to false) is still respected. Nothing changes in the JS runtime, the exported function list, types.d.ts or the API manifest.

With the fix, HiGHS's log shows my solution as its first incumbent (X = user solution):

 X       0       0         0   0.00%   inf             11388              Large ...

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 first mipUserSolution call, and checks that mip_primal_bound at 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 the mipImprovingSolution callback on purpose, which is why the test checks the primal bound.)

  • without the fix: the primal bound at the second firing (99) should be at least the submitted objective (7309)
  • with the fix: passes, along with node tests/test.js and 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.

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
@lovasoa
lovasoa merged commit 7cf673d into ERGO-Code:main Oct 16, 2026
6 checks passed
@lovasoa

lovasoa commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the report and for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants