Parser
Index
Documentation
CTBase.constraint_type
— Methodconstraint_type(
e,
t,
t0,
tf,
x,
u,
v
) -> Union{Symbol, Tuple{Symbol, Any}}
Return the type constraint among :initial
, :final
, :boundary
, :control_range
, :control_fun
, :state_range
, :state_fun
, :mixed
, :variable_range
, :variable_fun
(:other
otherwise), together with the appropriate value (range, updated expression...) Expressions like u(t0)
where u
is the control and t0
the initial time return :other
.
Example
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u; v = :v
julia> constraint_type(:( ẏ(t) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( ẋ(s) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( x(0)' ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( x(t)' ), t, t0, tf, x, u, v)
:state_fun
julia> constraint_type(:( x(0) ), t, t0, tf, x, u, v)
(:initial, nothing)
julia> constraint_type(:( x[1:2:5](0) ), t, t0, tf, x, u, v)
(:initial, 1:2:5)
julia> constraint_type(:( x[1:2](0) ), t, t0, tf, x, u, v)
(:initial, 1:2)
julia> constraint_type(:( x[1](0) ), t, t0, tf, x, u, v)
(:initial, 1)
julia> constraint_type(:( 2x[1](0)^2 ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( x(tf) ), t, t0, tf, x, u, v)
(:final, nothing)
j
julia> constraint_type(:( x[1:2:5](tf) ), t, t0, tf, x, u, v)
(:final, 1:2:5)
julia> constraint_type(:( x[1:2](tf) ), t, t0, tf, x, u, v)
(:final, 1:2)
julia> constraint_type(:( x[1](tf) ), t, t0, tf, x, u, v)
(:final, 1)
julia> constraint_type(:( 2x[1](tf)^2 ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( x[1](tf) - x[2](0) ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( u[1:2:5](t) ), t, t0, tf, x, u, v)
(:control_range, 1:2:5)
julia> constraint_type(:( u[1:2](t) ), t, t0, tf, x, u, v)
(:control_range, 1:2)
julia> constraint_type(:( u[1](t) ), t, t0, tf, x, u, v)
(:control_range, 1)
julia> constraint_type(:( u(t) ), t, t0, tf, x, u, v)
(:control_range, nothing)
julia> constraint_type(:( 2u[1](t)^2 ), t, t0, tf, x, u, v)
:control_fun
julia> constraint_type(:( x[1:2:5](t) ), t, t0, tf, x, u, v)
(:state_range, 1:2:5)
julia> constraint_type(:( x[1:2](t) ), t, t0, tf, x, u, v)
(:state_range, 1:2)
julia> constraint_type(:( x[1](t) ), t, t0, tf, x, u, v)
(:state_range, 1)
julia> constraint_type(:( x(t) ), t, t0, tf, x, u, v)
(:state_range, nothing)
julia> constraint_type(:( 2x[1](t)^2 ), t, t0, tf, x, u, v)
:state_fun
julia> constraint_type(:( 2u[1](t)^2 * x(t) ), t, t0, tf, x, u, v)
:mixed
julia> constraint_type(:( 2u[1](0)^2 * x(t) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( 2u[1](0)^2 * x(t) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( 2u[1](t)^2 * x(t) + v ), t, t0, tf, x, u, v)
:mixed
julia> constraint_type(:( v[1:2:10] ), t, t0, tf, x, u, v)
(:variable_range, 1:2:9)
julia> constraint_type(:( v[1:10] ), t, t0, tf, x, u, v)
(:variable_range, 1:10)
julia> constraint_type(:( v[2] ), t, t0, tf, x, u, v)
(:variable_range, 2)
julia> constraint_type(:( v ), t, t0, tf, x, u, v)
(:variable_range, nothing)
julia> constraint_type(:( v^2 + 1 ), t, t0, tf, x, u, v)
:variable_fun
julia> constraint_type(:( v[2]^2 + 1 ), t, t0, tf, x, u, v)
:variable_fun
CTBase.replace_call
— Methodreplace_call(e, x::Symbol, t, y) -> Any
Replace calls in e of the form (...x...)(t)
by (...y...)
.
Example
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;
julia> e = :( x[1](0) * 2x(tf) - x[2](tf) * 2x(0) )
:((x[1])(0) * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))
julia> x0 = Symbol(x, 0); e = replace_call(e, x, t0, x0)
:(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2x0))
julia> xf = Symbol(x, "f"); replace_call(ans, x, tf, xf)
:(x0[1] * (2xf) - xf[2] * (2x0))
julia> e = :( A*x(t) + B*u(t) ); replace_call(replace_call(e, x, t, x), u, t, u)
:(A * x + B * u)
julia> e = :( F0(x(t)) + u(t)*F1(x(t)) ); replace_call(replace_call(e, x, t, x), u, t, u)
:(F0(x) + u * F1(x))
julia> e = :( 0.5u(t)^2 ); replace_call(e, u, t, u)
:(0.5 * u ^ 2)
CTBase.replace_call
— Methodreplace_call(e, x::Vector{Symbol}, t, y) -> Any
Replace calls in e of the form (...x1...x2...)(t)
by (...y1...y2...)
for all symbols x1
, x2
... in the vector x
.
Example
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;
julia> e = :( (x^2 + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:(xx ^ 2 + uu[1])
julia> e = :( ((x^2)(t) + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:(xx ^ 2 + uu[1])
julia> e = :( ((x^2)(t0) + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:((xx ^ 2)(t0) + uu[1])
CTBase.@def
— MacroDefine an optimal control problem. One pass parsing of the definition. Can be used writing either ocp = @def begin ... end
or @def ocp begin ... end
. In the second case, setting log
to true
will display the parsing steps.
Example
ocp = @def begin
tf ∈ R, variable
t ∈ [ 0, tf ], time
x ∈ R², state
u ∈ R, control
tf ≥ 0
-1 ≤ u(t) ≤ 1
q = x₁
v = x₂
q(0) == 1
v(0) == 2
q(tf) == 0
v(tf) == 0
0 ≤ q(t) ≤ 5, (1)
-2 ≤ v(t) ≤ 3, (2)
ẋ(t) == [ v(t), u(t) ]
tf → min
end
@def ocp begin
tf ∈ R, variable
t ∈ [ 0, tf ], time
x ∈ R², state
u ∈ R, control
tf ≥ 0
-1 ≤ u(t) ≤ 1
q = x₁
v = x₂
q(0) == 1
v(0) == 2
q(tf) == 0
v(tf) == 0
0 ≤ q(t) ≤ 5, (1)
-2 ≤ v(t) ≤ 3, (2)
ẋ(t) == [ v(t), u(t) ]
tf → min
end true # final boolean to show parsing log
CTBase.__OCPModel
— Method__OCPModel(args...; kwargs...) -> OptimalControlModel
Redirection to Model
to avoid confusion with other functions Model from other packages if imported. This function is used by @def
.