-
Notifications
You must be signed in to change notification settings - Fork 0
/
twotank_codesign.py
352 lines (247 loc) · 9.66 KB
/
twotank_codesign.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import torch
import torch.nn as nn
import numpy as np
import neuromancer.psl as psl
from neuromancer.system import Node, System
from neuromancer.modules import blocks
from neuromancer.modules.activations import activations
from neuromancer.dataset import DictDataset
from neuromancer.constraint import variable
from neuromancer.loss import PenaltyLoss
from neuromancer.problem import Problem
from neuromancer.trainer import Trainer
from neuromancer.dynamics import ode, integrators
from neuromancer.plot import pltCL, pltPhase
from twotank_DPC_endpt import TwoTankPredict, train_DPC
from torch.utils.data import Dataset, DataLoader
import Nets
import matplotlib.pyplot as plt
import twotank_utils
from torch.func import functional_call, vmap, grad
# Computes trajectories x, u of the control system "cl_system" under design c and target xr
def compute_traj(c, x0, xr, cl_system):
batsize = len(x0)
c_expand = c.unsqueeze(1).repeat(1,nsteps+1,1)
data = {'x': x0,
'r': xr,
'c': c_expand}
trajectories = cl_system(data)
x = trajectories['x'].reshape(batsize, nsteps + 1, nx)
u = trajectories['x'].reshape(batsize, nsteps + 1, nx)
return x,u
def c_correction(c, x0, xr, cl_system, c_restore, alpha, n_corr_steps):
xresid_list = []
for _ in range(n_corr_steps):
c = c_restore(c)
x, u = compute_traj(c, x0, xr, cl_system)
x_f = x[:,-1,:]
xr_f = xr[:,-1,:]
grads_x = 2*(x_f - xr_f)
xresid = torch.norm( x_f - xr_f , dim=1).mean().item()
xresid_list.append(xresid)
grads_c = torch.autograd.grad( x_f, c, grads_x, retain_graph = True )[0]
c = c - alpha*grads_c
c = c_restore(c)
return c
seed = 2
torch.manual_seed(seed)
np.random.seed(seed)
plotting = False
"""
Instantiate the two-tank control system class and variables
"""
gt_model = psl.nonautonomous.TwoTank()
# sampling rate
ts = gt_model.params[1]['ts']
# problem dimensions
nx = gt_model.nx # number of states
nu = gt_model.nu # number of control inputs
nref = nx # number of references
nc = 2 # size of c
two_tank_ode = TwoTankPredict()
integrator = integrators.RK4(two_tank_ode, h=torch.tensor(ts)) # using 4th order runge kutta integrator
model = Node(integrator, ['x', 'u', 'c'], ['x'], name='model')
nsteps = 30 # prediction horizon
ntraj = nx*(nsteps + 1) # size of the reference trajectory (for NN input)
# (trajectory includes nsteps+1 for initial condition)
"""
Initiate an upper-level prediction model, and generate its initial
distribution over upper-level variables c, for DPC training
"""
c_input_size = nc
c_net = Nets.ReLUnet(c_input_size, 1, hidden_sizes = [(c_input_size),2*(c_input_size),2*(c_input_size), (c_input_size), nc], batch_norm = True, initialize = True)
sigmoid = torch.nn.Sigmoid()
"""
Instantiate some elements of the dataset: static start and end points for the control task
"""
n_train = 30000
n_dev = 1000
x0 = torch.Tensor([0.0,0.0])
# Generate train x0 and xr
x0_train = x0*torch.ones(n_train, 1, 2)
x2 = torch.rand(n_train,1); x1 = x2*torch.rand(n_train,1)
xf_train = torch.cat((x1,x2),dim=1)
xr_train = xf_train.unsqueeze(1).repeat(1,nsteps+1,1)
# Generate dev x0 and xr
x0_dev = x0*torch.ones(n_dev, 1, 2)
x2 = torch.rand(n_dev,1); x1 = x2*torch.rand(n_dev,1)
xf_dev = torch.cat((x1,x2),dim=1)
xr_dev = xf_dev.unsqueeze(1).repeat(1,nsteps+1,1)
""" Create and pre-train the inner-loop DPC model """
umin = 0
umax = 1.
xmin = 0
xmax = 1.
net = blocks.MLP_bounds(insize=nx + nref + nc,
outsize=nu, hsizes=[32, 32],
nonlin=activations['gelu'], min=umin, max=umax)
""" Define dataset for c: used for DPC training only!! """
cmax = 0.10
cmin = 0.01
c = (cmax-cmin)*torch.rand(n_train,nc) + cmin
target_c_train = c.unsqueeze(1).repeat(1, nsteps+1, 1)
c = (cmax-cmin)*torch.rand(n_dev,nc) + cmin
target_c_dev = c.unsqueeze(1).repeat(1, nsteps+1, 1)
# Used in upper-level prediction and correction models
cpred_min = target_c_train.min().item()
cpred_max = target_c_train.max().item()
def c_sigmoid(c_in):
return ( cpred_min + (cpred_max-cpred_min)*sigmoid(c_in) )
def c_project(c_in):
return torch.clamp(c_in, min=cpred_min, max=cpred_max)
""" Define a model to generate optimal-control traj for each c """
target_net = blocks.MLP_bounds(insize=nx + nref + nc,
outsize=nu, hsizes=[64, 64],
nonlin=activations['gelu'], min=umin, max=umax)
target_train_data = {'x': x0_train,
'r': xr_train,
'c': target_c_train }
target_train_dataset = DictDataset(target_train_data, name='train')
target_dev_data = {'x': x0_dev,
'r': xr_dev,
'c': target_c_dev }
target_dev_dataset = DictDataset(target_dev_data, name='dev')
DPC_target_epochs = 15
print("Entering training for target trajectories for {} epochs".format(DPC_target_epochs))
target_net, target_outputs = train_DPC(target_net, target_train_dataset, target_dev_dataset, DPC_target_epochs, nsteps=nsteps)
target_policy = Node(target_net, ['x', 'r', 'c'], ['u'], name='target_policy')
target_system = System([target_policy, model], nsteps=nsteps,
name='target_system')
"""
Create the upper-level training dataset
"""
batch_size_ul = 800
c_optimizer = torch.optim.Adam(c_net.parameters(), lr=5e-4)
c_train_loader = DataLoader( list(zip(x0_train, xf_train)), shuffle=True, batch_size=batch_size_ul )
gamma = 0.001
corr_steps_train = 5
corr_steps_post = 50
train_corr = 1
if train_corr:
c_restore_train = lambda c, x0, xr: c_correction(c, x0, xr, target_system, c_project, gamma, corr_steps_train)
c_restore_post = lambda c, x0, xr: c_correction(c, x0, xr, target_system, c_project, gamma, corr_steps_post)
else:
c_restore_train = lambda c, x0, xr: c_project(c)
c_restore_post = lambda c, x0, xr: c_correction(c, x0, xr, target_system, c_project, gamma, corr_steps_post)
"""
Upper-level training loop
"""
train_xloss_list = []
train_csum_list = []
test_xloss_list = []
test_csum_list = []
test_loss_list = []
test_ref_mean_list = []
test_cdiff_list = []
test_cabs_list = []
test_xresid_list = []
test_cresid_list = []
epochs = 80
DPC_dev_losses = []
bar_positions = [0]
for epoch in range(epochs):
"""Upper-level eval routine"""
if True:
(x0,xf) = (x0_dev, xf_dev)
batsize = len(xf)
xr = xf.unsqueeze(1).repeat(1,nsteps+1,1)
pred_input = xf
c = c_net(pred_input)
c = c_restore_train( c, x0, xr )
c_expand = c.unsqueeze(1).repeat(1,nsteps+1,1)
data = {'x': x0,
'r': xr,
'c': c_expand }
trajectories = target_system(data)
x = trajectories['x'].reshape(batsize, nsteps + 1, nx)
cdiff = (c[:,0] - c[:,1]).mean()
cabs = (c[:,0] - c[:,1]).abs().mean()
xresid = torch.norm( x[:,-1,:] - xr[:,-1,:] , dim=1).mean() # target/eval only the last time frame
xloss = torch.nn.MSELoss()( x[:,-1,:], xr[:,-1,:] ) # only match the last time frame
csum = c.sum(1).mean()
test_xloss_list.append(xloss.detach().mean().item())
test_csum_list.append( csum.detach().mean().item())
x_dev = x # save for plotting at the end
if epoch==0:
x_dev_init = x
""" Upper-level training of 1 epoch """
# Training: predict c given x0,xr
for (x0, xf) in c_train_loader: # x0 is constant over all samples
batsize = len(x0)
xr = xf.unsqueeze(1).repeat(1,nsteps+1,1)
pred_input = xf
c = c_net(pred_input)
c = c_restore_train( c, x0, xr )
c_expand = c.unsqueeze(1).repeat(1,nsteps+1,1)
data = {'x': x0,
'r': xr,
'c': c_expand }
trajectories = target_system(data)
x = trajectories['x'].reshape(batsize, nsteps + 1, nx)
xloss = torch.nn.MSELoss()( x[:,-1,:], xr[:,-1,:] ) # only match the last time frame
csum = c.sum(1).mean()
closs = 1.0*csum
cdiff = (c[:,0] - c[:,1]).mean()
loss = xloss + closs
loss.backward()
c_optimizer.step()
c_optimizer.zero_grad()
train_xloss_list.append(xloss.detach().mean().item())
train_csum_list.append(csum.detach().mean().item())
plt.semilogy( range(len(train_xloss_list)), train_xloss_list, label = 'xloss')
plt.semilogy( range(len(train_csum_list)), train_csum_list, label = 'csum')
plt.xlabel('Outer training iteration')
plt.ylabel('Training set batch loss')
plt.legend()
plt.show()
plt.semilogy( range(len(test_xloss_list)), test_xloss_list, label = 'xloss')
plt.semilogy( range(len(test_csum_list)), test_csum_list, label = 'csum')
plt.xlabel('Outer test epoch')
plt.ylabel('Test set loss')
plt.legend()
plt.show()
"""
Recompute the test set trajectories
"""
(x0,xf) = (x0_dev, xf_dev)
batsize = len(xf)
xr = xf.unsqueeze(1).repeat(1,nsteps+1,1)
pred_input = xf
c = c_net(pred_input)
#c = c_sigmoid(c)
c = c_correction(c, x0, xr, target_system, c_project, 0.0005, 5)
c_expand = c.unsqueeze(1).repeat(1,nsteps+1,1)
data = {'x': x0,
'r': xr,
'c': c_expand }
trajectories = target_system(data)
x = trajectories['x'].reshape(batsize, nsteps + 1, nx)
xpred = x
cpred = c
for k in range(len(xpred)):
plt.plot( [xf[k][0]], [xf[k][1]], 'r*', label=r"Target point" )
plt.plot( xpred[k][:,0].detach(), xpred[k][:,1].detach(), 'b*-', label=r"Predicted trajectory, c = [{:.4f}, {:.4f}]".format(cpred[k][0], cpred[k][1]) )
plt.xlim(0,1.0)
plt.ylim(0,1.0)
plt.legend()
plt.show()