diff --git a/src/ModelPredictiveControl.jl b/src/ModelPredictiveControl.jl index 84827076e..c33cc2c85 100644 --- a/src/ModelPredictiveControl.jl +++ b/src/ModelPredictiveControl.jl @@ -10,7 +10,8 @@ using RecipesBase using DifferentiationInterface: ADTypes.AbstractADType, AutoForwardDiff using DifferentiationInterface: AutoSparse, SecondOrder using DifferentiationInterface: gradient, jacobian, hessian -using DifferentiationInterface: value_and_gradient, value_gradient_and_hessian +using DifferentiationInterface: value_and_gradient, value_and_jacobian +using DifferentiationInterface: value_gradient_and_hessian using DifferentiationInterface: gradient!, value_and_gradient!, prepare_gradient using DifferentiationInterface: jacobian!, value_and_jacobian!, prepare_jacobian using DifferentiationInterface: hessian!, value_gradient_and_hessian!, prepare_hessian diff --git a/src/controller/execute.jl b/src/controller/execute.jl index affe7a4e8..945aca692 100644 --- a/src/controller/execute.jl +++ b/src/controller/execute.jl @@ -112,8 +112,10 @@ are also available: - `:gc`: custom nonlinear constraints values at the optimum, ``\mathbf{g_c}`` - `:∇J` or *`:nablaJ`* : optimal gradient of the objective function, ``\mathbf{\nabla} J`` - `:∇²J` or *`:nabla2J`* : optimal Hessian of the objective function, ``\mathbf{\nabla^2}J`` +- `:g` : optimal nonlinear inequality constraint values, ``\mathbf{g}`` - `:∇g` or *`:nablag`* : optimal Jacobian of the inequality constraint, ``\mathbf{\nabla g}`` - `:∇²ℓg` or *`:nabla2lg`* : optimal Hessian of the inequality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g}}`` +- `:geq` : optimal nonlinear equality constraint values, ``\mathbf{g_{eq}}`` - `:∇geq` or *`:nablageq`* : optimal Jacobian of the equality constraint, ``\mathbf{\nabla g_{eq}}`` - `:∇²ℓgeq` or *`:nabla2lgeq`* : optimal Hessian of the equality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g_{eq}}}`` diff --git a/src/controller/nonlinmpc.jl b/src/controller/nonlinmpc.jl index aebb63d64..4c6ed1fee 100644 --- a/src/controller/nonlinmpc.jl +++ b/src/controller/nonlinmpc.jl @@ -526,8 +526,8 @@ end """ addinfo!(info, mpc::NonLinMPC) -> info -For [`NonLinMPC`](@ref), add `:sol`, the custom nonlinear objective `:JE`, the custom -constraint `:gc`, and the various derivatives. +For [`NonLinMPC`](@ref), add `:sol`, the custom nonlinear objective `:JE`, the nonlinear +constraint vectors and the various derivatives. """ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real # --- variables specific to NonLinMPC --- @@ -585,7 +585,7 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃) return nothing end - ∇g = jacobian(g!, g, mpc.jacobian, mpc.Z̃, ∇g_cache...) + g, ∇g = value_and_jacobian(g!, g, mpc.jacobian, mpc.Z̃, ∇g_cache...) if !isnothing(mpc.hessian) && any(old_i_g) @warn( "Retrieving optimal Hessian of the Lagrangian is not fully supported yet.\n"* @@ -618,7 +618,7 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃) return nothing end - ∇geq = jacobian(geq!, geq, mpc.jacobian, mpc.Z̃, geq_cache...) + geq, ∇geq = value_and_jacobian(geq!, geq, mpc.jacobian, mpc.Z̃, geq_cache...) if !isnothing(mpc.hessian) && con.neq > 0 @warn( "Retrieving optimal Hessian of the Lagrangian is not fully supported yet.\n"* @@ -642,8 +642,10 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real end info[:∇J] = ∇J info[:∇²J] = ∇²J + info[:g] = g info[:∇g] = ∇g info[:∇²ℓg] = ∇²ℓg + info[:geq] = geq info[:∇geq] = ∇geq info[:∇²ℓgeq] = ∇²ℓgeq # --- non-Unicode fields --- diff --git a/src/estimator/mhe/execute.jl b/src/estimator/mhe/execute.jl index 8247222b0..3e2cfd922 100644 --- a/src/estimator/mhe/execute.jl +++ b/src/estimator/mhe/execute.jl @@ -94,12 +94,11 @@ following fields: - `:D` : measured disturbances over ``N_k``, ``\mathbf{D}`` - `:sol` : solution summary of the optimizer for printing -For [`NonLinModel`](@ref), it also includes the following derivative fields: +For [`NonLinModel`](@ref), it also includes the following fields: -- `:JE`: economic cost value at the optimum, ``J_E`` -- `:gc`: custom nonlinear constraints values at the optimum, ``\mathbf{g_c}`` - `:∇J` or *`:nablaJ`* : optimal gradient of the objective function, ``\mathbf{\nabla} J`` - `:∇²J` or *`:nabla2J`* : optimal Hessian of the objective function, ``\mathbf{\nabla^2}J`` +- `:g` : optimal nonlinear inequality constraint values, ``\mathbf{g}`` - `:∇g` or *`:nablag`* : optimal Jacobian of the inequality constraint, ``\mathbf{\nabla g}`` - `:∇²ℓg` or *`:nabla2lg`* : optimal Hessian of the inequality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g}}`` @@ -220,7 +219,7 @@ function addinfo!( update_prediction!(V̂, X̂0, û0, k0, ŷ0, g, estim, Z̃) return nothing end - ∇g = jacobian(g!, g, estim.jacobian, estim.Z̃, ∇g_cache...) + g, ∇g = value_and_jacobian(g!, g, estim.jacobian, estim.Z̃, ∇g_cache...) if !isnothing(estim.hessian) && any(old_i_g) @warn( "Retrieving optimal Hessian of the Lagrangian is not fully supported yet.\n"* @@ -241,6 +240,7 @@ function addinfo!( estim.con.i_g .= old_i_g # restore original finite/infinite constraint indices info[:∇J] = ∇J info[:∇²J] = ∇²J + info[:g] = g info[:∇g] = ∇g info[:∇²ℓg] = ∇²ℓg # --- non-Unicode fields ---