-
Notifications
You must be signed in to change notification settings - Fork 5
/
draw_delays.py
67 lines (59 loc) · 2.29 KB
/
draw_delays.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
'''
Created on Jul 27, 2014
@author: jeromethai
'''
import numpy as np
import matplotlib.pyplot as plt
from cvxopt import matrix, spdiag
def true_delays(delaytype):
"""Display true delay functions"""
coef = matrix([0.0, 0.0, 0.0, 0.15])
degree = len(coef)
a, b = 3.5, 3.0
xdata = np.linspace(0.0, 2.5, num=100)
if delaytype == 'Polynomial':
true_vals = [1+(coef.T * matrix(np.power(x,range(1,degree+1))))[0] for x in xdata]
title = 'True polynomial delay function'
if delaytype == 'Hyperbolic':
true_vals = [1 - a/b + a/(b-x) for x in xdata]
title = 'True hyperbolic delay function'
plt.plot( xdata, true_vals)
plt.xlabel('Link flow (1000 veh/h)')
plt.ylabel('Delay')
plt.title(title)
plt.legend()
plt.show()
def lin_delays(coef, current, type):
"""Display different linearizations"""
degree = len(coef)
xdata = np.linspace(0.0, 2.5, num=100)
x0 = [1+(coef.T * matrix(np.power(x,range(1,degree+1))))[0] for x in xdata]
#g = (coef.T * matrix(np.power(current,range(degree))))[0]
g = (coef.T * spdiag(range(1,degree+1)) * matrix(np.power(current,range(degree))))[0]
d = 1+(coef.T * matrix(np.power(current,range(1,degree+1))))[0]
x1 = [x for x in xdata if d+g*(x-current) >= 1.0]
y1 = [d+g*(x-current) for x in x1]
plt.plot(x1, y1, 'r', label='tangent')
plt.plot(xdata, x0, 'b', label='true')
plt.plot(xdata, [d]*100, 'g', label='constant')
plt.xlabel('Link flow (1000 veh/h)')
plt.ylabel('Delay')
plt.title('Linearized '+type+' delay function at x={}'.format(current))
plt.legend()
plt.show()
xdata = np.linspace(0.0, 2.5, num=100)
x0 = [1+(coef.T * matrix(np.power(x,range(1,degree+1))))[0] for x in xdata]
a = (coef.T*matrix(np.power(0.85, range(degree))))[0]
y2 = [1+a*x for x in xdata]
plt.plot(xdata, y2, 'r', label='linearization')
plt.plot(xdata, x0, 'b', label='true')
plt.xlabel('Link flow (1000 veh/h)')
plt.ylabel('Delay')
plt.title('Linearized '+type+' delay function')
plt.legend()
plt.show()
if __name__ == '__main__':
type = 'Hyperbolic'; coef = matrix([0.331, 0.217, 0.0104, 2.33e-7, 8.95e-8, 0.0107])
#type = 'Polynomial'; coef = matrix([0.0, 0.0, 0.0, 0.15])
#true_delays(type)
lin_delays(coef, 2.0, type)