Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Onepass #132

Merged
merged 15 commits into from
May 23, 2024
23 changes: 12 additions & 11 deletions src/ctparser_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ expr_it(e, _Expr, f) =
if e isa Expr
args = e.args
n = length(args)
newargs = [ expr_it(e.args[i], _Expr, f) for i ∈ 1:n ]
newargs = [ expr_it(e.args[i], _Expr, f) for i ∈ 1:n ]
return _Expr(e.head, newargs...)
else
return f(e)
Expand Down Expand Up @@ -115,15 +115,15 @@ replace_call(e, x::Vector{Symbol}, t, y) = begin
@assert length(x) == length(y)
foo(x, t, y) = (h, args...) -> begin
ee = Expr(h, args...)
@match ee begin
@match ee begin
:( $eee($tt) ) && if tt == t end =>
let ch = false
for i ∈ 1:length(x)
if has(eee, x[i])
eee = subs(eee, x[i], y[i])
ch = true
if has(eee, x[i])
eee = subs(eee, x[i], y[i])
ch = true # todo: unnecessary (as subs can be idempotent)?
end
end
end
ch ? eee : ee
end
_ => ee
Expand Down Expand Up @@ -188,7 +188,7 @@ end
"""
$(TYPEDSIGNATURES)

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

# Example
```jldoctest
Expand All @@ -205,16 +205,17 @@ true
has(e, x, t) = begin
foo(x, t) = (h, args...) -> begin
ee = Expr(h, args...)
if :yes ∈ args
:yes
else @match ee begin
if :yes ∈ args
:yes
else @match ee begin
:( $eee($tt) ) => (tt == t && has(eee, x)) ? :yes : ee
_ => ee end
end
end
expr_it(e, foo(x, t), x -> x) == :yes
end


"""
$(TYPEDSIGNATURES)

Expand Down Expand Up @@ -369,4 +370,4 @@ constraint_type(e, t, t0, tf, x, u, v) = begin
_ => :variable_fun end
_ => :other
end
end
end
73 changes: 49 additions & 24 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Define the variable dimension and possibly the names of each component.
# Examples
```jldoctest
julia> variable!(ocp, 1, "v")
julia> variable!(ocp, 2, "v", [ "v₁", "v₂" ])
julia> variable!(ocp, 2, "v", [ "v₁", "v₂" ])
```
"""
function variable!(ocp::OptimalControlModel, q::Dimension, name::String=__variable_name(),
Expand All @@ -129,6 +129,14 @@ function variable!(ocp::OptimalControlModel, q::Dimension, name::String=__variab
nothing # to force to return nothing
end

function variable!(ocp::OptimalControlModel, q::Dimension, name::Symbol, components_names::Vector{Symbol})
variable!(ocp, q, string(name), string.(components_names))
end

function variable!(ocp::OptimalControlModel, q::Dimension, name::Symbol, components_names::Vector{String})
variable!(ocp, q, string(name), components_names)
end

function variable!(ocp::OptimalControlModel, q::Dimension, name::Symbol)
variable!(ocp, q, string(name))
end
Expand Down Expand Up @@ -187,6 +195,15 @@ function state!(ocp::OptimalControlModel, n::Dimension, name::String=__state_nam
ocp.state_name = name
nothing # to force to return nothing
end

function state!(ocp::OptimalControlModel, n::Dimension, name::Symbol, components_names::Vector{Symbol})
state!(ocp, n, string(name), string.(components_names))
end

function state!(ocp::OptimalControlModel, n::Dimension, name::Symbol, components_names::Vector{String})
state!(ocp, n, string(name), components_names)
end

function state!(ocp::OptimalControlModel, n::Dimension, name::Symbol)
state!(ocp, n, string(name))
end
Expand Down Expand Up @@ -246,6 +263,14 @@ function control!(ocp::OptimalControlModel, m::Dimension, name::String=__control
nothing # to force to return nothing
end

function control!(ocp::OptimalControlModel, m::Dimension, name::Symbol, components_names::Vector{Symbol})
control!(ocp, m, string(name), string.(components_names))
end

function control!(ocp::OptimalControlModel, m::Dimension, name::Symbol, components_names::Vector{String})
control!(ocp, m, string(name), components_names)
end

function control!(ocp::OptimalControlModel, m::Dimension, name::Symbol)
control!(ocp, m, string(name))
end
Expand Down Expand Up @@ -438,12 +463,12 @@ Add an `:initial`, `:final`, `:control`, `:state` or `:variable` box constraint
# Examples

```jldoctest
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ], [ 1, 2 ])
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ], [ 1, 2 ])
julia> constraint!(ocp, :final, Index(1), 0, 2)
julia> constraint!(ocp, :control, Index(1), 0, 2)
julia> constraint!(ocp, :state, 2:3, [ 0, 0 ], [ 1, 2 ])
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ])
julia> constraint!(ocp, :state, 2:3, [ 0, 0 ], [ 1, 2 ])
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ])
```
"""
function constraint!(ocp::OptimalControlModel{<: TimeDependence, V}, type::Symbol, rg::RangeConstraint, lb::ctVector, ub::ctVector,
Expand Down Expand Up @@ -512,8 +537,8 @@ Add an `:initial` or `:final` value constraint on a range of the state, or a val
# Examples

```jldoctest
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ])
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ])
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ])
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ])
julia> constraint!(ocp, :final, Index(2), 0)
julia> constraint!(ocp, :variable, 2:3, [ 0, 3 ])
```
Expand Down Expand Up @@ -566,9 +591,9 @@ Add an `:initial` or `:final` value constraint on the state, or a `:variable` va
# Examples

```jldoctest
julia> constraint!(ocp, :initial, [ 0, 0 ])
julia> constraint!(ocp, :initial, [ 0, 0 ])
julia> constraint!(ocp, :final, 2) # if the state is of dimension 1
julia> constraint!(ocp, :variable, [ 3, 0, 1 ])
julia> constraint!(ocp, :variable, [ 3, 0, 1 ])
```
"""
function constraint!(ocp::OptimalControlModel, type::Symbol, val::ctVector, label::Symbol=__constraint_label())
Expand Down Expand Up @@ -618,10 +643,10 @@ Add an `:initial`, `:final`, `:control`, `:state` or `:variable` box constraint
# Examples

```jldoctest
julia> constraint!(ocp, :initial, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :final, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :initial, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :final, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :control, [ 0, 0 ], [ 2, 3 ])
julia> constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :variable, 0, 1) # the variable here is of dimension 1
```
"""
Expand Down Expand Up @@ -687,22 +712,22 @@ julia> constraint!(ocp, :boundary, (x0, xf, v) -> x0[3]+xf[2]*v[1], 0, 1)

# time independent and variable independent ocp
julia> constraint!(ocp, :control, u -> 2u, 0, 1)
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :mixed, (x, u) -> x[1]-u, 0, 1)

