CTParser.jl

The CTParser.jl package is part of the control-toolbox ecosystem.

flowchart TD B(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctbase.html'>CTBase</a>) M(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctmodels.html'>CTModels</a>) P(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctparser.html'>CTParser</a>) O(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-optimalcontrol.html'>OptimalControl</a>) D(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctdirect.html'>CTDirect</a>) F(<a href='https://control-toolbox.org/OptimalControl.jl/stable/dev-ctflows.html'>CTFlows</a>) O --> D O --> M O --> F O --> P F --> M O --> B F --> B D --> B D --> M P --> B M --> B style P fill:#FBF275

Index

Documentation

Public

CTParser.CTParserModule

CTParser module.

Lists all the imported modules and packages:

  • Base
  • Core
  • DocStringExtensions
  • MLStyle
  • OrderedCollections
  • Parameters
  • Unicode

List of all the exported names:

source

Private

CTParser.constraint_typeMethod
constraint_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
source
CTParser.expr_itMethod
expr_it(e, _Expr, f) -> Any

Expr iterator: apply _Expr to nodes and f to leaves of the AST.

Example

julia> id(e) = expr_it(e, Expr, x -> x)
source
CTParser.hasMethod
has(e, x, t) -> Union{Missing, Bool}

Return true if e contains a (...x...)(t) call.

Example

julia> e = :( ∫( x[1](t)^2 + 2*u(t) ) → min )
:(∫((x[1])(t) ^ 2 + 2 * u(t)) → min)

julia> has(e, :x, :t)
true

julia> has(e, :u, :t)
true
source
CTParser.hasMethod
has(e, e1) -> Union{Missing, Bool}

Return true if e contains e1.

Example

julia> e = :( ∫( x[1](t)^2 + 2*u(t) ) → min )
:(∫((x[1])(t) ^ 2 + 2 * u(t)) → min)

julia> has(e, 2)
true

julia> has(e, :x)
true

julia> has(e, :min)
true

julia> has(e, :( x[1](t)^2 ))
true

julia> !has(e, :( x[1](t)^3 ))
true

julia> !has(e, 3)
true

julia> !has(e, :max)
true

julia> has(:x, :x)
true

julia> !has(:x, 2)
true

julia> !has(:x, :y)
true
source
CTParser.parse!Method
parse!(p, p_ocp, e; log) -> Union{Expr, LineNumberNode}

Parse the expression e and update the ParsingInfo structure p.

Example

parse!(p, :p_ocp, :(v ∈ R, variable))
source
CTParser.replace_callMethod
replace_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)
source
CTParser.replace_callMethod
replace_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])
source
CTParser.subsMethod
subs(e, e1::Union{Real, Symbol}, e2) -> Any

Substitute expression e1 by expression e2 in expression e.

Examples

julia> e = :( ∫( r(t)^2 + 2u₁(t)) → min )
:(∫(r(t) ^ 2 + 2 * u₁(t)) → min)

julia> subs(e, :r, :( x[1] ))
:(∫((x[1])(t) ^ 2 + 2 * u₁(t)) → min)

julia> e = :( ∫( u₁(t)^2 + 2u₂(t)) → min )
:(∫(u₁(t) ^ 2 + 2 * u₂(t)) → min)

julia> for i ∈ 1:2
       e = subs(e, Symbol(:u, Char(8320+i)), :( u[$i] ))
       end; e
:(∫((u[1])(t) ^ 2 + 2 * (u[2])(t)) → min)

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); subs(e, :( $x[1]($(t0)) ), :( $x0[1] ))
:(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))
source
CTParser.@defMacro

Define 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
source