-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathrobust_test.py
More file actions
368 lines (337 loc) · 13.4 KB
/
robust_test.py
File metadata and controls
368 lines (337 loc) · 13.4 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""robust_array_test.py"""
import numpy as np
import pytest
from control import append, minreal, ss, tf
from control.robust import augw, h2syn, hinfsyn, mixsyn
class TestHinf:
@pytest.mark.slycot
def testHinfsyn(self):
"""Test hinfsyn"""
p = ss(-1, [[1, 1]], [[1], [1]], [[0, 1], [1, 0]])
k, cl, gam, rcond = hinfsyn(p, 1, 1)
# from Octave, which also uses SB10AD:
# a= -1; b1= 1; b2= 1; c1= 1; c2= 1; d11= 0; d12= 1; d21= 1; d22= 0;
# g = ss(a,[b1,b2],[c1;c2],[d11,d12;d21,d22]);
# [k,cl] = hinfsyn(g,1,1);
np.testing.assert_array_almost_equal(k.A, [[-3]])
np.testing.assert_array_almost_equal(k.B, [[1]])
np.testing.assert_array_almost_equal(k.C, [[-1]])
np.testing.assert_array_almost_equal(k.D, [[0]])
np.testing.assert_array_almost_equal(cl.A, [[-1, -1], [1, -3]])
np.testing.assert_array_almost_equal(cl.B, [[1], [1]])
np.testing.assert_array_almost_equal(cl.C, [[1, -1]])
np.testing.assert_array_almost_equal(cl.D, [[0]])
# TODO: add more interesting examples
class TestH2:
@pytest.mark.slycot
def testH2syn(self):
"""Test h2syn"""
p = ss(-1, [[1, 1]], [[1], [1]], [[0, 1], [1, 0]])
k = h2syn(p, 1, 1)
# from Octave, which also uses SB10HD for H2 synthesis:
# a= -1; b1= 1; b2= 1; c1= 1; c2= 1; d11= 0; d12= 1; d21= 1; d22= 0;
# g = ss(a,[b1,b2],[c1;c2],[d11,d12;d21,d22]);
# k = h2syn(g,1,1);
# the solution is the same as for the hinfsyn test
np.testing.assert_array_almost_equal(k.A, [[-3]])
np.testing.assert_array_almost_equal(k.B, [[1]])
np.testing.assert_array_almost_equal(k.C, [[-1]])
np.testing.assert_array_almost_equal(k.D, [[0]])
@pytest.mark.filterwarnings("ignore:connect:FutureWarning")
class TestAugw:
# tolerance for system equality
TOL = 1e-8
def siso_almost_equal(self, g, h):
"""siso_almost_equal(g,h) -> None
Raises AssertionError if g and h, two SISO LTI objects, are not almost
equal
"""
# TODO: use pytest's assertion rewriting feature
gmh = tf(minreal(g - h, verbose=False))
if not (gmh.num[0][0] < self.TOL).all():
maxnum = max(abs(gmh.num[0][0]))
raise AssertionError("systems not approx equal; "
"max num. coeff is {}\n"
"sys 1:\n"
"{}\n"
"sys 2:\n"
"{}".format(maxnum, g, h))
@pytest.mark.slycot
def testSisoW1(self):
"""SISO plant with S weighting"""
g = ss([-1.], [1.], [1.], [1.])
w1 = ss([-2], [2.], [1.], [2.])
p = augw(g, w1)
assert p.noutputs == 2
assert p.ninputs == 2
# w->z1 should be w1
self.siso_almost_equal(w1, p[0, 0])
# w->v should be 1
self.siso_almost_equal(ss([], [], [], [1]), p[1, 0])
# u->z1 should be -w1*g
self.siso_almost_equal(-w1 * g, p[0, 1])
# u->v should be -g
self.siso_almost_equal(-g, p[1, 1])
@pytest.mark.slycot
def testSisoW2(self):
"""SISO plant with KS weighting"""
g = ss([-1.], [1.], [1.], [1.])
w2 = ss([-2], [1.], [1.], [2.])
p = augw(g, w2=w2)
assert p.noutputs == 2
assert p.ninputs == 2
# w->z2 should be 0
self.siso_almost_equal(ss([], [], [], 0), p[0, 0])
# w->v should be 1
self.siso_almost_equal(ss([], [], [], [1]), p[1, 0])
# u->z2 should be w2
self.siso_almost_equal(w2, p[0, 1])
# u->v should be -g
self.siso_almost_equal(-g, p[1, 1])
@pytest.mark.slycot
def testSisoW3(self):
"""SISO plant with T weighting"""
g = ss([-1.], [1.], [1.], [1.])
w3 = ss([-2], [1.], [1.], [2.])
p = augw(g, w3=w3)
assert p.noutputs == 2
assert p.ninputs == 2
# w->z3 should be 0
self.siso_almost_equal(ss([], [], [], 0), p[0, 0])
# w->v should be 1
self.siso_almost_equal(ss([], [], [], [1]), p[1, 0])
# u->z3 should be w3*g
self.siso_almost_equal(w3 * g, p[0, 1])
# u->v should be -g
self.siso_almost_equal(-g, p[1, 1])
@pytest.mark.slycot
def testSisoW123(self):
"""SISO plant with all weights"""
g = ss([-1.], [1.], [1.], [1.])
w1 = ss([-2.], [2.], [1.], [2.])
w2 = ss([-3.], [3.], [1.], [3.])
w3 = ss([-4.], [4.], [1.], [4.])
p = augw(g, w1, w2, w3)
assert p.noutputs == 4
assert p.ninputs == 2
# w->z1 should be w1
self.siso_almost_equal(w1, p[0, 0])
# w->z2 should be 0
self.siso_almost_equal(0, p[1, 0])
# w->z3 should be 0
self.siso_almost_equal(0, p[2, 0])
# w->v should be 1
self.siso_almost_equal(ss([], [], [], [1]), p[3, 0])
# u->z1 should be -w1*g
self.siso_almost_equal(-w1 * g, p[0, 1])
# u->z2 should be w2
self.siso_almost_equal(w2, p[1, 1])
# u->z3 should be w3*g
self.siso_almost_equal(w3 * g, p[2, 1])
# u->v should be -g
self.siso_almost_equal(-g, p[3, 1])
@pytest.mark.slycot
def testMimoW1(self):
"""MIMO plant with S weighting"""
g = ss([[-1., -2], [-3, -4]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]])
w1 = ss([-2], [2.], [1.], [2.])
p = augw(g, w1)
assert p.noutputs == 4
assert p.ninputs == 4
# w->z1 should be diag(w1,w1)
self.siso_almost_equal(w1, p[0, 0])
self.siso_almost_equal(0, p[0, 1])
self.siso_almost_equal(0, p[1, 0])
self.siso_almost_equal(w1, p[1, 1])
# w->v should be I
self.siso_almost_equal(1, p[2, 0])
self.siso_almost_equal(0, p[2, 1])
self.siso_almost_equal(0, p[3, 0])
self.siso_almost_equal(1, p[3, 1])
# u->z1 should be -w1*g
self.siso_almost_equal(-w1 * g[0, 0], p[0, 2])
self.siso_almost_equal(-w1 * g[0, 1], p[0, 3])
self.siso_almost_equal(-w1 * g[1, 0], p[1, 2])
self.siso_almost_equal(-w1 * g[1, 1], p[1, 3])
# # u->v should be -g
self.siso_almost_equal(-g[0, 0], p[2, 2])
self.siso_almost_equal(-g[0, 1], p[2, 3])
self.siso_almost_equal(-g[1, 0], p[3, 2])
self.siso_almost_equal(-g[1, 1], p[3, 3])
@pytest.mark.slycot
def testMimoW2(self):
"""MIMO plant with KS weighting"""
g = ss([[-1., -2], [-3, -4]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]])
w2 = ss([-2], [2.], [1.], [2.])
p = augw(g, w2=w2)
assert p.noutputs == 4
assert p.ninputs == 4
# w->z2 should be 0
self.siso_almost_equal(0, p[0, 0])
self.siso_almost_equal(0, p[0, 1])
self.siso_almost_equal(0, p[1, 0])
self.siso_almost_equal(0, p[1, 1])
# w->v should be I
self.siso_almost_equal(1, p[2, 0])
self.siso_almost_equal(0, p[2, 1])
self.siso_almost_equal(0, p[3, 0])
self.siso_almost_equal(1, p[3, 1])
# u->z2 should be w2
self.siso_almost_equal(w2, p[0, 2])
self.siso_almost_equal(0, p[0, 3])
self.siso_almost_equal(0, p[1, 2])
self.siso_almost_equal(w2, p[1, 3])
# # u->v should be -g
self.siso_almost_equal(-g[0, 0], p[2, 2])
self.siso_almost_equal(-g[0, 1], p[2, 3])
self.siso_almost_equal(-g[1, 0], p[3, 2])
self.siso_almost_equal(-g[1, 1], p[3, 3])
@pytest.mark.slycot
def testMimoW3(self):
"""MIMO plant with T weighting"""
g = ss([[-1., -2], [-3, -4]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]])
w3 = ss([-2], [2.], [1.], [2.])
p = augw(g, w3=w3)
assert p.noutputs == 4
assert p.ninputs == 4
# w->z3 should be 0
self.siso_almost_equal(0, p[0, 0])
self.siso_almost_equal(0, p[0, 1])
self.siso_almost_equal(0, p[1, 0])
self.siso_almost_equal(0, p[1, 1])
# w->v should be I
self.siso_almost_equal(1, p[2, 0])
self.siso_almost_equal(0, p[2, 1])
self.siso_almost_equal(0, p[3, 0])
self.siso_almost_equal(1, p[3, 1])
# u->z3 should be w3*g
self.siso_almost_equal(w3 * g[0, 0], p[0, 2])
self.siso_almost_equal(w3 * g[0, 1], p[0, 3])
self.siso_almost_equal(w3 * g[1, 0], p[1, 2])
self.siso_almost_equal(w3 * g[1, 1], p[1, 3])
# # u->v should be -g
self.siso_almost_equal(-g[0, 0], p[2, 2])
self.siso_almost_equal(-g[0, 1], p[2, 3])
self.siso_almost_equal(-g[1, 0], p[3, 2])
self.siso_almost_equal(-g[1, 1], p[3, 3])
@pytest.mark.slycot
def testMimoW123(self):
"""MIMO plant with all weights"""
g = ss([[-1., -2], [-3, -4]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]],
[[1., 0.], [0., 1.]])
# this should be expaned to w1*I
w1 = ss([-2.], [2.], [1.], [2.])
# diagonal weighting
w2 = append(ss([-3.], [3.], [1.], [3.]), ss([-4.], [4.], [1.], [4.]))
# full weighting
w3 = ss([[-4., -5], [-6, -7]],
[[2., 3.], [5., 7.]],
[[11., 13.], [17., 19.]],
[[23., 29.], [31., 37.]])
p = augw(g, w1, w2, w3)
assert p.noutputs == 8
assert p.ninputs == 4
# w->z1 should be w1
self.siso_almost_equal(w1, p[0, 0])
self.siso_almost_equal(0, p[0, 1])
self.siso_almost_equal(0, p[1, 0])
self.siso_almost_equal(w1, p[1, 1])
# w->z2 should be 0
self.siso_almost_equal(0, p[2, 0])
self.siso_almost_equal(0, p[2, 1])
self.siso_almost_equal(0, p[3, 0])
self.siso_almost_equal(0, p[3, 1])
# w->z3 should be 0
self.siso_almost_equal(0, p[4, 0])
self.siso_almost_equal(0, p[4, 1])
self.siso_almost_equal(0, p[5, 0])
self.siso_almost_equal(0, p[5, 1])
# w->v should be I
self.siso_almost_equal(1, p[6, 0])
self.siso_almost_equal(0, p[6, 1])
self.siso_almost_equal(0, p[7, 0])
self.siso_almost_equal(1, p[7, 1])
# u->z1 should be -w1*g
self.siso_almost_equal(-w1 * g[0, 0], p[0, 2])
self.siso_almost_equal(-w1 * g[0, 1], p[0, 3])
self.siso_almost_equal(-w1 * g[1, 0], p[1, 2])
self.siso_almost_equal(-w1 * g[1, 1], p[1, 3])
# u->z2 should be w2
self.siso_almost_equal(w2[0, 0], p[2, 2])
self.siso_almost_equal(w2[0, 1], p[2, 3])
self.siso_almost_equal(w2[1, 0], p[3, 2])
self.siso_almost_equal(w2[1, 1], p[3, 3])
# u->z3 should be w3*g
w3g = w3 * g
self.siso_almost_equal(w3g[0, 0], minreal(p[4, 2]))
self.siso_almost_equal(w3g[0, 1], minreal(p[4, 3]))
self.siso_almost_equal(w3g[1, 0], minreal(p[5, 2]))
self.siso_almost_equal(w3g[1, 1], minreal(p[5, 3]))
# u->v should be -g
self.siso_almost_equal(-g[0, 0], p[6, 2])
self.siso_almost_equal(-g[0, 1], p[6, 3])
self.siso_almost_equal(-g[1, 0], p[7, 2])
self.siso_almost_equal(-g[1, 1], p[7, 3])
@pytest.mark.slycot
def testErrors(self):
"""Error cases handled"""
from control import augw, ss
# no weights
g1by1 = ss(-1, 1, 1, 0)
g2by2 = ss(-np.eye(2), np.eye(2), np.eye(2), np.zeros((2, 2)))
with pytest.raises(ValueError):
augw(g1by1)
# mismatched size of weight and plant
with pytest.raises(ValueError):
augw(g1by1, w1=g2by2)
with pytest.raises(ValueError):
augw(g1by1, w2=g2by2)
with pytest.raises(ValueError):
augw(g1by1, w3=g2by2)
@pytest.mark.filterwarnings("ignore:connect:FutureWarning")
class TestMixsyn:
"""Test control.robust.mixsyn"""
# it's a relatively simple wrapper; compare results with augw, hinfsyn
@pytest.mark.slycot
def testSiso(self):
"""mixsyn with SISO system"""
# Skogestad+Postlethwaite, Multivariable Feedback Control, 1st Ed., Example 2.11
s = tf([1, 0], 1)
# plant
g = 200 / (10 * s + 1) / (0.05 * s + 1) ** 2
# sensitivity weighting
M = 1.5
wb = 10
A = 1e-4
w1 = (s / M + wb) / (s + wb * A)
# KS weighting
w2 = tf(1, 1)
p = augw(g, w1, w2)
kref, clref, gam, rcond = hinfsyn(p, 1, 1)
ktest, cltest, info = mixsyn(g, w1, w2)
# check similar to S+P's example
np.testing.assert_allclose(gam, 1.37, atol=1e-2)
# mixsyn is a convenience wrapper around augw and hinfsyn, so
# results will be exactly the same. Given than, use the lazy
# but fragile testing option.
np.testing.assert_allclose(ktest.A, kref.A)
np.testing.assert_allclose(ktest.B, kref.B)
np.testing.assert_allclose(ktest.C, kref.C)
np.testing.assert_allclose(ktest.D, kref.D)
np.testing.assert_allclose(cltest.A, clref.A)
np.testing.assert_allclose(cltest.B, clref.B)
np.testing.assert_allclose(cltest.C, clref.C)
np.testing.assert_allclose(cltest.D, clref.D)
np.testing.assert_allclose(gam, info[0])
np.testing.assert_allclose(rcond, info[1])