@@ -41,57 +41,67 @@ sys = [ tf(1.90,[18.0,1]) tf(1.90,[18.0,1]) tf(1.90,[18.0,1]);
41
41
end
42
42
43
43
@testset " LinMPC moves and getinfo" begin
44
- mpc1 = LinMPC (LinModel (tf (5 , [2 , 1 ]), 3 ), Nwt= [0 ], Hp= 1000 , Hc= 1 )
45
- r = [5 ]
44
+ linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3 ), yop= [10 ])
45
+ mpc1 = LinMPC (linmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
46
+ r = [15 ]
46
47
u = moveinput! (mpc1, r)
47
48
@test u ≈ [1 ] atol= 1e-2
48
49
u = mpc1 (r)
49
50
@test u ≈ [1 ] atol= 1e-2
50
51
info = getinfo (mpc1)
51
52
@test info[:u ] ≈ u
52
53
@test info[:Ŷ ][end ] ≈ r[1 ] atol= 1e-2
53
- mpc2 = LinMPC (LinModel ( tf ( 5 , [ 2 , 1 ]), 3 ) , Nwt= [0 ], Cwt= Inf , Hp= 1000 , Hc= 1 )
54
- u = moveinput! (mpc2, [ 5 ] )
54
+ mpc2 = LinMPC (linmodel , Nwt= [0 ], Cwt= Inf , Hp= 1000 , Hc= 1 )
55
+ u = moveinput! (mpc2, r )
55
56
@test u ≈ [1 ] atol= 1e-2
56
- mpc3 = LinMPC (LinModel ( tf ( 5 , [ 2 , 1 ]), 3 ) , Mwt= [0 ], Nwt= [0 ], Lwt= [1 ])
57
+ mpc3 = LinMPC (linmodel , Mwt= [0 ], Nwt= [0 ], Lwt= [1 ])
57
58
u = moveinput! (mpc3, [0 ], R̂u= fill (12 , mpc3. Hp))
58
59
@test u ≈ [12 ] atol= 1e-2
59
- mpc_im = LinMPC (InternalModel (LinModel (tf (5 , [2 , 1 ]), 3 )))
60
- ym, u = mpc_im. estim. model () - [5 ], [0.0 ]
60
+
61
+ @test_throws ArgumentError moveinput! (mpc1, [0 ,0 ,0 ])
62
+ @test_throws ArgumentError moveinput! (mpc1, [0 ], [0 ,0 ])
63
+ @test_throws ArgumentError moveinput! (mpc1; D̂ = fill (0 , mpc1. Hp+ 1 ))
64
+ @test_throws ArgumentError moveinput! (mpc1; R̂y = fill (0 , mpc1. Hp+ 1 ))
65
+ @test_throws ArgumentError moveinput! (mpc1; R̂u = fill (0 , mpc1. Hp+ 1 ))
66
+ end
67
+
68
+ @testset " LinMPC step disturbance rejection" begin
69
+ linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3.0 ), yop= [10 ])
70
+ r = [15 ]
71
+ outdist = [5 ]
72
+ mpc_im = LinMPC (InternalModel (linmodel))
73
+ linmodel. x[:] .= 0
74
+ ym, u = linmodel () - outdist, [0.0 ]
61
75
for i= 1 : 25
62
- ym = mpc_im . estim . model () - [ 5 ]
76
+ ym = linmodel () - outdist
63
77
u = moveinput! (mpc_im, r; ym)
64
78
updatestate! (mpc_im, u, ym)
65
- updatestate! (mpc_im . estim . model , u)
79
+ updatestate! (linmodel , u)
66
80
end
67
81
@test u ≈ [2 ] atol= 1e-2
68
- @test ym ≈ [ 5 ] atol= 1e-2
82
+ @test ym ≈ r atol= 1e-2
69
83
mpc_nint_u = LinMPC (SteadyKalmanFilter (LinModel (tf (5 , [2 , 1 ]), 3 ), nint_u= [1 ]))
70
- ym, u = mpc_nint_u. estim. model () - [5 ], [0.0 ]
84
+ linmodel. x[:] .= 0
85
+ ym, u = linmodel () - outdist, [0.0 ]
71
86
for i= 1 : 25
72
- ym = mpc_nint_u . estim . model () - [ 5 ]
87
+ ym = linmodel () - outdist
73
88
u = moveinput! (mpc_nint_u, r; ym)
74
89
updatestate! (mpc_nint_u, u, ym)
75
- updatestate! (mpc_nint_u . estim . model , u)
90
+ updatestate! (linmodel , u)
76
91
end
77
92
@test u ≈ [2 ] atol= 1e-2
78
- @test ym ≈ [ 5 ] atol= 1e-2
93
+ @test ym ≈ r atol= 1e-2
79
94
mpc_nint_ym = LinMPC (SteadyKalmanFilter (LinModel (tf (5 , [2 , 1 ]), 3 ), nint_ym= [1 ]))
80
- ym, u = mpc_nint_ym. estim. model () - [5 ], [0.0 ]
95
+ linmodel. x[:] .= 0
96
+ ym, u = linmodel () - outdist, [0.0 ]
81
97
for i= 1 : 25
82
- ym = mpc_nint_ym . estim . model () - [ 5 ]
98
+ ym = linmodel () - outdist
83
99
u = moveinput! (mpc_nint_ym, r; ym)
84
100
updatestate! (mpc_nint_ym, u, ym)
85
- updatestate! (mpc_nint_ym . estim . model , u)
101
+ updatestate! (linmodel , u)
86
102
end
87
103
@test u ≈ [2 ] atol= 1e-2
88
- @test ym ≈ [5 ] atol= 1e-2
89
-
90
- @test_throws ArgumentError moveinput! (mpc1, [0 ,0 ,0 ])
91
- @test_throws ArgumentError moveinput! (mpc1, [0 ], [0 ,0 ])
92
- @test_throws ArgumentError moveinput! (mpc1; D̂ = fill (0 , mpc1. Hp+ 1 ))
93
- @test_throws ArgumentError moveinput! (mpc1; R̂y = fill (0 , mpc1. Hp+ 1 ))
94
- @test_throws ArgumentError moveinput! (mpc1; R̂u = fill (0 , mpc1. Hp+ 1 ))
104
+ @test ym ≈ r atol= 1e-2
95
105
end
96
106
97
107
@testset " LinMPC other methods" begin
@@ -264,36 +274,46 @@ end
264
274
mpc3 = ExplicitMPC (LinModel (tf (5 , [2 , 1 ]), 3 ), Mwt= [0 ], Nwt= [0 ], Lwt= [1 ])
265
275
u = moveinput! (mpc3, [0 ], R̂u= fill (12 , mpc3. Hp))
266
276
@test u ≈ [12 ] atol= 1e-2
267
- mpc_im = ExplicitMPC (InternalModel (LinModel (tf (5 , [2 , 1 ]), 3 )))
268
- ym, u = mpc_im. estim. model () - [5 ], [0.0 ]
277
+ end
278
+
279
+
280
+ @testset " ExplicitMPC step disturbance rejection" begin
281
+ linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3.0 ), yop= [10 ])
282
+ r = [15 ]
283
+ outdist = [5 ]
284
+ mpc_im = ExplicitMPC (InternalModel (linmodel))
285
+ linmodel. x[:] .= 0
286
+ ym, u = linmodel () - outdist, [0.0 ]
269
287
for i= 1 : 25
270
- ym = mpc_im . estim . model () - [ 5 ]
288
+ ym = linmodel () - outdist
271
289
u = moveinput! (mpc_im, r; ym)
272
290
updatestate! (mpc_im, u, ym)
273
- updatestate! (mpc_im . estim . model , u)
291
+ updatestate! (linmodel , u)
274
292
end
275
293
@test u ≈ [2 ] atol= 1e-2
276
- @test ym ≈ [ 5 ] atol= 1e-2
294
+ @test ym ≈ r atol= 1e-2
277
295
mpc_nint_u = ExplicitMPC (SteadyKalmanFilter (LinModel (tf (5 , [2 , 1 ]), 3 ), nint_u= [1 ]))
278
- ym, u = mpc_nint_u. estim. model () - [5 ], [0.0 ]
296
+ linmodel. x[:] .= 0
297
+ ym, u = linmodel () - outdist, [0.0 ]
279
298
for i= 1 : 25
280
- ym = mpc_nint_u . estim . model () - [ 5 ]
299
+ ym = linmodel () - outdist
281
300
u = moveinput! (mpc_nint_u, r; ym)
282
301
updatestate! (mpc_nint_u, u, ym)
283
- updatestate! (mpc_nint_u . estim . model , u)
302
+ updatestate! (linmodel , u)
284
303
end
285
304
@test u ≈ [2 ] atol= 1e-2
286
- @test ym ≈ [ 5 ] atol= 1e-2
305
+ @test ym ≈ r atol= 1e-2
287
306
mpc_nint_ym = ExplicitMPC (SteadyKalmanFilter (LinModel (tf (5 , [2 , 1 ]), 3 ), nint_ym= [1 ]))
288
- ym, u = mpc_nint_ym. estim. model () - [5 ], [0.0 ]
307
+ linmodel. x[:] .= 0
308
+ ym, u = linmodel () - outdist, [0.0 ]
289
309
for i= 1 : 25
290
- ym = mpc_nint_ym . estim . model () - [ 5 ]
310
+ ym = linmodel () - outdist
291
311
u = moveinput! (mpc_nint_ym, r; ym)
292
312
updatestate! (mpc_nint_ym, u, ym)
293
- updatestate! (mpc_nint_ym . estim . model , u)
313
+ updatestate! (linmodel , u)
294
314
end
295
315
@test u ≈ [2 ] atol= 1e-2
296
- @test ym ≈ [ 5 ] atol= 1e-2
316
+ @test ym ≈ r atol= 1e-2
297
317
end
298
318
299
319
@testset " ExplicitMPC other methods" begin
356
376
end
357
377
358
378
@testset " NonLinMPC moves and getinfo" begin
359
- linmodel = LinModel (tf (5 , [2 , 1 ]), 3.0 )
379
+ linmodel = setop! ( LinModel (tf (5 , [2 , 1 ]), 3.0 ), yop = [ 10 ] )
360
380
nmpc_lin = NonLinMPC (linmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
361
- r = [5 ]
381
+ r = [15 ]
362
382
u = moveinput! (nmpc_lin, r)
363
383
@test u ≈ [1 ] atol= 5e-2
364
384
u = nmpc_lin (r)
@@ -367,14 +387,14 @@ end
367
387
@test info[:u ] ≈ u
368
388
@test info[:Ŷ ][end ] ≈ r[1 ] atol= 5e-2
369
389
Hp = 1000
370
- R̂y = fill (5 , Hp)
390
+ R̂y = fill (r[ 1 ] , Hp)
371
391
JE = (_ , ŶE, _ ) -> sum ((ŶE[2 : end ] - R̂y). ^ 2 )
372
392
nmpc = NonLinMPC (linmodel, Mwt= [0 ], Nwt= [0 ], Cwt= Inf , Ewt= 1 , JE= JE, Hp= Hp, Hc= 1 )
373
393
u = moveinput! (nmpc)
374
394
@test u ≈ [1 ] atol= 5e-2
375
- linmodel = LinModel ([tf (5 , [2 , 1 ]) tf (7 , [8 ,1 ])], 3.0 , i_d= [2 ])
376
- f (x,u,d) = linmodel . A* x + linmodel . Bu* u + linmodel . Bd* d
377
- h (x,d) = linmodel . C* x + linmodel . Dd* d
395
+ linmodel2 = LinModel ([tf (5 , [2 , 1 ]) tf (7 , [8 ,1 ])], 3.0 , i_d= [2 ])
396
+ f (x,u,d) = linmodel2 . A* x + linmodel2 . Bu* u + linmodel2 . Bd* d
397
+ h (x,d) = linmodel2 . C* x + linmodel2 . Dd* d
378
398
nonlinmodel = NonLinModel (f, h, 3.0 , 1 , 2 , 1 , 1 )
379
399
nmpc2 = NonLinMPC (nonlinmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
380
400
d = [0.1 ]
@@ -395,37 +415,45 @@ end
395
415
C_Ymax_end = nmpc5. optim. nlp_model. operators. registered_multivariate_operators[end ]. f
396
416
@test C_Ymax_end (Float64 .((1.0 , 1.0 ))) ≤ 0.0 # test con_nonlinprog_i(i,::NTuple{N, Float64})
397
417
@test C_Ymax_end (Float32 .((1.0 , 1.0 ))) ≤ 0.0 # test con_nonlinprog_i(i,::NTuple{N, Real})
398
- nmpc_im = NonLinMPC (InternalModel (LinModel (tf (5 , [2 , 1 ]), 3 )))
399
- ym, u = nmpc_im. estim. model () - [5 ], [0.0 ]
418
+ end
419
+
420
+ @testset " NonLinMPC step disturbance rejection" begin
421
+ linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3.0 ), yop= [10 ])
422
+ r = [15 ]
423
+ outdist = [5 ]
424
+ nmpc_im = NonLinMPC (InternalModel (linmodel))
425
+ linmodel. x[:] .= 0
426
+ ym, u = linmodel () - outdist, [0.0 ]
400
427
for i= 1 : 25
401
- ym = nmpc_im . estim . model () - [ 5 ]
428
+ ym = linmodel () - outdist
402
429
u = moveinput! (nmpc_im, r; ym)
403
430
updatestate! (nmpc_im, u, ym)
404
- updatestate! (nmpc_im . estim . model , u)
431
+ updatestate! (linmodel , u)
405
432
end
406
433
@test u ≈ [2 ] atol= 1e-2
407
- @test ym ≈ [5 ] atol= 1e-2
408
- nmpc_nint_u = NonLinMPC (SteadyKalmanFilter (LinModel (tf (5 , [2 , 1 ]), 3 ), nint_u= [1 ]))
409
- ym, u = nmpc_nint_u. estim. model () - [5 ], [0.0 ]
434
+ @test ym ≈ r atol= 1e-2
435
+ nmpc_nint_u = NonLinMPC (SteadyKalmanFilter (linmodel, nint_u= [1 ]))
436
+ linmodel. x[:] .= 0
437
+ ym, u = linmodel () - outdist, [0.0 ]
410
438
for i= 1 : 25
411
- ym = nmpc_nint_u . estim . model () - [ 5 ]
439
+ ym = linmodel () - outdist
412
440
u = moveinput! (nmpc_nint_u, r; ym)
413
441
updatestate! (nmpc_nint_u, u, ym)
414
- updatestate! (nmpc_nint_u . estim . model , u)
442
+ updatestate! (linmodel , u)
415
443
end
416
444
@test u ≈ [2 ] atol= 1e-2
417
- @test ym ≈ [5 ] atol= 1e-2
418
- nmpc_nint_ym = NonLinMPC (SteadyKalmanFilter (LinModel (tf (5 , [2 , 1 ]), 3 ), nint_ym= [1 ]))
419
- ym, u = nmpc_nint_ym. estim. model () - [5 ], [0.0 ]
445
+ @test ym ≈ r atol= 1e-2
446
+ nmpc_nint_ym = NonLinMPC (SteadyKalmanFilter (linmodel, nint_ym= [1 ]))
447
+ linmodel. x[:] .= 0
448
+ ym, u = linmodel () - outdist, [0.0 ]
420
449
for i= 1 : 25
421
- ym = nmpc_nint_ym . estim . model () - [ 5 ]
450
+ ym = linmodel () - outdist
422
451
u = moveinput! (nmpc_nint_ym, r; ym)
423
452
updatestate! (nmpc_nint_ym, u, ym)
424
- updatestate! (nmpc_nint_ym . estim . model , u)
453
+ updatestate! (linmodel , u)
425
454
end
426
455
@test u ≈ [2 ] atol= 1e-2
427
- @test ym ≈ [5 ] atol= 1e-2
428
-
456
+ @test ym ≈ r atol= 1e-2
429
457
end
430
458
431
459
@testset " NonLinMPC other methods" begin
0 commit comments