annotate python/traffic_engineering.py @ 33:4bd7cc69b6cd

added traffic engineering utilities with first version of LaneGroups
author Nicolas Saunier <nico@confins.net>
date Sat, 10 Apr 2010 20:46:33 -0400
parents
children 388a5a25fe92
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
1 #! /usr/bin/env python
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
2 ''' Traffic Engineering Tools.'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
3
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
4 from math import ceil
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
5
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
6 __metaclass__ = type
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
7
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
8 #########################
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
9 # traffic signals
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
10 #########################
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
11
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
12 class Volume:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
13 '''Class to represent volumes with varied vehicule types '''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
14 def __init__(self, volume, types = ['pc'], proportions = [1], equivalents = [1], nLanes = 1):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
15 '''mvtEquivalent is the equivalent if the movement is right of left turn'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
16
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
17 # check the sizes of the lists
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
18 if sum(proportions) == 1:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
19 self.volume = volume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
20 self.types = types
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
21 self.proportions = proportions
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
22 self.equivalents = equivalents
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
23 self.nLanes = nLanes
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
24 else:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
25 pass
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
26
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
27 def getPCEVolume(self):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
28 '''Returns the passenger-car equivalent for the input volume'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
29 v = 0
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
30 for p, e in zip(self.proportions, self.equivalents):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
31 v += p*e
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
32 return v*self.volume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
34 class IntersectionMouvement:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
35 '''Represents an intersection movement
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
36 with a volume, a type (through, left or right)
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
37 and an equivalent for movement type'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
38 def __init__(self, volume, type, mvtEquivalent = 1):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
39 self.volume = volume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
40 self.type = type
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
41 self.mvtEquivalent = mvtEquivalent
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
42
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
43 def getTVUVolume(self):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
44 return self.mvtEquivalent*self.volume.getPCEVolume()
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
45
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
46 class IntersectionApproach:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
47 def __init__(self, leftTurnVolume, throughVolume, rightTurnVolume):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
48 self.leftTurnVolume = leftTurnVolume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
49 self.throughVolume = throughVolume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
50 self.rightTurnVolume = rightTurnVolume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
51
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
52 def getTVUVolume(self, leftTurnEquivalent = 1, throughEquivalent = 1, rightTurnEquivalent = 1):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
53 return self.leftTurnVolume.getPCEVolume()*leftTurnEquivalent+self.throughVolume.getPCEVolume()*throughEquivalent+self.rightTurnVolume.getPCEVolume()*rightTurnEquivalent
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
54
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
55 class LaneGroup:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
56 '''Class that represents a group of mouvements'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
57
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
58 def __init__(self):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
59 self.mouvements = {'left': [],
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
60 'through': [],
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
61 'right': []}
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
62
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
63 # all the add*Mvt should add a IntersectionMovement instance
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
64 def addLeftMvt(self, vol):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
65 self.mouvements['left'].append(vol)
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
66
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
67 def addRightMvt(self, vol):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
68 self.mouvements['right'].append(vol)
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
69
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
70 def addThroughMvt(self, vol):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
71 self.mouvements['through'].append(vol)
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
72
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
73 def getTVUVolume(self, leftTurnEquivalent = 1, throughEquivalent = 1, rightTurnEquivalent = 1):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
74 return leftTurnEquivalent*sum([m.getPCEVolume() for m in mouvements['left']])\
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
75 +rightTurnEquivalent*sum([m.getPCEVolume() for m in mouvements['right']])\
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
76 +throughTurnEquivalent*sum([m.getPCEVolume() for m in mouvements['through']])
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
77
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
78 def checkProtectedLeftTurn(leftMvt, opposedThroughMvt):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
79 '''Checks if one of the main two conditions on left turn is verified
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
80 The lane groups should contain left and through movement'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
81 return leftMvt.volume >= 200 or leftMvt.volume*opposedMvt.volume/opposedMvt.nLanes > 50000
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
82
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
83 def optimalCycle(lostTime, criticalCharge):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
84 return ceil((1.5*lostTime+5)/(1-criticalCharge))
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
85
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
86 def computeInterGreen(perceptionReactionTime, initialSpeed, intersectionLength, vehicleAverageLength = 6, deceleration = 3):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
87 '''Computes the intergreen time (yellow/amber plus all red time)
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
88 Deceleration is positive
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
89 All variables should be in the same units'''
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
90 if deceleration > 0:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
91 return [perceptionReactionTime+initialSpeed/(2*deceleration), (intersectionLength+vehicleAverageLength)/initialSpeed]
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
92 else:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
93 print 'Issue deceleration should be strictly positive'
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
94 return None