diff --git a/bench/Bench.jl b/bench/Bench.jl index a79fb74f..afa69478 100644 --- a/bench/Bench.jl +++ b/bench/Bench.jl @@ -2,7 +2,6 @@ using MLStyle using MacroTools function doBenchMarking(expr, f) - expr = MacroTools.striplines(expr) println("Benchmarking $expr") write(f, string(expr) * "\n") @@ -12,11 +11,9 @@ function doBenchMarking(expr, f) $expr end)) write(f, "\n```\n\n") - end function bench(file::String) - file_name = split(file, ".")[1] println("Benching $file_name.jl\n") @@ -39,7 +36,6 @@ function bench(file::String) #dump(expr) open(file_name_output, write = true, append = true) do f - if has_displayed write(f, "```julia\n") has_displayed = false @@ -48,13 +44,11 @@ function bench(file::String) if hasproperty(expr, :head) && expr.head == :macrocall && expr.args[1] == Symbol("@benchmark") - has_displayed = true doBenchMarking(expr, f) expr = :() else - MLStyle.@match expr begin :(display($benchname)) => begin has_displayed = true @@ -67,15 +61,11 @@ function bench(file::String) write(f, string(expr) * "\n") end end - end - end return expr - end include(mapexpr, file) - end diff --git a/bench/bench_nlp_constraints.jl b/bench/bench_nlp_constraints.jl index 0cb809ec..4eb3da5e 100644 --- a/bench/bench_nlp_constraints.jl +++ b/bench/bench_nlp_constraints.jl @@ -157,11 +157,9 @@ function nlp_constraints_original(ocp::OptimalControlModel) (ul, uind, uu), (xl, xind, xu), (vl, vind, vu) - end function test_alloc_bad(ocp, N) - println(" getters and setters") begin function get_state(XU, i, n, m) @@ -173,15 +171,15 @@ function test_alloc_bad(ocp, N) end function set_control_constraint!(C, i, ξ, nξ, nc) - C[(i-1)*nc+1:(i-1)*nc+nξ] = ξ + C[((i - 1) * nc + 1):((i - 1) * nc + nξ)] = ξ end function set_state_constraint!(C, i, η, nη, nξ, nc) - C[(i-1)*nc+nξ+1:(i-1)*nc+nξ+nη] = η + C[((i - 1) * nc + nξ + 1):((i - 1) * nc + nξ + nη)] = η end function set_mixed_constraint!(C, i, ψ, nψ, nξ, nη, nc) - C[(i-1)*nc+nξ+nη+1:(i-1)*nc+nξ+nη+nψ] = ψ + C[((i - 1) * nc + nξ + nη + 1):((i - 1) * nc + nξ + nη + nψ)] = ψ end end @@ -226,7 +224,6 @@ function test_alloc_bad(ocp, N) println(" end for loop") nothing - end # -------------------------------------------------------------------- @@ -408,41 +405,38 @@ function nlp_constraints_optimized(ocp::OptimalControlModel) (uind, ul, uu), (xind, xl, xu), (vind, vl, vu) - end function test_alloc_good(ocp, N) - begin - println(" getters and setters") begin function get_state(XU, i, n, m) if n == 1 - return XU[(i-1)*(n+m)+1] + return XU[(i - 1) * (n + m) + 1] else - return @view XU[(i-1)*(n+m)+1:(i-1)*(n+m)+n] + return @view XU[((i - 1) * (n + m) + 1):((i - 1) * (n + m) + n)] end end function get_control(XU, i, n, m) if m == 1 - return XU[(i-1)*(n+m)+n+1] + return XU[(i - 1) * (n + m) + n + 1] else - return @view XU[(i-1)*(n+m)+n+1:(i-1)*(n+m)+n+m] + return @view XU[((i - 1) * (n + m) + n + 1):((i - 1) * (n + m) + n + m)] end end function set_control_constraint!(C, i, valξ, nξ, nc) - C[(i-1)*nc+1:(i-1)*nc+nξ] = valξ + C[((i - 1) * nc + 1):((i - 1) * nc + nξ)] = valξ end function set_state_constraint!(C, i, valη, nη, nξ, nc) - C[(i-1)*nc+nξ+1:(i-1)*nc+nξ+nη] = valη + C[((i - 1) * nc + nξ + 1):((i - 1) * nc + nξ + nη)] = valη end function set_mixed_constraint!(C, i, valψ, nψ, nξ, nη, nc) - C[(i-1)*nc+nξ+nη+1:(i-1)*nc+nξ+nη+nψ] = valψ + C[((i - 1) * nc + nξ + nη + 1):((i - 1) * nc + nξ + nη + nψ)] = valψ end end @@ -510,24 +504,23 @@ function test_alloc_good(ocp, N) nothing =# t = times[i] - x[:] = XU[(i-1)*(n+m)+1:(i-1)*(n+m)+n] - u[:] = @view XU[(i-1)*(n+m)+n+1:(i-1)*(n+m)+n+m] + x[:] = XU[((i - 1) * (n + m) + 1):((i - 1) * (n + m) + n)] + u[:] = @view XU[((i - 1) * (n + m) + n + 1):((i - 1) * (n + m) + n + m)] ξ!(valξ, t, u, v) η!(valη, t, x, v) ψ!(valψ, t, x, u, v) #set_control_constraint!(C, i, valξ, nξ, nc) - C[(i-1)*nc+1:(i-1)*nc+nξ] = valξ + C[((i - 1) * nc + 1):((i - 1) * nc + nξ)] = valξ #set_state_constraint!(C, i, valη, nη, nξ, nc) - C[(i-1)*nc+nξ+1:(i-1)*nc+nξ+nη] = valη + C[((i - 1) * nc + nξ + 1):((i - 1) * nc + nξ + nη)] = valη #set_mixed_constraint!(C, i, valψ, nψ, nξ, nη, nc) - C[(i-1)*nc+nξ+nη+1:(i-1)*nc+nξ+nη+nψ] = valψ + C[((i - 1) * nc + nξ + nη + 1):((i - 1) * nc + nξ + nη + nψ)] = valψ #end end println(" end for loop") nothing end - end N = 10000 diff --git a/bench/bench_scalar.jl b/bench/bench_scalar.jl index d25fbe16..b13b205e 100644 --- a/bench/bench_scalar.jl +++ b/bench/bench_scalar.jl @@ -1,7 +1,6 @@ using BenchmarkTools function tt() - function bench_scalar(y) x = 0 for i = 1:y @@ -62,5 +61,4 @@ function tt() end @benchmark bench_scalar_6() - end diff --git a/bench/bench_usage.jl b/bench/bench_usage.jl index 1256fddc..3918ae4b 100644 --- a/bench/bench_usage.jl +++ b/bench/bench_usage.jl @@ -34,11 +34,11 @@ F = Fun_dim_usage_each_call((t, x, u) -> x + u[1]) @benchmark F(t, x, u) # fun with dimension usage handled by parameterization -struct Fun_dim_usage_parametrization{dim_x,dim_u} +struct Fun_dim_usage_parametrization{dim_x, dim_u} f::Function end -function (F::Fun_dim_usage_parametrization{1,1})( +function (F::Fun_dim_usage_parametrization{1, 1})( t::Real, x::Vector{<:Real}, u::Vector{<:Real}, @@ -51,11 +51,11 @@ end # bench fun with dimension usage handled by parameterization # x, u scalar -F = Fun_dim_usage_parametrization{1,1}((t, x, u) -> x + u) +F = Fun_dim_usage_parametrization{1, 1}((t, x, u) -> x + u) @benchmark F(t, x, u) # x scalar, u vector -F = Fun_dim_usage_parametrization{1,1}((t, x, u) -> x + u[1]) +F = Fun_dim_usage_parametrization{1, 1}((t, x, u) -> x + u[1]) @benchmark F(t, x, u) # direct call to the function diff --git a/ext/plot.jl b/ext/plot.jl index 381bf582..4076db2e 100644 --- a/ext/plot.jl +++ b/ext/plot.jl @@ -11,8 +11,8 @@ $(TYPEDEF) A leaf of a plot tree. """ struct PlotLeaf <: AbstractPlotTreeElement - value::Tuple{Symbol,Integer} - PlotLeaf(value::Tuple{Symbol,Integer}) = new(value) + value::Tuple{Symbol, Integer} + PlotLeaf(value::Tuple{Symbol, Integer}) = new(value) end """ @@ -21,12 +21,10 @@ $(TYPEDEF) A node of a plot tree. """ struct PlotNode <: AbstractPlotTreeElement - layout::Union{Symbol,Matrix{Any}} + layout::Union{Symbol, Matrix{Any}} children::Vector{<:AbstractPlotTreeElement} - PlotNode( - layout::Union{Symbol,Matrix{Any}}, - children::Vector{<:AbstractPlotTreeElement}, - ) = new(layout, children) + PlotNode(layout::Union{Symbol, Matrix{Any}}, children::Vector{<:AbstractPlotTreeElement}) = + new(layout, children) end # -------------------------------------------------------------------------------------------------- @@ -40,7 +38,7 @@ Update the plot `p` with the i-th component of a vectorial function of time `f(t - `time` can be `:default` or `:normalized`. """ function __plot_time!( - p::Union{Plots.Plot,Plots.Subplot}, + p::Union{Plots.Plot, Plots.Subplot}, sol::OptimalControlSolution, s::Symbol, i::Integer, @@ -54,9 +52,8 @@ function __plot_time!( t_label = @match time begin :default => t_label :normalized => "normalized " * t_label - _ => error( - "Internal error, no such choice for time: $time. Use :default or :normalized", - ) + _ => + error("Internal error, no such choice for time: $time. Use :default or :normalized") end # reset ylims: ylims=:auto @@ -112,16 +109,7 @@ function __plot_time( label::String, kwargs..., ) - return __plot_time!( - Plots.plot(), - sol, - s, - i, - time; - t_label = t_label, - label = label, - kwargs..., - ) + return __plot_time!(Plots.plot(), sol, s, i, time; t_label = t_label, label = label, kwargs...) end """ @@ -132,7 +120,7 @@ Update the plot `p` with a vectorial function of time `f(t) ∈ Rᵈ` where `f` - `time` can be `:default` or `:normalized`. """ function __plot_time!( - p::Union{Plots.Plot,Plots.Subplot}, + p::Union{Plots.Plot, Plots.Subplot}, sol::OptimalControlSolution, d::Dimension, s::Symbol, @@ -244,7 +232,6 @@ function __initial_plot( m = sol.control_dimension if layout == :group - @match control begin :components => begin px = Plots.plot() # state @@ -266,9 +253,7 @@ function __initial_plot( return Plots.plot(px, pp, pu, pn, layout = (2, 2); kwargs...) end _ => throw( - IncorrectArgument( - "No such choice for control. Use :components, :norm or :all", - ), + IncorrectArgument("No such choice for control. Use :components, :norm or :all"), ) end @@ -302,9 +287,7 @@ function __initial_plot( l = m + 1 end _ => throw( - IncorrectArgument( - "No such choice for control. Use :components, :norm or :all", - ), + IncorrectArgument("No such choice for control. Use :components, :norm or :all"), ) end @@ -327,15 +310,11 @@ function __initial_plot( return __plot_tree(root; kwargs...) else - throw(IncorrectArgument("No such choice for layout. Use :group or :split")) - end - end function __keep_series_attributes(; kwargs...) - series_attributes = Plots.attributes(:Series) out = [] @@ -344,7 +323,6 @@ function __keep_series_attributes(; kwargs...) end return out - end """ @@ -371,7 +349,6 @@ function Plots.plot!( costate_style = (), kwargs..., ) - if solution_label != "" solution_label = " " * solution_label end @@ -388,7 +365,6 @@ function Plots.plot!( series_attr = __keep_series_attributes(; kwargs...) if layout == :group - __plot_time!( p[1], sol, @@ -475,14 +451,11 @@ function Plots.plot!( ) end _ => throw( - IncorrectArgument( - "No such choice for control. Use :components, :norm or :all", - ), + IncorrectArgument("No such choice for control. Use :components, :norm or :all"), ) end elseif layout == :split - for i ∈ 1:n __plot_time!( p[i], @@ -496,7 +469,7 @@ function Plots.plot!( state_style..., ) __plot_time!( - p[i+n], + p[i + n], sol, :costate, i, @@ -511,7 +484,7 @@ function Plots.plot!( :components => begin for i ∈ 1:m __plot_time!( - p[i+2*n], + p[i + 2 * n], sol, :control, i, @@ -525,7 +498,7 @@ function Plots.plot!( end :norm => begin __plot_time!( - p[2*n+1], + p[2 * n + 1], sol, :control_norm, -1, @@ -539,7 +512,7 @@ function Plots.plot!( :all => begin for i ∈ 1:m __plot_time!( - p[i+2*n], + p[i + 2 * n], sol, :control, i, @@ -551,7 +524,7 @@ function Plots.plot!( ) end __plot_time!( - p[2*n+m+1], + p[2 * n + m + 1], sol, :control_norm, -1, @@ -563,20 +536,15 @@ function Plots.plot!( ) end _ => throw( - IncorrectArgument( - "No such choice for control. Use :components, :norm or :all", - ), + IncorrectArgument("No such choice for control. Use :components, :norm or :all"), ) end else - throw(IncorrectArgument("No such choice for layout. Use :group or :split")) - end return p - end function __size_plot(sol::OptimalControlSolution, control::Symbol) @@ -586,9 +554,7 @@ function __size_plot(sol::OptimalControlSolution, control::Symbol) :components => sol.control_dimension :norm => 1 :all => sol.control_dimension + 1 - _ => throw( - IncorrectArgument("No such choice for control. Use :components, :norm or :all"), - ) + _ => throw(IncorrectArgument("No such choice for control. Use :components, :norm or :all")) end return (600, 140 * (n + m)) end @@ -646,8 +612,8 @@ corresponding respectively to the argument `xx` and the argument `yy`. """ @recipe function f( sol::OptimalControlSolution, - xx::Union{Symbol,Tuple{Symbol,Integer}}, - yy::Union{Symbol,Tuple{Symbol,Integer}}, + xx::Union{Symbol, Tuple{Symbol, Integer}}, + yy::Union{Symbol, Tuple{Symbol, Integer}}, time::Symbol = :default, ) @@ -663,15 +629,14 @@ end function recipe_label( sol::OptimalControlSolution, - xx::Union{Symbol,Tuple{Symbol,Integer}}, - yy::Union{Symbol,Tuple{Symbol,Integer}}, + xx::Union{Symbol, Tuple{Symbol, Integer}}, + yy::Union{Symbol, Tuple{Symbol, Integer}}, ) # label = false # if xx isa Symbol && xx == :time - s, i = @match yy begin ::Symbol => (yy, 1) _ => yy @@ -684,7 +649,6 @@ function recipe_label( :control_norm => "‖" * sol.control_name * "‖" _ => error("Internal error, no such choice for label") end - end # return label @@ -697,10 +661,9 @@ Get the data for plotting. """ function __get_data_plot( sol::OptimalControlSolution, - xx::Union{Symbol,Tuple{Symbol,Integer}}; + xx::Union{Symbol, Tuple{Symbol, Integer}}; time::Symbol = :default, ) - T = sol.time_grid X = sol.state.(T) U = sol.control.(T) @@ -729,5 +692,4 @@ function __get_data_plot( :control_norm => [norm(U[i]) for i = 1:m] _ => error("Internal error, no such choice for xx") end - end diff --git a/src/CTBase.jl b/src/CTBase.jl index 8e0a75f3..389acd91 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -55,7 +55,7 @@ julia> const ctVector = Union{ctNumber, AbstractVector{<:ctNumber}} See also: [`ctNumber`](@ref), [`State`](@ref), [`Costate`](@ref), [`Control`](@ref), [`Variable`](@ref). """ -const ctVector = Union{ctNumber,AbstractVector{<:ctNumber}} # [] must be defined as Vector{Real}() +const ctVector = Union{ctNumber, AbstractVector{<:ctNumber}} # [] must be defined as Vector{Real}() """ Type alias for a time. @@ -88,7 +88,7 @@ julia> const TimesDisc = Union{Times, StepRangeLen} See also: [`Time`](@ref), [`Times`](@ref). """ -const TimesDisc = Union{Times,StepRangeLen} +const TimesDisc = Union{Times, StepRangeLen} """ Type alias for a state in Rⁿ. @@ -248,32 +248,22 @@ export set_AD_backend # functions export Hamiltonian, HamiltonianVectorField, VectorField export Mayer, Lagrange, Dynamics, ControlLaw, FeedbackControl, Multiplier -export BoundaryConstraint, - StateConstraint, ControlConstraint, MixedConstraint, VariableConstraint +export BoundaryConstraint, StateConstraint, ControlConstraint, MixedConstraint, VariableConstraint # model 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!, - constraint + time!, constraint!, dynamics!, objective!, state!, control!, remove_constraint!, constraint export is_autonomous, is_fixed, is_time_independent, is_time_dependent, is_min, is_max export is_variable_dependent, is_variable_independent export nlp_constraints!, constraints_labels 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 +export dim_control_constraints, dim_state_constraints, dim_mixed_constraints, dim_path_constraints export dim_boundary_constraints, dim_variable_constraints, dim_control_range export dim_state_range, dim_variable_range -export model_expression, - initial_time, initial_time_name, final_time, final_time_name, time_name +export model_expression, initial_time, initial_time_name, final_time, final_time_name, time_name export control_dimension, control_components_names, control_name export state_dimension, state_components_names, state_name export variable_dimension, variable_components_names, variable_name @@ -298,8 +288,7 @@ export variable_constraints!, mult_variable_constraints!, mult_variable_box_lower!, mult_variable_box_upper! export control_constraints!, mult_control_constraints!, mult_control_box_lower!, mult_control_box_upper! -export state_constraints!, - mult_state_constraints!, mult_state_box_lower!, mult_state_box_upper! +export state_constraints!, mult_state_constraints!, mult_state_box_lower!, mult_state_box_upper! export mixed_constraints!, mult_mixed_constraints! # initialization diff --git a/src/ctparser_utils.jl b/src/ctparser_utils.jl index f1568a62..2616ae40 100644 --- a/src/ctparser_utils.jl +++ b/src/ctparser_utils.jl @@ -50,7 +50,7 @@ julia> x0 = Symbol(x, 0); subs(e, :( \$x[1](\$(t0)) ), :( \$x0[1] )) :(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2 * x(0))) ``` """ -subs(e, e1::Union{Symbol,Real}, e2) = expr_it(e, Expr, x -> x == e1 ? e2 : x) # optimised for some litterals (including symbols) +subs(e, e1::Union{Symbol, Real}, e2) = expr_it(e, Expr, x -> x == e1 ? e2 : x) # optimised for some litterals (including symbols) subs(e, e1, e2) = begin foo(e1, e2) = (h, args...) -> begin @@ -204,22 +204,20 @@ true ``` """ has(e, x, t) = begin - foo(x, t) = - (h, args...) -> begin - ee = Expr(h, args...) - if :yes ∈ args - :yes - else - @match ee begin - :($eee($tt)) => (tt == t && has(eee, x)) ? :yes : ee - _ => ee - end + foo(x, t) = (h, args...) -> begin + ee = Expr(h, args...) + 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) @@ -349,9 +347,9 @@ constraint_type(e, t, t0, tf, x, u, v) = begin has(e, v), ] begin [true, false, false, false, false, false, _] => @match e begin - :($y[$i:$p:$j]($s)) && if (y == x && s == t0) + :($y[($i):($p):($j)]($s)) && if (y == x && s == t0) end => (:initial, i:p:j) - :($y[$i:$j]($s)) && if (y == x && s == t0) + :($y[($i):($j)]($s)) && if (y == x && s == t0) end => (:initial, i:j) :($y[$i]($s)) && if (y == x && s == t0) end => (:initial, i) @@ -360,9 +358,9 @@ constraint_type(e, t, t0, tf, x, u, v) = begin _ => :boundary end [false, true, false, false, false, false, _] => @match e begin - :($y[$i:$p:$j]($s)) && if (y == x && s == tf) + :($y[($i):($p):($j)]($s)) && if (y == x && s == tf) end => (:final, i:p:j) - :($y[$i:$j]($s)) && if (y == x && s == tf) + :($y[($i):($j)]($s)) && if (y == x && s == tf) end => (:final, i:j) :($y[$i]($s)) && if (y == x && s == tf) end => (:final, i) @@ -372,9 +370,9 @@ constraint_type(e, t, t0, tf, x, u, v) = begin end [true, true, false, false, false, false, _] => :boundary [false, false, true, false, false, false, _] => @match e begin - :($c[$i:$p:$j]($s)) && if (c == u && s == t) + :($c[($i):($p):($j)]($s)) && if (c == u && s == t) end => (:control_range, i:p:j) - :($c[$i:$j]($s)) && if (c == u && s == t) + :($c[($i):($j)]($s)) && if (c == u && s == t) end => (:control_range, i:j) :($c[$i]($s)) && if (c == u && s == t) end => (:control_range, i) @@ -383,9 +381,9 @@ constraint_type(e, t, t0, tf, x, u, v) = begin _ => :control_fun end [false, false, false, true, false, false, _] => @match e begin - :($y[$i:$p:$j]($s)) && if (y == x && s == t) + :($y[($i):($p):($j)]($s)) && if (y == x && s == t) end => (:state_range, i:p:j) - :($y[$i:$j]($s)) && if (y == x && s == t) + :($y[($i):($j)]($s)) && if (y == x && s == t) end => (:state_range, i:j) :($y[$i]($s)) && if (y == x && s == t) end => (:state_range, i) @@ -395,9 +393,9 @@ constraint_type(e, t, t0, tf, x, u, v) = begin end [false, false, true, true, false, false, _] => :mixed [false, false, false, false, false, false, true] => @match e begin - :($w[$i:$p:$j]) && if (w == v) + :($w[($i):($p):($j)]) && if (w == v) end => (:variable_range, i:p:j) - :($w[$i:$j]) && if (w == v) + :($w[($i):($j)]) && if (w == v) end => (:variable_range, i:j) :($w[$i]) && if (w == v) end => (:variable_range, i) diff --git a/src/description.jl b/src/description.jl index 0efae9bb..ec13af86 100644 --- a/src/description.jl +++ b/src/description.jl @@ -89,8 +89,7 @@ ERROR: IncorrectArgument: the description (:b,) is already in ((:a,), (:b,)) ``` """ function add(x::Tuple{Vararg{Description}}, y::Description)::Tuple{Vararg{Description}} - y ∈ x ? throw(IncorrectArgument("the description $y is already in $x")) : - return (x..., y) + y ∈ x ? throw(IncorrectArgument("the description $y is already in $x")) : return (x..., y) end """ @@ -111,10 +110,7 @@ julia> getFullDescription((:a,), desc_list) (:a, :b) ``` """ -function getFullDescription( - desc::Description, - desc_list::Tuple{Vararg{Description}}, -)::Description +function getFullDescription(desc::Description, desc_list::Tuple{Vararg{Description}})::Description n = size(desc_list, 1) table = zeros(Int8, n, 2) for i in range(1, n) diff --git a/src/differential_geometry.jl b/src/differential_geometry.jl index b55e8476..353aab95 100644 --- a/src/differential_geometry.jl +++ b/src/differential_geometry.jl @@ -95,7 +95,7 @@ julia> (X⋅f)([1, 2]) 0 ``` """ -function ⋅(X::VectorField{Autonomous,<:VariableDependence}, f::Function)::Function +function ⋅(X::VectorField{Autonomous, <:VariableDependence}, f::Function)::Function return (x, args...) -> ctgradient(y -> f(y, args...), x)' * X(x, args...) end @@ -113,7 +113,7 @@ julia> (X⋅f)(1, [1, 2], [2, 1]) 10 ``` """ -function ⋅(X::VectorField{NonAutonomous,<:VariableDependence}, f::Function)::Function +function ⋅(X::VectorField{NonAutonomous, <:VariableDependence}, f::Function)::Function return (t, x, args...) -> ctgradient(y -> f(t, y, args...), x)' * X(t, x, args...) end @@ -178,12 +178,7 @@ julia> Lie(φ, f, autonomous=false, variable=true)(1, [1, 2], [2, 1]) 10 ``` """ -function Lie( - X::Function, - f::Function; - autonomous::Bool = true, - variable::Bool = false, -)::Function +function Lie(X::Function, f::Function; autonomous::Bool = true, variable::Bool = false)::Function time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed return Lie(VectorField(X, time_dependence, variable_dependence), f) @@ -247,9 +242,9 @@ julia> CTBase.:(⅋)(X, Y)([1, 2]) ``` """ function ⅋( - X::VectorField{Autonomous,V}, - Y::VectorField{Autonomous,V}, -)::VectorField{Autonomous,V} where {V<:VariableDependence} + X::VectorField{Autonomous, V}, + Y::VectorField{Autonomous, V}, +)::VectorField{Autonomous, V} where {V <: VariableDependence} return VectorField( (x, args...) -> x isa ctNumber ? ctgradient(y -> Y(y, args...), x) * X(x, args...) : @@ -274,9 +269,9 @@ julia> CTBase.:(⅋)(X, Y)(1, [1, 2], [2, 3]) ``` """ function ⅋( - X::VectorField{NonAutonomous,V}, - Y::VectorField{NonAutonomous,V}, -)::VectorField{NonAutonomous,V} where {V<:VariableDependence} + X::VectorField{NonAutonomous, V}, + Y::VectorField{NonAutonomous, V}, +)::VectorField{NonAutonomous, V} where {V <: VariableDependence} return VectorField( (t, x, args...) -> x isa ctNumber ? ctgradient(y -> Y(t, y, args...), x) * X(t, x, args...) : @@ -305,14 +300,10 @@ julia> Lie(X, Y)([1, 2]) ``` """ function Lie( - X::VectorField{Autonomous,V}, - Y::VectorField{Autonomous,V}, -)::VectorField{Autonomous,V} where {V<:VariableDependence} - return VectorField( - (x, args...) -> (X ⅋ Y)(x, args...) - (Y ⅋ X)(x, args...), - Autonomous, - V, - ) + X::VectorField{Autonomous, V}, + Y::VectorField{Autonomous, V}, +)::VectorField{Autonomous, V} where {V <: VariableDependence} + return VectorField((x, args...) -> (X ⅋ Y)(x, args...) - (Y ⅋ X)(x, args...), Autonomous, V) end """ @@ -331,9 +322,9 @@ julia> Lie(X, Y)(1, [1, 2], 1) ``` """ function Lie( - X::VectorField{NonAutonomous,V}, - Y::VectorField{NonAutonomous,V}, -)::VectorField{NonAutonomous,V} where {V<:VariableDependence} + X::VectorField{NonAutonomous, V}, + Y::VectorField{NonAutonomous, V}, +)::VectorField{NonAutonomous, V} where {V <: VariableDependence} return VectorField( (t, x, args...) -> (X ⅋ Y)(t, x, args...) - (Y ⅋ X)(t, x, args...), NonAutonomous, @@ -366,19 +357,21 @@ julia> Poisson(F, g)([1, 2], [2, 1]) ``` """ function Poisson( - f::AbstractHamiltonian{Autonomous,V}, - g::AbstractHamiltonian{Autonomous,V}, -)::Hamiltonian{Autonomous,V} where {V<:VariableDependence} + f::AbstractHamiltonian{Autonomous, V}, + g::AbstractHamiltonian{Autonomous, V}, +)::Hamiltonian{Autonomous, V} where {V <: VariableDependence} function fg(x, p, args...) n = size(x, 1) ff, gg = @match n begin 1 => (z -> f(z[1], z[2], args...), z -> g(z[1], z[2], args...)) - _ => - (z -> f(z[1:n], z[n+1:2n], args...), z -> g(z[1:n], z[n+1:2n], args...)) + _ => ( + z -> f(z[1:n], z[(n + 1):(2n)], args...), + z -> g(z[1:n], z[(n + 1):(2n)], args...), + ) end df = ctgradient(ff, [x; p]) dg = ctgradient(gg, [x; p]) - return df[n+1:2n]' * dg[1:n] - df[1:n]' * dg[n+1:2n] + return df[(n + 1):(2n)]' * dg[1:n] - df[1:n]' * dg[(n + 1):(2n)] end return Hamiltonian(fg, Autonomous, V) end @@ -404,21 +397,21 @@ julia> Poisson(f, g, NonAutonomous, NonFixed)(2, [1, 2], [2, 1], [4, 4]) ``` """ function Poisson( - f::AbstractHamiltonian{NonAutonomous,V}, - g::AbstractHamiltonian{NonAutonomous,V}, -)::Hamiltonian{NonAutonomous,V} where {V<:VariableDependence} + f::AbstractHamiltonian{NonAutonomous, V}, + g::AbstractHamiltonian{NonAutonomous, V}, +)::Hamiltonian{NonAutonomous, V} where {V <: VariableDependence} function fg(t, x, p, args...) n = size(x, 1) ff, gg = @match n begin 1 => (z -> f(t, z[1], z[2], args...), z -> g(t, z[1], z[2], args...)) _ => ( - z -> f(t, z[1:n], z[n+1:2n], args...), - z -> g(t, z[1:n], z[n+1:2n], args...), + z -> f(t, z[1:n], z[(n + 1):(2n)], args...), + z -> g(t, z[1:n], z[(n + 1):(2n)], args...), ) end df = ctgradient(ff, [x; p]) dg = ctgradient(gg, [x; p]) - return df[n+1:2n]' * dg[1:n] - df[1:n]' * dg[n+1:2n] + return df[(n + 1):(2n)]' * dg[1:n] - df[1:n]' * dg[(n + 1):(2n)] end return Hamiltonian(fg, NonAutonomous, V) end @@ -445,9 +438,9 @@ julia> Poisson(F, G)(2, [1, 2], [2, 1], [4, 4]) ``` """ function Poisson( - f::HamiltonianLift{T,V}, - g::HamiltonianLift{T,V}, -)::HamiltonianLift{T,V} where {T<:TimeDependence,V<:VariableDependence} + f::HamiltonianLift{T, V}, + g::HamiltonianLift{T, V}, +)::HamiltonianLift{T, V} where {T <: TimeDependence, V <: VariableDependence} return HamiltonianLift(Lie(f.X, g.X)) end @@ -532,8 +525,8 @@ julia> Poisson(f, G)(2, [1, 2], [2, 1], [4, 4]) """ function Poisson( f::Function, - g::AbstractHamiltonian{T,V}, -)::Hamiltonian where {T<:TimeDependence,V<:VariableDependence} + g::AbstractHamiltonian{T, V}, +)::Hamiltonian where {T <: TimeDependence, V <: VariableDependence} return Poisson(Hamiltonian(f, T, V), g) end @@ -557,9 +550,9 @@ julia> Poisson(F, g)(2, [1, 2], [2, 1], [4, 4]) ``` """ function Poisson( - f::AbstractHamiltonian{T,V}, + f::AbstractHamiltonian{T, V}, g::Function, -)::Hamiltonian where {T<:TimeDependence,V<:VariableDependence} +)::Hamiltonian where {T <: TimeDependence, V <: VariableDependence} return Poisson(f, Hamiltonian(g, T, V)) end @@ -612,7 +605,6 @@ julia> @Lie {H0, H1}(1, [1, 2, 3], [1, 0, 7], 2) ``` """ macro Lie(expr::Expr) - fun(x) = @match (@capture(x, [a_, b_]), @capture(x, {c_, d_})) begin (true, false) => :(Lie($a, $b)) (false, true) => :(Poisson($c, $d)) @@ -621,7 +613,6 @@ macro Lie(expr::Expr) end return esc(postwalk(fun, expr)) - end """ @@ -649,7 +640,6 @@ julia> @Lie {H0, H1}(1, [1, 2, 3], [1, 0, 7], 2) autonomous=false variable=true ``` """ macro Lie(expr::Expr, arg1, arg2) - local autonomous = true local variable = false @@ -709,7 +699,6 @@ macro Lie(expr::Expr, arg1, arg2) end return esc(postwalk(fun, expr)) - end """ @@ -730,7 +719,6 @@ julia> @Lie {H0, H1}([1, 2, 3], [1, 0, 7], 2) variable=true ``` """ macro Lie(expr::Expr, arg) - local autonomous = true local variable = false @@ -768,5 +756,4 @@ macro Lie(expr::Expr, arg) end return esc(postwalk(fun, expr)) - end diff --git a/src/exception.jl b/src/exception.jl index b42c5e92..0aecca97 100644 --- a/src/exception.jl +++ b/src/exception.jl @@ -159,9 +159,7 @@ mutable struct ExtensionError <: CTException weakdeps::Tuple{Vararg{Symbol}} function ExtensionError(weakdeps::Symbol...) isempty(weakdeps) && throw( - UnauthorizedCall( - "Please provide at least one weak dependence for the extension.", - ), + UnauthorizedCall("Please provide at least one weak dependence for the extension."), ) e = new() e.weakdeps = weakdeps diff --git a/src/functions.jl b/src/functions.jl index f77b2da9..21fe3c23 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -151,7 +151,7 @@ julia> H = Hamiltonian((t, x, p, v) -> [t+x[1]^2+2p[2]+v[3]], autonomous=false, function Hamiltonian(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return Hamiltonian{time_dependence,variable_dependence}(f) + return Hamiltonian{time_dependence, variable_dependence}(f) end """ @@ -171,7 +171,7 @@ function Hamiltonian(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return Hamiltonian{time_dependence,variable_dependence}(f) + return Hamiltonian{time_dependence, variable_dependence}(f) end """ @@ -214,24 +214,19 @@ julia> H(1, [1, 0], [0, 1], [1, 2, 3]) 7 ``` """ -function (F::Hamiltonian{Autonomous,Fixed})(x::State, p::Costate)::ctNumber +function (F::Hamiltonian{Autonomous, Fixed})(x::State, p::Costate)::ctNumber return F.f(x, p) end -function (F::Hamiltonian{Autonomous,Fixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctNumber +function (F::Hamiltonian{Autonomous, Fixed})(t::Time, x::State, p::Costate, v::Variable)::ctNumber return F.f(x, p) end -function (F::Hamiltonian{Autonomous,NonFixed})(x::State, p::Costate, v::Variable)::ctNumber +function (F::Hamiltonian{Autonomous, NonFixed})(x::State, p::Costate, v::Variable)::ctNumber return F.f(x, p, v) end -function (F::Hamiltonian{Autonomous,NonFixed})( +function (F::Hamiltonian{Autonomous, NonFixed})( t::Time, x::State, p::Costate, @@ -240,11 +235,11 @@ function (F::Hamiltonian{Autonomous,NonFixed})( return F.f(x, p, v) end -function (F::Hamiltonian{NonAutonomous,Fixed})(t::Time, x::State, p::Costate)::ctNumber +function (F::Hamiltonian{NonAutonomous, Fixed})(t::Time, x::State, p::Costate)::ctNumber return F.f(t, x, p) end -function (F::Hamiltonian{NonAutonomous,Fixed})( +function (F::Hamiltonian{NonAutonomous, Fixed})( t::Time, x::State, p::Costate, @@ -253,7 +248,7 @@ function (F::Hamiltonian{NonAutonomous,Fixed})( return F.f(t, x, p) end -function (F::Hamiltonian{NonAutonomous,NonFixed})( +function (F::Hamiltonian{NonAutonomous, NonFixed})( t::Time, x::State, p::Costate, @@ -344,11 +339,11 @@ julia> H(1, [1, 0], [0, 1], [1, 2, 3]) 3 ``` """ -function (H::HamiltonianLift{Autonomous,Fixed})(x::State, p::Costate)::ctNumber +function (H::HamiltonianLift{Autonomous, Fixed})(x::State, p::Costate)::ctNumber return p' * H.X(x) end -function (H::HamiltonianLift{Autonomous,Fixed})( +function (H::HamiltonianLift{Autonomous, Fixed})( t::Time, x::State, p::Costate, @@ -357,15 +352,11 @@ function (H::HamiltonianLift{Autonomous,Fixed})( return p' * H.X(x) end -function (H::HamiltonianLift{Autonomous,NonFixed})( - x::State, - p::Costate, - v::Variable, -)::ctNumber +function (H::HamiltonianLift{Autonomous, NonFixed})(x::State, p::Costate, v::Variable)::ctNumber return p' * H.X(x, v) end -function (H::HamiltonianLift{Autonomous,NonFixed})( +function (H::HamiltonianLift{Autonomous, NonFixed})( t::Time, x::State, p::Costate, @@ -374,11 +365,11 @@ function (H::HamiltonianLift{Autonomous,NonFixed})( return p' * H.X(x, v) end -function (H::HamiltonianLift{NonAutonomous,Fixed})(t::Time, x::State, p::Costate)::ctNumber +function (H::HamiltonianLift{NonAutonomous, Fixed})(t::Time, x::State, p::Costate)::ctNumber return p' * H.X(t, x) end -function (H::HamiltonianLift{NonAutonomous,Fixed})( +function (H::HamiltonianLift{NonAutonomous, Fixed})( t::Time, x::State, p::Costate, @@ -387,7 +378,7 @@ function (H::HamiltonianLift{NonAutonomous,Fixed})( return p' * H.X(t, x) end -function (H::HamiltonianLift{NonAutonomous,NonFixed})( +function (H::HamiltonianLift{NonAutonomous, NonFixed})( t::Time, x::State, p::Costate, @@ -412,14 +403,10 @@ julia> Hv = HamiltonianVectorField((t, x, p, v) -> [t+x[1]^2+2p[2]+v[3], x[2]-3p ``` """ -function HamiltonianVectorField( - f::Function; - autonomous::Bool = true, - variable::Bool = false, -) +function HamiltonianVectorField(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return HamiltonianVectorField{time_dependence,variable_dependence}(f) + return HamiltonianVectorField{time_dependence, variable_dependence}(f) end """ @@ -445,7 +432,7 @@ function HamiltonianVectorField(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return HamiltonianVectorField{time_dependence,variable_dependence}(f) + return HamiltonianVectorField{time_dependence, variable_dependence}(f) end """ @@ -487,62 +474,62 @@ julia> Hv(1, [1, 0], [0, 1], [1, 2, 3, 4]) [7, -3] ``` """ -function (F::HamiltonianVectorField{Autonomous,Fixed})( +function (F::HamiltonianVectorField{Autonomous, Fixed})( x::State, p::Costate, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(x, p) end -function (F::HamiltonianVectorField{Autonomous,Fixed})( +function (F::HamiltonianVectorField{Autonomous, Fixed})( t::Time, x::State, p::Costate, v::Variable, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(x, p) end -function (F::HamiltonianVectorField{Autonomous,NonFixed})( +function (F::HamiltonianVectorField{Autonomous, NonFixed})( x::State, p::Costate, v::Variable, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(x, p, v) end -function (F::HamiltonianVectorField{Autonomous,NonFixed})( +function (F::HamiltonianVectorField{Autonomous, NonFixed})( t::Time, x::State, p::Costate, v::Variable, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(x, p, v) end -function (F::HamiltonianVectorField{NonAutonomous,Fixed})( +function (F::HamiltonianVectorField{NonAutonomous, Fixed})( t::Time, x::State, p::Costate, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(t, x, p) end -function (F::HamiltonianVectorField{NonAutonomous,Fixed})( +function (F::HamiltonianVectorField{NonAutonomous, Fixed})( t::Time, x::State, p::Costate, v::Variable, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(t, x, p) end -function (F::HamiltonianVectorField{NonAutonomous,NonFixed})( +function (F::HamiltonianVectorField{NonAutonomous, NonFixed})( t::Time, x::State, p::Costate, v::Variable, -)::Tuple{DState,DCostate} +)::Tuple{DState, DCostate} return F.f(t, x, p, v) end @@ -565,7 +552,7 @@ julia> V = VectorField((t, x, v) -> [t+x[1]^2, 2x[2]+v[3]], autonomous=false, va function VectorField(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return VectorField{time_dependence,variable_dependence}(f) + return VectorField{time_dependence, variable_dependence}(f) end """ @@ -591,7 +578,7 @@ function VectorField(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return VectorField{time_dependence,variable_dependence}(f) + return VectorField{time_dependence, variable_dependence}(f) end """ @@ -633,31 +620,31 @@ julia> V(1, [1, -1], [1, 2, 3]) [2, 1] ``` """ -function (F::VectorField{Autonomous,Fixed})(x::State)::ctVector +function (F::VectorField{Autonomous, Fixed})(x::State)::ctVector return F.f(x) end -function (F::VectorField{Autonomous,Fixed})(t::Time, x::State, v::Variable)::ctVector +function (F::VectorField{Autonomous, Fixed})(t::Time, x::State, v::Variable)::ctVector return F.f(x) end -function (F::VectorField{Autonomous,NonFixed})(x::State, v::Variable)::ctVector +function (F::VectorField{Autonomous, NonFixed})(x::State, v::Variable)::ctVector return F.f(x, v) end -function (F::VectorField{Autonomous,NonFixed})(t::Time, x::State, v::Variable)::ctVector +function (F::VectorField{Autonomous, NonFixed})(t::Time, x::State, v::Variable)::ctVector return F.f(x, v) end -function (F::VectorField{NonAutonomous,Fixed})(t::Time, x::State)::ctVector +function (F::VectorField{NonAutonomous, Fixed})(t::Time, x::State)::ctVector return F.f(t, x) end -function (F::VectorField{NonAutonomous,Fixed})(t::Time, x::State, v::Variable)::ctVector +function (F::VectorField{NonAutonomous, Fixed})(t::Time, x::State, v::Variable)::ctVector return F.f(t, x) end -function (F::VectorField{NonAutonomous,NonFixed})(t::Time, x::State, v::Variable)::ctVector +function (F::VectorField{NonAutonomous, NonFixed})(t::Time, x::State, v::Variable)::ctVector return F.f(t, x, v) end @@ -682,7 +669,7 @@ julia> L = Lagrange((t, x, u, v) -> t+2x[2]-u[1]^2+v[3], autonomous=false, varia function Lagrange(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return Lagrange{time_dependence,variable_dependence}(f) + return Lagrange{time_dependence, variable_dependence}(f) end """ @@ -710,7 +697,7 @@ function Lagrange(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return Lagrange{time_dependence,variable_dependence}(f) + return Lagrange{time_dependence, variable_dependence}(f) end """ @@ -755,46 +742,31 @@ julia> L(1, [1, 0], [1], [1, 2, 3]) 3 ``` """ -function (F::Lagrange{Autonomous,Fixed})(x::State, u::Control)::ctNumber +function (F::Lagrange{Autonomous, Fixed})(x::State, u::Control)::ctNumber return F.f(x, u) end -function (F::Lagrange{Autonomous,Fixed})( - t::Time, - x::State, - u::Control, - v::Variable, -)::ctNumber +function (F::Lagrange{Autonomous, Fixed})(t::Time, x::State, u::Control, v::Variable)::ctNumber return F.f(x, u) end -function (F::Lagrange{Autonomous,NonFixed})(x::State, u::Control, v::Variable)::ctNumber +function (F::Lagrange{Autonomous, NonFixed})(x::State, u::Control, v::Variable)::ctNumber return F.f(x, u, v) end -function (F::Lagrange{Autonomous,NonFixed})( - t::Time, - x::State, - u::Control, - v::Variable, -)::ctNumber +function (F::Lagrange{Autonomous, NonFixed})(t::Time, x::State, u::Control, v::Variable)::ctNumber return F.f(x, u, v) end -function (F::Lagrange{NonAutonomous,Fixed})(t::Time, x::State, u::Control)::ctNumber +function (F::Lagrange{NonAutonomous, Fixed})(t::Time, x::State, u::Control)::ctNumber return F.f(t, x, u) end -function (F::Lagrange{NonAutonomous,Fixed})( - t::Time, - x::State, - u::Control, - v::Variable, -)::ctNumber +function (F::Lagrange{NonAutonomous, Fixed})(t::Time, x::State, u::Control, v::Variable)::ctNumber return F.f(t, x, u) end -function (F::Lagrange{NonAutonomous,NonFixed})( +function (F::Lagrange{NonAutonomous, NonFixed})( t::Time, x::State, u::Control, @@ -821,7 +793,7 @@ julia> D = Dynamics((t, x, u, v) -> [t+2x[2]-u^2+v[3], x[1]], autonomous=false, function Dynamics(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return Dynamics{time_dependence,variable_dependence}(f) + return Dynamics{time_dependence, variable_dependence}(f) end """ @@ -846,7 +818,7 @@ function Dynamics(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return Dynamics{time_dependence,variable_dependence}(f) + return Dynamics{time_dependence, variable_dependence}(f) end """ @@ -878,46 +850,31 @@ julia> D(1, [1, 0], 1, [1, 2, 3]) [3, 1] ``` """ -function (F::Dynamics{Autonomous,Fixed})(x::State, u::Control)::ctVector +function (F::Dynamics{Autonomous, Fixed})(x::State, u::Control)::ctVector return F.f(x, u) end -function (F::Dynamics{Autonomous,Fixed})( - t::Time, - x::State, - u::Control, - v::Variable, -)::ctVector +function (F::Dynamics{Autonomous, Fixed})(t::Time, x::State, u::Control, v::Variable)::ctVector return F.f(x, u) end -function (F::Dynamics{Autonomous,NonFixed})(x::State, u::Control, v::Variable)::ctVector +function (F::Dynamics{Autonomous, NonFixed})(x::State, u::Control, v::Variable)::ctVector return F.f(x, u, v) end -function (F::Dynamics{Autonomous,NonFixed})( - t::Time, - x::State, - u::Control, - v::Variable, -)::ctVector +function (F::Dynamics{Autonomous, NonFixed})(t::Time, x::State, u::Control, v::Variable)::ctVector return F.f(x, u, v) end -function (F::Dynamics{NonAutonomous,Fixed})(t::Time, x::State, u::Control)::ctVector +function (F::Dynamics{NonAutonomous, Fixed})(t::Time, x::State, u::Control)::ctVector return F.f(t, x, u) end -function (F::Dynamics{NonAutonomous,Fixed})( - t::Time, - x::State, - u::Control, - v::Variable, -)::ctVector +function (F::Dynamics{NonAutonomous, Fixed})(t::Time, x::State, u::Control, v::Variable)::ctVector return F.f(t, x, u) end -function (F::Dynamics{NonAutonomous,NonFixed})( +function (F::Dynamics{NonAutonomous, NonFixed})( t::Time, x::State, u::Control, @@ -944,7 +901,7 @@ julia> S = StateConstraint((t, x, v) -> [t+x[1]^2, 2x[2]+v[3]], autonomous=false function StateConstraint(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return StateConstraint{time_dependence,variable_dependence}(f) + return StateConstraint{time_dependence, variable_dependence}(f) end """ @@ -969,7 +926,7 @@ function StateConstraint(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return StateConstraint{time_dependence,variable_dependence}(f) + return StateConstraint{time_dependence, variable_dependence}(f) end """ @@ -1005,35 +962,31 @@ julia> S(1, [1, -1], [1, 2, 3]) [2, 1] ``` """ -function (F::StateConstraint{Autonomous,Fixed})(x::State)::ctVector +function (F::StateConstraint{Autonomous, Fixed})(x::State)::ctVector return F.f(x) end -function (F::StateConstraint{Autonomous,Fixed})(t::Time, x::State, v::Variable)::ctVector +function (F::StateConstraint{Autonomous, Fixed})(t::Time, x::State, v::Variable)::ctVector return F.f(x) end -function (F::StateConstraint{Autonomous,NonFixed})(x::State, v::Variable)::ctVector +function (F::StateConstraint{Autonomous, NonFixed})(x::State, v::Variable)::ctVector return F.f(x, v) end -function (F::StateConstraint{Autonomous,NonFixed})(t::Time, x::State, v::Variable)::ctVector +function (F::StateConstraint{Autonomous, NonFixed})(t::Time, x::State, v::Variable)::ctVector return F.f(x, v) end -function (F::StateConstraint{NonAutonomous,Fixed})(t::Time, x::State)::ctVector +function (F::StateConstraint{NonAutonomous, Fixed})(t::Time, x::State)::ctVector return F.f(t, x) end -function (F::StateConstraint{NonAutonomous,Fixed})(t::Time, x::State, v::Variable)::ctVector +function (F::StateConstraint{NonAutonomous, Fixed})(t::Time, x::State, v::Variable)::ctVector return F.f(t, x) end -function (F::StateConstraint{NonAutonomous,NonFixed})( - t::Time, - x::State, - v::Variable, -)::ctVector +function (F::StateConstraint{NonAutonomous, NonFixed})(t::Time, x::State, v::Variable)::ctVector return F.f(t, x, v) end @@ -1056,7 +1009,7 @@ julia> C = ControlConstraint((t, u, v) -> [t+u[1]^2, 2u[2]+v[3]], autonomous=fal function ControlConstraint(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return ControlConstraint{time_dependence,variable_dependence}(f) + return ControlConstraint{time_dependence, variable_dependence}(f) end """ @@ -1079,7 +1032,7 @@ function ControlConstraint(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return ControlConstraint{time_dependence,variable_dependence}(f) + return ControlConstraint{time_dependence, variable_dependence}(f) end """ @@ -1113,47 +1066,31 @@ julia> C(1, [1, -1], [1, 2, 3]) [2, 1] ``` """ -function (F::ControlConstraint{Autonomous,Fixed})(u::Control)::ctVector +function (F::ControlConstraint{Autonomous, Fixed})(u::Control)::ctVector return F.f(u) end -function (F::ControlConstraint{Autonomous,Fixed})( - t::Time, - u::Control, - v::Variable, -)::ctVector +function (F::ControlConstraint{Autonomous, Fixed})(t::Time, u::Control, v::Variable)::ctVector return F.f(u) end -function (F::ControlConstraint{Autonomous,NonFixed})(u::Control, v::Variable)::ctVector +function (F::ControlConstraint{Autonomous, NonFixed})(u::Control, v::Variable)::ctVector return F.f(u, v) end -function (F::ControlConstraint{Autonomous,NonFixed})( - t::Time, - u::Control, - v::Variable, -)::ctVector +function (F::ControlConstraint{Autonomous, NonFixed})(t::Time, u::Control, v::Variable)::ctVector return F.f(u, v) end -function (F::ControlConstraint{NonAutonomous,Fixed})(t::Time, u::Control)::ctVector +function (F::ControlConstraint{NonAutonomous, Fixed})(t::Time, u::Control)::ctVector return F.f(t, u) end -function (F::ControlConstraint{NonAutonomous,Fixed})( - t::Time, - u::Control, - v::Variable, -)::ctVector +function (F::ControlConstraint{NonAutonomous, Fixed})(t::Time, u::Control, v::Variable)::ctVector return F.f(t, u) end -function (F::ControlConstraint{NonAutonomous,NonFixed})( - t::Time, - u::Control, - v::Variable, -)::ctVector +function (F::ControlConstraint{NonAutonomous, NonFixed})(t::Time, u::Control, v::Variable)::ctVector return F.f(t, u, v) end @@ -1175,7 +1112,7 @@ julia> M = MixedConstraint((t, x, u, v) -> [t+2x[2]-u^2+v[3], x[1]], autonomous= function MixedConstraint(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return MixedConstraint{time_dependence,variable_dependence}(f) + return MixedConstraint{time_dependence, variable_dependence}(f) end """ @@ -1200,7 +1137,7 @@ function MixedConstraint(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return MixedConstraint{time_dependence,variable_dependence}(f) + return MixedConstraint{time_dependence, variable_dependence}(f) end """ @@ -1238,11 +1175,11 @@ julia> M(1, [1, 0], 1, [1, 2, 3]) [3, 1] ``` """ -function (F::MixedConstraint{Autonomous,Fixed})(x::State, u::Control)::ctVector +function (F::MixedConstraint{Autonomous, Fixed})(x::State, u::Control)::ctVector return F.f(x, u) end -function (F::MixedConstraint{Autonomous,Fixed})( +function (F::MixedConstraint{Autonomous, Fixed})( t::Time, x::State, u::Control, @@ -1251,15 +1188,11 @@ function (F::MixedConstraint{Autonomous,Fixed})( return F.f(x, u) end -function (F::MixedConstraint{Autonomous,NonFixed})( - x::State, - u::Control, - v::Variable, -)::ctVector +function (F::MixedConstraint{Autonomous, NonFixed})(x::State, u::Control, v::Variable)::ctVector return F.f(x, u, v) end -function (F::MixedConstraint{Autonomous,NonFixed})( +function (F::MixedConstraint{Autonomous, NonFixed})( t::Time, x::State, u::Control, @@ -1268,11 +1201,11 @@ function (F::MixedConstraint{Autonomous,NonFixed})( return F.f(x, u, v) end -function (F::MixedConstraint{NonAutonomous,Fixed})(t::Time, x::State, u::Control)::ctVector +function (F::MixedConstraint{NonAutonomous, Fixed})(t::Time, x::State, u::Control)::ctVector return F.f(t, x, u) end -function (F::MixedConstraint{NonAutonomous,Fixed})( +function (F::MixedConstraint{NonAutonomous, Fixed})( t::Time, x::State, u::Control, @@ -1281,7 +1214,7 @@ function (F::MixedConstraint{NonAutonomous,Fixed})( return F.f(t, x, u) end -function (F::MixedConstraint{NonAutonomous,NonFixed})( +function (F::MixedConstraint{NonAutonomous, NonFixed})( t::Time, x::State, u::Control, @@ -1325,7 +1258,7 @@ julia> u = FeedbackControl((t, x, v) -> t+x[1]^2+2x[2]+v[3], autonomous=false, v function FeedbackControl(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return FeedbackControl{time_dependence,variable_dependence}(f) + return FeedbackControl{time_dependence, variable_dependence}(f) end """ @@ -1350,7 +1283,7 @@ function FeedbackControl(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return FeedbackControl{time_dependence,variable_dependence}(f) + return FeedbackControl{time_dependence, variable_dependence}(f) end """ @@ -1390,35 +1323,31 @@ julia> u(1, [1, 0], [1, 2, 3]) 5 ``` """ -function (F::FeedbackControl{Autonomous,Fixed})(x::State)::ctVector +function (F::FeedbackControl{Autonomous, Fixed})(x::State)::ctVector return F.f(x) end -function (F::FeedbackControl{Autonomous,Fixed})(t::Time, x::State, v::Variable)::ctVector +function (F::FeedbackControl{Autonomous, Fixed})(t::Time, x::State, v::Variable)::ctVector return F.f(x) end -function (F::FeedbackControl{Autonomous,NonFixed})(x::State, v::Variable)::ctVector +function (F::FeedbackControl{Autonomous, NonFixed})(x::State, v::Variable)::ctVector return F.f(x, v) end -function (F::FeedbackControl{Autonomous,NonFixed})(t::Time, x::State, v::Variable)::ctVector +function (F::FeedbackControl{Autonomous, NonFixed})(t::Time, x::State, v::Variable)::ctVector return F.f(x, v) end -function (F::FeedbackControl{NonAutonomous,Fixed})(t::Time, x::State)::ctVector +function (F::FeedbackControl{NonAutonomous, Fixed})(t::Time, x::State)::ctVector return F.f(t, x) end -function (F::FeedbackControl{NonAutonomous,Fixed})(t::Time, x::State, v::Variable)::ctVector +function (F::FeedbackControl{NonAutonomous, Fixed})(t::Time, x::State, v::Variable)::ctVector return F.f(t, x) end -function (F::FeedbackControl{NonAutonomous,NonFixed})( - t::Time, - x::State, - v::Variable, -)::ctVector +function (F::FeedbackControl{NonAutonomous, NonFixed})(t::Time, x::State, v::Variable)::ctVector return F.f(t, x, v) end @@ -1440,7 +1369,7 @@ julia> u = ControlLaw((t, x, p, v) -> t+x[1]^2+2p[2]+v[3], autonomous=false, var function ControlLaw(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return ControlLaw{time_dependence,variable_dependence}(f) + return ControlLaw{time_dependence, variable_dependence}(f) end """ @@ -1465,7 +1394,7 @@ function ControlLaw(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return ControlLaw{time_dependence,variable_dependence}(f) + return ControlLaw{time_dependence, variable_dependence}(f) end """ @@ -1505,46 +1434,31 @@ julia> u(1, [1, 0], [0, 1], [1, 2, 3]) 7 ``` """ -function (F::ControlLaw{Autonomous,Fixed})(x::State, p::Costate)::ctVector +function (F::ControlLaw{Autonomous, Fixed})(x::State, p::Costate)::ctVector return F.f(x, p) end -function (F::ControlLaw{Autonomous,Fixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctVector +function (F::ControlLaw{Autonomous, Fixed})(t::Time, x::State, p::Costate, v::Variable)::ctVector return F.f(x, p) end -function (F::ControlLaw{Autonomous,NonFixed})(x::State, p::Costate, v::Variable)::ctVector +function (F::ControlLaw{Autonomous, NonFixed})(x::State, p::Costate, v::Variable)::ctVector return F.f(x, p, v) end -function (F::ControlLaw{Autonomous,NonFixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctVector +function (F::ControlLaw{Autonomous, NonFixed})(t::Time, x::State, p::Costate, v::Variable)::ctVector return F.f(x, p, v) end -function (F::ControlLaw{NonAutonomous,Fixed})(t::Time, x::State, p::Costate)::ctVector +function (F::ControlLaw{NonAutonomous, Fixed})(t::Time, x::State, p::Costate)::ctVector return F.f(t, x, p) end -function (F::ControlLaw{NonAutonomous,Fixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctVector +function (F::ControlLaw{NonAutonomous, Fixed})(t::Time, x::State, p::Costate, v::Variable)::ctVector return F.f(t, x, p) end -function (F::ControlLaw{NonAutonomous,NonFixed})( +function (F::ControlLaw{NonAutonomous, NonFixed})( t::Time, x::State, p::Costate, @@ -1571,7 +1485,7 @@ julia> μ = Multiplier((t, x, p, v) -> t+x[1]^2+2p[2]+v[3], autonomous=false, va function Multiplier(f::Function; autonomous::Bool = true, variable::Bool = false) time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return Multiplier{time_dependence,variable_dependence}(f) + return Multiplier{time_dependence, variable_dependence}(f) end """ @@ -1596,7 +1510,7 @@ function Multiplier(f::Function, dependencies::DataType...) __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return Multiplier{time_dependence,variable_dependence}(f) + return Multiplier{time_dependence, variable_dependence}(f) end """ @@ -1636,46 +1550,31 @@ julia> μ(1, [1, 0], [0, 1], [1, 2, 3]) 7 ``` """ -function (F::Multiplier{Autonomous,Fixed})(x::State, p::Costate)::ctVector +function (F::Multiplier{Autonomous, Fixed})(x::State, p::Costate)::ctVector return F.f(x, p) end -function (F::Multiplier{Autonomous,Fixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctVector +function (F::Multiplier{Autonomous, Fixed})(t::Time, x::State, p::Costate, v::Variable)::ctVector return F.f(x, p) end -function (F::Multiplier{Autonomous,NonFixed})(x::State, p::Costate, v::Variable)::ctVector +function (F::Multiplier{Autonomous, NonFixed})(x::State, p::Costate, v::Variable)::ctVector return F.f(x, p, v) end -function (F::Multiplier{Autonomous,NonFixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctVector +function (F::Multiplier{Autonomous, NonFixed})(t::Time, x::State, p::Costate, v::Variable)::ctVector return F.f(x, p, v) end -function (F::Multiplier{NonAutonomous,Fixed})(t::Time, x::State, p::Costate)::ctVector +function (F::Multiplier{NonAutonomous, Fixed})(t::Time, x::State, p::Costate)::ctVector return F.f(t, x, p) end -function (F::Multiplier{NonAutonomous,Fixed})( - t::Time, - x::State, - p::Costate, - v::Variable, -)::ctVector +function (F::Multiplier{NonAutonomous, Fixed})(t::Time, x::State, p::Costate, v::Variable)::ctVector return F.f(t, x, p) end -function (F::Multiplier{NonAutonomous,NonFixed})( +function (F::Multiplier{NonAutonomous, NonFixed})( t::Time, x::State, p::Costate, diff --git a/src/init.jl b/src/init.jl index 6c263ec5..0827c18f 100644 --- a/src/init.jl +++ b/src/init.jl @@ -144,10 +144,9 @@ julia> init = OptimalControlInit(sol) """ mutable struct OptimalControlInit - state_init::Function control_init::Function - variable_init::Union{Nothing,ctVector} + variable_init::Union{Nothing, ctVector} #costate_init::Function #multipliers_init::Union{Nothing, ctVector} @@ -165,7 +164,6 @@ mutable struct OptimalControlInit control_dim = nothing, variable_dim = nothing, ) - init = new() # some matrix / vector conversions @@ -179,7 +177,6 @@ mutable struct OptimalControlInit init.variable_init = buildVectorInit(variable, variable_dim) return init - end """ @@ -230,7 +227,6 @@ mutable struct OptimalControlInit control_dim = control_dim, variable_dim = variable_dim, ) - end """ @@ -248,5 +244,4 @@ mutable struct OptimalControlInit variable_dim = sol.variable_dimension, ) end - end diff --git a/src/onepass.jl b/src/onepass.jl index f723e751..f840fd1e 100644 --- a/src/onepass.jl +++ b/src/onepass.jl @@ -18,20 +18,20 @@ $(TYPEDEF) """ @with_kw mutable struct ParsingInfo - v::Union{Symbol,Nothing} = nothing - t::Union{Symbol,Nothing} = nothing - t0::Union{Real,Symbol,Expr,Nothing} = nothing - tf::Union{Real,Symbol,Expr,Nothing} = nothing - x::Union{Symbol,Nothing} = nothing - u::Union{Symbol,Nothing} = nothing - aliases::OrderedDict{Symbol,Union{Real,Symbol,Expr}} = __init_aliases() + v::Union{Symbol, Nothing} = nothing + t::Union{Symbol, Nothing} = nothing + t0::Union{Real, Symbol, Expr, Nothing} = nothing + tf::Union{Real, Symbol, Expr, Nothing} = nothing + x::Union{Symbol, Nothing} = nothing + u::Union{Symbol, Nothing} = nothing + aliases::OrderedDict{Symbol, Union{Real, Symbol, Expr}} = __init_aliases() lnum::Integer = 0 line::String = "" t_dep::Bool = false end __init_aliases(; max_dim = 20) = begin - al = OrderedDict{Symbol,Union{Real,Symbol,Expr}}() + al = OrderedDict{Symbol, Union{Real, Symbol, Expr}}() for i ∈ 1:max_dim al[Symbol(:R, ctupperscripts(i))] = :(R^$i) end @@ -85,14 +85,11 @@ parse!(p, ocp, e; log = false) = begin @match e begin # aliases :($a = $e1) => @match e1 begin - :(($names) ∈ R^$q, variable) => - p_variable!(p, ocp, a, q; components_names = names, log) + :(($names) ∈ R^$q, variable) => p_variable!(p, ocp, a, q; components_names = names, log) :([$names] ∈ R^$q, variable) => p_variable!(p, ocp, a, q; components_names = names, log) - :(($names) ∈ R^$n, state) => - p_state!(p, ocp, a, n; components_names = names, log) - :([$names] ∈ R^$n, state) => - p_state!(p, ocp, a, n; components_names = names, log) + :(($names) ∈ R^$n, state) => p_state!(p, ocp, a, n; components_names = names, log) + :([$names] ∈ R^$n, state) => p_state!(p, ocp, a, n; components_names = names, log) :(($names) ∈ R^$m, control) => p_control!(p, ocp, a, m; components_names = names, log) :([$names] ∈ R^$m, control) => @@ -215,11 +212,8 @@ p_variable!(p, ocp, v, q; components_names = nothing, log = false) = begin if (isnothing(components_names)) __wrap(:(variable!($ocp, $q, $vv)), p.lnum, p.line) else - qq == length(components_names.args) || return __throw( - "the number of variable components must be $qq", - p.lnum, - p.line, - ) + 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 @@ -237,11 +231,7 @@ 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, - ) + __wrap(:(LineNumberNode(0, "alias: " * string($aa) * " = " * string($ee))), p.lnum, p.line) end p_time!(p, ocp, t, t0, tf; log = false) = begin @@ -333,11 +323,8 @@ p_control!(p, ocp, u, m; components_names = nothing, log = false) = begin if (isnothing(components_names)) __wrap(:(control!($ocp, $m, $uu)), p.lnum, p.line) else - mm == length(components_names.args) || return __throw( - "the number of control components must be $mm", - p.lnum, - p.line, - ) + 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 @@ -353,22 +340,10 @@ p_constraint!(p, ocp, e1, e2, e3, label = gensym(); log = false) = begin label isa Symbol || return __throw("forbidden label: $label", p.lnum, p.line) llabel = QuoteNode(label) code = @match c_type begin - (:initial, rg) => :(constraint!( - $ocp, - :initial; - rg = $rg, - lb = $e1, - ub = $e3, - label = $llabel, - )) - (:final, rg) => :(constraint!( - $ocp, - :final; - rg = $rg, - lb = $e1, - ub = $e3, - label = $llabel, - )) + (:initial, rg) => + :(constraint!($ocp, :initial; rg = $rg, lb = $e1, ub = $e3, label = $llabel)) + (:final, rg) => + :(constraint!($ocp, :final; rg = $rg, lb = $e1, ub = $e3, label = $llabel)) :boundary => begin gs = gensym() x0 = gensym() @@ -384,14 +359,8 @@ p_constraint!(p, ocp, e1, e2, e3, label = gensym(); log = false) = begin constraint!($ocp, :boundary; f = $gs, lb = $e1, ub = $e3, label = $llabel) end end - (:control_range, rg) => :(constraint!( - $ocp, - :control; - rg = $rg, - lb = $e1, - ub = $e3, - label = $llabel, - )) + (:control_range, rg) => + :(constraint!($ocp, :control; rg = $rg, lb = $e1, ub = $e3, label = $llabel)) :control_fun => begin gs = gensym() ut = gensym() @@ -408,14 +377,8 @@ p_constraint!(p, ocp, e1, e2, e3, label = gensym(); log = false) = begin constraint!($ocp, :control; f = $gs, lb = $e1, ub = $e3, label = $llabel) end end - (:state_range, rg) => :(constraint!( - $ocp, - :state; - rg = $rg, - lb = $e1, - ub = $e3, - label = $llabel, - )) + (:state_range, rg) => + :(constraint!($ocp, :state; rg = $rg, lb = $e1, ub = $e3, label = $llabel)) :state_fun => begin gs = gensym() xt = gensym() @@ -432,14 +395,8 @@ p_constraint!(p, ocp, e1, e2, e3, label = gensym(); log = false) = begin constraint!($ocp, :state; f = $gs, lb = $e1, ub = $e3, label = $llabel) end end - (:variable_range, rg) => :(constraint!( - $ocp, - :variable; - rg = $rg, - lb = $e1, - ub = $e3, - label = $llabel, - )) + (:variable_range, rg) => + :(constraint!($ocp, :variable; rg = $rg, lb = $e1, ub = $e3, label = $llabel)) :variable_fun => begin gs = gensym() args = [p.v] diff --git a/src/optimal_control_model-getters.jl b/src/optimal_control_model-getters.jl index 434271bb..24eda476 100644 --- a/src/optimal_control_model-getters.jl +++ b/src/optimal_control_model-getters.jl @@ -66,9 +66,8 @@ function nlp_constraints!(ocp::OptimalControlModel) for (_, c) ∈ constraints @match c begin - (type, f::BoundaryConstraint, lb, ub) && - if type ∈ [:initial, :final, :boundary] - end => begin + (type, f::BoundaryConstraint, lb, ub) && if type ∈ [:initial, :final, :boundary] + end => begin push!(ϕf, f) append!(ϕl, lb) append!(ϕu, ub) @@ -128,7 +127,7 @@ function nlp_constraints!(ocp::OptimalControlModel) for i ∈ 1:length(ξf) vali = ξf[i](t, u, v) li = length(vali) - val[j:j+li-1] .= vali # .= also allows scalar value for vali + val[j:(j + li - 1)] .= vali # .= also allows scalar value for vali j = j + li end return val @@ -141,7 +140,7 @@ function nlp_constraints!(ocp::OptimalControlModel) for i ∈ 1:length(ηf) vali = ηf[i](t, x, v) li = length(vali) - val[j:j+li-1] .= vali # .= also allows scalar value for vali + val[j:(j + li - 1)] .= vali # .= also allows scalar value for vali j = j + li end return val @@ -154,7 +153,7 @@ function nlp_constraints!(ocp::OptimalControlModel) for i ∈ 1:length(ψf) vali = ψf[i](t, x, u, v) li = length(vali) - val[j:j+li-1] .= vali # .= also allows scalar value for vali + val[j:(j + li - 1)] .= vali # .= also allows scalar value for vali j = j + li end return val @@ -167,7 +166,7 @@ function nlp_constraints!(ocp::OptimalControlModel) for i ∈ 1:length(ϕf) vali = ϕf[i](x0, xf, v) li = length(vali) - val[j:j+li-1] .= vali # .= also allows scalar value for vali + val[j:(j + li - 1)] .= vali # .= also allows scalar value for vali j = j + li end return val @@ -180,7 +179,7 @@ function nlp_constraints!(ocp::OptimalControlModel) for i ∈ 1:length(θf) vali = θf[i](v) li = length(vali) - val[j:j+li-1] .= vali # .= also allows scalar value for vali + val[j:(j + li - 1)] .= vali # .= also allows scalar value for vali j = j + li end return val @@ -203,7 +202,6 @@ function nlp_constraints!(ocp::OptimalControlModel) (ul, uind, uu), (xl, xind, xu), (vl, vind, vu) - end """ @@ -211,8 +209,8 @@ $(TYPEDSIGNATURES) Return `true` if the model is autonomous. """ -is_autonomous(ocp::OptimalControlModel{Autonomous,<:VariableDependence}) = true -is_autonomous(ocp::OptimalControlModel{NonAutonomous,<:VariableDependence}) = false +is_autonomous(ocp::OptimalControlModel{Autonomous, <:VariableDependence}) = true +is_autonomous(ocp::OptimalControlModel{NonAutonomous, <:VariableDependence}) = false """ $(TYPEDSIGNATURES) @@ -247,8 +245,8 @@ $(TYPEDSIGNATURES) Return `true` if the model is fixed (= has no variable). """ -is_fixed(ocp::OptimalControlModel{<:TimeDependence,Fixed}) = true -is_fixed(ocp::OptimalControlModel{<:TimeDependence,NonFixed}) = false +is_fixed(ocp::OptimalControlModel{<:TimeDependence, Fixed}) = true +is_fixed(ocp::OptimalControlModel{<:TimeDependence, NonFixed}) = false """ $(TYPEDSIGNATURES) @@ -318,9 +316,9 @@ julia> c(1) ``` """ function constraint( - ocp::OptimalControlModel{T,V}, + ocp::OptimalControlModel{T, V}, label::Symbol, -) where {T<:TimeDependence,V<:VariableDependence} +) where {T <: TimeDependence, V <: VariableDependence} con = ocp.constraints[label] @match con begin (:initial, f::BoundaryConstraint, _, _) => return f @@ -329,13 +327,12 @@ function constraint( (:control, f::ControlConstraint, _, _) => return f (:control, rg, _, _) => begin C = @match ocp begin - ::OptimalControlModel{Autonomous,Fixed} => - ControlConstraint(u -> u[rg], T, V) - ::OptimalControlModel{Autonomous,NonFixed} => + ::OptimalControlModel{Autonomous, Fixed} => ControlConstraint(u -> u[rg], T, V) + ::OptimalControlModel{Autonomous, NonFixed} => ControlConstraint((u, v) -> u[rg], T, V) - ::OptimalControlModel{NonAutonomous,Fixed} => + ::OptimalControlModel{NonAutonomous, Fixed} => ControlConstraint((t, u) -> u[rg], T, V) - ::OptimalControlModel{NonAutonomous,NonFixed} => + ::OptimalControlModel{NonAutonomous, NonFixed} => ControlConstraint((t, u, v) -> u[rg], T, V) _ => nothing end @@ -344,12 +341,12 @@ function constraint( (:state, f::StateConstraint, _, _) => return f (:state, rg, _, _) => begin S = @match ocp begin - ::OptimalControlModel{Autonomous,Fixed} => StateConstraint(x -> x[rg], T, V) - ::OptimalControlModel{Autonomous,NonFixed} => + ::OptimalControlModel{Autonomous, Fixed} => StateConstraint(x -> x[rg], T, V) + ::OptimalControlModel{Autonomous, NonFixed} => StateConstraint((x, v) -> x[rg], T, V) - ::OptimalControlModel{NonAutonomous,Fixed} => + ::OptimalControlModel{NonAutonomous, Fixed} => StateConstraint((t, x) -> x[rg], T, V) - ::OptimalControlModel{NonAutonomous,NonFixed} => + ::OptimalControlModel{NonAutonomous, NonFixed} => StateConstraint((t, x, v) -> x[rg], T, V) _ => nothing end @@ -396,9 +393,7 @@ function dim_path_constraints(ocp::OptimalControlModel) isnothing(ocp.dim_control_constraints) && return nothing isnothing(ocp.dim_state_constraints) && return nothing isnothing(ocp.dim_mixed_constraints) && return nothing - return ocp.dim_state_constraints + - ocp.dim_control_constraints + - ocp.dim_mixed_constraints + return ocp.dim_state_constraints + ocp.dim_control_constraints + ocp.dim_mixed_constraints end """ diff --git a/src/optimal_control_model-setters.jl b/src/optimal_control_model-setters.jl index c9ee5715..c6ee1671 100644 --- a/src/optimal_control_model-setters.jl +++ b/src/optimal_control_model-setters.jl @@ -29,12 +29,10 @@ julia> ocp = Model(autonomous=false, variable=true) """ function Model(; autonomous::Bool = true, variable::Bool = false) - time_dependence = autonomous ? Autonomous : NonAutonomous variable_dependence = variable ? NonFixed : Fixed - return OptimalControlModel{time_dependence,variable_dependence}() - + return OptimalControlModel{time_dependence, variable_dependence}() end """ @@ -61,12 +59,12 @@ julia> ocp = Model(Autonomous, NonFixed) """ function Model( dependencies::DataType..., -)::OptimalControlModel{<:TimeDependence,<:VariableDependence} +)::OptimalControlModel{<:TimeDependence, <:VariableDependence} # some checkings: __check_dependencies(dependencies) time_dependence = NonAutonomous ∈ dependencies ? NonAutonomous : Autonomous variable_dependence = NonFixed ∈ dependencies ? NonFixed : Fixed - return OptimalControlModel{time_dependence,variable_dependence}() + return OptimalControlModel{time_dependence, variable_dependence}() end """ @@ -92,9 +90,8 @@ function variable!( ) # checkings - is_fixed(ocp) && throw( - UnauthorizedCall("the ocp has no variable, you cannot use variable! function."), - ) + is_fixed(ocp) && + throw(UnauthorizedCall("the ocp has no variable, you cannot use variable! function.")) __is_variable_set(ocp) && throw(UnauthorizedCall("the variable has already been set.")) (q > 1) && (size(components_names, 1) ≠ q) && @@ -186,17 +183,12 @@ function state!( __is_state_set(ocp) && throw(UnauthorizedCall("the state has already been set.")) (n > 1) && (size(components_names, 1) ≠ n) && - throw( - IncorrectArgument( - "the number of state names must be equal to the state dimension", - ), - ) + throw(IncorrectArgument("the number of state names must be equal to the state dimension")) ocp.state_dimension = n ocp.state_components_names = components_names ocp.state_name = name return nothing - end function state!( @@ -205,7 +197,6 @@ function state!( name::Symbol, components_names::Vector{Symbol}, ) - state!(ocp, n, string(name), string.(components_names)) end @@ -215,12 +206,10 @@ function state!( 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 @@ -279,16 +268,13 @@ function control!( (m > 1) && (size(components_names, 1) ≠ m) && throw( - IncorrectArgument( - "the number of control names must be equal to the control dimension", - ), + IncorrectArgument("the number of control names must be equal to the control dimension"), ) ocp.control_dimension = m ocp.control_components_names = components_names ocp.control_name = name return nothing - end function control!( @@ -297,7 +283,6 @@ function control!( name::Symbol, components_names::Vector{Symbol}, ) - control!(ocp, m, string(name), string.(components_names)) end @@ -307,12 +292,10 @@ function control!( 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 @@ -347,12 +330,12 @@ julia> time!(ocp, t0=0, tf=1, name=:s ) # name is a Symbol ``` """ function time!( - ocp::OptimalControlModel{<:TimeDependence,VT}; - t0::Union{Time,Nothing} = nothing, - tf::Union{Time,Nothing} = nothing, - ind0::Union{Integer,Nothing} = nothing, - indf::Union{Integer,Nothing} = nothing, - name::Union{String,Symbol} = __time_name(), + ocp::OptimalControlModel{<:TimeDependence, VT}; + t0::Union{Time, Nothing} = nothing, + tf::Union{Time, Nothing} = nothing, + ind0::Union{Integer, Nothing} = nothing, + indf::Union{Integer, Nothing} = nothing, + name::Union{String, Symbol} = __time_name(), ) where {VT} # check if the problem has been set to Variable or NonVariable @@ -424,25 +407,21 @@ function time!( ocp.initial_time = t0 ocp.final_time = tf ocp.time_name = name - ocp.initial_time_name = - t0 isa Integer ? string(t0) : string(round(t0, digits = 2)) - ocp.final_time_name = - tf isa Integer ? string(tf) : string(round(tf, digits = 2)) + ocp.initial_time_name = t0 isa Integer ? string(t0) : string(round(t0, digits = 2)) + ocp.final_time_name = tf isa Integer ? string(tf) : string(round(tf, digits = 2)) end (::Nothing, ::Integer, ::Time, ::Nothing) => begin # (ind0, tf) ocp.initial_time = Index(ind0) ocp.final_time = tf ocp.time_name = name ocp.initial_time_name = ocp.variable_components_names[ind0] - ocp.final_time_name = - tf isa Integer ? string(tf) : string(round(tf, digits = 2)) + ocp.final_time_name = tf isa Integer ? string(tf) : string(round(tf, digits = 2)) end (::Time, ::Nothing, ::Nothing, ::Integer) => begin # (t0, indf) ocp.initial_time = t0 ocp.final_time = Index(indf) ocp.time_name = name - ocp.initial_time_name = - t0 isa Integer ? string(t0) : string(round(t0, digits = 2)) + ocp.initial_time_name = t0 isa Integer ? string(t0) : string(round(t0, digits = 2)) ocp.final_time_name = ocp.variable_components_names[indf] end (::Nothing, ::Integer, ::Nothing, ::Integer) => begin # (ind0, indf) @@ -456,7 +435,6 @@ function time!( end return nothing - end """ @@ -531,16 +509,15 @@ julia> constraint!(ocp, :mixed; f = (t, x, u, v) -> x[1]*v[2]-u, lb=0, ub=1) """ function constraint!( - ocp::OptimalControlModel{T,V}, + ocp::OptimalControlModel{T, V}, type::Symbol; - rg::Union{OrdinalRange{<:Integer},Index,Integer,Nothing} = nothing, - f::Union{Function,Nothing} = nothing, - lb::Union{ctVector,Nothing} = nothing, - ub::Union{ctVector,Nothing} = nothing, - val::Union{ctVector,Nothing} = nothing, + rg::Union{OrdinalRange{<:Integer}, Index, Integer, Nothing} = nothing, + f::Union{Function, Nothing} = nothing, + lb::Union{ctVector, Nothing} = nothing, + ub::Union{ctVector, Nothing} = nothing, + val::Union{ctVector, Nothing} = nothing, label::Symbol = __constraint_label(), -) where {T<:TimeDependence,V<:VariableDependence} - +) where {T <: TimeDependence, V <: VariableDependence} __check_all_set(ocp) type == :variable && is_fixed(ocp) && @@ -550,9 +527,8 @@ function constraint!( ", you cannot use constraint! function with type=:variable.", ), ) - label ∈ constraints_labels(ocp) && throw( - UnauthorizedCall("the constraint named " * String(label) * " already exists."), - ) + label ∈ constraints_labels(ocp) && + throw(UnauthorizedCall("the constraint named " * String(label) * " already exists.")) isnothing(val) && isnothing(lb) && isnothing(ub) && @@ -714,9 +690,9 @@ julia> dynamics!(ocp, f) ``` """ function dynamics!( - ocp::OptimalControlModel{T,V}, + ocp::OptimalControlModel{T, V}, f::Function, -) where {T<:TimeDependence,V<:VariableDependence} +) where {T <: TimeDependence, V <: VariableDependence} # we check if the dimensions and times have been set __check_all_set(ocp) @@ -725,7 +701,6 @@ function dynamics!( ocp.dynamics = Dynamics(f, T, V) return nothing - end """ @@ -753,16 +728,15 @@ julia> objective!(ocp, :lagrange, (x, u) -> x[1]^2 + u^2) # the control is of di If you set twice the objective, only the last one will be taken into account. """ function objective!( - ocp::OptimalControlModel{T,V}, + ocp::OptimalControlModel{T, V}, type::Symbol, f::Function, criterion::Symbol = __criterion_type(), -) where {T<:TimeDependence,V<:VariableDependence} +) where {T <: TimeDependence, V <: VariableDependence} # we check if the dimensions and times have been set __check_all_set(ocp) - __is_objective_set(ocp) && - throw(UnauthorizedCall("the objective has already been set.")) + __is_objective_set(ocp) && throw(UnauthorizedCall("the objective has already been set.")) # check the validity of the criterion !__is_criterion_valid(criterion) && throw( @@ -790,7 +764,6 @@ function objective!( end return nothing - end """ @@ -813,17 +786,16 @@ julia> objective!(ocp, :bolza, (x0, xf) -> x0[1] + xf[2], (x, u) -> x[1]^2 + u^2 ``` """ function objective!( - ocp::OptimalControlModel{T,V}, + ocp::OptimalControlModel{T, V}, type::Symbol, g::Function, f⁰::Function, criterion::Symbol = __criterion_type(), -) where {T<:TimeDependence,V<:VariableDependence} +) where {T <: TimeDependence, V <: VariableDependence} # we check if the dimensions and times have been set __check_all_set(ocp) - __is_objective_set(ocp) && - throw(UnauthorizedCall("the objective has already been set.")) + __is_objective_set(ocp) && throw(UnauthorizedCall("the objective has already been set.")) # check the validity of the criterion !__is_criterion_valid(criterion) && throw( @@ -850,7 +822,6 @@ function objective!( end return nothing - end """ diff --git a/src/optimal_control_model-type.jl b/src/optimal_control_model-type.jl index 09f90f55..65aff302 100644 --- a/src/optimal_control_model-type.jl +++ b/src/optimal_control_model-type.jl @@ -10,37 +10,37 @@ $(TYPEDEF) $(TYPEDFIELDS) """ @with_kw mutable struct OptimalControlModel{ - time_dependence<:TimeDependence, - variable_dependence<:VariableDependence, + time_dependence <: TimeDependence, + variable_dependence <: VariableDependence, } <: AbstractOptimalControlModel - model_expression::Union{Nothing,Expr} = nothing - initial_time::Union{Time,Index,Nothing} = nothing - initial_time_name::Union{String,Nothing} = nothing - final_time::Union{Time,Index,Nothing} = nothing - final_time_name::Union{String,Nothing} = nothing - time_name::Union{String,Nothing} = nothing - control_dimension::Union{Dimension,Nothing} = nothing - control_components_names::Union{Vector{String},Nothing} = nothing - control_name::Union{String,Nothing} = nothing - state_dimension::Union{Dimension,Nothing} = nothing - state_components_names::Union{Vector{String},Nothing} = nothing - state_name::Union{String,Nothing} = nothing - variable_dimension::Union{Dimension,Nothing} = nothing - variable_components_names::Union{Vector{String},Nothing} = nothing - variable_name::Union{String,Nothing} = nothing - lagrange::Union{Lagrange,Nothing} = nothing - mayer::Union{Mayer,Nothing} = nothing - criterion::Union{Symbol,Nothing} = nothing - dynamics::Union{Dynamics,Nothing} = nothing - constraints::Dict{Symbol,Tuple{Vararg{Any}}} = Dict{Symbol,Tuple{Vararg{Any}}}() - dim_control_constraints::Union{Dimension,Nothing} = nothing - dim_state_constraints::Union{Dimension,Nothing} = nothing - dim_mixed_constraints::Union{Dimension,Nothing} = nothing - dim_boundary_constraints::Union{Dimension,Nothing} = nothing - dim_variable_constraints::Union{Dimension,Nothing} = nothing - dim_control_range::Union{Dimension,Nothing} = nothing - dim_state_range::Union{Dimension,Nothing} = nothing - dim_variable_range::Union{Dimension,Nothing} = nothing + model_expression::Union{Nothing, Expr} = nothing + initial_time::Union{Time, Index, Nothing} = nothing + initial_time_name::Union{String, Nothing} = nothing + final_time::Union{Time, Index, Nothing} = nothing + final_time_name::Union{String, Nothing} = nothing + time_name::Union{String, Nothing} = nothing + control_dimension::Union{Dimension, Nothing} = nothing + control_components_names::Union{Vector{String}, Nothing} = nothing + control_name::Union{String, Nothing} = nothing + state_dimension::Union{Dimension, Nothing} = nothing + state_components_names::Union{Vector{String}, Nothing} = nothing + state_name::Union{String, Nothing} = nothing + variable_dimension::Union{Dimension, Nothing} = nothing + variable_components_names::Union{Vector{String}, Nothing} = nothing + variable_name::Union{String, Nothing} = nothing + lagrange::Union{Lagrange, Nothing} = nothing + mayer::Union{Mayer, Nothing} = nothing + criterion::Union{Symbol, Nothing} = nothing + dynamics::Union{Dynamics, Nothing} = nothing + constraints::Dict{Symbol, Tuple{Vararg{Any}}} = Dict{Symbol, Tuple{Vararg{Any}}}() + dim_control_constraints::Union{Dimension, Nothing} = nothing + dim_state_constraints::Union{Dimension, Nothing} = nothing + dim_mixed_constraints::Union{Dimension, Nothing} = nothing + dim_boundary_constraints::Union{Dimension, Nothing} = nothing + dim_variable_constraints::Union{Dimension, Nothing} = nothing + dim_control_range::Union{Dimension, Nothing} = nothing + dim_state_range::Union{Dimension, Nothing} = nothing + dim_variable_range::Union{Dimension, Nothing} = nothing end # ---------------------------------------------------------------------- @@ -243,16 +243,14 @@ function __check_dependencies(dependencies::Tuple{Vararg{DataType}}) ), ) size(dependencies, 1) > 2 && throw( - IncorrectArgument( - "the number of arguments about dependencies must be equal at most to 2", - ), - ) - size(filter(p -> !(p <: Union{TimeDependence,VariableDependence}), dependencies), 1) > - 0 && throw( - IncorrectArgument( - "wrong type arguments, possible arguments are: NonAutonomous, Autonomous, Fixed, NonFixed", - ), + IncorrectArgument("the number of arguments about dependencies must be equal at most to 2"), ) + size(filter(p -> !(p <: Union{TimeDependence, VariableDependence}), dependencies), 1) > 0 && + throw( + IncorrectArgument( + "wrong type arguments, possible arguments are: NonAutonomous, Autonomous, Fixed, NonFixed", + ), + ) end """ @@ -262,8 +260,7 @@ Throw ```UnauthorizedCall``` exception if the state of an ocp is not set. """ function __check_state_set(ocp::OptimalControlModel) - __is_state_not_set(ocp) && - throw(UnauthorizedCall("the state dimension has to be set before.")) + __is_state_not_set(ocp) && throw(UnauthorizedCall("the state dimension has to be set before.")) end """ @@ -284,8 +281,7 @@ Throw ```UnauthorizedCall``` exception if the time of an ocp is not set. """ function __check_is_time_set(ocp::OptimalControlModel) - __is_time_not_set(ocp) && - throw(UnauthorizedCall("the time dimension has to be set before.")) + __is_time_not_set(ocp) && throw(UnauthorizedCall("the time dimension has to be set before.")) end """ @@ -294,7 +290,7 @@ $(TYPEDSIGNATURES) Throw ```UnauthorizedCall``` exception if the variable of an ocp is not set. """ -function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence,NonFixed}) +function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence, NonFixed}) __is_variable_not_set(ocp) && throw(UnauthorizedCall("the variable dimension has to be set before.")) end @@ -305,7 +301,7 @@ $(TYPEDSIGNATURES) Do nothing, no variable for fixed ocp. """ -function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence,Fixed}) +function __check_variable_set(ocp::OptimalControlModel{<:TimeDependence, Fixed}) return nothing end diff --git a/src/optimal_control_solution-getters.jl b/src/optimal_control_solution-getters.jl index 5fc67666..3478ffe0 100644 --- a/src/optimal_control_solution-getters.jl +++ b/src/optimal_control_solution-getters.jl @@ -142,8 +142,7 @@ $(TYPEDSIGNATURES) Return the names of the components of the variable of the optimal control solution or `nothing`. """ -variable_components_names(sol::OptimalControlSolution)::String = - sol.variable_components_names +variable_components_names(sol::OptimalControlSolution)::String = sol.variable_components_names """ $(TYPEDSIGNATURES) @@ -235,7 +234,7 @@ $(TYPEDSIGNATURES) Return a dictionary of additional infos depending on the solver or `nothing`. """ -infos(sol::OptimalControlSolution)::Dict{Symbol,Any} = sol.infos +infos(sol::OptimalControlSolution)::Dict{Symbol, Any} = sol.infos # constraints and multipliers @@ -253,8 +252,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the boundary constraints of the optimal control solution or `nothing`. """ -mult_boundary_constraints(sol::OptimalControlSolution)::ctVector = - sol.mult_boundary_constraints +mult_boundary_constraints(sol::OptimalControlSolution)::ctVector = sol.mult_boundary_constraints """ $(TYPEDSIGNATURES) @@ -270,8 +268,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the variable constraints of the optimal control solution or `nothing`. """ -mult_variable_constraints(sol::OptimalControlSolution)::ctVector = - sol.mult_variable_constraints +mult_variable_constraints(sol::OptimalControlSolution)::ctVector = sol.mult_variable_constraints """ $(TYPEDSIGNATURES) @@ -303,8 +300,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the control constraints of the optimal control solution or `nothing`. """ -mult_control_constraints(sol::OptimalControlSolution)::Function = - sol.mult_control_constraints +mult_control_constraints(sol::OptimalControlSolution)::Function = sol.mult_control_constraints """ $(TYPEDSIGNATURES) diff --git a/src/optimal_control_solution-setters.jl b/src/optimal_control_solution-setters.jl index 92e837b3..d356d956 100644 --- a/src/optimal_control_solution-setters.jl +++ b/src/optimal_control_solution-setters.jl @@ -10,33 +10,33 @@ Constructor from an optimal control problem. Internal. """ function __OptimalControlSolution( ocp::OptimalControlModel; - state::Union{Nothing,Function} = nothing, - control::Union{Nothing,Function} = nothing, - objective::Union{Nothing,ctNumber} = nothing, - costate::Union{Nothing,Function} = nothing, - time_grid::Union{Nothing,TimesDisc} = nothing, - variable::Union{Nothing,Variable} = nothing, - iterations::Union{Nothing,Integer} = nothing, - stopping::Union{Nothing,Symbol} = nothing, - message::Union{Nothing,String} = nothing, - success::Union{Nothing,Bool} = nothing, - infos::Dict{Symbol,Any} = Dict{Symbol,Any}(), - boundary_constraints::Union{Nothing,ctVector} = nothing, - mult_boundary_constraints::Union{Nothing,ctVector} = nothing, - variable_constraints::Union{Nothing,ctVector} = nothing, - mult_variable_constraints::Union{Nothing,ctVector} = nothing, - mult_variable_box_lower::Union{Nothing,ctVector} = nothing, - mult_variable_box_upper::Union{Nothing,ctVector} = nothing, - control_constraints::Union{Nothing,Function} = nothing, - mult_control_constraints::Union{Nothing,Function} = nothing, - state_constraints::Union{Nothing,Function} = nothing, - mult_state_constraints::Union{Nothing,Function} = nothing, - mixed_constraints::Union{Nothing,Function} = nothing, - mult_mixed_constraints::Union{Nothing,Function} = nothing, - mult_state_box_lower::Union{Nothing,Function} = nothing, - mult_state_box_upper::Union{Nothing,Function} = nothing, - mult_control_box_lower::Union{Nothing,Function} = nothing, - mult_control_box_upper::Union{Nothing,Function} = nothing, + state::Union{Nothing, Function} = nothing, + control::Union{Nothing, Function} = nothing, + objective::Union{Nothing, ctNumber} = nothing, + costate::Union{Nothing, Function} = nothing, + time_grid::Union{Nothing, TimesDisc} = nothing, + variable::Union{Nothing, Variable} = nothing, + iterations::Union{Nothing, Integer} = nothing, + stopping::Union{Nothing, Symbol} = nothing, + message::Union{Nothing, String} = nothing, + success::Union{Nothing, Bool} = nothing, + infos::Dict{Symbol, Any} = Dict{Symbol, Any}(), + boundary_constraints::Union{Nothing, ctVector} = nothing, + mult_boundary_constraints::Union{Nothing, ctVector} = nothing, + variable_constraints::Union{Nothing, ctVector} = nothing, + mult_variable_constraints::Union{Nothing, ctVector} = nothing, + mult_variable_box_lower::Union{Nothing, ctVector} = nothing, + mult_variable_box_upper::Union{Nothing, ctVector} = nothing, + control_constraints::Union{Nothing, Function} = nothing, + mult_control_constraints::Union{Nothing, Function} = nothing, + state_constraints::Union{Nothing, Function} = nothing, + mult_state_constraints::Union{Nothing, Function} = nothing, + mixed_constraints::Union{Nothing, Function} = nothing, + mult_mixed_constraints::Union{Nothing, Function} = nothing, + mult_state_box_lower::Union{Nothing, Function} = nothing, + mult_state_box_upper::Union{Nothing, Function} = nothing, + mult_control_box_lower::Union{Nothing, Function} = nothing, + mult_control_box_upper::Union{Nothing, Function} = nothing, )::OptimalControlSolution # @@ -88,7 +88,6 @@ function __OptimalControlSolution( sol.mult_control_box_upper = mult_control_box_upper return sol - end """ @@ -97,36 +96,35 @@ $(TYPEDSIGNATURES) Constructor from an optimal control problem for a Fixed ocp. """ function OptimalControlSolution( - ocp::OptimalControlModel{<:TimeDependence,Fixed}; + ocp::OptimalControlModel{<:TimeDependence, Fixed}; state::Function, control::Function, objective::ctNumber, #variable::Union{Nothing, Variable}=nothing, - costate::Union{Nothing,Function} = nothing, - time_grid::Union{Nothing,TimesDisc} = nothing, - iterations::Union{Nothing,Integer} = nothing, - stopping::Union{Nothing,Symbol} = nothing, - message::Union{Nothing,String} = nothing, - success::Union{Nothing,Bool} = nothing, - infos::Dict{Symbol,Any} = Dict{Symbol,Any}(), - boundary_constraints::Union{Nothing,ctVector} = nothing, - mult_boundary_constraints::Union{Nothing,ctVector} = nothing, + costate::Union{Nothing, Function} = nothing, + time_grid::Union{Nothing, TimesDisc} = nothing, + iterations::Union{Nothing, Integer} = nothing, + stopping::Union{Nothing, Symbol} = nothing, + message::Union{Nothing, String} = nothing, + success::Union{Nothing, Bool} = nothing, + infos::Dict{Symbol, Any} = Dict{Symbol, Any}(), + boundary_constraints::Union{Nothing, ctVector} = nothing, + mult_boundary_constraints::Union{Nothing, ctVector} = nothing, #variable_constraints::Union{Nothing, ctVector}=nothing, #mult_variable_constraints::Union{Nothing, ctVector}=nothing, #mult_variable_box_lower::Union{Nothing, ctVector}=nothing, #mult_variable_box_upper::Union{Nothing, ctVector}=nothing, - control_constraints::Union{Nothing,Function} = nothing, - mult_control_constraints::Union{Nothing,Function} = nothing, - state_constraints::Union{Nothing,Function} = nothing, - mult_state_constraints::Union{Nothing,Function} = nothing, - mixed_constraints::Union{Nothing,Function} = nothing, - mult_mixed_constraints::Union{Nothing,Function} = nothing, - mult_state_box_lower::Union{Nothing,Function} = nothing, - mult_state_box_upper::Union{Nothing,Function} = nothing, - mult_control_box_lower::Union{Nothing,Function} = nothing, - mult_control_box_upper::Union{Nothing,Function} = nothing, + control_constraints::Union{Nothing, Function} = nothing, + mult_control_constraints::Union{Nothing, Function} = nothing, + state_constraints::Union{Nothing, Function} = nothing, + mult_state_constraints::Union{Nothing, Function} = nothing, + mixed_constraints::Union{Nothing, Function} = nothing, + mult_mixed_constraints::Union{Nothing, Function} = nothing, + mult_state_box_lower::Union{Nothing, Function} = nothing, + mult_state_box_upper::Union{Nothing, Function} = nothing, + mult_control_box_lower::Union{Nothing, Function} = nothing, + mult_control_box_upper::Union{Nothing, Function} = nothing, )::OptimalControlSolution - return __OptimalControlSolution( ocp; state = state, @@ -157,7 +155,6 @@ function OptimalControlSolution( mult_control_box_lower = mult_control_box_lower, mult_control_box_upper = mult_control_box_upper, ) - end """ @@ -166,36 +163,35 @@ $(TYPEDSIGNATURES) Constructor from an optimal control problem for a NonFixed ocp. """ function OptimalControlSolution( - ocp::OptimalControlModel{<:TimeDependence,NonFixed}; + ocp::OptimalControlModel{<:TimeDependence, NonFixed}; state::Function, control::Function, objective::ctNumber, variable::Variable, - costate::Union{Nothing,Function} = nothing, - time_grid::Union{Nothing,TimesDisc} = nothing, - iterations::Union{Nothing,Integer} = nothing, - stopping::Union{Nothing,Symbol} = nothing, - message::Union{Nothing,String} = nothing, - success::Union{Nothing,Bool} = nothing, - infos::Dict{Symbol,Any} = Dict{Symbol,Any}(), - boundary_constraints::Union{Nothing,ctVector} = nothing, - mult_boundary_constraints::Union{Nothing,ctVector} = nothing, - variable_constraints::Union{Nothing,ctVector} = nothing, - mult_variable_constraints::Union{Nothing,ctVector} = nothing, - mult_variable_box_lower::Union{Nothing,ctVector} = nothing, - mult_variable_box_upper::Union{Nothing,ctVector} = nothing, - control_constraints::Union{Nothing,Function} = nothing, - mult_control_constraints::Union{Nothing,Function} = nothing, - state_constraints::Union{Nothing,Function} = nothing, - mult_state_constraints::Union{Nothing,Function} = nothing, - mixed_constraints::Union{Nothing,Function} = nothing, - mult_mixed_constraints::Union{Nothing,Function} = nothing, - mult_state_box_lower::Union{Nothing,Function} = nothing, - mult_state_box_upper::Union{Nothing,Function} = nothing, - mult_control_box_lower::Union{Nothing,Function} = nothing, - mult_control_box_upper::Union{Nothing,Function} = nothing, + costate::Union{Nothing, Function} = nothing, + time_grid::Union{Nothing, TimesDisc} = nothing, + iterations::Union{Nothing, Integer} = nothing, + stopping::Union{Nothing, Symbol} = nothing, + message::Union{Nothing, String} = nothing, + success::Union{Nothing, Bool} = nothing, + infos::Dict{Symbol, Any} = Dict{Symbol, Any}(), + boundary_constraints::Union{Nothing, ctVector} = nothing, + mult_boundary_constraints::Union{Nothing, ctVector} = nothing, + variable_constraints::Union{Nothing, ctVector} = nothing, + mult_variable_constraints::Union{Nothing, ctVector} = nothing, + mult_variable_box_lower::Union{Nothing, ctVector} = nothing, + mult_variable_box_upper::Union{Nothing, ctVector} = nothing, + control_constraints::Union{Nothing, Function} = nothing, + mult_control_constraints::Union{Nothing, Function} = nothing, + state_constraints::Union{Nothing, Function} = nothing, + mult_state_constraints::Union{Nothing, Function} = nothing, + mixed_constraints::Union{Nothing, Function} = nothing, + mult_mixed_constraints::Union{Nothing, Function} = nothing, + mult_state_box_lower::Union{Nothing, Function} = nothing, + mult_state_box_upper::Union{Nothing, Function} = nothing, + mult_control_box_lower::Union{Nothing, Function} = nothing, + mult_control_box_upper::Union{Nothing, Function} = nothing, )::OptimalControlSolution - return __OptimalControlSolution( ocp; state = state, @@ -226,7 +222,6 @@ function OptimalControlSolution( mult_control_box_lower = mult_control_box_lower, mult_control_box_upper = mult_control_box_upper, ) - end # setters @@ -241,8 +236,7 @@ $(TYPEDSIGNATURES) Set the time grid. """ -time_grid!(sol::OptimalControlSolution, time_grid::TimesDisc) = - (sol.time_grid = time_grid; nothing) +time_grid!(sol::OptimalControlSolution, time_grid::TimesDisc) = (sol.time_grid = time_grid; nothing) """ $(TYPEDSIGNATURES) @@ -258,8 +252,7 @@ $(TYPEDSIGNATURES) Set the number of iterations. """ -iterations!(sol::OptimalControlSolution, iterations::Int) = - (sol.iterations = iterations; nothing) +iterations!(sol::OptimalControlSolution, iterations::Int) = (sol.iterations = iterations; nothing) """ $(TYPEDSIGNATURES) @@ -267,8 +260,7 @@ $(TYPEDSIGNATURES) Set the stopping criterion. """ -stopping!(sol::OptimalControlSolution, stopping::Symbol) = - (sol.stopping = stopping; nothing) +stopping!(sol::OptimalControlSolution, stopping::Symbol) = (sol.stopping = stopping; nothing) """ $(TYPEDSIGNATURES) @@ -292,7 +284,7 @@ $(TYPEDSIGNATURES) Set the additional infos. """ -infos!(sol::OptimalControlSolution, infos::Dict{Symbol,Any}) = (sol.infos = infos; nothing) +infos!(sol::OptimalControlSolution, infos::Dict{Symbol, Any}) = (sol.infos = infos; nothing) """ $(TYPEDSIGNATURES) @@ -309,10 +301,8 @@ $(TYPEDSIGNATURES) Set the multipliers to the boundary constraints. """ -mult_boundary_constraints!( - sol::OptimalControlSolution, - mult_boundary_constraints::ctVector, -) = (sol.mult_boundary_constraints = mult_boundary_constraints; nothing) +mult_boundary_constraints!(sol::OptimalControlSolution, mult_boundary_constraints::ctVector) = + (sol.mult_boundary_constraints = mult_boundary_constraints; nothing) """ $(TYPEDSIGNATURES) @@ -329,10 +319,8 @@ $(TYPEDSIGNATURES) Set the multipliers to the variable constraints. """ -mult_variable_constraints!( - sol::OptimalControlSolution, - mult_variable_constraints::ctVector, -) = (sol.mult_variable_constraints = mult_variable_constraints; nothing) +mult_variable_constraints!(sol::OptimalControlSolution, mult_variable_constraints::ctVector) = + (sol.mult_variable_constraints = mult_variable_constraints; nothing) """ $(TYPEDSIGNATURES) diff --git a/src/optimal_control_solution-type.jl b/src/optimal_control_solution-type.jl index bd7f4aec..8710802c 100644 --- a/src/optimal_control_solution-type.jl +++ b/src/optimal_control_solution-type.jl @@ -20,63 +20,62 @@ $(TYPEDFIELDS) @with_kw mutable struct OptimalControlSolution <: AbstractOptimalControlSolution # time_grid = [t0, t1, ...] - time_grid::Union{Nothing,TimesDisc} = nothing + time_grid::Union{Nothing, TimesDisc} = nothing # name of t, t0 and tf - time_name::Union{Nothing,String} = nothing - initial_time_name::Union{Nothing,String} = nothing - final_time_name::Union{Nothing,String} = nothing + time_name::Union{Nothing, String} = nothing + initial_time_name::Union{Nothing, String} = nothing + final_time_name::Union{Nothing, String} = nothing # control: dimension, name of u = (u₁, u₂, ...) and u(t) - control_dimension::Union{Nothing,Dimension} = nothing - control_components_names::Union{Nothing,Vector{String}} = nothing - control_name::Union{Nothing,String} = nothing - control::Union{Nothing,Function} = nothing + control_dimension::Union{Nothing, Dimension} = nothing + control_components_names::Union{Nothing, Vector{String}} = nothing + control_name::Union{Nothing, String} = nothing + control::Union{Nothing, Function} = nothing # state: dimension, name of x = (x₁, x₂, ...) and x(t) - state_dimension::Union{Nothing,Dimension} = nothing - state_components_names::Union{Nothing,Vector{String}} = nothing - state_name::Union{Nothing,String} = nothing - state::Union{Nothing,Function} = nothing + state_dimension::Union{Nothing, Dimension} = nothing + state_components_names::Union{Nothing, Vector{String}} = nothing + state_name::Union{Nothing, String} = nothing + state::Union{Nothing, Function} = nothing # variable: dimension, name of v = (v₁, v₂, ...) and value of v - variable_dimension::Union{Nothing,Dimension} = nothing - variable_components_names::Union{Nothing,Vector{String}} = nothing - variable_name::Union{Nothing,String} = nothing - variable::Union{Nothing,Variable} = nothing + variable_dimension::Union{Nothing, Dimension} = nothing + variable_components_names::Union{Nothing, Vector{String}} = nothing + variable_name::Union{Nothing, String} = nothing + variable::Union{Nothing, Variable} = nothing # costate: p(t) - costate::Union{Nothing,Function} = nothing + costate::Union{Nothing, Function} = nothing # objective value - objective::Union{Nothing,ctNumber} = nothing + objective::Union{Nothing, ctNumber} = nothing # solver infos - iterations::Union{Nothing,Int} = nothing # number of iterations - stopping::Union{Nothing,Symbol} = nothing # the stopping criterion - message::Union{Nothing,String} = nothing # the message corresponding to the stopping criterion - success::Union{Nothing,Bool} = nothing # whether or not the method has finished successfully: CN1, stagnation vs iterations max + iterations::Union{Nothing, Int} = nothing # number of iterations + stopping::Union{Nothing, Symbol} = nothing # the stopping criterion + message::Union{Nothing, String} = nothing # the message corresponding to the stopping criterion + success::Union{Nothing, Bool} = nothing # whether or not the method has finished successfully: CN1, stagnation vs iterations max # dictionary to save additional infos - infos::Dict{Symbol,Any} = Dict{Symbol,Any}() + infos::Dict{Symbol, Any} = Dict{Symbol, Any}() # constraints and multipliers - boundary_constraints::Union{Nothing,ctVector} = nothing - mult_boundary_constraints::Union{Nothing,ctVector} = nothing - variable_constraints::Union{Nothing,ctVector} = nothing - mult_variable_constraints::Union{Nothing,ctVector} = nothing - mult_variable_box_lower::Union{Nothing,ctVector} = nothing - mult_variable_box_upper::Union{Nothing,ctVector} = nothing - - control_constraints::Union{Nothing,Function} = nothing - mult_control_constraints::Union{Nothing,Function} = nothing - state_constraints::Union{Nothing,Function} = nothing - mult_state_constraints::Union{Nothing,Function} = nothing - mixed_constraints::Union{Nothing,Function} = nothing - mult_mixed_constraints::Union{Nothing,Function} = nothing - mult_state_box_lower::Union{Nothing,Function} = nothing - mult_state_box_upper::Union{Nothing,Function} = nothing - mult_control_box_lower::Union{Nothing,Function} = nothing - mult_control_box_upper::Union{Nothing,Function} = nothing - + boundary_constraints::Union{Nothing, ctVector} = nothing + mult_boundary_constraints::Union{Nothing, ctVector} = nothing + variable_constraints::Union{Nothing, ctVector} = nothing + mult_variable_constraints::Union{Nothing, ctVector} = nothing + mult_variable_box_lower::Union{Nothing, ctVector} = nothing + mult_variable_box_upper::Union{Nothing, ctVector} = nothing + + control_constraints::Union{Nothing, Function} = nothing + mult_control_constraints::Union{Nothing, Function} = nothing + state_constraints::Union{Nothing, Function} = nothing + mult_state_constraints::Union{Nothing, Function} = nothing + mixed_constraints::Union{Nothing, Function} = nothing + mult_mixed_constraints::Union{Nothing, Function} = nothing + mult_state_box_lower::Union{Nothing, Function} = nothing + mult_state_box_upper::Union{Nothing, Function} = nothing + mult_control_box_lower::Union{Nothing, Function} = nothing + mult_control_box_upper::Union{Nothing, Function} = nothing end diff --git a/src/print.jl b/src/print.jl index 9ebcb683..0bec8326 100644 --- a/src/print.jl +++ b/src/print.jl @@ -20,7 +20,7 @@ Print the optimal control problem. function Base.show( io::IO, ::MIME"text/plain", - ocp::OptimalControlModel{<:TimeDependence,<:VariableDependence}, + ocp::OptimalControlModel{<:TimeDependence, <:VariableDependence}, ) # check if the problem is empty @@ -60,7 +60,6 @@ function Base.show( end some_printing = true - end if __is_complete(ocp) # print the model if is is complete @@ -86,10 +85,8 @@ function Base.show( _v = is_variable_dependent(ocp) ? ", " * v_name : "" # other names - bounds_args_names = - x_name * "(" * t0_name * "), " * x_name * "(" * tf_name * ")" * _v - mixed_args_names = - t_ * x_name * "(" * t_name * "), " * u_name * "(" * t_name * ")" * _v + bounds_args_names = x_name * "(" * t0_name * "), " * x_name * "(" * tf_name * ")" * _v + mixed_args_names = t_ * x_name * "(" * t_name * "), " * u_name * "(" * t_name * ")" * _v state_args_names = t_ * x_name * "(" * t_name * ")" * _v control_args_names = t_ * u_name * "(" * t_name * ")" * _v @@ -153,12 +150,8 @@ function Base.show( println(io, "") # other constraints: control, state, mixed, boundary, bounds on u, bounds on x - (ξl, ξ, ξu), - (ηl, η, ηu), - (ψl, ψ, ψu), - (ϕl, ϕ, ϕu), - (ulb, uind, uub), - (xlb, xind, xub) = nlp_constraints!(ocp) + (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (ulb, uind, uub), (xlb, xind, xub) = + nlp_constraints!(ocp) has_constraints = false if !isempty(ξl) || !isempty(ulb) has_constraints = true @@ -233,23 +226,13 @@ function Base.show( end v_name_space *= " ∈ " * v_space # print - print( - io, - " where ", - x_name_space, - ", ", - u_name_space, - " and ", - v_name_space, - ".\n", - ) + print(io, " where ", x_name_space, ", ", u_name_space, " and ", v_name_space, ".\n") else # print print(io, " where ", x_name_space, " and ", u_name_space, ".\n") end some_printing = true - end # @@ -293,7 +276,6 @@ function Base.show( compact_printing = true, ) nothing - end function Base.show_default(io::IO, ocp::OptimalControlModel) diff --git a/src/repl.jl b/src/repl.jl index 354db87f..0b615681 100644 --- a/src/repl.jl +++ b/src/repl.jl @@ -24,7 +24,6 @@ Update the model adding the expression e. It must be public since in the ct repl is quoted each time an expression is parsed and is valid. """ function ct_repl_update_model(e::Expr) - ct_repl_data.debug && (println("debug> expression to add: ", e)) # update model @@ -36,7 +35,6 @@ function ct_repl_update_model(e::Expr) # return nothing - end """ @@ -45,7 +43,6 @@ $(TYPEDSIGNATURES) Create a ct REPL. """ function ct_repl(; debug = false, verbose = false) - global ct_repl_is_set global ct_repl_data global ct_repl_history @@ -95,32 +92,26 @@ function ct_repl(; debug = false, verbose = false) command = __transform_to_command(c) ct_repl_data.debug && println("debug> command: ", command, " and argument: ", a) - command ∈ keys(COMMANDS_ACTIONS) && ( - return COMMANDS_ACTIONS[command](ct_repl_data, a, ct_repl_history) - ) + command ∈ keys(COMMANDS_ACTIONS) && + (return COMMANDS_ACTIONS[command](ct_repl_data, a, ct_repl_history)) end :($c) => begin command = __transform_to_command(c) ct_repl_data.debug && println("debug> command: ", command) - command ∈ keys(COMMANDS_ACTIONS) && ( - return COMMANDS_ACTIONS[command](ct_repl_data, ct_repl_history) - ) + command ∈ keys(COMMANDS_ACTIONS) && + (return COMMANDS_ACTIONS[command](ct_repl_data, ct_repl_history)) end _ => nothing end # check if s finishes with a ";". If yes then remove it and return nothing at the end return_nothing = endswith(s, ";") ? true : false - return_nothing && (s = s[1:end-1]) + return_nothing && (s = s[1:(end - 1)]) e = Meta.parse(s) # - return_nothing && - ct_repl_data.debug && - println("\ndebug> new parsing string: ", s) - return_nothing && - ct_repl_data.debug && - println("debug> new expression parsed: ", e) + return_nothing && ct_repl_data.debug && println("\ndebug> new parsing string: ", s) + return_nothing && ct_repl_data.debug && println("debug> new expression parsed: ", e) if e isa Expr @@ -149,8 +140,7 @@ function ct_repl(; debug = false, verbose = false) $ocp_q # define the ocp ct_repl_update_model($ee) # add the expression in the model # ---------------------------------------------------------------- - $return_nothing ? nothing : - begin + $return_nothing ? nothing : begin println("\n", string($ct_repl_data.ocp_name)) $(ct_repl_data.ocp_name) end @@ -158,12 +148,9 @@ function ct_repl(; debug = false, verbose = false) return q else - println(txt_invalid) return nothing - end - end # parse_to_expr # makerepl command @@ -184,7 +171,6 @@ function ct_repl(; debug = false, verbose = false) println("ct repl is already set.") end end - end # ---------------------------------------------------------------- @@ -199,7 +185,7 @@ end function NAME_ACTION_FUNCTION( ct_repl_data::CTRepl, - name::Union{Symbol,Expr}, + name::Union{Symbol, Expr}, ct_repl_history::HistoryRepl, ) ocp_name = ct_repl_data.ocp_name @@ -220,10 +206,8 @@ function NAME_ACTION_FUNCTION( ct_repl_data.debug && println("debug> sol name: ", ct_repl_data.sol_name) __add!(ct_repl_history, ct_repl_data) # update ct_repl_history qo1 = - ct_repl_data.ocp_name ≠ ocp_name ? - :($(ct_repl_data.ocp_name) = "no optimal control") : :() - qs1 = - ct_repl_data.sol_name ≠ sol_name ? :($(ct_repl_data.sol_name) = "no solution") : :() + ct_repl_data.ocp_name ≠ ocp_name ? :($(ct_repl_data.ocp_name) = "no optimal control") : :() + qs1 = ct_repl_data.sol_name ≠ sol_name ? :($(ct_repl_data.sol_name) = "no solution") : :() qo2 = ct_repl_data.ocp_name ≠ ocp_name ? :($(ct_repl_data.ocp_name) = $(ocp_name)) : :() qs2 = ct_repl_data.sol_name ≠ sol_name ? :($(ct_repl_data.sol_name) = $(sol_name)) : :() name_q = ( @@ -244,7 +228,7 @@ function NAME_ACTION_FUNCTION( end # dict of actions associated to ct repl commands -COMMANDS_ACTIONS = Dict{Symbol,Function}( +COMMANDS_ACTIONS = Dict{Symbol, Function}( :SHOW => (ct_repl_data::CTRepl, ct_repl_history::HistoryRepl) -> begin q = quote @@ -326,7 +310,7 @@ COMMANDS_ACTIONS = Dict{Symbol,Function}( ) # dict of help messages associated to ct repl commands -COMMANDS_HELPS = Dict{Symbol,String}( +COMMANDS_HELPS = Dict{Symbol, String}( :SOLVE => "solve the optimal control problem", :PLOT => "plot the solution", #:DEBUG => "toggle debug mode", diff --git a/src/types.jl b/src/types.jl index ac208d32..2e0735f9 100644 --- a/src/types.jl +++ b/src/types.jl @@ -112,14 +112,14 @@ $(TYPEDEF) Abstract type for hamiltonians. """ -abstract type AbstractHamiltonian{time_dependence,variable_dependence} end +abstract type AbstractHamiltonian{time_dependence, variable_dependence} end """ $(TYPEDEF) Abstract type for vectorfields. """ -abstract type AbstractVectorField{time_dependence,variable_dependence} end +abstract type AbstractVectorField{time_dependence, variable_dependence} end """ $(TYPEDEF) @@ -190,8 +190,8 @@ julia> H(1, [1, 0], [0, 1], [1, 2, 3]) 7 ``` """ -struct Hamiltonian{time_dependence,variable_dependence} <: - AbstractHamiltonian{time_dependence,variable_dependence} +struct Hamiltonian{time_dependence, variable_dependence} <: + AbstractHamiltonian{time_dependence, variable_dependence} f::Function end @@ -265,8 +265,8 @@ julia> V(1, [1, -1], [1, 2, 3]) [2, 1] ``` """ -struct VectorField{time_dependence,variable_dependence} <: - AbstractVectorField{time_dependence,variable_dependence} +struct VectorField{time_dependence, variable_dependence} <: + AbstractVectorField{time_dependence, variable_dependence} f::Function end @@ -340,8 +340,8 @@ julia> Hv(1, [1, 0], [0, 1], [1, 2, 3, 4]) [7, -3] ``` """ -struct HamiltonianVectorField{time_dependence,variable_dependence} <: - AbstractVectorField{time_dependence,variable_dependence} +struct HamiltonianVectorField{time_dependence, variable_dependence} <: + AbstractVectorField{time_dependence, variable_dependence} f::Function end @@ -418,13 +418,13 @@ true ``` """ -struct HamiltonianLift{time_dependence,variable_dependence} <: - AbstractHamiltonian{time_dependence,variable_dependence} +struct HamiltonianLift{time_dependence, variable_dependence} <: + AbstractHamiltonian{time_dependence, variable_dependence} X::VectorField function HamiltonianLift( - X::VectorField{time_dependence,variable_dependence}, - ) where {time_dependence,variable_dependence} - new{time_dependence,variable_dependence}(X) + X::VectorField{time_dependence, variable_dependence}, + ) where {time_dependence, variable_dependence} + new{time_dependence, variable_dependence}(X) end end @@ -504,11 +504,10 @@ julia> L(1, [1, 0], [1], [1, 2, 3]) 3 ``` """ -struct Lagrange{time_dependence,variable_dependence} +struct Lagrange{time_dependence, variable_dependence} f::Function end - """ $(TYPEDEF) @@ -577,11 +576,10 @@ julia> D(1, [1, 0], 1, [1, 2, 3]) ``` """ -struct Dynamics{time_dependence,variable_dependence} +struct Dynamics{time_dependence, variable_dependence} f::Function end - """ $(TYPEDEF) @@ -656,7 +654,7 @@ julia> S(1, [1, -1], [1, 2, 3]) ``` """ -struct StateConstraint{time_dependence,variable_dependence} +struct StateConstraint{time_dependence, variable_dependence} f::Function end @@ -728,11 +726,10 @@ julia> C(1, [1, -1], [1, 2, 3]) ``` """ -struct ControlConstraint{time_dependence,variable_dependence} +struct ControlConstraint{time_dependence, variable_dependence} f::Function end - """ $(TYPEDEF) @@ -809,11 +806,10 @@ julia> M(1, [1, 0], 1, [1, 2, 3]) ``` """ -struct MixedConstraint{time_dependence,variable_dependence} +struct MixedConstraint{time_dependence, variable_dependence} f::Function end - """ $(TYPEDEF) @@ -855,7 +851,6 @@ struct VariableConstraint f::Function end - """ $(TYPEDEF) @@ -930,11 +925,10 @@ julia> u(1, [1, 0], [1, 2, 3]) ``` """ -struct FeedbackControl{time_dependence,variable_dependence} +struct FeedbackControl{time_dependence, variable_dependence} f::Function end - """ $(TYPEDEF) @@ -1009,11 +1003,10 @@ julia> u(1, [1, 0], [0, 1], [1, 2, 3]) ``` """ -struct ControlLaw{time_dependence,variable_dependence} +struct ControlLaw{time_dependence, variable_dependence} f::Function end - """ $(TYPEDEF) @@ -1088,7 +1081,7 @@ julia> μ(1, [1, 0], [0, 1], [1, 2, 3]) ``` """ -struct Multiplier{time_dependence,variable_dependence} +struct Multiplier{time_dependence, variable_dependence} f::Function end @@ -1121,7 +1114,7 @@ Base.append!(v::Vector, i::Index) = Base.append!(v, i.val) """ Type alias for an index or range. """ -const RangeConstraint = Union{Index,OrdinalRange{<:Int}} +const RangeConstraint = Union{Index, OrdinalRange{<:Int}} """ $(TYPEDEF) diff --git a/src/utils.jl b/src/utils.jl index c2f2e8af..2af577ad 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -148,11 +148,7 @@ $(TYPEDSIGNATURES) Return the interpolation of `f` at `x`. """ function ctinterpolate(x, f) # default for interpolation of the initialization - return Interpolations.linear_interpolation( - x, - f, - extrapolation_bc = Interpolations.Line(), - ) + return Interpolations.linear_interpolation(x, f, extrapolation_bc = Interpolations.Line()) end """ @@ -175,8 +171,8 @@ Transforms `x` to a Vector{<:Vector{<:ctNumber}}. """ function vec2vec(x::Vector{<:ctNumber}, n::Integer)::Vector{<:Vector{<:ctNumber}} y = [x[1:n]] - for i = n+1:n:length(x)-n+1 - y = vcat(y, [x[i:i+n-1]]) + for i = (n + 1):n:(length(x) - n + 1) + y = vcat(y, [x[i:(i + n - 1)]]) end return y end diff --git a/test/debug_scripts/alias.jl b/test/debug_scripts/alias.jl index 56da1edb..a68d38ff 100755 --- a/test/debug_scripts/alias.jl +++ b/test/debug_scripts/alias.jl @@ -4,7 +4,7 @@ # # find local CTBase -basename = '/' * joinpath(split(Base.source_path(), '/')[1:end-3]) +basename = '/' * joinpath(split(Base.source_path(), '/')[1:(end - 3)]) println(basename) using Pkg; Pkg.activate(basename); @@ -39,5 +39,4 @@ tf = 1.0 0 ≤ i(t) ≤ 1 v(tf) -> min - end diff --git a/test/debug_scripts/all_constraints.jl b/test/debug_scripts/all_constraints.jl index c04aa09c..a9c2c22a 100755 --- a/test/debug_scripts/all_constraints.jl +++ b/test/debug_scripts/all_constraints.jl @@ -5,9 +5,8 @@ # ref: https://github.com/control-toolbox/CTBase.jl/issues/9 # - # find local CTBase -basename = '/' * joinpath(split(Base.source_path(), '/')[1:end-3]) +basename = '/' * joinpath(split(Base.source_path(), '/')[1:(end - 3)]) using Pkg; Pkg.activate(basename); @@ -26,7 +25,6 @@ y0_u = 2.2222 # === initial @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -39,7 +37,6 @@ y0_u = 2.2222 end @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -51,7 +48,6 @@ end y0_b ≤ x[2:3](t0) ≤ y0_u => initial_5 end - # all used variables must be definedbefore each test xf = 11.11 xf2 = 11.111 @@ -63,7 +59,6 @@ yf_u = 2.2222 # === final @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -76,7 +71,6 @@ yf_u = 2.2222 end @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -88,10 +82,8 @@ end yf_b ≤ x[2:3](tf) ≤ yf_u => final_5 end - # === boundary @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -102,10 +94,8 @@ end 1 ≤ x[2](t0)^2 ≤ 2 x[2](tf)^2 == 1 1 ≤ x[2](tf)^2 ≤ 2 - end @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -116,10 +106,8 @@ end 1 ≤ x[2](t0)^2 ≤ 2 => boundary_4 x[2](tf)^2 == 1 => boundary_5 1 ≤ x[2](tf)^2 ≤ 2 => boundary_6 - end - # define more variables u_b = 1.0 u_u = 2.0 @@ -129,7 +117,6 @@ v_b = 5.0 v_u = 6.0 @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -144,7 +131,6 @@ v_u = 6.0 1 ≤ u[1](t)^2 + u[2](t)^2 ≤ 2 end @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -159,7 +145,6 @@ end 1 ≤ u[1](t)^2 + u[2](t)^2 ≤ 2 => control_8 end - # more vars x_b = 10.0 x_u = 11.0 @@ -170,7 +155,6 @@ y_u = 16.0 # === state @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -186,7 +170,6 @@ y_u = 16.0 end @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -201,10 +184,8 @@ end [-1, 1] ≤ x[1:2](t) + x[3:4](t) ≤ [0, 2] => state_8 end - # === mixed @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -213,7 +194,6 @@ end [-1, 1] ≤ u[2](t) * x[1:2](t) ≤ [0, 2] end @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -222,11 +202,9 @@ end [-1, 1] ≤ u[2](t) * x[1:2](t) ≤ [0, 2] => mixed_2 end - # === dynamics @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control @@ -235,9 +213,7 @@ end x'(t) == f(x(t), u(t)) end - @def begin - t ∈ [t0, tf], time x ∈ R^3, state u ∈ R^3, control diff --git a/test/debug_scripts/basic.jl b/test/debug_scripts/basic.jl index c56bf80b..3e5e38b7 100755 --- a/test/debug_scripts/basic.jl +++ b/test/debug_scripts/basic.jl @@ -4,14 +4,13 @@ # # find local CTBase -basename = '/' * joinpath(split(Base.source_path(), '/')[1:end-3]) +basename = '/' * joinpath(split(Base.source_path(), '/')[1:(end - 3)]) println(basename) using Pkg; Pkg.activate(basename); using CTBase - t0 = 0.0 tf = 1.0 @@ -30,7 +29,6 @@ B = [ n = 2 ocp = @def verbose_threshold = 50 debug = true begin - t ∈ [t0, tf], time x ∈ R^n, state u ∈ R, control @@ -41,7 +39,6 @@ ocp = @def verbose_threshold = 50 debug = true begin x'(t) == A * x(t) + B * u(t) ∫(0.5u(t)^2) → min - end # print problem definition diff --git a/test/debug_scripts/goddard.jl b/test/debug_scripts/goddard.jl index 5cdc2c44..39e843f0 100755 --- a/test/debug_scripts/goddard.jl +++ b/test/debug_scripts/goddard.jl @@ -4,7 +4,7 @@ # # find local CTBase -basename = '/' * joinpath(split(Base.source_path(), '/')[1:end-3]) +basename = '/' * joinpath(split(Base.source_path(), '/')[1:(end - 3)]) println(basename) using Pkg; Pkg.activate(basename); @@ -25,7 +25,6 @@ mf = 0.6 # Problem definition ocp = @def verbose_threshold = 100 begin - tf, variable t ∈ [t0, tf], time x ∈ R^3, state @@ -44,7 +43,6 @@ ocp = @def verbose_threshold = 100 begin x'(t) == F0(x(t)) + u(t) * F1(x(t)) r(tf) -> max - end function F0(x) diff --git a/test/debug_scripts/plot.jl b/test/debug_scripts/plot.jl index b53ae28f..9b4a0769 100644 --- a/test/debug_scripts/plot.jl +++ b/test/debug_scripts/plot.jl @@ -32,11 +32,4 @@ plt = plot(sol, size = (700, 450), time = :default) # second plot style = (linestyle = :dash,) -plot!( - plt, - sol2, - time = :default, - state_style = style, - costate_style = style, - control_style = style, -) +plot!(plt, sol2, time = :default, state_style = style, costate_style = style, control_style = style) diff --git a/test/test_aqua.jl b/test/test_aqua.jl index a7794bdc..4edd51be 100644 --- a/test/test_aqua.jl +++ b/test/test_aqua.jl @@ -1,5 +1,4 @@ function test_aqua() - @testset "Aqua.jl" begin Aqua.test_all( CTBase; @@ -20,5 +19,4 @@ function test_aqua() piracies = true, ) end - end diff --git a/test/test_ctparser_utils.jl b/test/test_ctparser_utils.jl index 23658a57..edcec0c5 100644 --- a/test/test_ctparser_utils.jl +++ b/test/test_ctparser_utils.jl @@ -1,6 +1,5 @@ # test utils function test_ctparser_utils() - e = :(∫(r(t)^2 + 2u₁(t)) → min) @test subs(e, :r, :(x[1])) == :(∫((x[1])(t)^2 + 2 * u₁(t)) → min) @@ -17,15 +16,13 @@ function test_ctparser_utils() u = :u e = :(x[1](0) * 2x(tf) - x[2](tf) * 2x(0)) x0 = Symbol(x, 0) - @test subs(e, :($x[1]($(t0))), :($x0[1])) == - :(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2 * x(0))) + @test subs(e, :($x[1]($(t0))), :($x0[1])) == :(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2 * x(0))) e = :(x[1](0) * 2x(tf) - x[2](tf) * 2x(0)) x0 = Symbol(x, "#0") xf = Symbol(x, "#f") e = replace_call(e, x, t0, x0) - @test replace_call(e, x, tf, xf) == - :(var"x#0"[1] * (2var"x#f") - var"x#f"[2] * (2var"x#0")) + @test replace_call(e, x, tf, xf) == :(var"x#0"[1] * (2var"x#f") - var"x#f"[2] * (2var"x#0")) e = :(A * x(t) + B * u(t)) @test replace_call(replace_call(e, x, t, x), u, t, u) == :(A * x + B * u) @@ -108,5 +105,4 @@ function test_ctparser_utils() @test constraint_type(:(v), t, t0, tf, x, u, v) == (:variable_range, nothing) @test constraint_type(:(v^2 + 1), t, t0, tf, x, u, v) == :variable_fun @test constraint_type(:(v[2]^2 + 1), t, t0, tf, x, u, v) == :variable_fun - end diff --git a/test/test_default.jl b/test/test_default.jl index eaa3156b..861a6b31 100644 --- a/test/test_default.jl +++ b/test/test_default.jl @@ -1,5 +1,4 @@ function test_default() - @testset "Audo diff" begin set_AD_backend(AutoForwardDiff()) @test CTBase.__get_AD_backend() == AutoForwardDiff() @@ -33,8 +32,7 @@ function test_default() @testset "Default value of the variable names of the Optimal Control Problem" begin @test CTBase.__variable_name() == "v" - @test CTBase.__variable_components_names(2, CTBase.__variable_name()) == - ["v₁", "v₂"] + @test CTBase.__variable_components_names(2, CTBase.__variable_name()) == ["v₁", "v₂"] end @testset "Default value of the time name of the Optimal Control Problem" begin @@ -68,5 +66,4 @@ function test_default() @testset "Default value of the initial guess" begin @test isnothing(CTBase.__ocp_init()) end - end diff --git a/test/test_description.jl b/test/test_description.jl index 6ed6d424..686f716f 100644 --- a/test/test_description.jl +++ b/test/test_description.jl @@ -19,8 +19,7 @@ function test_description() @test getFullDescription((:descent,), algorithmes) == (:descent, :bfgs, :bissection) @test getFullDescription((:bfgs,), algorithmes) == (:descent, :bfgs, :bissection) @test getFullDescription((:bissection,), algorithmes) == (:descent, :bfgs, :bissection) - @test getFullDescription((:backtracking,), algorithmes) == - (:descent, :bfgs, :backtracking) + @test getFullDescription((:backtracking,), algorithmes) == (:descent, :bfgs, :backtracking) @test getFullDescription((:fixedstep,), algorithmes) == (:descent, :bfgs, :fixedstep) @test getFullDescription((:fixedstep, :gradient), algorithmes) == (:descent, :gradient, :fixedstep) @@ -54,6 +53,4 @@ function test_description() algorithmes = () algorithmes = add(algorithmes, (:a, :b, :c)) @test_throws IncorrectArgument add(algorithmes, (:a, :b, :c)) - - end diff --git a/test/test_differential_geometry.jl b/test/test_differential_geometry.jl index 7c9e7bfe..d1913c9a 100644 --- a/test/test_differential_geometry.jl +++ b/test/test_differential_geometry.jl @@ -3,7 +3,6 @@ function test_differential_geometry() dummy_function() = nothing @testset "Lifts" begin - @testset "HamiltonianLift from VectorField" begin HL = HamiltonianLift( VectorField(x -> [x[1]^2, x[2]^2], autonomous = true, variable = false), @@ -11,20 +10,12 @@ function test_differential_geometry() @test HL([1, 0], [0, 1]) == 0 @test HL(1, [1, 0], [0, 1], 1) == 0 HL = HamiltonianLift( - VectorField( - (x, v) -> [x[1]^2, x[2]^2 + v], - autonomous = true, - variable = true, - ), + VectorField((x, v) -> [x[1]^2, x[2]^2 + v], autonomous = true, variable = true), ) @test HL([1, 0], [0, 1], 1) == 1 @test HL(1, [1, 0], [0, 1], 1) == 1 HL = HamiltonianLift( - VectorField( - (t, x) -> [t + x[1]^2, x[2]^2], - autonomous = false, - variable = false, - ), + VectorField((t, x) -> [t + x[1]^2, x[2]^2], autonomous = false, variable = false), ) @test HL(1, [1, 0], [0, 1]) == 0 @test HL(1, [1, 0], [0, 1], 1) == 0 @@ -42,11 +33,7 @@ function test_differential_geometry() HL = HamiltonianLift(x -> [x[1]^2, x[2]^2], autonomous = true, variable = false) @test HL([1, 0], [0, 1]) == 0 @test HL(1, [1, 0], [0, 1], 1) == 0 - HL = HamiltonianLift( - (x, v) -> [x[1]^2, x[2]^2 + v], - autonomous = true, - variable = true, - ) + HL = HamiltonianLift((x, v) -> [x[1]^2, x[2]^2 + v], autonomous = true, variable = true) @test HL([1, 0], [0, 1], 1) == 1 @test HL(1, [1, 0], [0, 1], 1) == 1 HL = HamiltonianLift( @@ -71,11 +58,7 @@ function test_differential_geometry() HL = HamiltonianLift((t, x) -> [t + x[1]^2, x[2]^2], NonAutonomous, Fixed) @test HL(1, [1, 0], [0, 1]) == 0 @test HL(1, [1, 0], [0, 1], 1) == 0 - HL = HamiltonianLift( - (t, x, v) -> [t + x[1]^2, x[2]^2 + v], - NonAutonomous, - NonFixed, - ) + HL = HamiltonianLift((t, x, v) -> [t + x[1]^2, x[2]^2 + v], NonAutonomous, NonFixed) @test HL(1, [1, 0], [0, 1], 1) == 1 end @@ -108,7 +91,6 @@ function test_differential_geometry() H = Lift(X) Test.@test H(1, 1, 1, 1) == 2 Test.@test H(1, [1, 2], [3, 4], 1) == 22 - end @testset "from Function" begin @@ -145,9 +127,7 @@ function test_differential_geometry() # exceptions Test.@test_throws IncorrectArgument Lift(X, Int64) - end - end # tests for Lift @testset "Directional derivative of a scalar function" begin @@ -209,7 +189,6 @@ function test_differential_geometry() f = (t, x, v) -> t + x^2 Test.@test (X ⋅ f)(1, 1, 1) == 8 Test.@test Lie(φ, f, NonAutonomous, NonFixed)(1, 1, 1) == 8 - end @testset "Directional derivative of a vector field" begin @@ -250,18 +229,12 @@ function test_differential_geometry() Test.@test CTBase.:(⅋)(X, Y)([1, 2], [2, 3]) == [7, -1] # nonautonomous, nonfixed, dim 2 - X = VectorField( - (t, x, v) -> [t + v[1] + v[2] + x[2], -x[1]], - NonFixed, - NonAutonomous, - ) + X = VectorField((t, x, v) -> [t + v[1] + v[2] + x[2], -x[1]], NonFixed, NonAutonomous) Y = VectorField((t, x, v) -> [v[1] + v[2] + x[1], x[2]], NonFixed, NonAutonomous) Test.@test CTBase.:(⅋)(X, Y)(1, [1, 2], [2, 3]) == [8, -1] - end @testset "Lie bracket - length 2" begin - @testset "autonomous case" begin f = x -> [x[2], 2x[1]] g = x -> [3x[2], -x[1]] @@ -311,7 +284,6 @@ function test_differential_geometry() end @testset "intrinsic definition" begin - X = VectorField(x -> [x[2]^2, -2x[1] * x[2]]) Y = VectorField(x -> [x[1] * (1 + x[2]), 3x[2]^3]) f = x -> x[1]^4 + 2x[2]^3 @@ -319,7 +291,6 @@ function test_differential_geometry() # check real intrinsic order 2 definition of Lie bracket Test.@test ((@Lie [X, Y]) ⋅ f)(x) == ((X ⋅ (Y ⋅ f))(x) - (Y ⋅ (X ⋅ f))(x)) - end end @@ -411,11 +382,9 @@ function test_differential_geometry() Test.@test Poisson(F, G)([1, 2], [2, 1], [4, 4]) == -Poisson(G, F)([1, 2], [2, 1], [4, 4]) # anticommutativity Test.@test Poisson(F₊G, H)([1, 2], [2, 1], [4, 4]) == - Poisson(F, H)([1, 2], [2, 1], [4, 4]) + - Poisson(G, H)([1, 2], [2, 1], [4, 4]) # bilinearity 1 + Poisson(F, H)([1, 2], [2, 1], [4, 4]) + Poisson(G, H)([1, 2], [2, 1], [4, 4]) # bilinearity 1 Test.@test Poisson(H, F₊G)([1, 2], [2, 1], [4, 4]) == - Poisson(H, F)([1, 2], [2, 1], [4, 4]) + - Poisson(H, G)([1, 2], [2, 1], [4, 4]) # bilinearity 2 + Poisson(H, F)([1, 2], [2, 1], [4, 4]) + Poisson(H, G)([1, 2], [2, 1], [4, 4]) # bilinearity 2 Test.@test Poisson(FG, H)([1, 2], [2, 1], [4, 4]) == Poisson(F, H)([1, 2], [2, 1], [4, 4]) * G([1, 2], [2, 1], [4, 4]) + F([1, 2], [2, 1], [4, 4]) * Poisson(G, H)([1, 2], [2, 1], [4, 4]) # Liebniz's rule @@ -454,10 +423,8 @@ function test_differential_geometry() Poisson(H, F)(2, [1, 2], [2, 1], [4, 4]) + Poisson(H, G)(2, [1, 2], [2, 1], [4, 4]) # bilinearity 2 Test.@test Poisson(FG, H)(2, [1, 2], [2, 1], [4, 4]) == - Poisson(F, H)(2, [1, 2], [2, 1], [4, 4]) * - G(2, [1, 2], [2, 1], [4, 4]) + - F(2, [1, 2], [2, 1], [4, 4]) * - Poisson(G, H)(2, [1, 2], [2, 1], [4, 4]) # Liebniz's rule + Poisson(F, H)(2, [1, 2], [2, 1], [4, 4]) * G(2, [1, 2], [2, 1], [4, 4]) + + F(2, [1, 2], [2, 1], [4, 4]) * Poisson(G, H)(2, [1, 2], [2, 1], [4, 4]) # Liebniz's rule Test.@test Poisson(F, Poisson(G, H))(2, [1, 2], [2, 1], [4, 4]) + Poisson(G, Poisson(H, F))(2, [1, 2], [2, 1], [4, 4]) + Poisson(H, Poisson(F, G))(2, [1, 2], [2, 1], [4, 4]) == 0 # Jacobi identity @@ -465,7 +432,6 @@ function test_differential_geometry() end @testset "poisson bracket of Lifts" begin - @testset "autonomous case" begin f = x -> [x[1] + x[2]^2, x[1], 0] g = x -> [0, x[2], x[1]^2 + 4 * x[2]] @@ -473,10 +439,10 @@ function test_differential_geometry() G = Lift(g) F_ = (x, p) -> p' * f(x) G_ = (x, p) -> p' * g(x) - Test.@test Poisson(F, G)([1, 2, 3], [4, 0, 4]) ≈ - Poisson(F_, G_)([1, 2, 3], [4, 0, 4]) atol = 1e-6 - Test.@test Poisson(F, G_)([1, 2, 3], [4, 0, 4]) ≈ - Poisson(F_, G)([1, 2, 3], [4, 0, 4]) atol = 1e-6 + Test.@test Poisson(F, G)([1, 2, 3], [4, 0, 4]) ≈ Poisson(F_, G_)([1, 2, 3], [4, 0, 4]) atol = + 1e-6 + Test.@test Poisson(F, G_)([1, 2, 3], [4, 0, 4]) ≈ Poisson(F_, G)([1, 2, 3], [4, 0, 4]) atol = + 1e-6 end @testset "nonautonomous case" begin @@ -519,7 +485,6 @@ function test_differential_geometry() Poisson(F_, G, NonAutonomous, NonFixed)(2, [1, 2, 3], [4, 0, 4], 1) atol = 1e-6 end - end # macros @@ -601,7 +566,6 @@ function test_differential_geometry() get_F0 = () -> F0 F011___ = @Lie [[get_F0(), F1], F1] Test.@test F011_(t, x, v) ≈ F011___(t, x, v) atol = 1e-6 - end @testset "poisson macro" begin @@ -758,8 +722,7 @@ function test_differential_geometry() Test.@test @Lie [F0, F1](x) + 4 * [F1, F2](x) == [8, -8, -2] Test.@test @Lie [F0, F1](x) - [F1, F2](x) == [-2, -3, -2] Test.@test @Lie [F0, F1](x) .* [F1, F2](x) == [0, 4, 0] - Test.@test @Lie [1, 1, 1] + ([[F0, F1], F1](x) + [F1, F2](x) + [1, 1, 1]) == - [4, 5, -5] + Test.@test @Lie [1, 1, 1] + ([[F0, F1], F1](x) + [F1, F2](x) + [1, 1, 1]) == [4, 5, -5] # nonautonomous nonfixed F0 = VectorField( @@ -772,8 +735,7 @@ function test_differential_geometry() Test.@test @Lie [F0, F1](t, x, v) + 4 * [F1, F2](t, x, v) == [8, -8, -2] Test.@test @Lie [F0, F1](t, x, v) - [F1, F2](t, x, v) == [-2, -3, -2] Test.@test @Lie [F0, F1](t, x, v) .* [F1, F2](t, x, v) == [0, 4, 0] - Test.@test @Lie [1, 1, 1] + - ([[F0, F1], F1](t, x, v) + [F1, F2](t, x, v) + [1, 1, 1]) == + Test.@test @Lie [1, 1, 1] + ([[F0, F1], F1](t, x, v) + [F1, F2](t, x, v) + [1, 1, 1]) == [4, 5, -5] # poisson @@ -805,8 +767,6 @@ function test_differential_geometry() Test.@test @Lie {H0, H1}(t, x, p, v) + 4 * {H1, H2}(t, x, p, v) == -15 Test.@test @Lie {H0, H1}(t, x, p, v) - {H1, H2}(t, x, p, v) == 7.5 Test.@test @Lie {H0, H1}(t, x, p, v) * {H1, H2}(t, x, p, v) == -13.5 - Test.@test @Lie 4 + ({{H0, H1}, H1}(t, x, p, v) + -2 * {H1, H2}(t, x, p, v) + 21) == - 39 + Test.@test @Lie 4 + ({{H0, H1}, H1}(t, x, p, v) + -2 * {H1, H2}(t, x, p, v) + 21) == 39 end - end # test_differential_geometry diff --git a/test/test_exception.jl b/test/test_exception.jl index 5f568e27..7ce7fe8c 100644 --- a/test/test_exception.jl +++ b/test/test_exception.jl @@ -1,5 +1,4 @@ function test_exception() - e = AmbiguousDescription((:e,)) @test_throws ErrorException error(e) @test typeof(sprint(showerror, e)) == String @@ -23,5 +22,4 @@ function test_exception() e = ExtensionError(:tata) @test_throws ErrorException error(e) @test typeof(sprint(showerror, e)) == String - end diff --git a/test/test_function.jl b/test/test_function.jl index 12e6fe48..fb694073 100644 --- a/test/test_function.jl +++ b/test/test_function.jl @@ -1,5 +1,4 @@ function test_function() - ∅ = Vector{Real}() dummy_function() = nothing @@ -13,10 +12,7 @@ function test_function() (x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], Int64, ) - B = BoundaryConstraint( - (x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], - variable = false, - ) + B = BoundaryConstraint((x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], variable = false) @test B([0, 0], [1, 1]) == [1, 2] @test B([0, 0], [1, 1], ∅) == [1, 2] B = BoundaryConstraint( @@ -30,10 +26,7 @@ function test_function() @test Mayer(dummy_function, Fixed) == Mayer(dummy_function, variable = false) @test Mayer(dummy_function, NonFixed) == Mayer(dummy_function, variable = true) - @test_throws IncorrectArgument Mayer( - (x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], - Int64, - ) + @test_throws IncorrectArgument Mayer((x0, xf) -> [xf[2] - x0[1], 2xf[1] + x0[2]^2], Int64) G = Mayer((x0, xf) -> [xf[2] - x0[1]], variable = false) @test_throws MethodError G([0, 0], [1, 1]) G = Mayer((x0, xf) -> xf[2] - x0[1], variable = false) @@ -64,18 +57,10 @@ function test_function() @test_throws MethodError H(t, [1, 0], [0, 1]) @test_throws MethodError H([1, 0], [0, 1], v) @test H(t, [1, 0], [0, 1], v) == 3 - H = Hamiltonian( - (x, p, v) -> x[1]^2 + 2p[2] + v[3], - autonomous = true, - variable = true, - ) + H = Hamiltonian((x, p, v) -> x[1]^2 + 2p[2] + v[3], autonomous = true, variable = true) @test H([1, 0], [0, 1], [1, 2, 3]) == 6 @test H(t, [1, 0], [0, 1], [1, 2, 3]) == 6 - H = Hamiltonian( - (t, x, p) -> t + x[1]^2 + 2p[2], - autonomous = false, - variable = false, - ) + H = Hamiltonian((t, x, p) -> t + x[1]^2 + 2p[2], autonomous = false, variable = false) @test H(1, [1, 0], [0, 1]) == 4 @test H(1, [1, 0], [0, 1], v) == 4 H = Hamiltonian( @@ -156,11 +141,7 @@ function test_function() @test_throws MethodError V(t, [1, -1]) @test_throws MethodError V([1, -1], v) @test V(t, [1, -1], v) == [1, -2] - V = VectorField( - (x, v) -> [x[1]^2, 2x[2] + v[3]], - autonomous = true, - variable = true, - ) + V = VectorField((x, v) -> [x[1]^2, 2x[2] + v[3]], autonomous = true, variable = true) @test V([1, -1], [1, 2, 3]) == [1, 1] @test V(t, [1, -1], [1, 2, 3]) == [1, 1] V = VectorField((t, x) -> [t + x[1]^2, 2x[2]], autonomous = false, variable = false) @@ -201,11 +182,7 @@ function test_function() L = Lagrange((t, x, u) -> t + 2x[2] - u[1]^2, autonomous = false, variable = false) @test L(1, [1, 0], [1]) == 0 @test L(1, [1, 0], [1], v) == 0 - L = Lagrange( - (t, x, u, v) -> t + 2x[2] - u[1]^2 + v[3], - autonomous = false, - variable = true, - ) + L = Lagrange((t, x, u, v) -> t + 2x[2] - u[1]^2 + v[3], autonomous = false, variable = true) @test L(1, [1, 0], [1], [1, 2, 3]) == 3 end @@ -231,18 +208,10 @@ function test_function() @test_throws MethodError D(t, [1, 0], 1) @test_throws MethodError D([1, 0], 1, v) @test D(t, [1, 0], 1, v) == [-1, 1] - D = Dynamics( - (x, u, v) -> [2x[2] - u^2 + v[3], x[1]], - autonomous = true, - variable = true, - ) + D = Dynamics((x, u, v) -> [2x[2] - u^2 + v[3], x[1]], autonomous = true, variable = true) @test D([1, 0], 1, [1, 2, 3]) == [2, 1] @test D(t, [1, 0], 1, [1, 2, 3]) == [2, 1] - D = Dynamics( - (t, x, u) -> [t + 2x[2] - u^2, x[1]], - autonomous = false, - variable = false, - ) + D = Dynamics((t, x, u) -> [t + 2x[2] - u^2, x[1]], autonomous = false, variable = false) @test D(1, [1, 0], 1) == [0, 1] @test D(1, [1, 0], 1, v) == [0, 1] D = Dynamics( @@ -274,18 +243,10 @@ function test_function() @test_throws MethodError S(t, [1, -1]) @test_throws MethodError S([1, -1], v) @test S(t, [1, -1], v) == [1, -2] - S = StateConstraint( - (x, v) -> [x[1]^2, 2x[2] + v[3]], - autonomous = true, - variable = true, - ) + S = StateConstraint((x, v) -> [x[1]^2, 2x[2] + v[3]], autonomous = true, variable = true) @test S([1, -1], [1, 2, 3]) == [1, 1] @test S(t, [1, -1], [1, 2, 3]) == [1, 1] - S = StateConstraint( - (t, x) -> [t + x[1]^2, 2x[2]], - autonomous = false, - variable = false, - ) + S = StateConstraint((t, x) -> [t + x[1]^2, 2x[2]], autonomous = false, variable = false) @test S(1, [1, -1]) == [2, -2] @test S(1, [1, -1], v) == [2, -2] S = StateConstraint( @@ -317,18 +278,10 @@ function test_function() @test_throws MethodError C(t, [1, -1]) @test_throws MethodError C([1, -1], v) @test C(t, [1, -1], v) == [1, -2] - C = ControlConstraint( - (u, v) -> [u[1]^2, 2u[2] + v[3]], - autonomous = true, - variable = true, - ) + C = ControlConstraint((u, v) -> [u[1]^2, 2u[2] + v[3]], autonomous = true, variable = true) @test C([1, -1], [1, 2, 3]) == [1, 1] @test C(t, [1, -1], [1, 2, 3]) == [1, 1] - C = ControlConstraint( - (t, u) -> [t + u[1]^2, 2u[2]], - autonomous = false, - variable = false, - ) + C = ControlConstraint((t, u) -> [t + u[1]^2, 2u[2]], autonomous = false, variable = false) @test C(1, [1, -1]) == [2, -2] @test C(1, [1, -1], v) == [2, -2] C = ControlConstraint( @@ -353,11 +306,7 @@ function test_function() # dim x = 2, dim u = 1, dim output = 2 @test_throws IncorrectArgument MixedConstraint((x, u) -> [2x[2] - u^2, x[1]], Int64) @test_throws IncorrectArgument MixedConstraint((x, u) -> [2x[2] - u^2, x[1]], Int64) - M = MixedConstraint( - (x, u) -> [2x[2] - u^2, x[1]], - autonomous = true, - variable = false, - ) + M = MixedConstraint((x, u) -> [2x[2] - u^2, x[1]], autonomous = true, variable = false) @test M([1, 0], 1) == [-1, 1] t = 1 v = Real[] @@ -412,18 +361,10 @@ function test_function() @test_throws MethodError u(t, [1, 0]) @test_throws MethodError u([1, 0], v) @test u(t, [1, 0], v) == 1 - u = FeedbackControl( - (x, v) -> x[1]^2 + 2x[2] + v[3], - autonomous = true, - variable = true, - ) + u = FeedbackControl((x, v) -> x[1]^2 + 2x[2] + v[3], autonomous = true, variable = true) @test u([1, 0], [1, 2, 3]) == 4 @test u(t, [1, 0], [1, 2, 3]) == 4 - u = FeedbackControl( - (t, x) -> t + x[1]^2 + 2x[2], - autonomous = false, - variable = false, - ) + u = FeedbackControl((t, x) -> t + x[1]^2 + 2x[2], autonomous = false, variable = false) @test u(1, [1, 0]) == 2 @test u(1, [1, 0], v) == 2 u = FeedbackControl( @@ -434,7 +375,6 @@ function test_function() @test u(1, [1, 0], [1, 2, 3]) == 5 end - @testset "ControlLaw" begin @test ControlLaw(dummy_function, Autonomous, Fixed) == ControlLaw(dummy_function, autonomous = true, variable = false) @@ -456,18 +396,10 @@ function test_function() @test_throws MethodError u(t, [1, 0], [0, 1]) @test_throws MethodError u([1, 0], [0, 1], v) @test u(t, [1, 0], [0, 1], v) == 3 - u = ControlLaw( - (x, p, v) -> x[1]^2 + 2p[2] + v[3], - autonomous = true, - variable = true, - ) + u = ControlLaw((x, p, v) -> x[1]^2 + 2p[2] + v[3], autonomous = true, variable = true) @test u([1, 0], [0, 1], [1, 2, 3]) == 6 @test u(t, [1, 0], [0, 1], [1, 2, 3]) == 6 - u = ControlLaw( - (t, x, p) -> t + x[1]^2 + 2p[2], - autonomous = false, - variable = false, - ) + u = ControlLaw((t, x, p) -> t + x[1]^2 + 2p[2], autonomous = false, variable = false) @test u(1, [1, 0], [0, 1]) == 4 @test u(1, [1, 0], [0, 1], v) == 4 u = ControlLaw( @@ -499,18 +431,10 @@ function test_function() @test_throws MethodError μ(t, [1, 0], [0, 1]) @test_throws MethodError μ([1, 0], [0, 1], v) @test μ(t, [1, 0], [0, 1], v) == 3 - μ = Multiplier( - (x, p, v) -> x[1]^2 + 2p[2] + v[3], - autonomous = true, - variable = true, - ) + μ = Multiplier((x, p, v) -> x[1]^2 + 2p[2] + v[3], autonomous = true, variable = true) @test μ([1, 0], [0, 1], [1, 2, 3]) == 6 @test μ(t, [1, 0], [0, 1], [1, 2, 3]) == 6 - μ = Multiplier( - (t, x, p) -> t + x[1]^2 + 2p[2], - autonomous = false, - variable = false, - ) + μ = Multiplier((t, x, p) -> t + x[1]^2 + 2p[2], autonomous = false, variable = false) @test μ(1, [1, 0], [0, 1]) == 4 @test μ(1, [1, 0], [0, 1], v) == 4 μ = Multiplier( @@ -520,5 +444,4 @@ function test_function() ) @test μ(1, [1, 0], [0, 1], [1, 2, 3]) == 7 end - end diff --git a/test/test_goddard.jl b/test/test_goddard.jl index 129eee24..f902775f 100644 --- a/test/test_goddard.jl +++ b/test/test_goddard.jl @@ -15,7 +15,6 @@ test_goddard() = begin # Abstract model @def ocp begin - tf ∈ R, variable t ∈ [t0, tf], time x ∈ R³, state @@ -36,7 +35,6 @@ test_goddard() = begin ẋ(t) == [v(t), -D(t) / m(t) - 1 / r²(t), 0] + u(t) * [0, Tmax / m(t), -b * Tmax] r(tf) → max - end F0(x) = begin @@ -56,5 +54,4 @@ test_goddard() = begin u = 2 tf = 1 @test ocp.dynamics(x, u, tf) == F0(x) + u * F1(x) - end diff --git a/test/test_model.jl b/test/test_model.jl index 44b40a4c..39f14f53 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -1,5 +1,4 @@ function test_model() # 30 55 185 - ∅ = Vector{Real}() @testset "variable!" begin @@ -47,13 +46,10 @@ function test_model() # 30 55 185 variable!(ocp, 2, :vv) @test ocp.variable_dimension == 2 @test ocp.variable_components_names == ["vv₁", "vv₂"] - end @testset "time, state and control set or not" begin - for i ∈ 1:7 - ocp = Model() i == 2 && begin @@ -79,13 +75,7 @@ function test_model() # 30 55 185 end # constraint! 1 - @test_throws UnauthorizedCall __constraint!( - ocp, - :initial, - 1:2:5, - [0, 0, 0], - [0, 0, 0], - ) + @test_throws UnauthorizedCall __constraint!(ocp, :initial, 1:2:5, [0, 0, 0], [0, 0, 0]) @test_throws UnauthorizedCall __constraint!(ocp, :initial, 2:3, [0, 0], [0, 0]) @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(2), 0, 0) @@ -98,13 +88,7 @@ function test_model() # 30 55 185 @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(1), 0, 2) @test_throws UnauthorizedCall __constraint!(ocp, :control, Index(1), 0, 2) @test_throws UnauthorizedCall __constraint!(ocp, :state, 2:3, [0, 0], [1, 2]) - @test_throws UnauthorizedCall __constraint!( - ocp, - :initial, - 1:2:5, - [0, 0, 0], - [1, 2, 1], - ) + @test_throws UnauthorizedCall __constraint!(ocp, :initial, 1:2:5, [0, 0, 0], [1, 2, 1]) # constraint! 4 @test_throws UnauthorizedCall __constraint!(ocp, :initial, [0, 0, 0], [1, 2, 1]) @@ -140,13 +124,7 @@ function test_model() # 30 55 185 [0, 0, 0], [1, 2, 1], ) - @test_throws UnauthorizedCall __constraint!( - ocp, - :mixed, - (x, u) -> x[1] - u, - 0, - 1, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (x, u) -> x[1] - u, 0, 1) # time dependent and variable independent ocp @test_throws UnauthorizedCall __constraint!(ocp, :control, (t, u) -> 2u, 0, 1) @@ -157,22 +135,10 @@ function test_model() # 30 55 185 [0, 0, 0], [1, 2, 1], ) - @test_throws UnauthorizedCall __constraint!( - ocp, - :mixed, - (t, x, u) -> x[1] - u, - 0, - 1, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (t, x, u) -> x[1] - u, 0, 1) # time independent and variable dependent ocp - @test_throws UnauthorizedCall __constraint!( - ocp, - :control, - (u, v) -> 2u * v[1], - 0, - 1, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :control, (u, v) -> 2u * v[1], 0, 1) @test_throws UnauthorizedCall __constraint!( ocp, :state, @@ -189,13 +155,7 @@ function test_model() # 30 55 185 ) # time dependent and variable dependent ocp - @test_throws UnauthorizedCall __constraint!( - ocp, - :control, - (t, u, v) -> 2u + v[2], - 0, - 1, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :control, (t, u, v) -> 2u + v[2], 0, 1) @test_throws UnauthorizedCall __constraint!( ocp, :state, @@ -239,13 +199,7 @@ function test_model() # 30 55 185 [0, 0, 0], [0, 0, 0], ) - @test_throws UnauthorizedCall __constraint!( - ocp, - :mixed, - (x, u) -> x[1] - u, - 0, - 0, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (x, u) -> x[1] - u, 0, 0) # time dependent and variable independent ocp @test_throws UnauthorizedCall __constraint!(ocp, :control, (t, u) -> 2u, 1, 1) @@ -256,22 +210,10 @@ function test_model() # 30 55 185 [0, 0, 0], [0, 0, 0], ) - @test_throws UnauthorizedCall __constraint!( - ocp, - :mixed, - (t, x, u) -> x[1] - u, - 0, - 0, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (t, x, u) -> x[1] - u, 0, 0) # time independent and variable dependent ocp - @test_throws UnauthorizedCall __constraint!( - ocp, - :control, - (u, v) -> 2u * v[1], - 1, - 1, - ) + @test_throws UnauthorizedCall __constraint!(ocp, :control, (u, v) -> 2u * v[1], 1, 1) @test_throws UnauthorizedCall __constraint!( ocp, :state, @@ -309,9 +251,7 @@ function test_model() # 30 55 185 0, 0, ) - end - end @testset "initial and / or final time already set" begin @@ -453,7 +393,6 @@ function test_model() # 30 55 185 @test_throws IncorrectArgument Model(NonAutonomous, Autonomous) @test_throws IncorrectArgument Model(NonFixed, Int64) @test_throws IncorrectArgument Model(NonFixed, Int64) - end @testset "time and variable dependence Bool" begin @@ -502,7 +441,6 @@ function test_model() # 30 55 185 @test_throws IncorrectArgument Model(NonAutonomous, Autonomous) @test_throws IncorrectArgument Model(NonFixed, Int64) @test_throws IncorrectArgument Model(NonFixed, Int64) - end @testset "state!" begin @@ -694,11 +632,9 @@ function test_model() # 30 55 185 objective!(ocp, :bolza, (x0, xf) -> x0[1] + xf[2], (x, u) -> x[1]^2 + u^2, :max) # the control is of dimension 1 @test is_max(ocp) @test !is_min(ocp) - end @testset "constraint! 1" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -725,11 +661,9 @@ function test_model() # 30 55 185 control!(ocp, 1) __constraint!(ocp, :initial, 0, 0, :c) @test_throws UnauthorizedCall __constraint!(ocp, :final, 0, 0, :c) - end @testset "constraint! 2" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -776,11 +710,9 @@ function test_model() # 30 55 185 control!(ocp, 1) __constraint!(ocp, :initial, Index(1), 0, 0, :c) @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(1), 0, 0, :c) - end @testset "constraint! 3" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -814,11 +746,9 @@ function test_model() # 30 55 185 control!(ocp, 1) __constraint!(ocp, :initial, 0, 1, :c) @test_throws UnauthorizedCall __constraint!(ocp, :final, 0, 1, :c) - end @testset "constraint! 4" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -836,38 +766,10 @@ function test_model() # 30 55 185 __time!(ocp, 0, 1) state!(ocp, 2) control!(ocp, 2) - @test_throws IncorrectArgument __constraint!( - ocp, - :initial, - Index(2), - [0, 1], - [1, 2], - :c0, - ) - @test_throws IncorrectArgument __constraint!( - ocp, - :final, - Index(2), - [1, 2], - [2, 3], - :cf, - ) - @test_throws IncorrectArgument __constraint!( - ocp, - :control, - Index(2), - [0, 1], - [1, 2], - :cu, - ) - @test_throws IncorrectArgument __constraint!( - ocp, - :state, - Index(2), - [0, 1], - [1, 2], - :cs, - ) + @test_throws IncorrectArgument __constraint!(ocp, :initial, Index(2), [0, 1], [1, 2], :c0) + @test_throws IncorrectArgument __constraint!(ocp, :final, Index(2), [1, 2], [2, 3], :cf) + @test_throws IncorrectArgument __constraint!(ocp, :control, Index(2), [0, 1], [1, 2], :cu) + @test_throws IncorrectArgument __constraint!(ocp, :state, Index(2), [0, 1], [1, 2], :cs) ocp = Model() __time!(ocp, 0, 1) @@ -889,11 +791,9 @@ function test_model() # 30 55 185 control!(ocp, 1) __constraint!(ocp, :initial, Index(1), 0, 1, :c) @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(1), 0, 1, :c) - end @testset "constraint! 5" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -934,14 +834,7 @@ function test_model() # 30 55 185 ) __constraint!(ocp, :control, u -> u[1:2], [0, 0], [0, 0], :cu) __constraint!(ocp, :state, x -> x[1:2], [0, 0], [0, 0], :cs) - __constraint!( - ocp, - :mixed, - (x, u) -> [x[1] + u[1], x[2] + u[2]], - [0, 0], - [0, 0], - :cm, - ) + __constraint!(ocp, :mixed, (x, u) -> [x[1] + u[1], x[2] + u[2]], [0, 0], [0, 0], :cm) @test constraint(ocp, :cb)([13, 14, 15], [17, 18, 19]) == [13 + 17, 14 + 18] @test constraint(ocp, :cu)([12, 13, 14]) == [12, 13] @test constraint(ocp, :cs)([12, 13, 14]) == [12, 13] @@ -954,11 +847,9 @@ function test_model() # 30 55 185 control!(ocp, 1) __constraint!(ocp, :control, u -> u, 0, 1, :c) @test_throws UnauthorizedCall __constraint!(ocp, :control, u -> u, 0, 1, :c) - end @testset "constraint! 6" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -999,23 +890,14 @@ function test_model() # 30 55 185 ) __constraint!(ocp, :control, u -> u[1:2], [0, 0], [1, 1], :cu) __constraint!(ocp, :state, x -> x[1:2], [0, 0], [1, 1], :cs) - __constraint!( - ocp, - :mixed, - (x, u) -> [x[1] + u[1], x[2] + u[2]], - [0, 0], - [1, 1], - :cm, - ) + __constraint!(ocp, :mixed, (x, u) -> [x[1] + u[1], x[2] + u[2]], [0, 0], [1, 1], :cm) @test constraint(ocp, :cb)([13, 14, 15], [17, 18, 19]) == [13 + 17, 14 + 18] @test constraint(ocp, :cu)([12, 13, 14]) == [12, 13] @test constraint(ocp, :cs)([12, 13, 14]) == [12, 13] @test constraint(ocp, :cm)([12, 13, 14], [15, 16, 17]) == [12 + 15, 13 + 16] - end @testset "constraint! 7" begin - x = 1 u = 2 v = [3, 4, 5, 6] @@ -1036,11 +918,9 @@ function test_model() # 30 55 185 @test constraint(ocp, :eq3)(v) == v[1:2] @test constraint(ocp, :eq4)(v) == v[1:2:4] @test constraint(ocp, :eq5)(v) == v .^ 2 - end @testset "constraint! 8" begin - x = 1 u = 2 v = [3, 4, 5, 6] @@ -1069,11 +949,9 @@ function test_model() # 30 55 185 @test constraint(ocp, :eq3)(v) == v[1:2] @test constraint(ocp, :eq4)(v) == v[1:2:4] @test constraint(ocp, :eq5)(v) == v .^ 2 - end @testset "constraint! 9" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -1101,11 +979,9 @@ function test_model() # 30 55 185 control!(ocp, 1) dynamics!(ocp, (x, u) -> [x[1] + u, x[2] + u]) @test ocp.dynamics([1, 2], 3) == [4, 5] - end @testset "constraint! 10" begin - ocp = Model(autonomous = false) __time!(ocp, 0, 1) state!(ocp, 1) @@ -1114,11 +990,9 @@ function test_model() # 30 55 185 __constraint!(ocp, :final, 1, 2, :cf) @test constraint(ocp, :c0)(12, ∅) == 12 @test constraint(ocp, :cf)(∅, 12) == 12 - end @testset "constraint! 11" begin - dummy(u) = u^2 + u ocp = Model(variable = true) @@ -1167,18 +1041,8 @@ function test_model() # 30 55 185 control!(ocp_error, 1) variable!(ocp_error, 1) @test_throws UnauthorizedCall constraint!(ocp_error, :variable) - @test_throws UnauthorizedCall constraint!( - ocp_error, - :control, - f = dummy, - label = :c1, - ) - @test_throws UnauthorizedCall constraint!( - ocp_error, - :state, - rg = 1:2:3, - label = :c2, - ) + @test_throws UnauthorizedCall constraint!(ocp_error, :control, f = dummy, label = :c1) + @test_throws UnauthorizedCall constraint!(ocp_error, :state, rg = 1:2:3, label = :c2) @test_throws IncorrectArgument constraint!( ocp_error, :state, @@ -1204,11 +1068,9 @@ function test_model() # 30 55 185 ub = [0, 0], label = :c5, ) - end @testset "remove_constraint! and constraints_labels" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 1) @@ -1222,11 +1084,9 @@ function test_model() # 30 55 185 k = constraints_labels(ocp) @test :cb ∉ k @test_throws IncorrectArgument remove_constraint!(ocp, :dummy_con) - end @testset "nlp_constraints! without variable" begin - ocp = Model() __time!(ocp, 0, 1) state!(ocp, 2) @@ -1308,11 +1168,9 @@ function test_model() # 30 55 185 @test dim_control_range(ocp) == 1 @test dim_state_range(ocp) == 2 @test dim_variable_range(ocp) == 0 - end @testset "nlp_constraints! with variable" begin - ocp = Model(variable = true) __time!(ocp, 0, 1) variable!(ocp, 4) @@ -1399,12 +1257,9 @@ function test_model() # 30 55 185 @test dim_control_range(ocp) == 1 @test dim_state_range(ocp) == 2 @test dim_variable_range(ocp) == 7 - end - @testset "val vs lb and ub, errors" begin - ocp = Model(variable = true) time!(ocp; t0 = 0, tf = 1) @@ -1429,13 +1284,7 @@ function test_model() # 30 55 185 ub = 1, label = :cf, ) - @test_throws UnauthorizedCall constraint!( - ocp, - :control; - val = 0, - ub = 0, - label = :cu, - ) + @test_throws UnauthorizedCall constraint!(ocp, :control; val = 0, ub = 0, label = :cu) @test_throws UnauthorizedCall constraint!( ocp, :state; @@ -1507,7 +1356,6 @@ function test_model() # 30 55 185 label = :cv4, ) - ocp = Model(variable = true) time!(ocp; t0 = 0, tf = 1) @@ -1532,13 +1380,7 @@ function test_model() # 30 55 185 lb = 1, label = :cf, ) - @test_throws UnauthorizedCall constraint!( - ocp, - :control; - val = 0, - lb = 0, - label = :cu, - ) + @test_throws UnauthorizedCall constraint!(ocp, :control; val = 0, lb = 0, label = :cu) @test_throws UnauthorizedCall constraint!( ocp, :state; @@ -1609,11 +1451,9 @@ function test_model() # 30 55 185 lb = -10, label = :cv4, ) - end @testset "val vs lb and ub, 1/2" begin - ocp = Model(variable = true) time!(ocp; t0 = 0, tf = 1) @@ -1642,14 +1482,7 @@ function test_model() # 30 55 185 ub = [100, 101], label = :css, ) - constraint!( - ocp, - :mixed; - f = (x, u, v) -> x[1] + u + v[2], - lb = -1, - ub = -1, - label = :cm, - ) + constraint!(ocp, :mixed; f = (x, u, v) -> x[1] + u + v[2], lb = -1, ub = -1, label = :cm) constraint!(ocp, :variable; lb = [5, 5, 5, 5], ub = [5, 5, 5, 5], label = :cv1) constraint!(ocp, :variable; rg = 1:2, lb = [10, 20], ub = [10, 20], label = :cv2) constraint!(ocp, :variable; rg = Index(3), lb = 1000, ub = 1000, label = :cv3) @@ -1723,11 +1556,9 @@ function test_model() # 30 55 185 @test dim_control_range(ocp) == 1 @test dim_state_range(ocp) == 2 @test dim_variable_range(ocp) == 7 - end @testset "val vs lb and ub, 2/2" begin - ocp = Model(variable = true) time!(ocp; t0 = 0, tf = 1) @@ -1739,13 +1570,7 @@ function test_model() # 30 55 185 constraint!(ocp, :final; rg = Index(1), val = 1, label = :cf) constraint!(ocp, :control; val = 0, label = :cu) constraint!(ocp, :state; val = [0, 1], label = :cs) - constraint!( - ocp, - :boundary; - f = (x0, xf, v) -> x0[2] + xf[2] + v[1], - val = 2, - label = :cb, - ) + constraint!(ocp, :boundary; f = (x0, xf, v) -> x0[2] + xf[2] + v[1], val = 2, label = :cb) constraint!(ocp, :control; f = (u, v) -> u + v[2], val = 20, label = :cuu) constraint!(ocp, :state; f = (x, v) -> x + v[1:2], val = [100, 101], label = :css) constraint!(ocp, :mixed; f = (x, u, v) -> x[1] + u + v[2], val = -1, label = :cm) @@ -1822,11 +1647,9 @@ function test_model() # 30 55 185 @test dim_control_range(ocp) == 1 @test dim_state_range(ocp) == 2 @test dim_variable_range(ocp) == 7 - end @testset "objective!" begin - ocp = Model() @test_throws UnauthorizedCall objective!(ocp, :lagrange, (x, u) -> 0.5u^2) @test_throws UnauthorizedCall objective!(ocp, :mayer, (t0, x0, tf, xf) -> 0.5x0^2) @@ -1896,11 +1719,9 @@ function test_model() # 30 55 185 objective!(ocp, :mayer, (x0, xf) -> 0.5x0[1]^2) @test ocp.mayer([2, 3], [5, 6]) == 2 @test isnothing(ocp.lagrange) - end @testset "redeclarations" begin - ocp = Model(variable = true) variable!(ocp, 1) @test_throws UnauthorizedCall variable!(ocp, 1) @@ -1943,12 +1764,6 @@ function test_model() # 30 55 185 state!(ocp, 1) control!(ocp, 1) objective!(ocp, :bolza, (x0, xf) -> x0 + xf, (x, u) -> x + u) - @test_throws UnauthorizedCall objective!( - ocp, - :bolza, - (x0, xf) -> x0 + xf, - (x, u) -> x + u, - ) + @test_throws UnauthorizedCall objective!(ocp, :bolza, (x0, xf) -> x0 + xf, (x, u) -> x + u) end - end diff --git a/test/test_onepass.jl b/test/test_onepass.jl index ee7746b4..848441ad 100644 --- a/test/test_onepass.jl +++ b/test/test_onepass.jl @@ -32,7 +32,6 @@ function test_onepass() x = 10 u = 20 @test o.dynamics(x, u) == x + u + b + 3 + d - end @testset "log" begin @@ -192,7 +191,6 @@ function test_onepass() @test ocp.control_dimension == 3 @test ocp.state_name == "x" @test ocp.state_dimension == 3 - end # --------------------------------------------------------------- @@ -267,7 +265,7 @@ function test_onepass() @test_throws ParsingError @def o begin v, variable - t ∈ [t0[v], tf[v+1]], time + t ∈ [t0[v], tf[v + 1]], time end t0 = 0.0 @@ -289,7 +287,6 @@ function test_onepass() @test ocp isa OptimalControlModel @test ocp.variable_dimension == 3 @test ocp.variable_name == "a" - end # --------------------------------------------------------------- @@ -329,7 +326,6 @@ function test_onepass() @test ocp.time_name == "t" @test ocp.initial_time == Index(1) @test ocp.final_time == tf - end # --------------------------------------------------------------- @@ -490,7 +486,6 @@ function test_onepass() @test ocp.control_dimension == 1 @test ocp.control_name == "a" - t0 = 5.0 tf = 5.1 @def ocp begin @@ -530,7 +525,6 @@ function test_onepass() @test ocp.final_time == tf @test ocp.control_dimension == n @test ocp.control_name == "u" - end # --------------------------------------------------------------- @@ -614,7 +608,6 @@ function test_onepass() w = 11 @test_throws MethodError o.dynamics(y, w, z) @test o.mayer(y0, yf, z) == y0[1] + y0[4]^3 + z[2] + yf[2] - end # --------------------------------------------------------------- @@ -1148,7 +1141,6 @@ function test_onepass() @test o.constraints[:eq9][3] == [0, 0] @test o.constraints[:eq10][3] == [0, 0] - t0 = 9.0 tf = 9.1 r0 = 1.0 @@ -1253,7 +1245,6 @@ function test_onepass() tf = 1.0 n = 3 @def ocp1 begin - t ∈ [t0, tf], time x ∈ R^n, state u ∈ R^n, control @@ -1290,7 +1281,6 @@ function test_onepass() @test ocp2.initial_time == t0 @test ocp2.final_time == tf - # all used variables must be defined before each test xf = 11.11 * ones(4) xf2 = 11.111 @@ -1332,7 +1322,6 @@ function test_onepass() yf_u = 2.2222 * ones(2) @def ocp4 begin - t ∈ [t0, tf], time x ∈ R^n, state u ∈ R^n, control @@ -1348,7 +1337,6 @@ function test_onepass() @test ocp4.initial_time == t0 @test ocp4.final_time == tf - # === boundary t0 = 0.4 tf = 1.4 @@ -1390,7 +1378,6 @@ function test_onepass() @test ocp6.initial_time == t0 @test ocp6.final_time == tf - # define more variables u_b = 1.0 u_u = 2.0 @@ -1502,7 +1489,6 @@ function test_onepass() tf = 1.111 n = 12 @def ocp11 begin - t ∈ [t0, tf], time x ∈ R^n, state u ∈ R^n, control @@ -1555,7 +1541,6 @@ function test_onepass() u ∈ R, control ẋ(t) == f(x(t), u(t)), named_dynamics_not_allowed # but allowed if unnamed ! end - end # --------------------------------------------------------------- @@ -1919,7 +1904,6 @@ function test_onepass() ∫(0.5u(t)^2) → max end @test ocp isa OptimalControlModel - end t0 = 0 @@ -1946,7 +1930,6 @@ function test_onepass() (-0.5 + x(t)) * ∫(u(t)^2) → max end - # --------------------------------------------------------------- # --------------------------------------------------------------- @testset "Bolza cost" begin @@ -2257,7 +2240,6 @@ function test_onepass() u ∈ R, control t * ∫(x(t) + u(t)) - 1 → max end - end # --------------------------------------------------------------- @@ -2377,7 +2359,6 @@ function test_onepass() w = 11 @test o.dynamics(y, w, z) == [y[1] + w^2 + y[4]^3 + z[2], y[3]^2, 0, 0] @test_throws UndefVarError o.mayer(y0, yf, z) - end # --------------------------------------------------------------- @@ -2402,7 +2383,6 @@ function test_onepass() x = 10 u = 20 @test o.dynamics(x, u) == x + u + b + 3 + d - end # --------------------------------------------------------------- @@ -2474,7 +2454,6 @@ function test_onepass() ẋ(t) == A * x(t) + B * u(t) ∫(u(t)^2) / 2 → min # forbidden end - end # --------------------------------------------------------------- @@ -2591,7 +2570,5 @@ function test_onepass() @test constraint(o, :eq3)(z) == z[1] @test o.dynamics(x, u, z) == [x[2], x[1]^2 + z[1]] @test o.lagrange(x, u, z) == u[1]^2 + z[1] * x[1] - end - end diff --git a/test/test_plot.jl b/test/test_plot.jl index 917b5033..7e6fc3ac 100644 --- a/test/test_plot.jl +++ b/test/test_plot.jl @@ -10,7 +10,7 @@ function test_plot() a = x0[1] b = x0[2] C = [ - -(tf - t0)^3/6.0 (tf-t0)^2/2.0 + -(tf - t0)^3/6.0 (tf - t0)^2/2.0 -(tf - t0)^2/2.0 (tf-t0) ] D = [-a - b * (tf - t0), -b] + xf @@ -51,5 +51,4 @@ function test_plot() @test plot(sol, layout = :split) isa Plots.Plot @test plot(sol, layout = :group) isa Plots.Plot @test display(sol) isa Nothing - end diff --git a/test/test_plot_manual.jl b/test/test_plot_manual.jl index 3008479d..a5ce33a0 100644 --- a/test/test_plot_manual.jl +++ b/test/test_plot_manual.jl @@ -19,7 +19,7 @@ xf = [0.0, 0.0] a = x0[1] b = x0[2] C = [ - -(tf - t0)^3/6.0 (tf-t0)^2/2.0 + -(tf - t0)^3/6.0 (tf - t0)^2/2.0 -(tf - t0)^2/2.0 (tf-t0) ] D = [-a - b * (tf - t0), -b] + xf @@ -82,7 +82,7 @@ xf = [0.0, 0.0] a = x0[1] b = x0[2] C = [ - -(tf - t0)^3/6.0 (tf-t0)^2/2.0 + -(tf - t0)^3/6.0 (tf - t0)^2/2.0 -(tf - t0)^2/2.0 (tf-t0) ] D = [-a - b * (tf - t0), -b] + xf @@ -121,14 +121,7 @@ sol.message = "ceci est un test" sol.success = true if do_plot_2 - plot!( - plt, - sol, - layout = layout, - size = size, - control = control_plt, - solution_label = "sol2", - ) + plot!(plt, sol, layout = layout, size = size, control = control_plt, solution_label = "sol2") else plt end @@ -214,6 +207,5 @@ sol.message = "structure: B+B0B+B0B+" sol.success = true sol.infos[:resolution] = :numerical - plt_transfert = plot(sol, layout=:split, size=(900, 600)) =# diff --git a/test/test_plot_series.jl b/test/test_plot_series.jl index 069b42f8..4231e4f6 100644 --- a/test/test_plot_series.jl +++ b/test/test_plot_series.jl @@ -1,7 +1,6 @@ using Plots function keep_series_attributes(; kwargs...) - series_attributes = Plots.attributes(:Series) out = [] @@ -10,7 +9,6 @@ function keep_series_attributes(; kwargs...) end return out - end function print_kwargs(; kwargs...) diff --git a/test/test_print.jl b/test/test_print.jl index 7256e984..e9148256 100644 --- a/test/test_print.jl +++ b/test/test_print.jl @@ -65,5 +65,4 @@ function test_print() dynamics!(ocp, (t, x, u) -> x + u) objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) @test display(ocp) isa Nothing - end diff --git a/test/test_solution.jl b/test/test_solution.jl index 2efc164b..b818fc6f 100644 --- a/test/test_solution.jl +++ b/test/test_solution.jl @@ -54,5 +54,4 @@ function test_solution() @test sol.variable == variable @test typeof(sol) == OptimalControlSolution @test_throws UndefKeywordError OptimalControlSolution(ocp; state, control, objective) - end diff --git a/test/test_utils.jl b/test/test_utils.jl index b4cb74b3..a2dc236f 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -1,7 +1,5 @@ function test_utils() - @testset "AD" begin - x0 = 1.0 f = x -> cos(x) @@ -23,11 +21,9 @@ function test_utils() g = x -> [cos(x[1]) + sin(x[2])] @test CTBase.ctjacobian(g, [x0, x0]) ≈ [-sin(x0) cos(x0)] atol = 1e-10 @test CTBase.__ctjacobian(g, [x0, x0]) ≈ [-sin(x0) cos(x0)] atol = 1e-10 - end @testset "Other" begin - v = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0] n = 2 u = vec2vec(v, n) @@ -81,7 +77,5 @@ function test_utils() @test_throws IncorrectArgument CTBase.ctupperscripts(-1) @test CTBase.ctupperscripts(019) == "¹⁹" @test CTBase.ctupperscripts(109) == "¹⁰⁹" - end - end diff --git a/test/utils.jl b/test/utils.jl index 6c5be10a..80a2484d 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,7 +1,7 @@ # time! function __time!( - ocp::OptimalControlModel{<:TimeDependence,NonFixed}, + ocp::OptimalControlModel{<:TimeDependence, NonFixed}, t0::Time, indf::Index, name::String = CTBase.__time_name(), @@ -14,7 +14,7 @@ function __time!(ocp::OptimalControlModel, t0::Time, indf::Index, name::Symbol) end function __time!( - ocp::OptimalControlModel{<:TimeDependence,NonFixed}, + ocp::OptimalControlModel{<:TimeDependence, NonFixed}, ind0::Index, tf::Time, name::String = CTBase.__time_name(), @@ -27,7 +27,7 @@ function __time!(ocp::OptimalControlModel, ind0::Index, tf::Time, name::Symbol) end function __time!( - ocp::OptimalControlModel{<:TimeDependence,NonFixed}, + ocp::OptimalControlModel{<:TimeDependence, NonFixed}, ind0::Index, indf::Index, name::String = CTBase.__time_name(), @@ -39,12 +39,7 @@ function __time!(ocp::OptimalControlModel, ind0::Index, indf::Index, name::Symbo time!(ocp; ind0 = ind0.val, indf = indf.val, name = name) end -function __time!( - ocp::OptimalControlModel, - t0::Time, - tf::Time, - name::String = CTBase.__time_name(), -) +function __time!(ocp::OptimalControlModel, t0::Time, tf::Time, name::String = CTBase.__time_name()) time!(ocp; t0 = t0, tf = tf, name = name) end @@ -55,55 +50,48 @@ end # constraint! function __constraint!( - ocp::OptimalControlModel{<:TimeDependence,V}, + ocp::OptimalControlModel{<:TimeDependence, V}, type::Symbol, rg::Index, - lb::Union{ctVector,Nothing}, - ub::Union{ctVector,Nothing}, + lb::Union{ctVector, Nothing}, + ub::Union{ctVector, Nothing}, label::Symbol = CTBase.__constraint_label(), -) where {V<:VariableDependence} - +) where {V <: VariableDependence} constraint!(ocp, type, rg = rg.val, f = nothing, lb = lb, ub = ub, label = label) nothing # to force to return nothing - end function __constraint!( - ocp::OptimalControlModel{<:TimeDependence,V}, + ocp::OptimalControlModel{<:TimeDependence, V}, type::Symbol, rg::OrdinalRange{<:Integer}, - lb::Union{ctVector,Nothing}, - ub::Union{ctVector,Nothing}, + lb::Union{ctVector, Nothing}, + ub::Union{ctVector, Nothing}, label::Symbol = CTBase.__constraint_label(), -) where {V<:VariableDependence} - +) where {V <: VariableDependence} constraint!(ocp, type, rg = rg, f = nothing, lb = lb, ub = ub, label = label) nothing # to force to return nothing - end function __constraint!( ocp::OptimalControlModel, type::Symbol, - lb::Union{ctVector,Nothing}, - ub::Union{ctVector,Nothing}, + lb::Union{ctVector, Nothing}, + ub::Union{ctVector, Nothing}, label::Symbol = CTBase.__constraint_label(), ) - constraint!(ocp, type, rg = nothing, f = nothing, lb = lb, ub = ub, label = label) nothing # to force to return nothing - end function __constraint!( - ocp::OptimalControlModel{T,V}, + ocp::OptimalControlModel{T, V}, type::Symbol, f::Function, - lb::Union{ctVector,Nothing}, - ub::Union{ctVector,Nothing}, + lb::Union{ctVector, Nothing}, + ub::Union{ctVector, Nothing}, label::Symbol = CTBase.__constraint_label(), -) where {T,V} - +) where {T, V} constraint!(ocp, type, rg = nothing, f = f, lb = lb, ub = ub, label = label) nothing # to force to return nothing end