5656 @test all ((- mpc. con. A_Ŷmin[:, end ], - mpc. con. A_Ŷmax[:, end ]) .≈ ([1.0 ,1.1 ], [1.2 ,1.3 ]))
5757end
5858
59-
6059@testset " LinMPC moves" begin
6160 mpc = LinMPC (LinModel (tf (5 , [2 , 1 ]), 3 ), Nwt= [0 ], Hp= 1000 , Hc= 1 )
6261 r = [5 ]
8079 f (x,u,d) = linmodel1. A* x + linmodel1. Bu* u + linmodel1. Bd* d
8180 h (x,d) = linmodel1. C* x + linmodel1. Du* d
8281 nonlinmodel = NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 )
83-
8482 nmpc1 = NonLinMPC (nonlinmodel, Hp= 15 )
8583 @test isa (nmpc1. estim, UnscentedKalmanFilter)
8684 @test size (nmpc1. R̂y, 1 ) == 15 * nmpc1. estim. model. ny
8987 nmpc3 = NonLinMPC (nonlinmodel, Hc= 4 , Cwt= 1e6 )
9088 @test size (nmpc3. Ẽ, 2 ) == 4 * nonlinmodel. nu + 1
9189 @test nmpc3. C == 1e6
92-
9390 nmpc4 = NonLinMPC (nonlinmodel, Mwt= [1 ,2 ], Hp= 15 )
9491 @test nmpc4. M_Hp ≈ Diagonal (diagm (repeat (Float64[1 , 2 ], 15 )))
9592 nmpc5 = NonLinMPC (nonlinmodel, Nwt= [3 ,4 ], Cwt= 1e3 , Hc= 5 )
9693 @test nmpc5. Ñ_Hc ≈ Diagonal (diagm ([repeat (Float64[3 , 4 ], 5 ); [1e3 ]]))
9794 nmpc6 = NonLinMPC (nonlinmodel, Lwt= [0 ,1 ], ru= [0 ,50 ], Hp= 15 )
9895 @test nmpc6. L_Hp ≈ Diagonal (diagm (repeat (Float64[0 , 1 ], 15 )))
9996 @test nmpc6. R̂u ≈ repeat ([0 ,50 ], 15 )
100- nmpc7 = NonLinMPC
97+ nmpc7 = NonLinMPC (nonlinmodel, Ewt= 1e-3 , JE= (UE,ŶE,D̂E) -> UE.* ŶE.* D̂E)
98+ @test nmpc7. E == 1e-3
99+ @test nmpc7. JE ([1 ,2 ],[3 ,4 ],[4 ,6 ]) == [12 , 48 ]
100+ nmpc8 = NonLinMPC (nonlinmodel, optim= JuMP. Model (OSQP. MathOptInterfaceOSQP. Optimizer))
101+ @test solver_name (nmpc8. optim) == " OSQP"
102+ im = InternalModel (nonlinmodel)
103+ nmpc9 = NonLinMPC (im)
104+ @test isa (nmpc9. estim, InternalModel)
105+ end
106+
107+ @testset " NonLinMPC constraints" begin
108+ linmodel1 = LinModel (sys,Ts,i_d= [3 ])
109+ f (x,u,d) = linmodel1. A* x + linmodel1. Bu* u + linmodel1. Bd* d
110+ h (x,d) = linmodel1. C* x + linmodel1. Du* d
111+ nonlinmodel = NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 )
112+ nmpc = NonLinMPC (nonlinmodel, Hp= 1 , Hc= 1 )
113+ setconstraint! (nmpc, umin= [5 , 9.9 ], umax= [100 ,99 ])
114+ @test all ((nmpc. con. Umin, nmpc. con. Umax) .≈ ([5 , 9.9 ], [100 ,99 ]))
115+ setconstraint! (nmpc, Δumin= [- 5 ,- 10 ], Δumax= [6 ,11 ])
116+ @test all ((nmpc. con. ΔŨmin, nmpc. con. ΔŨmax) .≈ ([- 5 ,- 10 ,0 ], [6 ,11 ,Inf ]))
117+ setconstraint! (nmpc, ŷmin= [5 ,10 ],ŷmax= [55 , 35 ])
118+ @test all ((nmpc. con. Ŷmin, nmpc. con. Ŷmax) .≈ ([5 ,10 ], [55 ,35 ]))
119+ setconstraint! (nmpc, c_umin= [0.1 ,0.2 ], c_umax= [0.3 ,0.4 ])
120+ @test all ((- nmpc. con. A_Umin[:, end ], - nmpc. con. A_Umax[:, end ]) .≈ ([0.1 ,0.2 ], [0.3 ,0.4 ]))
121+ setconstraint! (nmpc, c_Δumin= [0.05 ,0.15 ], c_Δumax= [0.25 ,0.35 ])
122+ @test all ((- nmpc. con. A_ΔŨmin[1 : end - 1 , end ], - nmpc. con. A_ΔŨmax[1 : end - 1 , end ]) .≈ ([0.05 ,0.15 ], [0.25 ,0.35 ]))
123+ setconstraint! (nmpc, c_ŷmin= [1.0 ,1.1 ], c_ŷmax= [1.2 ,1.3 ])
124+ println (nmpc. con. A_Ŷmin)
125+ @test all ((- nmpc. con. A_Ŷmin, - nmpc. con. A_Ŷmax) .≈ (zeros (0 ,3 ), zeros (0 ,3 )))
126+ @test all ((nmpc. con. c_Ŷmin, nmpc. con. c_Ŷmax) .≈ ([1.0 ,1.1 ], [1.2 ,1.3 ]))
127+ end
128+
129+ @testset " NonLinMPC moves" begin
130+ linmodel = LinModel (tf (5 , [2 , 1 ]), 3 )
131+ nmpc_lin = NonLinMPC (linmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
132+ r = [5 ]
133+ u = moveinput! (nmpc_lin, r)
134+ @test u ≈ [1 ] atol= 1e-3
135+ u = nmpc_lin (r)
136+ @test u ≈ [1 ] atol= 1e-3
101137
138+ f (x,u,_) = linmodel. A* x + linmodel. Bu* u
139+ h (x,_) = linmodel. C* x
140+ nonlinmodel = NonLinModel (f, h, 3 , 1 , 1 , 1 )
141+ nmpc_nonlin = NonLinMPC (nonlinmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
142+ r = [5 ]
143+ u = moveinput! (nmpc_nonlin, r)
144+ @test u ≈ [1 ] atol= 1e-3
145+ u = nmpc_nonlin (r)
146+ @test u ≈ [1 ] atol= 1e-3
147+ end
102148
149+ @testset " NonLinMPC other methods" begin
150+ linmodel = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
151+ f (x,u,_) = linmodel. A* x + linmodel. Bu* u
152+ h (x,_) = linmodel. C* x
153+ nonlinmodel = NonLinModel (f, h, Ts, 2 , 2 , 2 )
154+ nmpc1 = NonLinMPC (nonlinmodel)
155+ @test initstate! (nmpc1, [10 , 50 ], [20 , 25 ]) ≈ [zeros (2 ); [20 , 25 ]]
156+ setstate! (nmpc1, [1 ,2 ,3 ,4 ])
157+ @test nmpc1. estim. x̂ ≈ [1 ,2 ,3 ,4 ]
103158end
0 commit comments