Skip to content

[RFC] Add functional guesses - #4839

Merged
AayushSabharwal merged 10 commits into
SciML:masterfrom
SebastianM-C:smc/functional_guesses
Aug 9, 2026
Merged

[RFC] Add functional guesses#4839
AayushSabharwal merged 10 commits into
SciML:masterfrom
SebastianM-C:smc/functional_guesses

Conversation

@SebastianM-C

@SebastianM-C SebastianM-C commented Jul 28, 2026

Copy link
Copy Markdown
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

For dynamic optimization problems it is not enough to provide constant guesses for the initial conditions and in some cases a functional form of the guess is needed. This also has a precedent in BoundaryValueDiffEq with continuation: https://docs.sciml.ai/BoundaryValueDiffEq/dev/tutorials/continuation/#On-providing-initial-guess

How do we want the interface for this to look like? The proposal here is initial_trajectory as a Dict mapping the variables to the function, but we can adapt this to be closer to BVP.

Another question is how should the free final time problems be handled. We could internally scale the time if the simpler approach in this PR is not good enough.

@SebastianM-C
SebastianM-C force-pushed the smc/functional_guesses branch from 5a2e50b to b8da5fe Compare July 29, 2026 01:04
@ChrisRackauckas

Copy link
Copy Markdown
Member

I think in principle this is good. It doesn't even need to just be for dynamic optimization?

@SebastianM-C

Copy link
Copy Markdown
Member Author

yeah, we could have it interpreted in other problem types. is the name ok or should that be adjusted?

)
)

return Symbolics.build_function(expr, iv; expression = Val(false))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use ModelingToolkitBase.build_function_wrapper and then eval_or_rgf to get an RGF out.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and also moved the reusable codegen part to codegen.jl

opts = GeneratedFunctionOptions(; expression = Val{false}, eval_expression, eval_module)
rgf = generate_trajectory(sys, expr, opts)

return Base.Fix1(rgf, p)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this cause problems with remake? Or initialization, which re-creates p as a copy.

@SebastianM-C SebastianM-C Aug 7, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I don't think we run initialization on the dynamic optimization path, I'll have to double check. On remake I'm not sure how to handle it... The issue is that the backends need only a function of t, but we want to be able to use the (MTK) parameters in the functions. I don't think that we can easily remake, since almost everything else on this path has the same problem of baking the p in, as we need to substitute in the backend specific parameter representation. Probably remake needs to go through the process_ function again and we don't have a fast path remake possible for external backends, that would need to remake the underlying backend specific problem...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, that makes sense

@AayushSabharwal

Copy link
Copy Markdown
Member

One of the dynamic optimization tests is failing in Tests / tests / Optimization (julia 1, ubuntu-latest) / Tests - Optimization. Could you also rebase so the rest of the failures disappear?

SebastianM-C and others added 10 commits August 7, 2026 15:09
Allow users to provide function-valued initial trajectories for state
variables via the initial_trajectory kwarg. Functions are applied via
dispatched set_initial_trajectory! after variable creation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Add a tutorial section covering the `initial_trajectory` keyword: the
map-from-states-to-functions form, that unlisted states keep their constant
`u0map` seed, backend support (JuMP, InfiniteOpt, CasADi), and the normalized
`[0, 1]` time base used for free final time problems.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Take the trajectory guess as a symbolic expression in the independent variable
rather than a function, keeping the interface consistent with the other symbolic
keyword arguments. Parameters referenced by the expression are resolved from the
operating point, and the expression is then compiled to a callable, which is what
InfiniteOpt's `set_start_value` requires.

Expressions that reduce to a constant become constant functions, and a `Function`
is still passed through unchanged so guesses that cannot be written symbolically
(an interpolation of measured data, say) remain usable. Anything left unresolved
after substitution is reported by name.

The backends are unchanged: they continue to receive a callable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Cover expression compilation, constant guesses, `Function` passthrough, parameter
resolution from the operating point, and the error raised for quantities that
remain unresolved after substitution.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Add generate_trajectory to codegen.jl producing f(p, t) per the ecosystem
convention; parameters are read from the parameter object at evaluation
time instead of being substituted numerically. Trajectories are
symbolic-only for now.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@SebastianM-C
SebastianM-C force-pushed the smc/functional_guesses branch from bfb53a1 to 68a4281 Compare August 7, 2026 12:13
@AayushSabharwal
AayushSabharwal merged commit 8b2d9c8 into SciML:master Aug 9, 2026
78 of 93 checks passed
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