Skip to content

Commit d98d59e

Browse files
committed
debug: temporarily finite constraints in addinfo!
1 parent 88043f2 commit d98d59e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/controller/nonlinmpc.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
541541
info[:JE] = JE
542542
info[:gc] = LHS
543543
info[:sol] = JuMP.solution_summary(mpc.optim, verbose=true)
544-
# --- derivatives ---
544+
# --- objective derivatives ---
545545
model, optim = mpc.estim.model, mpc.optim
546546
transcription = mpc.transcription
547547
nu, ny, nx̂, nϵ = model.nu, model.ny, mpc.estim.nx̂, mpc.
@@ -573,6 +573,9 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
573573
else
574574
∇J, ∇²J = gradient(J!, mpc.gradient, mpc.Z̃, J_cache...), nothing
575575
end
576+
# --- inequality constraint derivatives ---
577+
old_i_g = copy(mpc.con.i_g)
578+
mpc.con.i_g .= 1 # temporarily set all constraints as finite so g is entirely computed
576579
∇g_cache = (
577580
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
578581
Cache(Û0), Cache(K0), Cache(X̂0),
@@ -600,6 +603,8 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
600603
else
601604
∇²ℓg = nothing
602605
end
606+
mpc.con.i_g .= old_i_g # restore original finite/infinite constraint indices
607+
# --- equality constraint derivatives ---
603608
geq_cache = (
604609
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
605610
Cache(Û0), Cache(K0), Cache(X̂0),

src/estimator/mhe/execute.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ following fields:
9494
- `:D` : measured disturbances over ``N_k``, ``\mathbf{D}``
9595
- `:sol` : solution summary of the optimizer for printing
9696
97+
For [`NonLinModel`](@ref), it also includes the following derivative fields:
98+
99+
- `:JE`: economic cost value at the optimum, ``J_E``
100+
- `:gc`: custom nonlinear constraints values at the optimum, ``\mathbf{g_c}``
101+
- `:∇J` or *`:nablaJ`* : gradient of the objective function, ``\mathbf{\nabla} J``
102+
- `:∇²J` or *`:nabla2J`* : Hessian of the objective function, ``\mathbf{\nabla^2}J``
103+
- `:∇g` or *`:nablag`* : Jacobian of the inequality constraint, ``\mathbf{\nabla g}``
104+
- `:∇²ℓg` or *`:nabla2lg`* : Hessian of the inequality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g}}``
105+
97106
# Examples
98107
```jldoctest
99108
julia> model = LinModel(ss(1.0, 1.0, 1.0, 0, 5.0));
@@ -176,7 +185,7 @@ For [`NonLinModel`](@ref), add the various derivatives.
176185
function addinfo!(
177186
info, estim::MovingHorizonEstimator{NT}, model::NonLinModel
178187
) where NT <:Real
179-
# --- derivatives ---
188+
# --- objective derivatives ---
180189
optim, con = estim.optim, estim.con
181190
nx̂, nym, nŷ, nu, nk = estim.nx̂, estim.nym, model.ny, model.nu, model.nk
182191
He = estim.He
@@ -200,6 +209,9 @@ function addinfo!(
200209
else
201210
∇J, ∇²J = gradient(J!, estim.gradient, estim.Z̃, J_cache...), nothing
202211
end
212+
# --- inequality constraint derivatives ---
213+
old_i_g = copy(estim.con.i_g)
214+
estim.con.i_g .= 1 # temporarily set all constraints as finite so g is entirely computed
203215
∇g_cache = (Cache(V̂), Cache(X̂0), Cache(û0), Cache(k0), Cache(ŷ0))
204216
function g!(g, Z̃, V̂, X̂0, û0, k0, ŷ0)
205217
update_prediction!(V̂, X̂0, û0, k0, ŷ0, g, estim, Z̃)
@@ -219,6 +231,7 @@ function addinfo!(
219231
else
220232
∇²ℓg = nothing
221233
end
234+
estim.con.i_g .= old_i_g # restore original finite/infinite constraint indices
222235
info[:∇J] = ∇J
223236
info[:∇²J] = ∇²J
224237
info[:∇g] = ∇g

0 commit comments

Comments
 (0)