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

232 preparing for inplace nlp #271

Merged
merged 37 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7c76c71
first commit
jbcaillau Aug 9, 2024
968ae6e
__wrap(code, ...) everywhere
jbcaillau Aug 29, 2024
98aff2a
solved conflicts in onepass.jl
jbcaillau Aug 29, 2024
fc292cb
Merge branch 'main' into 232-preparing-for-inplace-nlp
jbcaillau Aug 30, 2024
2341da6
added in_place info
jbcaillau Aug 30, 2024
c9e51bc
solved minor conflicts in CTBase.jl (export list)
jbcaillau Sep 2, 2024
75f8af2
added in place ξ!(val, t, u, v) # nonlinear control constraints; adde…
jbcaillau Sep 2, 2024
cc62bd2
added in place for all fun (= nonlinear) constraints
jbcaillau Sep 2, 2024
5a7ed3a
added in place types; to be tested
jbcaillau Sep 5, 2024
09785e0
added missing Types_ = is_in_place(oc
jbcaillau Sep 5, 2024
8dd6f02
fix unit tests
ocots Sep 5, 2024
53e0521
moved Type_ defs. at the beginning of constraint\!
jbcaillau Sep 5, 2024
6b5e28e
scalar range
jbcaillau Sep 6, 2024
670d3fb
treated scalar case with r[:] .= __view(..., rg)
jbcaillau Sep 6, 2024
0ae0f8d
added to_out_place in utils
jbcaillau Sep 6, 2024
3f974ac
added to_out_place in utils
jbcaillau Sep 6, 2024
cdcead8
added to_out_place in utils
jbcaillau Sep 6, 2024
5b01703
fixed utils test + updated tests in onepass with __
jbcaillau Sep 6, 2024
23fbaa8
fix: exported __constraint et al used for test
jbcaillau Sep 6, 2024
fc22616
fix in exports
jbcaillau Sep 6, 2024
4bf88fc
updated test_model with __getters
jbcaillau Sep 6, 2024
c1bed0a
removed useless change to __getter in test_model, as everything is fu…
jbcaillau Sep 6, 2024
57c8fa5
removed useless @_def; added nothing case in to_out_of_place
jbcaillau Sep 6, 2024
e2b1733
updated onepass to generate in place functions; called by @def that g…
jbcaillau Sep 6, 2024
807e6f1
fix: added __view of vector with an Index
jbcaillau Sep 6, 2024
9ff4907
fix: missing r in ControlConstraint! and some wrong sizes in lb / ub …
jbcaillau Sep 6, 2024
d516f63
fix: old dim error in test_onepass
jbcaillau Sep 6, 2024
fe95823
foo
jbcaillau Sep 6, 2024
0eff660
added in place tests in test_model
jbcaillau Sep 7, 2024
317fbc9
fix: test_model.jl
jbcaillau Sep 7, 2024
7151bd2
fix: test_model.jl
jbcaillau Sep 7, 2024
159476b
Update test_model.jl
jbcaillau Sep 7, 2024
6d538c5
test_model: pass
jbcaillau Sep 8, 2024
34f2d99
test_model: pass
jbcaillau Sep 8, 2024
ff75ab0
added @views in generated functions (onepass)
jbcaillau Sep 9, 2024
a9f1c7c
replace __view by @views r[:] .= ...
jbcaillau Sep 9, 2024
8abc547
replaced view(x, ...) by @view(x[...]) for further perf. improvements
jbcaillau Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import ForwardDiff
using Interpolations: linear_interpolation, Line, Interpolations # for default interpolation
using MLStyle # pattern matching
using Parameters # @with_kw: to have default values in struct
using Printf # to print an Opt imalControlModel
using Printf # to print an OptimalControlModel
using DataStructures # OrderedDict for aliases
using Unicode # unicode primitives
using PrettyTables # to print a table
Expand Down Expand Up @@ -258,16 +258,12 @@ export OptimalControlModel
export Model
export __OCPModel # redirection to Model to avoid confusion with other Model functions from other packages. Due to @def macro
export variable!,
time!,
constraint!,
dynamics!,
objective!,
state!,
control!,
remove_constraint!,
model_expression!
export is_autonomous, is_fixed, is_time_independent, is_time_dependent, is_min, is_max
time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, model_expression!
export is_autonomous, is_fixed
export is_time_independent, is_time_dependent
export is_min, is_max
export is_variable_dependent, is_variable_independent
export is_in_place
export nlp_constraints!, constraints, constraints_labels, constraint
export has_free_final_time, has_free_initial_time, has_lagrange_cost, has_mayer_cost
export dim_control_constraints, dim_state_constraints, dim_mixed_constraints, dim_path_constraints
Expand Down Expand Up @@ -320,4 +316,4 @@ export @def
export ct_repl, ct_repl_update_model
isdefined(Base, :active_repl) && ct_repl()

end
end
57 changes: 29 additions & 28 deletions src/onepass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,17 @@ p_variable!(p, ocp, v, q; components_names = nothing, log = false) = begin
p.aliases[Symbol(v, ctupperscripts(i))] = :($v^$i)
end # make: v¹, v²... if the variable is named v
if (isnothing(components_names))
__wrap(:(variable!($ocp, $q, $vv)), p.lnum, p.line)
code = :( variable!($ocp, $q, $vv) )
else
qq == length(components_names.args) ||
return __throw("the number of variable components must be $qq", p.lnum, p.line)
for i ∈ 1:qq
p.aliases[components_names.args[i]] = :($v[$i])
end # aliases from names given by the user
ss = QuoteNode(string.(components_names.args))
__wrap(:(variable!($ocp, $q, $vv, $ss)), p.lnum, p.line)
code = :( variable!($ocp, $q, $vv, $ss) )
end
return __wrap(code, p.lnum, p.line)
end

p_alias!(p, ocp, a, e; log = false) = begin
Expand All @@ -231,7 +232,8 @@ p_alias!(p, ocp, a, e; log = false) = begin
p.aliases[Symbol(a, ctupperscripts(i))] = :($a^$i)
end
p.aliases[a] = e
__wrap(:(LineNumberNode(0, "alias: " * string($aa) * " = " * string($ee))), p.lnum, p.line)
code = :( LineNumberNode(0, "alias: " * string($aa) * " = " * string($ee)) )
return __wrap(code, p.lnum, p.line)
end

p_time!(p, ocp, t, t0, tf; log = false) = begin
Expand Down Expand Up @@ -273,7 +275,7 @@ p_time!(p, ocp, t, t0, tf; log = false) = begin
_ => return __throw("bad time declaration", p.lnum, p.line)
end
end
__wrap(code, p.lnum, p.line)
return __wrap(code, p.lnum, p.line)
end

p_state!(p, ocp, x, n; components_names = nothing, log = false) = begin
Expand All @@ -293,16 +295,17 @@ p_state!(p, ocp, x, n; components_names = nothing, log = false) = begin
end # make: x¹, x²... if the state is named x
p.aliases[Symbol(Unicode.normalize(string(x, "̇")))] = :(∂($x))
if (isnothing(components_names))
__wrap(:(state!($ocp, $n, $xx)), p.lnum, p.line)
code = :( state!($ocp, $n, $xx) )
else
nn == length(components_names.args) ||
return __throw("the number of state components must be $nn", p.lnum, p.line)
for i ∈ 1:nn
p.aliases[components_names.args[i]] = :($x[$i])
end # aliases from names given by the user
ss = QuoteNode(string.(components_names.args))
__wrap(:(state!($ocp, $n, $xx, $ss)), p.lnum, p.line)
code = :( state!($ocp, $n, $xx, $ss) )
end
return __wrap(code, p.lnum, p.line)
end

p_control!(p, ocp, u, m; components_names = nothing, log = false) = begin
Expand All @@ -321,16 +324,17 @@ p_control!(p, ocp, u, m; components_names = nothing, log = false) = begin
p.aliases[Symbol(u, ctupperscripts(i))] = :($u^$i)
end # make: u¹, u²... if the control is named u
if (isnothing(components_names))
__wrap(:(control!($ocp, $m, $uu)), p.lnum, p.line)
code = :( control!($ocp, $m, $uu) )
else
mm == length(components_names.args) ||
return __throw("the number of control components must be $mm", p.lnum, p.line)
for i ∈ 1:mm
p.aliases[components_names.args[i]] = :($u[$i])
end # aliases from names given by the user
ss = QuoteNode(string.(components_names.args))
__wrap(:(control!($ocp, $m, $uu, $ss)), p.lnum, p.line)
code = :( control!($ocp, $m, $uu, $ss) )
end
return __wrap(code, p.lnum, p.line)
end

p_constraint!(p, ocp, e1, e2, e3, label = gensym(); log = false) = begin
Expand Down Expand Up @@ -426,7 +430,7 @@ p_constraint!(p, ocp, e1, e2, e3, label = gensym(); log = false) = begin
end
_ => return __throw("bad constraint declaration", p.lnum, p.line)
end
__wrap(code, p.lnum, p.line)
return __wrap(code, p.lnum, p.line)
end

p_dynamics!(p, ocp, x, t, e, label = nothing; log = false) = begin
Expand All @@ -443,16 +447,14 @@ p_dynamics!(p, ocp, x, t, e, label = nothing; log = false) = begin
e = replace_call(e, [p.x, p.u], p.t, [xt, ut])
p.t_dep = p.t_dep || has(e, t)
gs = gensym()
args = []
__t_dep(p) && push!(args, p.t)
push!(args, xt, ut)
__v_dep(p) && push!(args, p.v)
__wrap(quote
args = [ ]; __t_dep(p) && push!(args, p.t); push!(args, xt, ut); __v_dep(p) && push!(args, p.v)
code = quote
function $gs($(args...))
$e
end
dynamics!($ocp, $gs)
end, p.lnum, p.line)
end
return __wrap(code, p.lnum, p.line)
end

p_lagrange!(p, ocp, e, type; log = false) = begin
Expand All @@ -466,16 +468,14 @@ p_lagrange!(p, ocp, e, type; log = false) = begin
p.t_dep = p.t_dep || has(e, p.t)
ttype = QuoteNode(type)
gs = gensym()
args = []
__t_dep(p) && push!(args, p.t)
push!(args, xt, ut)
__v_dep(p) && push!(args, p.v)
__wrap(quote
args = [ ]; __t_dep(p) && push!(args, p.t); push!(args, xt, ut); __v_dep(p) && push!(args, p.v)
code = quote
function $gs($(args...))
$e
end
objective!($ocp, :lagrange, $gs, $ttype)
end, p.lnum, p.line)
end
return __wrap(code, p.lnum, p.line)
end

p_mayer!(p, ocp, e, type; log = false) = begin
Expand All @@ -494,14 +494,14 @@ p_mayer!(p, ocp, e, type; log = false) = begin
e = replace_call(e, p.x, p.t0, x0)
e = replace_call(e, p.x, p.tf, xf)
ttype = QuoteNode(type)
args = [x0, xf]
__v_dep(p) && push!(args, p.v)
__wrap(quote
args = [ x0, xf ]; __v_dep(p) && push!(args, p.v)
code = quote
function $gs($(args...))
$e
end
objective!($ocp, :mayer, $gs, $ttype)
end, p.lnum, p.line)
end
return __wrap(code, p.lnum, p.line)
end

p_bolza!(p, ocp, e1, e2, type; log = false) = begin
Expand All @@ -528,15 +528,16 @@ p_bolza!(p, ocp, e1, e2, type; log = false) = begin
push!(args2, xt, ut)
__v_dep(p) && push!(args2, p.v)
ttype = QuoteNode(type)
__wrap(quote
code = quote
function $gs1($(args1...))
$e1
end
function $gs2($(args2...))
$e2
end
objective!($ocp, :bolza, $gs1, $gs2, $ttype)
end, p.lnum, p.line)
end
return __wrap(code, p.lnum, p.line)
end

"""
Expand Down Expand Up @@ -627,4 +628,4 @@ macro def(ocp, e, log = false)
catch ex
:(throw($ex)) # can be caught by user
end
end
end
Loading
Loading