Mercurial > hg > nsaunier > traffic-intelligence
annotate python/traffic_engineering.py @ 61:a8c6d544f015
corrected bug loading Interactions
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 03 Nov 2010 22:51:38 -0400 |
| parents | 911b52744ceb |
| children | 930a6282c9a9 |
| 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 |
| 34 | 34 class IntersectionMovement: |
|
33
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''' |
| 34 | 38 def __init__(self, volume, type, mvtEquivalent = 1): |
|
33
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 |
| 34 | 58 def __init__(self, movements, nLanes): |
| 59 self.movements = movements | |
| 60 self.nLanes = nLanes | |
|
33
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
61 |
| 34 | 62 def getTVUVolume(self): |
| 63 return sum([mvt.getTVUVolume() for mvt in self.movements]) | |
|
33
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
64 |
|
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
65 def checkProtectedLeftTurn(leftMvt, opposedThroughMvt): |
|
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
66 '''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
|
67 The lane groups should contain left and through movement''' |
| 34 | 68 return leftMvt.volume >= 200 or leftMvt.volume*opposedThroughMvt.volume/opposedThroughMvt.nLanes > 50000 |
| 69 | |
| 70 def optimalCycle(lostTime, criticalCharge, rounding=True): | |
| 71 if rounding: | |
| 72 return ceil((1.5*lostTime+5)/(1-criticalCharge)) | |
| 73 else: | |
| 74 return (1.5*lostTime+5)/(1-criticalCharge) | |
| 75 | |
| 76 class Cycle: | |
| 77 '''Class to compute optimal cycle and the split of effective green times''' | |
| 78 def __init__(self, phases, lostTime, saturationVolume): | |
| 79 '''phases is a list of phases | |
| 80 a phase is a list of lanegroups''' | |
| 81 self.phases = phases | |
| 82 self.lostTime = lostTime | |
| 83 self.saturationVolume = saturationVolume | |
|
33
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
84 |
| 34 | 85 def computeCycle(self): |
| 86 self.criticalCharges = [] | |
| 87 for phase in self.phases: | |
| 88 self.criticalCharges.append(max([lg.getTVUVolume() for lg in phase])/(lg.nLanes*self.saturationVolume)) | |
| 89 | |
| 90 self.criticalCharge = sum(self.criticalCharges) | |
| 91 self.C0 = optimalCycle(self.lostTime, self.criticalCharge) | |
| 92 return self.C0 | |
| 93 | |
| 94 def computeEffectiveGreen(self): | |
| 95 from numpy import round | |
| 96 self.computeCycle() # in case it was not done before | |
| 97 effectiveGreenTime = self.C0-self.lostTime | |
| 98 self.effectiveGreens = [round(c*effectiveGreenTime/self.criticalCharge,1) for c in self.criticalCharges] | |
| 99 return self.effectiveGreens | |
| 100 | |
|
33
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
101 |
|
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
102 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
|
103 '''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
|
104 Deceleration is positive |
|
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
105 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
|
106 if deceleration > 0: |
| 36 | 107 return [perceptionReactionTime+float(initialSpeed)/(2*deceleration), float(intersectionLength+vehicleAverageLength)/initialSpeed] |
|
33
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
108 else: |
|
4bd7cc69b6cd
added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
109 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
|
110 return None |
|
37
911b52744ceb
added uniform delay function
Nicolas Saunier <nico@confins.net>
parents:
36
diff
changeset
|
111 |
|
911b52744ceb
added uniform delay function
Nicolas Saunier <nico@confins.net>
parents:
36
diff
changeset
|
112 def uniformDelay(cycleLength, effectiveGreen, saturationDegree): |
|
911b52744ceb
added uniform delay function
Nicolas Saunier <nico@confins.net>
parents:
36
diff
changeset
|
113 '''Computes the uniform delay''' |
|
911b52744ceb
added uniform delay function
Nicolas Saunier <nico@confins.net>
parents:
36
diff
changeset
|
114 return 0.5*cycleLength*(1-float(effectiveGreen)/cycleLength)/(1-float(effectiveGreen*saturationDegree)/cycleLength) |
