-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththetahat_dissipativity.py
972 lines (880 loc) · 43.8 KB
/
thetahat_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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
import time
import copy
import cvxpy as cp
import numpy as np
from variable_structs import (
ControllerLTIThetahatParameters,
ControllerThetahatParameters,
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
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(
plant_params: PlantParameters,
LDeltap,
LX,
controller_params: ControllerThetahatParameters,
stacker,
):
if stacker == "numpy":
stacker = np.bmat
elif stacker == "cvxpy":
stacker = cp.bmat
else:
raise ValueError(f"Stacker {stacker} must be 'numpy' or 'cvxpy'.")
K = controller_params
P = plant_params
ytpay11 = P.Ap @ K.R + P.Bpu @ K.NA21
ytpay12 = P.Ap + P.Bpu @ K.NA22 @ P.Cpy
ytpay21 = K.NA11
ytpay22 = K.S @ P.Ap + K.NA12 @ P.Cpy
ytpay = stacker([[ytpay11, ytpay12], [ytpay21, ytpay22]])
ytpbw11 = P.Bpw + P.Bpu @ K.NA22 @ P.Dpyw
ytpbw12 = P.Bpu @ K.Dkuw
ytpbw21 = K.S @ P.Bpw + K.NA12 @ P.Dpyw
ytpbw22 = K.NB
ytpbw = stacker([[ytpbw11, ytpbw12], [ytpbw21, ytpbw22]])
ytpbd1 = P.Bpd + P.Bpu @ K.NA22 @ P.Dpyd
ytpbd2 = K.S @ P.Bpd + K.NA12 @ P.Dpyd
ytpbd = stacker([[ytpbd1], [ytpbd2]])
mvwtcvy11 = P.MDeltapvw.T @ P.Cpv @ K.R + P.MDeltapvw.T @ P.Dpvu @ K.NA21
mvwtcvy12 = P.MDeltapvw.T @ P.Cpv + P.MDeltapvw.T @ P.Dpvu @ K.NA22 @ P.Cpy
mvwtcvy21 = K.NC
mvwtcvy22 = K.Dkvyhat @ P.Cpy
mvwtcvy = stacker([[mvwtcvy11, mvwtcvy12], [mvwtcvy21, mvwtcvy22]])
nxdecey1 = -P.Xde @ (P.Cpe @ K.R + P.Dpeu @ K.NA21)
nxdecey2 = -P.Xde @ (P.Cpe + P.Dpeu @ K.NA22 @ P.Cpy)
nxdecey = stacker([[nxdecey1, nxdecey2]])
ldeltacvy11 = LDeltap @ (P.Cpv @ K.R + P.Dpvu @ K.NA21)
ldeltacvy12 = LDeltap @ (P.Cpv + P.Dpvu @ K.NA22 @ P.Cpy)
ldeltacvy1 = stacker([[ldeltacvy11, ldeltacvy12]])
lxcey1 = LX @ (P.Cpe @ K.R + P.Dpeu @ K.NA21)
lxcey2 = LX @ (P.Cpe + P.Dpeu @ K.NA22 @ P.Cpy)
lxcey = stacker([[lxcey1, lxcey2]])
mvwtdvw11 = P.MDeltapvw.T @ (P.Dpvw + P.Dpvu @ K.NA22 @ P.Dpyw)
mvwtdvw12 = P.MDeltapvw.T @ P.Dpvu @ K.Dkuw
mvwtdvw21 = K.Dkvyhat @ P.Dpyw
mvwtdvw22 = K.Dkvwhat
mvwtdvw = stacker([[mvwtdvw11, mvwtdvw12], [mvwtdvw21, mvwtdvw22]])
mvwtdvd1 = P.MDeltapvw.T @ (P.Dpvd + P.Dpvu @ K.NA22 @ P.Dpyd)
mvwtdvd2 = K.Dkvyhat @ P.Dpyd
mvwtdvd = stacker([[mvwtdvd1], [mvwtdvd2]])
# fmt: off
Mww = stacker([
[P.MDeltapww, np.zeros((P.MDeltapww.shape[0], K.Lambda.shape[1]))],
[np.zeros((K.Lambda.shape[0], P.MDeltapww.shape[1])), -2 * K.Lambda],
])
# fmt: on
Dew = stacker([[P.Dpew + P.Dpeu @ K.NA22 @ P.Dpyw, P.Dpeu @ K.Dkuw]])
Ded = P.Dped + P.Dpeu @ K.NA22 @ P.Dpyd
# fmt: off
ldeltadvw1 = stacker([
[LDeltap @ (P.Dpvw + P.Dpvu @ K.NA22 @ P.Dpyw), LDeltap @ P.Dpvu @ K.Dkuw]
])
# fmt: on
ldeltadvd1 = LDeltap @ (P.Dpvd + P.Dpvu @ K.NA22 @ P.Dpyd)
# Define half the matrix and then add it to its transpose
# Ensure Mww is symmetric. It needs to be for the method overall anyway.
# Note it might be a cvxpy Parameter
if isinstance(P.MDeltapww, np.ndarray):
assert np.allclose(P.MDeltapww, P.MDeltapww.T)
# Ensure Xdd is symmetric. It needs to be for the method overall anyway.
assert np.allclose(P.Xdd, P.Xdd.T)
# fmt: off
row1 = stacker([[
ytpay.T,
np.zeros((
ytpay.T.shape[0],
mvwtdvw.shape[1]
+ P.Xdd.shape[1]
+ LDeltap.shape[0]
+ LX.shape[0],
)),
]])
row2 = stacker([[
ytpbw.T + mvwtcvy,
mvwtdvw + 0.5 * Mww,
np.zeros(
(ytpbw.T.shape[0], P.Xdd.shape[1] + LDeltap.shape[0] + LX.shape[0])
),
]])
row3 = stacker([[
ytpbd.T + nxdecey,
mvwtdvd.T - P.Xde @ Dew,
-P.Xde @ Ded - 0.5 * P.Xdd,
np.zeros((ytpbd.T.shape[0], LDeltap.shape[0] + LX.shape[0])),
]])
row4 = stacker([[
ldeltacvy1,
ldeltadvw1,
ldeltadvd1,
-0.5 * np.eye(ldeltacvy1.shape[0]),
np.zeros((ldeltacvy1.shape[0], LX.shape[0])),
]])
row5 = stacker([[
lxcey,
LX @ Dew,
LX @ Ded,
np.zeros((lxcey.shape[0], ldeltacvy1.shape[0])),
-0.5 * np.eye(lxcey.shape[0]),
]])
mat = stacker([
[row1],
[row2],
[row3],
[row4],
[row5],
])
# fmt: on
mat = mat + mat.T
return mat
class Projector:
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,
# Parameters for tuning condition number of I - RS,
trs_mode, # Either "fixed" or "variable"
min_trs, # Used as the trs value when trs_mode="fixed"
backoff_factor=1.1, # Multiplier for bound on suboptimality
):
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
self.trs_mode = trs_mode
self.min_trs = min_trs
assert self.trs_mode == "fixed", "trs_mode variable deprecated"
self.backoff_factor = backoff_factor
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_backoff_problem()
def _construct_projection_problem(self):
# Parameters: This is the thetahat to be projected into the stabilizing set.
self.proj_pThetahat = ControllerThetahatParameters(
S=cp.Parameter((self.state_size, self.state_size), PSD=True),
R=cp.Parameter((self.state_size, self.state_size), PSD=True),
NA11=cp.Parameter((self.state_size, self.state_size)),
NA12=cp.Parameter((self.state_size, self.input_size)),
NA21=cp.Parameter((self.output_size, self.state_size)),
NA22=cp.Parameter((self.output_size, self.input_size)),
NB=cp.Parameter((self.state_size, self.nonlin_size)),
NC=cp.Parameter((self.nonlin_size, self.state_size)),
Dkuw=cp.Parameter((self.output_size, self.nonlin_size)),
Dkvyhat=cp.Parameter((self.nonlin_size, self.input_size)),
Dkvwhat=cp.Parameter((self.nonlin_size, self.nonlin_size)),
Lambda=cp.Parameter((self.nonlin_size, self.nonlin_size), diag=True),
)
# Enable using the most up-to-date MDeltap during each projection
# TODO: is the symmetric specification here a numerical problem?
self.proj_pLDeltap = cp.Parameter((self.LDeltap.shape[0], self.LDeltap.shape[1]))
self.proj_pMDeltapvv = cp.Parameter((self.plant_params.MDeltapvv.shape[0], self.plant_params.MDeltapvv.shape[1]), symmetric=True)
self.proj_pMDeltapvw = cp.Parameter((self.plant_params.MDeltapvw.shape[0], self.plant_params.MDeltapvw.shape[1]))
self.proj_pMDeltapww = cp.Parameter((self.plant_params.MDeltapww.shape[0], self.plant_params.MDeltapww.shape[1]), symmetric=True)
plant_params = copy.copy(self.plant_params)
plant_params.MDeltapvv = self.proj_pMDeltapvv
plant_params.MDeltapvw = self.proj_pMDeltapvw
plant_params.MDeltapww = self.proj_pMDeltapww
# Variables: This will be the solution of the projection.
self.proj_vThetahat = ControllerThetahatParameters(
S=cp.Variable((self.state_size, self.state_size), PSD=True),
R=cp.Variable((self.state_size, self.state_size), PSD=True),
NA11=cp.Variable((self.state_size, self.state_size)),
NA12=cp.Variable((self.state_size, self.input_size)),
NA21=cp.Variable((self.output_size, self.state_size)),
NA22=cp.Variable((self.output_size, self.input_size)),
NB=cp.Variable((self.state_size, self.nonlin_size)),
NC=cp.Variable((self.nonlin_size, self.state_size)),
Dkuw=cp.Variable((self.output_size, self.nonlin_size)),
Dkvyhat=cp.Variable((self.nonlin_size, self.input_size)),
Dkvwhat=cp.Variable((self.nonlin_size, self.nonlin_size)),
Lambda=cp.Variable((self.nonlin_size, self.nonlin_size), diag=True),
)
mat = construct_dissipativity_matrix(
plant_params=plant_params, # Use the copy
# LDeltap=self.LDeltap,
LDeltap=self.proj_pLDeltap,
LX=self.LX,
controller_params=self.proj_vThetahat,
stacker="cvxpy",
)
# Used for conditioning I - RS
if self.trs_mode == "variable":
self.vtrs = cp.Variable(nonneg=True)
cost_ill_conditioning = -self.vtrs
elif self.trs_mode == "fixed":
self.vtrs = self.min_trs
cost_ill_conditioning = 0
else:
raise ValueError(f"Unexpected trs_mode value of {self.trs_mode}.")
# fmt: off
constraints = [
self.proj_vThetahat.S >> self.eps * np.eye(self.proj_vThetahat.S.shape[0]),
self.proj_vThetahat.R >> self.eps * np.eye(self.proj_vThetahat.R.shape[0]),
self.proj_vThetahat.Lambda >> self.eps * np.eye(self.proj_vThetahat.Lambda.shape[0]),
cp.bmat([
[self.proj_vThetahat.R, self.vtrs * np.eye(self.proj_vThetahat.R.shape[0])],
[self.vtrs * np.eye(self.proj_vThetahat.S.shape[0]), self.proj_vThetahat.S],
]) >> self.eps * np.eye(self.proj_vThetahat.R.shape[0] + self.proj_vThetahat.S.shape[0]),
# Well-posedness condition Lambda Dkvw + Dkvw^T Lambda - 2 Lambda < 0
self.proj_vThetahat.Dkvwhat + self.proj_vThetahat.Dkvwhat.T - 2*self.proj_vThetahat.Lambda << -self.eps * np.eye(self.proj_vThetahat.Lambda.shape[0]),
# Dissipativity condition
mat << 0,
]
if self.trs_mode == "variable":
constraints.append(self.vtrs >= self.min_trs)
cost_projection_error = sum([
cp.sum_squares(self.proj_pThetahat.Dkuw - self.proj_vThetahat.Dkuw),
cp.sum_squares(self.proj_pThetahat.S - self.proj_vThetahat.S),
cp.sum_squares(self.proj_pThetahat.R - self.proj_vThetahat.R),
cp.sum_squares(self.proj_pThetahat.Lambda - self.proj_vThetahat.Lambda),
cp.sum_squares(self.proj_pThetahat.NA11 - self.proj_vThetahat.NA11),
cp.sum_squares(self.proj_pThetahat.NA12 - self.proj_vThetahat.NA12),
cp.sum_squares(self.proj_pThetahat.NA21 - self.proj_vThetahat.NA21),
cp.sum_squares(self.proj_pThetahat.NA22 - self.proj_vThetahat.NA22),
cp.sum_squares(self.proj_pThetahat.NB - self.proj_vThetahat.NB),
cp.sum_squares(self.proj_pThetahat.NC - self.proj_vThetahat.NC),
cp.sum_squares(self.proj_pThetahat.Dkvyhat - self.proj_vThetahat.Dkvyhat),
cp.sum_squares(self.proj_pThetahat.Dkvwhat - self.proj_vThetahat.Dkvwhat),
])
# cost_size = sum([
# cp.sum_squares(self.proj_vThetahat.Dkuw),
# cp.sum_squares(self.proj_vThetahat.S),
# cp.sum_squares(self.proj_vThetahat.R),
# cp.sum_squares(self.proj_vThetahat.Lambda),
# cp.sum_squares(self.proj_vThetahat.NA11),
# cp.sum_squares(self.proj_vThetahat.NA12),
# cp.sum_squares(self.proj_vThetahat.NA21),
# cp.sum_squares(self.proj_vThetahat.NA22),
# cp.sum_squares(self.proj_vThetahat.NB),
# cp.sum_squares(self.proj_vThetahat.NC),
# cp.sum_squares(self.proj_vThetahat.Dkvyhat),
# cp.sum_squares(self.proj_vThetahat.Dkvwhat),
# ])
# fmt: on
# Must be only projection error for the backoff step
# + cost_ill_conditioning # + cost_size
objective = cost_projection_error
self.proj_problem = cp.Problem(cp.Minimize(objective), constraints)
def _construct_backoff_problem(self):
# Parameters: This is the thetahat to be projected into the stabilizing set.
self.backoff_pThetahat = ControllerThetahatParameters(
S=cp.Parameter((self.state_size, self.state_size), PSD=True),
R=cp.Parameter((self.state_size, self.state_size), PSD=True),
NA11=cp.Parameter((self.state_size, self.state_size)),
NA12=cp.Parameter((self.state_size, self.input_size)),
NA21=cp.Parameter((self.output_size, self.state_size)),
NA22=cp.Parameter((self.output_size, self.input_size)),
NB=cp.Parameter((self.state_size, self.nonlin_size)),
NC=cp.Parameter((self.nonlin_size, self.state_size)),
Dkuw=cp.Parameter((self.output_size, self.nonlin_size)),
Dkvyhat=cp.Parameter((self.nonlin_size, self.input_size)),
Dkvwhat=cp.Parameter((self.nonlin_size, self.nonlin_size)),
Lambda=cp.Parameter((self.nonlin_size, self.nonlin_size), diag=True),
)
# Enable using the most up-to-date MDeltap during each projection
self.backoff_pLDeltap = cp.Parameter((self.LDeltap.shape[0], self.LDeltap.shape[1]))
# TODO: is the symmetric specification here creating a numerical problem?
self.backoff_pMDeltapvv = cp.Parameter((self.plant_params.MDeltapvv.shape[0], self.plant_params.MDeltapvv.shape[1]), symmetric=True)
self.backoff_pMDeltapvw = cp.Parameter((self.plant_params.MDeltapvw.shape[0], self.plant_params.MDeltapvw.shape[1]))
self.backoff_pMDeltapww = cp.Parameter((self.plant_params.MDeltapww.shape[0], self.plant_params.MDeltapww.shape[1]), symmetric=True)
plant_params = copy.copy(self.plant_params)
plant_params.MDeltapvv = self.backoff_pMDeltapvv
plant_params.MDeltapvw = self.backoff_pMDeltapvw
plant_params.MDeltapww = self.backoff_pMDeltapww
# Squared projection error
self.backoff_optimal_projection_error = cp.Parameter(nonneg=True)
# Variables: This will be the solution of the projection.
self.backoff_vThetahat = ControllerThetahatParameters(
S=cp.Variable((self.state_size, self.state_size), PSD=True),
R=cp.Variable((self.state_size, self.state_size), PSD=True),
NA11=cp.Variable((self.state_size, self.state_size)),
NA12=cp.Variable((self.state_size, self.input_size)),
NA21=cp.Variable((self.output_size, self.state_size)),
NA22=cp.Variable((self.output_size, self.input_size)),
NB=cp.Variable((self.state_size, self.nonlin_size)),
NC=cp.Variable((self.nonlin_size, self.state_size)),
Dkuw=cp.Variable((self.output_size, self.nonlin_size)),
Dkvyhat=cp.Variable((self.nonlin_size, self.input_size)),
Dkvwhat=cp.Variable((self.nonlin_size, self.nonlin_size)),
Lambda=cp.Variable((self.nonlin_size, self.nonlin_size), diag=True),
)
self.backoff_veps = cp.Variable(pos=True)
mat = construct_dissipativity_matrix(
plant_params=plant_params, # Use copy
# LDeltap=self.LDeltap,
LDeltap=self.backoff_pLDeltap,
LX=self.LX,
controller_params=self.backoff_vThetahat,
stacker="cvxpy",
)
# fmt: off
cost_projection_error = sum([
cp.sum_squares(self.backoff_pThetahat.Dkuw - self.backoff_vThetahat.Dkuw),
cp.sum_squares(self.backoff_pThetahat.S - self.backoff_vThetahat.S),
cp.sum_squares(self.backoff_pThetahat.R - self.backoff_vThetahat.R),
cp.sum_squares(self.backoff_pThetahat.Lambda - self.backoff_vThetahat.Lambda),
cp.sum_squares(self.backoff_pThetahat.NA11 - self.backoff_vThetahat.NA11),
cp.sum_squares(self.backoff_pThetahat.NA12 - self.backoff_vThetahat.NA12),
cp.sum_squares(self.backoff_pThetahat.NA21 - self.backoff_vThetahat.NA21),
cp.sum_squares(self.backoff_pThetahat.NA22 - self.backoff_vThetahat.NA22),
cp.sum_squares(self.backoff_pThetahat.NB - self.backoff_vThetahat.NB),
cp.sum_squares(self.backoff_pThetahat.NC - self.backoff_vThetahat.NC),
cp.sum_squares(self.backoff_pThetahat.Dkvyhat - self.backoff_vThetahat.Dkvyhat),
cp.sum_squares(self.backoff_pThetahat.Dkvwhat - self.backoff_vThetahat.Dkvwhat),
])
# fmt: on
# fmt: off
constraints = [
self.backoff_vThetahat.S >> self.eps * np.eye(self.backoff_vThetahat.S.shape[0]),
self.backoff_vThetahat.R >> self.eps * np.eye(self.backoff_vThetahat.R.shape[0]),
self.backoff_vThetahat.Lambda >> self.eps * np.eye(self.backoff_vThetahat.Lambda.shape[0]),
cp.bmat([
[self.backoff_vThetahat.R, np.eye(self.backoff_vThetahat.R.shape[0])],
[np.eye(self.backoff_vThetahat.S.shape[0]), self.backoff_vThetahat.S],
]) >> self.backoff_veps * np.eye(self.backoff_vThetahat.R.shape[0] + self.backoff_vThetahat.S.shape[0]),
# Well-posedness condition Lambda Dkvw + Dkvw^T Lambda - 2 Lambda < 0
self.backoff_vThetahat.Dkvwhat + self.backoff_vThetahat.Dkvwhat.T - 2*self.backoff_vThetahat.Lambda << -self.eps * np.eye(self.backoff_vThetahat.Lambda.shape[0]),
# Dissipativity condition
mat << 0,
# Backoff
cost_projection_error <= self.backoff_factor**2 * self.backoff_optimal_projection_error
]
# fmt: on
objective = self.backoff_veps
self.backoff_problem = cp.Problem(cp.Maximize(objective), constraints)
def base_project(
self, controller_params: ControllerThetahatParameters, LDeltap, MDeltapvv, MDeltapvw, MDeltapww, solver=cp.MOSEK, **kwargs
):
"""Projects input variables to set corresponding to dissipative controllers."""
K = controller_params
self.proj_pThetahat.Dkuw.value = K.Dkuw
self.proj_pThetahat.S.value = K.S
self.proj_pThetahat.R.value = K.R
self.proj_pThetahat.Lambda.value = K.Lambda
self.proj_pThetahat.NA11.value = K.NA11
self.proj_pThetahat.NA12.value = K.NA12
self.proj_pThetahat.NA21.value = K.NA21
self.proj_pThetahat.NA22.value = K.NA22
self.proj_pThetahat.NB.value = K.NB
self.proj_pThetahat.NC.value = K.NC
self.proj_pThetahat.Dkvyhat.value = K.Dkvyhat
self.proj_pThetahat.Dkvwhat.value = K.Dkvwhat
self.proj_pLDeltap.value = LDeltap
self.proj_pMDeltapvv.value = MDeltapvv
self.proj_pMDeltapvw.value = MDeltapvw
self.proj_pMDeltapww.value = MDeltapww
print("\n\n")
print(f"Thetahat project MDeltapvv: {MDeltapvv}")
print("\n\n")
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 = ControllerThetahatParameters(
self.proj_vThetahat.S.value, self.proj_vThetahat.R.value, self.proj_vThetahat.NA11.value,
self.proj_vThetahat.NA12.value, self.proj_vThetahat.NA21.value, self.proj_vThetahat.NA22.value,
self.proj_vThetahat.NB.value, self.proj_vThetahat.NC.value, self.proj_vThetahat.Dkuw.value,
self.proj_vThetahat.Dkvyhat.value, self.proj_vThetahat.Dkvwhat.value, self.proj_vThetahat.Lambda.value.toarray()
)
# fmt: on
return new_controller_params, {"value": self.proj_problem.value}
def project(self, controller_params: ControllerThetahatParameters, LDeltap, MDeltapvv, MDeltapvw, MDeltapww, solver=cp.MOSEK, **kwargs):
"""Projects input variables to set corresponding to dissipative controllers, allowing some suboptimality to improve conditioning."""
# First solve projection to get optimal projection error
_, info = self.base_project(controller_params, LDeltap, MDeltapvv, MDeltapvw, MDeltapww, solver=solver, **kwargs)
self.backoff_optimal_projection_error.value = info["value"]
# Then solve backoff problem which allows some suboptimality in projection,
# but should improve conditioning of thetahat->theta reconstruction.
K = controller_params
self.backoff_pThetahat.Dkuw.value = K.Dkuw
self.backoff_pThetahat.S.value = K.S
self.backoff_pThetahat.R.value = K.R
self.backoff_pThetahat.Lambda.value = K.Lambda
self.backoff_pThetahat.NA11.value = K.NA11
self.backoff_pThetahat.NA12.value = K.NA12
self.backoff_pThetahat.NA21.value = K.NA21
self.backoff_pThetahat.NA22.value = K.NA22
self.backoff_pThetahat.NB.value = K.NB
self.backoff_pThetahat.NC.value = K.NC
self.backoff_pThetahat.Dkvyhat.value = K.Dkvyhat
self.backoff_pThetahat.Dkvwhat.value = K.Dkvwhat
self.backoff_pLDeltap.value = LDeltap
self.backoff_pMDeltapvv.value = MDeltapvv
self.backoff_pMDeltapvw.value = MDeltapvw
self.backoff_pMDeltapww.value = MDeltapww
try:
# t0 = time.perf_counter()
self.backoff_problem.solve(enforce_dpp=True, solver=solver, **kwargs)
# t1 = time.perf_counter()
# print(f"Backoff 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.backoff_problem.status not in feas_stats:
print(f"Failed to solve with status {self.backoff_problem.status}")
raise Exception()
# fmt: off
new_controller_params = ControllerThetahatParameters(
self.backoff_vThetahat.S.value, self.backoff_vThetahat.R.value, self.backoff_vThetahat.NA11.value,
self.backoff_vThetahat.NA12.value, self.backoff_vThetahat.NA21.value, self.backoff_vThetahat.NA22.value,
self.backoff_vThetahat.NB.value, self.backoff_vThetahat.NC.value, self.backoff_vThetahat.Dkuw.value,
self.backoff_vThetahat.Dkvyhat.value, self.backoff_vThetahat.Dkvwhat.value, self.backoff_vThetahat.Lambda.value.toarray()
)
# fmt: on
# Testing
print(f"Backoff eps value: {self.backoff_veps.value}")
# fmt: off
cost_projection_error = np.sqrt(np.sum([
np.sum(np.square(controller_params.Dkuw - new_controller_params.Dkuw)),
np.sum(np.square(controller_params.S - new_controller_params.S)),
np.sum(np.square(controller_params.R - new_controller_params.R)),
np.sum(np.square(controller_params.Lambda - new_controller_params.Lambda)),
np.sum(np.square(controller_params.NA11 - new_controller_params.NA11)),
np.sum(np.square(controller_params.NA12 - new_controller_params.NA12)),
np.sum(np.square(controller_params.NA21 - new_controller_params.NA21)),
np.sum(np.square(controller_params.NA22 - new_controller_params.NA22)),
np.sum(np.square(controller_params.NB - new_controller_params.NB)),
np.sum(np.square(controller_params.NC - new_controller_params.NC)),
np.sum(np.square(controller_params.Dkvyhat - new_controller_params.Dkvyhat)),
np.sum(np.square(controller_params.Dkvwhat - new_controller_params.Dkvwhat)),
]))
print(f"Projection error before vs after backoff: {np.sqrt(info['value'])} -> {cost_projection_error}")
# fmt: on
return new_controller_params
def is_dissipative(self, controller_params: ControllerThetahatParameters):
"""Check whether given variables already satisfy dissipativity condition."""
# All inputs must be numpy 2d arrays.
# Check S, R, and Lambda are positive definite
if not is_positive_definite(controller_params.S):
print("S is not PD.")
return False
if not is_positive_definite(controller_params.R):
print("R is not PD.")
return False
if not is_positive_definite(controller_params.Lambda):
print("Lambda is not PD.")
return False
# Check [R, I; I, S] is positive definite.
# fmt: off
riis = np.asarray(np.bmat([
[controller_params.R, np.eye(controller_params.R.shape[0])],
[np.eye(controller_params.R.shape[0]), controller_params.S]
]))
# fmt: on
if not is_positive_definite(riis):
print("[R, I; I, S] is not PD.")
return False
# Check well-posedness condition
if not is_positive_definite(
2 * controller_params.Lambda - controller_params.Dkvwhat - controller_params.Dkvwhat.T
):
print("Not well-posed.")
return False
# Check main dissipativity condition.
mat = construct_dissipativity_matrix(
plant_params=self.plant_params,
LDeltap=self.LDeltap,
LX=self.LX,
controller_params=controller_params,
stacker="numpy",
)
# Check dissipativity condition mat <= 0
return is_positive_semidefinite(-mat)
class LTIProjector:
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,
# Parameters for tuning condition number of I - RS,
trs_mode, # Either "fixed" or "variable"
min_trs, # Used as the trs value when trs_mode="fixed"
backoff_factor=1.1, # Multiplier for bound on suboptimality
):
self.plant_params = plant_params
self.eps = eps
self.output_size = output_size
self.state_size = state_size
self.input_size = input_size
self.trs_mode = trs_mode
self.min_trs = min_trs
assert self.trs_mode == "fixed", "trs_mode variable deprecated"
self.backoff_factor = backoff_factor
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_backoff_problem()
def _construct_projection_problem(self):
# Parameters: This is the thetahat to be projected into the stabilizing set.
self.proj_pThetahat = ControllerLTIThetahatParameters(
S=cp.Parameter((self.state_size, self.state_size), PSD=True),
R=cp.Parameter((self.state_size, self.state_size), PSD=True),
NA11=cp.Parameter((self.state_size, self.state_size)),
NA12=cp.Parameter((self.state_size, self.input_size)),
NA21=cp.Parameter((self.output_size, self.state_size)),
NA22=cp.Parameter((self.output_size, self.input_size)),
)
# Enable using the most up-to-date MDeltap during each projection
# TODO: is the symmetric specification here a numerical problem?
self.proj_pLDeltap = cp.Parameter((self.LDeltap.shape[0], self.LDeltap.shape[1]))
self.proj_pMDeltapvv = cp.Parameter((self.plant_params.MDeltapvv.shape[0], self.plant_params.MDeltapvv.shape[1]), symmetric=True)
self.proj_pMDeltapvw = cp.Parameter((self.plant_params.MDeltapvw.shape[0], self.plant_params.MDeltapvw.shape[1]))
self.proj_pMDeltapww = cp.Parameter((self.plant_params.MDeltapww.shape[0], self.plant_params.MDeltapww.shape[1]), symmetric=True)
plant_params = copy.copy(self.plant_params)
plant_params.MDeltapvv = self.proj_pMDeltapvv
plant_params.MDeltapvw = self.proj_pMDeltapvw
plant_params.MDeltapww = self.proj_pMDeltapww
# Variables: This will be the solution of the projection.
self.proj_vThetahat = ControllerLTIThetahatParameters(
S=cp.Variable((self.state_size, self.state_size), PSD=True),
R=cp.Variable((self.state_size, self.state_size), PSD=True),
NA11=cp.Variable((self.state_size, self.state_size)),
NA12=cp.Variable((self.state_size, self.input_size)),
NA21=cp.Variable((self.output_size, self.state_size)),
NA22=cp.Variable((self.output_size, self.input_size)),
)
controller_params = ControllerThetahatParameters(
S=self.proj_vThetahat.S,
R=self.proj_vThetahat.R,
NA11=self.proj_vThetahat.NA11,
NA12=self.proj_vThetahat.NA12,
NA21=self.proj_vThetahat.NA21,
NA22=self.proj_vThetahat.NA22,
NB=np.zeros((self.state_size, self.nonlin_size)),
NC=np.zeros((self.nonlin_size, self.state_size)),
Dkuw=np.zeros((self.output_size, self.nonlin_size)),
Dkvyhat=np.zeros((self.nonlin_size, self.input_size)),
Dkvwhat=np.zeros((self.nonlin_size, self.nonlin_size)),
Lambda=np.zeros((self.nonlin_size, self.nonlin_size)),
)
mat = construct_dissipativity_matrix(
plant_params=plant_params, # Use the copy
# LDeltap=self.LDeltap,
LDeltap=self.proj_pLDeltap,
LX=self.LX,
controller_params=controller_params,
stacker="cvxpy",
)
# Used for conditioning I - RS
if self.trs_mode == "variable":
self.vtrs = cp.Variable(nonneg=True)
cost_ill_conditioning = -self.vtrs
elif self.trs_mode == "fixed":
self.vtrs = self.min_trs
cost_ill_conditioning = 0
else:
raise ValueError(f"Unexpected trs_mode value of {self.trs_mode}.")
# fmt: off
constraints = [
# self.vtrs >= self.min_trs,
self.proj_vThetahat.S >> self.eps * np.eye(self.proj_vThetahat.S.shape[0]),
self.proj_vThetahat.R >> self.eps * np.eye(self.proj_vThetahat.R.shape[0]),
cp.bmat([
[self.proj_vThetahat.R, self.vtrs * np.eye(self.proj_vThetahat.R.shape[0])],
[self.vtrs * np.eye(self.proj_vThetahat.S.shape[0]), self.proj_vThetahat.S],
]) >> self.eps * np.eye(self.proj_vThetahat.R.shape[0] + self.proj_vThetahat.S.shape[0]),
mat << 0,
]
cost_projection_error = sum([
cp.sum_squares(self.proj_pThetahat.S - self.proj_vThetahat.S),
cp.sum_squares(self.proj_pThetahat.R - self.proj_vThetahat.R),
cp.sum_squares(self.proj_pThetahat.NA11 - self.proj_vThetahat.NA11),
cp.sum_squares(self.proj_pThetahat.NA12 - self.proj_vThetahat.NA12),
cp.sum_squares(self.proj_pThetahat.NA21 - self.proj_vThetahat.NA21),
cp.sum_squares(self.proj_pThetahat.NA22 - self.proj_vThetahat.NA22),
])
# cost_size = sum([
# cp.sum_squares(self.proj_vThetahat.S),
# cp.sum_squares(self.proj_vThetahat.R),
# cp.sum_squares(self.proj_vThetahat.NA11),
# cp.sum_squares(self.proj_vThetahat.NA12),
# cp.sum_squares(self.proj_vThetahat.NA21),
# cp.sum_squares(self.proj_vThetahat.NA22),
# ])
# fmt: on
objective = cost_projection_error # + cost_ill_conditioning # + cost_size
self.proj_problem = cp.Problem(cp.Minimize(objective), constraints)
def _construct_backoff_problem(self):
# Parameters: This is the thetahat to be projected into the stabilizing set.
self.backoff_pThetahat = ControllerLTIThetahatParameters(
S=cp.Parameter((self.state_size, self.state_size), PSD=True),
R=cp.Parameter((self.state_size, self.state_size), PSD=True),
NA11=cp.Parameter((self.state_size, self.state_size)),
NA12=cp.Parameter((self.state_size, self.input_size)),
NA21=cp.Parameter((self.output_size, self.state_size)),
NA22=cp.Parameter((self.output_size, self.input_size)),
)
# Enable using the most up-to-date MDeltap during each projection
self.backoff_pLDeltap = cp.Parameter((self.LDeltap.shape[0], self.LDeltap.shape[1]))
# TODO: is the symmetric specification here creating a numerical problem?
self.backoff_pMDeltapvv = cp.Parameter((self.plant_params.MDeltapvv.shape[0], self.plant_params.MDeltapvv.shape[1]), symmetric=True)
self.backoff_pMDeltapvw = cp.Parameter((self.plant_params.MDeltapvw.shape[0], self.plant_params.MDeltapvw.shape[1]))
self.backoff_pMDeltapww = cp.Parameter((self.plant_params.MDeltapww.shape[0], self.plant_params.MDeltapww.shape[1]), symmetric=True)
plant_params = copy.copy(self.plant_params)
plant_params.MDeltapvv = self.backoff_pMDeltapvv
plant_params.MDeltapvw = self.backoff_pMDeltapvw
plant_params.MDeltapww = self.backoff_pMDeltapww
# Squared projection error
self.backoff_optimal_projection_error = cp.Parameter(nonneg=True)
# Variables: This will be the solution of the projection.
self.backoff_vThetahat = ControllerLTIThetahatParameters(
S=cp.Variable((self.state_size, self.state_size), PSD=True),
R=cp.Variable((self.state_size, self.state_size), PSD=True),
NA11=cp.Variable((self.state_size, self.state_size)),
NA12=cp.Variable((self.state_size, self.input_size)),
NA21=cp.Variable((self.output_size, self.state_size)),
NA22=cp.Variable((self.output_size, self.input_size)),
)
self.backoff_veps = cp.Variable(pos=True)
controller_params = ControllerThetahatParameters(
S=self.backoff_vThetahat.S,
R=self.backoff_vThetahat.R,
NA11=self.backoff_vThetahat.NA11,
NA12=self.backoff_vThetahat.NA12,
NA21=self.backoff_vThetahat.NA21,
NA22=self.backoff_vThetahat.NA22,
NB=np.zeros((self.state_size, self.nonlin_size)),
NC=np.zeros((self.nonlin_size, self.state_size)),
Dkuw=np.zeros((self.output_size, self.nonlin_size)),
Dkvyhat=np.zeros((self.nonlin_size, self.input_size)),
Dkvwhat=np.zeros((self.nonlin_size, self.nonlin_size)),
Lambda=np.zeros((self.nonlin_size, self.nonlin_size)),
)
mat = construct_dissipativity_matrix(
plant_params=plant_params, # Use copy
# LDeltap=self.LDeltap,
LDeltap=self.backoff_pLDeltap,
LX=self.LX,
controller_params=controller_params,
stacker="cvxpy",
)
# fmt: off
cost_projection_error = sum([
cp.sum_squares(self.backoff_pThetahat.S - self.backoff_vThetahat.S),
cp.sum_squares(self.backoff_pThetahat.R - self.backoff_vThetahat.R),
cp.sum_squares(self.backoff_pThetahat.NA11 - self.backoff_vThetahat.NA11),
cp.sum_squares(self.backoff_pThetahat.NA12 - self.backoff_vThetahat.NA12),
cp.sum_squares(self.backoff_pThetahat.NA21 - self.backoff_vThetahat.NA21),
cp.sum_squares(self.backoff_pThetahat.NA22 - self.backoff_vThetahat.NA22),
])
constraints = [
self.backoff_vThetahat.S >> self.eps * np.eye(self.backoff_vThetahat.S.shape[0]),
self.backoff_vThetahat.R >> self.eps * np.eye(self.backoff_vThetahat.R.shape[0]),
cp.bmat([
[self.backoff_vThetahat.R, np.eye(self.backoff_vThetahat.R.shape[0])],
[np.eye(self.backoff_vThetahat.S.shape[0]), self.backoff_vThetahat.S],
]) >> self.backoff_veps * np.eye(self.backoff_vThetahat.R.shape[0] + self.backoff_vThetahat.S.shape[0]),
# Dissipativity condition
mat << 0,
# Backoff
cost_projection_error <= self.backoff_factor**2 * self.backoff_optimal_projection_error
]
# fmt: on
objective = self.backoff_veps
self.backoff_problem = cp.Problem(cp.Maximize(objective), constraints)
def base_project(
self, controller_params: ControllerLTIThetahatParameters, LDeltap, MDeltapvv, MDeltapvw, MDeltapww, solver=cp.MOSEK, **kwargs
):
"""Projects input variables to set corresponding to dissipative controllers."""
K = controller_params
self.proj_pThetahat.S.value = K.S
self.proj_pThetahat.R.value = K.R
self.proj_pThetahat.NA11.value = K.NA11
self.proj_pThetahat.NA12.value = K.NA12
self.proj_pThetahat.NA21.value = K.NA21
self.proj_pThetahat.NA22.value = K.NA22
self.proj_pLDeltap.value = LDeltap
self.proj_pMDeltapvv.value = MDeltapvv
self.proj_pMDeltapvw.value = MDeltapvw
self.proj_pMDeltapww.value = MDeltapww
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 = ControllerLTIThetahatParameters(
S=self.proj_vThetahat.S.value,
R=self.proj_vThetahat.R.value,
NA11=self.proj_vThetahat.NA11.value,
NA12=self.proj_vThetahat.NA12.value,
NA21=self.proj_vThetahat.NA21.value,
NA22=self.proj_vThetahat.NA22.value,
)
return new_controller_params, {"value": self.proj_problem.value}
def project(
self, controller_params: ControllerLTIThetahatParameters, LDeltap, MDeltapvv, MDeltapvw, MDeltapww, solver=cp.MOSEK, **kwargs
):
"""Projects input variables to set corresponding to dissipative controllers, allowing some suboptimality to improve conditioning."""
# First solve projection to get optimal projection error
_, info = self.base_project(controller_params, LDeltap, MDeltapvv, MDeltapvw, MDeltapww, solver=solver, **kwargs)
self.backoff_optimal_projection_error.value = info["value"]
# Then solve backoff problem which allows some suboptimality in projection,
# but should improve conditioning of thetahat->theta reconstruction.
K = controller_params
self.backoff_pThetahat.S.value = K.S
self.backoff_pThetahat.R.value = K.R
self.backoff_pThetahat.NA11.value = K.NA11
self.backoff_pThetahat.NA12.value = K.NA12
self.backoff_pThetahat.NA21.value = K.NA21
self.backoff_pThetahat.NA22.value = K.NA22
self.backoff_pLDeltap.value = LDeltap
self.backoff_pMDeltapvv.value = MDeltapvv
self.backoff_pMDeltapvw.value = MDeltapvw
self.backoff_pMDeltapww.value = MDeltapww
try:
# t0 = time.perf_counter()
self.backoff_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.backoff_problem.status not in feas_stats:
print(f"Failed to solve with status {self.backoff_problem.status}")
raise Exception()
# print(f"Projection objective: {self.backoff_problem.value}")
new_controller_params = ControllerLTIThetahatParameters(
S=self.backoff_vThetahat.S.value,
R=self.backoff_vThetahat.R.value,
NA11=self.backoff_vThetahat.NA11.value,
NA12=self.backoff_vThetahat.NA12.value,
NA21=self.backoff_vThetahat.NA21.value,
NA22=self.backoff_vThetahat.NA22.value,
)
# Testing
print(f"Backoff eps value: {self.backoff_veps.value}")
# fmt: off
cost_projection_error = np.sqrt(np.sum([
np.sum(np.square(controller_params.S - new_controller_params.S)),
np.sum(np.square(controller_params.R - new_controller_params.R)),
np.sum(np.square(controller_params.NA11 - new_controller_params.NA11)),
np.sum(np.square(controller_params.NA12 - new_controller_params.NA12)),
np.sum(np.square(controller_params.NA21 - new_controller_params.NA21)),
np.sum(np.square(controller_params.NA22 - new_controller_params.NA22)),
]))
print(f"Projection error before vs after backoff: {np.sqrt(info['value'])} -> {cost_projection_error}")
# fmt: on
return new_controller_params
def is_dissipative(self, controller_params: ControllerLTIThetahatParameters):
"""Check whether given variables already satisfy dissipativity condition."""
# All inputs must be numpy 2d arrays.
# Check S, R, and Lambda are positive definite
if not is_positive_definite(controller_params.S):
print("S is not PD.")
return False
if not is_positive_definite(controller_params.R):
print("R is not PD.")
return False
# Check [R, I; I, S] is positive definite.
# fmt: off
mat = np.asarray(np.bmat([
[controller_params.R, np.eye(controller_params.R.shape[0])],
[np.eye(controller_params.R.shape[0]), controller_params.S]
]))
# fmt: on
if not is_positive_definite(mat):
print("[R, I; I, S] is not PD.")
return False
# Check main dissipativity condition.
K = controller_params
controller_params = ControllerThetahatParameters(
S=K.S,
R=K.R,
NA11=K.NA11,
NA12=K.NA12,
NA21=K.NA21,
NA22=K.NA22,
NB=np.zeros((self.state_size, self.nonlin_size)),
NC=np.zeros((self.nonlin_size, self.state_size)),
Dkuw=np.zeros((self.output_size, self.nonlin_size)),
Dkvyhat=np.zeros((self.nonlin_size, self.input_size)),
Dkvwhat=np.zeros((self.nonlin_size, self.nonlin_size)),
Lambda=np.zeros((self.nonlin_size, self.nonlin_size)),
)
mat = construct_dissipativity_matrix(
plant_params=self.plant_params,
LDeltap=self.LDeltap,
LX=self.LX,
controller_params=controller_params,
stacker="numpy",
)
# Check condition mat <= 0
return is_positive_semidefinite(-mat)