Skip to content

Commit 1aeaa43

Browse files
authored
Merge pull request #132 from control-toolbox/onepass
WIP - Onepass
2 parents 9c156ad + 58e4b75 commit 1aeaa43

File tree

7 files changed

+1894
-1157
lines changed

7 files changed

+1894
-1157
lines changed

src/ctparser_utils.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ expr_it(e, _Expr, f) =
1414
if e isa Expr
1515
args = e.args
1616
n = length(args)
17-
newargs = [ expr_it(e.args[i], _Expr, f) for i 1:n ]
17+
newargs = [ expr_it(e.args[i], _Expr, f) for i 1:n ]
1818
return _Expr(e.head, newargs...)
1919
else
2020
return f(e)
@@ -115,15 +115,15 @@ replace_call(e, x::Vector{Symbol}, t, y) = begin
115115
@assert length(x) == length(y)
116116
foo(x, t, y) = (h, args...) -> begin
117117
ee = Expr(h, args...)
118-
@match ee begin
118+
@match ee begin
119119
:( $eee($tt) ) && if tt == t end =>
120120
let ch = false
121121
for i 1:length(x)
122-
if has(eee, x[i])
123-
eee = subs(eee, x[i], y[i])
124-
ch = true
122+
if has(eee, x[i])
123+
eee = subs(eee, x[i], y[i])
124+
ch = true # todo: unnecessary (as subs can be idempotent)?
125+
end
125126
end
126-
end
127127
ch ? eee : ee
128128
end
129129
_ => ee
@@ -188,7 +188,7 @@ end
188188
"""
189189
$(TYPEDSIGNATURES)
190190
191-
Return true if e contains an `(...x...)(t)` call.
191+
Return true if e contains a `(...x...)(t)` call.
192192
193193
# Example
194194
```jldoctest
@@ -205,16 +205,17 @@ true
205205
has(e, x, t) = begin
206206
foo(x, t) = (h, args...) -> begin
207207
ee = Expr(h, args...)
208-
if :yes args
209-
:yes
210-
else @match ee begin
208+
if :yes args
209+
:yes
210+
else @match ee begin
211211
:( $eee($tt) ) => (tt == t && has(eee, x)) ? :yes : ee
212212
_ => ee end
213213
end
214214
end
215215
expr_it(e, foo(x, t), x -> x) == :yes
216216
end
217217

218+
218219
"""
219220
$(TYPEDSIGNATURES)
220221
@@ -369,4 +370,4 @@ constraint_type(e, t, t0, tf, x, u, v) = begin
369370
_ => :variable_fun end
370371
_ => :other
371372
end
372-
end
373+
end

