Skip to content

Commit 55b7999

Browse files
authored
Merge pull request #419 from JuliaControl/debug_setmodel_mhe_ms
debug: update defect matrices in `setmodel!` for `MovingHorizonEstimator` and `LinModel`
2 parents 2ef27c3 + 4e57c2b commit 55b7999

4 files changed

Lines changed: 94 additions & 46 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
3-
version = "2.9.2"
3+
version = "2.9.3"
44
authors = ["Francis Gagnon"]
55

66
[deps]

‎src/controller/execute.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ function setmodel_controller!(mpc::PredictiveController, uop_old, x̂op_old)
770770
]
771771
# --- linear equality constraints ---
772772
con.Aeq .= Aeq
773-
# --- operating points ---
773+
# --- bound constraint operating points ---
774774
con.U0min .+= mpc.Uop # convert U0 to U with the old operating point
775775
con.U0max .+= mpc.Uop # convert U0 to U with the old operating point
776776
con.Y0min .+= mpc.Yop # convert Y0 to Y with the old operating point

‎src/estimator/mhe/execute.jl‎

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -924,26 +924,22 @@ function setmodel_estimator!(
924924
estim.G .= G
925925
estim.J .= J
926926
estim.B .= B
927-
# --- linear inequality constraints ---
927+
# --- state estimate constraints ---
928928
con.ẼX̂ .= ẼX̂
929929
con.GX̂ .= GX̂
930930
con.JX̂ .= JX̂
931931
con.BX̂ .= BX̂
932-
# convert x̂0 to x̂ with the old operating point:
933-
con.x̂0min .+= x̂op_old
934-
con.x̂0max .+= x̂op_old
935-
# convert X̂0 to X̂ with the old operating point:
936-
con.X̂0min .+= estim.X̂op
937-
con.X̂0max .+= estim.X̂op
938-
for i in 0:He-1
939-
estim.X̂op[(1+nx̂*i):(nx̂+nx̂*i)] .= estim.x̂op
940-
end
941-
# convert x̂ to x̂0 with the new operating point:
942-
con.x̂0min .-= estim.x̂op
943-
con.x̂0max .-= estim.x̂op
944-
# convert X̂ to X̂0 with the new operating point:
945-
con.X̂0min .-= estim.X̂op
946-
con.X̂0max .-= estim.X̂op
932+
# --- defect matrices ---
933+
ES, GS, JS, BS = init_defectmat_mhe(
934+
model, transcription, He, estim.Â, estim.B̂u, estim.B̂d, estim.x̂op, estim.f̂op,
935+
estim.direct
936+
)
937+
Aeq, ẼS = augmentdefect(ES, nε; slackfirst=true)
938+
con.ẼS .= ẼS
939+
con.GS .= GS
940+
con.JS .= JS
941+
con.BS .= BS
942+
# --- linear inequality constraints ---
947943
con.A_X̂min .= A_X̂min
948944
con.A_X̂max .= A_X̂max
949945
con.A_V̂min .= A_V̂min
@@ -958,26 +954,21 @@ function setmodel_estimator!(
958954
con.A_V̂min
959955
con.A_V̂max
960956
]
961-
Z̃min, Z̃max = init_boxconstraint_mhe(
962-
model, transcription, He, nx̂, nŵ, nε,
963-
con.x̂0min, con.x̂0max, con.X̂0min, con.X̂0max, con.Ŵmin, con.Ŵmax,
964-
con.A_x̂min, con.A_x̂max, con.C_x̂min, con.C_x̂max, con.A_Ŵmin, con.A_Ŵmax
965-
)
966-
con.Z̃min .= Z̃min
967-
con.Z̃max .= Z̃max
968-
Z̃var::Vector{JuMP.VariableRef} = estim.optim[:Z̃var]
969-
A = con.A[con.i_b, :]
970-
b = zeros(count(con.i_b)) # dummy value, updated before optimization (avoid ±Inf)
971-
# deletion is required for sparse solvers like OSQP, when the sparsity pattern changes
972-
JuMP.delete(estim.optim, estim.optim[:linconstraint])
973-
JuMP.unregister(estim.optim, :linconstraint)
974-
@constraint(estim.optim, linconstraint, A*Z̃var .≤ b)
975-
for i in eachindex(Z̃var)
976-
# deletion not required here since changing op. pts won't change finite status
977-
!isinf(Z̃min[i]) && JuMP.set_lower_bound(Z̃var[i], Z̃min[i])
978-
!isinf(Z̃max[i]) && JuMP.set_upper_bound(Z̃var[i], Z̃max[i])
957+
# --- linear equality constraints ---
958+
con.Aeq .= Aeq
959+
# --- bound constraint operating points ---
960+
con.x̂0min .+= x̂op_old # convert x̂0 to x̂ with the old operating point
961+
con.x̂0max .+= x̂op_old # convert x̂0 to x̂ with the old operating point
962+
con.X̂0min .+= estim.X̂op # convert X̂0 to X̂ with the old operating point
963+
con.X̂0max .+= estim.X̂op # convert X̂0 to X̂ with the old operating point
964+
for i in 0:He-1
965+
estim.X̂op[(1+nx̂*i):(nx̂+nx̂*i)] .= estim.x̂op
979966
end
980-
# --- data windows ---
967+
con.x̂0min .-= estim.x̂op # convert x̂ to x̂0 with the new operating point
968+
con.x̂0max .-= estim.x̂op # convert x̂ to x̂0 with the new operating point
969+
con.X̂0min .-= estim.X̂op # convert X̂ to X̂0 with the new operating point
970+
con.X̂0max .-= estim.X̂op # convert X̂ to X̂0 with the new operating point
971+
# --- data windows operating points ---
981972
for i in 1:He
982973
# convert y0m to ym with the old operating point:
983974
estim.Y0m[(1+nym*(i-1)):(nym*i)] .+= @views yop_old[estim.i_ym]
@@ -1002,6 +993,32 @@ function setmodel_estimator!(
1002993
estim.lastu0 .-= model.uop
1003994
estim.Z̃[nε+1:nε+nx̂] .-= x̂op
1004995
estim.x̂0arr_old .-= x̂op
996+
# --- box constraints ---
997+
Z̃min, Z̃max = init_boxconstraint_mhe(
998+
model, transcription, He, nx̂, nŵ, nε,
999+
con.x̂0min, con.x̂0max, con.X̂0min, con.X̂0max, con.Ŵmin, con.Ŵmax,
1000+
con.A_x̂min, con.A_x̂max, con.C_x̂min, con.C_x̂max, con.A_Ŵmin, con.A_Ŵmax
1001+
)
1002+
con.Z̃min .= Z̃min
1003+
con.Z̃max .= Z̃max
1004+
# --- JuMP optimization ---
1005+
Z̃var::Vector{JuMP.VariableRef} = estim.optim[:Z̃var]
1006+
A = con.A[con.i_b, :]
1007+
b = zeros(count(con.i_b)) # dummy value, updated before optimization (avoid ±Inf)
1008+
# deletion is required for sparse solvers like OSQP, when the sparsity pattern changes
1009+
JuMP.delete(estim.optim, estim.optim[:linconstraint])
1010+
JuMP.unregister(estim.optim, :linconstraint)
1011+
@constraint(estim.optim, linconstraint, A*Z̃var .≤ b)
1012+
if haskey(estim.optim, :linconstrainteq) # the key will be absent when Nk < He
1013+
JuMP.delete(estim.optim, estim.optim[:linconstrainteq])
1014+
JuMP.unregister(estim.optim, :linconstrainteq)
1015+
@constraint(estim.optim, linconstrainteq, con.Aeq*Z̃var .== con.beq)
1016+
end
1017+
for i in eachindex(Z̃var)
1018+
# deletion not required here since changing op. pts won't change finite status
1019+
!isinf(Z̃min[i]) && JuMP.set_lower_bound(Z̃var[i], Z̃min[i])
1020+
!isinf(Z̃max[i]) && JuMP.set_upper_bound(Z̃var[i], Z̃max[i])
1021+
end
10051022
# --- covariance matrices ---
10061023
if !isnothing(Q̂)
10071024
estim.cov.Q̂ .= to_hermitian(Q̂)

‎test/2_test_state_estim.jl‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ end
16411641

16421642
@testitem "MHE set model" setup=[SetupMPCtests] begin
16431643
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
1644-
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
1644+
linmodel = LinModel([0.5], [0.3], [1.0], 0, 0, 10.0)
16451645
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
16461646
He = 5
16471647
mhe = MovingHorizonEstimator(linmodel; He, nint_ym=0, direct=false)
@@ -1651,7 +1651,7 @@ end
16511651
preparestate!(mhe, [50.0])
16521652
x̂ = updatestate!(mhe, [2.0], [50.0])
16531653
@test x̂ ≈ [3.0]
1654-
newlinmodel = LinModel(ss(0.2, 0.3, 1.0, 0, 10.0))
1654+
newlinmodel = LinModel([0.2], [0.3], [1.0], 0, 0, 10.0)
16551655
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[3.0], fop=[3.0])
16561656
setmodel!(mhe, newlinmodel)
16571657
@test mhe. ≈ [0.2]
@@ -1676,18 +1676,49 @@ end
16761676
@test mhe.cov.invQ̂_He ≈ diagm(repeat([1e3], He))
16771677
@test mhe.cov.R̂ ≈ [1e-6]
16781678
@test mhe.cov.invR̂_He ≈ diagm(repeat([1e6], He))
1679+
1680+
linmodel = LinModel([0.5], [0.3], [1.0], 0, 0, 10.0)
1681+
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
1682+
mhe2 = MovingHorizonEstimator(
1683+
linmodel; He, nint_ym=0, direct=false, transcription=SingleShooting()
1684+
)
1685+
x̂ = updatestate!(mhe2, [2.0 + 1.0], [50.0])
1686+
@test x̂ ≈ [3.0 + 0.3] atol=1e-3
1687+
newlinmodel = LinModel([0.5], [0.9], [1.0], 0, 0, 10.0)
1688+
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
1689+
setmodel!(mhe2, newlinmodel)
1690+
initstate!(mhe2, [3.0], [55.0])
1691+
x̂ = updatestate!(mhe2, [3.0 + 1.0], [55.0])
1692+
@test x̂ ≈ [8.0 + 0.9] atol=1e-3
1693+
1694+
linmodel = LinModel([0.5], [0.3], [1.0], 0, 0, 10.0)
1695+
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
1696+
mhe3 = MovingHorizonEstimator(
1697+
linmodel; He, nint_ym=0, direct=false, transcription=MultipleShooting()
1698+
)
1699+
x̂ = updatestate!(mhe3, [2.0 + 1.0], [50.0])
1700+
@test x̂ ≈ [3.0 + 0.3] atol=1e-3
1701+
newlinmodel = LinModel([0.5], [0.9], [1.0], 0, 0, 10.0)
1702+
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
1703+
setmodel!(mhe3, newlinmodel)
1704+
initstate!(mhe3, [3.0], [55.0])
1705+
x̂ = updatestate!(mhe3, [3.0 + 1.0], [55.0])
1706+
@test x̂ ≈ [8.0 + 0.9] atol=1e-3
1707+
16791708
f(x,u,d,model) = model.A*x + model.Bu*u + model.Bd*d
16801709
h(x,d,model) = model.C*x + model.Du*d
16811710
nonlinmodel = NonLinModel(f, h, 10.0, 1, 1, 1, p=linmodel, solver=nothing)
1682-
mhe2 = MovingHorizonEstimator(nonlinmodel; He, nint_ym=0)
1683-
setmodel!(mhe2, Q̂=[1e-3], R̂=[1e-6])
1684-
@test mhe2.cov.Q̂ ≈ [1e-3]
1685-
@test mhe2.cov.invQ̂_He ≈ diagm(repeat([1e3], He))
1686-
@test mhe2.cov.R̂ ≈ [1e-6]
1687-
@test mhe2.cov.invR̂_He ≈ diagm(repeat([1e6], He))
1688-
@test_throws ErrorException setmodel!(mhe2, deepcopy(nonlinmodel))
1711+
mhe5 = MovingHorizonEstimator(nonlinmodel; He, nint_ym=0)
1712+
setmodel!(mhe5, Q̂=[1e-3], R̂=[1e-6])
1713+
@test mhe5.cov.Q̂ ≈ [1e-3]
1714+
@test mhe5.cov.invQ̂_He ≈ diagm(repeat([1e3], He))
1715+
@test mhe5.cov.R̂ ≈ [1e-6]
1716+
@test mhe5.cov.invR̂_He ≈ diagm(repeat([1e6], He))
1717+
1718+
@test_throws ErrorException setmodel!(mhe5, deepcopy(nonlinmodel))
16891719
@test_throws ErrorException setmodel!(mhe, Q̂=diagm([-0.1]))
16901720
@test_throws ErrorException setmodel!(mhe, R̂=diagm([-0.1]))
1721+
16911722
end
16921723

16931724
@testitem "MHE v.s. Kalman filters" setup=[SetupMPCtests] begin

0 commit comments

Comments
 (0)