comparison python/traffic_engineering.py @ 73:930a6282c9a9

added class for fundamental diagram (including plotting) with implementation of Greenberg model
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 17 Jan 2011 16:46:48 -0500
parents 911b52744ceb
children f234154207d4
comparison
equal deleted inserted replaced
72:575340e6fce3 73:930a6282c9a9
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 ''' Traffic Engineering Tools.''' 2 ''' Traffic Engineering Tools and Examples'''
3 3
4 from math import ceil 4 from math import ceil
5 5
6 __metaclass__ = type 6 __metaclass__ = type
7
8
9 #########################
10 # fundamental diagram
11 #########################
12
13 class FundamentalDiagram:
14 ''' '''
15 def __init__(self, name):
16 self.name = name
17
18 def q(self, k):
19 return k*self.v(k)
20
21 @staticmethod
22 def meanHeadway(k):
23 return 1/k
24
25 @staticmethod
26 def meanSpacing(q):
27 return 1/q
28
29 def plotVK(self, language='fr', units={}):
30 from numpy import arange
31 from matplotlib.pyplot import figure,plot,xlabel,ylabel
32 densities = [k for k in arange(1, self.kj+1)]
33 figure()
34 plot(densities, [self.v(k) for k in densities])
35 xlabel('Densite (veh/km)') # todo other languages and adapt to units
36 ylabel('Vitesse (km/h)')
37
38 def plotQK(self, language='fr', units={}):
39 from numpy import arange
40 from matplotlib.pyplot import figure,plot,xlabel,ylabel
41 densities = [k for k in arange(1, self.kj+1)]
42 figure()
43 plot(densities, [self.q(k) for k in densities])
44 xlabel('Densite (veh/km)') # todo other languages and adapt to units
45 ylabel('Debit (km/h)')
46
47 class GreenbergFD(FundamentalDiagram):
48 '''Speed is the logarithm of density'''
49 def __init__(self, vc, kj):
50 FundamentalDiagram.__init__(self,'Greenberg')
51 self.vc=vc
52 self.kj=kj
53
54 def v(self,k):
55 from numpy import log
56 return self.vc*log(self.kj/k)
57
58 def criticalDensity(self):
59 from numpy import e
60 self.kc = self.kj/e
61 return self.kc
62
63 def capacity(self):
64 self.qmax = self.kc*self.vc
65 return self.qmax
7 66
8 ######################### 67 #########################
9 # traffic signals 68 # traffic signals
10 ######################### 69 #########################
11 70