Skip to content

Commit

Permalink
test: add getinfo! tests with :gc field
Browse files Browse the repository at this point in the history
  • Loading branch information
franckgaga committed Dec 11, 2024
1 parent cd2f641 commit 6ca2912
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 10 additions & 6 deletions test/test_predictive_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 6ca2912

Please sign in to comment.