From 33b521f7cf6060ebf5c135b5d26a6995e9c9d00a Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Caillau Date: Sat, 22 Jun 2024 15:24:53 +0200 Subject: [PATCH] simplified nlp_constraints --- src/CTBase.jl | 2 +- src/model.jl | 31 ++++++------------------------- test/test_model.jl | 1 + 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/src/CTBase.jl b/src/CTBase.jl index 64dc27ef..e5f9fbae 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -274,4 +274,4 @@ export @def export ct_repl, ct_repl_update_model isdefined(Base, :active_repl) && ct_repl() -end +end \ No newline at end of file diff --git a/src/model.jl b/src/model.jl index 37411b29..b0c9e6f8 100644 --- a/src/model.jl +++ b/src/model.jl @@ -895,15 +895,7 @@ function nlp_constraints(ocp::OptimalControlModel) for (_, c) ∈ constraints @match c begin - (:initial, f::BoundaryConstraint, lb, ub) => begin - push!(ϕf, f) - append!(ϕl, lb) - append!(ϕu, ub) end - (:final, f::BoundaryConstraint, lb, ub) => begin - push!(ϕf, f) - append!(ϕl, lb) - append!(ϕu, ub) end - (:boundary, f::BoundaryConstraint, lb, ub) => begin + (type, f::BoundaryConstraint, lb, ub) && if type ∈ [:initial, :final, :boundary] end => begin push!(ϕf, f) append!(ϕl, lb) append!(ϕu, ub) end @@ -938,47 +930,36 @@ function nlp_constraints(ocp::OptimalControlModel) _ => error("Internal error") end end - function ξ(t, u, v) + function ξ(t, u, v) # nonlinear control constraints val = Vector{ctNumber}() for i ∈ 1:length(ξf) append!(val, ξf[i](t, u, v)) end return val end - function η(t, x, v) + function η(t, x, v) # nonlinear state constraints val = Vector{ctNumber}() for i ∈ 1:length(ηf) append!(val, ηf[i](t, x, v)) end return val end - function ψ(t, x, u, v) + function ψ(t, x, u, v) # nonlinear mixed constraints val = Vector{ctNumber}() for i ∈ 1:length(ψf) append!(val, ψf[i](t, x, u, v)) end return val end - function ϕ(x0, xf, v) + function ϕ(x0, xf, v) # nonlinear boundary constraints val = Vector{ctNumber}() for i ∈ 1:length(ϕf) append!(val, ϕf[i](x0, xf, v)) end return val end - function θ(v) + function θ(v) # nonlinear variable constraints val = Vector{ctNumber}() for i ∈ 1:length(θf) append!(val, θf[i](v)) end return val end - # set specific constraints dimensions - ocp.dim_control_constraints = length(ξl) - ocp.dim_state_constraints = length(ηl) - ocp.dim_mixed_constraints = length(ψl) - ocp.dim_path_constraints = ocp.dim_control_constraints + ocp.dim_state_constraints + ocp.dim_mixed_constraints - ocp.dim_boundary_conditions = length(ϕl) - ocp.dim_variable_constraints = length(θl) - ocp.dim_control_box = length(ul) - ocp.dim_state_box = length(xl) - ocp.dim_variable_box = length(vl) - return (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (θl, θ, θu), (ul, uind, uu), (xl, xind, xu), (vl, vind, vu) end diff --git a/test/test_model.jl b/test/test_model.jl index 0f6ca109..bec3886c 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -818,6 +818,7 @@ end @test_throws UnauthorizedCall constraint!(ocp_error, :state, rg=1:2:3, label=:c2) @test_throws IncorrectArgument constraint!(ocp_error, :state, rg=1:2:3, f=dummy, lb=[0,0], ub=[0,0], label=:c3) @test_throws IncorrectArgument constraint!(ocp_error, :state, f=dummy, rg=1:2:3, lb=[0,0], ub=[0,0], label=:c4) + @test_throws IncorrectArgument constraint!(ocp_error, :foo, lb=[0,0], ub=[0,0], label=:c5) end