Skip to content

Commit 2a67c9a

Browse files
committed
added: Mwt, Nwt and Lwt kwargs in setmodel!
1 parent 6fb8af8 commit 2a67c9a

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

src/controller/construct.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ function setconstraint!(
268268
size(con.A_x̂max, 1) 0 && (con.A_x̂max[:, end] .= -con.c_x̂max) # for LinModel
269269
end
270270
end
271-
# TODO: test setmodel! new arguments
272271
i_Umin, i_Umax = .!isinf.(con.U0min), .!isinf.(con.U0max)
273272
i_ΔŨmin, i_ΔŨmax = .!isinf.(con.ΔŨmin), .!isinf.(con.ΔŨmax)
274273
i_Ymin, i_Ymax = .!isinf.(con.Y0min), .!isinf.(con.Y0max)

src/controller/execute.jl

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,13 @@ prediction horizon ``H_p``.
543543
Keyword arguments with *`emphasis`* are non-Unicode alternatives.
544544
545545
- `mpc::PredictiveController` : controller to set model and weights.
546-
- `model=mpc.estim.model` : new plant model ([`NonLinModel`](@ref) not supported).
547-
- `M_Hp=mpc.M_Hp` : new ``\mathbf{M}_{H_p}`` weight matrix.
548-
- `Ñ_Hc=mpc.Ñ_Hc` or *`Ntilde_Hc`* : new ``\mathbf{Ñ}_{H_c}`` weight matrix (see definition
549-
above).
550-
- `L_Hp=mpc.L_Hp` : new ``\mathbf{L}_{H_p}`` weight matrix.
546+
- `model=mpc.estim.model` : new plant model (not supported by [`NonLinModel`](@ref)).
547+
- `Mwt=nothing` : new main diagonal in ``\mathbf{M}`` weight matrix (vector).
548+
- `Nwt=nothing` : new main diagonal in ``\mathbf{N}`` weight matrix (vector).
549+
- `Lwt=nothing` : new main diagonal in ``\mathbf{L}`` weight matrix (vector).
550+
- `M_Hp=nothing` : new ``\mathbf{M}_{H_p}`` weight matrix.
551+
- `Ñ_Hc=nothing` or *`Ntilde_Hc`* : new ``\mathbf{Ñ}_{H_c}`` weight matrix (see def. above).
552+
- `L_Hp=nothing` : new ``\mathbf{L}_{H_p}`` weight matrix.
551553
- additional keyword arguments are passed to `setmodel!(mpc.estim)`.
552554
553555
# Examples
@@ -566,18 +568,48 @@ julia> mpc.estim.model.A[], mpc.estim.R̂[], mpc.M_Hp[]
566568
function setmodel!(
567569
mpc::PredictiveController,
568570
model = mpc.estim.model;
569-
M_Hp = mpc.M_Hp,
570-
Ntilde_Hc = mpc.Ñ_Hc,
571-
L_Hp = mpc.L_Hp,
571+
Mwt = nothing,
572+
Nwt = nothing,
573+
Lwt = nothing,
574+
M_Hp = nothing,
575+
Ntilde_Hc = nothing,
576+
L_Hp = nothing,
572577
Ñ_Hc = Ntilde_Hc,
573578
kwargs...
574579
)
575580
x̂op_old = copy(mpc.estim.x̂op)
576581
nu, ny, Hp, Hc, nϵ = model.nu, model.ny, mpc.Hp, mpc.Hc, mpc.
577582
setmodel!(mpc.estim, model; kwargs...)
578-
mpc.M_Hp .= to_hermitian(M_Hp)
579-
mpc.Ñ_Hc .= to_hermitian(Ñ_Hc)
580-
mpc.L_Hp .= to_hermitian(L_Hp)
583+
if isnothing(M_Hp) && !isnothing(Mwt)
584+
size(Mwt) == (ny,) || throw(ArgumentError("Mwt should be a vector of length ny"))
585+
any(x -> x < 0, Mwt) && throw(ArgumentError("Mwt values should be nonnegative"))
586+
for i=1:ny*Hp
587+
mpc.M_Hp[i, i] = Mwt[(i-1) % ny + 1]
588+
end
589+
elseif !isnothing(M_Hp)
590+
size(M_Hp) == (ny*Hp, ny*Hp) || throw(ArgumentError("M_Hp size should be (ny*Hp, ny*Hp)"))
591+
mpc.M_Hp .= to_hermitian(M_Hp)
592+
end
593+
if isnothing(Ñ_Hc) && !isnothing(Nwt)
594+
size(Nwt) == (nu,) || throw(ArgumentError("Nwt should be a vector of length nu"))
595+
any(x -> x < 0, Nwt) && throw(ArgumentError("Nwt values should be nonnegative"))
596+
for i=1:nu*Hc
597+
mpc.Ñ_Hc[i, i] = Nwt[(i-1) % nu + 1]
598+
end
599+
elseif !isnothing(Ñ_Hc)
600+
size(Ñ_Hc) == (nu*Hc+nϵ, nu*Hc+nϵ) || throw(ArgumentError("Ñ_Hc size should be (nu*Hc+nϵ, nu*Hc+nϵ)"))
601+
mpc.Ñ_Hc .= to_hermitian(Ñ_Hc)
602+
end
603+
if isnothing(L_Hp) && !isnothing(Lwt)
604+
size(Lwt) == (nu,) || throw(ArgumentError("Lwt should be a vector of length nu"))
605+
any(x -> x < 0, Lwt) && throw(ArgumentError("Lwt values should be nonnegative"))
606+
for i=1:nu*Hp
607+
mpc.L_Hp[i, i] = Lwt[(i-1) % nu + 1]
608+
end
609+
elseif !isnothing(L_Hp)
610+
size(L_Hp) == (nu*Hp, nu*Hp) || throw(ArgumentError("L_Hp size should be (nu*Hp, nu*Hp)"))
611+
mpc.L_Hp .= to_hermitian(L_Hp)
612+
end
581613
setmodel_controller!(mpc, x̂op_old, M_Hp, Ñ_Hc, L_Hp)
582614
return mpc
583615
end

test/test_predictive_control.jl

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ end
260260

261261
@testset "LinMPC set model" begin
262262
estim = KalmanFilter(setop!(LinModel(tf(5, [2, 1]), 3), yop=[10], uop=[1]))
263-
mpc = LinMPC(estim, Nwt=[0], Hp=1000, Hc=1)
263+
mpc = LinMPC(estim, Nwt=[0], Cwt=1e4, Hp=1000, Hc=1)
264264
mpc = setconstraint!(mpc, umin=[-24], umax=[26])
265265
mpc = setconstraint!(mpc, ymin=[-54], ymax=[56])
266266
@test mpc.Yop fill(10.0, 1000)
@@ -286,6 +286,10 @@ end
286286
r = [40]
287287
u = moveinput!(mpc, r)
288288
@test u [13] atol=1e-2
289+
setmodel!(mpc, Mwt=[100], Nwt=[200], Lwt=[300])
290+
@test mpc.M_Hp diagm(fill(100, 1000))
291+
@test mpc.Ñ_Hc diagm([200, 1e4])
292+
@test mpc.L_Hp diagm(fill(300, 1000))
289293
setmodel!(mpc, M_Hp=diagm(1:1000), Ñ_Hc=diagm([0.1;1e6]), L_Hp=diagm(1.1:1000.1))
290294
@test mpc.M_Hp diagm(1:1000)
291295
@test mpc.Ñ_Hc diagm([0.1;1e6])
@@ -417,6 +421,10 @@ end
417421
r = [40]
418422
u = moveinput!(mpc, r)
419423
@test u [13] atol=1e-2
424+
setmodel!(mpc, Mwt=[100], Nwt=[200], Lwt=[300])
425+
@test mpc.M_Hp diagm(fill(100, 1000))
426+
@test mpc.Ñ_Hc diagm([200])
427+
@test mpc.L_Hp diagm(fill(300, 1000))
420428
setmodel!(mpc, M_Hp=diagm(1:1000), Ñ_Hc=[0.1], L_Hp=diagm(1.1:1000.1))
421429
@test mpc.M_Hp diagm(1:1000)
422430
@test mpc.Ñ_Hc [0.1]
@@ -694,7 +702,7 @@ end
694702

695703
@testset "NonLinMPC set model" begin
696704
estim = KalmanFilter(setop!(LinModel(tf(5, [2, 1]), 3), yop=[10], uop=[1]))
697-
mpc = NonLinMPC(estim, Nwt=[0], Hp=1000, Hc=1)
705+
mpc = NonLinMPC(estim, Nwt=[0], Cwt=1e4, Hp=1000, Hc=1)
698706
mpc = setconstraint!(mpc, umin=[-24], umax=[26])
699707
mpc = setconstraint!(mpc, ymin=[-54], ymax=[56])
700708
@test mpc.Yop fill(10.0, 1000)
@@ -720,17 +728,25 @@ end
720728
r = [40]
721729
u = moveinput!(mpc, r)
722730
@test u [13] atol=1e-2
731+
setmodel!(mpc, Mwt=[100], Nwt=[200], Lwt=[300])
732+
@test mpc.M_Hp diagm(fill(100, 1000))
733+
@test mpc.Ñ_Hc diagm([200, 1e4])
734+
@test mpc.L_Hp diagm(fill(300, 1000))
723735
setmodel!(mpc, M_Hp=diagm(1:1000), Ñ_Hc=diagm([0.1;1e6]), L_Hp=diagm(1.1:1000.1))
724736
@test mpc.M_Hp diagm(1:1000)
725737
@test mpc.Ñ_Hc diagm([0.1;1e6])
726738
@test mpc.L_Hp diagm(1.1:1000.1)
727739
f(x,u,d) = estim.model.A*x + estim.model.Bu*u + estim.model.Bd*d
728740
h(x,d) = estim.model.C*x + estim.model.Du*d
729741
nonlinmodel = NonLinModel(f, h, 10.0, 1, 1, 1)
730-
nmpc = NonLinMPC(nonlinmodel, Hp=1000, Hc=1)
731-
setmodel!(nmpc, M_Hp=diagm(1:1000), Ñ_Hc=diagm([0.1;1e6]), L_Hp=diagm(1.1:1000.1))
742+
nmpc = NonLinMPC(nonlinmodel, Nwt=[0], Cwt=1e4, Hp=1000, Hc=10)
743+
setmodel!(nmpc, Mwt=[100], Nwt=[200], Lwt=[300])
744+
@test nmpc.M_Hp diagm(fill(100, 1000))
745+
@test nmpc.Ñ_Hc diagm([fill(200, 10); 1e4])
746+
@test nmpc.L_Hp diagm(fill(300, 1000))
747+
setmodel!(nmpc, M_Hp=diagm(1:1000), Ñ_Hc=diagm([fill(0.1, 10);1e6]), L_Hp=diagm(1.1:1000.1))
732748
@test nmpc.M_Hp diagm(1:1000)
733-
@test nmpc.Ñ_Hc diagm([0.1;1e6])
749+
@test nmpc.Ñ_Hc diagm([fill(0.1, 10);1e6])
734750
@test nmpc.L_Hp diagm(1.1:1000.1)
735751
@test_throws ErrorException setmodel!(nmpc, deepcopy(nonlinmodel))
736752
end

0 commit comments

Comments
 (0)