-
Notifications
You must be signed in to change notification settings - Fork 0
/
KalmanNet_sysmdl.py
266 lines (200 loc) · 8.14 KB
/
KalmanNet_sysmdl.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
import torch
import numpy as np
from torch.distributions.multivariate_normal import MultivariateNormal
from lqr_utils import lqr_finite, kalman_finite, lqr_infinite
if torch.cuda.is_available():
dev = torch.device("cuda:0")
torch.set_default_tensor_type("torch.cuda.FloatTensor")
else:
dev = torch.device("cpu")
class SystemModel:
def __init__(self, f, G, Q, h, R, T, T_test, prior_Q=None, prior_Sigma=None, prior_S=None, is_mismatch=False):
####################
### Motion Model ###
####################
self.f = f
self.G = G
self.p = self.G.size()[1]
self.Q = Q
self.m = self.Q.size()[0]
# Model mismatch flag
self.is_mismatch = is_mismatch
#########################
### Observation Model ###
#########################
self.h = h
self.R = R
self.n = self.R.size()[0]
################
### Sequence ###
################
# Assign T
self.T = T
self.T_test = T_test
#########################
### Covariance Priors ###
#########################
if prior_Q is None:
self.prior_Q = torch.eye(self.m)
else:
self.prior_Q = prior_Q
if prior_Sigma is None:
self.prior_Sigma = torch.eye(self.m)
else:
self.prior_Sigma = prior_Sigma
if prior_S is None:
self.prior_S = torch.eye(self.n)
else:
self.prior_S = prior_S
#####################
### Init Sequence ###
#####################
def InitSequence(self, m1x_0, m2x_0):
self.m1x_0 = torch.squeeze(m1x_0).to(dev) #m1x_0
self.x_prev = torch.squeeze(m1x_0).to(dev) #m1x_0
self.m2x_0 = torch.squeeze(m2x_0).to(dev) #m2x_0
#########################
### Update Covariance ###
#########################
def UpdateCovariance_Gain(self, q, r):
self.q = q
self.Q = q * q * torch.eye(self.m)
self.r = r
self.R = r * r * torch.eye(self.n)
def UpdateCovariance_Matrix(self, Q, R):
self.Q = Q
self.R = R
#########################
### Generate Sequence ###
#########################
def GenerateSequence(self, T, q_noise, r_noise, steady_state=False, is_control_enable=True):
# Get control gain
if steady_state:
L = self.L_infinite # Corresponds to infinite dlqr
else:
L = self.L # Corresponds to finite dlqr
# Pre allocate an array for current state
self.x = torch.empty(size=[self.m, T+1])
self.x[:,0] = self.m1x_0
# Pre allocate an array for current observation
self.y = torch.empty(size=[self.n, T+1])
self.y[:,0] = self.h(self.x[:,0])
# Set x0 to be x previous
self.x_prev = self.m1x_0
# Pre allocate control
u = torch.zeros(self.p, T)
# xt = self.x_prev
# Generate Sequence Iteratively
for t in range(0, T):
########################
##### Control Input ####
########################
if is_control_enable:
# LQR input
dx = self.x[:, t-1] # - XT[k]
if steady_state:
u[:, t-1] = - torch.matmul(L, dx)
else:
u[:, t-1] = - torch.matmul(L[t-1], dx)
########################
#### State Evolution ###
########################
xt = self.f(self.x_prev, self.is_mismatch) + self.G.matmul(u[:, t-1]) + q_noise[:,t-1]
################
### Emission ###
################
yt = self.h(xt) + r_noise[:,t-1]
########################
### Squeeze to Array ###
########################
# Save Current State to Trajectory Array
self.x[:, t] = torch.squeeze(xt)
# Save Current Observation to Trajectory Array
self.y[:, t] = torch.squeeze(yt)
################################
### Save Current to Previous ###
################################
self.x_prev = xt
######################
### Generate Batch ###
######################
def GenerateBatch(self, size, T, Q_noise, R_noise, randomInit=False, seqInit=False, T_test=0, steady_state=False, is_control_enable=True):
# Allocate Empty Array for Input
self.Input = torch.empty(size, self.n, T)
# Allocate Empty Array for Target
self.Target = torch.empty(size, self.m, T)
### Generate Examples
initConditions = self.m1x_0
# Generate Sequence
for i in range(0, size):
# Noise sequence
q_noise = Q_noise[i]
r_noise = R_noise[i]
# Randomize initial conditions to get a rich dataset
if(randomInit):
variance = 100
initConditions = torch.rand_like(self.m1x_0) * variance
if(seqInit):
initConditions = self.x_prev
if((i*T % T_test)==0):
initConditions = torch.zeros_like(self.m1x_0)
self.InitSequence(initConditions, self.m2x_0)
self.GenerateSequence(T, q_noise, r_noise, steady_state=steady_state, is_control_enable=is_control_enable)
# Training sequence input
self.Input[i, :, :] = self.y[:,1:]
# Training sequence output
self.Target[i, :, :] = self.x[:,1:]
def sampling(self, q, r, gain):
if (gain != 0):
gain_q = 0.1
#aq = gain * q * np.random.randn(self.m, self.m)
aq = gain_q * q * torch.eye(self.m)
#aq = gain_q * q * torch.tensor([[1.0, 1.0], [1.0, 1.0]])
else:
aq = 0
Aq = q * torch.eye(self.m) + aq
Q_gen = np.transpose(Aq) * Aq
if (gain != 0):
gain_r = 0.5
#ar = gain * r * np.random.randn(self.n, self.n)
ar = gain_r * r * torch.eye(self.n)
#ar = gain_r * r * torch.tensor([[1.0, 1.0], [1.0, 1.0]])
else:
ar = 0
Ar = r * torch.eye(self.n) + ar
R_gen = np.transpose(Ar) * Ar
return [Q_gen, R_gen]
######################
######## LQR #########
######################
def InitCostMatrices(self, QN, Qx, Qu):
self.QT = QN
self.Qx = Qx
self.Qu = Qu
def ComputeLQRgains(self):
# self.L, self.S = lqr_finite(self.T, self.f, self.G, self.QT, self.Qx, self.Qu, self.is_mismatch)
self.L, self.S = lqr_finite(self.T_test, self.f, self.G, self.QT, self.Qx, self.Qu, self.is_mismatch)
self.L_infinite, self.S_infinite = lqr_infinite(self.f, self.G, self.Qx, self.Qu, self.is_mismatch)
# if isinstance(self.system, LinearSystem):
# self.L_true, self.S_true = lqr_finite(
# self.T, self.system.F, self.system.G, self.QT, self.Qx, self.Qu)
# self.L_infinite_true, self.S_infinite_true = lqr_infinite(
# self.system.F, self.system.G, self.Qx, self.Qu)
# else:
# self.L_true, self.S_true = self.L, self.S
# self.L_infinite_true, self.S_infinite_true = self.L_infinite, self.S_infinite
def GenNoiseSequence(self, T, N_samples):
# Allocate storage for data
seq_Q = torch.empty(N_samples, self.m, T)
seq_R = torch.empty(N_samples, self.n, T)
# Distributions to sample from
distrib_Q = MultivariateNormal(loc=torch.zeros([self.m]), covariance_matrix=self.Q)
distrib_R = MultivariateNormal(loc=torch.zeros([self.n]), covariance_matrix=self.R)
for k in range(N_samples):
for t in range(T):
q_sample = distrib_Q.rsample()
r_sample = distrib_R.rsample()
seq_Q[k,:,t] = q_sample
seq_R[k,:,t] = r_sample
noise = (seq_Q, seq_R)
return noise