Skip to content

Commit 3c5b01f

Browse files
committed
doc: added some details on the initial state estimate in the estimator docstrings
1 parent a15f0f1 commit 3c5b01f

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

src/estimator/kalman.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ model, which is specified by the numbers of integrator `nint_u` and `nint_ym` (s
9494
Help). Likewise, the covariance matrices are augmented with ``\mathbf{Q̂ = \text{diag}(Q,
9595
Q_{int_u}, Q_{int_{ym}})}`` and ``\mathbf{R̂ = R}``. The matrices ``\mathbf{Ĉ^m, D̂_d^m}`` are
9696
the rows of ``\mathbf{Ĉ, D̂_d}`` that correspond to measured outputs ``\mathbf{y^m}`` (and
97-
unmeasured ones, for ``\mathbf{Ĉ^u, D̂_d^u}``).
97+
unmeasured ones, for ``\mathbf{Ĉ^u, D̂_d^u}``). The Kalman filter will estimate the current
98+
state with the newest measurements ``\mathbf{x̂}_k(k)`` if `direct == true`, else it will
99+
predict the the state of the next time step ``\mathbf{x̂}_k(k+1)``.
98100
99101
# Arguments
100102
!!! info
@@ -331,10 +333,12 @@ end
331333
332334
Construct a time-varying Kalman Filter with the [`LinModel`](@ref) `model`.
333335
334-
The process model is identical to [`SteadyKalmanFilter`](@ref). The matrix ``\mathbf{P̂}_k``
335-
is the estimation error covariance of `model` states augmented with the stochastic ones
336-
(specified by `nint_u` and `nint_ym`). Three keyword arguments modify its initial value with
337-
``\mathbf{P̂}_{-1}(0) = \mathrm{diag}\{ \mathbf{P}(0), \mathbf{P_{int_{u}}}(0), \mathbf{P_{int_{ym}}}(0) \}``.
336+
The process model is identical to [`SteadyKalmanFilter`](@ref). The matrix ``\mathbf{P̂}`` is
337+
the estimation error covariance of `model` states augmented with the stochastic ones
338+
(specified by `nint_u` and `nint_ym`). Three keyword arguments specify its initial value with
339+
``\mathbf{P̂}_{-1}(0) = \mathrm{diag}\{ \mathbf{P}(0), \mathbf{P_{int_{u}}}(0),
340+
\mathbf{P_{int_{ym}}}(0) \}``. The initial state estimate ``\mathbf{x̂}_{-1}(0)`` can be
341+
manually specified with [`setstate!`](@ref), or automatically with [`initstate!`](@ref).
338342
339343
# Arguments
340344
!!! info
@@ -572,7 +576,11 @@ See [`SteadyKalmanFilter`](@ref) for details on ``\mathbf{v}(k), \mathbf{w}(k)``
572576
state-space functions augmented with the stochastic model of the unmeasured disturbances,
573577
which is specified by the numbers of integrator `nint_u` and `nint_ym` (see Extended Help).
574578
The ``\mathbf{ĥ^m}`` function represents the measured outputs of ``\mathbf{ĥ}`` function
575-
(and unmeasured ones, for ``\mathbf{ĥ^u}``).
579+
(and unmeasured ones, for ``\mathbf{ĥ^u}``). The matrix ``\mathbf{P̂}`` is the estimation
580+
error covariance of `model` state augmented with the stochastic ones. Three keyword
581+
arguments specify its initial value with ``\mathbf{P̂}_{-1}(0) =
582+
\mathrm{diag}\{ \mathbf{P}(0), \mathbf{P_{int_{u}}}(0), \mathbf{P_{int_{ym}}}(0) \}``. The
583+
initial state estimate ``\mathbf{x̂}_{-1}(0)`` can be manually specified with [`setstate!`](@ref).
576584
577585
# Arguments
578586
!!! info

