Skip to content

Commit 740258f

Browse files
committed
doc: added jacobian kwarg to ExtendedKalmanFilter
1 parent f1ff9fe commit 740258f

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

src/controller/transcription.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ operating point ``\mathbf{x̂_{op}}`` (see [`augment_model`](@ref)):
4343
```
4444
where ``\mathbf{x̂}_i(k+j)`` is the state prediction for time ``k+j``, estimated by the
4545
observer at time ``i=k`` or ``i=k-1`` depending on its `direct` flag. Note that
46-
``\mathbf{X̂_0 = X̂}`` if the operating points is zero, which is typically the case in
47-
practice for [`NonLinModel`](@ref). This transcription method is generally more efficient
48-
for large control horizon ``H_c``, unstable or highly nonlinear plant models/constraints.
46+
``\mathbf{X̂_0 = X̂}`` if the operating point is zero, which is typically the case in practice
47+
for [`NonLinModel`](@ref). This transcription method is generally more efficient for large
48+
control horizon ``H_c``, unstable or highly nonlinear plant models/constraints.
4949
50-
Sparse optimizers like `OSQP` or `Ipopt` are recommended for this method.
50+
Sparse optimizers like `OSQP` or `Ipopt` and sparse Jacobian computations are recommended
51+
for this transcription method.
5152
"""
5253
struct MultipleShooting <: TranscriptionMethod end
5354

src/estimator/kalman.jl

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,11 @@ This estimator is allocation-free if `model` simulations do not allocate.
624624
disturbances at measured outputs ``\mathbf{Q_{int_{ym}}}`` (composed of integrators).
625625
- `σPint_ym_0=fill(1,sum(nint_ym))` or *`sigmaPint_ym_0`* : same than `σP_0` but for the unmeasured
626626
disturbances at measured outputs ``\mathbf{P_{int_{ym}}}(0)`` (composed of integrators).
627-
- `direct=true`: construct with a direct transmission from ``\mathbf{y^m}`` (a.k.a. current
628-
estimator, in opposition to the delayed/predictor form).
629627
- `α=1e-3` or *`alpha`* : alpha parameter, spread of the state distribution ``(0 < α ≤ 1)``.
630628
- `β=2` or *`beta`* : beta parameter, skewness and kurtosis of the states distribution ``(β ≥ 0)``.
631629
- `κ=0` or *`kappa`* : kappa parameter, another spread parameter ``(0 ≤ κ ≤ 3)``.
630+
- `direct=true`: construct with a direct transmission from ``\mathbf{y^m}`` (a.k.a. current
631+
estimator, in opposition to the delayed/predictor form).
632632
633633
# Examples
634634
```jldoctest
@@ -960,12 +960,41 @@ Both [`LinModel`](@ref) and [`NonLinModel`](@ref) are supported. The process mod
960960
keyword arguments are identical to [`UnscentedKalmanFilter`](@ref), except for `α`, `β` and
961961
`κ` which do not apply to the extended Kalman Filter. The Jacobians of the augmented model
962962
``\mathbf{f̂, ĥ}`` are computed with [`ForwardDiff`](@extref ForwardDiff) automatic
963-
differentiation. This estimator allocates memory for the Jacobians.
964-
963+
differentiation. This estimator is allocation-free if `model` simulations do not allocate.
965964
!!! warning
966965
See the Extended Help of [`linearize`](@ref) function if you get an error like:
967966
`MethodError: no method matching (::var"##")(::Vector{ForwardDiff.Dual})`.
968967
968+
# Arguments
969+
!!! info
970+
Keyword arguments with *`emphasis`* are non-Unicode alternatives.
971+
972+
- `model::SimModel` : (deterministic) model for the estimations.
973+
- `i_ym=1:model.ny` : `model` output indices that are measured ``\mathbf{y^m}``, the rest
974+
are unmeasured ``\mathbf{y^u}``.
975+
- `σP_0=fill(1/model.nx,model.nx)` or *`sigmaP_0`* : main diagonal of the initial estimate
976+
covariance ``\mathbf{P}(0)``, specified as a standard deviation vector.
977+
- `σQ=fill(1/model.nx,model.nx)` or *`sigmaQ`* : main diagonal of the process noise
978+
covariance ``\mathbf{Q}`` of `model`, specified as a standard deviation vector.
979+
- `σR=fill(1,length(i_ym))` or *`sigmaR`* : main diagonal of the sensor noise covariance
980+
``\mathbf{R}`` of `model` measured outputs, specified as a standard deviation vector.
981+
- `nint_u=0`: integrator quantity for the stochastic model of the unmeasured disturbances at
982+
the manipulated inputs (vector), use `nint_u=0` for no integrator (see Extended Help).
983+
- `nint_ym=default_nint(model,i_ym,nint_u)` : same than `nint_u` but for the unmeasured
984+
disturbances at the measured outputs, use `nint_ym=0` for no integrator (see Extended Help).
985+
- `σQint_u=fill(1,sum(nint_u))` or *`sigmaQint_u`* : same than `σQ` but for the unmeasured
986+
disturbances at manipulated inputs ``\mathbf{Q_{int_u}}`` (composed of integrators).
987+
- `σPint_u_0=fill(1,sum(nint_u))` or *`sigmaPint_u_0`* : same than `σP_0` but for the unmeasured
988+
disturbances at manipulated inputs ``\mathbf{P_{int_u}}(0)`` (composed of integrators).
989+
- `σQint_ym=fill(1,sum(nint_ym))` or *`sigmaQint_u`* : same than `σQ` for the unmeasured
990+
disturbances at measured outputs ``\mathbf{Q_{int_{ym}}}`` (composed of integrators).
991+
- `σPint_ym_0=fill(1,sum(nint_ym))` or *`sigmaPint_ym_0`* : same than `σP_0` but for the unmeasured
992+
disturbances at measured outputs ``\mathbf{P_{int_{ym}}}(0)`` (composed of integrators).
993+
- `jacobian=AutoForwardDiff()`: an `AbstractADType` backend for the Jacobians of the augmented
994+
model, see [`DifferentiationInterface` doc](@extref DifferentiationInterface List).
995+
- `direct=true`: construct with a direct transmission from ``\mathbf{y^m}`` (a.k.a. current
996+
estimator, in opposition to the delayed/predictor form).
997+
969998
# Examples
970999
```jldoctest
9711000
julia> model = NonLinModel((x,u,_,_)->0.2x+u, (x,_,_)->-3x, 5.0, 1, 1, 1, solver=nothing);
@@ -1035,11 +1064,15 @@ end
10351064
10361065
Return the `linfunc!` function that computes the Jacobians of the augmented model.
10371066
1038-
The function has the following signature:
1067+
The function has the two following methods:
10391068
```
1040-
linfunc!(x̂0next, ŷ0, F̂, Ĥ, backend, x̂0, cst_u0, cst_d0) -> nothing
1069+
linfunc!(x̂0next , ::Nothing, F̂ , ::Nothing, backend, x̂0, cst_u0, cst_d0) -> nothing
1070+
linfunc!(::Nothing, ŷ0 , ::Nothing, Ĥ , backend, x̂0, _ , cst_d0) -> nothing
10411071
```
1042-
#TODO: continue here
1072+
To respectively compute only `F̂` or `Ĥ` Jacobian. The methods mutate all the arguments
1073+
before `backend` argument. The `backend` argument is an `AbstractADType` object from
1074+
`DifferentiationInterface`. The `cst_u0` and `cst_d0` are `DifferentiationInterface.Constant`
1075+
objects with the linearization points.
10431076
"""
10441077
function get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
10451078
As, Cs_u, Cs_y = init_estimstoch(model, i_ym, nint_u, nint_ym)
@@ -1056,13 +1089,6 @@ function get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
10561089
cst_d0 = Constant(zeros(NT, nd))
10571090
F̂_prep = prepare_jacobian(f̂_ekf!, x̂0next, jacobian, x̂0, tmp_û0, cst_u0, cst_d0; strict)
10581091
Ĥ_prep = prepare_jacobian(ĥ_ekf!, ŷ0, jacobian, x̂0, cst_d0; strict)
1059-
# main method to compute both Jacobians, it mutates all args before `backend`:
1060-
function linfunc!(x̂0next, ŷ0, F̂, Ĥ, backend, x̂0, cst_u0, cst_d0)
1061-
jacobian!(f̂_ekf!, x̂0next, F̂, F̂_prep, backend, x̂0, tmp_û0, cst_u0, cst_d0)
1062-
jacobian!(ĥ_ekf!, ŷ0, Ĥ, Ĥ_prep, backend, x̂0, cst_d0)
1063-
return nothing
1064-
end
1065-
# two additional methods to only compute one of the two Jacobians at a time:
10661092
function linfunc!(x̂0next, ŷ0::Nothing, F̂, Ĥ::Nothing, backend, x̂0, cst_u0, cst_d0)
10671093
return jacobian!(f̂_ekf!, x̂0next, F̂, F̂_prep, backend, x̂0, tmp_û0, cst_u0, cst_d0)
10681094
end
@@ -1102,8 +1128,8 @@ augmented process model:
11021128
\end{aligned}
11031129
```
11041130
The matrix ``\mathbf{Ĥ^m}`` is the rows of ``\mathbf{Ĥ}`` that are measured outputs. The
1105-
Jacobians are computed with [`ForwardDiff`](@extref ForwardDiff). The correction and
1106-
prediction step equations are provided below. The correction step is skipped if
1131+
Jacobians are computed with [`ForwardDiff`](@extref ForwardDiff) bu default. The correction
1132+
and prediction step equations are provided below. The correction step is skipped if
11071133
`estim.direct == true` since it's already done by the user.
11081134
11091135
# Correction Step

src/model/linearization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The function has the following signature:
88
linfunc!(xnext, y, A, Bu, C, Bd, Dd, backend, x, u, d, cst_x, cst_u, cst_d) -> nothing
99
```
1010
and it should modifies in-place all the arguments before `backend`. The `backend` argument
11-
is an `AbstractADType` backend from `DifferentiationInterface`. The `cst_x`, `cst_u` and
11+
is an `AbstractADType` object from `DifferentiationInterface`. The `cst_x`, `cst_u` and
1212
`cst_d` are `DifferentiationInterface.Constant` objects with the linearization points.
1313
"""
1414
function get_linearization_func(NT, f!, h!, nu, nx, ny, nd, p, backend)

0 commit comments

Comments
 (0)