-
Notifications
You must be signed in to change notification settings - Fork 0
/
theta_dissipativity.py
699 lines (621 loc) · 25.5 KB
/
theta_dissipativity.py
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# Dissipative code using the condition that is a BMI in controller parameters and storage function.
import enum
import time
import cvxpy as cp
import numpy as np
from variable_structs import (
ControllerLTIThetaParameters,
ControllerThetaParameters,
PlantParameters,
)
def is_positive_semidefinite(X):
if not np.allclose(X, X.T):
return False
eigvals, _eigvecs = np.linalg.eigh(X)
if np.min(eigvals) < 0:
return False, f"Minimum eigenvalue {np.min(eigvals)} < 0"
return True
def is_positive_definite(X):
# Check symmetric.
if not np.allclose(X, X.T):
return False
# Check PD (np.linalg.cholesky does not check for symmetry)
try:
np.linalg.cholesky(X)
except Exception as _e:
return False
return True
def construct_dissipativity_matrix(
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, P, LDelta, Mvw, Mww, Xdd, Xde, LX, stacker
):
"""Constructs dissipativity condition from closed-loop matrices."""
if stacker == "numpy":
stacker = np.bmat
elif stacker == "cvxpy":
stacker = cp.bmat
else:
raise ValueError(f"Stacker {stacker} must be 'numpy' or 'cvxpy'.")
# F = F1 + F2 + F3
# where F1 has A^T P + P A, F2 is term with M, and F3 is term with X
# fmt: off
F1 = stacker([
[A.T @ P + P @ A, P @ Bw, P @ Bd],
[Bw.T @ P, np.zeros((Bw.shape[1], Bw.shape[1] + Bd.shape[1]))],
[Bd.T @ P, np.zeros((Bd.shape[1], Bw.shape[1] + Bd.shape[1]))]
])
F2 = stacker([
[np.zeros((Cv.shape[1], Cv.shape[1])), Cv.T @ Mvw, np.zeros((Cv.shape[1], Dvd.shape[1]))],
[Mvw.T @ Cv, Mvw.T @ Dvw + Dvw.T @ Mvw + Mww, Mvw.T @ Dvd],
[np.zeros((Dvd.shape[1], Cv.shape[1])), Dvd.T @ Mvw, np.zeros((Dvd.shape[1], Dvd.shape[1]))]
])
F3 = -stacker([
[np.zeros((Ce.shape[1], Ce.shape[1] + Dew.shape[1])), Ce.T @ Xde.T],
[np.zeros((Dew.shape[1], Ce.shape[1] + Dew.shape[1])), Dew.T @ Xde.T],
[Xde @ Ce, Xde @ Dew, Xde @ Ded + Ded.T @ Xde.T + Xdd]
])
F = F1 + F2 + F3
# mat = [F, H^T; H, -I]
H = stacker([
[LDelta @ Cv, LDelta @ Dvw, LDelta @ Dvd],
[LX @ Ce, LX @ Dew, LX @ Ded]
])
mat = stacker([
[F, H.T],
[H, -np.eye(H.shape[0])]
])
# fmt: on
return mat
def construct_closed_loop(
plant_params: PlantParameters,
LDeltap,
controller_params: ControllerThetaParameters,
stacker,
):
Ap = plant_params.Ap
Bpw = plant_params.Bpw
Bpd = plant_params.Bpd
Bpu = plant_params.Bpu
Cpv = plant_params.Cpv
Dpvw = plant_params.Dpvw
Dpvd = plant_params.Dpvd
Dpvu = plant_params.Dpvu
Cpe = plant_params.Cpe
Dpew = plant_params.Dpew
Dped = plant_params.Dped
Dpeu = plant_params.Dpeu
Cpy = plant_params.Cpy
Dpyw = plant_params.Dpyw
Dpyd = plant_params.Dpyd
MDeltapvv = plant_params.MDeltapvv
MDeltapvw = plant_params.MDeltapvw
MDeltapww = plant_params.MDeltapww
Ak = controller_params.Ak
Bkw = controller_params.Bkw
Bky = controller_params.Bky
Ckv = controller_params.Ckv
Dkvw = controller_params.Dkvw
Dkvy = controller_params.Dkvy
Cku = controller_params.Cku
Dkuw = controller_params.Dkuw
Dkuy = controller_params.Dkuy
Lambda = controller_params.Lambda
if stacker == "numpy":
stacker = np.bmat
elif stacker == "cvxpy":
stacker = cp.bmat
else:
raise ValueError(f"Stacker {stacker} must be 'numpy' or 'cvxpy'.")
# fmt: off
A = stacker([
[Ap + Bpu @ Dkuy @ Cpy, Bpu @ Cku],
[Bky @ Cpy, Ak]
])
Bw = stacker([
[Bpw + Bpu @ Dkuy @ Dpyw, Bpu @ Dkuw],
[Bky @ Dpyw, Bkw]
])
Bd = stacker([
[Bpd + Bpu @ Dkuy @ Dpyd],
[Bky @ Dpyd]
])
Cv = stacker([
[Cpv + Dpvu @ Dkuy @ Cpy, Dpvu @ Cku],
[Dkvy @ Cpy, Ckv]
])
Dvw = stacker([
[Dpvw + Dpvu @ Dkuy @ Dpyw, Dpvu @ Dkuw],
[Dkvy @ Dpyw, Dkvw]
])
Dvd = stacker([
[Dpvd + Dpvu @ Dkuy @ Dpyd],
[Dkvy @ Dpyd]
])
Ce = stacker([
[Cpe + Dpeu @ Dkuy @ Cpy, Dpeu @ Cku]
])
Dew = stacker([
[Dpew + Dpeu @ Dkuy @ Dpyw, Dpeu @ Dkuw]
])
Ded = stacker([
[Dped + Dpeu @ Dkuy @ Dpyd]
])
LDelta = np.bmat([
[LDeltap, np.zeros((LDeltap.shape[0], Lambda.shape[1]))],
[np.zeros((Lambda.shape[0], LDeltap.shape[1] + Lambda.shape[1]))]
])
Mvw = stacker([
[MDeltapvw, np.zeros((MDeltapvv.shape[0], Lambda.shape[1]))],
[np.zeros((Lambda.shape[0], MDeltapvw.shape[1])), Lambda]
])
Mww = stacker([
[MDeltapww, np.zeros((MDeltapww.shape[0], Lambda.shape[1]))],
[np.zeros((Lambda.shape[0], MDeltapww.shape[1])), -2*Lambda]
])
# fmt: on
return A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, LDelta, Mvw, Mww
class Projector:
"""Projection and verification related to dissipativity BMI."""
def __init__(
self,
plant_params: PlantParameters,
# Epsilon to be used in enforcing definiteness of conditions
eps,
# Dimensions of variables for controller
nonlin_size,
output_size,
state_size,
input_size,
):
self.plant_params = plant_params
self.eps = eps
self.nonlin_size = nonlin_size
self.output_size = output_size
self.state_size = state_size
self.input_size = input_size
assert is_positive_semidefinite(plant_params.MDeltapvv)
Dm, Vm = np.linalg.eigh(plant_params.MDeltapvv)
self.LDeltap = np.diag(np.sqrt(Dm)) @ Vm.T
assert is_positive_semidefinite(-plant_params.Xee)
Dx, Vx = np.linalg.eigh(-plant_params.Xee)
self.LX = np.diag(np.sqrt(Dx)) @ Vx.T
self._construct_projection_problem()
self._construct_check_dissipativity_problem()
def _construct_projection_problem(self):
# Define projection problem: Projecting theta into BMI parameterized by P and Lambda.
# Parameters
plant_state_size = self.plant_params.Ap.shape[0]
P_size = plant_state_size + self.state_size
self.pprojP = cp.Parameter((P_size, P_size), PSD=True)
pprojLambda = cp.Parameter((self.nonlin_size, self.nonlin_size), diag=True)
pprojAk = cp.Parameter((self.state_size, self.state_size))
pprojBkw = cp.Parameter((self.state_size, self.nonlin_size))
pprojBky = cp.Parameter((self.state_size, self.input_size))
pprojCkv = cp.Parameter((self.nonlin_size, self.state_size))
pprojDkvw = cp.Parameter((self.nonlin_size, self.nonlin_size))
pprojDkvy = cp.Parameter((self.nonlin_size, self.input_size))
pprojCku = cp.Parameter((self.output_size, self.state_size))
pprojDkuw = cp.Parameter((self.output_size, self.nonlin_size))
pprojDkuy = cp.Parameter((self.output_size, self.input_size))
# fmt: off
self.pproj_k = ControllerThetaParameters(
pprojAk, pprojBkw, pprojBky, pprojCkv, pprojDkvw, pprojDkvy,
pprojCku, pprojDkuw, pprojDkuy, pprojLambda
)
# fmt: on
# Variables
vprojAk = cp.Variable((self.state_size, self.state_size))
vprojBkw = cp.Variable((self.state_size, self.nonlin_size))
vprojBky = cp.Variable((self.state_size, self.input_size))
vprojCkv = cp.Variable((self.nonlin_size, self.state_size))
vprojDkvw = cp.Variable((self.nonlin_size, self.nonlin_size))
vprojDkvy = cp.Variable((self.nonlin_size, self.input_size))
vprojCku = cp.Variable((self.output_size, self.state_size))
vprojDkuw = cp.Variable((self.output_size, self.nonlin_size))
vprojDkuy = cp.Variable((self.output_size, self.input_size))
# fmt: off
self.vproj_k = ControllerThetaParameters(
vprojAk, vprojBkw, vprojBky, vprojCkv, vprojDkvw, vprojDkvy,
vprojCku, vprojDkuw, vprojDkuy, None,
)
controller_params = ControllerThetaParameters(
vprojAk, vprojBkw, vprojBky, vprojCkv, vprojDkvw, vprojDkvy,
vprojCku, vprojDkuw, vprojDkuy, self.pproj_k.Lambda
)
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, LDelta, Mvw, Mww = construct_closed_loop(
self.plant_params, self.LDeltap, controller_params, "cvxpy"
)
mat = construct_dissipativity_matrix(
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, self.pprojP,
LDelta, Mvw, Mww,
self.plant_params.Xdd, self.plant_params.Xde, self.LX,
"cvxpy"
)
# fmt: on
constraints = [
self.pprojP >> self.eps * np.eye(self.pprojP.shape[0]),
# Ordinarily only need Lambda PSD, but for the following well-posedness condition need it PD
self.pproj_k.Lambda >> self.eps * np.eye(self.pproj_k.Lambda.shape[0]),
# Well-posedness condition Lambda Dkvw + Dkvw^T Lambda - 2 Lambda < 0
pprojLambda @ vprojDkvw + vprojDkvw.T @ pprojLambda - 2 * pprojLambda
<< -self.eps * np.eye(pprojLambda.shape[0]),
# Dissipativity condition
mat << 0,
]
# fmt: off
cost_projection_error = sum([
cp.sum_squares(pprojAk - vprojAk),
cp.sum_squares(pprojBkw - vprojBkw),
cp.sum_squares(pprojBky - vprojBky),
cp.sum_squares(pprojCkv - vprojCkv),
cp.sum_squares(pprojDkvw - vprojDkvw),
cp.sum_squares(pprojDkvy - vprojDkvy),
cp.sum_squares(pprojCku - vprojCku),
cp.sum_squares(pprojDkuw - vprojDkuw),
cp.sum_squares(pprojDkuy - vprojDkuy),
])
cost_size = sum([
cp.sum_squares(vprojAk),
cp.sum_squares(vprojBkw),
cp.sum_squares(vprojBky),
cp.sum_squares(vprojCkv),
cp.sum_squares(vprojDkvw),
cp.sum_squares(vprojDkvy),
cp.sum_squares(vprojCku),
cp.sum_squares(vprojDkuw),
cp.sum_squares(vprojDkuy),
])
# fmt: on
objective = cost_projection_error
self.proj_problem = cp.Problem(cp.Minimize(objective), constraints)
def project(
self,
controller_params: ControllerThetaParameters,
P,
solver=cp.MOSEK,
**kwargs,
):
"""Projects input variables to set corresponding to dissipative controllers."""
self.pproj_k.Ak.value = controller_params.Ak
self.pproj_k.Bkw.value = controller_params.Bkw
self.pproj_k.Bky.value = controller_params.Bky
self.pproj_k.Ckv.value = controller_params.Ckv
self.pproj_k.Dkvw.value = controller_params.Dkvw
self.pproj_k.Dkvy.value = controller_params.Dkvy
self.pproj_k.Cku.value = controller_params.Cku
self.pproj_k.Dkuw.value = controller_params.Dkuw
self.pproj_k.Dkuy.value = controller_params.Dkuy
self.pproj_k.Lambda.value = controller_params.Lambda
self.pprojP.value = P
try:
# t0 = time.perf_counter()
self.proj_problem.solve(enforce_dpp=True, solver=solver, **kwargs)
# t1 = time.perf_counter()
# print(f"Projection solving took {t1-t0} seconds.")
except Exception as e:
print(f"Failed to solve: {e}")
raise e
feas_stats = [
cp.OPTIMAL,
cp.UNBOUNDED,
cp.OPTIMAL_INACCURATE,
cp.UNBOUNDED_INACCURATE,
]
if self.proj_problem.status not in feas_stats:
print(f"Failed to solve with status {self.proj_problem.status}")
raise Exception()
# print(f"Projection objective: {self.proj_problem.value}")
# fmt: off
new_controller_params = ControllerThetaParameters(
self.vproj_k.Ak.value, self.vproj_k.Bkw.value, self.vproj_k.Bky.value,
self.vproj_k.Ckv.value, self.vproj_k.Dkvw.value, self.vproj_k.Dkvy.value,
self.vproj_k.Cku.value, self.vproj_k.Dkuw.value, self.vproj_k.Dkuy.value,
None
)
# fmt: on
return new_controller_params
def _construct_check_dissipativity_problem(self):
# Define dissipativity verification problem: find if there exist P and Lambda
# that certify a given controller is dissipative.
# Parameters
plant_state_size = self.plant_params.Ap.shape[0]
P_size = plant_state_size + self.state_size
self.pcheckP = cp.Parameter((P_size, P_size), PSD=True)
pcheckLambda = cp.Parameter((self.nonlin_size, self.nonlin_size), diag=True)
pcheckAk = cp.Parameter((self.state_size, self.state_size))
pcheckBkw = cp.Parameter((self.state_size, self.nonlin_size))
pcheckBky = cp.Parameter((self.state_size, self.input_size))
pcheckCkv = cp.Parameter((self.nonlin_size, self.state_size))
pcheckDkvw = cp.Parameter((self.nonlin_size, self.nonlin_size))
pcheckDkvy = cp.Parameter((self.nonlin_size, self.input_size))
pcheckCku = cp.Parameter((self.output_size, self.state_size))
pcheckDkuw = cp.Parameter((self.output_size, self.nonlin_size))
pcheckDkuy = cp.Parameter((self.output_size, self.input_size))
# fmt: off
self.pcheck_k = ControllerThetaParameters(
pcheckAk, pcheckBkw, pcheckBky, pcheckCkv, pcheckDkvw, pcheckDkvy,
pcheckCku, pcheckDkuw, pcheckDkuy, pcheckLambda
)
# fmt: on
# Variables
self.vcheckP = cp.Variable((P_size, P_size), PSD=True)
self.vcheckLambda = cp.Variable((self.nonlin_size, self.nonlin_size), diag=True)
self.vcheckEps = cp.Variable(nonneg=True)
# fmt: off
controller_params = ControllerThetaParameters(
pcheckAk, pcheckBkw, pcheckBky, pcheckCkv, pcheckDkvw, pcheckDkvy,
pcheckCku, pcheckDkuw, pcheckDkuy, self.vcheckLambda
)
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, LDelta, Mvw, Mww = construct_closed_loop(
self.plant_params, self.LDeltap, controller_params, "cvxpy"
)
mat = construct_dissipativity_matrix(
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, self.vcheckP,
LDelta, Mvw, Mww,
self.plant_params.Xdd, self.plant_params.Xde, self.LX,
"cvxpy"
)
# fmt: on
constraints = [
self.vcheckP >> self.eps * np.eye(self.vcheckP.shape[0]),
self.vcheckLambda >> self.eps * np.eye(self.vcheckLambda.shape[0]),
# Well-posedness condition Lambda Dkvw + Dkvw^T Lambda - 2 Lambda < 0
self.vcheckLambda @ pcheckDkvw
+ pcheckDkvw.T @ self.vcheckLambda
- 2 * self.vcheckLambda
<< -self.eps * np.eye(self.vcheckLambda.shape[0]),
# Dissipativity condition
mat << -self.vcheckEps,
self.vcheckEps >= 0,
]
# fmt: off
cost_projection_error = sum([
cp.sum_squares(self.vcheckP - self.pcheckP),
cp.sum_squares(self.vcheckLambda - self.pcheck_k.Lambda),
])
cost_size = sum([
cp.sum_squares(self.vcheckP),
cp.sum_squares(self.vcheckLambda),
])
# fmt: on
objective = -self.vcheckEps
self.check_problem = cp.Problem(cp.Minimize(objective), constraints)
def is_dissipative(
self,
controller_params: ControllerThetaParameters,
P,
solver=cp.MOSEK,
**kwargs,
):
"""Checks if there exist P and Lambda that certify the controller is dissipative."""
self.pcheck_k.Ak.value = controller_params.Ak
self.pcheck_k.Bkw.value = controller_params.Bkw
self.pcheck_k.Bky.value = controller_params.Bky
self.pcheck_k.Ckv.value = controller_params.Ckv
self.pcheck_k.Dkvw.value = controller_params.Dkvw
self.pcheck_k.Dkvy.value = controller_params.Dkvy
self.pcheck_k.Cku.value = controller_params.Cku
self.pcheck_k.Dkuw.value = controller_params.Dkuw
self.pcheck_k.Dkuy.value = controller_params.Dkuy
self.pcheck_k.Lambda.value = controller_params.Lambda
self.pcheckP.value = P
try:
t0 = time.perf_counter()
self.check_problem.solve(enforce_dpp=True, solver=solver, **kwargs)
t1 = time.perf_counter()
# print(f"Projection solving took {t1-t0} seconds.")
except Exception as e:
print(f"Failed to solve: {e}")
return False, None, None
feas_stats = [
cp.OPTIMAL,
cp.UNBOUNDED,
cp.OPTIMAL_INACCURATE,
cp.UNBOUNDED_INACCURATE,
]
if self.check_problem.status not in feas_stats:
print(f"Failed to solve with status {self.check_problem.status}")
return False, None, None
# print(f"Projection objective: {self.check_problem.value}")
newP = self.vcheckP.value
newLambda = self.vcheckLambda.value.toarray()
return True, newP, newLambda
class LTIProjector:
"""Projection and verification related to dissipativity BMI, with LTI controller."""
def __init__(
self,
plant_params: PlantParameters,
# Epsilon to be used in enforcing definiteness of conditions
eps,
# Dimensions of variables for controller
output_size,
state_size,
input_size,
):
self.plant_params = plant_params
self.eps = eps
self.output_size = output_size
self.state_size = state_size
self.input_size = input_size
self.nonlin_size = 1 # placeholder nonlin size used for creating zeros
assert is_positive_semidefinite(plant_params.MDeltapvv)
Dm, Vm = np.linalg.eigh(plant_params.MDeltapvv)
self.LDeltap = np.diag(np.sqrt(Dm)) @ Vm.T
assert is_positive_semidefinite(-plant_params.Xee)
Dx, Vx = np.linalg.eigh(-plant_params.Xee)
self.LX = np.diag(np.sqrt(Dx)) @ Vx.T
self._construct_projection_problem()
self._construct_check_dissipativity_problem()
def _construct_projection_problem(self):
# Define projection problem: Projecting theta into BMI parameterized by P and Lambda.
# Parameters
plant_state_size = self.plant_params.Ap.shape[0]
P_size = plant_state_size + self.state_size
self.pprojP = cp.Parameter((P_size, P_size), PSD=True)
pprojAk = cp.Parameter((self.state_size, self.state_size))
pprojBky = cp.Parameter((self.state_size, self.input_size))
pprojCku = cp.Parameter((self.output_size, self.state_size))
pprojDkuy = cp.Parameter((self.output_size, self.input_size))
self.pproj_k = ControllerLTIThetaParameters(pprojAk, pprojBky, pprojCku, pprojDkuy)
# Variables
vprojAk = cp.Variable((self.state_size, self.state_size))
vprojBky = cp.Variable((self.state_size, self.input_size))
vprojCku = cp.Variable((self.output_size, self.state_size))
vprojDkuy = cp.Variable((self.output_size, self.input_size))
self.vproj_k = ControllerLTIThetaParameters(vprojAk, vprojBky, vprojCku, vprojDkuy)
Bkw = np.zeros((self.state_size, self.nonlin_size))
Ckv = np.zeros((self.nonlin_size, self.state_size))
Dkvw = np.zeros((self.nonlin_size, self.nonlin_size))
Dkvy = np.zeros((self.nonlin_size, self.input_size))
Dkuw = np.zeros((self.output_size, self.nonlin_size))
Lambda = np.zeros((self.nonlin_size, self.nonlin_size))
controller_params = ControllerThetaParameters(
vprojAk, Bkw, vprojBky, Ckv, Dkvw, Dkvy, vprojCku, Dkuw, vprojDkuy, Lambda
)
# fmt: off
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, LDelta, Mvw, Mww = construct_closed_loop(
self.plant_params, self.LDeltap, controller_params, "cvxpy",
)
mat = construct_dissipativity_matrix(
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, self.pprojP,
LDelta, Mvw, Mww,
self.plant_params.Xdd, self.plant_params.Xde, self.LX,
"cvxpy",
)
# fmt: on
constraints = [
self.pprojP >> self.eps * np.eye(self.pprojP.shape[0]),
# Dissipativity condition
mat << 0,
]
# fmt: off
cost_projection_error = sum([
cp.sum_squares(pprojAk - vprojAk),
cp.sum_squares(pprojBky - vprojBky),
cp.sum_squares(pprojCku - vprojCku),
cp.sum_squares(pprojDkuy - vprojDkuy),
])
cost_size = sum([
cp.sum_squares(vprojAk),
cp.sum_squares(vprojBky),
cp.sum_squares(vprojCku),
cp.sum_squares(vprojDkuy),
])
# fmt: on
objective = cost_projection_error
self.proj_problem = cp.Problem(cp.Minimize(objective), constraints)
def project(
self,
controller_params: ControllerLTIThetaParameters,
P,
solver=cp.MOSEK,
**kwargs,
):
"""Projects input variables to set corresponding to dissipative controllers."""
self.pproj_k.Ak.value = controller_params.Ak
self.pproj_k.Bky.value = controller_params.Bky
self.pproj_k.Cku.value = controller_params.Cku
self.pproj_k.Dkuy.value = controller_params.Dkuy
self.pprojP.value = P
try:
# t0 = time.perf_counter()
self.proj_problem.solve(enforce_dpp=True, solver=solver, **kwargs)
# t1 = time.perf_counter()
# print(f"Projection solving took {t1-t0} seconds.")
except Exception as e:
print(f"Failed to solve: {e}")
raise e
feas_stats = [
cp.OPTIMAL,
cp.UNBOUNDED,
cp.OPTIMAL_INACCURATE,
cp.UNBOUNDED_INACCURATE,
]
if self.proj_problem.status not in feas_stats:
print(f"Failed to solve with status {self.proj_problem.status}")
raise Exception()
# print(f"Projection objective: {self.proj_problem.value}")
new_controller_params = ControllerLTIThetaParameters(
self.vproj_k.Ak.value,
self.vproj_k.Bky.value,
self.vproj_k.Cku.value,
self.vproj_k.Dkuy.value,
)
return new_controller_params
def _construct_check_dissipativity_problem(self):
# Define dissipativity verification problem: find if there exists P
# that certify a given controller is dissipative.
# Parameters
plant_state_size = self.plant_params.Ap.shape[0]
P_size = plant_state_size + self.state_size
self.pcheckP = cp.Parameter((P_size, P_size), PSD=True)
pcheckAk = cp.Parameter((self.state_size, self.state_size))
pcheckBky = cp.Parameter((self.state_size, self.input_size))
pcheckCku = cp.Parameter((self.output_size, self.state_size))
pcheckDkuy = cp.Parameter((self.output_size, self.input_size))
self.pcheck_k = ControllerLTIThetaParameters(pcheckAk, pcheckBky, pcheckCku, pcheckDkuy)
# Variables
self.vcheckP = cp.Variable((P_size, P_size), PSD=True)
self.vcheckEps = cp.Variable(nonneg=True)
Bkw = np.zeros((self.state_size, self.nonlin_size))
Ckv = np.zeros((self.nonlin_size, self.state_size))
Dkvw = np.zeros((self.nonlin_size, self.nonlin_size))
Dkvy = np.zeros((self.nonlin_size, self.input_size))
Dkuw = np.zeros((self.output_size, self.nonlin_size))
Lambda = np.zeros((self.nonlin_size, self.nonlin_size))
controller_params = ControllerThetaParameters(
pcheckAk, Bkw, pcheckBky, Ckv, Dkvw, Dkvy, pcheckCku, Dkuw, pcheckDkuy, Lambda
)
# fmt: off
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, LDelta, Mvw, Mww = construct_closed_loop(
self.plant_params, self.LDeltap, controller_params, "cvxpy"
)
mat = construct_dissipativity_matrix(
A, Bw, Bd, Cv, Dvw, Dvd, Ce, Dew, Ded, self.vcheckP,
LDelta, Mvw, Mww,
self.plant_params.Xdd, self.plant_params.Xde, self.LX,
"cvxpy"
)
# fmt: on
constraints = [
self.vcheckP >> self.eps * np.eye(self.vcheckP.shape[0]),
# Dissipativity condition
mat << -self.vcheckEps,
self.vcheckEps >= 0,
]
cost_projection_error = cp.sum_squares(self.vcheckP - self.pcheckP)
cost_size = cp.sum_squares(self.vcheckP)
objective = -self.vcheckEps
self.check_problem = cp.Problem(cp.Minimize(objective), constraints)
def is_dissipative(
self,
controller_params: ControllerLTIThetaParameters,
P,
solver=cp.MOSEK,
**kwargs,
):
"""Checks if there exist P and Lambda that certify the controller is dissipative."""
self.pcheck_k.Ak.value = controller_params.Ak
self.pcheck_k.Bky.value = controller_params.Bky
self.pcheck_k.Cku.value = controller_params.Cku
self.pcheck_k.Dkuy.value = controller_params.Dkuy
self.pcheckP.value = P
try:
# t0 = time.perf_counter()
self.check_problem.solve(enforce_dpp=True, solver=solver, **kwargs)
# t1 = time.perf_counter()
# print(f"Projection solving took {t1-t0} seconds.")
except Exception as e:
print(f"Failed to solve: {e}")
return False, None
feas_stats = [
cp.OPTIMAL,
cp.UNBOUNDED,
cp.OPTIMAL_INACCURATE,
cp.UNBOUNDED_INACCURATE,
]
if self.check_problem.status not in feas_stats:
print(f"Failed to solve with status {self.check_problem.status}")
return False, None
# print(f"Projection objective: {self.check_problem.value}")
newP = self.vcheckP.value
return True, newP