Skip to content

Commit 46b04fe

Browse files
committed
test: also test Ue argument in JE function for NonLinMPC
1 parent d9f174e commit 46b04fe

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

test/test_predictive_control.jl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -566,25 +566,35 @@ end
566566

567567
@testset "NonLinMPC moves and getinfo" begin
568568
linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10])
569-
nmpc_lin = NonLinMPC(linmodel, Nwt=[0], Hp=1000, Hc=1)
570-
r = [15]
569+
Hp = 1000
570+
nmpc_lin = NonLinMPC(linmodel, Nwt=[0], Hp=Hp, Hc=1)
571+
ry, ru = [15], [4]
571572
preparestate!(nmpc_lin, [10])
572-
u = moveinput!(nmpc_lin, r)
573+
u = moveinput!(nmpc_lin, ry)
573574
@test u [1] atol=5e-2
574-
u = nmpc_lin(r)
575+
u = nmpc_lin(ry)
575576
@test u [1] atol=5e-2
576577
info = getinfo(nmpc_lin)
577578
@test info[:u] u
578-
@test info[:Ŷ][end] r[1] atol=5e-2
579-
Hp = 1000
580-
R̂y = fill(r[1], Hp)
581-
JE = (_ , Ŷe, _ , R̂y) -> sum((Ŷe[2:end] - R̂y).^2)
582-
nmpc = NonLinMPC(linmodel, Mwt=[0], Nwt=[0], Cwt=Inf, Ewt=1, JE=JE, p=R̂y, Hp=Hp, Hc=1)
579+
@test info[:Ŷ][end] ry[1] atol=5e-2
580+
setmodel!(nmpc_lin; Mwt=[0], Lwt=[1])
581+
u = moveinput!(nmpc_lin; R̂u=fill(ru[1], Hp))
582+
@test u [4] atol=5e-2
583+
function JE(Ue, Ŷe, _ , p)
584+
Wy, R̂y, Wu, R̂u = p
585+
return Wy*sum((R̂y-Ŷe[2:end]).^2) + Wu*sum((R̂u-Ue[1:end-1]).^2)
586+
end
587+
R̂y, R̂u = fill(ry[1], Hp), fill(ru[1], Hp)
588+
p = [1, R̂y, 0, R̂u]
589+
nmpc = NonLinMPC(linmodel, Mwt=[0], Nwt=[0], Cwt=Inf, Ewt=1, JE=JE, p=p, Hp=Hp, Hc=1)
583590
preparestate!(nmpc, [10])
584591
u = moveinput!(nmpc)
585592
@test u [1] atol=5e-2
586593
# ensure that the current estimated output is updated for correct JE values:
587594
@test nmpc. evaloutput(nmpc.estim, Float64[])
595+
nmpc.p .= [0, R̂y, 1, R̂u]
596+
u = moveinput!(nmpc)
597+
@test u [4] atol=5e-2
588598
linmodel2 = LinModel([tf(5, [2000, 1]) tf(7, [8000,1])], 3000.0, i_d=[2])
589599
f = (x,u,d,_) -> linmodel2.A*x + linmodel2.Bu*u + linmodel2.Bd*d
590600
h = (x,d,_) -> linmodel2.C*x + linmodel2.Dd*d
@@ -613,7 +623,7 @@ end
613623
@test g_Y0min_end(20.0, 10.0) 0.0
614624
# test gfunc_i(i,::NTuple{N, ForwardDiff.Dual}) :
615625
@test ForwardDiff.gradient(vec->g_Y0min_end(vec...), [20.0, 10.0]) [-5, -5] atol=1e-3
616-
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
626+
linmodel3 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), 0, 0, 3000.0)
617627
nmpc6 = NonLinMPC(linmodel3, Hp=10)
618628
preparestate!(nmpc6, [0])
619629
@test moveinput!(nmpc6, [0]) [0.0]

0 commit comments

Comments
 (0)