-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path4_test_plot_sim.jl
More file actions
260 lines (245 loc) · 11.1 KB
/
Copy path4_test_plot_sim.jl
File metadata and controls
260 lines (245 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
@testitem "SimModel quick simulation" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
model = LinModel(sys, Ts, i_d=[3])
res = sim!(model, 15)
@test repr(res) == "Simulation results of LinModel with 15 time steps."
@test isa(res.obj, LinModel)
@test length(res.T_data) == 15
@test res.U_data[:, 1] ≈ model.uop .+ 1
@test res.D_data[:, 1] ≈ model.dop
@test res.X_data[:, 1] ≈ zeros(model.nx)
@test_nowarn sim!(model, 15, progress=false)
res_man = SimResult(model, res.U_data, res.Y_data, res.D_data; X_data=res.X_data)
@test res_man.U_data ≈ res.U_data
@test res_man.Y_data ≈ res.Y_data
@test res_man.D_data ≈ res.D_data
@test res_man.X_data ≈ res.X_data
@test_throws ArgumentError SimResult(model, [res.U_data model.uop], res.Y_data, res.D_data)
end
@testitem "SimModel Plots" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, Plots
model = LinModel(sys, Ts, i_d=[3])
res = sim!(model, 15, [1, 3], [-10])
p = plot(res, plotx=true)
@test p[1][1][:x] ≈ res.T_data
@test p[1][1][:y] ≈ res.Y_data[1, :]
@test p[2][1][:y] ≈ res.Y_data[2, :]
@test p[3][1][:y][1:2:end] ≈ res.U_data[1, :]
@test p[4][1][:y][1:2:end] ≈ res.U_data[2, :]
@test p[5][1][:y] ≈ res.D_data[1, :]
@test p[6][1][:y] ≈ res.X_data[1, :]
@test p[7][1][:y] ≈ res.X_data[2, :]
@test p[8][1][:y] ≈ res.X_data[3, :]
@test p[9][1][:y] ≈ res.X_data[4, :]
p = plot(res, ploty=[2])
@test p[1][1][:x] ≈ res.T_data
@test p[1][1][:y] ≈ res.Y_data[2, :]
p = plot(res, ploty=false, plotu=false, plotd=false, plotx=2:4)
@test p[1][1][:x] ≈ res.T_data
@test p[1][1][:y] ≈ res.X_data[2, :]
@test p[2][1][:y] ≈ res.X_data[3, :]
@test p[3][1][:y] ≈ res.X_data[4, :]
end
@testitem "StateEstimator quick simulation" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
estim = SteadyKalmanFilter(LinModel(sys, Ts, i_d=[3]))
res = sim!(estim, 15)
@test isa(res.obj, SteadyKalmanFilter)
@test length(res.T_data) == 15
@test res.U_data[:, 1] ≈ estim.model.uop .+ 1
@test res.Ud_data[:, 1] ≈ estim.model.uop .+ 1
@test res.D_data[:, 1] ≈ estim.model.dop
@test res.X_data[:, 1] ≈ zeros(estim.model.nx)
@test res.X̂_data[:, 1] ≈ zeros(estim.nx̂)
@test_nowarn sim!(estim, 15, progress=false)
res_man = SimResult(
estim, res.U_data, res.Y_data, res.D_data;
X_data=res.X_data, X̂_data=res.X̂_data
)
@test res_man.U_data ≈ res.U_data
@test res_man.Y_data ≈ res.Y_data
@test res_man.D_data ≈ res.D_data
@test res_man.X_data ≈ res.X_data
@test res_man.X̂_data ≈ res.X̂_data
res2 = sim!(estim, 15, x_noise=[0.1, 0.2, 0.3, 0.4])
@test !(res2.X_data ≈ res.X_data)
end
@testitem "StateEstimator Plots" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, Plots
estim = MovingHorizonEstimator(LinModel(sys, Ts, i_d=[3]), He=5)
estim = setconstraint!(estim, x̂min=[-100,-101,-102,-103,-Inf,-Inf])
estim = setconstraint!(estim, x̂max=[+100,+101,+102,+103,+Inf,+Inf])
res = sim!(estim, 15, [1, 3], [-10])
p1 = plot(res, plotx=true)
@test p1[1][1][:x] ≈ res.T_data
@test p1[end-3][1][:y] ≈ res.X_data[1,:]
@test p1[end-2][1][:y] ≈ res.X_data[2,:]
@test p1[end-1][1][:y] ≈ res.X_data[3,:]
@test p1[end-0][1][:y] ≈ res.X_data[4,:]
p2 = plot(res, plotx̂=true, plotx̂min=false, plotx̂max=false)
@test p2[1][1][:x] ≈ res.T_data
@test p2[end-5][1][:y] ≈ res.X̂_data[1,:]
@test p2[end-4][1][:y] ≈ res.X̂_data[2,:]
@test p2[end-3][1][:y] ≈ res.X̂_data[3,:]
@test p2[end-2][1][:y] ≈ res.X̂_data[4,:]
@test p2[end-1][1][:y] ≈ res.X̂_data[5,:]
@test p2[end-0][1][:y] ≈ res.X̂_data[6,:]
p3 = plot(res, plotxwithx̂=true, plotx̂min=false, plotx̂max=false)
@test p3[1][1][:x] ≈ res.T_data
@test p3[end-5][1][:y] ≈ res.X_data[1,:]
@test p3[end-5][2][:y] ≈ res.X̂_data[1,:]
@test p3[end-4][1][:y] ≈ res.X_data[2,:]
@test p3[end-4][2][:y] ≈ res.X̂_data[2,:]
@test p3[end-3][1][:y] ≈ res.X_data[3,:]
@test p3[end-3][2][:y] ≈ res.X̂_data[3,:]
@test p3[end-2][1][:y] ≈ res.X_data[4,:]
@test p3[end-2][2][:y] ≈ res.X̂_data[4,:]
@test p3[end-1][1][:y] ≈ res.X̂_data[5,:]
@test p3[end-0][1][:y] ≈ res.X̂_data[6,:]
p4 = plot(res, plotx̂=true, plotx̂min=true, plotx̂max=false)
@test p4[1][1][:x] ≈ res.T_data
@test all(p4[end-5][2][:y] .≈ -100)
@test all(p4[end-4][2][:y] .≈ -101)
@test all(p4[end-3][2][:y] .≈ -102)
@test all(p4[end-2][2][:y] .≈ -103)
p5 = plot(res, plotx̂=true, plotx̂min=false, plotx̂max=true)
@test p5[1][1][:x] ≈ res.T_data
@test all(p5[end-5][2][:y] .≈ +100)
@test all(p5[end-4][2][:y] .≈ +101)
@test all(p5[end-3][2][:y] .≈ +102)
@test all(p5[end-2][2][:y] .≈ +103)
p6 = plot(res, plotxwithx̂=true, plotx̂min=true, plotx̂max=false)
@test p6[1][1][:x] ≈ res.T_data
@test all(p6[end-5][3][:y] .≈ -100)
@test all(p6[end-4][3][:y] .≈ -101)
@test all(p6[end-3][3][:y] .≈ -102)
@test all(p6[end-2][3][:y] .≈ -103)
p7 = plot(res, plotxwithx̂=true, plotx̂min=false, plotx̂max=true)
@test p7[1][1][:x] ≈ res.T_data
@test all(p7[end-5][3][:y] .≈ +100)
@test all(p7[end-4][3][:y] .≈ +101)
@test all(p7[end-3][3][:y] .≈ +102)
@test all(p7[end-2][3][:y] .≈ +103)
end
@testitem "PredictiveController quick simulation" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
mpc1 = LinMPC(LinModel(sys, Ts, i_d=[3]))
res = sim!(mpc1, 15)
@test isa(res.obj, LinMPC)
@test length(res.T_data) == 15
@test res.Ry_data[:, 1] ≈ mpc1.estim.model.yop .+ 1
@test res.Ud_data ≈ res.U_data
@test res.D_data[:, 1] ≈ mpc1.estim.model.dop
@test res.X_data[:, 1] ≈ zeros(mpc1.estim.model.nx)
@test res.X̂_data[:, 1] ≈ zeros(mpc1.estim.nx̂)
@test_nowarn sim!(mpc1, 15, progress=false)
mpc2 = ExplicitMPC(LinModel(sys, Ts, i_d=[3]))
res = sim!(mpc2, 15)
@test isa(res.obj, ExplicitMPC)
@test length(res.T_data) == 15
@test res.Ry_data[:, 1] ≈ mpc2.estim.model.yop .+ 1
@test res.Ud_data ≈ res.U_data
@test res.D_data[:, 1] ≈ mpc2.estim.model.dop
@test res.X_data[:, 1] ≈ zeros(mpc2.estim.model.nx)
@test res.X̂_data[:, 1] ≈ zeros(mpc2.estim.nx̂)
res_man = SimResult(
mpc1, res.U_data, res.Y_data, res.D_data;
X_data=res.X_data, X̂_data=res.X̂_data,
Ry_data=res.Ry_data
)
@test res_man.U_data ≈ res.U_data
@test res_man.Y_data ≈ res.Y_data
@test res_man.D_data ≈ res.D_data
@test res_man.X_data ≈ res.X_data
@test res_man.X̂_data ≈ res.X̂_data
@test res_man.Ry_data ≈ res.Ry_data
end
@testitem "PredictiveController Plots" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, Plots
estim = MovingHorizonEstimator(LinModel(sys, Ts, i_d=[3]), He=5)
estim = setconstraint!(estim, x̂min=[-100,-101,-102,-103,-104,-105])
estim = setconstraint!(estim, x̂max=[+100,+101,+102,+103,+104,+105])
mpc = LinMPC(estim, Lwt=[0.01, 0.01])
mpc = setconstraint!(mpc, umin=[-50, -51], umax=[52, 53], ymin=[-54,-55], ymax=[56,57])
res = sim!(mpc, 15)
p1 = plot(res, plotŷ=true)
@test p1[1][1][:x] ≈ res.T_data
@test p1[1][1][:y] ≈ res.Y_data[1,:]
@test p1[1][2][:y] ≈ res.Ŷ_data[1,:]
@test p1[2][1][:y] ≈ res.Y_data[2,:]
@test p1[2][2][:y] ≈ res.Ŷ_data[2,:]
p2 = plot(res, plotx=true)
@test p2[1][1][:x] ≈ res.T_data
@test p2[end-3][1][:y] ≈ res.X_data[1,:]
@test p2[end-2][1][:y] ≈ res.X_data[2,:]
@test p2[end-1][1][:y] ≈ res.X_data[3,:]
@test p2[end-0][1][:y] ≈ res.X_data[4,:]
p3 = plot(res, plotx̂=true)
@test p3[1][1][:x] ≈ res.T_data
@test p3[end-5][1][:y] ≈ res.X̂_data[1,:]
@test p3[end-4][1][:y] ≈ res.X̂_data[2,:]
@test p3[end-3][1][:y] ≈ res.X̂_data[3,:]
@test p3[end-2][1][:y] ≈ res.X̂_data[4,:]
@test p3[end-1][1][:y] ≈ res.X̂_data[5,:]
@test p3[end-0][1][:y] ≈ res.X̂_data[6,:]
p4 = plot(res, plotxwithx̂=true)
@test p4[1][1][:x] ≈ res.T_data
@test p4[end-5][1][:y] ≈ res.X_data[1,:]
@test p4[end-5][2][:y] ≈ res.X̂_data[1,:]
@test p4[end-4][1][:y] ≈ res.X_data[2,:]
@test p4[end-4][2][:y] ≈ res.X̂_data[2,:]
@test p4[end-3][1][:y] ≈ res.X_data[3,:]
@test p4[end-3][2][:y] ≈ res.X̂_data[3,:]
@test p4[end-2][1][:y] ≈ res.X_data[4,:]
@test p4[end-2][2][:y] ≈ res.X̂_data[4,:]
@test p4[end-1][1][:y] ≈ res.X̂_data[5,:]
@test p4[end-0][1][:y] ≈ res.X̂_data[6,:]
p5 = plot(res, plotumin=true, plotumax=false, plotymin=false, plotymax=false)
@test p5[1][1][:x] ≈ res.T_data
@test all(p5[end-2][3][:y] .≈ -50.0)
@test all(p5[end-1][3][:y] .≈ -51.0)
p6 = plot(res, plotumin=false, plotumax=true, plotymin=false, plotymax=false)
@test p6[1][1][:x] ≈ res.T_data
@test all(p6[end-2][3][:y] .≈ 52.0)
@test all(p6[end-1][3][:y] .≈ 53.0)
p7 = plot(res, plotumin=false, plotumax=false, plotymin=true, plotymax=false)
@test p7[1][1][:x] ≈ res.T_data
@test all(p7[end-4][3][:y] .≈ -54.0)
@test all(p7[end-3][3][:y] .≈ -55.0)
p8 = plot(res, plotumin=false, plotumax=false, plotymin=false, plotymax=true)
@test p8[1][1][:x] ≈ res.T_data
@test all(p8[end-4][3][:y] .≈ 56.0)
@test all(p8[end-3][3][:y] .≈ 57.0)
p9 = plot(res, plotx̂=true, plotx̂min=true, plotx̂max=false)
@test p9[1][1][:x] ≈ res.T_data
@test all(p9[end-5][2][:y] .≈ -100.0)
@test all(p9[end-4][2][:y] .≈ -101.0)
@test all(p9[end-3][2][:y] .≈ -102.0)
@test all(p9[end-2][2][:y] .≈ -103.0)
@test all(p9[end-1][2][:y] .≈ -104.0)
@test all(p9[end-0][2][:y] .≈ -105.0)
p10 = plot(res, plotx̂=true, plotx̂min=false, plotx̂max=true)
@test p10[1][1][:x] ≈ res.T_data
@test all(p10[end-5][2][:y] .≈ +100.0)
@test all(p10[end-4][2][:y] .≈ +101.0)
@test all(p10[end-3][2][:y] .≈ +102.0)
@test all(p10[end-2][2][:y] .≈ +103.0)
@test all(p10[end-1][2][:y] .≈ +104.0)
@test all(p10[end-0][2][:y] .≈ +105.0)
p11 = plot(res, plotxwithx̂=true, plotx̂min=true, plotx̂max=false)
@test p11[1][1][:x] ≈ res.T_data
@test all(p11[end-5][3][:y] .≈ -100.0)
@test all(p11[end-4][3][:y] .≈ -101.0)
@test all(p11[end-3][3][:y] .≈ -102.0)
@test all(p11[end-2][3][:y] .≈ -103.0)
@test all(p11[end-1][2][:y] .≈ -104.0)
@test all(p11[end-0][2][:y] .≈ -105.0)
p12 = plot(res, plotxwithx̂=true, plotx̂min=false, plotx̂max=true)
@test p12[1][1][:x] ≈ res.T_data
@test all(p12[end-5][3][:y] .≈ +100.0)
@test all(p12[end-4][3][:y] .≈ +101.0)
@test all(p12[end-3][3][:y] .≈ +102.0)
@test all(p12[end-2][3][:y] .≈ +103.0)
@test all(p12[end-1][2][:y] .≈ +104.0)
@test all(p12[end-0][2][:y] .≈ +105.0)
end