Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/src/manual/nonlinmpc1.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ nmpc = setconstraint!(nmpc; umin, umax)

The option `Cwt=Inf` disables the slack variable `ϵ` for constraint softening. By default,
[`NonLinMPC`](@ref) controllers use [`Ipopt`](https://coin-or.github.io/Ipopt/) and a direct
[`SingleShooting`](@ref) transcription method to solve the optimal control problem. We test
`mpc` performance on `plant` by imposing an angular setpoint of 180° (inverted position):
[`MultipleShooting`](@ref) transcription method to solve the optimal control problem. We
test `mpc` performance on `plant` by imposing an angular setpoint of 180° (inverted position):

```@example man_nonlin
using JuMP; unset_time_limit_sec(nmpc.optim) # hide
Expand Down Expand Up @@ -303,7 +303,7 @@ We construct the controller by enabling relaxation with the `Cwt` argument, and
specifying the number of custom inequality constraints `nc`:

```@example man_nonlin
Cwt, Pmax, nc = 1e5, 3, Hp+1
Cwt, Pmax, nc = 5e4, 3, Hp+1
nmpc2 = NonLinMPC(estim2; Hp, Hc, Nwt=Nwt, Mwt=[0.5, 0], Cwt, gc!, nc, p=Pmax)
nmpc2 = setconstraint!(nmpc2; umin, umax)
using JuMP; unset_time_limit_sec(nmpc2.optim) # hide
Expand Down Expand Up @@ -359,7 +359,7 @@ A [`SteadyKalmanFilter`](@ref) and a [`LinMPC`](@ref) are designed from `linmode
```@example man_nonlin
skf = SteadyKalmanFilter(linmodel; σQ, σR, nint_u, σQint_u)
mpc = LinMPC(skf; Hp, Hc, Mwt, Nwt, Cwt=Inf)
mpc = setconstraint!(mpc, umin=[-1.5], umax=[+1.5])
mpc = setconstraint!(mpc; umin, umax)
```

The linear controller satisfactorily rejects the 10° step disturbance:
Expand Down
54 changes: 29 additions & 25 deletions src/controller/nonlinmpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ subject to [`setconstraint!`](@ref) bounds, and the custom inequality constraint
```math
\mathbf{g_c}(\mathbf{U_e, Ŷ_e, D̂_e, p}, ϵ) ≤ \mathbf{0}
```
with the decision variables ``\mathbf{Z}`` and slack ``ϵ``. By default, a [`SingleShooting`](@ref)
transcription method is used, hence ``\mathbf{Z=ΔU}``. The economic function ``J_E`` can
penalizes solutions with high economic costs. Setting all the weights to 0 except ``E``
creates a pure economic model predictive controller (EMPC). As a matter of fact, ``J_E`` can
be any nonlinear function as a custom objective, even if there is no economic interpretation
to it. The arguments of ``J_E`` and ``\mathbf{g_c}`` include the manipulated inputs,
predicted outputs and measured disturbances, extended from ``k`` to ``k+H_p`` (inclusively,
see Extended Help for more details):
with the decision variables ``\mathbf{Z}`` and slack ``ϵ``. The transcription method defines
the content of ``\mathbf{Z}`` (default to [`MultipleShooting`](@ref)). The economic function
``J_E`` can penalizes solutions with high economic costs. Setting all the weights to zero
except ``E`` creates a pure economic model predictive controller (EMPC). As a matter of
fact, ``J_E`` can be any nonlinear function as a custom objective, even if there is no
economic interpretation to it. The arguments of ``J_E`` and ``\mathbf{g_c}`` include the
manipulated inputs, predicted outputs and measured disturbances, extended from ``k`` to
``k+H_p`` (inclusively, see Extended Help for more details):
```math
\mathbf{U_e} = \begin{bmatrix} \mathbf{U} \\ \mathbf{u}(k+H_p-1) \end{bmatrix} , \quad
\mathbf{Ŷ_e} = \begin{bmatrix} \mathbf{ŷ}(k) \\ \mathbf{Ŷ} \end{bmatrix} , \quad
Expand Down Expand Up @@ -232,7 +232,7 @@ This controller allocates memory at each time step for the optimization.
- `nc=0` : number of custom nonlinear inequality constraints.
- `p=model.p` : ``J_E`` and ``\mathbf{g_c}`` functions parameter ``\mathbf{p}`` (any type).
- `transcription=default_transcription(model)` : a [`TranscriptionMethod`](@ref) object,
default to [`SingleShooting`](@ref) or `model.transcription` for [`NonLinModelDAE`](@ref).
default to [`MultipleShooting`](@ref) or `model.transcription` for [`NonLinModelDAE`](@ref).
- `optim=JuMP.Model(Ipopt.Optimizer)` : nonlinear optimizer used in the predictive
controller, provided as a [`JuMP.Model`](@extref) object (default to [`Ipopt`](https://github.com/jump-dev/Ipopt.jl) optimizer).
- `gradient=AutoForwardDiff()` : an `AbstractADType` backend for the gradient of the objective
Expand All @@ -249,14 +249,14 @@ This controller allocates memory at each time step for the optimization.
```jldoctest
julia> model = NonLinModel((x,u,_,_)->0.5x+u, (x,_,_)->x, 5, 1, 1, 1, solver=nothing);

julia> mpc = NonLinMPC(model, Hp=20, Hc=10, transcription=MultipleShooting())
julia> mpc = NonLinMPC(model, Hp=20, Hc=10, transcription=SingleShooting())
NonLinMPC controller with a sample time Ts = 5.0 s:
├ estimator: UnscentedKalmanFilter
├ model: NonLinModel
├ optimizer: Ipopt
├ transcription: MultipleShooting
├ optimizer: Ipopt
├ transcription: SingleShooting
├ gradient: AutoForwardDiff
├ jacobian: AutoSparse (AutoForwardDiff, TracerSparsityDetector, GreedyColoringAlgorithm)
├ jacobian: AutoForwardDiff
├ hessian: nothing
└ dimensions:
│ ├ 20 prediction steps Hp
Expand All @@ -267,11 +267,11 @@ NonLinMPC controller with a sample time Ts = 5.0 s:
│ ├ 0 unmeasured outputs yu
│ └ 0 measured disturbances d
└ optimization:
├ 51 decision variables Z̃ (1 slack variable, 1 bounds)
├ 11 decision variables Z̃ (1 slack variable, 1 bounds)
├ 0 linear inequality constraints A (0 custom)
├ 20 linear equality constraints Aeq
├ 0 linear equality constraints Aeq
├ 0 nonlinear inequality constraints g (0 custom)
└ 20 nonlinear equality constraints geq
└ 0 nonlinear equality constraints geq
```

# Extended Help
Expand Down Expand Up @@ -398,10 +398,10 @@ julia> mpc = NonLinMPC(estim, Hp=20, Cwt=1e4)
NonLinMPC controller with a sample time Ts = 5.0 s:
├ estimator: UnscentedKalmanFilter
├ model: NonLinModel
├ optimizer: Ipopt
├ transcription: SingleShooting
├ optimizer: Ipopt
├ transcription: MultipleShooting
├ gradient: AutoForwardDiff
├ jacobian: AutoForwardDiff
├ jacobian: AutoSparse (AutoForwardDiff, TracerSparsityDetector, GreedyColoringAlgorithm)
├ hessian: nothing
└ dimensions:
│ ├ 20 prediction steps Hp
Expand All @@ -412,11 +412,11 @@ NonLinMPC controller with a sample time Ts = 5.0 s:
│ ├ 0 unmeasured outputs yu
│ └ 0 measured disturbances d
└ optimization:
├ 3 decision variables Z̃ (1 slack variable, 1 bounds)
├ 0 linear inequality constraints A (0 custom)
├ 0 linear equality constraints Aeq
├ 0 nonlinear inequality constraints g (0 custom)
└ 0 nonlinear equality constraints geq
├ 43 decision variables Z̃ (1 slack variable, 1 bounds)
├ 0 linear inequality constraints A (0 custom)
├ 20 linear equality constraints Aeq
├ 0 nonlinear inequality constraints g (0 custom)
└ 20 nonlinear equality constraints geq
```
"""
function NonLinMPC(
Expand Down Expand Up @@ -464,7 +464,11 @@ end

default_estimator(model::SimModelODE; kwargs...) = UnscentedKalmanFilter(model; kwargs...)
default_estimator(model::SimModelDAE; kwargs...) = MovingHorizonEstimator(model; kwargs...)
default_estimator(model::LinModel; kwargs...) = SteadyKalmanFilter(model; kwargs...)
default_estimator(model::LinModel; kwargs...) = SteadyKalmanFilter(model; kwargs...)

"Default to `MultipleShooting()`, or `model.transcription` if `model` is a `NonLinModelDAE`."
default_transcription(::SimModel) = MultipleShooting()
default_transcription(model::NonLinModelDAE) = model.transcription

"""
validate_JE(NT, JE) -> nothing
Expand Down
2 changes: 1 addition & 1 deletion src/estimator/internal_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct InternalModel{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
model::SM, i_ym, Asm, Bsm, Csm, Dsm
) where {NT<:Real, SM<:SimModel}
nu, ny, nd = model.nu, model.ny, model.nd
nk̄ = get_nk̄(model, default_transcription(model))
nk̄ = get_nk̄(model)
nym, nyu = validate_ym(model, i_ym)
validate_internalmodel(model, nym, Csm, Dsm)
As, Bs, Cs, Ds = stoch_ym2y(model, i_ym, Asm, Bsm, Csm, Dsm)
Expand Down
12 changes: 8 additions & 4 deletions src/estimator/mhe/construct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ at each time step for the optimization.
not (details in Extended Help).
- `nc=0` : number of custom nonlinear inequality constraints.
- `p=model.p` : ``\mathbf{g_c}`` functions parameter ``\mathbf{p}`` (any type).
- `transcription=default_transcription(model)` : a [`TranscriptionMethod`](@ref) object,
- `transcription=default_transcription_mhe(model)` : a [`TranscriptionMethod`](@ref) object,
default to [`SingleShooting`](@ref) or `model.transcription` for [`NonLinModelDAE`](@ref).
- `optim=default_optim_mhe(model,nc)` : a [`JuMP.Model`](@extref) object with a quadratic or
nonlinear optimizer for solving (default to [`Ipopt`](https://github.com/jump-dev/Ipopt.jl),
Expand Down Expand Up @@ -571,7 +571,7 @@ function MovingHorizonEstimator(
gc ::Function = gc!,
nc ::Int = 0,
p = model.p,
transcription::TranscriptionMethod = default_transcription(model),
transcription::TranscriptionMethod = default_transcription_mhe(model),
optim::JM = default_optim_mhe(model, nc),
gradient::AbstractADType = DEFAULT_GRADIENT,
jacobian::AbstractADType = default_jacobian(transcription),
Expand Down Expand Up @@ -604,7 +604,7 @@ end
gc!=(_,_,_,_,_,_,_,_,_,_,_,_) -> nothing,
gc=gc!,
nc=0,
transcription=default_transcription(model),
transcription=default_transcription_mhe(model),
optim=default_optim_mhe(model, nc),
gradient=AutoForwardDiff(),
jacobian=AutoForwardDiff(),
Expand All @@ -625,7 +625,7 @@ function MovingHorizonEstimator(
gc ::Function = gc!,
nc = 0,
p = model.p,
transcription::TranscriptionMethod = default_transcription(model),
transcription::TranscriptionMethod = default_transcription_mhe(model),
optim::JM = default_optim_mhe(model, nc),
gradient::AbstractADType = DEFAULT_GRADIENT,
jacobian::AbstractADType = default_jacobian(transcription),
Expand Down Expand Up @@ -657,6 +657,10 @@ function MovingHorizonEstimator(
)
end

"Default to `Singlehooting()`, or `model.transcription` if `model` is a `NonLinModelDAE`."
default_transcription_mhe(::SimModel) = SingleShooting()
default_transcription_mhe(model::NonLinModelDAE) = model.transcription

"Default optimizer for MHE, depending on the model and the number of custom NL constraints."
function default_optim_mhe(model::SimModel, nc)
if model isa LinModel && iszero(nc)
Expand Down
4 changes: 0 additions & 4 deletions src/model/nonlinmodeldae.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ function validate_strictly_proper(NT, fq!, h!, nu, nx, na, ny, nd, p)
return iszero_Ha
end

"Default to `SingleShooting()`, or `model.transcription` if `model` is a `NonLinModelDAE`."
default_transcription(::SimModel) = SingleShooting()
default_transcription(model::NonLinModelDAE) = model.transcription

"Get the number of algebraic variable `na` in `model`."
get_na(model::NonLinModelDAE) = model.na
get_na(::SimModel) = 0
Expand Down
2 changes: 1 addition & 1 deletion src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ R̂y = repeat([55; 30], 3)
nlmodel = setop!(nlmodel, uop=[10, 10], yop=[50, 30])
y = nlmodel()

transcription = MultipleShooting(f_threads=true)
transcription = MultipleShooting()
nmpc_ukf = setconstraint!(
NonLinMPC(nlmodel; Hp=3, Mwt=[0, 0], Ewt=1, JE=JE, p=R̂y, transcription),
ymin=[45, -Inf]
Expand Down
2 changes: 2 additions & 0 deletions src/sim_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ end
"Get length of the `k̄` vector with all the solver intermediate steps or all the collocation pts."
get_nk̄(model::SimModel, ::ShootingMethod) = model.nk̄
get_nk̄(model::SimModel, transcription::CollocationMethod) = model.nx*transcription.no
get_nk̄(model::SimModelDAE) = model.nx*model.transcription.no
get_nk̄(model::SimModelODE) = model.nk̄

include("model/linmodel.jl")
include("model/linearization.jl")
Expand Down
Loading
Loading