Private API
This page lists non-exported (internal) symbols of CTParser.
From CTParser
@init
CTParser.@init — Macro
@init ocp begin
...
endBuild an initial guess object for an optimal control problem from a small initialisation DSL.
The block following @init is interpreted as a collection of assignment rules for the state, control and variable components of an optimal control problem, using a compact syntax of the form
q(t) := sin(t) # time-dependent function
x(T) := X # time grid and associated samples
u := 0.1 # constant value
a = 1.0 # ordinary Julia alias (not part of the initial guess)
v(t) := a # time-dependent function using the alias aboveThe macro itself only rewrites this DSL into a NamedTuple-based representation. All dimensional checks, interpretation of aliases and construction of the concrete initial guess object are delegated to the backend selected by init_prefix (by défaut :CTModels), via build_initial_guess and validate_initial_guess.
An optional keyword-like trailing argument controls logging:
ig = @init ocp begin
u(t) := t
end log = trueWhen log = true, the macro additionally prints a human-readable NamedTuple-like representation of the specification.
Arguments
ocp: symbolic optimal control problem built with@def.begin ... end: block containing the initialisation DSL.log: optional Boolean keyword (defaultfalse) enabling textual logging of the parsed specification.
Returns
AbstractOptimalControlInitialGuess: backend-specific initial guess object produced by the current backend (par défautCTModels).
Example
julia> using CTParser
julia> ocp = @def begin
t ∈ [0, 1], time
x ∈ R, state
u ∈ R, control
ẋ(t) == u(t)
x(0) == 0
x(1) == 0
∫(0.5u(t)^2) → min
end
julia> ig = @init ocp begin
u(t) := t
end
julia> ig isa CTModels.AbstractOptimalControlInitialGuess
trueINIT_PREFIX
CTParser.INIT_PREFIX — Constant
Current backend prefix used by @init.
This reference stores the symbol of the backend module that provides build_initial_guess and validate_initial_guess. It is initialised by __default_init_prefix and can be updated at runtime via init_prefix!.
__default_init_prefix
CTParser.__default_init_prefix — Function
__default_init_prefix() -> Symbol
Return the default backend prefix used by @init.
The returned symbol identifies the module that implements build_initial_guess and validate_initial_guess. In the current implementation this is :CTModels.
Returns
Symbol: name of the default backend module.
Example
julia> using CTParser
julia> CTParser.__default_init_prefix()
:CTModels_collect_init_specs
CTParser._collect_init_specs — Function
_collect_init_specs(
ex
) -> Tuple{Vector{Expr}, Vector{Symbol}, Vector{Any}}
Internal helper that parses the body of an @init block.
The function walks through the expression ex and splits it into
- alias statements, which are left as ordinary Julia assignments and executed verbatim inside the generated block;
- initialisation specifications of the form
lhs := rhsorlhs(t) := rhs/lhs(T) := rhs, which are converted into keys and values used to build aNamedTuple.
Arguments
ex::Any: expression or block coming from the body of@init.
Returns
alias_stmts::Vector{Expr}: ordinary statements to execute before building the initial guess.keys::Vector{Symbol}: names of the components being initialised (e.g.:q,:v,:u,:tf).vals::Vector{Any}: expressions representing the corresponding values, functions or(T, data)pairs.
init_fun
CTParser.init_fun — Function
init_fun(ocp, e) -> Tuple{String, Expr}
Lowering function used by @init to build the expanded code.
Given an optimal control problem ocp and the body e of an @init block, this function collects the initialisation specifications, builds an appropriate NamedTuple expression and constructs the call to build_initial_guess / validate_initial_guess on the current backend (prefix returned by init_prefix).
It also produces a compact string representation of the specification, used for optional logging when log = true is requested at the macro level.
Arguments
ocp: symbolic optimal control problem built with@def.e: expression corresponding to the body of the@initblock.
Returns
log_str::String: human-readableNamedTuple-like description of the specification.code_expr::Expr: block of Julia code that builds and validates the initial guess when executed.
init_prefix
CTParser.init_prefix — Function
init_prefix() -> Symbol
Return the current backend prefix used by @init.
This is the symbol of the module that will be used to interpret the NamedTuple produced by the initialisation DSL, typically :CTModels.
Returns
Symbol: name of the backend module currently used by@init.
Example
julia> using CTParser
julia> CTParser.init_prefix()
:CTModelsinit_prefix!
CTParser.init_prefix! — Function
init_prefix!(p)
Set the backend prefix used by @init.
This function updates the global INIT_PREFIX, thereby changing which module is used to build and validate initial guesses.
Arguments
p::Symbol: name of the backend module to use (e.g.:CTModels).
Returns
Nothing.
Example
julia> using CTParser
julia> old = CTParser.init_prefix();
julia> CTParser.init_prefix!(:MyBackend)
julia> CTParser.init_prefix()
:MyBackend
julia> CTParser.init_prefix!(old); # restore