-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathssmodels_ct.py
268 lines (215 loc) · 9.11 KB
/
ssmodels_ct.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
267
268
import torch
import torch.nn as nn
import numpy as np
from torch.jit import Final
from typing import List
class NeuralStateSpaceModel(nn.Module):
n_x: Final[int]
n_u: Final[int]
n_feat: Final[int]
def __init__(self, n_x, n_u, n_feat=64, scale_dx=1.0, init_small=True, activation='relu'):
super(NeuralStateSpaceModel, self).__init__()
self.n_x = n_x
self.n_u = n_u
self.n_feat = n_feat
self.scale_dx = scale_dx
if activation == 'relu':
activation = nn.ReLU()
elif activation == 'softplus':
activation = nn.Softplus()
elif activation == 'tanh':
activation = nn.Tanh()
self.net = nn.Sequential(
nn.Linear(n_x+n_u, n_feat), # 2 states, 1 input
activation,
nn.Linear(n_feat, n_x)
)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-4)
nn.init.constant_(m.bias, val=0)
def forward(self, in_x, in_u):
in_xu = torch.cat((in_x, in_u), -1) # concatenate x and u over the last dimension to create the [xu] input
dx = self.net(in_xu) # \dot x = f([xu])
dx = dx * self.scale_dx
return dx
class DeepNeuralStateSpaceModel(nn.Module):
n_x: Final[int]
n_u: Final[int]
n_feat: Final[int]
def __init__(self, n_x, n_u, n_feat=64, scale_dx=1.0, init_small=True):
super(DeepNeuralStateSpaceModel, self).__init__()
self.n_x = n_x
self.n_u = n_u
self.n_feat = n_feat
self.scale_dx = scale_dx
self.net = nn.Sequential(
nn.Linear(n_x + n_u, n_feat), # 2 states, 1 input
nn.ReLU(),
nn.Linear(n_feat, n_feat),
nn.ReLU(),
nn.Linear(n_feat, n_x)
)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-4)
nn.init.constant_(m.bias, val=0)
def forward(self, in_x, in_u):
in_xu = torch.cat((in_x, in_u), -1) # concatenate x and u over the last dimension to create the [xu] input
dx = self.net(in_xu) # \dot x = f([xu])
dx = dx * self.scale_dx
return dx
class MechanicalStateSpaceSystem(nn.Module):
n_x: Final[int]
n_u: Final[int]
n_feat: Final[int]
def __init__(self, n_feat=64, init_small=True, typical_ts=1.0):
super(MechanicalStateSpaceSystem, self).__init__()
self.n_feat = n_feat
self.typical_ts = typical_ts
self.net = nn.Sequential(
nn.Linear(3, n_feat), # 2 states, 1 input
nn.ReLU(),
nn.Linear(n_feat, 1)
)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-3)
nn.init.constant_(m.bias, val=0)
def forward(self, in_x, in_u):
list_dx: List[torch.Tensor]
in_xu = torch.cat((in_x, in_u), -1) # concatenate x and u over the last dimension to create the [xu] input
dx_v = self.net(in_xu)/self.typical_ts # \dot x = f([xu])
list_dx = [in_x[..., [1]], dx_v]
dx = torch.cat(list_dx, -1) # dot x = v, dot v = net
return dx
class MechanicalStateSpaceSystemV2(nn.Module):
n_x: Final[int]
n_u: Final[int]
n_feat: Final[int]
def __init__(self, n_feat=64, init_small=True, typical_ts=1.0):
super(MechanicalStateSpaceSystemV2, self).__init__()
self.n_feat = n_feat
self.typical_ts = typical_ts
self.net = nn.Sequential(
nn.Linear(2, n_feat), # 2 states, 1 input
nn.ReLU(),
nn.Linear(n_feat, 1)
)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-3)
nn.init.constant_(m.bias, val=0)
def forward(self, in_x, in_u):
list_dx: List[torch.Tensor]
in_xu = torch.cat((in_x[..., [1]], in_u), -1) # concatenate x and u over the last dimension to create the [xu] input
dx_v = self.net(in_xu)/self.typical_ts # \dot x = f([xu])
list_dx = [in_x[..., [1]], dx_v]
dx = torch.cat(list_dx, -1) # dot x = v, dot v = net
return dx
class StateSpaceModelLin(nn.Module):
def __init__(self, A, B):
super(StateSpaceModelLin, self).__init__()
self.A = nn.Linear(2, 2, bias=False)
self.A.weight = torch.nn.Parameter(torch.tensor(A.astype(np.float32)), requires_grad=False)
self.B = nn.Linear(1, 2, bias=False)
self.B.weight = torch.nn.Parameter(torch.tensor(B.astype(np.float32)), requires_grad=False)
def forward(self, X, U):
dx = self.A(X) + self.B(U)
return dx
class CascadedTanksNeuralStateSpaceModel(nn.Module):
def __init__(self, n_feat=64, scale_dx=1.0, init_small=True, activation='relu'):
super(CascadedTanksNeuralStateSpaceModel, self).__init__()
self.n_feat = n_feat
self.scale_dx = scale_dx
# Neural network for the first state equation = NN(x_1, u)
self.net_dx1 = nn.Sequential(
nn.Linear(2, n_feat),
nn.Tanh(),
#nn.Linear(n_feat, n_feat),
#nn.Tanh(),
nn.Linear(n_feat, 1),
)
# Neural network for the first state equation = NN(x_1, x2)
self.net_dx2 = nn.Sequential(
nn.Linear(2, n_feat),
nn.Tanh(),
#nn.Linear(n_feat, n_feat),
#nn.Tanh(),
nn.Linear(n_feat, 1),
)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net_dx1.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-4)
nn.init.constant_(m.bias, val=0)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net_dx2.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-4)
nn.init.constant_(m.bias, val=0)
def forward(self, in_x, in_u):
# the first state derivative is NN(x1, u)
in_1 = torch.cat((in_x[..., [0]], in_u), -1) # concatenate 1st state component with input
dx_1 = self.net_dx1(in_1)
# the second state derivative is NN(x1, x2)
in_2 = in_x
dx_2 = self.net_dx2(in_2)
# the state derivative is built by concatenation of dx_1 and dx_2, possibly scaled for numerical convenience
dx = torch.cat((dx_1, dx_2), -1)
dx = dx * self.scale_dx
return dx
class CascadedTanksOverflowNeuralStateSpaceModel(nn.Module):
def __init__(self, n_feat=64, scale_dx=1.0, init_small=True):
super(CascadedTanksOverflowNeuralStateSpaceModel, self).__init__()
self.n_feat = n_feat
self.scale_dx = scale_dx
# Neural network for the first state equation = NN(x_1, u)
self.net_dx1 = nn.Sequential(
nn.Linear(2, n_feat),
nn.ReLU(),
#nn.Linear(n_feat, n_feat),
#nn.ReLU(),
nn.Linear(n_feat, 1),
)
# Neural network for the first state equation = NN(x_1, x2, u) # we assume that with overflow the input may influence the 2nd tank instantaneously
self.net_dx2 = nn.Sequential(
nn.Linear(3, n_feat),
nn.ReLU(),
#nn.Linear(n_feat, n_feat),
#nn.ReLU(),
nn.Linear(n_feat, 1),
)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net_dx1.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-4)
nn.init.constant_(m.bias, val=0)
# Small initialization is better for multi-step methods
if init_small:
for m in self.net_dx2.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=1e-4)
nn.init.constant_(m.bias, val=0)
def forward(self, in_x, in_u):
# the first state derivative is NN_1(x1, u)
in_1 = torch.cat((in_x[..., [0]], in_u), -1) # concatenate 1st state component with input
dx_1 = self.net_dx1(in_1)
# the second state derivative is NN_2(x1, x2, u)
in_2 = torch.cat((in_x, in_u), -1) # concatenate states with input to define the
dx_2 = self.net_dx2(in_2)
# the state derivative is built by concatenation of dx_1 and dx_2, possibly scaled for numerical convenience
dx = torch.cat((dx_1, dx_2), -1)
dx = dx * self.scale_dx
return dx