# time dependent and variable independent ocp
julia> constraint!(ocp, :control, (t, u) -> 2u, 0, 1)
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :mixed, (t, x, u) -> x[1]-u, 0, 1)

# time independent and variable dependent ocp
julia> constraint!(ocp, :control, (u, v) -> 2u*v[1], 0, 1)
julia> constraint!(ocp, :state, (x, v) -> x-v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :state, (x, v) -> x-v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :mixed, (x, u, v) -> x[1]-v[2]*u, 0, 1)

# time dependent and variable dependent ocp
julia> constraint!(ocp, :control, (t, u, v) -> 2u+v[2], 0, 1)
julia> constraint!(ocp, :state, (t, x, v) -> x-t*v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :state, (t, x, v) -> x-t*v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
julia> constraint!(ocp, :mixed, (t, x, u, v) -> x[1]*v[2]-u, 0, 1)
```
"""
Expand Down Expand Up @@ -761,22 +786,22 @@ julia> constraint!(ocp, :boundary, (x0, xf, v) -> x0[3]+xf[2]*v[1], 0)

# time independent and variable independent ocp
julia> constraint!(ocp, :control, u -> 2u, 1)
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ])
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ])
julia> constraint!(ocp, :mixed, (x, u) -> x[1]-u, 0)

# time dependent and variable independent ocp
julia> constraint!(ocp, :control, (t, u) -> 2u, 1)
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ])
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ])
julia> constraint!(ocp, :mixed, (t, x, u) -> x[1]-u, 0)

# time independent and variable dependent ocp
julia> constraint!(ocp, :control, (u, v) -> 2u*v[1], 1)
julia> constraint!(ocp, :state, (x, v) -> x-v[2], [ 0, 0, 0 ])
julia> constraint!(ocp, :state, (x, v) -> x-v[2], [ 0, 0, 0 ])
julia> constraint!(ocp, :mixed, (x, u) -> x[1]-u+v[1], 0)

# time dependent and variable dependent ocp
julia> constraint!(ocp, :control, (t, u, v) -> 2u-t*v[2], 1)
julia> constraint!(ocp, :state, (t, x, v) -> x-t+v[1], [ 0, 0, 0 ])
julia> constraint!(ocp, :state, (t, x, v) -> x-t+v[1], [ 0, 0, 0 ])
julia> constraint!(ocp, :mixed, (t, x, u, v) -> x[1]-u*v[1], 0)
```
"""
Expand All @@ -800,12 +825,12 @@ Add an `:initial`, `:final`, `:control`, `:state` or `:variable` box constraint
# Examples

```jldoctest
julia> constraint!(ocp, :initial, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :initial, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :final, val=Index(1), lb=0, ub=2)
julia> constraint!(ocp, :control, val=Index(1), lb=0, ub=2)
julia> constraint!(ocp, :state, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :initial, rg=1:2:5, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :variable, rg=1:2, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :state, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
julia> constraint!(ocp, :initial, rg=1:2:5, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
julia> constraint!(ocp, :variable, rg=1:2, lb=[ 0, 0 ], ub=[ 1, 2 ])
```
"""
function constraint!(ocp::OptimalControlModel{<: TimeDependence, <: VariableDependence}, type::Symbol;
Expand Down
Loading
Loading