Skip to content

Commit cfadb0e

Browse files
committed
test: seperating NMPC construction
1 parent d446beb commit cfadb0e

1 file changed

Lines changed: 51 additions & 42 deletions

File tree

test/3_test_predictive_control.jl

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -757,62 +757,82 @@ end
757757
@test mpc.weights.L_Hp diagm(1.1:1000.1)
758758
end
759759

760-
@testitem "NonLinMPC construction" setup=[SetupMPCtests] begin
760+
@testitem "NonLinMPC construction (LinModel)" setup=[SetupMPCtests] begin
761761
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
762-
using JuMP, Ipopt, DifferentiationInterface
763-
import FiniteDiff
764762
linmodel1 = LinModel(sys,Ts,i_d=[3])
765763
nmpc0 = NonLinMPC(linmodel1, Hp=15)
766764
@test isa(nmpc0.estim, SteadyKalmanFilter)
767-
f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d
768-
h = (x,d,model) -> model.C*x + model.Dd*d
769-
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, p=linmodel1, solver=nothing)
770-
nmpc1 = NonLinMPC(nonlinmodel, Hp=15)
771-
@test isa(nmpc1.estim, UnscentedKalmanFilter)
772-
@test size(nmpc1.R̂y, 1) == 15*nmpc1.estim.model.ny
773-
nmpc2 = NonLinMPC(nonlinmodel, Hp=15, Hc=4, Cwt=Inf)
774-
@test size(nmpc2.Ẽ, 2) == 4*nonlinmodel.nu
775-
nmpc3 = NonLinMPC(nonlinmodel, Hp=15, Hc=4, Cwt=1e4)
776-
@test size(nmpc3.Ẽ, 2) == 4*nonlinmodel.nu + 1
765+
nmpc3 = NonLinMPC(linmodel1, Hp=15, Hc=4, Cwt=1e4)
766+
@test size(nmpc3.Ẽ, 2) == 4*linmodel1.nu + 1
777767
@test nmpc3.weights.Ñ_Hc[end] == 1e4
778-
nmpc4 = NonLinMPC(nonlinmodel, Hp=15, Mwt=[1,2])
768+
nmpc4 = NonLinMPC(linmodel1, Hp=15, Mwt=[1,2])
779769
@test nmpc4.weights.M_Hp Diagonal(diagm(repeat(Float64[1, 2], 15)))
780770
@test nmpc4.weights.M_Hp isa Hermitian{Float64, Diagonal{Float64, Vector{Float64}}}
781-
nmpc5 = NonLinMPC(nonlinmodel, Hp=15 ,Nwt=[3,4], Cwt=1e3, Hc=5)
771+
nmpc5 = NonLinMPC(linmodel1, Hp=15 ,Nwt=[3,4], Cwt=1e3, Hc=5)
782772
@test nmpc5.weights.Ñ_Hc Diagonal(diagm([repeat(Float64[3, 4], 5); [1e3]]))
783773
@test nmpc5.weights.Ñ_Hc isa Hermitian{Float64, Diagonal{Float64, Vector{Float64}}}
784-
nmpc6 = NonLinMPC(nonlinmodel, Hp=15, Lwt=[0,1])
774+
nmpc6 = NonLinMPC(linmodel1, Hp=15, Lwt=[0,1])
785775
@test nmpc6.weights.L_Hp Diagonal(diagm(repeat(Float64[0, 1], 15)))
786776
@test nmpc6.weights.L_Hp isa Hermitian{Float64, Diagonal{Float64, Vector{Float64}}}
787-
nmpc7 = NonLinMPC(nonlinmodel, Hp=15, Ewt=1e-3, JE=(Ue,Ŷe,D̂e,p) -> p*dot(Ue,Ŷe)+sum(D̂e), p=10)
777+
nmpc7 = NonLinMPC(linmodel1, Hp=15, Ewt=1e-3, JE=(Ue,Ŷe,D̂e,p) -> p*dot(Ue,Ŷe)+sum(D̂e), p=10)
788778
@test nmpc7.weights.E == 1e-3
789779
@test nmpc7.JE([1,2],[3,4],[4,6],10) == 10*dot([1,2],[3,4])+sum([4,6])
790-
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer, "nlp_scaling_max_gradient"=>1.0))
791-
nmpc8 = NonLinMPC(nonlinmodel, Hp=15, optim=optim)
792-
@test solver_name(nmpc8.optim) == "Ipopt"
793-
@test get_attribute(nmpc8.optim, "nlp_scaling_max_gradient") == 1.0
794-
im = InternalModel(nonlinmodel)
795-
nmpc9 = NonLinMPC(im, Hp=15)
796-
@test isa(nmpc9.estim, InternalModel)
797780
nmpc10 = NonLinMPC(linmodel1, nint_u=[1, 1], nint_ym=[0, 0])
798781
@test nmpc10.estim.nint_u == [1, 1]
799782
@test nmpc10.estim.nint_ym == [0, 0]
800-
nmpc11 = NonLinMPC(nonlinmodel, Hp=15, nint_u=[1, 1], nint_ym=[0, 0])
801-
@test nmpc11.estim.nint_u == [1, 1]
802-
@test nmpc11.estim.nint_ym == [0, 0]
803-
nmpc12 = NonLinMPC(nonlinmodel, Hp=10, M_Hp=Hermitian(diagm(1.01:0.01:1.2), :L))
783+
nmpc12 = NonLinMPC(linmodel1, Hp=10, M_Hp=Hermitian(diagm(1.01:0.01:1.2), :L))
804784
@test nmpc12.weights.M_Hp diagm(1.01:0.01:1.2)
805785
@test nmpc12.weights.M_Hp isa Hermitian{Float64, Matrix{Float64}}
806-
nmpc13 = NonLinMPC(nonlinmodel, Hp=10, N_Hc=Hermitian(diagm([0.1,0.11,0.12,0.13]), :L), Cwt=Inf)
786+
nmpc13 = NonLinMPC(linmodel1, Hp=10, N_Hc=Hermitian(diagm([0.1,0.11,0.12,0.13]), :L), Cwt=Inf)
807787
@test nmpc13.weights.Ñ_Hc diagm([0.1,0.11,0.12,0.13])
808788
@test nmpc13.weights.Ñ_Hc isa Hermitian{Float64, Matrix{Float64}}
809-
nmcp14 = NonLinMPC(nonlinmodel, Hp=10, L_Hp=Hermitian(diagm(0.001:0.001:0.02), :L))
789+
nmcp14 = NonLinMPC(linmodel1, Hp=10, L_Hp=Hermitian(diagm(0.001:0.001:0.02), :L))
810790
@test nmcp14.weights.L_Hp diagm(0.001:0.001:0.02)
811791
@test nmcp14.weights.L_Hp isa Hermitian{Float64, Matrix{Float64}}
812-
nmpc15 = NonLinMPC(nonlinmodel, Hp=10, gc=(Ue,Ŷe,D̂e,p,ϵ)-> [p*dot(Ue,Ŷe)+sum(D̂e)+ϵ], nc=1, p=10)
792+
nmpc15 = NonLinMPC(linmodel1, Hp=10, gc=(Ue,Ŷe,D̂e,p,ϵ)-> [p*dot(Ue,Ŷe)+sum(D̂e)+ϵ], nc=1, p=10)
813793
LHS = zeros(1)
814794
nmpc15.con.gc!(LHS,[1,2],[3,4],[4,6],10,0.1)
815795
@test LHS [10*dot([1,2],[3,4])+sum([4,6])+0.1]
796+
nmpc17 = NonLinMPC(linmodel1, Hp=10, transcription=MultipleShooting())
797+
@test nmpc17.transcription == MultipleShooting()
798+
@test length(nmpc17.Z̃) == linmodel1.nu*nmpc17.Hc + nmpc17.estim.nx̂*nmpc17.Hp + nmpc17.
799+
@test size(nmpc17.con.Aeq, 1) == nmpc17.estim.nx̂*nmpc17.Hp
800+
801+
@test_throws DimensionMismatch NonLinMPC(linmodel1, Hp=15, Ewt=[1, 1])
802+
@test_throws ErrorException NonLinMPC(linmodel1, Hp=15, JE = (_,_,_)->0.0)
803+
@test_throws ErrorException NonLinMPC(linmodel1, Hp=15, gc = (_,_,_,_)->[0.0], nc=1)
804+
@test_throws ErrorException NonLinMPC(linmodel1, Hp=15, gc! = (_,_,_,_)->[0.0], nc=1)
805+
806+
@test_logs (:warn, Regex(".*")) NonLinMPC(linmodel1, Hp=15, JE=(Ue,_,_,_)->Ue)
807+
@test_logs (:warn, Regex(".*")) NonLinMPC(linmodel1, Hp=15, gc=(Ue,_,_,_,_)->Ue, nc=0)
808+
end
809+
810+
@testitem "NonLinMPC construction (NonLinModel)" setup=[SetupMPCtests] begin
811+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
812+
using JuMP, Ipopt, DifferentiationInterface
813+
import FiniteDiff
814+
linmodel1 = LinModel(sys,Ts,i_d=[3])
815+
nmpc0 = NonLinMPC(linmodel1, Hp=15)
816+
@test isa(nmpc0.estim, SteadyKalmanFilter)
817+
f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d
818+
h = (x,d,model) -> model.C*x + model.Dd*d
819+
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, p=linmodel1, solver=nothing)
820+
821+
nmpc1 = NonLinMPC(nonlinmodel, Hp=15)
822+
@test isa(nmpc1.estim, UnscentedKalmanFilter)
823+
@test size(nmpc1.R̂y, 1) == 15*nmpc1.estim.model.ny
824+
nmpc2 = NonLinMPC(nonlinmodel, Hp=15, Hc=4, Cwt=Inf)
825+
@test size(nmpc2.Ẽ, 2) == 4*nonlinmodel.nu
826+
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer, "nlp_scaling_max_gradient"=>1.0))
827+
nmpc8 = NonLinMPC(nonlinmodel, Hp=15, optim=optim)
828+
@test solver_name(nmpc8.optim) == "Ipopt"
829+
@test get_attribute(nmpc8.optim, "nlp_scaling_max_gradient") == 1.0
830+
im = InternalModel(nonlinmodel)
831+
nmpc9 = NonLinMPC(im, Hp=15)
832+
@test isa(nmpc9.estim, InternalModel)
833+
nmpc11 = NonLinMPC(nonlinmodel, Hp=15, nint_u=[1, 1], nint_ym=[0, 0])
834+
@test nmpc11.estim.nint_u == [1, 1]
835+
@test nmpc11.estim.nint_ym == [0, 0]
816836
gc! = (LHS,_,_,_,_,_)-> (LHS .= 0.0) # useless, only for coverage
817837
nmpc16 = NonLinMPC(nonlinmodel, Hp=10, transcription=MultipleShooting(), nc=10, gc=gc!)
818838
@test nmpc16.transcription == MultipleShooting()
@@ -825,10 +845,6 @@ end
825845
@test length(nmpc16.Z̃) == nonlinmodel_c.nu*nmpc16.Hc + nmpc16.estim.nx̂*nmpc16.Hp + nmpc16.
826846
@test nmpc16.con.neq == nmpc16.estim.nx̂*nmpc16.Hp
827847
@test nmpc16.con.nc == 10
828-
nmpc17 = NonLinMPC(linmodel1, Hp=10, transcription=MultipleShooting())
829-
@test nmpc17.transcription == MultipleShooting()
830-
@test length(nmpc17.Z̃) == linmodel1.nu*nmpc17.Hc + nmpc17.estim.nx̂*nmpc17.Hp + nmpc17.
831-
@test size(nmpc17.con.Aeq, 1) == nmpc17.estim.nx̂*nmpc17.Hp
832848
nmpc18 = NonLinMPC(nonlinmodel, Hp=10,
833849
gradient=AutoFiniteDiff(),
834850
jacobian=AutoFiniteDiff(),
@@ -850,17 +866,10 @@ end
850866
@test isa(nmpc15.estim, UnscentedKalmanFilter{Float32})
851867
@test isa(nmpc15.optim, JuMP.GenericModel{Float64}) # Ipopt does not support Float32
852868

853-
@test_throws DimensionMismatch NonLinMPC(nonlinmodel, Hp=15, Ewt=[1, 1])
854869
@test_throws ArgumentError NonLinMPC(nonlinmodel)
855-
@test_throws ErrorException NonLinMPC(nonlinmodel, Hp=15, JE = (_,_,_)->0.0)
856-
@test_throws ErrorException NonLinMPC(nonlinmodel, Hp=15, gc = (_,_,_,_)->[0.0], nc=1)
857-
@test_throws ErrorException NonLinMPC(nonlinmodel, Hp=15, gc! = (_,_,_,_)->[0.0], nc=1)
858870
@test_throws ArgumentError NonLinMPC(nonlinmodel, transcription=TrapezoidalCollocation())
859871
@test_throws ArgumentError NonLinMPC(nonlinmodel, transcription=TrapezoidalCollocation(2))
860872
@test_throws ErrorException NonLinMPC(linmodel1, oracle=false, hessian=AutoFiniteDiff())
861-
862-
@test_logs (:warn, Regex(".*")) NonLinMPC(nonlinmodel, Hp=15, JE=(Ue,_,_,_)->Ue)
863-
@test_logs (:warn, Regex(".*")) NonLinMPC(nonlinmodel, Hp=15, gc=(Ue,_,_,_,_)->Ue, nc=0)
864873
end
865874

866875
@testitem "NonLinMPC moves and getinfo" setup=[SetupMPCtests] begin

0 commit comments

Comments
 (0)