Skip to content

Commit ad1e0b9

Browse files
committed
debug : input setpoint ru for NonLinModel now works
added : unit test for input setpoint ru for NonLinMPC
1 parent 80b710d commit ad1e0b9

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

example/test.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using JuMP, Ipopt
2+
3+
optim = Model(Ipopt.Optimizer)
4+
5+
nvar = 1 # nvar = 1 : error, nvar = 2 : works
6+
function Jfunc(ΔŨtup...)
7+
ΔŨ = collect(ΔŨtup)
8+
return sum(ΔŨ.^2)
9+
end
10+
11+
@variable(optim, ΔŨvar[1:nvar])
12+
register(optim, :Jfunc, nvar, Jfunc, autodiff=true)
13+
@NLobjective(optim, Min, Jfunc(ΔŨvar...))

src/controller/nonlinmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ function init_optimization!(mpc::NonLinMPC)
234234
# inspired from https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/tips_and_tricks/#User-defined-functions-with-vector-outputs
235235
Jfunc, Cfunc = let mpc=mpc, model=model, nC=nC, nvar=nvar , nŶ=Hp*ny
236236
last_ΔŨtup_float, last_ΔŨtup_dual = nothing, nothing
237-
Ŷ_cache::DiffCacheType = DiffCache(zeros(nŶ), nvar)
238-
C_cache::DiffCacheType = DiffCache(zeros(nC), nvar)
237+
Ŷ_cache::DiffCacheType = DiffCache(zeros(nŶ), nvar + 3)
238+
C_cache::DiffCacheType = DiffCache(zeros(nC), nvar + 3)
239239
function Jfunc(ΔŨtup::Float64...)
240240
= get_tmp(Ŷ_cache, ΔŨtup[1])
241241
ΔŨ = collect(ΔŨtup)
@@ -356,7 +356,7 @@ function obj_nonlinprog(mpc::NonLinMPC, model::SimModel, Ŷ, ΔŨ::Vector{T})
356356
# --- input setpoint tracking term ---
357357
if !isempty(mpc.R̂u)
358358
êu = mpc.R̂u - U
359-
JR̂u = êu'*mpc.L_Hp*
359+
JR̂u = êu'*mpc.L_Hp*êu
360360
else
361361
JR̂u = 0.0
362362
end

src/precompile_calls.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ y = model()
77
mpc_im = setconstraint!(LinMPC(InternalModel(model)), ŷmin=[45, -Inf])
88
initstate!(mpc_im, model.uop, y)
99
u = mpc_im([50, 30], ym=y)
10-
updatestate!(mpc_im, u, y)
10+
1111
mpc_kf = setconstraint!(LinMPC(KalmanFilter(model)), ŷmin=[45, -Inf])
1212
initstate!(mpc_kf, model.uop, model())
1313
u = mpc_kf([50, 30])
14-
updatestate!(mpc_kf, u, y)
14+
sim!(mpc_kf, 15)
15+
mpc_lo = setconstraint!(LinMPC(Luenberger(model)), ŷmin=[45, -Inf])
16+
initstate!(mpc_lo, model.uop, model())
17+
u = mpc_lo([50, 30])
18+
sim!(mpc_lo, 15)
1519
mpc_ukf = setconstraint!(LinMPC(UnscentedKalmanFilter(model)), ŷmin=[45, -Inf])
1620
initstate!(mpc_ukf, model.uop, model())
1721
u = mpc_ukf([50, 30])
18-
updatestate!(mpc_ukf, u, y)
22+
sim!(mpc_ukf, 15)
1923
mpc_skf = setconstraint!(LinMPC(SteadyKalmanFilter(model)), ŷmin=[45, -Inf])
2024
initstate!(mpc_skf, model.uop, model())
2125
u = mpc_skf([50, 30])
22-
updatestate!(mpc_skf, u, y)
23-
updatestate!(model, u)
26+
sim!(mpc_skf, 15)
27+
28+
sim!(model, 15)
2429

