diff --git a/docs/src/manual/nonlinmpc1.md b/docs/src/manual/nonlinmpc1.md index 89c0bef5b..06fe0e3ad 100644 --- a/docs/src/manual/nonlinmpc1.md +++ b/docs/src/manual/nonlinmpc1.md @@ -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 @@ -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 @@ -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: diff --git a/src/controller/nonlinmpc.jl b/src/controller/nonlinmpc.jl index 71772820c..2d0c4cb11 100644 --- a/src/controller/nonlinmpc.jl +++ b/src/controller/nonlinmpc.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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( @@ -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 diff --git a/src/estimator/internal_model.jl b/src/estimator/internal_model.jl index 3eb3d83f1..4a82c7c8f 100644 --- a/src/estimator/internal_model.jl +++ b/src/estimator/internal_model.jl @@ -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) diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 8cee5621d..1f4c0cb40 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -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), @@ -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), @@ -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(), @@ -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), @@ -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) diff --git a/src/model/nonlinmodeldae.jl b/src/model/nonlinmodeldae.jl index bf962f885..9113b2edf 100644 --- a/src/model/nonlinmodeldae.jl +++ b/src/model/nonlinmodeldae.jl @@ -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 diff --git a/src/precompile.jl b/src/precompile.jl index 04d0b25ac..9a7c7197f 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -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] diff --git a/src/sim_model.jl b/src/sim_model.jl index e1eb593c9..28d3e20f4 100644 --- a/src/sim_model.jl +++ b/src/sim_model.jl @@ -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") diff --git a/test/3_test_predictive_control.jl b/test/3_test_predictive_control.jl index 711b2b878..07ae2fc8a 100644 --- a/test/3_test_predictive_control.jl +++ b/test/3_test_predictive_control.jl @@ -809,8 +809,11 @@ end linmodel1 = LinModel(sys,Ts,i_d=[3]) nmpc0 = NonLinMPC(linmodel1, Hp=15) @test isa(nmpc0.estim, SteadyKalmanFilter) + @test nmpc0.transcription isa MultipleShooting + @test length(nmpc0.Z̃) == linmodel1.nu*nmpc0.Hc + nmpc0.estim.nx̂*nmpc0.Hp + nmpc0.nϵ + @test size(nmpc0.con.Aeq, 1) == nmpc0.estim.nx̂*nmpc0.Hp nmpc3 = NonLinMPC(linmodel1, Hp=15, Hc=4, Cwt=1e4) - @test size(nmpc3.Ẽ, 2) == 4*linmodel1.nu + 1 + @test size(nmpc3.Ẽ, 2) == 4*linmodel1.nu + 15*linmodel1.nx + 15*linmodel1.ny + 1 @test nmpc3.weights.Ñ_Hc[end] == 1e4 nmpc4 = NonLinMPC(linmodel1, Hp=15, Mwt=[1,2]) @test nmpc4.weights.M_Hp ≈ Diagonal(diagm(repeat(Float64[1, 2], 15))) @@ -840,10 +843,10 @@ end LHS = zeros(1) nmpc15.con.gc!(LHS,[1,2],[3,4],[4,6],10,0.1) @test LHS ≈ [10*dot([1,2],[3,4])+sum([4,6])+0.1] - nmpc17 = NonLinMPC(linmodel1, Hp=10, transcription=MultipleShooting()) - @test nmpc17.transcription == MultipleShooting() - @test length(nmpc17.Z̃) == linmodel1.nu*nmpc17.Hc + nmpc17.estim.nx̂*nmpc17.Hp + nmpc17.nϵ - @test size(nmpc17.con.Aeq, 1) == nmpc17.estim.nx̂*nmpc17.Hp + nmpc17 = NonLinMPC(linmodel1, Hp=10, transcription=SingleShooting()) + @test nmpc17.transcription isa SingleShooting + @test length(nmpc17.Z̃) == linmodel1.nu*nmpc17.Hc + nmpc17.nϵ + @test size(nmpc17.con.Aeq, 1) == 0 @test_throws DimensionMismatch NonLinMPC(linmodel1, Hp=15, Ewt=[1, 1]) @test_throws ErrorException NonLinMPC(linmodel1, Hp=15, JE = (_,_,_)->0.0) @@ -861,17 +864,19 @@ end using JuMP, Ipopt, DifferentiationInterface import FiniteDiff linmodel1 = LinModel(sys,Ts,i_d=[3]) - nmpc0 = NonLinMPC(linmodel1, Hp=15) - @test isa(nmpc0.estim, SteadyKalmanFilter) f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d h = (x,d,model) -> model.C*x + model.Dd*d nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, p=linmodel1, solver=nothing) nmpc1 = NonLinMPC(nonlinmodel, Hp=15) @test isa(nmpc1.estim, UnscentedKalmanFilter) - @test size(nmpc1.R̂y, 1) == 15*nmpc1.estim.model.ny - nmpc2 = NonLinMPC(nonlinmodel, Hp=15, Hc=4, Cwt=Inf) - @test size(nmpc2.Ẽ, 2) == 4*nonlinmodel.nu + @test nmpc1.transcription isa MultipleShooting + @test length(nmpc1.Z̃) == nonlinmodel.nu*nmpc1.Hc + nmpc1.estim.nx̂*nmpc1.Hp + nmpc1.nϵ + @test nmpc1.con.neq == nonlinmodel.nx*nmpc1.Hp + @test size(nmpc1.R̂y, 1) == nmpc1.Hp*nmpc1.estim.model.ny + gc! = (LHS,_,_,_,_,_)-> (LHS .= 0.0) # useless gc!, only for coverage + nmpc2 = NonLinMPC(nonlinmodel, Hp=15, Hc=4, Cwt=Inf, nc=15, gc=gc!) + @test size(nmpc2.Ẽ, 2) == nmpc2.Hc*nonlinmodel.nu + nmpc2.estim.nx̂*nmpc2.Hp + nmpc2.nϵ optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer, "nlp_scaling_max_gradient"=>1.0)) nmpc8 = NonLinMPC(nonlinmodel, Hp=15, optim=optim) @test solver_name(nmpc8.optim) == "Ipopt" @@ -882,11 +887,10 @@ end nmpc11 = NonLinMPC(nonlinmodel, Hp=15, nint_u=[1, 1], nint_ym=[0, 0]) @test nmpc11.estim.nint_u == [1, 1] @test nmpc11.estim.nint_ym == [0, 0] - gc! = (LHS,_,_,_,_,_)-> (LHS .= 0.0) # useless, only for coverage - nmpc16 = NonLinMPC(nonlinmodel, Hp=10, transcription=MultipleShooting(), nc=10, gc=gc!) - @test isa(nmpc16.transcription, MultipleShooting) - @test length(nmpc16.Z̃) == nonlinmodel.nu*nmpc16.Hc + nmpc16.estim.nx̂*nmpc16.Hp + nmpc16.nϵ - @test nmpc16.con.neq == nmpc16.estim.model.nx*nmpc16.Hp + nmpc16 = NonLinMPC(nonlinmodel, Hp=10, transcription=SingleShooting(), nc=10, gc=gc!) + @test nmpc16.transcription isa SingleShooting + @test length(nmpc16.Z̃) == nonlinmodel.nu*nmpc16.Hc + nmpc16.nϵ + @test nmpc16.con.neq == 0 @test nmpc16.con.nc == 10 nonlinmodel_c = NonLinModel((ẋ,x,u,_,_)->ẋ .= -0.1x .+ u, (y,x,_,_)->y.=x, 1, 1, 1, 1) nmpc17 = NonLinMPC(nonlinmodel_c, Hp=10, transcription=TrapezoidalCollocation(), nc=10, gc=gc!) @@ -916,7 +920,8 @@ end @test nmpc20.hessian == SecondOrder(AutoForwardDiff(), AutoForwardDiff()) @test_nowarn repr(nmpc20) # printing SecondOrder backends, for coverage - nonlinmodel2 = NonLinModel{Float32}(f, h, Ts, 2, 4, 2, 1, solver=nothing) + linmodel2 = LinModel{Float32}(linmodel1.A, linmodel1.Bu, linmodel1.C, linmodel1.Bd, linmodel1.Dd, Ts) + nonlinmodel2 = NonLinModel{Float32}(f, h, Ts, 2, 4, 2, 1, solver=nothing, p=linmodel2) nmpc15 = NonLinMPC(nonlinmodel2, Hp=15) @test isa(nmpc15, NonLinMPC{Float32}) @test isa(nmpc15.estim, UnscentedKalmanFilter{Float32}) @@ -964,6 +969,21 @@ end import FiniteDiff linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10]) + nmpc9 = NonLinMPC(linmodel, Nwt=[0], Hp=100, Hc=1, transcription=SingleShooting()) + preparestate!(nmpc9, [10]) + u = moveinput!(nmpc9, [20]) + @test u ≈ [2] atol=5e-2 + info = getinfo(nmpc9) + @test info[:u] ≈ u + @test info[:Ŷ][end] ≈ 20 atol=5e-2 +end + +@testitem "NonLinMPC moves & getinfo (LinModel, MS)" setup=[SetupMPCtests] begin + using .SetupMPCtests, ControlSystemsBase, LinearAlgebra + using DifferentiationInterface + import FiniteDiff + linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10]) + Hp = 100 nmpc_lin = NonLinMPC(linmodel, Nwt=[0], Hp=Hp, Hc=1) ry, ru = [15], [4] @@ -1014,22 +1034,49 @@ end @test_nowarn ModelPredictiveControl.info2debugstr(info) end -@testitem "NonLinMPC moves & getinfo (LinModel, MS)" setup=[SetupMPCtests] begin +@testitem "NonLinMPC moves & getinfo (NonLinModel, SS)" setup=[SetupMPCtests] begin using .SetupMPCtests, ControlSystemsBase, LinearAlgebra using DifferentiationInterface import FiniteDiff - linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10]) - - nmpc9 = NonLinMPC(linmodel, Nwt=[0], Hp=100, Hc=1, transcription=MultipleShooting()) - preparestate!(nmpc9, [10]) - u = moveinput!(nmpc9, [20]) + linmodel2 = LinModel([tf(5, [2000, 1]) tf(7, [8000,1])], 3000.0, i_d=[2]) + f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d + h = (x,d,model) -> model.C*x + model.Dd*d + nonlinmodel = NonLinModel(f, h, 3000.0, 1, 2, 1, 1, solver=nothing, p=linmodel2) + + transcription = SingleShooting() + nmpc3 = NonLinMPC(nonlinmodel; Hp=15, Mwt=[0], Nwt=[0], Lwt=[1], transcription) + preparestate!(nmpc3, [0], [0]) + u = moveinput!(nmpc3, [0], [0], R̂u=fill(12, nmpc3.Hp)) + @test u ≈ [12] atol=5e-2 + + transcription = SingleShooting() + nmpc4 = NonLinMPC(nonlinmodel; Nwt=[0], Hp=100, Hc=1, transcription) + preparestate!(nmpc4, [0], [0]) + u = moveinput!(nmpc4, [10], [0]) @test u ≈ [2] atol=5e-2 - info = getinfo(nmpc9) + info = getinfo(nmpc4) + @test info[:u] ≈ u + @test info[:Ŷ][end] ≈ 10 atol=5e-2 + + function f!(ẋ, x, u, d, p) + ẋ[] = -p[] * (x[] - 0.2 * u[] - 0.2 * d[]) + return nothing + end + function h!(y, x, d, _ ) + y[] = x[] + 0.1 * d[] + end + p = [0.001] + model = NonLinModel(f!, h!, 1000.0, 1, 1, 1, 1; p) + transcription = SingleShooting(1) + nmpc_foh = NonLinMPC(model; Hp=5, Hc=1, Nwt=[0], transcription) + preparestate!(nmpc_foh, [0.0], [0.0]) + u = moveinput!(nmpc_foh, [1.0], [0.0]) + info = getinfo(nmpc_foh) + @test nmpc_foh.transcription.h == 1 @test info[:u] ≈ u - @test info[:Ŷ][end] ≈ 20 atol=5e-2 end -@testitem "NonLinMPC moves & getinfo (NonLinModel, SS)" setup=[SetupMPCtests] begin +@testitem "NonLinMPC moves & getinfo (NonLinModel, MS)" setup=[SetupMPCtests] begin using .SetupMPCtests, ControlSystemsBase, LinearAlgebra using DifferentiationInterface import FiniteDiff @@ -1054,11 +1101,16 @@ end preparestate!(nmpc2, [0], [0]) u = moveinput!(nmpc2, 7d, d) @test u ≈ [0] atol=5e-2 - - nmpc3 = NonLinMPC(nonlinmodel, Hp=15, Mwt=[0], Nwt=[0], Lwt=[1]) - preparestate!(nmpc3, [0], [0]) - u = moveinput!(nmpc3, [0], d, R̂u=fill(12, nmpc3.Hp)) - @test u ≈ [12] atol=5e-2 + + transcription = MultipleShooting(f_threads=true, h_threads=true) + nmpc4t = NonLinMPC(nonlinmodel; Nwt=[0], Hp=100, Hc=1, transcription, hessian=true) + nmpc4t = setconstraint!(nmpc4t, ymax=[100], ymin=[-100]) # coverage of getinfo! Hessians of Lagrangian + preparestate!(nmpc4t, [0], [0]) + u = moveinput!(nmpc4t, [10], [0]) + @test u ≈ [2] atol=5e-2 + info = getinfo(nmpc4t) + @test info[:u] ≈ u + @test info[:Ŷ][end] ≈ 10 atol=5e-2 nonlinmodel2 = NonLinModel{Float32}(f, h, 3000.0, 1, 2, 1, 1, solver=nothing, p=linmodel2) nmpc7 = NonLinMPC(nonlinmodel2, Hp=10) @@ -1087,52 +1139,6 @@ end ΔU_diff = diff(getinfo(nmpc11)[:U]) @test ΔU_diff[[2, 4, 5, 7, 8, 9]] ≈ zeros(6) atol=1e-9 - function f!(ẋ, x, u, d, p) - ẋ[] = -p[] * (x[] - 0.2 * u[] - 0.2 * d[]) - return nothing - end - function h!(y, x, d, _ ) - y[] = x[] + 0.1 * d[] - end - p = [0.001] - model = NonLinModel(f!, h!, 1000.0, 1, 1, 1, 1; p) - transcription = SingleShooting(1) - nmpc_foh = NonLinMPC(model; Hp=5, Hc=1, Nwt=[0], transcription) - preparestate!(nmpc_foh, [0.0], [0.0]) - u = moveinput!(nmpc_foh, [1.0], [0.0]) - info = getinfo(nmpc_foh) - @test nmpc_foh.transcription.h == 1 - @test info[:u] ≈ u -end - -@testitem "NonLinMPC moves & getinfo (NonLinModel, MS)" setup=[SetupMPCtests] begin - using .SetupMPCtests, ControlSystemsBase, LinearAlgebra - using DifferentiationInterface - import FiniteDiff - linmodel2 = LinModel([tf(5, [2000, 1]) tf(7, [8000,1])], 3000.0, i_d=[2]) - f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d - h = (x,d,model) -> model.C*x + model.Dd*d - nonlinmodel = NonLinModel(f, h, 3000.0, 1, 2, 1, 1, solver=nothing, p=linmodel2) - - transcription = MultipleShooting() - nmpc4 = NonLinMPC(nonlinmodel; Nwt=[0], Hp=100, Hc=1, transcription) - preparestate!(nmpc4, [0], [0]) - u = moveinput!(nmpc4, [10], [0]) - @test u ≈ [2] atol=5e-2 - info = getinfo(nmpc4) - @test info[:u] ≈ u - @test info[:Ŷ][end] ≈ 10 atol=5e-2 - - transcription = MultipleShooting(f_threads=true, h_threads=true) - nmpc4t = NonLinMPC(nonlinmodel; Nwt=[0], Hp=100, Hc=1, transcription, hessian=true) - nmpc4t = setconstraint!(nmpc4t, ymax=[100], ymin=[-100]) # coverage of getinfo! Hessians of Lagrangian - preparestate!(nmpc4t, [0], [0]) - u = moveinput!(nmpc4t, [10], [0]) - @test u ≈ [2] atol=5e-2 - info = getinfo(nmpc4t) - @test info[:u] ≈ u - @test info[:Ŷ][end] ≈ 10 atol=5e-2 - function f!(ẋ, x, u, d, p) ẋ[] = -p[] * (x[] - 0.2 * u[] - 0.2 * d[]) return nothing @@ -1350,14 +1356,14 @@ end nlmodel = setop!(nlmodel, yop=[10]) u, ym = let nlmodel=nlmodel, r=r, outdist=outdist estim = UnscentedKalmanFilter(nlmodel, nint_u=[1]) - nmpc_nint_u_ms = NonLinMPC(estim; Hp=10, transcription=MultipleShooting()) + nmpc_nint_u_ss = NonLinMPC(estim; Hp=10, transcription=SingleShooting()) nlmodel.x0 .= 0 ym, u = nlmodel() - outdist, [0.0] for i=1:25 ym = nlmodel() - outdist - preparestate!(nmpc_nint_u_ms, ym) - u = moveinput!(nmpc_nint_u_ms, r) - updatestate!(nmpc_nint_u_ms, u, ym) + preparestate!(nmpc_nint_u_ss, ym) + u = moveinput!(nmpc_nint_u_ss, r) + updatestate!(nmpc_nint_u_ss, u, ym) updatestate!(nlmodel, u) end u, ym @@ -1461,8 +1467,8 @@ end @test -nmpc.con.A_ΔUmin[:, end] ≈ [0.05,0.06] @test -nmpc.con.A_ΔUmax[:, end] ≈ [0.07,0.08] setconstraint!(nmpc, c_ymin=[1.00,1.01], c_ymax=[1.02,1.03]) - @test -nmpc.con.A_Ymin ≈ zeros(0,3) - @test -nmpc.con.A_Ymax ≈ zeros(0,3) + @test -nmpc.con.A_Ymin ≈ zeros(0,9) + @test -nmpc.con.A_Ymax ≈ zeros(0,9) @test nmpc.con.C_ymin ≈ [1.00,1.01] @test nmpc.con.C_ymax ≈ [1.02,1.03] setconstraint!(nmpc, @@ -1472,28 +1478,28 @@ end @test nmpc.con.c_x̂min ≈ [0.21,0.22,0.23,0.24,0.25,0.26] @test nmpc.con.c_x̂max ≈ [0.31,0.32,0.33,0.34,0.35,0.36] - nmpc_ms = NonLinMPC(nonlinmodel, Hp=1, Hc=1, transcription=MultipleShooting()) + nmpc_ss = NonLinMPC(nonlinmodel, Hp=1, Hc=1, transcription=SingleShooting()) - setconstraint!(nmpc_ms, ymin=[-6, -11],ymax=[55, 35]) - @test nmpc_ms.con.Y0min ≈ [-6,-11] - @test nmpc_ms.con.Y0max ≈ [55,35] - setconstraint!(nmpc_ms, x̂min=[-21,-22,-23,-24,-25,-26], x̂max=[21,22,23,24,25,26]) - @test nmpc_ms.con.x̂0min ≈ [-21,-22,-23,-24,-25,-26] - @test nmpc_ms.con.x̂0max ≈ [21,22,23,24,25,26] - - setconstraint!(nmpc_ms, c_ymin=[1.00,1.01], c_ymax=[1.02,1.03]) - @test -nmpc_ms.con.A_Ymin ≈ zeros(0,9) - @test -nmpc_ms.con.A_Ymax ≈ zeros(0,9) - @test nmpc_ms.con.C_ymin ≈ [1.00,1.01] - @test nmpc_ms.con.C_ymax ≈ [1.02,1.03] - setconstraint!(nmpc_ms, + setconstraint!(nmpc_ss, ymin=[-6, -11],ymax=[55, 35]) + @test nmpc_ss.con.Y0min ≈ [-6,-11] + @test nmpc_ss.con.Y0max ≈ [55,35] + setconstraint!(nmpc_ss, x̂min=[-21,-22,-23,-24,-25,-26], x̂max=[21,22,23,24,25,26]) + @test nmpc_ss.con.x̂0min ≈ [-21,-22,-23,-24,-25,-26] + @test nmpc_ss.con.x̂0max ≈ [21,22,23,24,25,26] + + setconstraint!(nmpc_ss, c_ymin=[1.00,1.01], c_ymax=[1.02,1.03]) + @test -nmpc_ss.con.A_Ymin ≈ zeros(0,3) + @test -nmpc_ss.con.A_Ymax ≈ zeros(0,3) + @test nmpc_ss.con.C_ymin ≈ [1.00,1.01] + @test nmpc_ss.con.C_ymax ≈ [1.02,1.03] + setconstraint!(nmpc_ss, c_x̂min=[0.21,0.22,0.23,0.24,0.25,0.26], c_x̂max=[0.31,0.32,0.33,0.34,0.35,0.36] ) @test -nmpc_lin.con.A_x̂min[:, end] ≈ [0.21,0.22,0.23,0.24,0.25,0.26] @test -nmpc_lin.con.A_x̂max[:, end] ≈ [0.31,0.32,0.33,0.34,0.35,0.36] - @test nmpc_ms.con.c_x̂min ≈ [0.21,0.22,0.23,0.24,0.25,0.26] - @test nmpc_ms.con.c_x̂max ≈ [0.31,0.32,0.33,0.34,0.35,0.36] + @test nmpc_ss.con.c_x̂min ≈ [0.21,0.22,0.23,0.24,0.25,0.26] + @test nmpc_ss.con.c_x̂max ≈ [0.31,0.32,0.33,0.34,0.35,0.36] end @@ -1650,7 +1656,7 @@ end @test all(isapprox.(info[:gc][Hp+1:end], 0.0; atol=1e-1)) nmpc_ms = NonLinMPC( - nonlinmodel; Hp, Hc=5, transcription=MultipleShooting(), gc, nc=2Hp, p=[0; 0] + nonlinmodel; Hp, Hc=5, transcription=SingleShooting(), gc, nc=2Hp, p=[0; 0] ) JuMP.set_attribute(nmpc_ms.optim, "constr_viol_tol", 1e-3) @@ -1752,7 +1758,7 @@ end @test mpc.weights.Ñ_Hc ≈ diagm([0.1;1e6]) @test mpc.weights.L_Hp ≈ diagm(1.1:1000.1) f = (x,u,d,_) -> estim.model.A*x + estim.model.Bu*u + estim.model.Bd*d - h = (x,d,_) -> estim.model.C*x + estim.model.Du*d + h = (x,d,_) -> estim.model.C*x + estim.model.Dd*d nonlinmodel = NonLinModel(f, h, 10.0, 1, 1, 1) nmpc = NonLinMPC(nonlinmodel, Nwt=[0], Cwt=1e4, Hp=1000, Hc=10) setmodel!(nmpc, Mwt=[100], Nwt=[200], Lwt=[300])