forked from arjhuang/pise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathojits02_ngsim.py
332 lines (280 loc) · 13.3 KB
/
ojits02_ngsim.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
"""
@author: Archie Huang
Built upon Dr. Maziar Raissi's PINNs - https://github.com/maziarraissi/PINNs
Processed NGSIM Data source: Dr. Allan Avila - https://github.com/Allan-Avila/Highway-Traffic-Dynamics-KMD-Code
Use Tensorflow 1.x
"""
import random
import tensorflow as tf
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy.io
from scipy.interpolate import griddata
from pyDOE import lhs
import time
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import MultipleLocator
import pandas as pd
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
se = 25
np.random.seed(se)
tf.set_random_seed(se)
# PINN Class
class PhysicsInformedNN:
def __init__(self, X_u, u, X_f, layers, lb, ub):
self.lb = lb
self.ub = ub
self.x_u = X_u[:, 0:1]
self.t_u = X_u[:, 1:2]
self.x_f = X_f[:, 0:1]
self.t_f = X_f[:, 1:2]
self.u = u
self.layers = layers
self.weights, self.biases = self.initialize_NN(layers)
self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True))
self.x_u_tf = tf.placeholder(tf.float32, shape=[None, self.x_u.shape[1]])
self.t_u_tf = tf.placeholder(tf.float32, shape=[None, self.t_u.shape[1]])
self.u_tf = tf.placeholder(tf.float32, shape=[None, self.u.shape[1]])
self.x_f_tf = tf.placeholder(tf.float32, shape=[None, self.x_f.shape[1]])
self.t_f_tf = tf.placeholder(tf.float32, shape=[None, self.t_f.shape[1]])
self.u_pred = self.net_u(self.x_u_tf, self.t_u_tf)
self.f_pred = self.net_f(self.x_f_tf, self.t_f_tf)
self.loss = tf.reduce_mean(tf.square(self.u_tf - self.u_pred)) + tf.reduce_mean(tf.square(self.f_pred))
self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method='L-BFGS-B',
options={'maxiter': 10000,
'maxfun': 10000,
'maxcor': 50,
'maxls': 20,
'ftol': 1.0 * np.finfo(float).eps})
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
for l in range(0, num_layers - 1):
W = self.xavier_init(size=[layers[l], layers[l + 1]])
b = tf.Variable(tf.zeros([1, layers[l + 1]], dtype=tf.float32), dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases
def xavier_init(self, size):
in_dim = size[0]
out_dim = size[1]
xavier_stddev = np.sqrt(2 / (in_dim + out_dim))
return tf.Variable(tf.truncated_normal([in_dim, out_dim], stddev=xavier_stddev, seed=se), dtype=tf.float32)
def neural_net(self, X, weights, biases):
num_layers = len(weights) + 1
H = 2.0 * (X - self.lb) / (self.ub - self.lb) - 1.0
for l in range(0, num_layers - 2):
W = weights[l]
b = biases[l]
H = tf.tanh(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def net_u(self, x, t):
u = self.neural_net(tf.concat([x, t], 1), self.weights, self.biases)
return u
def net_f(self, x, t):
u = self.net_u(x, t)
u_t = tf.gradients(u, t)[0]
u_x = tf.gradients(u, x)[0]
f = 0.20 * u_x - 2 * 0.20 / 46.64 * u * u_x - 0.20 / 46.64 * u_t
return f
def callback(self, loss):
print('Loss:', loss)
def train(self):
tf_dict = {self.x_u_tf: self.x_u, self.t_u_tf: self.t_u, self.u_tf: self.u,
self.x_f_tf: self.x_f, self.t_f_tf: self.t_f}
self.optimizer.minimize(self.sess,
feed_dict=tf_dict,
fetches=[self.loss],
loss_callback=self.callback)
def predict(self, X_star):
u_star = self.sess.run(self.u_pred, {self.x_u_tf: X_star[:, 0:1], self.t_u_tf: X_star[:, 1:2]})
f_star = self.sess.run(self.f_pred, {self.x_f_tf: X_star[:, 0:1], self.t_f_tf: X_star[:, 1:2]})
return u_star, f_star
# Regular NN class
class NN:
def __init__(self, X_u, u, X_f, layers, lb, ub):
self.lb = lb
self.ub = ub
self.x_u = X_u[:, 0:1]
self.t_u = X_u[:, 1:2]
self.x_f = X_f[:, 0:1]
self.t_f = X_f[:, 1:2]
self.u = u
self.layers = layers
self.weights, self.biases = self.initialize_NN(layers)
self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True))
self.x_u_tf = tf.placeholder(tf.float32, shape=[None, self.x_u.shape[1]])
self.t_u_tf = tf.placeholder(tf.float32, shape=[None, self.t_u.shape[1]])
self.u_tf = tf.placeholder(tf.float32, shape=[None, self.u.shape[1]])
self.x_f_tf = tf.placeholder(tf.float32, shape=[None, self.x_f.shape[1]])
self.t_f_tf = tf.placeholder(tf.float32, shape=[None, self.t_f.shape[1]])
self.u_pred = self.net_u(self.x_u_tf, self.t_u_tf)
self.f_pred = self.net_f(self.x_f_tf, self.t_f_tf)
self.loss = tf.reduce_mean(tf.square(self.u_tf - self.u_pred))
self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method='L-BFGS-B',
options={'maxiter': 10000,
'maxfun': 10000,
'maxcor': 50,
'maxls': 20,
'ftol': 1.0 * np.finfo(float).eps})
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
for l in range(0, num_layers - 1):
W = self.xavier_init(size=[layers[l], layers[l + 1]])
b = tf.Variable(tf.zeros([1, layers[l + 1]], dtype=tf.float32), dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases
def xavier_init(self, size):
in_dim = size[0]
out_dim = size[1]
xavier_stddev = np.sqrt(2 / (in_dim + out_dim))
return tf.Variable(tf.truncated_normal([in_dim, out_dim], stddev=xavier_stddev), dtype=tf.float32)
def neural_net(self, X, weights, biases):
num_layers = len(weights) + 1
H = 2.0 * (X - self.lb) / (self.ub - self.lb) - 1.0
for l in range(0, num_layers - 2):
W = weights[l]
b = biases[l]
H = tf.tanh(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def net_u(self, x, t):
u = self.neural_net(tf.concat([x, t], 1), self.weights, self.biases)
return u
def net_f(self, x, t):
u = self.net_u(x, t)
u_t = tf.gradients(u, t)[0]
u_x = tf.gradients(u, x)[0]
f = 0.20 * u_x - 2 * 0.20 / 46.64 * u * u_x - 0.20 / 46.64 * u_t
return f
def callback(self, loss):
print('Loss:', loss)
def train(self):
tf_dict = {self.x_u_tf: self.x_u, self.t_u_tf: self.t_u, self.u_tf: self.u,
self.x_f_tf: self.x_f, self.t_f_tf: self.t_f}
self.optimizer.minimize(self.sess,
feed_dict=tf_dict,
fetches=[self.loss],
loss_callback=self.callback)
def predict(self, X_star):
u_star = self.sess.run(self.u_pred, {self.x_u_tf: X_star[:, 0:1], self.t_u_tf: X_star[:, 1:2]})
f_star = self.sess.run(self.f_pred, {self.x_f_tf: X_star[:, 0:1], self.t_f_tf: X_star[:, 1:2]})
return u_star, f_star
if __name__ == "__main__":
N_u = 800
N_f = 12000
layers = [2, 20, 20, 20, 20, 20, 20, 20, 20, 1]
data = scipy.io.loadmat('data/synthetic.mat') # use as frame of x and t
t = data['tScale'].T.flatten()[:, None]
x = data['xScale'].T.flatten()[:, None]
vel = pd.read_table('data/NGSIM_US80_4pm_Velocity_Data.txt', delim_whitespace=True)
# binning
x = (x[:vel.shape[0]] / 5 * 20).astype(int) # 20-ft bins
t = (t[:vel.shape[1]] * 5).astype(int) # 5-s bins
Exact = np.real(vel.T)
X, T = np.meshgrid(x, t)
X_star = np.hstack((X.flatten()[:, None], T.flatten()[:, None]))
u_star = Exact.flatten()[:, None]
lb = X_star.min(0)
ub = X_star.max(0)
############################### Training Data #################################
idx = np.random.choice(X_star.shape[0], N_u, replace=False)
X_u_train = X_star[idx, :]
u_train = u_star[idx, :]
X_f_train = lb + (ub - lb) * lhs(2, N_f)
X_f_train = np.vstack((X_f_train, X_u_train))
############################### Training Data #################################
# PINN Model
model = PhysicsInformedNN(X_u_train, u_train, X_f_train, layers, lb, ub)
start_time = time.time()
model.train()
elapsed = time.time() - start_time
print('Training time: %.4f' % elapsed)
u_pred, f_pred = model.predict(X_star)
error_u = np.linalg.norm(u_star - u_pred, 2) / np.linalg.norm(u_star, 2)
print('Error u: %e' % error_u)
U_pred = griddata(X_star, u_pred.flatten(), (X, T), method='cubic')
Error = np.abs(Exact - U_pred)
# Regular NN Model
model2 = NN(X_u_train, u_train, X_f_train, layers, lb, ub)
start_time2 = time.time()
model2.train()
elapsed2 = time.time() - start_time2
print('Training time: %.4f' % elapsed2)
u_pred2, f_pred2 = model2.predict(X_star)
error_u2 = np.linalg.norm(u_star - u_pred2, 2) / np.linalg.norm(u_star, 2)
print('Error u: %e' % error_u2)
U_pred2 = griddata(X_star, u_pred2.flatten(), (X, T), method='cubic')
Error2 = np.abs(Exact - U_pred2)
################################# Plot #################################
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex",
"text.usetex": True,
"font.family": "serif",
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}",
r"\usepackage[T1]{fontenc}",
]
}
mpl.rcParams.update(pgf_with_latex)
fig = plt.figure(figsize=(8, 6.5))
####### Row 0: PIDL: u(t,x) ##################
gs0 = gridspec.GridSpec(1, 2)
gs0.update(top=0.9, bottom=0.6, left=0.15, right=0.85, wspace=0)
ax = plt.subplot(gs0[:, :])
ax.tick_params(axis='both', which='major', labelsize=20)
ax.yaxis.set_major_locator(MultipleLocator(200))
h = ax.imshow(U_pred, interpolation='nearest', cmap='rainbow_r',
extent=[x.min(), x.max(), t.min(), t.max()],
origin='lower', aspect='auto')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cax.tick_params(labelsize=20)
fig.colorbar(h, cax=cax, ticks=[0, 10, 20, 30, 40, 50, 60, 70])
ax.plot(X_u_train[:, 0], X_u_train[:, 1], 'kx', markersize=0.8, clip_on=False)
ax.set_ylabel('Time $t$ (s)', fontsize=20)
ax.set_xlabel('Location $x$ (m)', fontsize=20)
ax.legend(frameon=False, loc='best', fontsize=20)
ax.set_title('PIDL Estimation $v (x,t)$ (m/s)', fontsize=20)
####### Row 1: DL: u(t,x) ##################
gs1 = gridspec.GridSpec(1, 2)
gs1.update(top=0.4, bottom=0.1, left=0.15, right=0.85, wspace=0)
ax = plt.subplot(gs1[:, :])
ax.tick_params(axis='both', which='major', labelsize=20)
ax.yaxis.set_major_locator(MultipleLocator(200))
h = ax.imshow(U_pred2, interpolation='nearest', cmap='rainbow_r',
extent=[x.min(), x.max(), t.min(), t.max()],
origin='lower', aspect='auto')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cax.tick_params(labelsize=20)
fig.colorbar(h, cax=cax, ticks=[0, 10, 20, 30, 40, 50, 60, 70])
ax.plot(X_u_train[:, 0], X_u_train[:, 1], 'kx', markersize=0.8, clip_on=False)
ax.set_ylabel('Time $t$ (s)', fontsize=20)
ax.set_xlabel('Location $x$ (m)', fontsize=20)
ax.legend(frameon=False, loc='best', fontsize=20)
ax.set_title('DL Estimation $v (x,t)$ (m/s)', fontsize=20)
plt.savefig('figures/ngsim{}_pidl_dl.pdf'.format(N_u))
plt.savefig('figures/ngsim{}_pidl_dl.eps'.format(N_u))
plt.show()
################################# Plot #################################