-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.py
237 lines (193 loc) · 9.15 KB
/
terminal.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
""" Terminal set computation
(c) Martin Doff-Sotta, University of Oxford ([email protected])
"""
import numpy as np
import cvxpy as cp
from pvtol_model import linearise, discretise
import scipy.linalg
## Cartesian product
def cartesian_product(*arrays):
"""
Generate the n-ary cartesian product of the n input arrays
Input: unpacked input arrays
Output: matrix whose rows are ordered n-tuples formed from the input
"""
la = len(arrays)
dtype = np.result_type(*arrays)
arr = np.empty([len(a) for a in arrays] + [la], dtype=dtype)
for i, a in enumerate(np.ix_(*arrays)):
arr[...,i] = a
return arr.reshape(-1, la)
def get_term(param, delta, weights_g, weights_h, sigma, dsigma):
""" Compute terminal cost, terminal constraint bound and terminal matrix
Output: terminal matrix Q_N, terminal constraint bound gamma_N and terminal gain K_N
"""
# Initialisation
alpha = 10 # objective weight
n = len(param.x_init) # number of states
m = len(param.u_init) # number of inputs
I_s = np.eye(n)
I_i = np.eye(m)
Q = param.Q
R = param.R
C = Q[-1, None, :]
Q_inv = np.linalg.inv(Q)
eps = np.finfo(float).eps
# Variables definition (SDP)
Q_N = cp.Variable((n,n), symmetric=True)
S = cp.Variable((n,n), symmetric=True)
Y = cp.Variable((m,n))
gamma_inv = cp.Variable((1,1))
# Terminal set definition
all_vertices = np.vstack([param.x_term[:, None], param.u_term[:, None]]) # stack state and input terminal bounds
vertices_list = np.hstack([all_vertices, -all_vertices]) # assemble vertices
Ver_trans = cartesian_product(*vertices_list) # generate all possible vertices
Ver = Ver_trans.T + np.vstack([param.h_r[:, None], param.u_r[:, None]]) # vertices of terminal set
# Objective
objective = cp.Minimize(cp.trace(Q_N) + alpha*gamma_inv)
# Initialise constraints
constr = []
# Constraint S = Q_N^-1
constr += [cp.vstack([cp.hstack([S, np.eye(n)]), cp.hstack([np.eye(n), Q_N])]) >> eps*np.eye(n*2)]
# Terminal cost constraint
#Y_ = cp.vstack([np.zeros((n-m, n)), Y])
Y_ = Y
R_inv = np.linalg.inv(R)
#R_ = scipy.linalg.block_diag(np.zeros((n-m, n-m)), R_inv)
R_ = R_inv
#R_ = cp.diag(np.array([*(0,)*(n-m), ], dtype=object))
#CS = cp.vstack([C @ S, np.zeros_like(C)])
O = np.zeros((n, n))
for i in range(Ver.shape[1]):
A1, B1, A2, B2 = linearise(Ver[0:n, i, None],
Ver[n:n+m, i, None], weights_g, weights_h, sigma, dsigma)
A, B = discretise(A1[0]-A2[0], B1[0]-B2[0], delta) # discrete-time ss
M = (A @ S + B @ Y)
constr += [cp.vstack([cp.hstack([S, M.T, S, Y.T]),
cp.hstack([M, S, np.zeros((n, n)), np.zeros((n, m))]),
cp.hstack([S, np.zeros((n, n)), Q_inv, np.zeros((n, m))]),
cp.hstack([Y, np.zeros((m, n)), np.zeros((m, n)), R_inv])])\
>> eps*np.eye(3*n+m) ]
# Terminal constraint F x + G u <= h
G = cp.vstack([np.zeros((n, m)), np.zeros((n, m)), np.eye(m), -np.eye(m)])
F = cp.vstack([np.eye(n), -np.eye(n), np.zeros((m, n)), np.zeros((m, n))])
h = cp.vstack([param.x_max[:, None], -param.x_min[:, None],
param.u_max[:, None], -param.u_min[:, None]])
h_loc = cp.vstack([h- (F @ param.h_r[:, None] + G @ param.u_r[:, None]),
param.u_term[:, None], param.x_term[:, None]])
G_loc = cp.vstack([G, np.eye(m), np.zeros((n, m))])
F_loc = cp.vstack([F, np.zeros((m, n)), np.eye(n)])
# Could try to uncomment this (and comment two next for loops instead)
"""for o in range(h_loc.shape[0]):
block1 = cp.hstack([gamma_inv @ (h_loc[o,:,None])**2,
F_loc[o, None, :] @ S + G_loc[o, None, :] @ Y])
block2 = cp.hstack([(F_loc[o, None, :] @ S + G_loc[o, None, :] @ Y).T, S])
constr += [cp.vstack([block1, block2]) >> eps*np.eye(n+1)]"""
for o in range(n):
constr += [gamma_inv * param.x_term[o]**2 - S[o, o] >> eps]
for o in range(m):
block1 = cp.hstack([gamma_inv @ param.u_term[o, None, None]**2,
Y[o, None, :]])
block2 = cp.hstack([Y[o, None, :].T,
S])
constr += [cp.vstack([block1, block2]) >> eps*np.eye(n+1)]
# Solve SDP problem
problem = cp.Problem(objective, constr)
problem.solve(verbose=False)
# Post-processing
gamma_N = 1/(gamma_inv.value[0, 0])
K_N = Y.value @ Q_N.value
return Q_N.value, gamma_N, K_N
def get_term_dist(param, delta, weights_g, weights_h, sigma, dsigma):
""" Compute terminal cost, terminal constraint bound and terminal matrix with
disturbance.
Input: parameter structure param, time step delta
Ouput: terminal matrix Q_N, terminal constraint bound gamma_N and terminal gain K_N
From: Mark Cannon March 2023
"""
# Initialisation
n = len(param.x_init) # number of states
m = len(param.u_init) # number of inputs
I_s = np.eye(n)
I_i = np.eye(m)
Q = param.Q
R = param.R
C = Q[-1, None, :]
Q_inv = np.linalg.inv(Q)
eps = np.finfo(float).eps
lam_scaling = 1
# Variables definition (SDP)
#Q_N = cp.Variable((n,n), symmetric=True)
S = cp.Variable((n,n), symmetric=True)
Y = cp.Variable((m,n))
beta = cp.Variable((1,1), pos=True)
lam = cp.Variable((1,1), pos=True)
# Terminal set definition
all_vertices = np.vstack([param.x_term[:, None], param.u_term[:, None]]) # stack state and input terminal bounds
vertices_list = np.hstack([all_vertices, -all_vertices]) # assemble vertices
Ver_trans = cartesian_product(*vertices_list) # generate all possible vertices
Ver = Ver_trans.T + np.vstack([param.h_r[:, None], param.u_r[:, None]]) # vertices of terminal set
# Disturbance vertices (TO BE CHECKED)
pre_w = np.hstack([param.W_up[:, None], param.W_low[:, None]])
Ver_w_trans = cartesian_product(*pre_w)
Ver_w = Ver_w_trans.T
"""Ver_w = np.hstack([np.vstack([param.W_low, param.W_up]),
np.vstack([param.W_up, param.W_low]),
np.vstack([param.W_low, param.W_low]),
np.vstack([param.W_up, param.W_up])])"""
## Problem 1
# Objective
objective = cp.Minimize(beta - lam * lam_scaling)
# Initialise constraints
constr = []
# Constraint S = Q_N^-1
#constr += [cp.vstack([cp.hstack([S, np.eye(n)]), cp.hstack([np.eye(n), Q_N])]) >> eps*np.eye(n*2)]
# Constraint S >= lam * eye(n)
constr += [S >> lam * np.eye(n)]
# Terminal cost constraint
R_inv = np.linalg.inv(R)
for i in range(Ver.shape[1]):
A1, B1, A2, B2 = linearise(Ver[0:n, i, None], Ver[n:n+m, i, None],
weights_g, weights_h, sigma, dsigma)
A, B = discretise(A1[0]-A2[0], B1[0]-B2[0], delta) # discrete-time ss
M = (A @ S + B @ Y)
for j in range(Ver_w.shape[1]):
W = np.vstack([0, Ver_w[:, j, None], 0]) # CHECK
constr += [cp.vstack([cp.hstack([S, np.zeros((n, 1)), M.T, S, Y.T]),
cp.hstack([np.zeros((1, n)), beta, W.T, np.zeros((1, n)), np.zeros((1, m))]),
cp.hstack([M, W, S, np.zeros((n, n)), np.zeros((n, m))]),
cp.hstack([S, np.zeros((n, 1)), np.zeros((n, n)), Q_inv, np.zeros((n, m))]),
cp.hstack([Y, np.zeros((m, 1)), np.zeros((m, n)), np.zeros((m, n)), R_inv])]) >> eps*np.eye(3*n+m+1) ]
# Solve SDP problem
problem = cp.Problem(objective, constr)
problem.solve(verbose=False)
# Post-processing Problem 1
beta_N = beta.value[0, 0]
Q_N = np.linalg.inv(S.value)
K_N = Y.value @ Q_N
## Problem 2
gam = np.zeros(3 * n + 3 * m)
Q_N_inv = S.value
q0 = 0
for q in range(n):
gam[q0 + q] = param.x_term[q] ** 2 / (Q_N_inv[q, q])
q0 = q0 + n
for q in range(m):
gam[q0 + q] = param.u_term[q] ** 2 / (K_N[q, :] @ Q_N_inv @ (K_N[q, :].T))
q0 = q0 + m
for q in range(n):
gam[q0 + q] = (param.x_max[q] - param.h_r[q]) ** 2 / (Q_N_inv[q, q])
q0 = q0 + n
for q in range(n):
gam[q0 + q] = (param.x_min[q] - param.h_r[q]) ** 2 / (Q_N_inv[q, q])
q0 = q0 + n
for q in range(m):
gam[q0 + q] = (param.u_max[q] - param.u_r[q]) ** 2 / (K_N[q, :] @ Q_N_inv @ (K_N[q, :].T))
q0 = q0 + m
for q in range(m):
gam[q0 + q] = (param.u_min[q] - param.u_r[q]) ** 2 / (K_N[q, :] @ Q_N_inv @ (K_N[q, :].T))
# Check invariance
gamma_N = gam.min()
Q_isqrt = scipy.linalg.sqrtm(Q_N_inv)
gamma_N_min = beta_N / max(np.linalg.eigvals(Q_isqrt @ (Q + K_N.T @ R @ K_N) @ Q_isqrt))
return Q_N, gamma_N, K_N, beta_N, gamma_N_min