Skip to content

Add sol.prob / get_prob to recover the OptimizationProblem from a solution (Optimization.jl#1191) - #1367

Closed
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:optimization-sol-prob-accessor
Closed

Add sol.prob / get_prob to recover the OptimizationProblem from a solution (Optimization.jl#1191)#1367
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:optimization-sol-prob-accessor

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

Adds a way to recover the original OptimizationProblem from an OptimizationSolution, addressing SciML/Optimization.jl#1191 (sol.prob returns the cache, not the problem).

OptimizationSolution stores the solved cache in sol.cache. Historically sol.prob was deprecated to return the cache (2.x) and then removed (3.x), so there is currently no supported way to get the original problem back from a solution.

What changed

  • Add SciMLBase.get_prob(sol) and SciMLBase.has_prob(sol), mirroring the existing get_p/get_syms accessors. get_prob returns the original problem retained by the cache (sol.cache.prob).
  • Re-add sol.prob as sugar for get_prob(sol) via the solution getproperty, alongside the existing :ps accessor.
  • Caches that don't retain the problem (e.g. DefaultOptimizationCache) raise a clear ArgumentError instead of silently returning the cache.
  • propertynames/hasproperty are intentionally left unchanged (matching :ps), so wrap_sol's hasproperty(sol, :prob) guard — and every other hasproperty-gated path — is unaffected.
  • Version 3.16.0 → 3.17.0.

Pairs with

OptimizationBase storing the original problem on OptimizationCache (Optimization.jl PR). The two are complementary but independent: this PR works for any AbstractOptimizationCache that exposes a prob field and errors otherwise.

Note for review

This re-introduces sol.prob (previously deprecated to return the cache, then removed). Here it returns the problem. If you'd rather keep sol.prob retired and expose only get_prob(sol), the getproperty branch can be dropped — happy to do so.

Tests (run locally)

Added a self-contained testset in solution_interface.jl using a mock prob-retaining AbstractOptimizationCache and DefaultOptimizationCache (negative path). Verified the full solution_interface.jl passes (existing + new 7-assertion set), and an end-to-end OptimizationBase+LBFGSB run where sol.prob === prob, get_prob(sol) === prob, has_prob(sol). Runic-formatted.

🚧 Draft — please ignore until reviewed by @ChrisRackauckas.

…olution

`OptimizationSolution` stores the solved `OptimizationCache` in `sol.cache`, not
the original problem. `sol.prob` was previously deprecated to return the cache
(2.x) and then removed entirely, so today there is no way to get the original
`OptimizationProblem` back from a solution (SciML/Optimization.jl#1191).

Add `SciMLBase.get_prob(sol)` (with `has_prob(sol)`), mirroring the existing
`get_p`/`get_syms` accessors, returning the original problem retained by the
cache (`sol.cache.prob`). Re-add `sol.prob` as sugar for `get_prob(sol)` via the
solution `getproperty`, alongside the existing `:ps` accessor. Caches that do not
retain the problem (e.g. `DefaultOptimizationCache`) raise a clear `ArgumentError`
rather than silently returning the cache.

This pairs with OptimizationBase storing the original problem on `OptimizationCache`
(Optimization.jl). `propertynames`/`hasproperty` are intentionally left unchanged
(matching `:ps`), so `wrap_sol`'s `hasproperty(sol, :prob)` guard is unaffected.

Adds a self-contained test in solution_interface.jl using a mock prob-retaining
cache and `DefaultOptimizationCache` for the negative path.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Base PR storing the original problem on OptimizationCache (so sol.cache.prob exists for this to return): SciML/Optimization.jl#1209.

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Note on the red downstream checks: they are CI-infrastructure noise, not this change. Two causes in the logs: (1) the package gracefully skips with @info "Not compatible with this release. No problem." (SemVer resolver skip for the 3.16→3.17 minor bump), and (2) the job then exits 255 on error: could not lock config file ~/.gitconfig: File exists — a git-config lock race between concurrent jobs on the self-hosted runner during post-job cleanup. StochasticDiffEq downstream jobs and runic pass, which wouldn't happen if the change broke using SciMLBase. The change is additive to optimization-solution accessors and can't affect DDE/ODE loading. SciMLBase's own solution_interface.jl (incl. the new testset) passes locally.

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.

2 participants