Skip to content

Commit 4c6f86e

Browse files
committed
test: LinearMPC.MPC comparisons with custom linear constraints
1 parent b3f236f commit 4c6f86e

1 file changed

Lines changed: 127 additions & 1 deletion

File tree

test/5_test_extensions.jl

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@testitem "LinearMPCext extension" setup=[SetupMPCtests] begin
1+
@testitem "LinearMPCext general" setup=[SetupMPCtests] begin
22
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
33
import LinearMPC
44
model = LinModel(sys, Ts, i_u=1:2)
@@ -50,4 +50,130 @@
5050
"OSQP.\nThe results in closed-loop may be different."),
5151
LinearMPC.MPC(mpc_osqp)
5252
)
53+
54+
end
55+
56+
@testitem "LinearMPCext with Wy weight" setup=[SetupMPCtests] begin
57+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
58+
import LinearMPC
59+
model = LinModel(tf([2], [10, 1]), 3.0)
60+
model = setop!(model, yop=[50], uop=[20])
61+
optim = JuMP.Model(DAQP.Optimizer)
62+
mpc1 = LinMPC(model, Hp=20, Hc=5, Wy=[1], optim=optim)
63+
mpc1 = setconstraint!(mpc1, wmax=[55])
64+
mpc2 = LinearMPC.MPC(mpc1)
65+
function sim_wy(model, mpc1, mpc2, N)
66+
r = [60.0]
67+
u1 = [20.0]
68+
u2 = [20.0]
69+
model.x0 .= 0
70+
u_data1, u_data2 = zeros(1, N), zeros(1, N)
71+
for k in 0:N-1
72+
y = model()
73+
= preparestate!(mpc1, y)
74+
u1 = moveinput!(mpc1, r, lastu=u1)
75+
u2 = LinearMPC.compute_control(mpc2, x̂, r=r, uprev=u2)
76+
u_data1[:, k+1], u_data2[:, k+1] = u1, u2
77+
updatestate!(model, u1)
78+
updatestate!(mpc1, u1, y)
79+
end
80+
return u_data1, u_data2
81+
end
82+
N = 30
83+
u_data1, u_data2 = sim_wy(model, mpc1, mpc2, N)
84+
@test u_data1 u_data2 atol=1e-2 rtol=1e-2
85+
end
86+
87+
@testitem "LinearMPCext with Wu weight" setup=[SetupMPCtests] begin
88+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
89+
import LinearMPC
90+
model = LinModel(tf([2], [10, 1]), 3.0)
91+
model = setop!(model, uop=[20], yop=[50])
92+
optim = JuMP.Model(DAQP.Optimizer)
93+
mpc1 = LinMPC(model, Nwt=[0], Hp=250, Hc=1, Wu=[1], optim=optim)
94+
mpc1 = setconstraint!(mpc1, wmin=[19.0])
95+
mpc2 = LinearMPC.MPC(mpc1)
96+
function sim_wu(model, mpc1, mpc2, N)
97+
r = [40.0]
98+
u1 = [20.0]
99+
u2 = [20.0]
100+
model.x0 .= 0
101+
u_data1, u_data2 = zeros(1, N), zeros(1, N)
102+
for k in 0:N-1
103+
y = model()
104+
= preparestate!(mpc1, y)
105+
u1 = moveinput!(mpc1, r, lastu=u1)
106+
u2 = LinearMPC.compute_control(mpc2, x̂, r=r, uprev=u2)
107+
u_data1[:, k+1], u_data2[:, k+1] = u1, u2
108+
updatestate!(model, u1)
109+
updatestate!(mpc1, u1, y)
110+
end
111+
return u_data1, u_data2
112+
end
113+
N = 30
114+
u_data1, u_data2 = sim_wu(model, mpc1, mpc2, N)
115+
@test u_data1 u_data2 atol=1e-2 rtol=1e-2
116+
end
117+
118+
@testitem "LinearMPCext with Wd weight" setup=[SetupMPCtests] begin
119+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
120+
import LinearMPC
121+
model = LinModel([tf([2], [10, 1]) tf(0.1, [7, 1])], 3.0, i_d=[2])
122+
model = setop!(model, uop=[25], dop=[30], yop=[50])
123+
optim = JuMP.Model(DAQP.Optimizer)
124+
mpc1 = LinMPC(model, Nwt=[0], Hp=250, Hc=1, Wd=[1], Wu=[1], optim=optim)
125+
mpc1 = setconstraint!(mpc1, wmax=[60])
126+
mpc2 = LinearMPC.MPC(mpc1)
127+
function sim_wd(model, mpc1, mpc2, N)
128+
r = [80.0]
129+
d = [30.0]
130+
u1 = [25.0]
131+
u2 = [25.0]
132+
model.x0 .= 0
133+
u_data1, u_data2 = zeros(1, N), zeros(1, N)
134+
for k in 0:N-1
135+
y = model(d)
136+
= preparestate!(mpc1, y, d)
137+
u1 = moveinput!(mpc1, r, d, lastu=u1)
138+
u2 = LinearMPC.compute_control(mpc2, x̂, r=r, d=d, uprev=u2)
139+
u_data1[:, k+1], u_data2[:, k+1] = u1, u2
140+
updatestate!(model, u1, d)
141+
updatestate!(mpc1, u1, y, d)
142+
end
143+
return u_data1, u_data2
144+
end
145+
N = 30
146+
u_data1, u_data2 = sim_wd(model, mpc1, mpc2, N)
147+
@test u_data1 u_data2 atol=1e-2 rtol=1e-2
148+
end
149+
150+
@testitem "LinearMPCext with Wr weight" setup=[SetupMPCtests] begin
151+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
152+
import LinearMPC
153+
model = LinModel(tf([2], [10, 1]), 3.0)
154+
model = setop!(model, yop=[50], uop=[20])
155+
optim = JuMP.Model(DAQP.Optimizer)
156+
mpc1 = LinMPC(model, Hp=20, Hc=5, Wy=[1], Wr=[1], optim=optim)
157+
mpc1 = setconstraint!(mpc1, wmin=[85])
158+
mpc2 = LinearMPC.MPC(mpc1)
159+
function sim_wr(model, mpc1, mpc2, N)
160+
r = [40.0]
161+
u1 = [20.0]
162+
u2 = [20.0]
163+
model.x0 .= 0
164+
u_data1, u_data2 = zeros(1, N), zeros(1, N)
165+
for k in 0:N-1
166+
y = model()
167+
= preparestate!(mpc1, y)
168+
u1 = moveinput!(mpc1, r, lastu=u1)
169+
u2 = LinearMPC.compute_control(mpc2, x̂, r=r, uprev=u2)
170+
u_data1[:, k+1], u_data2[:, k+1] = u1, u2
171+
updatestate!(model, u1)
172+
updatestate!(mpc1, u1, y)
173+
end
174+
return u_data1, u_data2
175+
end
176+
N = 30
177+
u_data1, u_data2 = sim_wr(model, mpc1, mpc2, N)
178+
@test u_data1 u_data2 atol=1e-2 rtol=1e-2
53179
end

0 commit comments

Comments
 (0)