From 6ca2912753dd45d691466610bb37be78733c6022 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Wed, 11 Dec 2024 09:03:29 -0500 Subject: [PATCH] test: add `getinfo!` tests with `:gc` field --- Project.toml | 2 +- test/test_predictive_control.jl | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 7cdcf1203..e3ee77f3a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ModelPredictiveControl" uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c" authors = ["Francis Gagnon"] -version = "1.1.2" +version = "1.1.3" [deps] ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e" diff --git a/test/test_predictive_control.jl b/test/test_predictive_control.jl index 2156e2151..ee0ae637a 100644 --- a/test/test_predictive_control.jl +++ b/test/test_predictive_control.jl @@ -730,10 +730,11 @@ end end @testset "NonLinMPC constraint violation" begin - gc(Ue, Ŷe, _ ,p , ϵ) = [p[1]*(Ue .- 4.2 .- ϵ); p[2]*(Ŷe .- 3.14 .- ϵ)] + gc(Ue, Ŷe, _ ,p , ϵ) = [p[1]*(Ue[1:end-1] .- 4.2 .- ϵ); p[2]*(Ŷe[2:end] .- 3.14 .- ϵ)] + Hp=50 linmodel = LinModel(tf([2], [10000, 1]), 3000.0) - nmpc_lin = NonLinMPC(linmodel, Hp=50, Hc=5, gc=gc, nc=2*(50+1), p=[0; 0]) + nmpc_lin = NonLinMPC(linmodel, Hp=Hp, Hc=5, gc=gc, nc=2Hp, p=[0; 0]) setconstraint!(nmpc_lin, x̂min=[-1e6,-Inf], x̂max=[1e6,+Inf]) setconstraint!(nmpc_lin, umin=[-10], umax=[10]) @@ -768,7 +769,7 @@ end @test all(isapprox.(info[:Ŷ], 0.9; atol=1e-1)) setconstraint!(nmpc_lin, ymin=[-100], ymax=[100]) - setconstraint!(nmpc_lin, Ymin=[-0.5; fill(-100, 49)], Ymax=[0.9; fill(+100, 49)]) + setconstraint!(nmpc_lin, Ymin=[-0.5; fill(-100, Hp-1)], Ymax=[0.9; fill(+100, Hp-1)]) moveinput!(nmpc_lin, [-10]) info = getinfo(nmpc_lin) @test info[:Ŷ][end] ≈ -10 atol=1e-1 @@ -792,17 +793,18 @@ end moveinput!(nmpc_lin, [100]) info = getinfo(nmpc_lin) @test all(isapprox.(info[:U], 4.2; atol=1e-1)) + @test all(isapprox.(info[:gc][1:Hp], 0.0; atol=1e-1)) nmpc_lin.p .= [0; 1] moveinput!(nmpc_lin, [100]) info = getinfo(nmpc_lin) @test all(isapprox.(info[:Ŷ], 3.14; atol=1e-1)) - + @test all(isapprox.(info[:gc][Hp+1:end], 0.0; atol=1e-1)) f = (x,u,_,p) -> p.A*x + p.Bu*u h = (x,_,p) -> p.C*x nonlinmodel = NonLinModel(f, h, linmodel.Ts, 1, 1, 1, solver=nothing, p=linmodel) - nmpc = NonLinMPC(nonlinmodel, Hp=50, Hc=5, gc=gc, nc=2*(50+1), p=[0; 0]) + nmpc = NonLinMPC(nonlinmodel, Hp=50, Hc=5, gc=gc, nc=2Hp, p=[0; 0]) setconstraint!(nmpc, x̂min=[-1e6,-Inf], x̂max=[1e6,+Inf]) setconstraint!(nmpc, umin=[-10], umax=[10]) @@ -837,7 +839,7 @@ end @test all(isapprox.(info[:Ŷ], 0.9; atol=1e-1)) setconstraint!(nmpc, ymin=[-100], ymax=[100]) - setconstraint!(nmpc, Ymin=[-0.5; fill(-100, 49)], Ymax=[0.9; fill(+100, 49)]) + setconstraint!(nmpc, Ymin=[-0.5; fill(-100, Hp-1)], Ymax=[0.9; fill(+100, Hp-1)]) moveinput!(nmpc, [-10]) info = getinfo(nmpc) @test info[:Ŷ][end] ≈ -10 atol=1e-1 @@ -861,11 +863,13 @@ end moveinput!(nmpc, [100]) info = getinfo(nmpc) @test all(isapprox.(info[:U], 4.2; atol=1e-1)) + @test all(isapprox.(info[:gc][1:Hp], 0.0; atol=1e-1)) nmpc.p .= [0; 1] moveinput!(nmpc, [100]) info = getinfo(nmpc) @test all(isapprox.(info[:Ŷ], 3.14; atol=1e-1)) + @test all(isapprox.(info[:gc][Hp+1:end], 0.0; atol=1e-1)) end