Skip to content

Commit 3686161

Browse files
committed
added: @warn about random values in getinfo!
The sparsity structure is still a useful information IMO, so it is worth it to output the matrices anyway with this warning
1 parent e7bf9d3 commit 3686161

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

src/controller/execute.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ are also available:
117117
- `:∇geq` or *`:nablageq`* : Jacobian of the equality constraint, ``\mathbf{\nabla g_{eq}}``
118118
- `:∇²ℓgeq` or *`:nabla2lgeq`* : Hessian of the equality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g_{eq}}}``
119119
120+
Note that Hessian of Lagrangians are not fully supported yet. Their nonzero coefficients are
121+
random values for now.
122+
120123
# Examples
121124
```jldoctest
122125
julia> mpc = LinMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Hp=1, Hc=1);

src/controller/nonlinmpc.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,11 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
586586
return nothing
587587
end
588588
∇g = jacobian(g!, g, mpc.jacobian, mpc.Z̃, ∇g_cache...)
589-
if !isnothing(mpc.hessian) && any(old_i_g)
589+
if !isnothing(mpc.hessian) && any(old_i_g)
590+
@warn(
591+
"Retrieving optimal Hessian of the Lagrangian is not fully supported yet.\n"*
592+
"Its nonzero coefficients are random values for now.", maxlog=1
593+
)
590594
function ℓ_g(Z̃, λ, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, geq, g)
591595
update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
592596
return dot(λ, g)
@@ -598,7 +602,7 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
598602
)
599603
nonlincon = optim[:nonlinconstraint]
600604
λ = JuMP.dual.(nonlincon) # FIXME: does not work for now
601-
λ = ones(NT, ng)
605+
λ = rand(NT, ng)
602606
∇²ℓg = hessian(ℓ_g, mpc.hessian, mpc.Z̃, Constant(λ), ∇²g_cache...)
603607
else
604608
∇²ℓg = nothing
@@ -616,6 +620,10 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
616620
end
617621
∇geq = jacobian(geq!, geq, mpc.jacobian, mpc.Z̃, geq_cache...)
618622
if !isnothing(mpc.hessian) && con.neq > 0
623+
@warn(
624+
"Retrieving optimal Hessian of the Lagrangian is not fully supported yet.\n"*
625+
"Its nonzero coefficients are random values for now.", maxlog=1
626+
)
619627
∇²geq_cache = (
620628
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
621629
Cache(Û0), Cache(K0), Cache(X̂0),

src/estimator/mhe/execute.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ For [`NonLinModel`](@ref), it also includes the following derivative fields:
103103
- `:∇g` or *`:nablag`* : Jacobian of the inequality constraint, ``\mathbf{\nabla g}``
104104
- `:∇²ℓg` or *`:nabla2lg`* : Hessian of the inequality Lagrangian, ``\mathbf{\nabla^2}\ell_{\mathbf{g}}``
105105
106+
Note that Hessian of Lagrangians are not fully supported yet. Their nonzero coefficients are
107+
random values for now.
108+
106109
# Examples
107110
```jldoctest
108111
julia> model = LinModel(ss(1.0, 1.0, 1.0, 0, 5.0));
@@ -218,7 +221,11 @@ function addinfo!(
218221
return nothing
219222
end
220223
∇g = jacobian(g!, g, estim.jacobian, estim.Z̃, ∇g_cache...)
221-
if !isnothing(estim.hessian) && any(old_i_g)
224+
if !isnothing(estim.hessian) && any(old_i_g)
225+
@warn(
226+
"Retrieving optimal Hessian of the Lagrangian is not fully supported yet.\n"*
227+
"Its nonzero coefficients are random values for now.", maxlog=1
228+
)
222229
∇²g_cache = (Cache(V̂), Cache(X̂0), Cache(û0), Cache(k0), Cache(ŷ0), Cache(g))
223230
function ℓ_g(Z̃, λ, V̂, X̂0, û0, k0, ŷ0, g)
224231
update_prediction!(V̂, X̂0, û0, k0, ŷ0, g, estim, Z̃)

test/2_test_state_estim.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ end
967967
@test info[:Ŷ][end-1:end] [50, 30] atol=1e-9
968968

969969
mhe1c = MovingHorizonEstimator(nonlinmodel, He=2, direct=false, hessian=true)
970+
mhe1c = setconstraint!(mhe1c, v̂min = [-1000, -1000], v̂max = [1000, 1000]) # coverage of getinfo Hessian of Lagrangian
970971
preparestate!(mhe1c, [50, 30], [5])
971972
= updatestate!(mhe1c, [10, 50], [50, 30], [5])
972973
@test zeros(6) atol=1e-9

test/3_test_predictive_control.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,6 @@ end
814814
preparestate!(nmpc4, [0], [0])
815815
u = moveinput!(nmpc4, [0], d, R̂u=fill(12, nmpc4.Hp))
816816
@test u [12] atol=5e-2
817-
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), 0, 0, 3000.0)
818817
nmpc5 = NonLinMPC(nonlinmodel, Hp=1, Hc=1, Cwt=Inf, transcription=MultipleShooting())
819818
nmpc5 = setconstraint!(nmpc5, ymin=[1])
820819
f! = (ẋ,x,u,_,_) -> ẋ .= -0.001x .+ u
@@ -830,6 +829,7 @@ end
830829
preparestate!(nmpc5_1, [0.0])
831830
u = moveinput!(nmpc5_1, [1/0.001])
832831
@test u [1.0] atol=5e-2
832+
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), 0, 0, 3000.0)
833833
nmpc6 = NonLinMPC(linmodel3, Hp=10)
834834
preparestate!(nmpc6, [0])
835835
@test moveinput!(nmpc6, [0]) [0.0] atol=5e-2
@@ -849,6 +849,7 @@ end
849849
@test info[:Ŷ][end] 10 atol=5e-2
850850
transcription = MultipleShooting(f_threads=true, h_threads=true)
851851
nmpc8t = NonLinMPC(nonlinmodel; Nwt=[0], Hp=100, Hc=1, transcription)
852+
nmpc8t = setconstraint!(nmpc8t, ymax=[100], ymin=[-100]) # coverage of getinfo! Hessians of Lagrangian
852853
preparestate!(nmpc8t, [0], [0])
853854
u = moveinput!(nmpc8t, [10], [0])
854855
@test u [2] atol=5e-2

0 commit comments

Comments
 (0)