-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattas_sp_fem.py
229 lines (171 loc) · 5.87 KB
/
attas_sp_fem.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
"""ATTAS aircraft short-period mode estimation --- Traditional PEM/FEM."""
import collections
import os
import attrdict
import jax
import jax.numpy as jnp
import jax.scipy as jsp
import numpy as np
import scipy.io
import scipy.linalg
from scipy import optimize
import riccati
jax.config.update("jax_enable_x64", True)
class Decision:
"""Decision variable specification."""
def __init__(self, shape, start):
if isinstance(shape, int):
shape = (shape,)
self.shape = shape
"""Decision variable shape."""
self.size = np.prod(shape, dtype=int)
"""Total number of elements."""
self.start = start
"""Start index in parent vector."""
end = start + self.size
self.slice = np.s_[start:end]
"""Slice of variable in parent vector."""
def unpack(self, vec):
"""Unpack variable from parent vector."""
return vec[self.slice].reshape(self.shape)
def pack(self, vec, value):
"""Pack variable into parent vector."""
val_flat = np.broadcast_to(value, self.shape).ravel()
vec[self.slice] = val_flat
class Problem:
def __init__(self, nx, u, y):
self.dec_specs = collections.OrderedDict()
"""Decision variable specifications."""
self.ndec = 0
"""Total number of decision variables."""
self.u = jnp.asarray(u)
"""Inputs."""
self.y = jnp.asarray(y)
"""Measurements."""
self.nx = nx
"""Size of state vector."""
self.nu = np.size(u, 1)
"""Size of input vector."""
self.ny = np.size(y, 1)
"""Size of output vector."""
N = np.size(y, 0)
assert N == np.size(u, 0)
self.N = N
"""Number of measurement instants."""
# Register decision variables
self.add_decision('x0', nx)
self.add_decision('A', (nx, nx))
self.add_decision('B', (nx, self.nu))
self.add_decision('lsQd', nx)
self.add_decision('lsRd', self.ny)
def add_decision(self, name, shape=()):
self.dec_specs[name] = spec = Decision(shape, self.ndec)
self.ndec += spec.size
def unpack_decision(self, dvec):
if jnp.shape(dvec) != (self.ndec,):
raise ValueError("invalid shape for `dvec`")
dvars = attrdict.AttrDict()
for name, spec in self.dec_specs.items():
dvars[name] = spec.unpack(dvec)
return dvars
def pack_decision(self, dvars, dvec=None):
if dvec is None:
dvec = np.zeros(self.ndec)
for name, value in dvars.items():
spec = self.dec_specs.get(name)
if spec is not None:
spec.pack(dvec, value)
return dvec
def merit(self, dvec):
v = self.unpack_decision(dvec)
A = v.A
B = v.B
u = self.u
y = self.y
C = jnp.identity(self.nx)
D = jnp.zeros((self.ny, self.nu))
sQd = jnp.exp(v.lsQd)
sRd = jnp.exp(v.lsRd)
Qd = sQd ** 2
Rd = sRd ** 2
sQ = jnp.diag(sQd)
sR = jnp.diag(sRd)
Q = jnp.diag(Qd)
R = jnp.diag(Rd)
Pp = riccati.dare(A.T, C.T, Q, R)
sPp = jnp.linalg.cholesky(Pp)
nx = len(A)
ny = len(C)
N = len(y)
# Kailath Eq. (12.3.8)
z = jnp.zeros_like(C.T)
corr_mat = jnp.block([[sR, C @ sPp],
[z, sPp]])
q, r = jnp.linalg.qr(corr_mat.T)
s = jnp.sign(r.diagonal())
sRp = (r.T * s)[:ny, :ny]
sPc = (r.T * s)[ny:, ny:]
Kn = (r.T * s)[ny:, :ny]
x = v.x0
loglike = -N * jnp.log(sRp.diagonal()).sum()
for k in range(len(y)):
e = y[k] - (C @ x + D @ u[k])
en = jnp.linalg.solve(sRp, e)
loglike = loglike - 0.5 * jnp.sum(en ** 2)
xcorr = x + Kn @ en
x = A @ xcorr + B @ u[k]
return loglike
def load_data():
# Retrieve data
d2r = np.pi / 180
module_dir = os.path.dirname(__file__)
data_file_path = os.path.join(module_dir, 'data', 'fAttasElv1.mat')
data = scipy.io.loadmat(data_file_path)['fAttasElv1'][30:-30]
t = data[:, 0] - data[0, 0]
u = data[:, [21]] * d2r
y = data[:, [7, 12]] * d2r
# Shift and rescale
yshift = np.r_[-0.003, -0.04]
yscale = np.r_[10.0, 20.0]
ushift = np.r_[-0.04]
uscale = np.r_[25.0]
y = (y + yshift) * yscale
u = (u + ushift) * uscale
# Add artificial noise
np.random.seed(0)
y[:, :] += 1e-2 * np.random.randn(*y.shape)
return t, u, y, yshift, yscale, ushift, uscale
if __name__ == '__main__':
nx = 2
nu = 1
ny = 2
# Load experiment data
t, u, y, yshift, yscale, ushift, uscale = load_data()
problem = Problem(nx, u, y)
A0 = np.array([[0.92, -0.097], [0.0831, 0.977]])
B0 = np.array([[-0.11], [0.015]])
lsQd0 = np.array([-4.3, -4.9])
lsRd0 = np.array([-5.4, -4.4])
dvar0 = dict(A=A0, B=B0, lsQd=lsQd0, lsRd=lsRd0)
dvec0 = problem.pack_decision(dvar0)
# Define optimization functions
obj = lambda x: -problem.merit(x)
grad = jax.grad(obj)
hessp = lambda x, p: jax.jvp(grad, (x,), (p,))[1]
opt = {'gtol': 2e-6, 'disp': True, 'maxiter': 200}
sol = optimize.minimize(
obj, dvec0, method='trust-krylov', jac=grad, hessp=hessp, options=opt
)
varopt = problem.unpack_decision(sol.x)
vargrad = problem.unpack_decision(sol.jac)
A = varopt.A
B = varopt.B
lsQd = varopt.lsQd
lsRd = varopt.lsRd
x0 = varopt.x0
sRd = np.exp(lsRd)
sQd = np.exp(lsQd)
xsim = np.zeros_like(y)
xsim[0] = x0
for i in range(1, len(y)):
xsim[i] = A @ xsim[i-1] + B @ u[i - 1]