Skip to content

Commit 76b0a12

Browse files
committed
test: simple tests on setmodel! for MHE
1 parent 10dde49 commit 76b0a12

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

src/estimator/mhe/execute.jl

+21-23
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ function con_nonlinprog!(g, estim::MovingHorizonEstimator, ::SimModel, X̂0, V̂
460460
end
461461

462462

463-
"Update the augmented model matrices of `estim` by default."
463+
"Update the augmented model, prediction matrices, constrains and data windows for MHE."
464464
function setmodel_estimator!(
465465
estim::MovingHorizonEstimator, model::LinModel, uop_old, yop_old, dop_old
466466
)
@@ -477,7 +477,6 @@ function setmodel_estimator!(
477477
estim.D̂d .= D̂d
478478
# --- update state estimate and its operating points ---
479479
x̂op_old = copy(estim.x̂op)
480-
X̂op_old = copy(estim.X̂op)
481480
estim.x̂0 .+= estim.x̂op # convert x̂0 to x̂ with the old operating point
482481
estim.x̂op .= x̂op
483482
estim.f̂op .= f̂op
@@ -498,21 +497,20 @@ function setmodel_estimator!(
498497
con.Jx̂ .= Jx̂
499498
con.Bx̂ .= Bx̂
500499
# convert x̃0 to x̃ with the old operating point:
501-
con.x̃0min[end-nx̂+1:end] .-= x̂op_old
502-
con.x̃0max[end-nx̂+1:end] .-= x̂op_old
500+
con.x̃0min[end-nx̂+1:end] .+= x̂op_old
501+
con.x̃0max[end-nx̂+1:end] .+= x̂op_old
503502
# convert X̂0 to X̂ with the old operating point:
504-
con.X̂0min .-= X̂op_old
505-
con.X̂0max .-= X̂op_old
503+
con.X̂0min .+= estim.X̂op
504+
con.X̂0max .+= estim.X̂op
506505
for i in 0:He-1
507506
estim.X̂op[(1+nx̂*i):(nx̂+nx̂*i)] .= estim.x̂op
508507
end
509508
# convert x̃ to x̃0 with the new operating point:
510-
con.x̃0min[end-nx̂+1:end] .+= estim.x̂op
511-
con.x̃0max[end-nx̂+1:end] .+= estim.x̂op
509+
con.x̃0min[end-nx̂+1:end] .-= estim.x̂op
510+
con.x̃0max[end-nx̂+1:end] .-= estim.x̂op
512511
# convert X̂ to X̂0 with the new operating point:
513-
con.X̂0min .+= estim.X̂op
514-
con.X̂0max .+= estim.X̂op
515-
con.A
512+
con.X̂0min .-= estim.X̂op
513+
con.X̂0max .-= estim.X̂op
516514
con.A_X̂min .= A_X̂min
517515
con.A_X̂max .= A_X̂max
518516
con.A_V̂min .= A_V̂min
@@ -531,25 +529,25 @@ function setmodel_estimator!(
531529
# --- data windows ---
532530
for i in 1:He
533531
# convert x̂0 to x̂ with the old operating point:
534-
estim.X̂0[(1+nx̂*(i-1)):(nx̂*i)] .-= x̂op_old
532+
estim.X̂0[(1+nx̂*(i-1)):(nx̂*i)] .+= x̂op_old
535533
# convert ym0 to ym with the old operating point:
536-
estim.Y0m[(1+nym*(i-1)):(nym*i)] .-= @views yop_old[estim.i_ym]
534+
estim.Y0m[(1+nym*(i-1)):(nym*i)] .+= @views yop_old[estim.i_ym]
537535
# convert u0 to u with the old operating point:
538-
estim.U0[(1+nu*(i-1)):(nu*i)] .-= uop_old
536+
estim.U0[(1+nu*(i-1)):(nu*i)] .+= uop_old
539537
# convert d0 to d with the old operating point:
540-
estim.D0[(1+nd*(i-1)):(nd*i)] .-= dop_old
538+
estim.D0[(1+nd*(i-1)):(nd*i)] .+= dop_old
541539
# convert x̂ to x̂0 with the new operating point:
542-
estim.X̂0[(1+nx̂*(i-1)):(nx̂*i)] .+= x̂op
540+
estim.X̂0[(1+nx̂*(i-1)):(nx̂*i)] .-= x̂op
543541
# convert ym to y0m with the new operating point:
544-
estim.Y0m[(1+nym*(i-1)):(nym*i)] .+= @views yop_old[estim.i_ym]
542+
estim.Y0m[(1+nym*(i-1)):(nym*i)] .-= @views model.yop[estim.i_ym]
545543
# convert u to u0 with the new operating point:
546-
estim.U0[(1+nu*(i-1)):(nu*i)] .+= uop_old
544+
estim.U0[(1+nu*(i-1)):(nu*i)] .-= model.uop
547545
# convert d to d0 with the new operating point:
548-
estim.D0[(1+nd*(i-1)):(nd*i)] .+= dop_old
546+
estim.D0[(1+nd*(i-1)):(nd*i)] .-= model.dop
549547
end
550-
estim.Z̃[nϵ+1:+nx̂] .-= x̂op_old
551-
estim.Z̃[nϵ+1:+nx̂] .+= x̂op
552-
estim.x̂0arr_old .-= x̂op_old
553-
estim.x̂0arr_old .+= x̂op
548+
estim.Z̃[nϵ+1:+nx̂] .+= x̂op_old
549+
estim.x̂0arr_old .+= x̂op_old
550+
estim.Z̃[nϵ+1:+nx̂] .-= x̂op
551+
estim.x̂0arr_old .-= x̂op
554552
return nothing
555553
end

test/test_state_estim.jl

+31
Original file line numberDiff line numberDiff line change
@@ -922,4 +922,35 @@ end
922922
= updatestate!(mhe2, [10, 50], [50, 30])
923923
info = getinfo(mhe2)
924924
@test info[:V̂] [-1,-1] atol=5e-2
925+
end
926+
927+
@testset "MovingHorizonEstimator set model" begin
928+
linmodel = LinModel(ss(0.5, 0.3, 1.0, 0, 10.0))
929+
linmodel = setop!(linmodel, uop=[2.0], yop=[50.0], xop=[3.0], fop=[3.0])
930+
mhe = MovingHorizonEstimator(linmodel, He=1, nint_ym=0)
931+
setconstraint!(mhe, x̂min=[-1000], x̂max=[1000])
932+
@test mhe. [0.5]
933+
@test evaloutput(mhe) [50.0]
934+
= updatestate!(mhe, [2.0], [50.0])
935+
@test [3.0]
936+
newlinmodel = LinModel(ss(0.2, 0.3, 1.0, 0, 10.0))
937+
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[3.0], fop=[3.0])
938+
setmodel!(mhe, newlinmodel)
939+
@test mhe. [0.2]
940+
@test evaloutput(mhe) [55.0]
941+
@test mhe.lastu0 [2.0 - 3.0]
942+
@test mhe.U0 [2.0 - 3.0]
943+
@test mhe.Y0m [50.0 - 55.0]
944+
= updatestate!(mhe, [3.0], [55.0])
945+
@test [3.0]
946+
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
947+
setmodel!(mhe, newlinmodel)
948+
@test mhe.x̂0 [3.0 - 8.0]
949+
@test mhe.Z̃[1] 3.0 - 8.0
950+
@test mhe.X̂0 [3.0 - 8.0]
951+
@test mhe.x̂0arr_old [3.0 - 8.0]
952+
@test mhe.con.X̂0min [-1000 - 8.0]
953+
@test mhe.con.X̂0max [+1000 - 8.0]
954+
@test mhe.con.x̃0min [-1000 - 8.0]
955+
@test mhe.con.x̃0max [+1000 - 8.0]
925956
end

0 commit comments

Comments
 (0)