From 65d34c4b440670038c908c1ab9bce46a4fa646a9 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 15 May 2026 14:07:22 +1200 Subject: [PATCH] Don't restore VariablePrimalStart on exit from optimize Because this may invalidate the solver's stored solution. Whether it does depends on the solver. It doesn't for HiGHS in the tests because we cache the start in the MOI wrapper. --- src/MathOptLazy.jl | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/MathOptLazy.jl b/src/MathOptLazy.jl index 509f3d7..c666178 100644 --- a/src/MathOptLazy.jl +++ b/src/MathOptLazy.jl @@ -407,17 +407,17 @@ end ### MOI.optimize! -function _get_start(model, x) - if !MOI.supports(model, MOI.VariablePrimalStart(), MOI.VariableIndex) - return nothing - end - return Dict(xi => MOI.get(model, MOI.VariablePrimalStart(), xi) for xi in x) -end - function MOI.optimize!(model::Optimizer) needs_solve = true x = MOI.get(model, MOI.ListOfVariableIndices()) - start = _get_start(model, x) + # TODO(odow): if the solver supports VariablePrimalStart, we will update the + # primal starts during the solve process. This is destructive and in-place. + # That is, we will overwrite any start set by the user. We can't restore the + # start at the end of the loop because this may invalidate the inner + # solver's solution. To fix properly, we should cache the start in + # ::Optimizer, but this is a hassle and no one probably cares. Revisit this + # decision if it ever becomes a problem. + start = MOI.supports(model, MOI.VariablePrimalStart(), MOI.VariableIndex) while needs_solve needs_solve = false MOI.optimize!(model.inner) @@ -428,18 +428,13 @@ function MOI.optimize!(model::Optimizer) constraints_added += _add_if_necessary(model, v, X) end needs_solve = constraints_added > 0 - if start !== nothing + if start && needs_solve for (xi, v) in X MOI.set(model, MOI.VariablePrimalStart(), xi, v) end end end end - if start !== nothing - for (xi, v) in start - MOI.set(model, MOI.VariablePrimalStart(), xi, v) - end - end return end