From f1e0512c76193245908bad6dd240983d31e07989 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Thu, 29 Aug 2024 23:58:07 +0200 Subject: [PATCH 1/5] getters for tests and constraints, time --- bench/bench_nlp_constraints.jl | 8 +- bench/bench_nlp_constraints.md | 8 +- ext/plot.jl | 32 +- src/CTBase.jl | 2 +- src/init.jl | 6 +- src/onepass.jl | 4 +- src/optimal_control_model-getters.jl | 170 ++--- src/optimal_control_model-setters.jl | 56 +- src/optimal_control_solution-getters.jl | 6 +- src/optimal_control_solution-setters.jl | 24 +- src/print.jl | 38 +- test/runtests.jl | 3 - test/test_goddard.jl | 2 +- test/test_model.jl | 763 ++++++++++----------- test/test_onepass.jl | 868 ++++++++++++------------ test/test_print.jl | 38 +- test/utils.jl | 97 --- 17 files changed, 992 insertions(+), 1133 deletions(-) delete mode 100644 test/utils.jl diff --git a/bench/bench_nlp_constraints.jl b/bench/bench_nlp_constraints.jl index 4eb3da5e..0c61baad 100644 --- a/bench/bench_nlp_constraints.jl +++ b/bench/bench_nlp_constraints.jl @@ -197,8 +197,8 @@ function test_alloc_bad(ocp, N) begin v = Real[] - n = ocp.state_dimension - m = ocp.control_dimension + n = state_dimension(ocp) + m = control_dimension(ocp) times = LinRange(0, 1, N) XU = ones(N * (n + m)) @@ -456,8 +456,8 @@ function test_alloc_good(ocp, N) begin v = Real[] - n = ocp.state_dimension - m = ocp.control_dimension + n = state_dimension(ocp) + m = control_dimension(ocp) times = LinRange(0, 1, N) XU = zeros(N * (n + m)) diff --git a/bench/bench_nlp_constraints.md b/bench/bench_nlp_constraints.md index f0f90403..96dcfb0f 100644 --- a/bench/bench_nlp_constraints.md +++ b/bench/bench_nlp_constraints.md @@ -172,8 +172,8 @@ function test_alloc_bad(ocp, N) println(" declare variables") begin v = Real[] - n = ocp.state_dimension - m = ocp.control_dimension + n = state_dimension(ocp) + m = control_dimension(ocp) times = LinRange(0, 1, N) XU = ones(N * (n + m)) nξ = length(ξl) @@ -373,8 +373,8 @@ function test_alloc_good(ocp, N) println(" declare variables") begin v = Real[] - n = ocp.state_dimension - m = ocp.control_dimension + n = state_dimension(ocp) + m = control_dimension(ocp) times = LinRange(0, 1, N) XU = zeros(N * (n + m)) nξ = length(ξl) diff --git a/ext/plot.jl b/ext/plot.jl index 4076db2e..7c6d2ec6 100644 --- a/ext/plot.jl +++ b/ext/plot.jl @@ -228,8 +228,8 @@ function __initial_plot( ) # parameters - n = sol.state_dimension - m = sol.control_dimension + n = state_dimension(sol) + m = control_dimension(sol) if layout == :group @match control begin @@ -354,12 +354,12 @@ function Plots.plot!( end # - n = sol.state_dimension - m = sol.control_dimension - x_labels = sol.state_components_names - u_labels = sol.control_components_names - u_label = sol.control_name - t_label = sol.time_name + n = state_dimension(sol) + m = control_dimension(sol) + x_labels = state_components_names(sol) + u_labels = control_components_names(sol) + u_label = control_name(sol) + t_label = time_name(sol) # split series attributes series_attr = __keep_series_attributes(; kwargs...) @@ -548,12 +548,12 @@ function Plots.plot!( end function __size_plot(sol::OptimalControlSolution, control::Symbol) - n = sol.state_dimension - #m = sol.control_dimension + n = state_dimension(sol) + #m = control_dimension(sol) m = @match control begin - :components => sol.control_dimension + :components => control_dimension(sol) :norm => 1 - :all => sol.control_dimension + 1 + :all => control_dimension(sol) + 1 _ => throw(IncorrectArgument("No such choice for control. Use :components, :norm or :all")) end return (600, 140 * (n + m)) @@ -643,10 +643,10 @@ function recipe_label( end label = @match s begin - :state => sol.state_components_names[i] - :control => sol.control_components_names[i] - :costate => "p" * sol.state_components_names[i] - :control_norm => "‖" * sol.control_name * "‖" + :state => state_components_names(sol)[i] + :control => control_components_names(sol)[i] + :costate => "p" * state_components_names(sol)[i] + :control_norm => "‖" * control_name(sol) * "‖" _ => error("Internal error, no such choice for label") end end diff --git a/src/CTBase.jl b/src/CTBase.jl index 2bd74187..758a7752 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -261,7 +261,7 @@ export variable!, 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 nlp_constraints!, 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_boundary_constraints, dim_variable_constraints, dim_control_range diff --git a/src/init.jl b/src/init.jl index 0827c18f..7c572b45 100644 --- a/src/init.jl +++ b/src/init.jl @@ -239,9 +239,9 @@ mutable struct OptimalControlInit state = sol.state, control = sol.control, variable = sol.variable, - state_dim = sol.state_dimension, - control_dim = sol.control_dimension, - variable_dim = sol.variable_dimension, + state_dim = state_dimension(sol), + control_dim = control_dimension(sol), + variable_dim = variable_dimension(sol), ) end end diff --git a/src/onepass.jl b/src/onepass.jl index f840fd1e..753c46ff 100644 --- a/src/onepass.jl +++ b/src/onepass.jl @@ -248,7 +248,7 @@ p_time!(p, ocp, t, t0, tf; log = false) = begin end => :(time!($ocp; ind0 = $i, tf = $tf, name = $tt)) :($v1) && if (v1 == p.v) end => quote - ($ocp.variable_dimension ≠ 1) && throw( + (variable_dimension($ocp) ≠ 1) && throw( IncorrectArgument("variable must be of dimension one for a time"), ) time!($ocp; ind0 = 1, tf = $tf, name = $tt) @@ -260,7 +260,7 @@ p_time!(p, ocp, t, t0, tf; log = false) = begin end => :(time!($ocp; t0 = $t0, indf = $i, name = $tt)) :($v1) && if (v1 == p.v) end => quote - ($ocp.variable_dimension ≠ 1) && throw( + (variable_dimension($ocp) ≠ 1) && throw( IncorrectArgument("variable must be of dimension one for a time"), ) time!($ocp; t0 = $t0, indf = 1, name = $tt) diff --git a/src/optimal_control_model-getters.jl b/src/optimal_control_model-getters.jl index 24eda476..75817f3a 100644 --- a/src/optimal_control_model-getters.jl +++ b/src/optimal_control_model-getters.jl @@ -207,88 +207,12 @@ end """ $(TYPEDSIGNATURES) -Return `true` if the model is autonomous. -""" -is_autonomous(ocp::OptimalControlModel{Autonomous, <:VariableDependence}) = true -is_autonomous(ocp::OptimalControlModel{NonAutonomous, <:VariableDependence}) = false - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined as time dependent. -""" -is_time_dependent(ocp::OptimalControlModel) = !is_autonomous(ocp) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined as time independent. -""" -is_time_independent(ocp::OptimalControlModel) = !is_time_dependent(ocp) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the criterion type of `ocp` is `:min`. -""" -is_min(ocp::OptimalControlModel) = ocp.criterion == :min - -""" -$(TYPEDSIGNATURES) - -Return `true` if the criterion type of `ocp` is `:max`. -""" -is_max(ocp::OptimalControlModel) = !is_min(ocp) - -""" -$(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 +Return the constraints of the ocp or nothing. """ -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined as variable dependent. -""" -is_variable_dependent(ocp::OptimalControlModel) = !is_fixed(ocp) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined as variable independent. -""" -is_variable_independent(ocp::OptimalControlModel) = !is_variable_dependent(ocp) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined with free initial time. -""" -has_free_initial_time(ocp::OptimalControlModel) = (typeof(ocp.initial_time) == Index) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined with free final time. -""" -has_free_final_time(ocp::OptimalControlModel) = (typeof(ocp.final_time) == Index) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined with lagrange cost. -""" -has_lagrange_cost(ocp::OptimalControlModel) = !isnothing(ocp.lagrange) - -""" -$(TYPEDSIGNATURES) - -Return `true` if the model has been defined with mayer cost. -""" -has_mayer_cost(ocp::OptimalControlModel) = !isnothing(ocp.mayer) +function constraints(ocp::OptimalControlModel) + return ocp.constraints +end """ $(TYPEDSIGNATURES) @@ -587,3 +511,89 @@ Return the dynamics of the optimal control problem or `nothing`. """ dynamics(ocp::OptimalControlModel) = ocp.dynamics + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model is autonomous. +""" +is_autonomous(ocp::OptimalControlModel{Autonomous, <:VariableDependence}) = true +is_autonomous(ocp::OptimalControlModel{NonAutonomous, <:VariableDependence}) = false + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined as time dependent. +""" +is_time_dependent(ocp::OptimalControlModel) = !is_autonomous(ocp) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined as time independent. +""" +is_time_independent(ocp::OptimalControlModel) = !is_time_dependent(ocp) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the criterion type of `ocp` is `:min`. +""" +is_min(ocp::OptimalControlModel) = criterion(ocp) == :min + +""" +$(TYPEDSIGNATURES) + +Return `true` if the criterion type of `ocp` is `:max`. +""" +is_max(ocp::OptimalControlModel) = !is_min(ocp) + +""" +$(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 + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined as variable dependent. +""" +is_variable_dependent(ocp::OptimalControlModel) = !is_fixed(ocp) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined as variable independent. +""" +is_variable_independent(ocp::OptimalControlModel) = !is_variable_dependent(ocp) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined with free initial time. +""" +has_free_initial_time(ocp::OptimalControlModel) = (typeof(initial_time(ocp)) == Index) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined with free final time. +""" +has_free_final_time(ocp::OptimalControlModel) = (typeof(final_time(ocp)) == Index) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined with lagrange cost. +""" +has_lagrange_cost(ocp::OptimalControlModel) = !isnothing(lagrange(ocp)) + +""" +$(TYPEDSIGNATURES) + +Return `true` if the model has been defined with mayer cost. +""" +has_mayer_cost(ocp::OptimalControlModel) = !isnothing(mayer(ocp)) diff --git a/src/optimal_control_model-setters.jl b/src/optimal_control_model-setters.jl index c6ee1671..3eb5d96c 100644 --- a/src/optimal_control_model-setters.jl +++ b/src/optimal_control_model-setters.jl @@ -142,33 +142,33 @@ Define the state dimension and possibly the names of each component. ```@example julia> state!(ocp, 1) -julia> ocp.state_dimension +julia> state_dimension(ocp) 1 -julia> ocp.state_components_names +julia> state_components_names(ocp) ["x"] julia> state!(ocp, 1, "y") -julia> ocp.state_dimension +julia> state_dimension(ocp) 1 -julia> ocp.state_components_names +julia> state_components_names(ocp) ["y"] julia> state!(ocp, 2) -julia> ocp.state_dimension +julia> state_dimension(ocp) 2 -julia> ocp.state_components_names +julia> state_components_names(ocp) ["x₁", "x₂"] julia> state!(ocp, 2, :y) -julia> ocp.state_dimension +julia> state_dimension(ocp) 2 -julia> ocp.state_components_names +julia> state_components_names(ocp) ["y₁", "y₂"] julia> state!(ocp, 2, "y") -julia> ocp.state_dimension +julia> state_dimension(ocp) 2 -julia> ocp.state_components_names +julia> state_components_names(ocp) ["y₁", "y₂"] ``` """ @@ -226,33 +226,33 @@ Define the control dimension and possibly the names of each coordinate. ```@example julia> control!(ocp, 1) -julia> ocp.control_dimension +julia> control_dimension(ocp) 1 -julia> ocp.control_components_names +julia> control_components_names(ocp) ["u"] julia> control!(ocp, 1, "v") -julia> ocp.control_dimension +julia> control_dimension(ocp) 1 -julia> ocp.control_components_names +julia> control_components_names(ocp) ["v"] julia> control!(ocp, 2) -julia> ocp.control_dimension +julia> control_dimension(ocp) 2 -julia> ocp.control_components_names +julia> control_components_names(ocp) ["u₁", "u₂"] julia> control!(ocp, 2, :v) -julia> ocp.control_dimension +julia> control_dimension(ocp) 2 -julia> ocp.control_components_names +julia> control_components_names(ocp) ["v₁", "v₂"] julia> control!(ocp, 2, "v") -julia> ocp.control_dimension +julia> control_dimension(ocp) 2 -julia> ocp.control_components_names +julia> control_components_names(ocp) ["v₁", "v₂"] ``` """ @@ -342,7 +342,7 @@ function time!( (VT == NonFixed) && (!isnothing(ind0) || !isnothing(indf)) && __check_variable_set(ocp) # check if indices are in 1:q - q = ocp.variable_dimension + q = variable_dimension(ocp) !isnothing(ind0) && !(1 ≤ ind0 ≤ q) && throw(IncorrectArgument("the index of t0 variable must be contained in 1:$q")) @@ -414,7 +414,7 @@ function time!( ocp.initial_time = Index(ind0) ocp.final_time = tf ocp.time_name = name - ocp.initial_time_name = ocp.variable_components_names[ind0] + ocp.initial_time_name = variable_components_names(ocp)[ind0] ocp.final_time_name = tf isa Integer ? string(tf) : string(round(tf, digits = 2)) end (::Time, ::Nothing, ::Nothing, ::Integer) => begin # (t0, indf) @@ -422,14 +422,14 @@ function time!( ocp.final_time = Index(indf) ocp.time_name = name ocp.initial_time_name = t0 isa Integer ? string(t0) : string(round(t0, digits = 2)) - ocp.final_time_name = ocp.variable_components_names[indf] + ocp.final_time_name = variable_components_names(ocp)[indf] end (::Nothing, ::Integer, ::Nothing, ::Integer) => begin # (ind0, indf) ocp.initial_time = Index(ind0) ocp.final_time = Index(indf) ocp.time_name = name - ocp.initial_time_name = ocp.variable_components_names[ind0] - ocp.final_time_name = ocp.variable_components_names[indf] + ocp.initial_time_name = variable_components_names(ocp)[ind0] + ocp.final_time_name = variable_components_names(ocp)[indf] end _ => throw(IncorrectArgument("Provided arguments are inconsistent.")) end @@ -553,9 +553,9 @@ function constraint!( isnothing(ub) && (ub = Inf * (size(lb, 1) == 1 ? 1 : ones(eltype(lb), size(lb, 1)))) # dimensions - n = ocp.state_dimension - m = ocp.control_dimension - q = ocp.variable_dimension + n = state_dimension(ocp) + m = control_dimension(ocp) + q = variable_dimension(ocp) # range (typeof(rg) <: Int) && (rg = Index(rg)) diff --git a/src/optimal_control_solution-getters.jl b/src/optimal_control_solution-getters.jl index 3478ffe0..5411b2fe 100644 --- a/src/optimal_control_solution-getters.jl +++ b/src/optimal_control_solution-getters.jl @@ -44,7 +44,7 @@ $(TYPEDSIGNATURES) Return the names of the components of the control of the optimal control solution or `nothing`. """ -control_components_names(sol::OptimalControlSolution)::String = sol.control_components_names +control_components_names(sol::OptimalControlSolution) = sol.control_components_names """ $(TYPEDSIGNATURES) @@ -93,7 +93,7 @@ $(TYPEDSIGNATURES) Return the names of the components of the state of the optimal control solution or `nothing`. """ -state_components_names(sol::OptimalControlSolution)::String = sol.state_components_names +state_components_names(sol::OptimalControlSolution) = sol.state_components_names """ $(TYPEDSIGNATURES) @@ -142,7 +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) = sol.variable_components_names """ $(TYPEDSIGNATURES) diff --git a/src/optimal_control_solution-setters.jl b/src/optimal_control_solution-setters.jl index d356d956..d5ee8701 100644 --- a/src/optimal_control_solution-setters.jl +++ b/src/optimal_control_solution-setters.jl @@ -43,18 +43,18 @@ function __OptimalControlSolution( sol = OptimalControlSolution() # data from ocp - sol.initial_time_name = ocp.initial_time_name - sol.final_time_name = ocp.final_time_name - sol.time_name = ocp.time_name - sol.control_dimension = ocp.control_dimension - sol.control_components_names = ocp.control_components_names - sol.control_name = ocp.control_name - sol.state_dimension = ocp.state_dimension - sol.state_components_names = ocp.state_components_names - sol.state_name = ocp.state_name - sol.variable_dimension = ocp.variable_dimension - sol.variable_components_names = ocp.variable_components_names - sol.variable_name = ocp.variable_name + sol.initial_time_name = initial_time_name(ocp) + sol.final_time_name = final_time_name(ocp) + sol.time_name = time_name(ocp) + sol.control_dimension = control_dimension(ocp) + sol.control_components_names = control_components_names(ocp) + sol.control_name = control_name(ocp) + sol.state_dimension = state_dimension(ocp) + sol.state_components_names = state_components_names(ocp) + sol.state_name = state_name(ocp) + sol.variable_dimension = variable_dimension(ocp) + sol.variable_components_names = variable_components_names(ocp) + sol.variable_name = variable_name(ocp) # data from args sol.state = state diff --git a/src/print.jl b/src/print.jl index 0bec8326..c6012e4a 100644 --- a/src/print.jl +++ b/src/print.jl @@ -29,11 +29,11 @@ function Base.show( # some_printing = false - # print the code of the model if ocp.model_expression is not nothing - if !isnothing(ocp.model_expression) + # print the code of the model if model_expression(ocp) is not nothing + if !isnothing(model_expression(ocp)) # some checks - @assert hasproperty(ocp.model_expression, :head) + @assert hasproperty(model_expression(ocp), :head) # #println(io) @@ -53,7 +53,7 @@ function Base.show( # print the code tab = 4 - code = striplines(ocp.model_expression) + code = striplines(model_expression(ocp)) @match code.head begin :block => [__print(code.args[i], io, tab) for i ∈ eachindex(code.args)] _ => __print(code, io, tab) @@ -65,20 +65,20 @@ function Base.show( if __is_complete(ocp) # print the model if is is complete # dimensions - x_dim = ocp.state_dimension - u_dim = ocp.control_dimension - v_dim = is_variable_dependent(ocp) ? ocp.variable_dimension : -1 + x_dim = state_dimension(ocp) + u_dim = control_dimension(ocp) + v_dim = is_variable_dependent(ocp) ? variable_dimension(ocp) : -1 # names - t_name = ocp.time_name - t0_name = ocp.initial_time_name - tf_name = ocp.final_time_name - x_name = ocp.state_name - u_name = ocp.control_name - v_name = is_variable_dependent(ocp) ? ocp.variable_name : "" - xi_names = ocp.state_components_names - ui_names = ocp.control_components_names - vi_names = is_variable_dependent(ocp) ? ocp.variable_components_names : [] + t_name = time_name(ocp) + t0_name = initial_time_name(ocp) + tf_name = final_time_name(ocp) + x_name = state_name(ocp) + u_name = control_name(ocp) + v_name = is_variable_dependent(ocp) ? variable_name(ocp) : "" + xi_names = state_components_names(ocp) + ui_names = control_components_names(ocp) + vi_names = is_variable_dependent(ocp) ? variable_components_names(ocp) : [] # dependencies t_ = is_time_dependent(ocp) ? t_name * ", " : "" @@ -103,11 +103,11 @@ function Base.show( print(io, "J(" * x_name * ", " * u_name * _v * ") = ") # Mayer - !isnothing(ocp.mayer) && print(io, "g(" * bounds_args_names * ")") - (!isnothing(ocp.mayer) && !isnothing(ocp.lagrange)) && print(io, " + ") + !isnothing(mayer(ocp)) && print(io, "g(" * bounds_args_names * ")") + (!isnothing(mayer(ocp)) && !isnothing(lagrange(ocp))) && print(io, " + ") # Lagrange - if !isnothing(ocp.lagrange) + if !isnothing(lagrange(ocp)) println( io, '\u222B', diff --git a/test/runtests.jl b/test/runtests.jl index 27e0b56f..28f34bfd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,9 +14,6 @@ const has = CTBase.has const replace_call = CTBase.replace_call const constraint_type = CTBase.constraint_type -# -include("utils.jl") - # @testset verbose = true showtiming = true "Base" begin for name ∈ ( diff --git a/test/test_goddard.jl b/test/test_goddard.jl index f902775f..3bac4ad4 100644 --- a/test/test_goddard.jl +++ b/test/test_goddard.jl @@ -53,5 +53,5 @@ test_goddard() = begin x = x0 u = 2 tf = 1 - @test ocp.dynamics(x, u, tf) == F0(x) + u * F1(x) + @test dynamics(ocp)(x, u, tf) == F0(x) + u * F1(x) end diff --git a/test/test_model.jl b/test/test_model.jl index 39f14f53..7aa363e3 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -5,47 +5,47 @@ function test_model() # 30 55 185 ocp = Model(variable = false) @test_throws UnauthorizedCall variable!(ocp, 1) - @test_throws UnauthorizedCall __constraint!(ocp, :variable, 2:3, [0, 3], [0, 3]) - @test_throws UnauthorizedCall __constraint!(ocp, :variable, 0, 1) # the variable here is of dimension 1 - @test_throws UnauthorizedCall __constraint!(ocp, :variable, 1:2, [0, 0], [1, 2]) - @test_throws UnauthorizedCall __constraint!(ocp, :variable, [3, 0, 1], [3, 0, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :variable, rg = 2:3, lb = [0, 3], ub = [0, 3]) + @test_throws UnauthorizedCall constraint!(ocp, :variable, lb = 0, ub = 1) # the variable here is of dimension 1 + @test_throws UnauthorizedCall constraint!(ocp, :variable, rg = 1:2, lb = [0, 0], ub = [1, 2]) + @test_throws UnauthorizedCall constraint!(ocp, :variable, lb = [3, 0, 1], ub = [3, 0, 1]) ocp = Model(variable = true) variable!(ocp, 1) - @test ocp.variable_dimension == 1 + @test variable_dimension(ocp) == 1 ocp = Model(variable = true) variable!(ocp, 1, "vv") @test is_variable_dependent(ocp) - @test ocp.variable_dimension == 1 - @test ocp.variable_components_names == ["vv"] + @test variable_dimension(ocp) == 1 + @test variable_components_names(ocp) == ["vv"] ocp = Model(variable = true) variable!(ocp, 1, :vv) - @test ocp.variable_dimension == 1 - @test ocp.variable_components_names == ["vv"] + @test variable_dimension(ocp) == 1 + @test variable_components_names(ocp) == ["vv"] ocp = Model(variable = true) variable!(ocp, 2) - @test ocp.variable_dimension == 2 + @test variable_dimension(ocp) == 2 ocp = Model(variable = true) variable!(ocp, 2, "vv") - @test ocp.variable_dimension == 2 - @test ocp.variable_components_names == ["vv₁", "vv₂"] + @test variable_dimension(ocp) == 2 + @test variable_components_names(ocp) == ["vv₁", "vv₂"] ocp = Model(variable = true) variable!(ocp, 2, "uu", ["vv₁", "vv₂"]) - @test ocp.variable_dimension == 2 - @test ocp.variable_components_names == ["vv₁", "vv₂"] + @test variable_dimension(ocp) == 2 + @test variable_components_names(ocp) == ["vv₁", "vv₂"] ocp = Model(variable = true) @test_throws MethodError variable!(ocp, 2, ["vv1", "vv2"]) ocp = Model(variable = true) variable!(ocp, 2, :vv) - @test ocp.variable_dimension == 2 - @test ocp.variable_components_names == ["vv₁", "vv₂"] + @test variable_dimension(ocp) == 2 + @test variable_components_names(ocp) == ["vv₁", "vv₂"] end @testset "time, state and control set or not" begin @@ -53,7 +53,7 @@ function test_model() # 30 55 185 ocp = Model() i == 2 && begin - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) end i == 3 && begin state!(ocp, 2) @@ -62,11 +62,11 @@ function test_model() # 30 55 185 control!(ocp, 1) end i == 5 && begin - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) end i == 6 && begin - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) control!(ocp, 1) end i == 7 && begin @@ -75,181 +75,137 @@ 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, 2:3, [0, 0], [0, 0]) - @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(2), 0, 0) + @test_throws UnauthorizedCall constraint!(ocp, :initial, rg = 1:2:5, lb = [0, 0, 0], ub = [0, 0, 0]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, rg = 2:3, lb = [0, 0], ub = [0, 0]) + @test_throws UnauthorizedCall constraint!(ocp, :final, rg = 2, lb = 0, ub = 0) # constraint! 2 - @test_throws UnauthorizedCall __constraint!(ocp, :initial, [0, 0], [0, 0]) - @test_throws UnauthorizedCall __constraint!(ocp, :final, 2, 2) # if the state is of dimension 1 + @test_throws UnauthorizedCall constraint!(ocp, :initial, lb = [0, 0], ub = [0, 0]) + @test_throws UnauthorizedCall constraint!(ocp, :final, lb = 2, ub = 2) # if the state is of dimension 1 # constraint! 3 - @test_throws UnauthorizedCall __constraint!(ocp, :initial, 2:3, [0, 0], [1, 2]) - @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, rg = 2:3, lb = [0, 0], ub = [1, 2]) + @test_throws UnauthorizedCall constraint!(ocp, :final, rg = 1, lb = 0, ub = 2) + @test_throws UnauthorizedCall constraint!(ocp, :control, rg = 1, lb = 0, ub = 2) + @test_throws UnauthorizedCall constraint!(ocp, :state, rg = 2:3, lb = [0, 0], ub = [1, 2]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, rg = 1:2:5, lb = [0, 0, 0], ub = [1, 2, 1]) # constraint! 4 - @test_throws UnauthorizedCall __constraint!(ocp, :initial, [0, 0, 0], [1, 2, 1]) - @test_throws UnauthorizedCall __constraint!(ocp, :final, [0, 0, 0], [1, 2, 1]) - @test_throws UnauthorizedCall __constraint!(ocp, :control, [0, 0], [2, 3]) - @test_throws UnauthorizedCall __constraint!(ocp, :state, [0, 0, 0], [1, 2, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :initial, lb = [0, 0, 0], ub = [1, 2, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :final, lb = [0, 0, 0], ub = [1, 2, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :control, lb = [0, 0], ub = [2, 3]) + @test_throws UnauthorizedCall constraint!(ocp, :state, lb = [0, 0, 0], ub = [1, 2, 1]) # constraint! 5 # variable independent ocp - @test_throws UnauthorizedCall __constraint!( - ocp, - :boundary, - (x0, xf) -> x0[3] + xf[2], - 0, - 1, - ) + @test_throws UnauthorizedCall constraint!(ocp, :boundary, f = (x0, xf) -> x0[3] + xf[2], lb = 0, ub = 1) # variable dependent ocp - @test_throws UnauthorizedCall __constraint!( - ocp, - :boundary, - (x0, xf, v) -> x0[3] + xf[2] * v[1], - 0, - 1, - ) + @test_throws UnauthorizedCall constraint!(ocp, :boundary, f = (x0, xf, v) -> x0[3] + xf[2] * v[1], lb = 0, ub = 1) # time independent and variable independent ocp - @test_throws UnauthorizedCall __constraint!(ocp, :control, u -> 2u, 0, 1) - @test_throws UnauthorizedCall __constraint!( - ocp, - :state, - x -> x - 1, - [0, 0, 0], - [1, 2, 1], - ) - @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (x, u) -> x[1] - u, 0, 1) + @test_throws UnauthorizedCall constraint!(ocp, :control, f = u -> 2u, lb = 0, ub = 1) + @test_throws UnauthorizedCall constraint!(ocp, :state, f = x -> x - 1, lb = [0, 0, 0], ub = [1, 2, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :mixed, f = (x, u) -> x[1] - u, lb = 0, ub = 1) # time dependent and variable independent ocp - @test_throws UnauthorizedCall __constraint!(ocp, :control, (t, u) -> 2u, 0, 1) - @test_throws UnauthorizedCall __constraint!( - ocp, - :state, - (t, x) -> x - t, - [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, :control, f = (t, u) -> 2u, lb = 0, ub = 1) + @test_throws UnauthorizedCall constraint!(ocp, :state, f = (t, x) -> x - t, lb = [0, 0, 0], ub = [1, 2, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :mixed, f = (t, x, u) -> x[1] - u, lb = 0, ub = 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, - :state, - (x, v) -> x - v[1], - [0, 0, 0], - [1, 2, 1], - ) - @test_throws UnauthorizedCall __constraint!( - ocp, - :mixed, - (x, u, v) -> x[1] - v[2] * u, - 0, - 1, - ) + @test_throws UnauthorizedCall constraint!(ocp, :control, f = (u, v) -> 2u * v[1], lb = 0, ub = 1) + @test_throws UnauthorizedCall constraint!(ocp, :state, f = (x, v) -> x - v[1], lb = [0, 0, 0], ub = [1, 2, 1]) + @test_throws UnauthorizedCall constraint!(ocp, :mixed, f = (x, u, v) -> x[1] - v[2] * u, lb = 0, ub = 1) # 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, - :state, - (t, x, v) -> x - t * v[1], - [0, 0, 0], - [1, 2, 1], - ) - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!(ocp, :control, f = (t, u, v) -> 2u + v[2], lb = 0, ub = 1) + @test_throws UnauthorizedCall constraint!(ocp, :state, f = (t, x, v) -> x - t * v[1], lb = [0, 0, 0], ub = [1, 2, 1]) + @test_throws UnauthorizedCall constraint!( ocp, :mixed, - (t, x, u, v) -> x[1] * v[2] - u, - 0, - 1, + f = (t, x, u, v) -> x[1] * v[2] - u, + lb = 0, + ub = 1, ) # constraint! 6 # variable independent ocp - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!( ocp, :boundary, - (x0, xf) -> x0[3] + xf[2], - 0, - 0, + f = (x0, xf) -> x0[3] + xf[2], + lb = 0, + ub = 0, ) # variable dependent ocp - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!( ocp, :boundary, - (x0, xf, v) -> x0[3] + xf[2] * v[1], - 0, - 0, + f = (x0, xf, v) -> x0[3] + xf[2] * v[1], + lb = 0, + ub = 0, ) # time independent and variable independent ocp - @test_throws UnauthorizedCall __constraint!(ocp, :control, u -> 2u, 1, 1) - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!(ocp, :control, f = u -> 2u, lb = 1, ub = 1) + @test_throws UnauthorizedCall constraint!( ocp, :state, - x -> x - 1, - [0, 0, 0], - [0, 0, 0], + f = x -> x - 1, + lb = [0, 0, 0], + ub = [0, 0, 0], ) - @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (x, u) -> x[1] - u, 0, 0) + @test_throws UnauthorizedCall constraint!(ocp, :mixed, f = (x, u) -> x[1] - u, lb = 0, ub = 0) # time dependent and variable independent ocp - @test_throws UnauthorizedCall __constraint!(ocp, :control, (t, u) -> 2u, 1, 1) - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!(ocp, :control, f = (t, u) -> 2u, lb = 1, ub = 1) + @test_throws UnauthorizedCall constraint!( ocp, :state, - (t, x) -> x - t, - [0, 0, 0], - [0, 0, 0], + f = (t, x) -> x - t, + lb = [0, 0, 0], + ub = [0, 0, 0], ) - @test_throws UnauthorizedCall __constraint!(ocp, :mixed, (t, x, u) -> x[1] - u, 0, 0) + @test_throws UnauthorizedCall constraint!(ocp, :mixed, f = (t, x, u) -> x[1] - u, lb = 0, ub = 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, - :state, - (x, v) -> x - v[2], - [0, 0, 0], - [0, 0, 0], + @test_throws UnauthorizedCall constraint!(ocp, :control, f = (u, v) -> 2u * v[1], lb = 1, ub = 1) + @test_throws UnauthorizedCall constraint!(ocp, :state, + f = (x, v) -> x - v[2], + lb = [0, 0, 0], + ub = [0, 0, 0], ) - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!( ocp, :mixed, - (x, u) -> x[1] - u + v[1], - 0, - 0, + f = (x, u) -> x[1] - u + v[1], + lb = 0, + ub = 0, ) # time dependent and variable dependent ocp - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!( ocp, :control, - (t, u, v) -> 2u - t * v[2], - 1, - 1, + f = (t, u, v) -> 2u - t * v[2], + lb = 1, + ub = 1, ) - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!( ocp, :state, - (t, x, v) -> x - t + v[1], - [0, 0, 0], - [0, 0, 0], + f = (t, x, v) -> x - t + v[1], + lb = [0, 0, 0], + ub = [0, 0, 0], ) - @test_throws UnauthorizedCall __constraint!( + @test_throws UnauthorizedCall constraint!( ocp, :mixed, - (t, x, u, v) -> x[1] - u * v[1], - 0, - 0, + f = (t, x, u, v) -> x[1] - u * v[1], + lb = 0, + ub = 0, ) end end @@ -258,48 +214,48 @@ function test_model() # 30 55 185 ocp = Model(variable = true) @test !CTBase.__is_time_set(ocp) variable!(ocp, 1) - __time!(ocp, 0, Index(1)) + time!(ocp; t0 = 0, indf = 1) @test CTBase.__is_time_set(ocp) ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, Index(1), 1) + time!(ocp; ind0 = 1, tf = 1) @test CTBase.__is_time_set(ocp) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) @test CTBase.__is_time_set(ocp) ocp = Model() - @test_throws MethodError __time!(ocp, 0, Index(1)) - @test_throws MethodError __time!(ocp, Index(1), 1) + @test_throws MethodError time!(ocp; t0 = 0, indf = 1) + @test_throws MethodError time!(ocp; ind0 = 1, tf = 1) ocp = Model(variable = true) - @test_throws UnauthorizedCall __time!(ocp, 0, Index(1)) - @test_throws UnauthorizedCall __time!(ocp, Index(1), 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, indf = 1) + @test_throws UnauthorizedCall time!(ocp; ind0 = 1, tf = 1) ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, 0, Index(1)) - @test_throws UnauthorizedCall __time!(ocp, 0, Index(1)) - @test_throws UnauthorizedCall __time!(ocp, Index(1), 1) - @test_throws UnauthorizedCall __time!(ocp, 0, 1) + time!(ocp; t0 = 0, indf = 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, indf = 1) + @test_throws UnauthorizedCall time!(ocp; ind0 = 1, tf = 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, tf = 1) ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, Index(1), 1) - @test_throws UnauthorizedCall __time!(ocp, 0, Index(1)) - @test_throws UnauthorizedCall __time!(ocp, Index(1), 1) - @test_throws UnauthorizedCall __time!(ocp, 0, 1) + time!(ocp; ind0 = 1, tf = 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, indf = 1) + @test_throws UnauthorizedCall time!(ocp; ind0 = 1, tf = 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, tf = 1) ocp = Model(variable = true) - @test_throws UnauthorizedCall __time!(ocp, 0, Index(1)) - @test_throws UnauthorizedCall __time!(ocp, Index(1), 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, indf = 1) + @test_throws UnauthorizedCall time!(ocp; ind0 = 1, tf = 1) ocp = Model(variable = true) - __time!(ocp, 0, 1) - @test_throws UnauthorizedCall __time!(ocp, 0, Index(1)) - @test_throws UnauthorizedCall __time!(ocp, Index(1), 1) + time!(ocp; t0 = 0, tf = 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, indf = 1) + @test_throws UnauthorizedCall time!(ocp; ind0 = 1, tf = 1) end @testset "Index" begin @@ -446,147 +402,147 @@ function test_model() # 30 55 185 @testset "state!" begin ocp = Model() state!(ocp, 1) - @test ocp.state_dimension == 1 - @test ocp.state_components_names == ["x"] + @test state_dimension(ocp) == 1 + @test state_components_names(ocp) == ["x"] ocp = Model() state!(ocp, 1, "y") - @test ocp.state_dimension == 1 - @test ocp.state_components_names == ["y"] + @test state_dimension(ocp) == 1 + @test state_components_names(ocp) == ["y"] ocp = Model() state!(ocp, 2) - @test ocp.state_dimension == 2 - @test ocp.state_components_names == ["x₁", "x₂"] + @test state_dimension(ocp) == 2 + @test state_components_names(ocp) == ["x₁", "x₂"] ocp = Model() @test_throws MethodError state!(ocp, 2, ["y₁", "y₂"]) ocp = Model() state!(ocp, 2, :y) - @test ocp.state_dimension == 2 - @test ocp.state_components_names == ["y₁", "y₂"] + @test state_dimension(ocp) == 2 + @test state_components_names(ocp) == ["y₁", "y₂"] ocp = Model() state!(ocp, 2, "y") - @test ocp.state_dimension == 2 - @test ocp.state_components_names == ["y₁", "y₂"] + @test state_dimension(ocp) == 2 + @test state_components_names(ocp) == ["y₁", "y₂"] ocp = Model() state!(ocp, 2, "y", ["z₁", "z₂"]) - @test ocp.state_dimension == 2 - @test ocp.state_components_names == ["z₁", "z₂"] + @test state_dimension(ocp) == 2 + @test state_components_names(ocp) == ["z₁", "z₂"] end @testset "control!" begin ocp = Model() control!(ocp, 1) - @test ocp.control_dimension == 1 - @test ocp.control_components_names == ["u"] + @test control_dimension(ocp) == 1 + @test control_components_names(ocp) == ["u"] ocp = Model() control!(ocp, 1, "v") - @test ocp.control_dimension == 1 - @test ocp.control_components_names == ["v"] + @test control_dimension(ocp) == 1 + @test control_components_names(ocp) == ["v"] ocp = Model() control!(ocp, 2) - @test ocp.control_dimension == 2 - @test ocp.control_components_names == ["u₁", "u₂"] + @test control_dimension(ocp) == 2 + @test control_components_names(ocp) == ["u₁", "u₂"] ocp = Model() @test_throws MethodError control!(ocp, 2, ["v₁", "v₂"]) ocp = Model() control!(ocp, 2, :v) - @test ocp.control_dimension == 2 - @test ocp.control_components_names == ["v₁", "v₂"] + @test control_dimension(ocp) == 2 + @test control_components_names(ocp) == ["v₁", "v₂"] ocp = Model() control!(ocp, 2, "v") - @test ocp.control_dimension == 2 - @test ocp.control_components_names == ["v₁", "v₂"] + @test control_dimension(ocp) == 2 + @test control_components_names(ocp) == ["v₁", "v₂"] ocp = Model() control!(ocp, 2, "u", ["v₁", "v₂"]) - @test ocp.control_dimension == 2 - @test ocp.control_components_names == ["v₁", "v₂"] + @test control_dimension(ocp) == 2 + @test control_components_names(ocp) == ["v₁", "v₂"] end @testset "time!" begin # initial and final times ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) @test !CTBase.__is_initial_time_free(ocp) @test !CTBase.__is_final_time_free(ocp) - @test ocp.initial_time == 0 - @test ocp.final_time == 1 - @test ocp.time_name == "t" + @test initial_time(ocp) == 0 + @test final_time(ocp) == 1 + @test time_name(ocp) == "t" ocp = Model() - __time!(ocp, 0, 1, "s") - @test ocp.initial_time == 0 - @test ocp.final_time == 1 - @test ocp.time_name == "s" + time!(ocp; t0 = 0, tf = 1, name = "s") + @test initial_time(ocp) == 0 + @test final_time(ocp) == 1 + @test time_name(ocp) == "s" ocp = Model() - __time!(ocp, 0, 1, :s) - @test ocp.initial_time == 0 - @test ocp.final_time == 1 - @test ocp.time_name == "s" + time!(ocp; t0 = 0, tf = 1, name = :s) + @test initial_time(ocp) == 0 + @test final_time(ocp) == 1 + @test time_name(ocp) == "s" # initial time ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, 0, Index(1)) + time!(ocp; t0 = 0, indf = 1) @test !CTBase.__is_initial_time_free(ocp) @test CTBase.__is_final_time_free(ocp) - @test ocp.initial_time == 0 - @test ocp.final_time == Index(1) - @test ocp.time_name == "t" + @test initial_time(ocp) == 0 + @test final_time(ocp) == Index(1) + @test time_name(ocp) == "t" ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, 0, Index(1), "s") - @test ocp.initial_time == 0 - @test ocp.final_time == Index(1) - @test ocp.time_name == "s" + time!(ocp; t0 = 0, indf = 1, name = "s") + @test initial_time(ocp) == 0 + @test final_time(ocp) == Index(1) + @test time_name(ocp) == "s" ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, 0, Index(1), :s) - @test ocp.initial_time == 0 - @test ocp.final_time == Index(1) - @test ocp.time_name == "s" + time!(ocp; t0 = 0, indf = 1, name = :s) + @test initial_time(ocp) == 0 + @test final_time(ocp) == Index(1) + @test time_name(ocp) == "s" # final time ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, Index(1), 1) + time!(ocp; ind0 = 1, tf = 1) @test CTBase.__is_initial_time_free(ocp) @test !CTBase.__is_final_time_free(ocp) - @test ocp.initial_time == Index(1) - @test ocp.final_time == 1 - @test ocp.time_name == "t" + @test initial_time(ocp) == Index(1) + @test final_time(ocp) == 1 + @test time_name(ocp) == "t" ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, Index(1), 1, "s") - @test ocp.initial_time == Index(1) - @test ocp.final_time == 1 - @test ocp.time_name == "s" + time!(ocp; ind0 = 1, tf = 1, name = "s") + @test initial_time(ocp) == Index(1) + @test final_time(ocp) == 1 + @test time_name(ocp) == "s" ocp = Model(variable = true) variable!(ocp, 1) - __time!(ocp, Index(1), 1, :s) - @test ocp.initial_time == Index(1) - @test ocp.final_time == 1 - @test ocp.time_name == "s" + time!(ocp; ind0 = 1, tf = 1, name = :s) + @test initial_time(ocp) == Index(1) + @test final_time(ocp) == 1 + @test time_name(ocp) == "s" end @testset "is_min vs is_max" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) objective!(ocp, :mayer, (x0, xf) -> x0[1] + xf[2]) @@ -594,7 +550,7 @@ function test_model() # 30 55 185 @test !is_max(ocp) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) objective!(ocp, :mayer, (x0, xf) -> x0[1] + xf[2], :max) @@ -602,7 +558,7 @@ function test_model() # 30 55 185 @test !is_min(ocp) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) objective!(ocp, :lagrange, (x, u) -> 0.5u^2) @@ -610,7 +566,7 @@ function test_model() # 30 55 185 @test !is_max(ocp) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) objective!(ocp, :lagrange, (x, u) -> 0.5u^2, :max) @@ -618,7 +574,7 @@ function test_model() # 30 55 185 @test !is_min(ocp) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) objective!(ocp, :bolza, (x0, xf) -> x0[1] + xf[2], (x, u) -> x[1]^2 + u^2) # the control is of dimension 1 @@ -626,7 +582,7 @@ function test_model() # 30 55 185 @test !is_max(ocp) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) objective!(ocp, :bolza, (x0, xf) -> x0[1] + xf[2], (x, u) -> x[1]^2 + u^2, :max) # the control is of dimension 1 @@ -636,43 +592,43 @@ function test_model() # 30 55 185 @testset "constraint! 1" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - @test_throws IncorrectArgument __constraint!(ocp, :initial, [0, 1], [0, 1], :c0) - __constraint!(ocp, :initial, 0, 0, :c0) - __constraint!(ocp, :final, 1, 1, :cf) + @test_throws IncorrectArgument constraint!(ocp, :initial, lb = [0, 1], ub = [0, 1], label = :c0) + constraint!(ocp, :initial, lb = 0, ub = 0, label = :c0) + constraint!(ocp, :final, lb = 1, ub = 1, label = :cf) @test constraint(ocp, :c0)(12, ∅) == 12 @test constraint(ocp, :cf)(∅, 12) == 12 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) - __constraint!(ocp, :initial, [0, 1], [0, 1], :c0) - __constraint!(ocp, :final, [1, 2], [1, 2], :cf) + constraint!(ocp, :initial, lb = [0, 1], ub = [0, 1], label = :c0) + constraint!(ocp, :final, lb = [1, 2], ub = [1, 2], label = :cf) @test constraint(ocp, :c0)([12, 13], ∅) == [12, 13] @test constraint(ocp, :cf)(∅, [12, 13]) == [12, 13] # constraint already exists ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :initial, 0, 0, :c) - @test_throws UnauthorizedCall __constraint!(ocp, :final, 0, 0, :c) + constraint!(ocp, :initial, lb = 0, ub = 0, label = :c) + @test_throws UnauthorizedCall constraint!(ocp, :final, lb = 0, ub = 0, label = :c) end @testset "constraint! 2" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) x = 12 x0 = 0 xf = 1 - __constraint!(ocp, :initial, Index(1), x0, x0, :c0) - __constraint!(ocp, :final, Index(1), xf, xf, :cf) + constraint!(ocp, :initial, rg = 1, lb = x0, ub = x0, label = :c0) + constraint!(ocp, :final, rg = 1, lb = xf, ub = xf, label = :cf) @test constraint(ocp, :c0)(x, ∅) == x @test constraint(ocp, :cf)(∅, x) == x @@ -682,58 +638,58 @@ function test_model() # 30 55 185 @test constraint(ocp, :cff)(∅, x) == x ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) x = [12, 13] x0 = [0, 1] xf = [1, 2] - @test_throws IncorrectArgument __constraint!(ocp, :initial, Index(2), x0, x0, :c0) - @test_throws IncorrectArgument __constraint!(ocp, :final, Index(2), xf, xf, :cf) + @test_throws IncorrectArgument constraint!(ocp, :initial, rg = 2, lb = x0, ub = x0, label = :c0) + @test_throws IncorrectArgument constraint!(ocp, :final, rg = 2, lb = xf, ub = xf, label = :cf) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) x = [12, 13] x0 = [0, 1] xf = [1, 2] - __constraint!(ocp, :initial, 1:2, x0, x0, :c0) - __constraint!(ocp, :final, 1:2, xf, xf, :cf) + constraint!(ocp, :initial, rg = 1:2, lb = x0, ub = x0, label = :c0) + constraint!(ocp, :final, rg = 1:2, lb = xf, ub = xf, label = :cf) @test constraint(ocp, :c0)(x, ∅) == x[1:2] @test constraint(ocp, :cf)(∅, x) == x[1:2] # constraint already exists ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) - __constraint!(ocp, :initial, Index(1), 0, 0, :c) - @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(1), 0, 0, :c) + constraint!(ocp, :initial, rg = 1, lb = 0, ub = 0, label = :c) + @test_throws UnauthorizedCall constraint!(ocp, :final, rg = 1, lb = 0, ub = 0, label = :c) end @testset "constraint! 3" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :initial, 0, 1, :c0) - __constraint!(ocp, :final, 1, 2, :cf) - __constraint!(ocp, :control, 0, 1, :cu) - __constraint!(ocp, :state, 0, 1, :cs) + constraint!(ocp, :initial, lb = 0, ub = 1, label = :c0) + constraint!(ocp, :final, lb = 1, ub = 2, label = :cf) + constraint!(ocp, :control, lb = 0, ub = 1, label = :cu) + constraint!(ocp, :state, lb = 0, ub = 1, label = :cs) @test constraint(ocp, :c0)(12, ∅) == 12 @test constraint(ocp, :cf)(∅, 12) == 12 @test constraint(ocp, :cu)(12) == 12 @test constraint(ocp, :cs)(12) == 12 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) - __constraint!(ocp, :initial, [0, 1], [1, 2], :c0) - __constraint!(ocp, :final, [1, 2], [2, 3], :cf) - __constraint!(ocp, :control, [0, 1], [1, 2], :cu) - __constraint!(ocp, :state, [0, 1], [1, 2], :cs) + constraint!(ocp, :initial, lb = [0, 1], ub = [1, 2], label = :c0) + constraint!(ocp, :final, lb = [1, 2], ub = [2, 3], label = :cf) + constraint!(ocp, :control, lb = [0, 1], ub = [1, 2], label = :cu) + constraint!(ocp, :state, lb = [0, 1], ub = [1, 2], label = :cs) @test constraint(ocp, :c0)([12, 13], ∅) == [12, 13] @test constraint(ocp, :cf)(∅, [12, 13]) == [12, 13] @test constraint(ocp, :cu)([12, 13]) == [12, 13] @@ -741,44 +697,44 @@ function test_model() # 30 55 185 # constraint already exists ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :initial, 0, 1, :c) - @test_throws UnauthorizedCall __constraint!(ocp, :final, 0, 1, :c) + constraint!(ocp, :initial, lb = 0, ub = 1, label = :c) + @test_throws UnauthorizedCall constraint!(ocp, :final, lb = 0, ub = 1, label = :c) end @testset "constraint! 4" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :initial, Index(1), 0, 1, :c0) - __constraint!(ocp, :final, Index(1), 1, 2, :cf) - __constraint!(ocp, :control, Index(1), 0, 1, :cu) - __constraint!(ocp, :state, Index(1), 0, 1, :cs) + constraint!(ocp, :initial, rg = 1, lb = 0, ub = 1, label = :c0) + constraint!(ocp, :final, rg = 1, lb = 1, ub = 2, label = :cf) + constraint!(ocp, :control, rg = 1, lb = 0, ub = 1, label = :cu) + constraint!(ocp, :state, rg = 1, lb = 0, ub = 1, label = :cs) @test constraint(ocp, :c0)(12, ∅) == 12 @test constraint(ocp, :cf)(∅, 12) == 12 @test constraint(ocp, :cu)(12) == 12 @test constraint(ocp, :cs)(12) == 12 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 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, rg = 2, lb = [0, 1], ub = [1, 2], label = :c0) + @test_throws IncorrectArgument constraint!(ocp, :final, rg = 2, lb = [1, 2], ub = [2, 3], label = :cf) + @test_throws IncorrectArgument constraint!(ocp, :control, rg = 2, lb = [0, 1], ub = [1, 2], label = :cu) + @test_throws IncorrectArgument constraint!(ocp, :state, rg = 2, lb = [0, 1], ub = [1, 2], label = :cs) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) - __constraint!(ocp, :initial, 1:2, [0, 1], [1, 2], :c0) - __constraint!(ocp, :final, 1:2, [1, 2], [2, 3], :cf) - __constraint!(ocp, :control, 1:2, [0, 1], [1, 2], :cu) - __constraint!(ocp, :state, 1:2, [0, 1], [1, 2], :cs) + constraint!(ocp, :initial, rg = 1:2, lb = [0, 1], ub = [1, 2], label = :c0) + constraint!(ocp, :final, rg = 1:2, lb = [1, 2], ub = [2, 3], label = :cf) + constraint!(ocp, :control, rg = 1:2, lb = [0, 1], ub = [1, 2], label = :cu) + constraint!(ocp, :state, rg = 1:2, lb = [0, 1], ub = [1, 2], label = :cs) @test constraint(ocp, :c0)([12, 13], ∅) == [12, 13] @test constraint(ocp, :cf)(∅, [12, 13]) == [12, 13] @test constraint(ocp, :cu)([12, 13]) == [12, 13] @@ -786,55 +742,48 @@ function test_model() # 30 55 185 # constraint already exists ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) - __constraint!(ocp, :initial, Index(1), 0, 1, :c) - @test_throws UnauthorizedCall __constraint!(ocp, :final, Index(1), 0, 1, :c) + constraint!(ocp, :initial, rg = 1, lb = 0, ub = 1, label = :c) + @test_throws UnauthorizedCall constraint!(ocp, :final, rg = 1, lb = 0, ub = 1, label = :c) end @testset "constraint! 5" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :boundary, (x0, xf) -> x0 + xf, 0, 0, :cb) - __constraint!(ocp, :control, u -> u, 0, 0, :cu) - __constraint!(ocp, :state, x -> x, 0, 0, :cs) - __constraint!(ocp, :mixed, (x, u) -> x + u, 1, 1, :cm) + constraint!(ocp, :boundary, f = (x0, xf) -> x0 + xf, lb = 0, ub = 0, label = :cb) + constraint!(ocp, :control, f = u -> u, lb = 0, ub = 0, label = :cu) + constraint!(ocp, :state, f = x -> x, lb = 0, ub = 0, label = :cs) + constraint!(ocp, :mixed, f = (x, u) -> x + u, lb = 1, ub = 1, label = :cm) @test constraint(ocp, :cb)(12, 13) == 12 + 13 @test constraint(ocp, :cu)(12) == 12 @test constraint(ocp, :cs)(12) == 12 @test constraint(ocp, :cm)(12, 13) == 12 + 13 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) - __constraint!(ocp, :boundary, (x0, xf) -> x0[1] + xf[1], 0, 0, :cb) - __constraint!(ocp, :control, u -> u[1], 0, 0, :cu) - __constraint!(ocp, :state, x -> x[1], 0, 0, :cs) - __constraint!(ocp, :mixed, (x, u) -> x[1] + u[1], 1, 1, :cm) + constraint!(ocp, :boundary, f = (x0, xf) -> x0[1] + xf[1], lb = 0, ub = 0, label=:cb) + constraint!(ocp, :control, f = u -> u[1], lb = 0, ub = 0, label = :cu) + constraint!(ocp, :state, f = x -> x[1], lb = 0, ub = 0, label = :cs) + constraint!(ocp, :mixed, f = (x, u) -> x[1] + u[1], lb = 1, ub = 1, label=:cm) @test constraint(ocp, :cb)([13, 14], [16, 17]) == 13 + 16 @test constraint(ocp, :cu)([12, 13]) == 12 @test constraint(ocp, :cs)([12, 13]) == 12 @test constraint(ocp, :cm)([12, 13], [14, 15]) == 12 + 14 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 3) control!(ocp, 3) - __constraint!( - ocp, - :boundary, - (x0, xf) -> [x0[1] + xf[1], x0[2] + xf[2]], - [0, 0], - [0, 0], - :cb, - ) - __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, :boundary, f = (x0, xf) -> [x0[1] + xf[1], x0[2] + xf[2]], lb = [0, 0], ub = [0, 0], label = :cb) + constraint!(ocp, :control, f = u -> u[1:2], lb = [0, 0], ub = [0, 0], label = :cu) + constraint!(ocp, :state, f = x -> x[1:2], lb = [0, 0], ub = [0, 0], label = :cs) + constraint!(ocp, :mixed, f = (x, u) -> [x[1] + u[1], x[2] + u[2]], lb = [0, 0], ub = [0, 0], label = :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] @@ -842,55 +791,55 @@ function test_model() # 30 55 185 # constraint already exists ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) - __constraint!(ocp, :control, u -> u, 0, 1, :c) - @test_throws UnauthorizedCall __constraint!(ocp, :control, u -> u, 0, 1, :c) + constraint!(ocp, :control, f = u -> u, lb = 0, ub = 1, label = :c) + @test_throws UnauthorizedCall constraint!(ocp, :control, f = u -> u, lb = 0, ub = 1, label = :c) end @testset "constraint! 6" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :boundary, (x0, xf) -> x0 + xf, 0, 1, :cb) - __constraint!(ocp, :control, u -> u, 0, 1, :cu) - __constraint!(ocp, :state, x -> x, 0, 1, :cs) - __constraint!(ocp, :mixed, (x, u) -> x + u, 1, 1, :cm) + constraint!(ocp, :boundary, f = (x0, xf) -> x0 + xf, lb = 0, ub = 1, label = :cb) + constraint!(ocp, :control, f = u -> u, lb = 0, ub = 1, label = :cu) + constraint!(ocp, :state, f = x -> x, lb = 0, ub = 1, label = :cs) + constraint!(ocp, :mixed, f = (x, u) -> x + u, lb = 1, ub = 1, label = :cm) @test constraint(ocp, :cb)(12, 13) == 12 + 13 @test constraint(ocp, :cu)(12) == 12 @test constraint(ocp, :cs)(12) == 12 @test constraint(ocp, :cm)(12, 13) == 12 + 13 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) - __constraint!(ocp, :boundary, (x0, xf) -> x0[1] + xf[1], 0, 1, :cb) - __constraint!(ocp, :control, u -> u[1], 0, 1, :cu) - __constraint!(ocp, :state, x -> x[1], 0, 1, :cs) - __constraint!(ocp, :mixed, (x, u) -> x[1] + u[1], 1, 1, :cm) + constraint!(ocp, :boundary, f = (x0, xf) -> x0[1] + xf[1], lb = 0, ub = 1, label=:cb) + constraint!(ocp, :control, f = u -> u[1], lb = 0, ub = 1, label = :cu) + constraint!(ocp, :state, f = x -> x[1], lb = 0, ub = 1, label = :cs) + constraint!(ocp, :mixed, f = (x, u) -> x[1] + u[1], lb = 1, ub = 1, label=:cm) @test constraint(ocp, :cb)([13, 14], [16, 17]) == 13 + 16 @test constraint(ocp, :cu)([12, 13]) == 12 @test constraint(ocp, :cs)([12, 13]) == 12 @test constraint(ocp, :cm)([12, 13], [14, 15]) == 12 + 14 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) - __constraint!( + constraint!( ocp, :boundary, - (x0, xf) -> [x0[1] + xf[1], x0[2] + xf[2]], - [0, 0], - [1, 1], - :cb, + f = (x0, xf) -> [x0[1] + xf[1], x0[2] + xf[2]], + lb = [0, 0], + ub = [1, 1], + label = :cb, ) - __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, :control, f = u -> u[1:2], lb = [0, 0], ub = [1, 1], label = :cu) + constraint!(ocp, :state, f = x -> x[1:2], lb = [0, 0], ub = [1, 1], label = :cs) + constraint!(ocp, :mixed, f = (x, u) -> [x[1] + u[1], x[2] + u[2]], lb = [0, 0], ub = [1, 1], label = :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] @@ -904,15 +853,15 @@ function test_model() # 30 55 185 x0 = 7 xf = 8 ocp = Model(variable = true) - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) variable!(ocp, 4) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :variable, [0, 0, 0, 0], [1, 1, 1, 1], :eq1) - __constraint!(ocp, :variable, Index(1), 0, 1, :eq2) - __constraint!(ocp, :variable, 1:2, [0, 0], [1, 2], :eq3) - __constraint!(ocp, :variable, 1:2:4, [0, 0], [-1, 1], :eq4) - __constraint!(ocp, :variable, v -> v .^ 2, [0, 0, 0, 0], [1, 0, 1, 0], :eq5) + constraint!(ocp, :variable, lb = [0, 0, 0, 0], ub = [1, 1, 1, 1], label = :eq1) + constraint!(ocp, :variable, rg = 1, lb = 0, ub = 1, label = :eq2) + constraint!(ocp, :variable, rg = 1:2, lb = [0, 0], ub = [1, 2], label = :eq3) + constraint!(ocp, :variable, rg = 1:2:4, lb = [0, 0], ub = [-1, 1], label = :eq4) + constraint!(ocp, :variable, f = v -> v .^ 2, lb = [0, 0, 0, 0], ub = [1, 0, 1, 0], label = :eq5) @test constraint(ocp, :eq1)(v) == v @test constraint(ocp, :eq2)(v) == v[1] @test constraint(ocp, :eq3)(v) == v[1:2] @@ -927,19 +876,19 @@ function test_model() # 30 55 185 x0 = 7 xf = 8 ocp = Model(variable = true) - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) variable!(ocp, 4) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :boundary, (x0, xf, v) -> x0 + xf + v[1], 0, 1, :cb) - __constraint!(ocp, :control, (u, v) -> u + v[1], 0, 1, :cu) - __constraint!(ocp, :state, (x, v) -> x + v[1], 0, 1, :cs) - __constraint!(ocp, :mixed, (x, u, v) -> x + u + v[1], 1, 1, :cm) - __constraint!(ocp, :variable, [0, 0, 0, 0], [0, 0, 0, 0], :eq1) - __constraint!(ocp, :variable, Index(1), 0, 0, :eq2) - __constraint!(ocp, :variable, 1:2, [0, 0], [0, 0], :eq3) - __constraint!(ocp, :variable, 1:2:4, [0, 0], [0, 0], :eq4) - __constraint!(ocp, :variable, v -> v .^ 2, [0, 0, 0, 0], [0, 0, 0, 0], :eq5) + constraint!(ocp, :boundary, f = (x0, xf, v) -> x0 + xf + v[1], lb = 0, ub = 1, label = :cb) + constraint!(ocp, :control, f = (u, v) -> u + v[1], lb = 0, ub = 1, label=:cu) + constraint!(ocp, :state, f = (x, v) -> x + v[1], lb = 0, ub = 1, label=:cs) + constraint!(ocp, :mixed, f = (x, u, v) -> x + u + v[1], lb = 1, ub = 1, label = :cm) + constraint!(ocp, :variable, lb = [0, 0, 0, 0], ub = [0, 0, 0, 0], label = :eq1) + constraint!(ocp, :variable, rg = 1, lb = 0, ub = 0, label = :eq2) + constraint!(ocp, :variable, rg = 1:2, lb = [0, 0], ub = [0, 0], label = :eq3) + constraint!(ocp, :variable, rg = 1:2:4, lb = [0, 0], ub = [0, 0], label = :eq4) + constraint!(ocp, :variable, f = v -> v .^ 2, lb = [0, 0, 0, 0], ub = [0, 0, 0, 0], label = :eq5) @test constraint(ocp, :cb)(x0, xf, v) == x0 + xf + v[1] @test constraint(ocp, :cu)(u, v) == u + v[1] @test constraint(ocp, :cs)(x, v) == x + v[1] @@ -953,41 +902,41 @@ function test_model() # 30 55 185 @testset "constraint! 9" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) dynamics!(ocp, (x, u) -> x + u) - @test ocp.dynamics(1, 2) == 3 + @test dynamics(ocp)(1, 2) == 3 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) dynamics!(ocp, (x, u) -> x[1] + u[1]) - @test ocp.dynamics([1, 2], [3, 4]) == 4 + @test dynamics(ocp)([1, 2], [3, 4]) == 4 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) dynamics!(ocp, (x, u) -> [x[1] + u[1], x[2] + u[2]]) - @test ocp.dynamics([1, 2], [3, 4]) == [4, 6] + @test dynamics(ocp)([1, 2], [3, 4]) == [4, 6] ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) dynamics!(ocp, (x, u) -> [x[1] + u, x[2] + u]) - @test ocp.dynamics([1, 2], 3) == [4, 5] + @test dynamics(ocp)([1, 2], 3) == [4, 5] end @testset "constraint! 10" begin ocp = Model(autonomous = false) - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :initial, 0, 1, :c0) - __constraint!(ocp, :final, 1, 2, :cf) + constraint!(ocp, :initial, lb = 0, ub = 1, label = :c0) + constraint!(ocp, :final, lb = 1, ub = 2, label = :cf) @test constraint(ocp, :c0)(12, ∅) == 12 @test constraint(ocp, :cf)(∅, 12) == 12 end @@ -996,16 +945,16 @@ function test_model() # 30 55 185 dummy(u) = u^2 + u ocp = Model(variable = true) - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) variable!(ocp, 3) - __constraint!(ocp, :state, 0, 1, :c0) - __constraint!(ocp, :control, dummy, 1, 1, :c1) - __constraint!(ocp, :variable, 1:2:3, [-Inf, -Inf], [0, 0], :c2) + constraint!(ocp, :state, lb = 0, ub = 1, label = :c0) + constraint!(ocp, :control, f = dummy, lb = 1, ub = 1, label = :c1) + constraint!(ocp, :variable, rg = 1:2:3, lb = [-Inf, -Inf], ub = [0, 0], label = :c2) ocp_bis = Model(variable = true) - __time!(ocp_bis, 0, 1) + time!(ocp_bis; t0 = 0, tf = 1) state!(ocp_bis, 1) control!(ocp_bis, 1) variable!(ocp_bis, 3) @@ -1016,16 +965,16 @@ function test_model() # 30 55 185 @test ocp.constraints == ocp_bis.constraints ocp_ter = Model(variable = true) - __time!(ocp_ter, 0, 1) + time!(ocp_ter; t0 = 0, tf = 1) state!(ocp_ter, 3) control!(ocp_ter, 1) variable!(ocp_ter, 1) - __constraint!(ocp_ter, :variable, 1, 1, :c0) - __constraint!(ocp_ter, :control, dummy, 1, Inf, :c1) - __constraint!(ocp_ter, :state, 1:2:3, [0, 0], [0, 0], :c2) + constraint!(ocp_ter, :variable, lb = 1, ub = 1, label = :c0) + constraint!(ocp_ter, :control, f= dummy, lb = 1, ub = Inf, label = :c1) + constraint!(ocp_ter, :state, rg = 1:2:3, lb = [0, 0], ub = [0, 0], label = :c2) ocp_quad = Model(variable = true) - __time!(ocp_quad, 0, 1) + time!(ocp_quad; t0 = 0, tf = 1) state!(ocp_quad, 3) control!(ocp_quad, 1) variable!(ocp_quad, 1) @@ -1036,7 +985,7 @@ function test_model() # 30 55 185 @test ocp_ter.constraints == ocp_quad.constraints ocp_error = ocp_error = Model(variable = true) - __time!(ocp_error, 0, 1) + time!(ocp_error; t0 = 0, tf = 1) state!(ocp_error, 3) control!(ocp_error, 1) variable!(ocp_error, 1) @@ -1072,11 +1021,11 @@ function test_model() # 30 55 185 @testset "remove_constraint! and constraints_labels" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) - __constraint!(ocp, :boundary, (x0, xf) -> x0 + xf, 0, 1, :cb) - __constraint!(ocp, :control, u -> u, 0, 1, :cu) + constraint!(ocp, :boundary, f = (x0, xf) -> x0 + xf, lb = 0, ub = 1, label = :cb) + constraint!(ocp, :control, f = u -> u, lb = 0, ub = 1, label = :cu) k = constraints_labels(ocp) @test :cb ∈ k @test :cu ∈ k @@ -1088,17 +1037,17 @@ function test_model() # 30 55 185 @testset "nlp_constraints! without variable" begin ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 1) - __constraint!(ocp, :initial, Index(2), 10, 10, :ci) - __constraint!(ocp, :final, Index(1), 1, 1, :cf) - __constraint!(ocp, :control, 0, 1, :cu) - __constraint!(ocp, :state, [0, 1], [1, 2], :cs) - __constraint!(ocp, :boundary, (x0, xf) -> x0[2] + xf[2], 0, 1, :cb) - __constraint!(ocp, :control, u -> u, 0, 1, :cuu) - __constraint!(ocp, :state, x -> x, [0, 1], [1, 2], :css) - __constraint!(ocp, :mixed, (x, u) -> x[1] + u, 1, 1, :cm) + constraint!(ocp, :initial, rg = 2, lb = 10, ub = 10, label = :ci) + constraint!(ocp, :final, rg = 1, lb = 1, ub = 1, label = :cf) + constraint!(ocp, :control, lb = 0, ub = 1, label = :cu) + constraint!(ocp, :state, lb = [0, 1], ub = [1, 2], label = :cs) + constraint!(ocp, :boundary, f = (x0, xf) -> x0[2] + xf[2], lb = 0, ub = 1, label=:cb) + constraint!(ocp, :control, f = u -> u, lb = 0, ub = 1, label = :cuu) + constraint!(ocp, :state, f = x -> x, lb = [0, 1], ub = [1, 2], label = :css) + constraint!(ocp, :mixed, f = (x, u) -> x[1] + u, lb = 1, ub = 1, label=:cm) # dimensions (not set yet) @test dim_control_constraints(ocp) === nothing @@ -1172,22 +1121,22 @@ function test_model() # 30 55 185 @testset "nlp_constraints! with variable" begin ocp = Model(variable = true) - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) variable!(ocp, 4) state!(ocp, 2) control!(ocp, 1) - __constraint!(ocp, :initial, Index(2), 10, 10, :ci) - __constraint!(ocp, :final, Index(1), 1, 1, :cf) - __constraint!(ocp, :control, 0, 1, :cu) - __constraint!(ocp, :state, [0, 1], [1, 2], :cs) - __constraint!(ocp, :boundary, (x0, xf, v) -> x0[2] + xf[2] + v[1], 0, 1, :cb) - __constraint!(ocp, :control, (u, v) -> u + v[2], 0, 1, :cuu) - __constraint!(ocp, :state, (x, v) -> x + v[1:2], [0, 1], [1, 2], :css) - __constraint!(ocp, :mixed, (x, u, v) -> x[1] + u + v[2], 1, 1, :cm) - __constraint!(ocp, :variable, [0, 0, 0, 0], [5, 5, 5, 5], :cv1) - __constraint!(ocp, :variable, 1:2, [1, 2], [3, 4], :cv2) - __constraint!(ocp, :variable, Index(3), 2, 3, :cv3) - __constraint!(ocp, :variable, v -> v[3]^2, 0, 1, :cv4) + constraint!(ocp, :initial, rg = 2, lb = 10, ub = 10, label = :ci) + constraint!(ocp, :final, rg = 1, lb = 1, ub = 1, label = :cf) + constraint!(ocp, :control, lb = 0, ub = 1, label = :cu) + constraint!(ocp, :state, lb = [0, 1], ub = [1, 2], label = :cs) + constraint!(ocp, :boundary, f = (x0, xf, v) -> x0[2] + xf[2] + v[1], lb = 0, ub = 1, label = :cb) + constraint!(ocp, :control, f = (u, v) -> u + v[2], lb = 0, ub = 1, label = :cuu) + constraint!(ocp, :state, f = (x, v) -> x + v[1:2], lb = [0, 1], ub = [1, 2], label = :css) + constraint!(ocp, :mixed, f = (x, u, v) -> x[1] + u + v[2], lb = 1, ub = 1, label = :cm) + constraint!(ocp, :variable, lb = [0, 0, 0, 0], ub = [5, 5, 5, 5], label = :cv1) + constraint!(ocp, :variable, rg = 1:2, lb = [1, 2], ub = [3, 4], label = :cv2) + constraint!(ocp, :variable, rg = 3, lb = 2, ub = 3, label = :cv3) + constraint!(ocp, :variable, f = v -> v[3]^2, lb = 0, ub = 1, label = :cv4) # dimensions (not set yet) @test dim_control_constraints(ocp) === nothing @@ -1661,64 +1610,64 @@ function test_model() # 30 55 185 ) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) objective!(ocp, :lagrange, (x, u) -> 0.5u^2) - @test ocp.lagrange(1, 2) == 2 + @test lagrange(ocp)(1, 2) == 2 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) objective!(ocp, :lagrange, (x, u) -> 0.5u[1]^2) - @test ocp.lagrange([1, 2], [3, 4]) == 4.5 + @test lagrange(ocp)([1, 2], [3, 4]) == 4.5 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) objective!(ocp, :mayer, (x0, xf) -> 0.5x0^2) - @test ocp.mayer(2, 3) == 2 + @test mayer(ocp)(2, 3) == 2 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) objective!(ocp, :mayer, (x0, xf) -> 0.5x0[1]^2) - @test ocp.mayer([2, 3], [5, 6]) == 2 + @test mayer(ocp)([2, 3], [5, 6]) == 2 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) objective!(ocp, :bolza, (x0, xf) -> 0.5x0^2, (x, u) -> 0.5u^2) - @test ocp.mayer(2, 3) == 2 - @test ocp.lagrange(1, 2) == 2 + @test mayer(ocp)(2, 3) == 2 + @test lagrange(ocp)(1, 2) == 2 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) objective!(ocp, :bolza, (x0, xf) -> 0.5x0[1]^2, (x, u) -> 0.5u[1]^2) - @test ocp.mayer([2, 3], [5, 6]) == 2 - @test ocp.lagrange([1, 2], [3, 4]) == 4.5 + @test mayer(ocp)([2, 3], [5, 6]) == 2 + @test lagrange(ocp)([1, 2], [3, 4]) == 4.5 ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) objective!(ocp, :lagrange, (x, u) -> 0.5u[1]^2) - @test ocp.lagrange([1, 2], [3, 4]) == 4.5 - @test isnothing(ocp.mayer) + @test lagrange(ocp)([1, 2], [3, 4]) == 4.5 + @test isnothing(mayer(ocp)) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 2) control!(ocp, 2) objective!(ocp, :mayer, (x0, xf) -> 0.5x0[1]^2) - @test ocp.mayer([2, 3], [5, 6]) == 2 - @test isnothing(ocp.lagrange) + @test mayer(ocp)([2, 3], [5, 6]) == 2 + @test isnothing(lagrange(ocp)) end @testset "redeclarations" begin @@ -1727,8 +1676,8 @@ function test_model() # 30 55 185 @test_throws UnauthorizedCall variable!(ocp, 1) ocp = Model() - __time!(ocp, 0, 1) - @test_throws UnauthorizedCall __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) + @test_throws UnauthorizedCall time!(ocp; t0 = 0, tf = 1) ocp = Model() state!(ocp, 1) @@ -1739,28 +1688,28 @@ function test_model() # 30 55 185 @test_throws UnauthorizedCall control!(ocp, 1) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) dynamics!(ocp, (x, u) -> x + u) @test_throws UnauthorizedCall dynamics!(ocp, (x, u) -> x + u) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) objective!(ocp, :mayer, (x0, xf) -> x0 + xf) @test_throws UnauthorizedCall objective!(ocp, :mayer, (x0, xf) -> x0 + xf) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) objective!(ocp, :lagrange, (x, u) -> x + u) @test_throws UnauthorizedCall objective!(ocp, :lagrange, (x, u) -> x + u) ocp = Model() - __time!(ocp, 0, 1) + time!(ocp; t0 = 0, tf = 1) state!(ocp, 1) control!(ocp, 1) objective!(ocp, :bolza, (x0, xf) -> x0 + xf, (x, u) -> x + u) diff --git a/test/test_onepass.jl b/test/test_onepass.jl index 848441ad..f90bcbb4 100644 --- a/test/test_onepass.jl +++ b/test/test_onepass.jl @@ -12,8 +12,8 @@ function test_onepass() tf = λ₂ t ∈ [0, tf], time end - @test oo.initial_time == 0 - @test oo.final_time == Index(2) + @test initial_time(oo) == 0 + @test final_time(oo) == Index(2) a = 1 f(b) = begin # closure of a, local c, and @def in function @@ -31,7 +31,7 @@ function test_onepass() d = 4 x = 10 u = 20 - @test o.dynamics(x, u) == x + u + b + 3 + d + @test dynamics(o)(x, u) == x + u + b + 3 + d end @testset "log" begin @@ -42,8 +42,8 @@ function test_onepass() tf = λ₂ t ∈ [0, tf], time end true - @test o.initial_time == 0 - @test o.final_time == Index(2) + @test initial_time(o) == 0 + @test final_time(o) == Index(2) end # --------------------------------------------------------------- @@ -56,27 +56,27 @@ function test_onepass() u = (uu1, uu2, uu3) in R³, control v = (vv1, vv2) in R², variable end - @test o.state_components_names == ["y", "z"] - @test o.control_components_names == ["uu1", "uu2", "uu3"] - @test o.variable_components_names == ["vv1", "vv2"] + @test state_components_names(o) == ["y", "z"] + @test control_components_names(o) == ["uu1", "uu2", "uu3"] + @test variable_components_names(o) == ["vv1", "vv2"] @def o begin x = (y, z) ∈ R², state u = (uu1, uu2, uu3) ∈ R³, control v = (vv1, vv2) ∈ R², variable end - @test o.state_components_names == ["y", "z"] - @test o.control_components_names == ["uu1", "uu2", "uu3"] - @test o.variable_components_names == ["vv1", "vv2"] + @test state_components_names(o) == ["y", "z"] + @test control_components_names(o) == ["uu1", "uu2", "uu3"] + @test variable_components_names(o) == ["vv1", "vv2"] @def o begin x = [y, z] ∈ R², state u = [uu1, uu2, uu3] ∈ R³, control v = [vv1, vv2] ∈ R², variable end - @test o.state_components_names == ["y", "z"] - @test o.control_components_names == ["uu1", "uu2", "uu3"] - @test o.variable_components_names == ["vv1", "vv2"] + @test state_components_names(o) == ["y", "z"] + @test control_components_names(o) == ["uu1", "uu2", "uu3"] + @test variable_components_names(o) == ["vv1", "vv2"] @test_throws ParsingError @def o begin # a name must be provided (y, z) ∈ R², state @@ -118,8 +118,8 @@ function test_onepass() u = 3 @test constraint(o, :eq1)(x0, xf) == x0[1] @test constraint(o, Symbol("♡"))(x0, xf) == x0[2] - @test o.dynamics(x, u) == [x[2], (x[1] + 2x[2])^2] - @test o.lagrange(x, u) == u^2 + x[1] + @test dynamics(o)(x, u) == [x[2], (x[1] + 2x[2])^2] + @test lagrange(o)(x, u) == u^2 + x[1] @def o begin t ∈ [0, 1], time @@ -137,8 +137,8 @@ function test_onepass() u = 3 @test constraint(o, :eq1)(x0, xf) == x0[1] @test constraint(o, Symbol("♡"))(x0, xf) == x0[2] - @test o.dynamics(x, u) == [x[2], (x[1] + 2x[2])^2] - @test o.lagrange(x, u) == u^2 + x[1] + @test dynamics(o)(x, u) == [x[2], (x[1] + 2x[2])^2] + @test lagrange(o)(x, u) == u^2 + x[1] @def o begin t ∈ [0, 1], time @@ -158,8 +158,8 @@ function test_onepass() c = [u, 0] @test constraint(o, :eq1)(x0, xf) == x0[1] @test constraint(o, Symbol("♡"))(x0, xf) == x0[2] - @test o.dynamics(x, c) == [x[2], (x[1] + 2x[2])^2] - @test o.lagrange(x, c) == u^2 + x[1] + @test dynamics(o)(x, c) == [x[2], (x[1] + 2x[2])^2] + @test lagrange(o)(x, c) == u^2 + x[1] @def o begin t ∈ [0, 1], time @@ -167,11 +167,11 @@ function test_onepass() u = (u₁, v) ∈ R^2, control ẋ(t) == [x[1](t) + 2v(t), 2x[3](t), x[1](t) + v(t)] end - @test o.state_dimension == 3 - @test o.control_dimension == 2 + @test state_dimension(o) == 3 + @test control_dimension(o) == 2 x = [1, 2, 3] u = [-1, 2] - @test o.dynamics(x, u) == [x[1] + 2u[2], 2x[3], x[1] + u[2]] + @test dynamics(o)(x, u) == [x[1] + 2u[2], 2x[3], x[1] + u[2]] t0 = 0.0 tf = 0.1 @@ -184,13 +184,13 @@ function test_onepass() a = x₃ end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_name == "u" - @test ocp.control_dimension == 3 - @test ocp.state_name == "x" - @test ocp.state_dimension == 3 + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_name(ocp) == "u" + @test control_dimension(ocp) == 3 + @test state_name(ocp) == "x" + @test state_dimension(ocp) == 3 end # --------------------------------------------------------------- @@ -203,36 +203,36 @@ function test_onepass() tf = λ₂ t ∈ [0, tf], time end - @test o.initial_time == 0 - @test o.final_time == Index(2) + @test initial_time(o) == 0 + @test final_time(o) == Index(2) @def o begin λ = (λ₁, tf) ∈ R^2, variable t ∈ [0, tf], time end - @test o.initial_time == 0 - @test o.final_time == Index(2) + @test initial_time(o) == 0 + @test final_time(o) == Index(2) @def o begin t0 ∈ R, variable t ∈ [t0, 1], time end - @test o.initial_time == Index(1) - @test o.final_time == 1 + @test initial_time(o) == Index(1) + @test final_time(o) == 1 @def o begin tf ∈ R, variable t ∈ [0, tf], time end - @test o.initial_time == 0 - @test o.final_time == Index(1) + @test initial_time(o) == 0 + @test final_time(o) == Index(1) @def o begin v ∈ R², variable s ∈ [v[1], v[2]], time end - @test o.initial_time == Index(1) - @test o.final_time == Index(2) + @test initial_time(o) == Index(1) + @test final_time(o) == Index(2) @def o begin v ∈ R², variable @@ -240,8 +240,8 @@ function test_onepass() sf = v₂ s ∈ [s0, sf], time end - @test o.initial_time == Index(1) - @test o.final_time == Index(2) + @test initial_time(o) == Index(1) + @test final_time(o) == Index(2) @test_throws IncorrectArgument @def o begin t0 ∈ R², variable @@ -275,8 +275,8 @@ function test_onepass() a ∈ R, variable end @test ocp isa OptimalControlModel - @test ocp.variable_dimension == 1 - @test ocp.variable_name == "a" + @test variable_dimension(ocp) == 1 + @test variable_name(ocp) == "a" t0 = 0.0 tf = 0.1 @@ -285,8 +285,8 @@ function test_onepass() a ∈ R³, variable end @test ocp isa OptimalControlModel - @test ocp.variable_dimension == 3 - @test ocp.variable_name == "a" + @test variable_dimension(ocp) == 3 + @test variable_name(ocp) == "a" end # --------------------------------------------------------------- @@ -296,16 +296,16 @@ function test_onepass() t0 = 0 @def o t ∈ [t0, t0 + 4], time - @test o.initial_time == t0 - @test o.final_time == t0 + 4 + @test initial_time(o) == t0 + @test final_time(o) == t0 + 4 @test_throws ParsingError @def o t ∈ 1 @def ocp t ∈ [0.0, 1.0], time @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == 0.0 - @test ocp.final_time == 1.0 + @test time_name(ocp) == "t" + @test initial_time(ocp) == 0.0 + @test final_time(ocp) == 1.0 t0 = 3.0 @def ocp begin @@ -313,9 +313,9 @@ function test_onepass() t ∈ [t0, tf], time end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == Index(1) + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == Index(1) tf = 3.14 @def ocp begin @@ -323,9 +323,9 @@ function test_onepass() t ∈ [t0, tf], time end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == Index(1) - @test ocp.final_time == tf + @test time_name(ocp) == "t" + @test initial_time(ocp) == Index(1) + @test final_time(ocp) == tf end # --------------------------------------------------------------- @@ -337,8 +337,8 @@ function test_onepass() x ∈ R, state u ∈ R, control end - @test o.state_dimension == 1 - @test o.control_dimension == 1 + @test state_dimension(o) == 1 + @test control_dimension(o) == 1 # state t0 = 1.0 @@ -348,11 +348,11 @@ function test_onepass() u ∈ R, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == 1 - @test ocp.state_name == "u" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == 1 + @test state_name(ocp) == "u" t0 = 2.0 tf = 2.1 @@ -361,11 +361,11 @@ function test_onepass() v ∈ R^4, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == 4 - @test ocp.state_name == "v" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == 4 + @test state_name(ocp) == "v" t0 = 3.0 tf = 3.1 @@ -374,11 +374,11 @@ function test_onepass() w ∈ R^3, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == 3 - @test ocp.state_name == "w" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == 3 + @test state_name(ocp) == "w" t0 = 4.0 tf = 4.1 @@ -387,11 +387,11 @@ function test_onepass() a ∈ R, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == 1 - @test ocp.state_name == "a" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == 1 + @test state_name(ocp) == "a" t0 = 5.0 tf = 5.1 @@ -400,11 +400,11 @@ function test_onepass() b ∈ R¹, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == 1 - @test ocp.state_name == "b" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == 1 + @test state_name(ocp) == "b" t0 = 6.0 tf = 6.1 @@ -413,11 +413,11 @@ function test_onepass() u ∈ R⁹, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == 9 - @test ocp.state_name == "u" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == 9 + @test state_name(ocp) == "u" n = 3 t0 = 7.0 @@ -427,11 +427,11 @@ function test_onepass() u ∈ R^n, state end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.state_dimension == n - @test ocp.state_name == "u" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test state_dimension(ocp) == n + @test state_name(ocp) == "u" # control t0 = 1.0 @@ -441,11 +441,11 @@ function test_onepass() u ∈ R, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == 1 - @test ocp.control_name == "u" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == 1 + @test control_name(ocp) == "u" t0 = 2.0 tf = 2.1 @@ -454,11 +454,11 @@ function test_onepass() v ∈ R^4, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == 4 - @test ocp.control_name == "v" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == 4 + @test control_name(ocp) == "v" t0 = 3.0 tf = 3.1 @@ -467,11 +467,11 @@ function test_onepass() w ∈ R^3, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == 3 - @test ocp.control_name == "w" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == 3 + @test control_name(ocp) == "w" t0 = 4.0 tf = 4.1 @@ -480,11 +480,11 @@ function test_onepass() a ∈ R, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == 1 - @test ocp.control_name == "a" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == 1 + @test control_name(ocp) == "a" t0 = 5.0 tf = 5.1 @@ -493,11 +493,11 @@ function test_onepass() b ∈ R¹, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == 1 - @test ocp.control_name == "b" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == 1 + @test control_name(ocp) == "b" t0 = 6.0 tf = 6.1 @@ -506,11 +506,11 @@ function test_onepass() u ∈ R⁹, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == 9 - @test ocp.control_name == "u" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == 9 + @test control_name(ocp) == "u" n = 3 t0 = 7.0 @@ -520,11 +520,11 @@ function test_onepass() u ∈ R^n, control end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_dimension == n - @test ocp.control_name == "u" + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_dimension(ocp) == n + @test control_name(ocp) == "u" end # --------------------------------------------------------------- @@ -538,11 +538,11 @@ function test_onepass() u ∈ R^2, control ẋ(t) == [x[1](t) + 2u[2](t), 2x[3](t), x[1](t) + u[2](t)] end - @test o.state_dimension == 3 - @test o.control_dimension == 2 + @test state_dimension(o) == 3 + @test control_dimension(o) == 2 x = [1, 2, 3] u = [-1, 2] - @test o.dynamics(x, u) == [x[1] + 2u[2], 2x[3], x[1] + u[2]] + @test dynamics(o)(x, u) == [x[1] + 2u[2], 2x[3], x[1] + u[2]] @def o begin z ∈ R², variable s ∈ [0, z₁], time @@ -556,7 +556,7 @@ function test_onepass() z = [5, 6] y = [1, 2, 3, 4] w = 9 - @test o.dynamics(y, w, z) == [y[1], y[3]^2 + w + z[1], 0, 0] + @test dynamics(o)(y, w, z) == [y[1], y[3]^2 + w + z[1], 0, 0] @def o begin z ∈ R², variable @@ -571,7 +571,7 @@ function test_onepass() z = [5, 6] y = [1, 2, 3, 4] w = 9 - @test_throws MethodError o.dynamics(y, w, z) + @test_throws MethodError dynamics(o)(y, w, z) @def o begin z ∈ R², variable @@ -588,7 +588,7 @@ function test_onepass() y0 = y yf = 3y0 ww = 19 - @test o.dynamics(y, ww, z) == [y[1] + ww^2 + y[4]^3 + z[2], y[3]^2, 0, 0] + @test dynamics(o)(y, ww, z) == [y[1] + ww^2 + y[4]^3 + z[2], y[3]^2, 0, 0] @def o begin z ∈ R², variable @@ -606,8 +606,8 @@ function test_onepass() y0 = y yf = 3y0 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] + @test_throws MethodError dynamics(o)(y, w, z) + @test mayer(o)(y0, yf, z) == y0[1] + y0[4]^3 + z[2] + yf[2] end # --------------------------------------------------------------- @@ -721,8 +721,8 @@ function test_onepass() u = 3 @test constraint(o, :eq1)(x0, xf) == x0[1] @test constraint(o, Symbol("♡"))(x0, xf) == x0[2] - @test o.dynamics(x, u) == [x[2], (x[1] + 2x[2])^2] - @test o.lagrange(x, u) == u^2 + x[1] + @test dynamics(o)(x, u) == [x[2], (x[1] + 2x[2])^2] + @test lagrange(o)(x, u) == u^2 + x[1] @def o begin t ∈ [0, 1], time @@ -742,8 +742,8 @@ function test_onepass() u = 3 @test constraint(o, :eq1)(x0, xf) == x0[1] @test constraint(o, Symbol("♡"))(x0, xf) == x0[2] - @test o.dynamics(x, u) == [x[2], (x[1] + 2x[2])^2] - @test o.lagrange(x, u) == u^2 + x[1] + @test dynamics(o)(x, u) == [x[2], (x[1] + 2x[2])^2] + @test lagrange(o)(x, u) == u^2 + x[1] @def o begin z ∈ R², variable @@ -765,8 +765,8 @@ function test_onepass() z = [4, 5] @test constraint(o, :eq1)(x0, xf, z) == x0[1] @test constraint(o, Symbol("♡"))(x0, xf, z) == x0[2] - @test o.dynamics(x, u, z) == [x[2], (x[1] + 2x[2])^2 + z[1]] - @test o.lagrange(x, u, z) == u^2 + z[2] * x[1] + @test dynamics(o)(x, u, z) == [x[2], (x[1] + 2x[2])^2 + z[1]] + @test lagrange(o)(x, u, z) == u^2 + z[2] * x[1] @def o begin t ∈ [0, 1], time @@ -785,8 +785,8 @@ function test_onepass() u = 3 @test constraint(o, :eq1)(x0, xf) == x0[1]^2 + xf[2] @test constraint(o, Symbol("♡"))(x0, xf) == x0[2] - @test o.dynamics(x, u) == [x[2], x[1]^2] - @test o.lagrange(x, u) == u^2 + x[1] + @test dynamics(o)(x, u) == [x[2], x[1]^2] + @test lagrange(o)(x, u) == u^2 + x[1] @def o begin z ∈ R, variable @@ -807,8 +807,8 @@ function test_onepass() z = 4 @test constraint(o, :eq1)(x0, xf, z) == x0[1] - z @test constraint(o, Symbol("♡"))(x0, xf, z) == x0[2] - @test o.dynamics(x, u, z) == [x[2], x[1]^2 + z] - @test o.lagrange(x, u, z) == u^2 + z * x[1] + @test dynamics(o)(x, u, z) == [x[2], x[1]^2 + z] + @test lagrange(o)(x, u, z) == u^2 + z * x[1] @def o begin z ∈ R, variable @@ -831,8 +831,8 @@ function test_onepass() @test constraint(o, :eq1)(x0, xf, z) == x0[1] - z @test constraint(o, :eq2)(x0, xf, z) == xf[2]^2 @test constraint(o, Symbol("♡"))(x0, xf, z) == x0 - @test o.dynamics(x, u, z) == [x[2], x[1]^2 + z] - @test o.lagrange(x, u, z) == u^2 + z * x[1] + @test dynamics(o)(x, u, z) == [x[2], x[1]^2 + z] + @test lagrange(o)(x, u, z) == u^2 + z * x[1] @def o begin z ∈ R, variable @@ -855,14 +855,14 @@ function test_onepass() @test constraint(o, :eq1)(x0, xf, z) == x0[1] - z @test constraint(o, :eq2)(x0, xf, z) == xf[2]^2 @test constraint(o, :eq3)(x0, xf, z) == x0 - @test o.dynamics(x, u, z) == [x[2], x[1]^2 + z] - @test o.lagrange(x, u, z) == u^2 + z * x[1] - @test o.constraints[:eq1][3] == 0 - @test o.constraints[:eq1][4] == 1 - @test o.constraints[:eq2][3] == 0 - @test o.constraints[:eq2][4] == 1 - @test o.constraints[:eq3][3] == [0, 0] - @test o.constraints[:eq3][4] == [1, 1] + @test dynamics(o)(x, u, z) == [x[2], x[1]^2 + z] + @test lagrange(o)(x, u, z) == u^2 + z * x[1] + @test constraints(o)[:eq1][3] == 0 + @test constraints(o)[:eq1][4] == 1 + @test constraints(o)[:eq2][3] == 0 + @test constraints(o)[:eq2][4] == 1 + @test constraints(o)[:eq3][3] == [0, 0] + @test constraints(o)[:eq3][4] == [1, 1] @def o begin v ∈ R², variable @@ -946,36 +946,36 @@ function test_onepass() v ≤ 0, (15) end - @test o.constraints[:eq1][3] == -Inf - @test o.constraints[:eq2][3] == -Inf - @test o.constraints[:eq3][3] == -Inf - @test o.constraints[:eq4][3] == -Inf - @test o.constraints[:eq5][3] == -Inf - @test o.constraints[:eq6][3] == -Inf - @test o.constraints[:eq7][3] == -Inf - @test o.constraints[:eq8][3] == -Inf - @test o.constraints[:eq9][3] == -Inf - @test o.constraints[:eq10][3] == -Inf - @test o.constraints[:eq11][3] == -Inf - @test o.constraints[:eq12][3] == -Inf - @test o.constraints[:eq13][3] == -Inf - @test o.constraints[:eq14][3] == -Inf - @test o.constraints[:eq15][3] == -Inf - @test o.constraints[:eq1][4] == 0 - @test o.constraints[:eq2][4] == 0 - @test o.constraints[:eq3][4] == 0 - @test o.constraints[:eq4][4] == 0 - @test o.constraints[:eq5][4] == 0 - @test o.constraints[:eq6][4] == 0 - @test o.constraints[:eq7][4] == 0 - @test o.constraints[:eq8][4] == 0 - @test o.constraints[:eq9][4] == 0 - @test o.constraints[:eq10][4] == 0 - @test o.constraints[:eq11][4] == 0 - @test o.constraints[:eq12][4] == 0 - @test o.constraints[:eq13][4] == 0 - @test o.constraints[:eq14][4] == 0 - @test o.constraints[:eq15][4] == 0 + @test constraints(o)[:eq1][3] == -Inf + @test constraints(o)[:eq2][3] == -Inf + @test constraints(o)[:eq3][3] == -Inf + @test constraints(o)[:eq4][3] == -Inf + @test constraints(o)[:eq5][3] == -Inf + @test constraints(o)[:eq6][3] == -Inf + @test constraints(o)[:eq7][3] == -Inf + @test constraints(o)[:eq8][3] == -Inf + @test constraints(o)[:eq9][3] == -Inf + @test constraints(o)[:eq10][3] == -Inf + @test constraints(o)[:eq11][3] == -Inf + @test constraints(o)[:eq12][3] == -Inf + @test constraints(o)[:eq13][3] == -Inf + @test constraints(o)[:eq14][3] == -Inf + @test constraints(o)[:eq15][3] == -Inf + @test constraints(o)[:eq1][4] == 0 + @test constraints(o)[:eq2][4] == 0 + @test constraints(o)[:eq3][4] == 0 + @test constraints(o)[:eq4][4] == 0 + @test constraints(o)[:eq5][4] == 0 + @test constraints(o)[:eq6][4] == 0 + @test constraints(o)[:eq7][4] == 0 + @test constraints(o)[:eq8][4] == 0 + @test constraints(o)[:eq9][4] == 0 + @test constraints(o)[:eq10][4] == 0 + @test constraints(o)[:eq11][4] == 0 + @test constraints(o)[:eq12][4] == 0 + @test constraints(o)[:eq13][4] == 0 + @test constraints(o)[:eq14][4] == 0 + @test constraints(o)[:eq15][4] == 0 @def o begin v ∈ R, variable @@ -1014,36 +1014,36 @@ function test_onepass() v ≥ 0, (15) end - @test o.constraints[:eq1][3] == 0 - @test o.constraints[:eq2][3] == 0 - @test o.constraints[:eq3][3] == 0 - @test o.constraints[:eq4][3] == 0 - @test o.constraints[:eq5][3] == 0 - @test o.constraints[:eq6][3] == 0 - @test o.constraints[:eq7][3] == 0 - @test o.constraints[:eq8][3] == 0 - @test o.constraints[:eq9][3] == 0 - @test o.constraints[:eq10][3] == 0 - @test o.constraints[:eq11][3] == 0 - @test o.constraints[:eq12][3] == 0 - @test o.constraints[:eq13][3] == 0 - @test o.constraints[:eq14][3] == 0 - @test o.constraints[:eq15][3] == 0 - @test o.constraints[:eq1][4] == Inf - @test o.constraints[:eq2][4] == Inf - @test o.constraints[:eq3][4] == Inf - @test o.constraints[:eq4][4] == Inf - @test o.constraints[:eq5][4] == Inf - @test o.constraints[:eq6][4] == Inf - @test o.constraints[:eq7][4] == Inf - @test o.constraints[:eq8][4] == Inf - @test o.constraints[:eq9][4] == Inf - @test o.constraints[:eq10][4] == Inf - @test o.constraints[:eq11][4] == Inf - @test o.constraints[:eq12][4] == Inf - @test o.constraints[:eq13][4] == Inf - @test o.constraints[:eq14][4] == Inf - @test o.constraints[:eq15][4] == Inf + @test constraints(o)[:eq1][3] == 0 + @test constraints(o)[:eq2][3] == 0 + @test constraints(o)[:eq3][3] == 0 + @test constraints(o)[:eq4][3] == 0 + @test constraints(o)[:eq5][3] == 0 + @test constraints(o)[:eq6][3] == 0 + @test constraints(o)[:eq7][3] == 0 + @test constraints(o)[:eq8][3] == 0 + @test constraints(o)[:eq9][3] == 0 + @test constraints(o)[:eq10][3] == 0 + @test constraints(o)[:eq11][3] == 0 + @test constraints(o)[:eq12][3] == 0 + @test constraints(o)[:eq13][3] == 0 + @test constraints(o)[:eq14][3] == 0 + @test constraints(o)[:eq15][3] == 0 + @test constraints(o)[:eq1][4] == Inf + @test constraints(o)[:eq2][4] == Inf + @test constraints(o)[:eq3][4] == Inf + @test constraints(o)[:eq4][4] == Inf + @test constraints(o)[:eq5][4] == Inf + @test constraints(o)[:eq6][4] == Inf + @test constraints(o)[:eq7][4] == Inf + @test constraints(o)[:eq8][4] == Inf + @test constraints(o)[:eq9][4] == Inf + @test constraints(o)[:eq10][4] == Inf + @test constraints(o)[:eq11][4] == Inf + @test constraints(o)[:eq12][4] == Inf + @test constraints(o)[:eq13][4] == Inf + @test constraints(o)[:eq14][4] == Inf + @test constraints(o)[:eq15][4] == Inf @def o begin v ∈ R^2, variable @@ -1072,26 +1072,26 @@ function test_onepass() [v₁^2, 0] ≤ [0, 0], (10) end - @test o.constraints[:eq1][3] == -[Inf, Inf] - @test o.constraints[:eq2][3] == -[Inf, Inf] - @test o.constraints[:eq3][3] == -[Inf, Inf] - @test o.constraints[:eq4][3] == -[Inf, Inf] - @test o.constraints[:eq5][3] == -[Inf, Inf] - @test o.constraints[:eq6][3] == -[Inf, Inf] - @test o.constraints[:eq7][3] == -[Inf, Inf] - @test o.constraints[:eq8][3] == -[Inf, Inf] - @test o.constraints[:eq9][3] == -[Inf, Inf] - @test o.constraints[:eq10][3] == -[Inf, Inf] - @test o.constraints[:eq1][4] == [0, 0] - @test o.constraints[:eq2][4] == [0, 0] - @test o.constraints[:eq3][4] == [0, 0] - @test o.constraints[:eq4][4] == [0, 0] - @test o.constraints[:eq5][4] == [0, 0] - @test o.constraints[:eq6][4] == [0, 0] - @test o.constraints[:eq7][4] == [0, 0] - @test o.constraints[:eq8][4] == [0, 0] - @test o.constraints[:eq9][4] == [0, 0] - @test o.constraints[:eq10][4] == [0, 0] + @test constraints(o)[:eq1][3] == -[Inf, Inf] + @test constraints(o)[:eq2][3] == -[Inf, Inf] + @test constraints(o)[:eq3][3] == -[Inf, Inf] + @test constraints(o)[:eq4][3] == -[Inf, Inf] + @test constraints(o)[:eq5][3] == -[Inf, Inf] + @test constraints(o)[:eq6][3] == -[Inf, Inf] + @test constraints(o)[:eq7][3] == -[Inf, Inf] + @test constraints(o)[:eq8][3] == -[Inf, Inf] + @test constraints(o)[:eq9][3] == -[Inf, Inf] + @test constraints(o)[:eq10][3] == -[Inf, Inf] + @test constraints(o)[:eq1][4] == [0, 0] + @test constraints(o)[:eq2][4] == [0, 0] + @test constraints(o)[:eq3][4] == [0, 0] + @test constraints(o)[:eq4][4] == [0, 0] + @test constraints(o)[:eq5][4] == [0, 0] + @test constraints(o)[:eq6][4] == [0, 0] + @test constraints(o)[:eq7][4] == [0, 0] + @test constraints(o)[:eq8][4] == [0, 0] + @test constraints(o)[:eq9][4] == [0, 0] + @test constraints(o)[:eq10][4] == [0, 0] @def o begin v ∈ R^2, variable @@ -1120,26 +1120,26 @@ function test_onepass() [v₁^2, 0] ≥ [0, 0], (10) end - @test o.constraints[:eq1][4] == [Inf, Inf] - @test o.constraints[:eq2][4] == [Inf, Inf] - @test o.constraints[:eq3][4] == [Inf, Inf] - @test o.constraints[:eq4][4] == [Inf, Inf] - @test o.constraints[:eq5][4] == [Inf, Inf] - @test o.constraints[:eq6][4] == [Inf, Inf] - @test o.constraints[:eq7][4] == [Inf, Inf] - @test o.constraints[:eq8][4] == [Inf, Inf] - @test o.constraints[:eq9][4] == [Inf, Inf] - @test o.constraints[:eq10][4] == [Inf, Inf] - @test o.constraints[:eq1][3] == [0, 0] - @test o.constraints[:eq2][3] == [0, 0] - @test o.constraints[:eq3][3] == [0, 0] - @test o.constraints[:eq4][3] == [0, 0] - @test o.constraints[:eq5][3] == [0, 0] - @test o.constraints[:eq6][3] == [0, 0] - @test o.constraints[:eq7][3] == [0, 0] - @test o.constraints[:eq8][3] == [0, 0] - @test o.constraints[:eq9][3] == [0, 0] - @test o.constraints[:eq10][3] == [0, 0] + @test constraints(o)[:eq1][4] == [Inf, Inf] + @test constraints(o)[:eq2][4] == [Inf, Inf] + @test constraints(o)[:eq3][4] == [Inf, Inf] + @test constraints(o)[:eq4][4] == [Inf, Inf] + @test constraints(o)[:eq5][4] == [Inf, Inf] + @test constraints(o)[:eq6][4] == [Inf, Inf] + @test constraints(o)[:eq7][4] == [Inf, Inf] + @test constraints(o)[:eq8][4] == [Inf, Inf] + @test constraints(o)[:eq9][4] == [Inf, Inf] + @test constraints(o)[:eq10][4] == [Inf, Inf] + @test constraints(o)[:eq1][3] == [0, 0] + @test constraints(o)[:eq2][3] == [0, 0] + @test constraints(o)[:eq3][3] == [0, 0] + @test constraints(o)[:eq4][3] == [0, 0] + @test constraints(o)[:eq5][3] == [0, 0] + @test constraints(o)[:eq6][3] == [0, 0] + @test constraints(o)[:eq7][3] == [0, 0] + @test constraints(o)[:eq8][3] == [0, 0] + @test constraints(o)[:eq9][3] == [0, 0] + @test constraints(o)[:eq10][3] == [0, 0] t0 = 9.0 tf = 9.1 @@ -1163,13 +1163,13 @@ function test_onepass() mf ≤ m(t) ≤ m0, (5) end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_name == "u" - @test ocp.control_dimension == 2 - @test ocp.state_name == "x" - @test ocp.state_dimension == 3 + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_name(ocp) == "u" + @test control_dimension(ocp) == 2 + @test state_name(ocp) == "x" + @test state_dimension(ocp) == 3 @def ocp begin t ∈ [t0, tf], time @@ -1185,13 +1185,13 @@ function test_onepass() mf ≤ m(t) ≤ m0 end @test ocp isa OptimalControlModel - @test ocp.time_name == "t" - @test ocp.initial_time == t0 - @test ocp.final_time == tf - @test ocp.control_name == "u" - @test ocp.control_dimension == 2 - @test ocp.state_name == "x" - @test ocp.state_dimension == 3 + @test time_name(ocp) == "t" + @test initial_time(ocp) == t0 + @test final_time(ocp) == tf + @test control_name(ocp) == "u" + @test control_dimension(ocp) == 2 + @test state_name(ocp) == "x" + @test state_dimension(ocp) == 3 # dyslexic definition: t -> u -> x -> t u0 = 9.0 @@ -1215,13 +1215,13 @@ function test_onepass() bf ≤ b(u) ≤ b0 end @test ocp isa OptimalControlModel - @test ocp.time_name == "u" - @test ocp.initial_time == u0 - @test ocp.final_time == uf - @test ocp.control_name == "x" - @test ocp.control_dimension == 2 - @test ocp.state_name == "t" - @test ocp.state_dimension == 3 + @test time_name(ocp) == "u" + @test initial_time(ocp) == u0 + @test final_time(ocp) == uf + @test control_name(ocp) == "x" + @test control_dimension(ocp) == 2 + @test state_name(ocp) == "t" + @test state_dimension(ocp) == 3 # # test all constraints on @def macro @@ -1255,10 +1255,10 @@ function test_onepass() y0_b ≤ x[2:3](t0) ≤ y0_u end @test ocp1 isa OptimalControlModel - @test ocp1.state_dimension == n - @test ocp1.control_dimension == n - @test ocp1.initial_time == t0 - @test ocp1.final_time == tf + @test state_dimension(ocp1) == n + @test control_dimension(ocp1) == n + @test initial_time(ocp1) == t0 + @test final_time(ocp1) == tf t0 = 0.1 tf = 1.1 @@ -1276,10 +1276,10 @@ function test_onepass() [1, 2] ≤ x[2:3](t0) ≤ [3, 4], initial_5 end @test ocp2 isa OptimalControlModel - @test ocp2.state_dimension == n - @test ocp2.control_dimension == n - @test ocp2.initial_time == t0 - @test ocp2.final_time == tf + @test state_dimension(ocp2) == n + @test control_dimension(ocp2) == n + @test initial_time(ocp2) == t0 + @test final_time(ocp2) == tf # all used variables must be defined before each test xf = 11.11 * ones(4) @@ -1305,10 +1305,10 @@ function test_onepass() yf_b ≤ x[2:3](tf) ≤ yf_u end @test ocp3 isa OptimalControlModel - @test ocp3.state_dimension == n - @test ocp3.control_dimension == n - @test ocp3.initial_time == t0 - @test ocp3.final_time == tf + @test state_dimension(ocp3) == n + @test control_dimension(ocp3) == n + @test initial_time(ocp3) == t0 + @test final_time(ocp3) == tf t0 = 0.3 tf = 1.3 @@ -1332,10 +1332,10 @@ function test_onepass() yf_b ≤ x[2:3](tf) ≤ yf_u, final_5 end @test ocp4 isa OptimalControlModel - @test ocp4.state_dimension == n - @test ocp4.control_dimension == n - @test ocp4.initial_time == t0 - @test ocp4.final_time == tf + @test state_dimension(ocp4) == n + @test control_dimension(ocp4) == n + @test initial_time(ocp4) == t0 + @test final_time(ocp4) == tf # === boundary t0 = 0.4 @@ -1353,10 +1353,10 @@ function test_onepass() 1 ≤ x[2](tf)^2 ≤ 2 end @test ocp5 isa OptimalControlModel - @test ocp5.state_dimension == n - @test ocp5.control_dimension == n - @test ocp5.initial_time == t0 - @test ocp5.final_time == tf + @test state_dimension(ocp5) == n + @test control_dimension(ocp5) == n + @test initial_time(ocp5) == t0 + @test final_time(ocp5) == tf t0 = 0.5 tf = 1.5 @@ -1373,10 +1373,10 @@ function test_onepass() 1 ≤ x[2](tf)^2 ≤ 2, boundary_6 end @test ocp6 isa OptimalControlModel - @test ocp6.state_dimension == n - @test ocp6.control_dimension == n - @test ocp6.initial_time == t0 - @test ocp6.final_time == tf + @test state_dimension(ocp6) == n + @test control_dimension(ocp6) == n + @test initial_time(ocp6) == t0 + @test final_time(ocp6) == tf # define more variables u_b = 1.0 @@ -1401,10 +1401,10 @@ function test_onepass() 1 ≤ u[1](t)^2 + u[2](t)^2 ≤ 2 end @test ocp7 isa OptimalControlModel - @test ocp7.state_dimension == n - @test ocp7.control_dimension == n - @test ocp7.initial_time == t0 - @test ocp7.final_time == tf + @test state_dimension(ocp7) == n + @test control_dimension(ocp7) == n + @test initial_time(ocp7) == t0 + @test final_time(ocp7) == tf t0 = 0.7 tf = 1.7 @@ -1426,10 +1426,10 @@ function test_onepass() 1 ≤ u[1](t)^2 + u[2](t)^2 ≤ 2, control_8 end @test ocp8 isa OptimalControlModel - @test ocp8.state_dimension == n - @test ocp8.control_dimension == n - @test ocp8.initial_time == t0 - @test ocp8.final_time == tf + @test state_dimension(ocp8) == n + @test control_dimension(ocp8) == n + @test initial_time(ocp8) == t0 + @test final_time(ocp8) == tf # more vars x_b = 10.0 @@ -1457,10 +1457,10 @@ function test_onepass() [-1, 1] ≤ x[1:2](t) + x[3:4](t) ≤ [0, 2] end @test ocp9 isa OptimalControlModel - @test ocp9.state_dimension == n - @test ocp9.control_dimension == n - @test ocp9.initial_time == t0 - @test ocp9.final_time == tf + @test state_dimension(ocp9) == n + @test control_dimension(ocp9) == n + @test initial_time(ocp9) == t0 + @test final_time(ocp9) == tf t0 = 0.9 tf = 1.9 @@ -1479,10 +1479,10 @@ function test_onepass() [-1, 1] ≤ x[1:2](t) + x[3:4](t) ≤ [0, 2], state_8 end @test ocp10 isa OptimalControlModel - @test ocp10.state_dimension == n - @test ocp10.control_dimension == n - @test ocp10.initial_time == t0 - @test ocp10.final_time == tf + @test state_dimension(ocp10) == n + @test control_dimension(ocp10) == n + @test initial_time(ocp10) == t0 + @test final_time(ocp10) == tf # === mixed t0 = 0.111 @@ -1496,10 +1496,10 @@ function test_onepass() [-1, 1] ≤ u[2](t) * x[1:2](t) ≤ [0, 2] end @test ocp11 isa OptimalControlModel - @test ocp11.state_dimension == n - @test ocp11.control_dimension == n - @test ocp11.initial_time == t0 - @test ocp11.final_time == tf + @test state_dimension(ocp11) == n + @test control_dimension(ocp11) == n + @test initial_time(ocp11) == t0 + @test final_time(ocp11) == tf @def ocp12 begin t ∈ [t0, tf], time @@ -1509,10 +1509,10 @@ function test_onepass() [-1, 1] ≤ u[2](t) * x[1:2](t) ≤ [0, 2], mixed_2 end @test ocp12 isa OptimalControlModel - @test ocp12.state_dimension == n - @test ocp12.control_dimension == n - @test ocp12.initial_time == t0 - @test ocp12.final_time == tf + @test state_dimension(ocp12) == n + @test control_dimension(ocp12) == n + @test initial_time(ocp12) == t0 + @test final_time(ocp12) == tf # === dynamics @@ -1525,10 +1525,10 @@ function test_onepass() ẋ(t) == 2x(t) + u(t)^2 end @test ocp13 isa OptimalControlModel - @test ocp13.state_dimension == 1 - @test ocp13.control_dimension == 1 - @test ocp13.initial_time == t0 - @test ocp13.final_time == tf + @test state_dimension(ocp13) == 1 + @test control_dimension(ocp13) == 1 + @test initial_time(ocp13) == t0 + @test final_time(ocp13) == tf # some syntax (even parseable) are not allowed # this is the actual exhaustive list @@ -1574,9 +1574,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :min t0 = 0 tf = 1 @@ -1602,9 +1602,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == -0.5u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == -0.5u^2 + @test criterion(o) == :min t0 = 0 tf = 1 @@ -1630,9 +1630,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :min t0 = 0 tf = 1 @@ -1658,9 +1658,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :min t0 = 0 tf = 1 @@ -1686,9 +1686,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == -0.5u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == -0.5u^2 + @test criterion(o) == :min t0 = 0 tf = 1 @@ -1714,9 +1714,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == (-0.5 + tf) * u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == (-0.5 + tf) * u^2 + @test criterion(o) == :min t0 = 0 tf = 1 @@ -1768,9 +1768,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :max + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :max t0 = 0 tf = 1 @@ -1796,9 +1796,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == -0.5u^2 - @test o.criterion == :max + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == -0.5u^2 + @test criterion(o) == :max t0 = 0 tf = 1 @@ -1824,9 +1824,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :max + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :max t0 = 0 tf = 1 @@ -1852,9 +1852,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :max + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :max t0 = 0 tf = 1 @@ -1880,9 +1880,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == -0.5u^2 - @test o.criterion == :max + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == -0.5u^2 + @test criterion(o) == :max # ----------------------------------- t0 = 0.0 @@ -1948,9 +1948,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 3xf - @test o.lagrange(x, u) == x + u - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 3xf + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -1962,9 +1962,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == x + u - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -1976,9 +1976,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == 2(x + u) - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == 2(x + u) + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -1990,9 +1990,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == -(x + u) - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == -(x + u) + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -2004,9 +2004,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == -2(x + u) - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == -2(x + u) + @test criterion(o) == :min @test_throws ParsingError @def o begin t ∈ [0, 1], time @@ -2035,9 +2035,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 5xf - @test o.lagrange(x, u) == x + u - @test o.criterion == :max + @test mayer(o)(x0, xf) == x0 + 5xf + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :max @def o begin t ∈ [0, 1], time @@ -2049,9 +2049,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == 2(x + u) - @test o.criterion == :max + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == 2(x + u) + @test criterion(o) == :max @def o begin t ∈ [0, 1], time @@ -2063,9 +2063,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == -(x + u) - @test o.criterion == :max + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == -(x + u) + @test criterion(o) == :max @def o begin t ∈ [0, 1], time @@ -2077,9 +2077,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == -2(x + u) - @test o.criterion == :max + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == -2(x + u) + @test criterion(o) == :max @test_throws ParsingError @def o begin t ∈ [0, 1], time @@ -2108,9 +2108,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == x + u - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -2122,9 +2122,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == 2(x + u) - @test o.criterion == :min + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == 2(x + u) + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -2136,9 +2136,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == -(x0 + 2xf) - @test o.lagrange(x, u) == x + u - @test o.criterion == :min + @test mayer(o)(x0, xf) == -(x0 + 2xf) + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :min @def o begin t ∈ [0, 1], time @@ -2150,9 +2150,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == -(x0 + 2xf) - @test o.lagrange(x, u) == 2(x + u) - @test o.criterion == :min + @test mayer(o)(x0, xf) == -(x0 + 2xf) + @test lagrange(o)(x, u) == 2(x + u) + @test criterion(o) == :min @test_throws ParsingError @def o begin t ∈ [0, 1], time @@ -2181,9 +2181,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == x + u - @test o.criterion == :max + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :max @def o begin t ∈ [0, 1], time @@ -2195,9 +2195,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == x0 + 2xf - @test o.lagrange(x, u) == 2(x + u) - @test o.criterion == :max + @test mayer(o)(x0, xf) == x0 + 2xf + @test lagrange(o)(x, u) == 2(x + u) + @test criterion(o) == :max @def o begin t ∈ [0, 1], time @@ -2209,9 +2209,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == -(x0 + 2xf) - @test o.lagrange(x, u) == x + u - @test o.criterion == :max + @test mayer(o)(x0, xf) == -(x0 + 2xf) + @test lagrange(o)(x, u) == x + u + @test criterion(o) == :max @def o begin t ∈ [0, 1], time @@ -2223,9 +2223,9 @@ function test_onepass() u = 2 x0 = 3 xf = 4 - @test o.mayer(x0, xf) == -(x0 + 2xf) - @test o.lagrange(x, u) == 2(x + u) - @test o.criterion == :max + @test mayer(o)(x0, xf) == -(x0 + 2xf) + @test lagrange(o)(x, u) == 2(x + u) + @test criterion(o) == :max @test_throws ParsingError @def o begin t ∈ [0, 1], time @@ -2258,7 +2258,7 @@ function test_onepass() y0 = [1, 2, 3, 4] yf = 2 * [1, 2, 3, 4] @test is_min(o) - @test o.mayer(y0, yf) == y0[3] + yf[4] + @test mayer(o)(y0, yf) == y0[3] + yf[4] @def o begin s ∈ [0, 1], time @@ -2271,7 +2271,7 @@ function test_onepass() y0 = [1, 2, 3, 4] yf = 2 * [1, 2, 3, 4] @test is_max(o) - @test o.mayer(y0, yf) == y0[3] + yf[4] + @test mayer(o)(y0, yf) == y0[3] + yf[4] @def o begin z ∈ R^2, variable @@ -2286,7 +2286,7 @@ function test_onepass() y0 = [1, 2, 3, 4] yf = 2 * [1, 2, 3, 4] @test is_min(o) - @test o.mayer(y0, yf, z) == y0[3] + yf[4] + z[2] + @test mayer(o)(y0, yf, z) == y0[3] + yf[4] + z[2] @def o begin z ∈ R², variable @@ -2304,8 +2304,8 @@ function test_onepass() y0 = y yf = 3y0 w = 7 - @test o.dynamics(y, w, z) == [y[1] + w^2 + y[4]^3 + z[2], y[3]^2, 0, 0] - @test o.mayer(y0, yf, z) == y0[3] + yf[4] + z[2] + @test dynamics(o)(y, w, z) == [y[1] + w^2 + y[4]^3 + z[2], y[3]^2, 0, 0] + @test mayer(o)(y0, yf, z) == y0[3] + yf[4] + z[2] @def o begin z ∈ R², variable @@ -2323,8 +2323,8 @@ function test_onepass() y0 = y yf = 3y0 w = 7 - @test o.dynamics(y, w, z) == [y[1] + w^2 + y[4]^3 + z[2], y[3]^2, 0, 0] - @test o.mayer(y0, yf, z) == y0[3] + yf[4] + z[2] + @test dynamics(o)(y, w, z) == [y[1] + w^2 + y[4]^3 + z[2], y[3]^2, 0, 0] + @test mayer(o)(y0, yf, z) == y0[3] + yf[4] + z[2] @def o begin z ∈ R², variable @@ -2339,7 +2339,7 @@ function test_onepass() z = [5, 6] y0 = y yf = 3y0 - @test o.mayer(y0, yf, z) == y0[1] + y0[4]^3 + z[2] + yf[2] + @test mayer(o)(y0, yf, z) == y0[1] + y0[4]^3 + z[2] + yf[2] @def o begin z ∈ R², variable @@ -2357,8 +2357,8 @@ function test_onepass() y0 = y yf = 3y0 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) + @test dynamics(o)(y, w, z) == [y[1] + w^2 + y[4]^3 + z[2], y[3]^2, 0, 0] + @test_throws UndefVarError mayer(o)(y0, yf, z) end # --------------------------------------------------------------- @@ -2382,7 +2382,7 @@ function test_onepass() d = 4 x = 10 u = 20 - @test o.dynamics(x, u) == x + u + b + 3 + d + @test dynamics(o)(x, u) == x + u + b + 3 + d end # --------------------------------------------------------------- @@ -2487,9 +2487,9 @@ function test_onepass() 1 ] @test constraint(o, :eq1)(x0, xf) == x0 - @test o.dynamics(x, u) == A * x + B * u - @test o.lagrange(x, u) == 0.5u^2 - @test o.criterion == :min + @test dynamics(o)(x, u) == A * x + B * u + @test lagrange(o)(x, u) == 0.5u^2 + @test criterion(o) == :min @def o begin z in R, variable @@ -2514,8 +2514,8 @@ function test_onepass() @test constraint(o, :eq2)(x0, xf, z) == xf[2]^2 @test constraint(o, Symbol("♡"))(x0, xf, z) == x0 @test constraint(o, :eq3)(z) == z - @test o.dynamics(x, u, z) == [x[2], x[1]^2 + z] - @test o.lagrange(x, u, z) == u^2 + z * x[1] + @test dynamics(o)(x, u, z) == [x[2], x[1]^2 + z] + @test lagrange(o)(x, u, z) == u^2 + z * x[1] @def o begin z in R, variable @@ -2540,8 +2540,8 @@ function test_onepass() @test constraint(o, :eq2)(x0, xf, z) == xf[2]^2 @test constraint(o, Symbol("♡"))(x0, xf, z) == x0 @test constraint(o, :eq3)(z) == z - @test o.dynamics(x, u, z) == [x[2], x[1]^2 + z] - @test o.lagrange(x, u, z) == u^2 + z * x[1] + @test dynamics(o)(x, u, z) == [x[2], x[1]^2 + z] + @test lagrange(o)(x, u, z) == u^2 + z * x[1] @def o begin z in R^2, variable @@ -2568,7 +2568,7 @@ function test_onepass() @test constraint(o, :eq2)(x0, xf, z) == xf[2]^2 @test constraint(o, Symbol("♡"))(x0, xf, z) == x0 @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] + @test dynamics(o)(x, u, z) == [x[2], x[1]^2 + z[1]] + @test lagrange(o)(x, u, z) == u[1]^2 + z[1] * x[1] end end diff --git a/test/test_print.jl b/test/test_print.jl index e9148256..b2ef7b2e 100644 --- a/test/test_print.jl +++ b/test/test_print.jl @@ -18,9 +18,9 @@ function test_print() ocp = Model(autonomous = false) state!(ocp, 2, "state", ["r", "v"]) # dimension of the state with the names of the components control!(ocp, 1) # dimension of the control - __time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time - __constraint!(ocp, :initial, [-1, 0], [-1, 0]) - __constraint!(ocp, :final, [0, 0], [0, 0]) + time!(ocp; t0 = 0, tf = 1, name = "s") # initial and final time, with the name of the variable time + constraint!(ocp, :initial, lb = [-1, 0], ub = [-1, 0]) + constraint!(ocp, :final, lb = [0, 0], ub = [0, 0]) A = [ 0 1 0 0 @@ -30,11 +30,11 @@ function test_print() 1 ] dynamics!(ocp, (t, x, u) -> A * x + B * u) - __constraint!(ocp, :state, (t, x) -> x[2], 0, 1) - __constraint!(ocp, :control, (t, u) -> u, -1, 1) - __constraint!(ocp, :mixed, (t, x, u) -> x[1] + u, 2, 3) - __constraint!(ocp, :state, Index(1), -10, 10) - __constraint!(ocp, :control, -2, 2) + constraint!(ocp, :state, f = (t, x) -> x[2], lb = 0, ub = 1) + constraint!(ocp, :control, f = (t, u) -> u, lb = -1, ub = 1) + constraint!(ocp, :mixed, f = (t, x, u) -> x[1] + u, lb = 2, ub = 3) + constraint!(ocp, :state, rg = 1, lb = -10, ub = 10) + constraint!(ocp, :control, lb = -2, ub = 2) objective!(ocp, :bolza, (t0, x0, tf, xf) -> tf, (t, x, u) -> 0.5u^2) @test display(ocp) isa Nothing @@ -42,15 +42,15 @@ function test_print() ocp = Model(autonomous = false) state!(ocp, 1, "y") # dimension of the state with the names of the components control!(ocp, 1, "v") # dimension of the control - __time!(ocp, 0, 1, "s") # initial and final time, with the name of the variable time - __constraint!(ocp, :initial, -1, -1) - __constraint!(ocp, :final, 0, 0) + time!(ocp; t0 = 0, tf = 1, name = "s") # initial and final time, with the name of the variable time + constraint!(ocp, :initial, lb = -1, ub = -1) + constraint!(ocp, :final, lb = 0, ub = 0) dynamics!(ocp, (t, x, u) -> x + u) - __constraint!(ocp, :state, (t, x) -> x, 0, 1) - __constraint!(ocp, :control, (t, u) -> u, -1, 1) - __constraint!(ocp, :mixed, (t, x, u) -> x + u, 2, 3) - __constraint!(ocp, :state, -10, 10) - __constraint!(ocp, :control, -2, 2) + constraint!(ocp, :state, f = (t, x) -> x, lb = 0, ub = 1) + constraint!(ocp, :control, f = (t, u) -> u, lb = -1, ub = 1) + constraint!(ocp, :mixed, f = (t, x, u) -> x + u, lb = 2, ub = 3) + constraint!(ocp, :state, lb = -10, ub = 10) + constraint!(ocp, :control, lb = -2, ub = 2) objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) @test display(ocp) isa Nothing @@ -59,9 +59,9 @@ function test_print() variable!(ocp, 1) state!(ocp, 1, "y") # dimension of the state with the names of the components control!(ocp, 2) # dimension of the control - __time!(ocp, 0, Index(1), "s") # initial and final time, with the name of the variable time - __constraint!(ocp, :initial, -1, -1) - __constraint!(ocp, :final, 0, 0) + time!(ocp; t0 = 0, indf = 1, name = "s") # initial and final time, with the name of the variable time + constraint!(ocp, :initial, lb = -1, ub = -1) + constraint!(ocp, :final, lb = 0, ub = 0) dynamics!(ocp, (t, x, u) -> x + u) objective!(ocp, :mayer, (t0, x0, tf, xf) -> tf) @test display(ocp) isa Nothing diff --git a/test/utils.jl b/test/utils.jl deleted file mode 100644 index 80a2484d..00000000 --- a/test/utils.jl +++ /dev/null @@ -1,97 +0,0 @@ -# time! - -function __time!( - ocp::OptimalControlModel{<:TimeDependence, NonFixed}, - t0::Time, - indf::Index, - name::String = CTBase.__time_name(), -) - time!(ocp; t0 = t0, indf = indf.val, name = name) -end - -function __time!(ocp::OptimalControlModel, t0::Time, indf::Index, name::Symbol) - time!(ocp; t0 = t0, indf = indf.val, name = name) -end - -function __time!( - ocp::OptimalControlModel{<:TimeDependence, NonFixed}, - ind0::Index, - tf::Time, - name::String = CTBase.__time_name(), -) - time!(ocp; ind0 = ind0.val, tf = tf, name = name) -end - -function __time!(ocp::OptimalControlModel, ind0::Index, tf::Time, name::Symbol) - time!(ocp; ind0 = ind0.val, tf = tf, name = name) -end - -function __time!( - ocp::OptimalControlModel{<:TimeDependence, NonFixed}, - ind0::Index, - indf::Index, - name::String = CTBase.__time_name(), -) - time!(ocp; ind0 = ind0.val, indf = indf.val, name = name) -end - -function __time!(ocp::OptimalControlModel, ind0::Index, indf::Index, name::Symbol) - time!(ocp; ind0 = ind0.val, indf = indf.val, name = name) -end - -function __time!(ocp::OptimalControlModel, t0::Time, tf::Time, name::String = CTBase.__time_name()) - time!(ocp; t0 = t0, tf = tf, name = name) -end - -function __time!(ocp::OptimalControlModel, t0::Time, tf::Time, name::Symbol) - time!(ocp; t0 = t0, tf = tf, name = name) -end - -# constraint! - -function __constraint!( - ocp::OptimalControlModel{<:TimeDependence, V}, - type::Symbol, - rg::Index, - lb::Union{ctVector, Nothing}, - ub::Union{ctVector, Nothing}, - label::Symbol = CTBase.__constraint_label(), -) 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}, - type::Symbol, - rg::OrdinalRange{<:Integer}, - lb::Union{ctVector, Nothing}, - ub::Union{ctVector, Nothing}, - label::Symbol = CTBase.__constraint_label(), -) 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}, - 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}, - type::Symbol, - f::Function, - lb::Union{ctVector, Nothing}, - ub::Union{ctVector, Nothing}, - label::Symbol = CTBase.__constraint_label(), -) where {T, V} - constraint!(ocp, type, rg = nothing, f = f, lb = lb, ub = ub, label = label) - nothing # to force to return nothing -end From a4142d5f2d9dca61f67ed4b735655e2ccf48f0c9 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Fri, 30 Aug 2024 08:59:24 +0200 Subject: [PATCH 2/5] remove output types --- src/optimal_control_solution-getters.jl | 74 ++++++++++++------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/optimal_control_solution-getters.jl b/src/optimal_control_solution-getters.jl index 5411b2fe..b7a6a2b4 100644 --- a/src/optimal_control_solution-getters.jl +++ b/src/optimal_control_solution-getters.jl @@ -4,7 +4,7 @@ $(TYPEDSIGNATURES) Return the time grid of the optimal control solution or `nothing`. """ -time_grid(sol::OptimalControlSolution)::TimesDisc = sol.time_grid +time_grid(sol::OptimalControlSolution) = sol.time_grid """ $(TYPEDSIGNATURES) @@ -12,7 +12,7 @@ $(TYPEDSIGNATURES) Return the name of the initial time of the optimal control solution or `nothing`. """ -initial_time_name(sol::OptimalControlSolution)::String = sol.initial_time_name +initial_time_name(sol::OptimalControlSolution) = sol.initial_time_name """ $(TYPEDSIGNATURES) @@ -20,7 +20,7 @@ $(TYPEDSIGNATURES) Return the name of final time of the optimal control solution or `nothing`. """ -final_time_name(sol::OptimalControlSolution)::String = sol.final_time_name +final_time_name(sol::OptimalControlSolution) = sol.final_time_name """ $(TYPEDSIGNATURES) @@ -28,7 +28,7 @@ $(TYPEDSIGNATURES) Return the name of the time component of the optimal control solution or `nothing`. """ -time_name(sol::OptimalControlSolution)::String = sol.time_name +time_name(sol::OptimalControlSolution) = sol.time_name """ $(TYPEDSIGNATURES) @@ -36,7 +36,7 @@ $(TYPEDSIGNATURES) Return the dimension of the control of the optimal control solution or `nothing`. """ -control_dimension(sol::OptimalControlSolution)::Dimension = sol.control_dimension +control_dimension(sol::OptimalControlSolution) = sol.control_dimension """ $(TYPEDSIGNATURES) @@ -52,7 +52,7 @@ $(TYPEDSIGNATURES) Return the name of the control of the optimal control solution or `nothing`. """ -control_name(sol::OptimalControlSolution)::String = sol.control_name +control_name(sol::OptimalControlSolution) = sol.control_name """ $(TYPEDSIGNATURES) @@ -65,7 +65,7 @@ julia> u = control(sol) julia> u0 = u(t0) # control at initial time ``` """ -control(sol::OptimalControlSolution)::Function = sol.control +control(sol::OptimalControlSolution) = sol.control """ $(TYPEDSIGNATURES) @@ -85,7 +85,7 @@ $(TYPEDSIGNATURES) Return the dimension of the state of the optimal control solution or `nothing`. """ -state_dimension(sol::OptimalControlSolution)::Dimension = sol.state_dimension +state_dimension(sol::OptimalControlSolution) = sol.state_dimension """ $(TYPEDSIGNATURES) @@ -101,7 +101,7 @@ $(TYPEDSIGNATURES) Return the name of the state of the optimal control solution or `nothing`. """ -state_name(sol::OptimalControlSolution)::String = sol.state_name +state_name(sol::OptimalControlSolution) = sol.state_name """ $(TYPEDSIGNATURES) @@ -114,7 +114,7 @@ julia> x = state(sol) julia> x0 = x(t0) ``` """ -state(sol::OptimalControlSolution)::Function = sol.state +state(sol::OptimalControlSolution) = sol.state """ $(TYPEDSIGNATURES) @@ -134,7 +134,7 @@ $(TYPEDSIGNATURES) Return the dimension of the variable of the optimal control solution or `nothing`. """ -variable_dimension(sol::OptimalControlSolution)::Dimension = sol.variable_dimension +variable_dimension(sol::OptimalControlSolution) = sol.variable_dimension """ $(TYPEDSIGNATURES) @@ -150,7 +150,7 @@ $(TYPEDSIGNATURES) Return the name of the variable of the optimal control solution or `nothing`. """ -variable_name(sol::OptimalControlSolution)::String = sol.variable_name +variable_name(sol::OptimalControlSolution) = sol.variable_name """ $(TYPEDSIGNATURES) @@ -161,7 +161,7 @@ Return the variable of the optimal control solution or `nothing`. julia> v = variable(sol) ``` """ -variable(sol::OptimalControlSolution)::Variable = sol.variable +variable(sol::OptimalControlSolution) = sol.variable """ $(TYPEDSIGNATURES) @@ -174,7 +174,7 @@ julia> p = costate(sol) julia> p0 = p(t0) ``` """ -costate(sol::OptimalControlSolution)::Function = sol.costate +costate(sol::OptimalControlSolution) = sol.costate """ $(TYPEDSIGNATURES) @@ -186,7 +186,7 @@ julia> p = costate_discretized(sol) julia> p0 = p[1] # costate at initial time ``` """ -costate_discretized(sol::OptimalControlSolution) = sol.costate.(sol.time_grid) +costate_discretized(sol::OptimalControlSolution) = costate(sol).(sol.time_grid) """ $(TYPEDSIGNATURES) @@ -194,7 +194,7 @@ $(TYPEDSIGNATURES) Return the objective value of the optimal control solution or `nothing`. """ -objective(sol::OptimalControlSolution)::ctNumber = sol.objective +objective(sol::OptimalControlSolution) = sol.objective """ $(TYPEDSIGNATURES) @@ -202,7 +202,7 @@ $(TYPEDSIGNATURES) Return the number of iterations (if solved by an iterative method) of the optimal control solution or `nothing`. """ -iterations(sol::OptimalControlSolution)::Int = sol.iterations +iterations(sol::OptimalControlSolution) = sol.iterations """ $(TYPEDSIGNATURES) @@ -210,7 +210,7 @@ $(TYPEDSIGNATURES) Return the stopping criterion (a Symbol) of the optimal control solution or `nothing`. """ -stopping(sol::OptimalControlSolution)::Symbol = sol.stopping +stopping(sol::OptimalControlSolution) = sol.stopping """ $(TYPEDSIGNATURES) @@ -218,7 +218,7 @@ $(TYPEDSIGNATURES) Return the message associated to the stopping criterion of the optimal control solution or `nothing`. """ -message(sol::OptimalControlSolution)::String = sol.message +message(sol::OptimalControlSolution) = sol.message """ $(TYPEDSIGNATURES) @@ -226,7 +226,7 @@ $(TYPEDSIGNATURES) Return the true if the solver has finished successfully of false if not, or `nothing`. """ -success(sol::OptimalControlSolution)::Bool = sol.success +success(sol::OptimalControlSolution) = sol.success """ $(TYPEDSIGNATURES) @@ -234,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) = sol.infos # constraints and multipliers @@ -244,7 +244,7 @@ $(TYPEDSIGNATURES) Return the boundary constraints of the optimal control solution or `nothing`. """ -boundary_constraints(sol::OptimalControlSolution)::ctVector = sol.boundary_constraints +boundary_constraints(sol::OptimalControlSolution) = sol.boundary_constraints """ $(TYPEDSIGNATURES) @@ -252,7 +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) = sol.mult_boundary_constraints """ $(TYPEDSIGNATURES) @@ -260,7 +260,7 @@ $(TYPEDSIGNATURES) Return the variable constraints of the optimal control solution or `nothing`. """ -variable_constraints(sol::OptimalControlSolution)::ctVector = sol.variable_constraints +variable_constraints(sol::OptimalControlSolution) = sol.variable_constraints """ $(TYPEDSIGNATURES) @@ -268,7 +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) = sol.mult_variable_constraints """ $(TYPEDSIGNATURES) @@ -276,7 +276,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the variable lower bounds of the optimal control solution or `nothing`. """ -mult_variable_box_lower(sol::OptimalControlSolution)::ctVector = sol.mult_variable_box_lower +mult_variable_box_lower(sol::OptimalControlSolution) = sol.mult_variable_box_lower """ $(TYPEDSIGNATURES) @@ -284,7 +284,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the variable upper bounds of the optimal control solution or `nothing`. """ -mult_variable_box_upper(sol::OptimalControlSolution)::ctVector = sol.mult_variable_box_upper +mult_variable_box_upper(sol::OptimalControlSolution) = sol.mult_variable_box_upper """ $(TYPEDSIGNATURES) @@ -292,7 +292,7 @@ $(TYPEDSIGNATURES) Return the control constraints of the optimal control solution or `nothing`. """ -control_constraints(sol::OptimalControlSolution)::Function = sol.control_constraints +control_constraints(sol::OptimalControlSolution) = sol.control_constraints """ $(TYPEDSIGNATURES) @@ -300,7 +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) = sol.mult_control_constraints """ $(TYPEDSIGNATURES) @@ -308,7 +308,7 @@ $(TYPEDSIGNATURES) Return the state constraints of the optimal control solution or `nothing`. """ -state_constraints(sol::OptimalControlSolution)::Function = sol.state_constraints +state_constraints(sol::OptimalControlSolution) = sol.state_constraints """ $(TYPEDSIGNATURES) @@ -316,7 +316,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the state constraints of the optimal control solution or `nothing`. """ -mult_state_constraints(sol::OptimalControlSolution)::Function = sol.mult_state_constraints +mult_state_constraints(sol::OptimalControlSolution) = sol.mult_state_constraints """ $(TYPEDSIGNATURES) @@ -324,7 +324,7 @@ $(TYPEDSIGNATURES) Return the mixed state-control constraints of the optimal control solution or `nothing`. """ -mixed_constraints(sol::OptimalControlSolution)::Function = sol.mixed_constraints +mixed_constraints(sol::OptimalControlSolution) = sol.mixed_constraints """ $(TYPEDSIGNATURES) @@ -332,7 +332,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the mixed state-control constraints of the optimal control solution or `nothing`. """ -mult_mixed_constraints(sol::OptimalControlSolution)::Function = sol.mult_mixed_constraints +mult_mixed_constraints(sol::OptimalControlSolution) = sol.mult_mixed_constraints """ $(TYPEDSIGNATURES) @@ -340,7 +340,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the state lower bounds of the optimal control solution or `nothing`. """ -mult_state_box_lower(sol::OptimalControlSolution)::Function = sol.mult_state_box_lower +mult_state_box_lower(sol::OptimalControlSolution) = sol.mult_state_box_lower """ $(TYPEDSIGNATURES) @@ -348,7 +348,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the state upper bounds of the optimal control solution or `nothing`. """ -mult_state_box_upper(sol::OptimalControlSolution)::Function = sol.mult_state_box_upper +mult_state_box_upper(sol::OptimalControlSolution) = sol.mult_state_box_upper """ $(TYPEDSIGNATURES) @@ -356,7 +356,7 @@ $(TYPEDSIGNATURES) Return the multipliers to the control lower bounds of the optimal control solution or `nothing`. """ -mult_control_box_lower(sol::OptimalControlSolution)::Function = sol.mult_control_box_lower +mult_control_box_lower(sol::OptimalControlSolution) = sol.mult_control_box_lower """ $(TYPEDSIGNATURES) @@ -364,4 +364,4 @@ $(TYPEDSIGNATURES) Return the multipliers to the control upper bounds of the optimal control solution or `nothing`. """ -mult_control_box_upper(sol::OptimalControlSolution)::Function = sol.mult_control_box_upper +mult_control_box_upper(sol::OptimalControlSolution) = sol.mult_control_box_upper From 6444959c41284c9f92dbb5158660d44f0518099f Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Fri, 30 Aug 2024 09:21:55 +0200 Subject: [PATCH 3/5] up --- ext/plot.jl | 8 +++--- src/CTBase.jl | 4 +-- src/init.jl | 6 ++-- src/onepass.jl | 2 +- src/optimal_control_model-getters.jl | 5 +--- src/optimal_control_model-setters.jl | 8 ++++++ src/optimal_control_solution-getters.jl | 4 +-- src/optimal_control_solution-setters.jl | 20 ++++++------- src/print.jl | 2 +- test/test_model.jl | 4 +-- test/test_solution.jl | 38 ++++++++++++------------- 11 files changed, 53 insertions(+), 48 deletions(-) diff --git a/ext/plot.jl b/ext/plot.jl index 7c6d2ec6..4474b72f 100644 --- a/ext/plot.jl +++ b/ext/plot.jl @@ -664,10 +664,10 @@ function __get_data_plot( xx::Union{Symbol, Tuple{Symbol, Integer}}; time::Symbol = :default, ) - T = sol.time_grid - X = sol.state.(T) - U = sol.control.(T) - P = sol.costate.(T) + T = time_grid(sol) + X = state(sol).(T) + U = control(sol).(T) + P = costate(sol).(T) vv, ii = @match xx begin ::Symbol => (xx, 1) diff --git a/src/CTBase.jl b/src/CTBase.jl index 758a7752..426362b5 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -258,10 +258,10 @@ 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!, model_expression! 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, constraints_labels +export nlp_constraints!, constraints, constraints_labels, constraint export has_free_final_time, has_free_initial_time, has_lagrange_cost, has_mayer_cost export dim_control_constraints, dim_state_constraints, dim_mixed_constraints, dim_path_constraints export dim_boundary_constraints, dim_variable_constraints, dim_control_range diff --git a/src/init.jl b/src/init.jl index 7c572b45..c9d34918 100644 --- a/src/init.jl +++ b/src/init.jl @@ -236,9 +236,9 @@ mutable struct OptimalControlInit """ function OptimalControlInit(sol::OptimalControlSolution; unused_kwargs...) return OptimalControlInit( - state = sol.state, - control = sol.control, - variable = sol.variable, + state = state(sol), + control = control(sol), + variable = variable(sol), state_dim = state_dimension(sol), control_dim = control_dimension(sol), variable_dim = variable_dimension(sol), diff --git a/src/onepass.jl b/src/onepass.jl index 753c46ff..827985f3 100644 --- a/src/onepass.jl +++ b/src/onepass.jl @@ -622,7 +622,7 @@ macro def(ocp, e, log = false) _ => :($ocp = __OCPModel(autonomous = false, variable = true)) end ee = QuoteNode(e) - code = Expr(:block, init, code, :($ocp.model_expression = $ee; $ocp)) + code = Expr(:block, init, code, :(model_expression!($ocp, $ee); $ocp)) esc(code) catch ex :(throw($ex)) # can be caught by user diff --git a/src/optimal_control_model-getters.jl b/src/optimal_control_model-getters.jl index 75817f3a..b7c74285 100644 --- a/src/optimal_control_model-getters.jl +++ b/src/optimal_control_model-getters.jl @@ -36,9 +36,6 @@ function nlp_constraints!(ocp::OptimalControlModel) # we check if the dimensions and times have been set __check_all_set(ocp) - # - constraints = ocp.constraints - ξf = Vector{ControlConstraint}() ξl = Vector{ctNumber}() ξu = Vector{ctNumber}() @@ -64,7 +61,7 @@ function nlp_constraints!(ocp::OptimalControlModel) vl = Vector{ctNumber}() vu = Vector{ctNumber}() - for (_, c) ∈ constraints + for (_, c) ∈ constraints(ocp) @match c begin (type, f::BoundaryConstraint, lb, ub) && if type ∈ [:initial, :final, :boundary] end => begin diff --git a/src/optimal_control_model-setters.jl b/src/optimal_control_model-setters.jl index 3eb5d96c..0f8f3c99 100644 --- a/src/optimal_control_model-setters.jl +++ b/src/optimal_control_model-setters.jl @@ -8,6 +8,14 @@ """ $(TYPEDSIGNATURES) +Set the model expression of the optimal control problem or `nothing`. + +""" +model_expression!(ocp::OptimalControlModel, model_expression::Expr) = (ocp.model_expression = model_expression; nothing) + +""" +$(TYPEDSIGNATURES) + Return a new `OptimalControlModel` instance, that is a model of an optimal control problem. The model is defined by the following optional keyword argument: diff --git a/src/optimal_control_solution-getters.jl b/src/optimal_control_solution-getters.jl index b7a6a2b4..c53324c5 100644 --- a/src/optimal_control_solution-getters.jl +++ b/src/optimal_control_solution-getters.jl @@ -77,7 +77,7 @@ julia> u = control_discretized(sol) julia> u0 = u[1] # control at initial time ``` """ -control_discretized(sol::OptimalControlSolution) = sol.control.(sol.time_grid) +control_discretized(sol::OptimalControlSolution) = control(sol).(sol.time_grid) """ $(TYPEDSIGNATURES) @@ -126,7 +126,7 @@ julia> x = state_discretized(sol) julia> x0 = x[1] # state at initial time ``` """ -state_discretized(sol::OptimalControlSolution) = sol.state.(sol.time_grid) +state_discretized(sol::OptimalControlSolution) = state(sol).(sol.time_grid) """ $(TYPEDSIGNATURES) diff --git a/src/optimal_control_solution-setters.jl b/src/optimal_control_solution-setters.jl index d5ee8701..ef1a7496 100644 --- a/src/optimal_control_solution-setters.jl +++ b/src/optimal_control_solution-setters.jl @@ -100,7 +100,7 @@ function OptimalControlSolution( state::Function, control::Function, objective::ctNumber, - #variable::Union{Nothing, Variable}=nothing, + variable::Union{Nothing, Variable}=nothing, costate::Union{Nothing, Function} = nothing, time_grid::Union{Nothing, TimesDisc} = nothing, iterations::Union{Nothing, Integer} = nothing, @@ -110,10 +110,10 @@ function OptimalControlSolution( 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, + 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, @@ -132,7 +132,7 @@ function OptimalControlSolution( objective = objective, costate = costate, time_grid = time_grid, - #variable=variable, + variable=variable, iterations = iterations, stopping = stopping, message = message, @@ -140,10 +140,10 @@ function OptimalControlSolution( infos = infos, boundary_constraints = boundary_constraints, mult_boundary_constraints = mult_boundary_constraints, - #variable_constraints = variable_constraints, - #mult_variable_constraints = mult_variable_constraints, - #mult_variable_box_lower = mult_variable_box_lower, - #mult_variable_box_upper = mult_variable_box_upper, + variable_constraints = variable_constraints, + mult_variable_constraints = mult_variable_constraints, + mult_variable_box_lower = mult_variable_box_lower, + mult_variable_box_upper = mult_variable_box_upper, control_constraints = control_constraints, mult_control_constraints = mult_control_constraints, state_constraints = state_constraints, diff --git a/src/print.jl b/src/print.jl index c6012e4a..e0afe8fa 100644 --- a/src/print.jl +++ b/src/print.jl @@ -259,7 +259,7 @@ function Base.show( data, __is_dynamics_not_set(ocp) ? "X" : "V", __is_objective_not_set(ocp) ? "X" : "V", - isempty(ocp.constraints) ? "X" : "V", + isempty(constraints(ocp)) ? "X" : "V", ) println("") h1 = Highlighter((data, i, j) -> data[i, j] == "X", bold = true, foreground = :red) diff --git a/test/test_model.jl b/test/test_model.jl index 7aa363e3..4e3bb619 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -962,7 +962,7 @@ function test_model() # 30 55 185 constraint!(ocp_bis, :control, f = dummy, ub = 1, lb = 1, label = :c1) constraint!(ocp_bis, :variable, rg = 1:2:3, ub = [0, 0], label = :c2) - @test ocp.constraints == ocp_bis.constraints + @test constraints(ocp) == constraints(ocp_bis) ocp_ter = Model(variable = true) time!(ocp_ter; t0 = 0, tf = 1) @@ -982,7 +982,7 @@ function test_model() # 30 55 185 constraint!(ocp_quad, :control, f = dummy, lb = 1, label = :c1) constraint!(ocp_quad, :state, rg = 1:2:3, lb = [0, 0], ub = [0, 0], label = :c2) - @test ocp_ter.constraints == ocp_quad.constraints + @test constraints(ocp_ter) == constraints(ocp_quad) ocp_error = ocp_error = Model(variable = true) time!(ocp_error; t0 = 0, tf = 1) diff --git a/test/test_solution.jl b/test/test_solution.jl index b818fc6f..0c8b9e80 100644 --- a/test/test_solution.jl +++ b/test/test_solution.jl @@ -12,26 +12,26 @@ function test_solution() end times = range(0, 1, 10) - state = t -> t - control = t -> 2t - costate = t -> t - objective = 1 + x = t -> t + u = t -> 2t + p = t -> t + obj = 1 sol = OptimalControlSolution( ocp; - state = state, - control = control, - costate = costate, - objective = objective, + state = x, + control = u, + costate = p, + objective = obj, time_grid = times, ) - @test sol.objective == objective + @test objective(sol) == obj @test typeof(sol) == OptimalControlSolution # getters - @test all(state_discretized(sol) .== state.(times)) - @test all(control_discretized(sol) .== control.(times)) - @test all(costate_discretized(sol) .== costate.(times)) + @test all(state_discretized(sol) .== x.(times)) + @test all(control_discretized(sol) .== u.(times)) + @test all(costate_discretized(sol) .== p.(times)) # NonFixed ocp @def ocp begin @@ -45,13 +45,13 @@ function test_solution() ∫(0.5u(t)^2) → min end - state = t -> t - control = t -> 2t - objective = 1 - variable = 1 - sol = OptimalControlSolution(ocp; state, control, objective, variable) + x = t -> t + u = t -> 2t + obj = 1 + v = 1 + sol = OptimalControlSolution(ocp; state = x, control = u, objective = obj, variable = v) - @test sol.variable == variable + @test variable(sol) == v @test typeof(sol) == OptimalControlSolution - @test_throws UndefKeywordError OptimalControlSolution(ocp; state, control, objective) + @test_throws UndefKeywordError OptimalControlSolution(ocp; x, u, obj) end From cfebe0febdd0e62365937af528a2a69ea90f91d0 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Fri, 30 Aug 2024 09:38:38 +0200 Subject: [PATCH 4/5] no call to setters nor getters in onepass --- src/onepass.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/onepass.jl b/src/onepass.jl index 827985f3..8f035128 100644 --- a/src/onepass.jl +++ b/src/onepass.jl @@ -248,7 +248,7 @@ p_time!(p, ocp, t, t0, tf; log = false) = begin end => :(time!($ocp; ind0 = $i, tf = $tf, name = $tt)) :($v1) && if (v1 == p.v) end => quote - (variable_dimension($ocp) ≠ 1) && throw( + ($ocp.variable_dimension ≠ 1) && throw( IncorrectArgument("variable must be of dimension one for a time"), ) time!($ocp; ind0 = 1, tf = $tf, name = $tt) @@ -260,7 +260,7 @@ p_time!(p, ocp, t, t0, tf; log = false) = begin end => :(time!($ocp; t0 = $t0, indf = $i, name = $tt)) :($v1) && if (v1 == p.v) end => quote - (variable_dimension($ocp) ≠ 1) && throw( + ($ocp.variable_dimension ≠ 1) && throw( IncorrectArgument("variable must be of dimension one for a time"), ) time!($ocp; t0 = $t0, indf = 1, name = $tt) @@ -622,7 +622,7 @@ macro def(ocp, e, log = false) _ => :($ocp = __OCPModel(autonomous = false, variable = true)) end ee = QuoteNode(e) - code = Expr(:block, init, code, :(model_expression!($ocp, $ee); $ocp)) + code = Expr(:block, init, code, :($ocp.model_expression! = $ee; $ocp)) esc(code) catch ex :(throw($ex)) # can be caught by user From fdebad397fb148767e4cd822d03b26021fa66764 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Fri, 30 Aug 2024 09:55:27 +0200 Subject: [PATCH 5/5] no call to setters nor getters in onepass --- src/onepass.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/onepass.jl b/src/onepass.jl index 8f035128..f840fd1e 100644 --- a/src/onepass.jl +++ b/src/onepass.jl @@ -622,7 +622,7 @@ macro def(ocp, e, log = false) _ => :($ocp = __OCPModel(autonomous = false, variable = true)) end ee = QuoteNode(e) - code = Expr(:block, init, code, :($ocp.model_expression! = $ee; $ocp)) + code = Expr(:block, init, code, :($ocp.model_expression = $ee; $ocp)) esc(code) catch ex :(throw($ex)) # can be caught by user