Skip to content

Commit 3b72edc

Browse files
committed
doc details on AD limitations
1 parent d86ccb2 commit 3b72edc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/controller/nonlinmpc.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ since ``H_c ≤ H_p`` implies that ``\mathbf{u}(k+H_p) = \mathbf{u}(k+H_p-1)``.
140140
Replace any of the 3 arguments with `_` if not needed (see `JE` default value below).
141141
142142
This method uses the default state estimator, an [`UnscentedKalmanFilter`](@ref) with
143-
default arguments.
143+
default arguments.
144+
145+
!!! warning
146+
See Extended Help if you get an error like `MethodError: no method matching
147+
Float64(::ForwardDiff.Dual)`
144148
145149
# Arguments
146150
- `model::SimModel` : model used for controller predictions and state estimations.
@@ -174,6 +178,12 @@ NonLinMPC controller with a sample time Ts = 10.0 s, UnscentedKalmanFilter estim
174178
`NonLinMPC` controllers based on [`LinModel`](@ref) compute the predictions with matrix
175179
algebra instead of a `for` loop. This feature can accelerate the optimization and is not
176180
available in any other package, to my knowledge.
181+
182+
The optimizations rely on [`JuMP.jl`](https://github.com/jump-dev/JuMP.jl) automatic
183+
differentiation (AD) to compute the objective and constraint derivatives. Optimizers
184+
generally benefit from exact derivatives like AD. However, the [`NonLinModel`](@ref) `f`
185+
and `h` functions must be compatible with this feature. See [Automatic differentiation](https://jump.dev/JuMP.jl/stable/manual/nlp/#Automatic-differentiation)
186+
for common mistakes when writing these functions.
177187
"""
178188
NonLinMPC(model::SimModel; kwargs...) = NonLinMPC(UnscentedKalmanFilter(model); kwargs...)
179189

@@ -324,7 +334,7 @@ end
324334
Evaluate the outputs predictions ``\\mathbf{Ŷ}`` when `model` is not a [`LinModel`](@ref).
325335
"""
326336
function evalŶ(mpc::NonLinMPC, model::SimModel, x̂d, d0, D̂0, U0::Vector{T}) where {T}
327-
Ŷd0 = Vector{T}(undef, model.ny*mpc.Hp)
337+
Ŷd0 = Vector{Float64}(undef, model.ny*mpc.Hp)
328338
x̂d::Vector{T} = copy(x̂d)
329339
for j=1:mpc.Hp
330340
u0 = U0[(1 + model.nu*(j-1)):(model.nu*j)]

src/model/nonlinmodel.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ The state update ``\mathbf{f}`` and output ``\mathbf{h}`` functions are defined
3737
manipulated inputs, states, outputs and measured disturbances.
3838
3939
!!! tip
40-
Replace the `d` argument with `_` if `nd = 0` (see Examples below).
40+
Replace the `d` argument with `_` if `nd = 0` (see Examples below).
4141
4242
Nonlinear continuous-time state-space functions are not supported for now. In such a case,
4343
manually call a differential equation solver in the `f` function (e.g.: Euler method).
4444
45+
!!! warning
46+
The `f` and `h` must be pure Julia functions to use the model in [`NonLinMPC`](@ref).
47+
4548
See also [`LinModel`](@ref).
4649
4750
# Examples

0 commit comments

Comments
 (0)