Repository navigation
Expand file tree
/
Copy pathlmis.py
More file actions
632 lines (492 loc) · 21.8 KB
/
Copy pathlmis.py
File metadata and controls
632 lines (492 loc) · 21.8 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
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
import torch.nn as nn
from torch.nn.parameter import Parameter
import geotorch as geo
import cvxpy as cp
import numpy as np
import torch
from torch.nn import Module
from torch import Tensor
from models import NNLinear
from utils import find_module
'''
Utility functions
'''
def block_diag(arr_list):
'''create a block diagonal matrix from a list of cvxpy matrices'''
# rows and cols of block diagonal matrix
m = np.sum([arr.shape[0] for arr in arr_list])
n = np.sum([arr.shape[1] for arr in arr_list])
# loop to create the list for the bmat function
block_list = [] # list for bmat function
ind = np.array([0,0])
for arr in arr_list:
# index of the end of arr in the block diagonal matrix
ind += arr.shape
# list of one row of blocks
horz_list = [arr]
# block of zeros to the left of arr
zblock_l = np.zeros((arr.shape[0], ind[1]-arr.shape[1]))
if zblock_l.shape[1] > 0:
horz_list.insert(0, zblock_l)
# block of zeros to the right of arr
zblock_r = np.zeros((arr.shape[0], n-ind[1]))
if zblock_r.shape[1] > 0:
horz_list.append(zblock_r)
block_list.append(horz_list)
B = cp.bmat(block_list)
return B
'''
Continuous time LMI versions
'''
class LMI_Lyap(Module):
'''
A base class for all LMI based on Lyapunov stability for a continuous time linear system
M = A^TP + PA
To create a new LMI make it child of this class and override init_lmi and forward methods.
If more than 1 matrix for Lyapunov certificate add a method like symP_
'''
def __init__(self, A : torch.Tensor, epsilon : float = 1e-6) -> None:
super(LMI_Lyap, self).__init__()
self.P = Parameter(torch.empty_like(A))
#self.P = Parameter(torch.empty_like(A))
geo.positive_definite(self, "P")
M, P = self.init_lmi(A,epsilon = epsilon)
self.P = Parameter(P.requires_grad_(True))
self.A = A
self.nm = M.shape[0]
def init_lmi(self, A : torch.Tensor, epsilon = 1e-6, solver = "MOSEK"):
A = A.detach().numpy()
print(" Initializing Lyapunov LMI \n")
nx = A.shape[0]
P = cp.Variable((nx,nx), 'P', PSD=True)
M = A.T@P + P@A
constraints = [M << -np.eye(nx)*epsilon, P -(epsilon)*np.eye(nx)>> 0]
objective = cp.Minimize(0) # Feasibility problem
prob = cp.Problem(objective, constraints= constraints)
prob.solve(solver)
if prob.status not in ["infeasible", "unbounded"]:
# Otherwise, problem.value is inf or -inf, respectively.
print(" P eigenvalues : \n")
eigVal = np.linalg.eig(P.value)[0]
print(eigVal)
else:
raise ValueError("SDP problem is infeasible or unbounded")
P = torch.Tensor(P.value).to(dtype=torch.float32)
M = torch.Tensor(M.value).to(dtype=torch.float32)
return M, P
def symP_(self):
self.P = Parameter(0.5*(self.P + self.P.T))
def forward(self):
self.symP_()
eig_val, _ = torch.linalg.eig(self.P)
if not torch.all(torch.real(eig_val)>0):
raise AssertionError("P is not SDP")
M = -(self.A.T@self.P + self.P@self.A)
return M # M must be SDP
class LMI_HInf(Module):
def __init__(self, model, config) -> None:
super(LMI_HInf, self).__init__()
linmod = find_module(model, NNLinear)
epsilon = config.epsilon
gamma = config.gamma
nd = config.nd
if linmod is not None:
self.A = linmod.A.weight
nu = linmod.B.weight.shape[1]
self.B = linmod.B.weight[:,nu-nd:]
self.C = linmod.C.weight
else:
raise AttributeError("Model has no linear module")
self.P = nn.Linear(self.A.shape[0], self.A.shape[0], bias = False)
P, gamma = self.eval_(abs_tol=1e-5, solver ="MOSEK", epsilon= epsilon)
self.P.weight = Parameter(P)
#geo.positive_definite(self.P, "weight")
self.gamma = Parameter(gamma)
print(f'Gamma prescrit = {float(self.gamma):.4e}')
def forward(self):
'''
M = [A^TPA-P A^T*P*B C^T
* B^T*P*B -gammaI 0(nu,ny)
* * -\gamma I_{nu}] <0
** returns : -M
'''
self.symetrizeP_()
eig_val, _ = torch.linalg.eig(self.P.weight)
if not torch.all(torch.real(eig_val)>0):
raise AssertionError("P is not SDP")
nu = self.B.shape[1]
M11 = torch.matmul(self.A.T, self.P.weight) + torch.matmul(self.P.weight, self.A) + 1/self.gamma*torch.matmul(self.C.T, self.C)
M12 = torch.matmul(self.P.weight, self.B)
M22 = -self.gamma*torch.eye(nu)
M = torch.cat((torch.cat((M11,M12), 1), torch.cat((M12.T,M22), 1)), 0)
eig_val, _ = torch.linalg.eig(-M)
if not torch.all(torch.real(eig_val)>0):
raise AssertionError("M is not SDP")
return -M # Always have to return an positive semidefinite matrix
def eval_(self, abs_tol = 1e-6, solver = "MOSEK", epsilon = 1e-6):
A = self.A.detach().numpy()
B = self.B.detach().numpy()
C = self.C.detach().numpy()
print(" Initializing HInf LMI \n")
nu = B.shape[1]
nx = A.shape[0]
ny = C.shape[0]
P = cp.Variable((nx,nx), 'P', PSD=True)
gamma = cp.Variable()
D = np.zeros((ny,nu))
M = cp.bmat([[A.T@P + P@A , P@B, C.T], [B.T@P, -gamma *np.eye(nu), D.T], [ C, D, -gamma*np.eye(ny)]])
constraints = [M << -np.eye(nu+nx+ny)*epsilon, P -(abs_tol)*np.eye(nx)>> 0, gamma - abs_tol >=0]
objective = cp.Minimize(gamma) # Find the L2 gain of the BLA
prob = cp.Problem(objective, constraints= constraints)
prob.solve(solver)
if prob.status not in ["infeasible", "unbounded"]:
# Otherwise, problem.value is inf or -inf, respectively.
print("Gamma = {} \n".format(prob.value))
print(" P eigenvalues : \n")
eigVal = np.linalg.eig(P.value)[0]
print(eigVal)
else:
raise ValueError("SDP problem is infeasible or unbounded")
# Evaluate if it closed to the boundary of the LMI
# X = np.linalg.inv(np.matmul(A.T ,P.value) + np.matmul(P.value,A))
# t = np.matmul(-A, X) -np.matmul(X,A.T) + 2*alpha*X
# If it is close to zero it is at the center
return torch.Tensor(P.value).to(dtype=torch.float32), torch.Tensor(gamma.value).to(dtype=torch.float32)
def symetrizeP_(self):
self.P.weight = Parameter(0.5*(self.P.weight + self.P.weight.T))
class LMI_H2(Module): # TO-DO
'''
This module will compute for a linear system with a specified state-space representaiton
x_+ = Ax + Bu
y = Cx
nu = trace(CPC^T) with P solution A^TP + PA + BB^T = 0
The H2 norm of a system can be interpreted as an average gain over all frequecies
'''
def __init__(self, model, gamma : float, epsilon = 1e-6) -> None:
super(LMI_HInf, self).__init__()
linmod = find_module(model, NNLinear)
if linmod is not None:
self.A = linmod.A
self.B = linmod.B
self.C = linmod.C
else:
raise AttributeError("Model has no linear module")
self.P = nn.Linear(self.A.shape[0], self.A.shape[0], bias = False)
P, nu = self.solve_lmi(self.A,self.B, self.C, abs_tol=1e-5, solver ="MOSEK", epsilon= epsilon)
self.P.weight = Parameter(P.requires_grad_(True))
#geo.positive_definite(self.P, "weight")
self.nu = Parameter(nu)
print(f'Nu prescrit = {float(self.nu):.4e}')
def forward(self):
''' H2 = trace(CPC^T) with P solution A^TP + PA + BB^T = 0
M = [A^TPA-P A^T*P*B C^T
* B^T*P*B -gammaI 0(nu,ny)
* * -\gamma I_{nu}] <0
** returns : -M
'''
self.symetrizeP_()
eig_val, _ = torch.linalg.eig(self.P.weight)
if not torch.all(torch.real(eig_val)>0):
raise AssertionError("P is not SDP")
nu = self.B.shape[1]
M11 = torch.matmul(self.A.T, self.P.weight) + torch.matmul(self.P.weight, self.A) + 1/self.gamma*torch.matmul(self.C.T, self.C)
M12 = torch.matmul(self.P.weight, self.B)
M22 = -self.nu*torch.eye(nu)
M = torch.cat((torch.cat((M11,M12), 1), torch.cat((M12.T,M22), 1)), 0)
return -M # Always have to return an positive semidefinite matrix
def solve_lmi(self, A: torch.Tensor,B: torch.Tensor,C: torch.Tensor, abs_tol = 1e-6, solver = "MOSEK", espilon = 0.1):
A = A.detach().numpy()
B = B.detach().numpy()
C = C.detach().numpy()
print(" Initializing H2 LMI \n")
nu = B.shape[1]
nx = A.shape[0]
ny = C.shape[0]
P = cp.Variable((nx,nx), 'P', PSD=True)
D = np.zeros((ny,nu))
M = A.T@P + P@A + B@B.T
constraints = [M == 0, P -(abs_tol)*np.eye(nx)>> 0]
nu = cp.trace(C@P@C.T)
objective = cp.Minimize(nu) # Find the H2 gain
prob = cp.Problem(objective, constraints= constraints)
prob.solve(solver)
if prob.status not in ["infeasible", "unbounded"]:
# Otherwise, problem.value is inf or -inf, respectively.
print("nu = {} \n".format(prob.value))
print(" P eigenvalues : \n")
eigVal = np.linalg.eig(P.value)[0]
print(eigVal)
else:
raise ValueError("SDP problem is infeasible or unbounded")
# Evaluate if it closed to the boundary of the LMI
# X = np.linalg.inv(np.matmul(A.T ,P.value) + np.matmul(P.value,A))
# t = np.matmul(-A, X) -np.matmul(X,A.T) + 2*alpha*X
# If it is close to zero it is at the center
return torch.Tensor(P.value).to(dtype=torch.float32), torch.Tensor(nu.value).to(dtype=torch.float32)
def symetrizeP_(self):
self.P.weight = Parameter(0.5*(self.P.weight + self.P.weight.T))
class LMI_decay_rate(Module):
def __init__(self, model, alpha : float, epsilon = 1e-6) -> None:
super(LMI_decay_rate, self).__init__()
self.alpha = alpha
linmod = find_module(model, NNLinear)
if linmod is not None:
self.A = linmod.A
else:
raise AttributeError("Model has no linear module")
self.P = Parameter(torch.empty_like(self.A))
geo.positive_semidefinite(self, "P")
M,P = self.solve_lmi(epsilon = epsilon)
self.P = Parameter(P)
self.nm = M.shape[0]
def forward(self):
eig_val, _ = torch.linalg.eig(self.P)
if not torch.all(torch.real(eig_val)>0):
raise AssertionError("P is not SDP")
M = -(self.A.T@self.P + self.P@self.A + 2*self.alpha*self.P)
return M # M must be SDP
def solve_lmi(self, epsilon = 1e-6, solver = "MOSEK"):
A = self.A.detach().numpy()
print(" Initializing Lyapunov LMI \n")
alpha = self.alpha
nx = A.shape[0]
P = cp.Variable((nx,nx), 'P', PSD=True)
M = A.T@P + P@A + 2*alpha*P
constraints = [M << -np.eye(nx)*epsilon, P -(epsilon)*np.eye(nx)>> 0]
objective = cp.Minimize(0) # Feasibility problem
prob = cp.Problem(objective, constraints= constraints)
prob.solve(solver)
if prob.status not in ["infeasible", "unbounded"]:
# Otherwise, problem.value is inf or -inf, respectively.
print(" P eigenvalues : \n")
eigVal = np.linalg.eig(P.value)[0]
print(eigVal)
else:
raise ValueError("SDP problem is infeasible or unbounded")
# Evaluate if it closed to the boundary of the LMI
# X = np.linalg.inv(np.matmul(A.T ,P.value) + np.matmul(P.value,A))
# t = np.matmul(-A, X) -np.matmul(X,A.T) + 2*alpha*X
# If it is close to zero it is at the center
P = torch.Tensor(P.value).to(dtype=torch.float32)
M = torch.Tensor(M.value).to(dtype=torch.float32)
return M, P
def symP_(self):
self.P = Parameter(0.5*(self.P + self.P.T))
class Lipschitz1Layer(Module):
def __init__(self, upper_slope :float, W_out : Tensor, W_in : Tensor, init_margin : float = 1e-6) -> None:
super(Lipschitz1Layer,self).__init__()
self.alpha = 0.0
self.beta = upper_slope
self.w_out = W_out
self.w_in = W_in
M,T = self.solve_lmi(epsilon = init_margin)
self.T = Parameter(T)
self.nM = M.shape[0]
def solve_lmi(self, epsilon = 1e-6, solver = "MOSEK"):
print(" Initializing Lipschitz LMI \n")
W_out = self.w_out.detach().numpy()
W_in = self.w_in.detach().numpy()
beta = self.beta
n_in = self.w_in.shape[1]
n_hid = self.w_in.shape[0]
n_out = self.w_out.shape[0]
self.n_in = n_in
self.n_out = n_out
self.n_hid = n_hid
T = cp.Variable((n_hid,n_hid), 'T', PSD=True)
lip = cp.Variable() # square of the true lipschitz constant for solving the problem
M11 = -lip*np.eye(n_in)
M12 = beta*W_in.T@T
M23 = W_out.T
M = cp.bmat([[M11, M12, np.zeros((n_in,n_out))],
[M12.T, -2*T, M23], [np.zeros((n_out,n_in)), M23.T, -np.eye(n_out, n_out)]])
nM = n_in+n_hid+n_out
constraints = [M << -np.eye(nM)*epsilon, T -(epsilon)*np.eye(nM)>> 0, lip-epsilon > 0]
objective = cp.Minimize(lip) # Feasibility problem
prob = cp.Problem(objective, constraints= constraints)
prob.solve(solver)
if prob.status not in ["infeasible", "unbounded"]:
# Otherwise, problem.value is inf or -inf, respectively.
print(" Lipschitz Constant upper bound : \n")
print(lip.value)
else:
raise ValueError("SDP problem is infeasible or unbounded")
self.lip = torch.Tensor(np.sqrt(lip.value)).to(dtype=torch.float32)
# Evaluate if it closed to the boundary of the LMI
# X = np.linalg.inv(np.matmul(A.T ,P.value) + np.matmul(P.value,A))
# t = np.matmul(-A, X) -np.matmul(X,A.T) + 2*alpha*X
# If it is close to zero it is at the center
M = torch.Tensor(M.value).to(dtype=torch.float32)
T = torch.Tensor(T.value).to(dtype=torch.float32)
return -M, T
def forward(self):
eig_val, _ = torch.linalg.eig(self.T)
if not torch.all(torch.real(eig_val)>0):
raise AssertionError("T is not SDP")
M11 = self.lip*torch.eye(self.n_in)
M12 = self.beta*self.w_out.T@self.T
M13 = torch.zeros((self.n_in, self.n_out))
M23 = self.w_out.T
M = torch.cat((torch.cat((M11,M12,M13), 1),
torch.cat((M12.T,-2*self.T, M23), 1),
torch.cat((M13.T, M23.T, -torch.eye(self.n_out)))), 0)
return -M # M must be SDP
def symT(self):
self.T = Parameter(0.5*(self.T + self.T.T))
class Lipschitz(Module):
def __init__(self, model, upper_slope : float, epsilon : float = 1e-6, L_presc = 1e7) -> None:
super(Lipschitz, self).__init__()
layer_dims = []
idx_weights = []
idx = 0
for layer in model.layers:
if hasattr(layer, 'weight'):
layer_dims.append(tuple(layer.weight.shape))
idx_weights.append(idx)
idx = idx +1
self.idx_weights = idx_weights
self.beta = upper_slope
self.dims = model.dims
self.layers = model.layers # Model must be pure feedforward
T, L = self.solve_lmi(epsilon = epsilon)
# Implementation to do with n independent variables it won't be necessary to re diag the tensor at each step
self.T = Parameter(T.requires_grad_(True))
if L< L_presc:
self.L = L_presc
else:
self.L = L
print(f'Prescribed Lipschitz constant = {float(self.L):.4e}')
def solve_lmi(self, epsilon = 1e-6, solver = "MOSEK"):
print("Initializing Lipschitz LMI \n")
Ts = [cp.Variable((self.dims[i], self.dims[i]), diag = True) for i in range(1,len(self.dims)-1)]
T = block_diag(Ts)
Ft = cp.bmat([[np.zeros(T.shape), self.beta*T],[self.beta*T, -2*T]])
dims_A = self.idx_weights[:-1]
Ws = [self.layers[i].weight.detach().numpy() for i in dims_A]
W = block_diag(Ws)
print(W.shape)
A = cp.hstack([W,np.zeros((W.shape[0],self.dims[-2]))])
print(A.shape)
dims_B = self.dims[1:-1]
I_B = np.eye(sum(dims_B))
B = cp.hstack([np.zeros((I_B.shape[0],self.dims[0])), I_B])
print(B.shape)
AB = cp.vstack([A,B])
LMI = AB.T @ Ft @ AB
LMI_schur = block_diag([LMI,np.zeros((self.dims[-1],self.dims[-1]))])
# 2eme partie LMI
lip = cp.Variable()
L = -lip*np.eye(self.dims[0])
# Block schur last layer
b11 = np.zeros((self.dims[-2],self.dims[-2]))
b12 = self.layers[self.idx_weights[-1]].weight.T.detach().numpy()
b21 = self.layers[self.idx_weights[-1]].weight.detach().numpy()
b22 = -np.eye(self.dims[-1])
bf = cp.bmat([[b11, b12], [b21,b22]])
dim_inter = sum(self.dims[1:-2])
if dim_inter > 0:
inter = np.zeros((dim_inter, dim_inter))
part2 = block_diag([L,inter, bf])
else:
part2 = block_diag([L,bf])
M = LMI_schur + part2
'''
Dn = -np.eye(self.dims[-1])
lDiag = [D0, Ts, Dn]
lSubDiag = [self.beta*Ts[i] @ self.layers[i].weight().detach().numpy() for i in range(0,len(self.layers)-1)]
lSubDiag.append(self.layers[-1].weight().detach().numpy())
lUpDiag = [subDiagTensor.T for subDiagTensor in lSubDiag]
M_diag = block_diag(lDiag)
M_subdiag = block_diag(lSubDiag, k=-1)
M_updiag = block_diag(lUpDiag, k=1)
M = M_diag + M_subdiag + M_updiag
'''
nM = M.shape[0]
nT = T.shape[0]
constraints = [M << -np.eye(nM)*epsilon, T -(epsilon)*np.eye(nT)>> 0, lip-epsilon >= 0]
objective = cp.Minimize(lip) # Find lowest lipschitz constant
prob = cp.Problem(objective, constraints= constraints)
prob.solve(solver)
if prob.status not in ["infeasible", "unbounded"]:
# Otherwise, problem.value is inf or -inf, respectively.
print(" Lipschitz Constant upper bound : \n")
print(np.sqrt(lip.value))
else:
raise ValueError("SDP problem is infeasible or unbounded")
lip = torch.Tensor(np.array(np.sqrt(lip.value))).to(dtype=torch.float32)
# Evaluate if it closed to the boundary of the LMI
# X = np.linalg.inv(np.matmul(A.T ,P.value) + np.matmul(P.value,A))
# t = np.matmul(-A, X) -np.matmul(X,A.T) + 2*alpha*X
# If it is close to zero it is at the center
#Ts = [torch.Tensor(tens.value.todense()).to(dtype=torch.float32) for tens in Ts]
#T = torch.block_diag()
T = torch.Tensor(T.value).to(dtype=torch.float32)
M = torch.Tensor(M.value).to(dtype=torch.float32)
return T, lip
def forward(self):
T = self.diagT_()
Ft = torch.vstack([torch.hstack([torch.zeros(T.shape), self.beta*T]),torch.hstack([self.beta*T, -2*T])])
dims_A = self.idx_weights[:-1]
Ws = [self.layers[i].weight for i in dims_A]
W = torch.block_diag(*tuple(Ws))
A = torch.hstack([W,torch.zeros((W.shape[0],self.dims[-2]))])
dims_B = self.dims[1:-1]
I_B = torch.eye(sum(dims_B))
B = torch.hstack([torch.zeros((I_B.shape[0],self.dims[0])), I_B])
AB = torch.cat([A,B])
LMI = AB.T @ Ft @ AB
LMI_schur = torch.block_diag(LMI,torch.zeros((self.dims[-1],self.dims[-1])))
# 2eme partie LMI
lip = self.L**2
L = -lip*torch.eye(self.dims[0])
# Block schur last layer
b11 = torch.zeros((self.dims[-2],self.dims[-2]))
b12 = self.layers[self.idx_weights[-1]].weight.T
b1 = torch.hstack([b11,b12])
b21 = self.layers[self.idx_weights[-1]].weight
b22 = -torch.eye(self.dims[-1])
b2 = torch.hstack([b21,b22])
bf = torch.vstack([b1,b2])
dim_inter = sum(self.dims[1:-2])
inter = torch.zeros((dim_inter, dim_inter))
part2 = torch.block_diag(L,inter, bf)
M = LMI_schur + part2
#torch.block_diag
if torch.any(torch.isnan(M)):
print(M)
if torch.any(torch.isnan(T)):
print(T)
return -M, T
def diagT_(self):
if torch.any(torch.isnan(self.T)):
print(torch.diag(torch.diag(self.T)))
return torch.diag(torch.diag(self.T))
'''
Discrete time LMI versions
'''
class LMI_HInf_Dist_discrete(Module):
def __init__(self, A,B,C,gamma) -> None:
super(LMI_HInf_Dist_discrete, self).__init__()
self.A = A
self.B = B
self.C = C
self.P = nn.Linear(A.shape[0],A.shape[0])
geo.PSD(self.P) # Positive semi definite
self.gamma = gamma
def forward(self):
'''
M = [A^TPA-P A^T*P*B C^T
* B^T*P*B -gammaI 0(nu,ny)
* * -\gamma I_{nu}] <0
** returns : -M
'''
M11 = torch.matmul(torch.matmul(self.A.T, self.P), self.A) - self.P
M12 = torch.matmul(torch.matmul(self.A.T, self.P), self.B)
M13 = self.C.T
M22 = torch.matmul(torch.matmul(self.B.T, self.P), self.B) - self.gamma*torch.eye(nu)
nu = self.B.shape[1]
ny = self.C.shape[0]
M23 = torch.zeros((nu,ny))
M33 = self.gamma*torch.eye(ny)
M = torch.Tensor([[M11, M12, M13],[M12.T, M22, M23],[M13.T, M23.T, M33]])
return -M