-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
238 lines (221 loc) · 11.4 KB
/
Copy pathfunctions.py
File metadata and controls
238 lines (221 loc) · 11.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
import numpy as np
import cvxpy as cp
from SLSFinite import *
from Polytope import *
import matplotlib.pyplot as plt
import copy
def polytope_constraints(SLS_data, Poly_x, Poly_u, Poly_w):
# load SLS constraints
constraints = SLS_data.SLP_constraints()
# add polytope containment constraints
Poly_xu = Poly_x.cart(Poly_u)
Lambda = cp.Variable((np.shape(Poly_xu.H)[0], np.shape(Poly_w.H)[0]), nonneg=True)
constraints += [Lambda @ Poly_w.H == Poly_xu.H @ SLS_data.Phi_matrix,
Lambda @ Poly_w.h <= Poly_xu.h]
return constraints, Lambda
def optimize(A_list, B_list, C_list, Poly_x, Poly_u, Poly_w, opt_eps, norm=None):
"""
Parameters
----------
A_list: list of matrices [A_0, ...A_T]
B_list: list of tensors [B_0, ...B_T]
C_list: list of tensors [C_0, ...C_T]
where A_t, B_t, C_t are the matrices in the dynamics of the system at time t.
Poly_x, Poly_u, Poly_w: Polytope
Polytopes cartesian products of polytopes
for x, u and w over all times t = 0,...,T.
Returns
-------
result: float
Optimal cost (np.inf if problem is not feasible).
SLS_data: SLSFinite object
Instance of the class SLSFinite containing the variables corresponding
to the optimal cost.
Lambda: cvxpy.Variable, shape (H_x[0], H_w[0])
Polytope containment variable corresponding to the optimal cost.
"""
# constraints
SLS_data = SLSFinite(A_list, B_list, C_list, norm)
[constraints, Lambda] = polytope_constraints(SLS_data, Poly_x, Poly_u, Poly_w)
# objective function
objective = cp.Minimize(0)
problem = cp.Problem(objective, constraints)
result = problem.solve( solver=cp.MOSEK,
mosek_params = {'MSK_DPAR_INTPNT_CO_TOL_DFEAS': opt_eps,
},
verbose=True)
if problem.status != cp.OPTIMAL:
raise Exception("Solver did not converge!")
return result, SLS_data, Lambda
def optimize_RTH(A_list, B_list, C_list, Poly_x, Poly_u, Poly_w, N, delta, rank_eps, opt_eps):
"""
Parameters
----------
A_list: list of matrices [A_0, ...A_T]
B_list: list of tensors [B_0, ...B_T]
C_list: list of tensors [C_0, ...C_T]
where A_t, B_t, C_t are the matrices in the dynamics of the system at time t.
Poly_x, Poly_u, Poly_w: Polytope
Polytopes cartesian products of polytopes
for x, u and w over all times t = 0,...,T.
N: int
Number of iterations of the reweighted nuclear norm iteration.
key: str
Set the cost function.
Returns
-------
result_list: float
Optimal cost (np.inf if problem is not feasible).
SLS_data_list: SLSFinite object
List of Instance of the class SLSFinite containing the variables corresponding
to the optimal cost.
Lambda: cvxpy.Variable, shape (H_x[0] + H_u[0], H_w[0])
Polytope containment variable corresponding to the optimal cost.
"""
SLS_data = SLSFinite(A_list, B_list, C_list)
[constraints, Lambda] = polytope_constraints(SLS_data, Poly_x, Poly_u, Poly_w)
# Initialize Paramters
W_1 = cp.Parameter(2*[SLS_data.nu*(SLS_data.T+1)])
W_2 = cp.Parameter(2*[SLS_data.ny*(SLS_data.T+1)])
W_1.value = delta**(-1/2)*np.eye(SLS_data.nu*(SLS_data.T+1))
W_2.value = delta**(-1/2)*np.eye(SLS_data.ny*(SLS_data.T+1))
result_list = N*[None]
SLS_data_list = N*[None]
objective = cp.Minimize(cp.norm(W_1 @ SLS_data.Phi_uy @ W_2, 'nuc'))
#define problem
problem = cp.Problem(objective, constraints)
for k in range(N):
result = problem.solve(solver=cp.MOSEK,
mosek_params = {'MSK_DPAR_INTPNT_CO_TOL_DFEAS': opt_eps,
},
verbose=True)
if problem.status != cp.OPTIMAL:
raise Exception("Solver did not converge!")
result_list[k] = result
SLS_data_list[k] = copy.deepcopy(SLS_data)
#update params
[U, S, Vh] = np.linalg.svd((W_1 @ SLS_data.Phi_uy @ W_2).value , full_matrices=False)
Y = np.linalg.inv(W_1.value).dot((W_1 @ SLS_data.Phi_uy @ W_2).value).dot(Vh.T).dot(U.T).dot(np.linalg.inv(W_1.value))
Z = np.linalg.inv(W_2.value).dot(Vh.T).dot(U.T).dot((W_1 @ SLS_data.Phi_uy @ W_2).value).dot(np.linalg.inv(W_2.value))
# help function
def update_W(Q, dim, delta):
W = (Q + delta*np.eye(dim))
[eig, eigv] = np.linalg.eigh(W)
assert np.all(eig > 0)
W = eigv.dot(np.diag(eig**(-1/2))).dot(np.linalg.inv(eigv))
return W
W_1.value = update_W(Y, SLS_data.nu*(SLS_data.T+1), delta)
W_2.value = update_W(Z, SLS_data.ny*(SLS_data.T+1), delta)
# calculate F
SLS_data_list[-1].calculate_dependent_variables("Reweighted Nuclear Norm")
# compute causal factorization of F and truncate using rank_eps
SLS_data_list[-1].causal_factorization(rank_eps)
# compute truncated Phi matrix for checking feasibility
SLS_data_list[-1].F_trunc_to_Phi_trunc()
# check feasibility up to 1e-8
Poly_xu = Poly_x.cart(Poly_u)
print("rank F:", SLS_data_list[-1].rank_F_trunc)
print("band (D,E) = messages:", SLS_data_list[-1].E.shape[0])
print("Error true F and truncated F:", np.max( np.abs(SLS_data_list[-1].F - SLS_data_list[-1].F_trunc) ) )
print("Error true Phi and truncated Phi:", np.max( np.abs(SLS_data_list[-1].Phi_matrix.value - SLS_data_list[-1].Phi_trunc) ) )
print("Error truncated polytope constraint:", np.max( np.abs(Lambda.value.dot(Poly_w.H) - Poly_xu.H.dot(SLS_data_list[-1].Phi_trunc)) ) )
assert np.all( Lambda.value.dot(Poly_w.h) <= Poly_xu.h + 1e-8 )
assert np.all( np.isclose( Lambda.value.dot(Poly_w.H), (Poly_xu.H.dot(SLS_data_list[-1].Phi_trunc)).astype('float') , atol = 1e-8) )
# check feasibility by reoptimizing over Lambda
# Poly_xu = Poly_x.cart(Poly_u)
# reopt_Lambda = cp.Variable((np.shape(Poly_xu.H)[0], np.shape(Poly_w.H)[0]), nonneg=True)
# reopt_constraints = [reopt_Lambda @ Poly_w.H == Poly_xu.H @ SLS_data_list[-1].Phi_trunc,
# reopt_Lambda @ Poly_w.h <= Poly_xu.h]
# reopt_objective = cp.Minimize(0)
# reopt_problem = cp.Problem(reopt_objective, reopt_constraints)
# _ = reopt_problem.solve( solver=cp.MOSEK,
# mosek_params = {'MSK_DPAR_INTPNT_CO_TOL_DFEAS': opt_eps,
# },
# verbose=True)
# if reopt_problem.status != cp.OPTIMAL:
# raise Exception("Solver did not converge!")
return [result_list, SLS_data_list, Lambda]#, reopt_Lambda]
def optimize_reweighted_atomic(A_list, B_list, C_list, Poly_x, Poly_u, Poly_w, N, key, delta, opt_eps):
SLS_data = SLSFinite(A_list, B_list, C_list)
[constraints, Lambda] = polytope_constraints(SLS_data, Poly_x, Poly_u, Poly_w)
# Initialize Paramters
if key == 'Reweighted Sensor Norm':
W = cp.Parameter(SLS_data.ny*(SLS_data.T+1))
objective = cp.Minimize(cp.sum(cp.norm(SLS_data.Phi_uy @ cp.diag(W), 2, 0)))
W.value = delta**-1 * np.ones(SLS_data.ny*(SLS_data.T+1))
elif key == 'Reweighted Actuator Norm':
W = cp.Parameter(SLS_data.nu*(SLS_data.T+1))
objective = cp.Minimize(cp.sum(cp.norm(cp.diag(W) @ SLS_data.Phi_uy, 2, 1)))
W.value = delta**-1 * np.ones(SLS_data.nu*(SLS_data.T+1))
result_list = N*[None]
SLS_list = N*[None]
norm_list = N*[None]
#define problem
problem = cp.Problem(objective, constraints)
for k in range(N):
result = problem.solve(solver=cp.MOSEK,
mosek_params = {'MSK_DPAR_INTPNT_CO_TOL_DFEAS': opt_eps,
},
verbose=True)
if problem.status != cp.OPTIMAL:
raise Exception("Solver did not converge!")
result_list[k] = result
SLS_list[k] = copy.deepcopy(SLS_data)
#update params
if key == 'Reweighted Sensor Norm':
norm_list[k] = np.linalg.norm(SLS_data.Phi_uy.value, 2, 0)
W.value = (np.linalg.norm(SLS_data.Phi_uy.value, 2, 0) + delta)**-1
if key == 'Reweighted Actuator Norm':
norm_list[k] = np.linalg.norm(SLS_data.Phi_uy.value, 2, 1)
W.value = (np.linalg.norm(SLS_data.Phi_uy.value, 2, 1) + delta)**-1
return result_list, SLS_list, Lambda, norm_list
def optimize_sparsity(A_list, B_list, C_list, Poly_x, Poly_u, Poly_w, key, N, delta, rank_eps, opt_eps):
"""
Parameters
----------
A_list: list of matrices [A_0, ...A_T]
B_list: list of tensors [B_0, ...B_T]
C_list: list of tensors [C_0, ...C_T]
where A_t, B_t, C_t are the matrices in the dynamics of the system at time t.
Poly_x, Poly_u, Poly_w: Polytope
Polytopes cartesian products of polytopes
for x, u and w over all times t = 0,...,T.
N: int
Number of iterations of the reweighted nuclear norm iteration.
key: str
Set the cost function.
Returns
-------
result_list: float
Optimal cost (np.inf if problem is not feasible).
SLS_data_list: SLSFinite object
List of Instance of the class SLSFinite containing the variables corresponding
to the optimal cost.
Lambda: cvxpy.Variable, shape (H_x[0] + H_u[0], H_w[0])
Polytope containment variable corresponding to the optimal cost.
"""
result_list, SLS_list, Lambda, norm_list = optimize_reweighted_atomic(A_list, B_list, C_list, Poly_x, Poly_u, Poly_w, N, key, delta, opt_eps)
# reoptimize over last iteration by removing columns or rows of the lowest 2-norms
argmin = SLS_list[-1].Phi_uy.value
if key == 'Reweighted Sensor Norm':
# 2-norms of column vectors = sensor norm
norm_argmin = np.linalg.norm(argmin, 2, 0)
elif key == 'Reweighted Actuator Norm':
# 2-norms of row vectors = actuator norm
norm_argmin = np.linalg.norm(argmin, 2, 1)
else:
raise Exception('Choose either the reweighted sensor or actuator norm to minimize!')
ind = np.where(norm_argmin<=rank_eps)[0]
reopt_result, reopt_SLS, reopt_Lambda = optimize(A_list, B_list, C_list, Poly_x, Poly_u, Poly_w, opt_eps, norm=[key, ind])
reopt_kept_indices = [i for i in np.arange(len(norm_argmin)) if i not in ind] # columns/rows kept
reopt_SLS.calculate_dependent_variables(key) # only get F, no other truncation needed.
reopt_SLS.F_trunc = reopt_SLS.F
reopt_SLS.F_trunc_to_Phi_trunc()
# check feasibility after truncating lower triangularity
Poly_xu = Poly_x.cart(Poly_u)
print("Error true F and truncated F:", np.max( np.abs(reopt_SLS.F - reopt_SLS.F_trunc) ) )
print("Error true Phi and truncated Phi:", np.max( np.abs(reopt_SLS.Phi_matrix.value - reopt_SLS.Phi_trunc) ) )
print("Error truncated polytope constraint:", np.max( np.abs(reopt_Lambda.value.dot(Poly_w.H) - Poly_xu.H.dot(reopt_SLS.Phi_trunc)) ) )
assert np.all( reopt_Lambda.value.dot(Poly_w.h) <= Poly_xu.h + 1e-8 )
assert np.all( np.isclose( reopt_Lambda.value.dot(Poly_w.H), (Poly_xu.H.dot(reopt_SLS.Phi_trunc)).astype('float') , atol = 1e-8) )
return [reopt_result, reopt_SLS, reopt_Lambda, norm_list, reopt_kept_indices, SLS_list]