Skip to content
Closed
52 changes: 12 additions & 40 deletions benchmark/2_bench_state_estim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,6 @@ N = 35;

x_0 = [0.1, 0.1]; x̂_0 = [0, 0, 0]; u = [0.5]

### The MHE without exact Hessians does not work well on the inverted pendulum, commenting them:
# optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
# direct = true
# mhe_pendulum_ipopt_curr = MovingHorizonEstimator(
# model; He, σQ, σR, nint_u, σQint_u, optim, direct
# )
# mhe_pendulum_ipopt_curr = setconstraint!(mhe_pendulum_ipopt_curr; v̂min, v̂max)
# JuMP.unset_time_limit_sec(mhe_pendulum_ipopt_curr.optim)

optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
direct = true
hessian = true
Expand All @@ -322,15 +313,6 @@ mhe_pendulum_ipopt_currhms = MovingHorizonEstimator(
mhe_pendulum_ipopt_currhms = setconstraint!(mhe_pendulum_ipopt_currhms; v̂min, v̂max)
JuMP.unset_time_limit_sec(mhe_pendulum_ipopt_currhms.optim)

# ## The MHE without exact Hessians does not work well on the inverted pendulum, commenting them:
# optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
# direct = false
# mhe_pendulum_ipopt_pred = MovingHorizonEstimator(
# model; He, σQ, σR, nint_u, σQint_u, optim, direct
# )
# mhe_pendulum_ipopt_pred = setconstraint!(mhe_pendulum_ipopt_pred; v̂min, v̂max)
# JuMP.unset_time_limit_sec(mhe_pendulum_ipopt_pred.optim)

optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer,"sb"=>"yes"), add_bridges=false)
direct = false
hessian = true
Expand Down Expand Up @@ -388,52 +370,42 @@ mhe_pendulum_uno_currhms = setconstraint!(mhe_pendulum_uno_currhms; v̂min, v̂m
JuMP.unset_time_limit_sec(mhe_pendulum_uno_currhms.optim)

samples, evals, seconds = 25, 1, 15*60
# CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Current form"] =
# @benchmarkable(
# sim!($mhe_pendulum_ipopt_curr, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
# samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
# )
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Current form (Hessian)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Current form"] =
@benchmarkable(
sim!($mhe_pendulum_ipopt_currh, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Current form (Hessian, MultipleShooting)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Current form (MultipleShooting)"] =
@benchmarkable(
sim!($mhe_pendulum_ipopt_currhms, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
# CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Prediction form"] =
# @benchmarkable(
# sim!($mhe_pendulum_ipopt_pred, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
# samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
# )
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Prediction form (Hessian)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Prediction form"] =
@benchmarkable(
sim!($mhe_pendulum_ipopt_predh, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Prediction form (Hessian, MultipleShooting)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Ipopt"]["Prediction form (MultipleShooting)"] =
@benchmarkable(
sim!($mhe_pendulum_ipopt_predhms, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["MadNLP"]["Current form (Hessian)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["MadNLP"]["Current form"] =
@benchmarkable(
sim!($mhe_pendulum_madnlp_currh, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["MadNLP"]["Prediction form (Hessian)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["MadNLP"]["Prediction form"] =
@benchmarkable(
sim!($mhe_pendulum_madnlp_predh, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Uno"]["Current form (Hessian)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Uno"]["Current form"] =
@benchmarkable(
sim!($mhe_pendulum_uno_currh, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Uno"]["Current form (Hessian, MultipleShooting)"] =
CASE_ESTIM["Pendulum"]["MovingHorizonEstimator"]["Uno"]["Current form (MultipleShooting)"] =
@benchmarkable(
sim!($mhe_pendulum_uno_currhms, $N, $u; plant=$plant, x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
Expand Down Expand Up @@ -486,22 +458,22 @@ mhe_ph_uno_och = setconstraint!(mhe_ph_uno_och, x̂min=[0, 0, 0])
JuMP.unset_time_limit_sec(mhe_ph_uno_och.optim)

samples, evals, seconds = 25, 1, 15*60
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["TrapezoidalCollocation (Hessian)"] =
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["TrapezoidalCollocation"] =
@benchmarkable(
sim!($mhe_ph_ipopt_tch, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["OrthogonalCollocation (Hessian)"] =
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Ipopt"]["OrthogonalCollocation"] =
@benchmarkable(
sim!($mhe_ph_ipopt_och, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["TrapezoidalCollocation (Hessian)"] =
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["TrapezoidalCollocation"] =
@benchmarkable(
sim!($mhe_ph_uno_tch, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
)
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["OrthogonalCollocation (Hessian)"] =
CASE_ESTIM["pH neutral."]["MovingHorizonEstimator"]["Uno"]["OrthogonalCollocation"] =
@benchmarkable(
sim!($mhe_ph_uno_och, $N, $u; x_0=$x_0, x̂_0=$x̂_0, progress=false),
samples=samples, evals=evals, seconds=seconds, setup=GC.gc()
Expand Down
14 changes: 6 additions & 8 deletions docs/src/manual/nonlinmpc2.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,15 @@ time-invariant. More precisely, the concentration of the acid feed is assumed to
disturbed by a random-walk, instead of the measured output. Among all the settings of the
[`MovingHorizonEstimator`](@ref), a proper tuning of the covariance matrices through `σQ`,
`σR` and `σP_0`, and a past horizon `He` long enough to see the main dynamics can improve
the stability on a highly nonlinear and stiff plant model like here. An exact Hessian matrix
with `hessian=true` also helps for DAEs, since the dynamics are encoded in the nonlinear
equality constraints. We can also bound the three estimated states ``\mathbf{x̂}`` to
positive values since they are concentrations in mol/L, and the estimated algebraic
``\mathbf{â}`` to valid pH values:
the stability on a highly nonlinear and stiff plant model like here. We can also bound the
three estimated states ``\mathbf{x̂}`` to positive values since they are concentrations in
mol/L, and the estimated algebraic ``\mathbf{â}`` to valid pH values:

```@example 1
He = 8;
nint_ym=0; nint_u=0; # disable the default stochastic model
He = 8; hessian = true
σQ = [0.5e-3, 0.5e-3, 0.9e-3]; σR=[0.05]; σP_0 = [0.05, 0.05, 1e-3]
mhe = MovingHorizonEstimator(model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0)
mhe = MovingHorizonEstimator(model; nint_ym, nint_u, He, σQ, σR, σP_0)
using JuMP; unset_time_limit_sec(mhe.optim) # no wall time limit during optimization
mhe = setconstraint!(mhe, x̂min=[0, 0, 0], âmin=[1.0], âmax=[14.0])
```
Expand Down Expand Up @@ -384,7 +382,7 @@ predictive control law. The controller regulates the effluent pH to 7.0, with an
deviation of ±1.0. The base flow rate is also constrained between 0 and 20 L/min:

```@example 1
nmpc = NonLinMPC(mhe, Hp=8, Hc=2, Nwt=[5.0], hessian=true)
nmpc = NonLinMPC(mhe, Hp=8, Hc=2, Nwt=[5.0])
using JuMP; unset_time_limit_sec(nmpc.optim) # no wall time limit during optimization
nmpc = setconstraint!(nmpc, ymin=[6], ymax=[8], umin=[0], umax=[20])
```
Expand Down
4 changes: 2 additions & 2 deletions src/estimator/manual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ ManualEstimator estimator with a sample time Ts = 0.5 s:

julia> YandŶ = round.(man_sim(), digits=6)
2×5 Matrix{Float64}:
0.0 0.239713 0.227556 0.157837 0.098629
-0.0 0.238713 0.226556 0.156837 0.097629
0.0 0.239713 0.227556 0.157837 0.098629
0.0 0.238713 0.226556 0.156837 0.097629
```

A second use case is to allow the user to manually provide the state estimate computed
Expand Down
20 changes: 8 additions & 12 deletions src/estimator/mhe/construct.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const DEFAULT_NONLINMHE_HESSIAN = AutoSparse(
AutoForwardDiff();
sparsity_detector=TracerSparsityDetector(),
coloring_algorithm=GreedyColoringAlgorithm(ALL_COLORING_ORDERS, postprocessing=true),
)
const DEFAULT_NONLINMHE_HESSIAN = AutoForwardDiff()

@doc raw"""
Include all the data for the constraints of [`MovingHorizonEstimator`](@ref).
Expand Down Expand Up @@ -366,9 +362,9 @@ at each time step for the optimization.
function when `model` is not a [`LinModel`](@ref), see [`DifferentiationInterface` doc](@extref DifferentiationInterface List).
- `jacobian=default_jacobian(transcription)` : an `AbstractADType` backend for the Jacobian
of the nonlinear constraints, see `gradient` above for the options (default in Extended Help).
- `hessian=false` : an `AbstractADType` backend for the Hessian of the Lagrangian, see
`gradient` above for the options. The default `false` skip it and use the quasi-Newton
method of `optim` (see Extended Help).
- `hessian=true` : an `AbstractADType` backend for the Hessian of the Lagrangian, see
`gradient` above for the options. Use `false` to skip it and use the quasi-Newton
method of `optim` (see Extended Help for the default).
- `covestim=nothing`: a [`StateEstimator`](@ref) object for the arrival covariance estimation
``\mathbf{P̂}_{k-N_k}(k-N_k+p)``, `nothing` means the default choice (see Extended Help).
- `direct=true`: construct with a direct transmission from ``\mathbf{y^m}`` (a.k.a. current
Expand All @@ -385,7 +381,7 @@ MovingHorizonEstimator estimator with a sample time Ts = 5.0 s:
├ transcription: SingleShooting
├ gradient: AutoForwardDiff
├ jacobian: AutoForwardDiff
├ hessian: nothing
├ hessian: AutoSparse (AutoForwardDiff, TracerSparsityDetector, GreedyColoringAlgorithm)
├ arrival covariance: UnscentedKalmanFilter
├ direct: true
└ dimensions:
Expand Down Expand Up @@ -575,7 +571,7 @@ function MovingHorizonEstimator(
optim::JM = default_optim_mhe(model, nc),
gradient::AbstractADType = DEFAULT_GRADIENT,
jacobian::AbstractADType = default_jacobian(transcription),
hessian::Union{AbstractADType, Bool, Nothing} = false,
hessian::Union{AbstractADType, Bool, Nothing} = true,
covestim::Union{StateEstimator, Nothing} = nothing,
direct = true,
σP_0 = sigmaP_0,
Expand Down Expand Up @@ -608,7 +604,7 @@ end
optim=default_optim_mhe(model, nc),
gradient=AutoForwardDiff(),
jacobian=AutoForwardDiff(),
hessian=false,
hessian=true,
covestim=nothing,
direct=true
)
Expand All @@ -629,7 +625,7 @@ function MovingHorizonEstimator(
optim::JM = default_optim_mhe(model, nc),
gradient::AbstractADType = DEFAULT_GRADIENT,
jacobian::AbstractADType = default_jacobian(transcription),
hessian::Union{AbstractADType, Bool, Nothing} = false,
hessian::Union{AbstractADType, Bool, Nothing} = true,
covestim::Union{StateEstimator, Nothing} = nothing,
direct = true
) where {NT<:Real, SM<:SimModel{NT}, JM<:JuMP.GenericModel}
Expand Down
6 changes: 3 additions & 3 deletions test/2_test_state_estim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,8 @@ end
mhe2 = MovingHorizonEstimator(dae; He=2, Cwt=1e4, direct=false, transcription)
preparestate!(mhe2, [0.0], [0.0])
x̂ = updatestate!(mhe2, [0.0], [0.0], [0.0])
@test x̂ ≈ zeros(mhe2.nx̂) atol=1e-5
@test mhe2.x̂0 ≈ zeros(mhe2.nx̂) atol=1e-5
@test x̂ ≈ zeros(mhe2.nx̂) atol=1e-3
@test mhe2.x̂0 ≈ zeros(mhe2.nx̂) atol=1e-3
initstate!(mhe2, [0], [0], [0])
@test mhe2.Z̃[2:2] ≈ mhe2.Z̃[4:4] ≈ mhe2.Z̃[6:6] ≈ xs_0 # xd in X̂0
@test mhe2.Z̃[3:3] ≈ mhe2.Z̃[5:5] ≈ mhe2.Z̃[7:7] ≈ [0.0] # xs in X̂0
Expand Down Expand Up @@ -2152,7 +2152,7 @@ end
@test x̂ ≈ [8.0 + 0.9] atol=1e-3

f(x,u,d,model) = model.A*x + model.Bu*u + model.Bd*d
h(x,d,model) = model.C*x + model.Du*d
h(x,d,model) = model.C*x + model.Dd*d
nonlinmodel = NonLinModel(f, h, 10.0, 1, 1, 1, p=linmodel, solver=nothing)
mhe5 = MovingHorizonEstimator(nonlinmodel; He, nint_ym=0)
setmodel!(mhe5, Q̂=[1e-3], R̂=[1e-6])
Expand Down
Loading