src/model.jl

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Define the variable dimension and possibly the names of each component.
113113
# Examples
114114
```jldoctest
115115
julia> variable!(ocp, 1, "v")
116-
julia> variable!(ocp, 2, "v", [ "v₁", "v₂" ])
116+
julia> variable!(ocp, 2, "v", [ "v₁", "v₂" ])
117117
```
118118
"""
119119
function variable!(ocp::OptimalControlModel, q::Dimension, name::String=__variable_name(),
@@ -129,6 +129,14 @@ function variable!(ocp::OptimalControlModel, q::Dimension, name::String=__variab
129129
nothing # to force to return nothing
130130
end
131131

132+
function variable!(ocp::OptimalControlModel, q::Dimension, name::Symbol, components_names::Vector{Symbol})
133+
variable!(ocp, q, string(name), string.(components_names))
134+
end
135+
136+
function variable!(ocp::OptimalControlModel, q::Dimension, name::Symbol, components_names::Vector{String})
137+
variable!(ocp, q, string(name), components_names)
138+
end
139+
132140
function variable!(ocp::OptimalControlModel, q::Dimension, name::Symbol)
133141
variable!(ocp, q, string(name))
134142
end
@@ -187,6 +195,15 @@ function state!(ocp::OptimalControlModel, n::Dimension, name::String=__state_nam
187195
ocp.state_name = name
188196
nothing # to force to return nothing
189197
end
198+
199+
function state!(ocp::OptimalControlModel, n::Dimension, name::Symbol, components_names::Vector{Symbol})
200+
state!(ocp, n, string(name), string.(components_names))
201+
end
202+
203+
function state!(ocp::OptimalControlModel, n::Dimension, name::Symbol, components_names::Vector{String})
204+
state!(ocp, n, string(name), components_names)
205+
end
206+
190207
function state!(ocp::OptimalControlModel, n::Dimension, name::Symbol)
191208
state!(ocp, n, string(name))
192209
end
@@ -246,6 +263,14 @@ function control!(ocp::OptimalControlModel, m::Dimension, name::String=__control
246263
nothing # to force to return nothing
247264
end
248265

266+
function control!(ocp::OptimalControlModel, m::Dimension, name::Symbol, components_names::Vector{Symbol})
267+
control!(ocp, m, string(name), string.(components_names))
268+
end
269+
270+
function control!(ocp::OptimalControlModel, m::Dimension, name::Symbol, components_names::Vector{String})
271+
control!(ocp, m, string(name), components_names)
272+
end
273+
249274
function control!(ocp::OptimalControlModel, m::Dimension, name::Symbol)
250275
control!(ocp, m, string(name))
251276
end
@@ -438,12 +463,12 @@ Add an `:initial`, `:final`, `:control`, `:state` or `:variable` box constraint
438463
# Examples
439464
440465
```jldoctest
441-
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ], [ 1, 2 ])
466+
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ], [ 1, 2 ])
442467
julia> constraint!(ocp, :final, Index(1), 0, 2)
443468
julia> constraint!(ocp, :control, Index(1), 0, 2)
444-
julia> constraint!(ocp, :state, 2:3, [ 0, 0 ], [ 1, 2 ])
445-
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ], [ 1, 2, 1 ])
446-
julia> constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ])
469+
julia> constraint!(ocp, :state, 2:3, [ 0, 0 ], [ 1, 2 ])
470+
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ], [ 1, 2, 1 ])
471+
julia> constraint!(ocp, :variable, 1:2, [ 0, 0 ], [ 1, 2 ])
447472
```
448473
"""
449474
function constraint!(ocp::OptimalControlModel{<: TimeDependence, V}, type::Symbol, rg::RangeConstraint, lb::ctVector, ub::ctVector,
@@ -512,8 +537,8 @@ Add an `:initial` or `:final` value constraint on a range of the state, or a val
512537
# Examples
513538
514539
```jldoctest
515-
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ])
516-
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ])
540+
julia> constraint!(ocp, :initial, 1:2:5, [ 0, 0, 0 ])
541+
julia> constraint!(ocp, :initial, 2:3, [ 0, 0 ])
517542
julia> constraint!(ocp, :final, Index(2), 0)
518543
julia> constraint!(ocp, :variable, 2:3, [ 0, 3 ])
519544
```
@@ -566,9 +591,9 @@ Add an `:initial` or `:final` value constraint on the state, or a `:variable` va
566591
# Examples
567592
568593
```jldoctest
569-
julia> constraint!(ocp, :initial, [ 0, 0 ])
594+
julia> constraint!(ocp, :initial, [ 0, 0 ])
570595
julia> constraint!(ocp, :final, 2) # if the state is of dimension 1
571-
julia> constraint!(ocp, :variable, [ 3, 0, 1 ])
596+
julia> constraint!(ocp, :variable, [ 3, 0, 1 ])
572597
```
573598
"""
574599
function constraint!(ocp::OptimalControlModel, type::Symbol, val::ctVector, label::Symbol=__constraint_label())
@@ -618,10 +643,10 @@ Add an `:initial`, `:final`, `:control`, `:state` or `:variable` box constraint
618643
# Examples
619644
620645
```jldoctest
621-
julia> constraint!(ocp, :initial, [ 0, 0, 0 ], [ 1, 2, 1 ])
622-
julia> constraint!(ocp, :final, [ 0, 0, 0 ], [ 1, 2, 1 ])
646+
julia> constraint!(ocp, :initial, [ 0, 0, 0 ], [ 1, 2, 1 ])
647+
julia> constraint!(ocp, :final, [ 0, 0, 0 ], [ 1, 2, 1 ])
623648
julia> constraint!(ocp, :control, [ 0, 0 ], [ 2, 3 ])
624-
julia> constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ])
649+
julia> constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ])
625650
julia> constraint!(ocp, :variable, 0, 1) # the variable here is of dimension 1
626651
```
627652
"""
@@ -687,22 +712,22 @@ julia> constraint!(ocp, :boundary, (x0, xf, v) -> x0[3]+xf[2]*v[1], 0, 1)
687712
688713
# time independent and variable independent ocp
689714
julia> constraint!(ocp, :control, u -> 2u, 0, 1)
690-
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ], [ 1, 2, 1 ])
715+
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ], [ 1, 2, 1 ])
691716
julia> constraint!(ocp, :mixed, (x, u) -> x[1]-u, 0, 1)
692717
693718
# time dependent and variable independent ocp
694719
julia> constraint!(ocp, :control, (t, u) -> 2u, 0, 1)
695-
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ], [ 1, 2, 1 ])
720+
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ], [ 1, 2, 1 ])
696721
julia> constraint!(ocp, :mixed, (t, x, u) -> x[1]-u, 0, 1)
697722
698723
# time independent and variable dependent ocp
699724
julia> constraint!(ocp, :control, (u, v) -> 2u*v[1], 0, 1)
700-
julia> constraint!(ocp, :state, (x, v) -> x-v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
725+
julia> constraint!(ocp, :state, (x, v) -> x-v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
701726
julia> constraint!(ocp, :mixed, (x, u, v) -> x[1]-v[2]*u, 0, 1)
702727
703728
# time dependent and variable dependent ocp
704729
julia> constraint!(ocp, :control, (t, u, v) -> 2u+v[2], 0, 1)
705-
julia> constraint!(ocp, :state, (t, x, v) -> x-t*v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
730+
julia> constraint!(ocp, :state, (t, x, v) -> x-t*v[1], [ 0, 0, 0 ], [ 1, 2, 1 ])
706731
julia> constraint!(ocp, :mixed, (t, x, u, v) -> x[1]*v[2]-u, 0, 1)
707732
```
708733
"""
@@ -761,22 +786,22 @@ julia> constraint!(ocp, :boundary, (x0, xf, v) -> x0[3]+xf[2]*v[1], 0)
761786
762787
# time independent and variable independent ocp
763788
julia> constraint!(ocp, :control, u -> 2u, 1)
764-
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ])
789+
julia> constraint!(ocp, :state, x -> x-1, [ 0, 0, 0 ])
765790
julia> constraint!(ocp, :mixed, (x, u) -> x[1]-u, 0)
766791
767792
# time dependent and variable independent ocp
768793
julia> constraint!(ocp, :control, (t, u) -> 2u, 1)
769-
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ])
794+
julia> constraint!(ocp, :state, (t, x) -> x-t, [ 0, 0, 0 ])
770795
julia> constraint!(ocp, :mixed, (t, x, u) -> x[1]-u, 0)
771796
772797
# time independent and variable dependent ocp
773798
julia> constraint!(ocp, :control, (u, v) -> 2u*v[1], 1)
774-
julia> constraint!(ocp, :state, (x, v) -> x-v[2], [ 0, 0, 0 ])
799+
julia> constraint!(ocp, :state, (x, v) -> x-v[2], [ 0, 0, 0 ])
775800
julia> constraint!(ocp, :mixed, (x, u) -> x[1]-u+v[1], 0)
776801
777802
# time dependent and variable dependent ocp
778803
julia> constraint!(ocp, :control, (t, u, v) -> 2u-t*v[2], 1)
779-
julia> constraint!(ocp, :state, (t, x, v) -> x-t+v[1], [ 0, 0, 0 ])
804+
julia> constraint!(ocp, :state, (t, x, v) -> x-t+v[1], [ 0, 0, 0 ])
780805
julia> constraint!(ocp, :mixed, (t, x, u, v) -> x[1]-u*v[1], 0)
781806
```
782807
"""
@@ -800,12 +825,12 @@ Add an `:initial`, `:final`, `:control`, `:state` or `:variable` box constraint
800825
# Examples
801826
802827
```jldoctest
803-
julia> constraint!(ocp, :initial, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
828+
julia> constraint!(ocp, :initial, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
804829
julia> constraint!(ocp, :final, val=Index(1), lb=0, ub=2)
805830
julia> constraint!(ocp, :control, val=Index(1), lb=0, ub=2)
806-
julia> constraint!(ocp, :state, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
807-
julia> constraint!(ocp, :initial, rg=1:2:5, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
808-
julia> constraint!(ocp, :variable, rg=1:2, lb=[ 0, 0 ], ub=[ 1, 2 ])
831+
julia> constraint!(ocp, :state, rg=2:3, lb=[ 0, 0 ], ub=[ 1, 2 ])
832+
julia> constraint!(ocp, :initial, rg=1:2:5, lb=[ 0, 0, 0 ], ub=[ 1, 2, 1 ])
833+
julia> constraint!(ocp, :variable, rg=1:2, lb=[ 0, 0 ], ub=[ 1, 2 ])
809834
```
810835
"""
811836
function constraint!(ocp::OptimalControlModel{<: TimeDependence, <: VariableDependence}, type::Symbol;

0 commit comments

Comments
 (0)