Skip to content

Commit 1e147e0

Browse files
committed
added: number of colors in getinfo for MHE
1 parent 89230f3 commit 1e147e0

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/estimator/mhe/execute.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ For [`NonLinModel`](@ref), it also includes the following fields:
9898
9999
- `:∇J` or *`:nablaJ`* : optimal gradient of the objective function, ``\mathbf{\nabla} J``
100100
- `:∇²J` or *`:nabla2J`* : optimal Hessian of the objective function, ``\mathbf{\nabla^2}J``
101+
- `:∇²J_ncolors` or *`:nabla2J_ncolors`* : number of colors in `:∇²J` sparsity pattern
101102
- `:g` : optimal nonlinear inequality constraint values, ``\mathbf{g}``
102103
- `:∇g` or *`:nablag`* : optimal Jacobian of the inequality constraint, ``\mathbf{\nabla g}``
104+
- `:∇g_ncolors` or *`:nablag_ncolors`* : number of colors in `:∇g` sparsity pattern
103105
- `:∇²ℓg` or *`:nabla2lg`* : optimal Hessian of the inequality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g}}``
106+
- `:∇²ℓg_ncolors` or *`:nabla2lg_ncolors`* : number of colors in `:∇²ℓg` sparsity pattern
104107
105108
Note that the inequality constraint vectors and matrices only include the non-`Inf` values.
106109
@@ -207,9 +210,13 @@ function addinfo!(
207210
return obj_nonlinprog!(x̄, estim, model, V̂, Z̃)
208211
end
209212
if !isnothing(hess)
210-
_, ∇J_opt, ∇²J_opt = value_gradient_and_hessian(J!, hess, estim.Z̃, J_cache...)
213+
prep_∇²J = prepare_hessian(J!, hess, estim.Z̃, J_cache...)
214+
_, ∇J_opt, ∇²J_opt = value_gradient_and_hessian(J!, prep_∇²J, hess, estim.Z̃, J_cache...)
215+
∇²J_ncolors = get_ncolors(prep_∇²J)
211216
else
212-
∇J_opt, ∇²J_opt = gradient(J!, estim.gradient, estim.Z̃, J_cache...), nothing
217+
prep_∇J = prepare_gradient(J!, estim.gradient, estim.Z̃, J_cache...)
218+
∇J_opt = gradient(J!, prep_∇J, estim.gradient, estim.Z̃, J_cache...)
219+
∇²J_opt, ∇²J_ncolors = nothing, nothing
213220
end
214221
# --- inequality constraint derivatives ---
215222
∇g_cache = (Cache(V̂), Cache(X̂0), Cache(û0), Cache(k), Cache(ŷ0), Cache(g))
@@ -218,7 +225,9 @@ function addinfo!(
218225
gi .= @views g[i_g]
219226
return nothing
220227
end
221-
g_opt, ∇g_opt = value_and_jacobian(gi!, gi, estim.jacobian, estim.Z̃, ∇g_cache...)
228+
prep_∇g = prepare_jacobian(gi!, gi, estim.jacobian, estim.Z̃, ∇g_cache...)
229+
g_opt, ∇g_opt = value_and_jacobian(gi!, gi, prep_∇g, estim.jacobian, estim.Z̃, ∇g_cache...)
230+
∇g_ncolors = get_ncolors(prep_∇g)
222231
if !isnothing(hess) && ngi > 0
223232
nonlincon = optim[:nonlinconstraint]
224233
λi = try
@@ -239,25 +248,31 @@ function addinfo!(
239248
)
240249
function ℓ_gi(Z̃, λi, V̂, X̂0, û0, k, ŷ0, g, gi)
241250
update_prediction!(V̂, X̂0, û0, k, ŷ0, g, estim, Z̃)
242-
@show size(g)
243-
@show size(gi)
244251
gi .= @views g[i_g]
245252
return dot(λi, gi)
246253
end
247-
∇²ℓg_opt = hessian(ℓ_gi, hess, estim.Z̃, Constant(λi), ∇²g_cache...)
254+
prep_∇²ℓg = prepare_hessian(ℓ_gi, hess, estim.Z̃, Constant(λi), ∇²g_cache...)
255+
∇²ℓg_opt = hessian(ℓ_gi, prep_∇²ℓg, hess, estim.Z̃, Constant(λi), ∇²g_cache...)
256+
∇²ℓg_ncolors = get_ncolors(prep_∇²ℓg)
248257
else
249-
∇²ℓg_opt = nothing
258+
∇²ℓg_opt, ∇²ℓg_ncolors = nothing, nothing
250259
end
251260
info[:∇J] = ∇J_opt
252261
info[:∇²J] = ∇²J_opt
262+
info[:∇²J_ncolors] = ∇²J_ncolors
253263
info[:g] = g_opt
254264
info[:∇g] = ∇g_opt
265+
info[:∇g_ncolors] = ∇g_ncolors
255266
info[:∇²ℓg] = ∇²ℓg_opt
267+
info[:∇²ℓg_ncolors] = ∇²ℓg_ncolors
256268
# --- non-Unicode fields ---
257269
info[:nablaJ] = ∇J_opt
258270
info[:nabla2J] = ∇²J_opt
271+
info[:nabla2J_ncolors] = ∇²J_ncolors
259272
info[:nablag] = ∇g_opt
273+
info[:nablag_ncolors] = ∇g_ncolors
260274
info[:nabla2lg] = ∇²ℓg_opt
275+
info[:nabla2lg_ncolors] = ∇²ℓg_ncolors
261276
return info
262277
end
263278

0 commit comments

Comments
 (0)