2530
f(x,u,_) = model.A*x + model.Bu*u
2631
h(x,_) = model.C*x
@@ -30,8 +35,8 @@ y = nlmodel()
3035
nmpc_im = setconstraint!(NonLinMPC(InternalModel(nlmodel)), ŷmin=[45, -Inf])
3136
initstate!(nmpc_im, nlmodel.uop, y)
3237
u = nmpc_im([50, 30], ym=y)
33-
updatestate!(nmpc_im, u, y)
38+
sim!(nmpc_im, 15)
3439
nmpc_ukf = setconstraint!(NonLinMPC(UnscentedKalmanFilter(nlmodel)), ŷmin=[45, -Inf])
3540
initstate!(nmpc_ukf, nlmodel.uop, y)
3641
u = nmpc_ukf([50, 30])
37-
updatestate!(nmpc_ukf, u, y)
42+
sim!(nmpc_ukf, 15)

test/test_predictive_control.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ end
8787
nmpc0 = NonLinMPC(linmodel1, Hp=15)
8888
@test isa(nmpc0.estim, SteadyKalmanFilter)
8989
f(x,u,d) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
90-
h(x,d) = linmodel1.C*x + linmodel1.Du*d
90+
h(x,d) = linmodel1.C*x + linmodel1.Dd*d
9191
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1)
9292
nmpc1 = NonLinMPC(nonlinmodel, Hp=15)
9393
@test isa(nmpc1.estim, UnscentedKalmanFilter)
@@ -117,7 +117,7 @@ end
117117
@testset "NonLinMPC constraints" begin
118118
linmodel1 = LinModel(sys,Ts,i_d=[3])
119119
f(x,u,d) = linmodel1.A*x + linmodel1.Bu*u + linmodel1.Bd*d
120-
h(x,d) = linmodel1.C*x + linmodel1.Du*d
120+
h(x,d) = linmodel1.C*x + linmodel1.Dd*d
121121
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1)
122122
nmpc = NonLinMPC(nonlinmodel, Hp=1, Hc=1)
123123
setconstraint!(nmpc, umin=[5, 9.9], umax=[100,99])
@@ -146,19 +146,24 @@ end
146146
info, _ = getinfo(nmpc_lin)
147147
@test info[:u] u
148148
@test info[:Ŷ][end] 5 atol=1e-2
149-
150149
f(x,u,_) = linmodel.A*x + linmodel.Bu*u
151150
h(x,_) = linmodel.C*x
152151
nonlinmodel = NonLinModel(f, h, 3, 1, 1, 1)
153-
nmpc_nonlin = NonLinMPC(nonlinmodel, Nwt=[0], Hp=1000, Hc=1)
152+
nmpc1 = NonLinMPC(nonlinmodel, Nwt=[0], Hp=1000, Hc=1)
154153
r = [5]
155-
u = moveinput!(nmpc_nonlin, r)
154+
u = moveinput!(nmpc1, r)
156155
@test u [1] atol=1e-2
157-
u = nmpc_nonlin(r)
156+
u = nmpc1(r)
158157
@test u [1] atol=1e-2
159-
info, _ = getinfo(nmpc_nonlin)
158+
info, _ = getinfo(nmpc1)
160159
@test info[:u] u
161160
@test info[:Ŷ][end] 5 atol=1e-2
161+
nmpc2 = NonLinMPC(nonlinmodel, Nwt=[0], Cwt=Inf, Hp=1000, Hc=1)
162+
u = moveinput!(nmpc2, [5])
163+
@test u [1] atol=1e-2
164+
nmpc3 = NonLinMPC(nonlinmodel, Mwt=[0], Nwt=[0], Lwt=[1], ru=[12])
165+
u = moveinput!(nmpc3, [0])
166+
@test u [12] atol=1e-2
162167
end
163168

164169
@testset "NonLinMPC other methods" begin
@@ -170,4 +175,4 @@ end
170175
@test initstate!(nmpc1, [10, 50], [20, 25]) [zeros(2); [20, 25]]
171176
setstate!(nmpc1, [1,2,3,4])
172177
@test nmpc1.estim. [1,2,3,4]
173-
end
178+
end

0 commit comments

Comments
 (0)