comparison python/traffic_engineering.py @ 665:15e244d2a1b5

corrected bug with circular import for VideoFilenameAddable, moved to base module
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 20 May 2015 13:57:47 +0200
parents 3b13ec964476
children 3666342dabe2
comparison
equal deleted inserted replaced
664:455f9b93819c 665:15e244d2a1b5
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 ''' Traffic Engineering Tools and Examples''' 2 ''' Traffic Engineering Tools and Examples'''
3 3
4 import prediction
5
4 from math import ceil 6 from math import ceil
5
6 import prediction
7
8 __metaclass__ = type
9 7
10 8
11 ######################### 9 #########################
12 # Simulation 10 # Simulation
13 ######################### 11 #########################
24 h = expovariate(flow) 22 h = expovariate(flow)
25 headways.append(h) 23 headways.append(h)
26 totalTime += h 24 totalTime += h
27 return headways 25 return headways
28 26
29 class RoadUser: 27 class RoadUser(object):
30 '''Simple example of inheritance to plot different road users ''' 28 '''Simple example of inheritance to plot different road users '''
31 def __init__(self, position, velocity): 29 def __init__(self, position, velocity):
32 'Both fields are 2D numpy arrays' 30 'Both fields are 2D numpy arrays'
33 self.position = position.astype(float) 31 self.position = position.astype(float)
34 self.velocity = velocity.astype(float) 32 self.velocity = velocity.astype(float)
59 57
60 ######################### 58 #########################
61 # fundamental diagram 59 # fundamental diagram
62 ######################### 60 #########################
63 61
64 class FundamentalDiagram: 62 class FundamentalDiagram(object):
65 ''' ''' 63 ''' '''
66 def __init__(self, name): 64 def __init__(self, name):
67 self.name = name 65 self.name = name
68 66
69 def q(self, k): 67 def q(self, k):
117 115
118 ######################### 116 #########################
119 # intersection 117 # intersection
120 ######################### 118 #########################
121 119
122 class FourWayIntersection: 120 class FourWayIntersection(object):
123 '''Simple class for simple intersection outline''' 121 '''Simple class for simple intersection outline'''
124 def __init__(self, dimension, coordX, coordY): 122 def __init__(self, dimension, coordX, coordY):
125 self.dimension = dimension 123 self.dimension = dimension
126 self.coordX = coordX 124 self.coordX = coordX
127 self.coordY = coordY 125 self.coordY = coordY
142 140
143 ######################### 141 #########################
144 # traffic signals 142 # traffic signals
145 ######################### 143 #########################
146 144
147 class Volume: 145 class Volume(object):
148 '''Class to represent volumes with varied vehicule types ''' 146 '''Class to represent volumes with varied vehicule types '''
149 def __init__(self, volume, types = ['pc'], proportions = [1], equivalents = [1], nLanes = 1): 147 def __init__(self, volume, types = ['pc'], proportions = [1], equivalents = [1], nLanes = 1):
150 '''mvtEquivalent is the equivalent if the movement is right of left turn''' 148 '''mvtEquivalent is the equivalent if the movement is right of left turn'''
151 149
152 # check the sizes of the lists 150 # check the sizes of the lists
170 v = 0 168 v = 0
171 for p, e in zip(self.proportions, self.equivalents): 169 for p, e in zip(self.proportions, self.equivalents):
172 v += p*e 170 v += p*e
173 return v*self.volume 171 return v*self.volume
174 172
175 class IntersectionMovement: 173 class IntersectionMovement(object):
176 '''Represents an intersection movement 174 '''Represents an intersection movement
177 with a volume, a type (through, left or right) 175 with a volume, a type (through, left or right)
178 and an equivalent for movement type''' 176 and an equivalent for movement type'''
179 def __init__(self, volume, mvtEquivalent = 1): 177 def __init__(self, volume, mvtEquivalent = 1):
180 self.volume = volume 178 self.volume = volume
181 self.mvtEquivalent = mvtEquivalent 179 self.mvtEquivalent = mvtEquivalent
182 180
183 def getTVUVolume(self): 181 def getTVUVolume(self):
184 return self.mvtEquivalent*self.volume.getPCUVolume() 182 return self.mvtEquivalent*self.volume.getPCUVolume()
185 183
186 class LaneGroup: 184 class LaneGroup(object):
187 '''Class that represents a group of mouvements''' 185 '''Class that represents a group of mouvements'''
188 186
189 def __init__(self, movements, nLanes): 187 def __init__(self, movements, nLanes):
190 self.movements = movements 188 self.movements = movements
191 self.nLanes = nLanes 189 self.nLanes = nLanes
201 199
202 def minimumCycle(lostTime, criticalCharge, degreeSaturation=1.): 200 def minimumCycle(lostTime, criticalCharge, degreeSaturation=1.):
203 'degree of saturation can be used as the peak hour factor too' 201 'degree of saturation can be used as the peak hour factor too'
204 return lostTime/(1-criticalCharge/degreeSaturation) 202 return lostTime/(1-criticalCharge/degreeSaturation)
205 203
206 class Cycle: 204 class Cycle(object):
207 '''Class to compute optimal cycle and the split of effective green times''' 205 '''Class to compute optimal cycle and the split of effective green times'''
208 def __init__(self, phases, lostTime, saturationVolume): 206 def __init__(self, phases, lostTime, saturationVolume):
209 '''phases is a list of phases 207 '''phases is a list of phases
210 a phase is a list of lanegroups''' 208 a phase is a list of lanegroups'''
211 self.phases = phases 209 self.phases = phases