Repository navigation
Add HomotopySweep: natural-parameter continuation solver for HomotopyProblem - #962
Conversation
Adds `HomotopySweep <: AbstractNonlinearSolveAlgorithm`, a natural-parameter continuation solver stub for `SciMLBase.HomotopyProblem`. Wires include/export in the main module and adds a construction test (9 assertions, all passing).
Add _lambda_setter helpers (integer-index and symbolic-SII variants) and CommonSolve.solve(::HomotopyProblem, ::HomotopySweep) that sweeps λ from λspan[1] to λspan[2] in nsteps steps, warm-starting each inner NonlinearProblem solve from the previous step's solution. Also imports HomotopyProblem from SciMLBase into the NonlinearSolve module. Happy-path test (oop, λ in p by index) converges u → √c = 2.0 for c = 4.0.
… in sublibs, not core src/)
| last_sol = solve(inner_prob, alg.inner, args...; kwargs...) | ||
|
|
||
| if SciMLBase.successful_retcode(last_sol) | ||
| u = last_sol.u |
There was a problem hiding this comment.
This is a pointer assignment, so won't it just alias and start overwriting?
There was a problem hiding this comment.
Correct — the comment's claim was false under pointer assignment, and the sharper version of the problem is on the copy-in side: with alias = NonlinearAliasSpecifier(alias_u0 = true) forwarded to the inner solves, a failed attempt iterates directly in its u0 buffer and leaves diverged garbage there, corrupting the warm start for the bisected retry. Fixed in both directions: each inner problem now receives copy(u), and on success we take u = copy(last_sol.u) (defensive, in case an inner solver returns a view into persistent workspace). A regression test runs an aliased sweep against a non-aliased reference and requires exactly identical u (the step sequences are deterministic).
| # conservative: step size is not restored after a bisection | ||
| dλ = dλ / 2 # bisect; retry from the same λ (do not advance) | ||
| else | ||
| # on failure: u is the last CONVERGED iterate (λ<λ1); resid is from the failed step (advisory) |
There was a problem hiding this comment.
This is not true if pointer assignment.
There was a problem hiding this comment.
Addressed together with the pointer-assignment thread above — with copy-in (copy(u) per attempt) and copy-out (copy(last_sol.u) on success) in place, the "last converged iterate" claim is now actually guaranteed, and a regression test covers it.
| end | ||
|
|
||
| function HomotopySweep(; inner = nothing, nsteps = 10, adaptive = true, min_dλ = 1.0e-3) | ||
| nsteps >= 1 || throw(ArgumentError("HomotopySweep `nsteps` must be ≥ 1, got $nsteps")) |
There was a problem hiding this comment.
I'm not sure about having a magic number here. initial_step_factor = 0.1, nsteps= nothing, adaptive = true. If adaptive = false, nsteps must be set. initial_step_factor is a default initial step, but not necessarily a default number of steps, which IMO would be dangerous.
There was a problem hiding this comment.
min_dλ = 1.0e-3 this is not floating point dependent. I would make it nothing, and in the algorithm instead use a value of sqrt(eps(lambda))
There was a problem hiding this comment.
That way if you change precision, the minimum changes with it. 1e-3 is more for Float32 than Float64.
There was a problem hiding this comment.
Adopted as prescribed: nsteps = nothing, adaptive = true, initial_step_factor = 0.1, min_dλ = nothing; adaptive = false without an explicit nsteps throws an ArgumentError, and min_dλ = nothing resolves to sqrt(eps(typeof(λ))) at solve time so the floor scales with precision (min_dλ <= 0 also throws — it would bisect forever). One judgment call to confirm: when nsteps is given together with adaptive = true, it sets the initial step to span/nsteps and bisection stays active; initial_step_factor only applies when nsteps === nothing. A second one: the eps floor is absolute, not scaled by the span width — on large-magnitude spans (e.g. λspan = (1e9, 2e9)) bisection can push dλ below eps(λ) before the floor bites, making λ + dλ == λ, so there is now a stall guard returning ReturnCode.Stalled instead of looping forever, with a test locking it. A span-scaled floor (sqrt(eps(λT)) * abs(λ1 - λ0)) would largely keep the sweep out of that regime — preference?
| elseif alg.adaptive && abs(dλ) / 2 >= alg.min_dλ | ||
| # conservative: step size is not restored after a bisection | ||
| dλ = dλ / 2 # bisect; retry from the same λ (do not advance) |
There was a problem hiding this comment.
This is a bit simple, but can be improved in a future PR.
There was a problem hiding this comment.
Agreed; keeping bisection-only here, with step regrowth and/or a predictor-corrector as the natural follow-up. One scoping question: with no regrowth, a single hard region that collapses dλ to the floor commits the sweep to up to span/min_dλ further inner solves. An outer maxsteps cap (or regrowth itself) would bound that — left out to keep this PR minimal.
|
Close but there's still a bit to do for a first merge. But main thing, the homotopy parameter should definitely be separated from the full parameter vector. |
Review feedback (SciML#962): - λ is a separate scalar argument now: each step wraps f with FixLambda (a named callable, so the inner solver compiles once) and hands the inner solver a plain NonlinearProblem with the user's p untouched; _lambda_setter machinery removed - copy(last_sol.u): sol.u may alias solver-internal storage that the next inner solve can mutate (kwargs incl. alias_u0 are forwarded), which would corrupt the last-converged-iterate guarantee of the failure path - step-size kwargs per review: nsteps = nothing + initial_step_factor = 0.1 defaults; adaptive = false requires explicit nsteps - min_dλ = nothing resolves to sqrt(eps(typeof(λ))) so the floor scales with the working precision instead of a fixed 1e-3
|
λ is now fully separated from the parameter vector across both PRs; SciML/SciMLBase.jl#1375 is updated with the A few additional hardening changes landed alongside, so the diff holds no surprises: problem-level |
|
Bump the SciMLBase minimum to 3.19 |
| @@ -0,0 +1,188 @@ | |||
| @testitem "HomotopySweep construction + defaults" tags = [:core] begin | |||
There was a problem hiding this comment.
Update to the new test format.
There was a problem hiding this comment.
Converted following the #961 layout — each @testitem is now a standalone test/Core/homotopy_sweep_tests__itemN.jl included via @safetestset from the Core group in test/runtests.jl.
Follow the post-SciML#961 canonical structure: each former @testitem in test/homotopy_sweep_tests.jl becomes a standalone test/Core/homotopy_sweep_tests__itemN.jl, registered via @safetestset in the Core group block of test/runtests.jl. Test bodies are unchanged (14 items, 45 assertions).
…earSolve 4.20.0 HomotopyProblem ships in SciMLBase 3.19.0, so the minimum moves to 3.19 in the root and NonlinearSolveBase Project.tomls (as requested in review). NonlinearSolveBase gains HomotopySweep (feature -> 2.31.0); the root re-exports it and its Core test group references it, so the root NonlinearSolveBase floor rises to 2.31 (-> 4.20.0). The lts grids and the root Downgrade job resolve NonlinearSolveBase from the registry and stay red until 2.31.0 is registered, then heal automatically.
The HomotopySweep default-algorithm entry points solve(::HomotopyProblem, ::Nothing, ...) and solve(::HomotopyProblem; ...) are all-foreign signatures, so Aqua.test_piracies flags them. Add HomotopyProblem to treat_as_own, matching the existing NonlinearProblem entry (Aqua matches treat_as_own exactly and does not expand subtypes, so the AbstractNonlinearProblem entry does not cover it).
|
Done — Three release-chain items came out of checking the CI semantics:
|
|
There are still a lot of things I want to do to improve the solver here, but this is a baseline that I think is good to start with. |
| local last_sol | ||
|
|
||
| while true | ||
| next_λ = abs(λend - λ) <= abs(dλ) ? λend : λ + dλ |
There was a problem hiding this comment.
This isn't evaluated at 0, so the initial is never found 😅
The sweep initialized λ = λspan[1] but the first inner solve ran at λspan[1] + dλ warm-started from u0, so the system at λspan[1] -- the `simplified` system for the canonical (0, 1) span -- was never solved. Adaptive bisection only fires on inner-solve failure, so a poor u0 that converges onto the wrong branch at λspan[1] + dλ is tracked to a wrong root with a success retcode (e.g. H = (1-λ)(u-4) + λ(u^2-4) from u0 = -10 returns -2 instead of +2). Solve at λ = λspan[1] from u0 before stepping; continue from that iterate. The zero-width-span, adaptive-bisection, and failure contracts are preserved -- a failed anchor fails fast with the inner retcode and leaves u at u0. Tests: item15 (wrong-branch regression -> +2), item16 (anchor-failure contract), item17 (zero-width span); item10 rewritten to keep a solvable λspan[1] anchor so it still exercises the mid-sweep stall guard. Follow-up to SciML#962.
…on) (#972) The sweep initialized λ = λspan[1] but the first inner solve ran at λspan[1] + dλ warm-started from u0, so the system at λspan[1] -- the `simplified` system for the canonical (0, 1) span -- was never solved. Adaptive bisection only fires on inner-solve failure, so a poor u0 that converges onto the wrong branch at λspan[1] + dλ is tracked to a wrong root with a success retcode (e.g. H = (1-λ)(u-4) + λ(u^2-4) from u0 = -10 returns -2 instead of +2). Solve at λ = λspan[1] from u0 before stepping; continue from that iterate. The zero-width-span, adaptive-bisection, and failure contracts are preserved -- a failed anchor fails fast with the inner retcode and leaves u at u0. Tests: item15 (wrong-branch regression -> +2), item16 (anchor-failure contract), item17 (zero-width span); item10 rewritten to keep a solvable λspan[1] anchor so it still exercises the mid-sweep stall guard. Follow-up to #962.
Summary
Adds
HomotopySweep, a natural-parameter continuation solver forSciMLBase.HomotopyProblem. Given a homotopy family0 = H(u, p, λ), it sweepsλacross the problem'sλspan(default0 → 1), warm-starting an inner nonlinear solve at each step from the previous step's solution, with step-size bisection on a non-converging step. This robustly solves systems whose target form (λ=1) is hard to solve cold but whoseλ=0form is easy.It is MTK-independent — any user with a hand-built
HomotopyProblemcansolve(prob, HomotopySweep()). It is unrelated to the polynomialHomotopyContinuationJL(which tracks complex polynomial paths via HomotopyContinuation.jl);HomotopySweepis a real-arithmetic embedding-homotopy continuation for general nonlinear systems.API
inner— inner nonlinear algorithm used at each λ-step;nothingselects the default polyalgorithm (not a hardcoded Newton).nsteps— number of λ steps acrossλspan.adaptive/min_dλ— on a non-converging inner solve, halve the λ increment and retry, down tomin_dλ; otherwise surface the inner solver's failure retcode.A
HomotopyProblemsolved with no algorithm (solve(prob, nothing)) defaults toHomotopySweep().The algorithm lives in
NonlinearSolveBase(beside the sibling meta-algorithmNonlinearSolvePolyAlgorithm) and is re-exported by NonlinearSolve.Tests
test/Core/homotopy_sweep_tests__item*.jl(Core group): construction/defaults; monotone-sweep convergence; an out-of-basin rescue acceptance test (a guess from which a cold Newton diverges —atan(x-3)saturation — rescued to the actual root by continuation); failure retcode when the family has no real root past a fold; composable inner solver (default polyalg / NewtonRaphson / SimpleNewtonRaphson); integer-eltypep; and thenothing-algorithm default route.Depends on
SciML/SciMLBase.jl#1375 — adds
HomotopyProblem, which this solver dispatches on. This PR should merge after that one releases; the[compat] SciMLBaselower bound will be bumped to the release carryingHomotopyProblem.Part of a series
Second of three coordinated PRs for the Modelica
homotopy()operator:SciMLBase
HomotopyProblem(SciML/SciMLBase.jl#1375) → (this PR) NonlinearSolveHomotopySweep→ ModelingToolkit lowering (to follow).