Skip to content

Commit d3a5a4a

Browse files
committed
debug: ℓ_gi signature in MHE
1 parent 2630866 commit d3a5a4a

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/controller/nonlinmpc.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -641,23 +641,23 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J
641641
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
642642
end
643643
Z̃_J = fill(myNaN, nZ̃) # NaN to force update at first call
644-
J_context = (
644+
J_cache = (
645645
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
646646
Cache(Û0), Cache(K0), Cache(X̂0),
647647
Cache(gc), Cache(g), Cache(geq),
648648
)
649-
∇J_prep = prepare_gradient(J!, grad, Z̃_J, J_context...; strict)
649+
∇J_prep = prepare_gradient(J!, grad, Z̃_J, J_cache...; strict)
650650
∇J = Vector{JNT}(undef, nZ̃)
651651
if !isnothing(hess)
652-
∇²J_prep = prepare_hessian(J!, hess, Z̃_J, J_context...; strict)
652+
∇²J_prep = prepare_hessian(J!, hess, Z̃_J, J_cache...; strict)
653653
∇²J = init_diffmat(JNT, hess, ∇²J_prep, nZ̃, nZ̃)
654654
end
655655
update_objective! = if !isnothing(hess)
656656
function (J, ∇J, ∇²J, Z̃_J, Z̃_arg)
657657
if isdifferent(Z̃_arg, Z̃_J)
658658
Z̃_J .= Z̃_arg
659659
J[], _ = value_gradient_and_hessian!(
660-
J!, ∇J, ∇²J, ∇²J_prep, hess, Z̃_J, J_context...
660+
J!, ∇J, ∇²J, ∇²J_prep, hess, Z̃_J, J_cache...
661661
)
662662
end
663663
end
@@ -666,7 +666,7 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J
666666
if isdifferent(Z̃_arg, Z̃_∇J)
667667
Z̃_∇J .= Z̃_arg
668668
J[], _ = value_and_gradient!(
669-
J!, ∇J, ∇J_prep, grad, Z̃_∇J, J_context...
669+
J!, ∇J, ∇J_prep, grad, Z̃_∇J, J_cache...
670670
)
671671
end
672672
end
@@ -774,30 +774,30 @@ function get_nonlincon_oracle(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JN
774774
return dot(λi, gi)
775775
end
776776
Z̃_∇gi = fill(myNaN, nZ̃) # NaN to force update at first call
777-
gi_context = (
777+
gi_cache = (
778778
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
779779
Cache(Û0), Cache(K0), Cache(X̂0),
780780
Cache(gc), Cache(geq), Cache(g)
781781
)
782-
∇gi_prep = prepare_jacobian(gi!, gi, jac, Z̃_∇gi, ∇gi_context...; strict)
782+
∇gi_prep = prepare_jacobian(gi!, gi, jac, Z̃_∇gi, ∇gi_cache...; strict)
783783
∇gi = init_diffmat(JNT, jac, ∇gi_prep, nZ̃, ngi)
784784
∇gi_structure = init_diffstructure(∇gi)
785785
if !isnothing(hess)
786-
∇²gi_context = (
786+
∇²gi_cache = (
787787
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
788788
Cache(Û0), Cache(K0), Cache(X̂0),
789789
Cache(gc), Cache(geq), Cache(g), Cache(gi)
790790
)
791791
∇²gi_prep = prepare_hessian(
792-
ℓ_gi, hess, Z̃_∇gi, Constant(λi), ∇²gi_context...; strict
792+
ℓ_gi, hess, Z̃_∇gi, Constant(λi), ∇²gi_cache...; strict
793793
)
794794
∇²ℓ_gi = init_diffmat(JNT, hess, ∇²gi_prep, nZ̃, nZ̃)
795795
∇²gi_structure = lowertriangle_indices(init_diffstructure(∇²ℓ_gi))
796796
end
797797
function update_con!(gi, ∇gi, Z̃_∇gi, Z̃_arg)
798798
if isdifferent(Z̃_arg, Z̃_∇gi)
799799
Z̃_∇gi .= Z̃_arg
800-
value_and_jacobian!(gi!, gi, ∇gi, ∇gi_prep, jac, Z̃_∇gi, ∇gi_context...)
800+
value_and_jacobian!(gi!, gi, ∇gi, ∇gi_prep, jac, Z̃_∇gi, ∇gi_cache...)
801801
end
802802
return nothing
803803
end
@@ -812,7 +812,7 @@ function get_nonlincon_oracle(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JN
812812
function ∇²gi_func!(∇²ℓ_arg, Z̃_arg, λ_arg)
813813
Z̃_∇gi .= Z̃_arg
814814
λi .= λ_arg
815-
hessian!(ℓ_gi, ∇²ℓ_gi, ∇²gi_prep, hess, Z̃_∇gi, Constant(λi), ∇²gi_context...)
815+
hessian!(ℓ_gi, ∇²ℓ_gi, ∇²gi_prep, hess, Z̃_∇gi, Constant(λi), ∇²gi_cache...)
816816
return diffmat2vec!(∇²ℓ_arg, ∇²ℓ_gi, ∇²gi_structure)
817817
end
818818
gi_min = fill(-myInf, ngi)
@@ -837,30 +837,30 @@ function get_nonlincon_oracle(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JN
837837
return dot(λeq, geq)
838838
end
839839
Z̃_∇geq = fill(myNaN, nZ̃) # NaN to force update at first call
840-
geq_context = (
840+
geq_cache = (
841841
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
842842
Cache(Û0), Cache(K0), Cache(X̂0),
843843
Cache(gc), Cache(g)
844844
)
845-
∇geq_prep = prepare_jacobian(geq!, geq, jac, Z̃_∇geq, ∇geq_context...; strict)
845+
∇geq_prep = prepare_jacobian(geq!, geq, jac, Z̃_∇geq, ∇geq_cache...; strict)
846846
∇geq = init_diffmat(JNT, jac, ∇geq_prep, nZ̃, neq)
847847
∇geq_structure = init_diffstructure(∇geq)
848848
if !isnothing(hess)
849-
∇²geq_context = (
849+
∇²geq_cache = (
850850
Cache(ΔŨ), Cache(x̂0end), Cache(Ue), Cache(Ŷe), Cache(U0), Cache(Ŷ0),
851851
Cache(Û0), Cache(K0), Cache(X̂0),
852852
Cache(gc), Cache(geq), Cache(g)
853853
)
854854
∇²geq_prep = prepare_hessian(
855-
ℓ_geq, hess, Z̃_∇geq, Constant(λeq), ∇²geq_context...; strict
855+
ℓ_geq, hess, Z̃_∇geq, Constant(λeq), ∇²geq_cache...; strict
856856
)
857857
∇²ℓ_geq = init_diffmat(JNT, hess, ∇²geq_prep, nZ̃, nZ̃)
858858
∇²geq_structure = lowertriangle_indices(init_diffstructure(∇²ℓ_geq))
859859
end
860860
function update_con_eq!(geq, ∇geq, Z̃_∇geq, Z̃_arg)
861861
if isdifferent(Z̃_arg, Z̃_∇geq)
862862
Z̃_∇geq .= Z̃_arg
863-
value_and_jacobian!(geq!, geq, ∇geq, ∇geq_prep, jac, Z̃_∇geq, ∇geq_context...)
863+
value_and_jacobian!(geq!, geq, ∇geq, ∇geq_prep, jac, Z̃_∇geq, ∇geq_cache...)
864864
end
865865
return nothing
866866
end
@@ -875,7 +875,7 @@ function get_nonlincon_oracle(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JN
875875
function ∇²geq_func!(∇²ℓ_arg, Z̃_arg, λ_arg)
876876
Z̃_∇geq .= Z̃_arg
877877
λeq .= λ_arg
878-
hessian!(ℓ_geq, ∇²ℓ_geq, ∇²geq_prep, hess, Z̃_∇geq, Constant(λeq), ∇²geq_context...)
878+
hessian!(ℓ_geq, ∇²ℓ_geq, ∇²geq_prep, hess, Z̃_∇geq, Constant(λeq), ∇²geq_cache...)
879879
return diffmat2vec!(∇²ℓ_arg, ∇²ℓ_geq, ∇²geq_structure)
880880
end
881881
geq_min = geq_max = zeros(JNT, neq)

src/estimator/mhe/construct.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,19 +1379,19 @@ function get_nonlinobj_op(
13791379
return obj_nonlinprog!(x̄, estim, model, V̂, Z̃)
13801380
end
13811381
Z̃_J = fill(myNaN, nZ̃) # NaN to force update_predictions! at first call
1382-
J_context = (
1382+
J_cache = (
13831383
Cache(V̂), Cache(X̂0), Cache(û0), Cache(k0), Cache(ŷ0),
13841384
Cache(g),
13851385
Cache(x̄),
13861386
)
13871387
# temporarily "fill" the estimation window for the preparation of the gradient:
13881388
estim.Nk[] = He
1389-
∇J_prep = prepare_gradient(J!, grad, Z̃_J, J_context...; strict)
1389+
∇J_prep = prepare_gradient(J!, grad, Z̃_J, J_cache...; strict)
13901390
estim.Nk[] = 0
13911391
∇J = Vector{JNT}(undef, nZ̃)
13921392
if !isnothing(hess)
13931393
estim.Nk[] = He # see comment above
1394-
∇²J_prep = prepare_hessian(J!, hess, Z̃_J, J_context...; strict)
1394+
∇²J_prep = prepare_hessian(J!, hess, Z̃_J, J_cache...; strict)
13951395
estim.Nk[] = 0
13961396
∇²J = init_diffmat(JNT, hess, ∇²J_prep, nZ̃, nZ̃)
13971397
end
@@ -1400,15 +1400,15 @@ function get_nonlinobj_op(
14001400
if isdifferent(Z̃_arg, Z̃_∇J)
14011401
Z̃_∇J .= Z̃_arg
14021402
J[], _ = value_gradient_and_hessian!(
1403-
J!, ∇J, ∇²J, ∇²J_prep, hess, Z̃_J, J_context...
1403+
J!, ∇J, ∇²J, ∇²J_prep, hess, Z̃_J, J_cache...
14041404
)
14051405
end
14061406
end
14071407
else
14081408
function (J, ∇J, Z̃_∇J, Z̃_arg)
14091409
if isdifferent(Z̃_arg, Z̃_∇J)
14101410
Z̃_∇J .= Z̃_arg
1411-
J[], _ = value_and_gradient!(J!, ∇J, ∇J_prep, grad, Z̃_J, J_context...)
1411+
J[], _ = value_and_gradient!(J!, ∇J, ∇J_prep, grad, Z̃_J, J_cache...)
14121412
end
14131413
end
14141414
end
@@ -1484,28 +1484,28 @@ function get_nonlincon_oracle(
14841484
gi .= @views g[i_g]
14851485
return nothing
14861486
end
1487-
function ℓ_gi(Z̃, V̂, X̂0, û0, k0, ŷ0, g, gi)
1487+
function ℓ_gi(Z̃, λi, V̂, X̂0, û0, k0, ŷ0, g, gi)
14881488
update_prediction!(V̂, X̂0, û0, k0, ŷ0, g, estim, Z̃)
14891489
gi .= @views g[i_g]
14901490
return dot(λi, gi)
14911491
end
14921492
Z̃_∇gi = fill(myNaN, nZ̃) # NaN to force update_predictions! at first call
1493-
gi_context = (
1493+
gi_cache = (
14941494
Cache(V̂), Cache(X̂0), Cache(û0), Cache(k0), Cache(ŷ0), Cache(g)
14951495
)
14961496
# temporarily "fill" the estimation window for the preparation of the gradient:
14971497
estim.Nk[] = He
1498-
∇gi_prep = prepare_jacobian(gi!, gi, jac, Z̃_∇gi, ∇gi_context...; strict)
1498+
∇gi_prep = prepare_jacobian(gi!, gi, jac, Z̃_∇gi, ∇gi_cache...; strict)
14991499
estim.Nk[] = 0
15001500
∇gi = init_diffmat(JNT, jac, ∇gi_prep, nZ̃, ngi)
15011501
∇gi_structure = init_diffstructure(∇gi)
15021502
if !isnothing(hess)
1503-
∇²gi_context = (
1503+
∇²gi_cache = (
15041504
Cache(V̂), Cache(X̂0), Cache(û0), Cache(k0), Cache(ŷ0), Cache(g), Cache(gi)
15051505
)
15061506
estim.Nk[] = He # see comment above
15071507
∇²gi_prep = prepare_hessian(
1508-
ℓ_gi, hess, Z̃_∇gi, Constant(λi), ∇²gi_context...; strict
1508+
ℓ_gi, hess, Z̃_∇gi, Constant(λi), ∇²gi_cache...; strict
15091509
)
15101510
estim.Nk[] = 0
15111511
∇²ℓ_gi = init_diffmat(JNT, hess, ∇²gi_prep, nZ̃, nZ̃)
@@ -1514,7 +1514,7 @@ function get_nonlincon_oracle(
15141514
function update_con!(gi, ∇gi, Z̃_∇gi, Z̃_arg)
15151515
if isdifferent(Z̃_arg, Z̃_∇gi)
15161516
Z̃_∇gi .= Z̃_arg
1517-
value_and_jacobian!(gi!, gi, ∇gi, ∇gi_prep, jac, Z̃_∇gi, ∇gi_context...)
1517+
value_and_jacobian!(gi!, gi, ∇gi, ∇gi_prep, jac, Z̃_∇gi, ∇gi_cache...)
15181518
end
15191519
return nothing
15201520
end
@@ -1529,7 +1529,7 @@ function get_nonlincon_oracle(
15291529
function ∇²gi_func!(∇²ℓ_arg, Z̃_arg, λ_arg)
15301530
Z̃_∇gi .= Z̃_arg
15311531
λi .= λ_arg
1532-
hessian!(ℓ_gi, ∇²ℓ_gi, ∇²gi_prep, hess, Z̃_∇gi, Constant(λi), ∇²gi_context...)
1532+
hessian!(ℓ_gi, ∇²ℓ_gi, ∇²gi_prep, hess, Z̃_∇gi, Constant(λi), ∇²gi_cache...)
15331533
return diffmat2vec!(∇²ℓ_arg, ∇²ℓ_gi, ∇²gi_structure)
15341534
end
15351535
gi_min = fill(-myInf, ngi)

0 commit comments

Comments
 (0)