src/estimator/mhe/construct.jl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,32 @@ MovingHorizonEstimator estimator with a sample time Ts = 10.0 s, Ipopt optimizer
300300
with ``p=1`` is particularly useful for the MHE since it moves its expensive
301301
computations after the MPC optimization. That is, [`preparestate!`](@ref) will solve the
302302
optimization by default, but it can be postponed to [`updatestate!`](@ref) with
303-
`direct=false`. Note that contrarily to all the other estimators in the module, the MHE
304-
in its current form with ``p=0`` interprets the initial state estimate and covariance as
305-
``\mathbf{x̂}_{-1}(-1)`` and ``\mathbf{P̂}_{-1}(-1)``, that is, an *a posteriori*
306-
estimate[^2] from the last time step.
307-
308-
[^2]: M. Hovd (2012), "A Note On The Smoothing Formulation Of Moving Horizon Estimation",
309-
*Facta Universitatis*, Vol. 11 №2.
303+
`direct=false`.
310304
311305
The Extended Help of [`SteadyKalmanFilter`](@ref) details the augmentation with `nint_ym`
312306
and `nint_u` arguments. The default augmentation scheme is identical, that is `nint_u=0`
313307
and `nint_ym` computed by [`default_nint`](@ref). Note that the constructor does not
314308
validate the observability of the resulting augmented [`NonLinModel`](@ref). In such
315309
cases, it is the user's responsibility to ensure that it is still observable.
316-
317-
The slack variable ``ϵ`` relaxes the constraints if enabled, see [`setconstraint!`](@ref).
318-
It is disabled by default for the MHE (from `Cwt=Inf`) but it should be activated for
319-
problems with two or more types of bounds, to ensure feasibility (e.g. on the estimated
320-
state ``\mathbf{x̂}`` and sensor noise ``\mathbf{v̂}``).
321-
322-
The optimization and the estimation of the covariance at arrival
323-
``\mathbf{P̂}_{k-N_k}(k-N_k+p)`` depend on `model`:
310+
311+
The estimation covariance at arrival ``\mathbf{P̂}_{k-N_k}(k-N_k+p)`` gives an uncertainty
312+
on the state estimate at the beginning of the window ``k-N_k+p``, that is, in the past.
313+
It is not the same as the current estimate covariance ``\mathbf{P̂}_k(k)``, a value not
314+
computed by the MHE (contrarily to e.g. the [`KalmanFilter`](@ref)). Three keyword
315+
arguments specify its initial value with ``\mathbf{P̂_i} = \mathrm{diag}\{ \mathbf{P}(0),
316+
\mathbf{P_{int_{u}}}(0), \mathbf{P_{int_{ym}}}(0) \}``. The initial state estimate
317+
``\mathbf{x̂_i}`` can be manually specified with [`setstate!`](@ref), or automatically
318+
with [`initstate!`](@ref) for [`LinModel`](@ref). Note the MHE with ``p=0`` is slightly
319+
inconsistent with all the other estimators here. It interprets the initial values as
320+
``\mathbf{x̂}_{-1}(-1) = \mathbf{x̂_i}`` and ``\mathbf{P̂}_{-1}(-1) = \mathbf{P̂_i}``, that
321+
is, an *a posteriori* estimate[^2] from the last time step. The MHE with ``p=1`` is
322+
consistent, interpreting them as ``\mathbf{x̂}_{-1}(0) = \mathbf{x̂_i}`` and
323+
``\mathbf{P̂}_{-1}(0) = \mathbf{P̂_i}``.
324+
325+
[^2]: M. Hovd (2012), "A Note On The Smoothing Formulation Of Moving Horizon Estimation",
326+
*Facta Universitatis*, Vol. 11 №2.
327+
328+
The optimization and the update of the arrival covariance depend on `model`:
324329
325330
- If `model` is a [`LinModel`](@ref), the optimization is treated as a quadratic program
326331
with a time-varying Hessian, which is generally cheaper than nonlinear programming. By
@@ -330,10 +335,14 @@ MovingHorizonEstimator estimator with a sample time Ts = 10.0 s, Ipopt optimizer
330335
functions must be compatible with this feature. See [Automatic differentiation](https://jump.dev/JuMP.jl/stable/manual/nlp/#Automatic-differentiation)
331336
for common mistakes when writing these functions. An [`UnscentedKalmanFilter`](@ref)
332337
estimates the arrival covariance by default.
333-
334-
Note that if `Cwt≠Inf`, the attribute `nlp_scaling_max_gradient` of `Ipopt` is set to
335-
`10/Cwt` (if not already set), to scale the small values of ``ϵ``. Use the second
336-
constructor to specify the covariance estimation method.
338+
339+
The slack variable ``ϵ`` relaxes the constraints if enabled, see [`setconstraint!`](@ref).
340+
It is disabled by default for the MHE (from `Cwt=Inf`) but it should be activated for
341+
problems with two or more types of bounds, to ensure feasibility (e.g. on the estimated
342+
state ``\mathbf{x̂}`` and sensor noise ``\mathbf{v̂}``). Note that if `Cwt≠Inf`, the
343+
attribute `nlp_scaling_max_gradient` of `Ipopt` is set to `10/Cwt` (if not already set),
344+
to scale the small values of ``ϵ``. Use the second constructor to specify the covariance
345+
estimation method.
337346
"""
338347
function MovingHorizonEstimator(
339348
model::SM;

0 commit comments

Comments
 (0)