annotate python/traffic_engineering.py @ 198:c91c8fd8bf1e

simple vehicle model with constant acceleration
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 16 Feb 2012 18:59:12 -0500
parents 2bf5b76320c0
children aeaaf5579b46
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
73
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
2 ''' Traffic Engineering Tools and Examples'''
33
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
73
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
8
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
9 #########################
198
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
10 # Simulation
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
11 #########################
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
12
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
13 class Vehicle:
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
14 'Generic vehicle class'
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
15 def __init__(self, position = 0, speed = 0, acceleration = 0, prt = 2.5, leader = None, log=True):
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
16 self.position = position
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
17 self.speed = speed
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
18 self.acceleration = acceleration
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
19 self.prt = prt
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
20 self.leader = leader
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
21 self.log = log
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
22 if log:
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
23 self.positions = [position]
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
24 self.speeds = [speed]
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
25 self.accelerations = [acceleration]
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
26 # todo add microModel
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
27
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
28 def updatePosition(self, dt):
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
29 speed = self.speed
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
30 self.speed += self.acceleration*dt
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
31 self.position += speed*dt
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
32 if self.log:
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
33 self.positions.append(self.position)
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
34 self.speeds.append(self.speed)
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
35 self.accelerations.append(self.acceleration)
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
36
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
37 def updateAcceleration(self, dt):
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
38 '''Updates acceleration and speed as a function of leader
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
39 and other factors'''
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
40 pass
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
41
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
42 def update(self, dt):
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
43 self.updatePosition(dt)
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
44 self.updateAcceleration(dt) # function of leader
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
45
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
46 def printStats(self):
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
47 print('{0} {1} {2}'.format(self.position, self.speed, self.acceleration))
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
48
c91c8fd8bf1e simple vehicle model with constant acceleration
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
49 #########################
73
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
50 # fundamental diagram
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
51 #########################
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
52
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
53 class FundamentalDiagram:
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
54 ''' '''
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
55 def __init__(self, name):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
56 self.name = name
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
57
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
58 def q(self, k):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
59 return k*self.v(k)
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
60
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
61 @staticmethod
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
62 def meanHeadway(k):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
63 return 1/k
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
64
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
65 @staticmethod
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
66 def meanSpacing(q):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
67 return 1/q
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
68
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
69 def plotVK(self, language='fr', units={}):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
70 from numpy import arange
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
71 from matplotlib.pyplot import figure,plot,xlabel,ylabel
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
72 densities = [k for k in arange(1, self.kj+1)]
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
73 figure()
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
74 plot(densities, [self.v(k) for k in densities])
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
75 xlabel('Densite (veh/km)') # todo other languages and adapt to units
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
76 ylabel('Vitesse (km/h)')
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
77
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
78 def plotQK(self, language='fr', units={}):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
79 from numpy import arange
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
80 from matplotlib.pyplot import figure,plot,xlabel,ylabel
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
81 densities = [k for k in arange(1, self.kj+1)]
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
82 figure()
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
83 plot(densities, [self.q(k) for k in densities])
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
84 xlabel('Densite (veh/km)') # todo other languages and adapt to units
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
85 ylabel('Debit (km/h)')
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
86
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
87 class GreenbergFD(FundamentalDiagram):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
88 '''Speed is the logarithm of density'''
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
89 def __init__(self, vc, kj):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
90 FundamentalDiagram.__init__(self,'Greenberg')
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
91 self.vc=vc
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
92 self.kj=kj
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
93
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
94 def v(self,k):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
95 from numpy import log
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
96 return self.vc*log(self.kj/k)
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
97
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
98 def criticalDensity(self):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
99 from numpy import e
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
100 self.kc = self.kj/e
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
101 return self.kc
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
102
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
103 def capacity(self):
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
104 self.qmax = self.kc*self.vc
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
105 return self.qmax
930a6282c9a9 added class for fundamental diagram (including plotting) with implementation of Greenberg model
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 37
diff changeset
106
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
107 #########################
116
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
108 # intersection
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
109 #########################
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
110
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
111 class FourWayIntersection:
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
112 '''Simple class for simple intersection outline'''
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
113 def __init__(self, dimension, coordX, coordY):
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
114 self.dimension = dimension
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
115 self.coordX = coordX
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
116 self.coordY = coordY
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
117
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
118 def plot(self, options = 'k'):
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
119 from matplotlib.pyplot import plot, axis
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
120
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
121 minX = min(self.dimension[0])
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
122 maxX = max(self.dimension[0])
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
123 minY = min(self.dimension[1])
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
124 maxY = max(self.dimension[1])
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
125
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
126 plot([minX, self.coordX[0], self.coordX[0]], [self.coordY[0], self.coordY[0], minY],options)
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
127 plot([self.coordX[1], self.coordX[1], maxX], [minY, self.coordY[0], self.coordY[0]],options)
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
128 plot([minX, self.coordX[0], self.coordX[0]], [self.coordY[1], self.coordY[1], maxY],options)
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
129 plot([self.coordX[1], self.coordX[1], maxX], [maxY, self.coordY[1], self.coordY[1]],options)
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
130 axis('equal')
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
131
2bf5b76320c0 moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 87
diff changeset
132 #########################
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
133 # traffic signals
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
134 #########################
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
135
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
136 class Volume:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
137 '''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
138 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
139 '''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
140
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
141 # check the sizes of the lists
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
142 if sum(proportions) == 1:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
143 self.volume = volume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
144 self.types = types
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
145 self.proportions = proportions
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
146 self.equivalents = equivalents
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
147 self.nLanes = nLanes
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
148 else:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
149 pass
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
150
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
151 def getPCEVolume(self):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
152 '''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
153 v = 0
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
154 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
155 v += p*e
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
156 return v*self.volume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
157
34
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
158 class IntersectionMovement:
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
159 '''Represents an intersection movement
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
160 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
161 and an equivalent for movement type'''
34
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
162 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
163 self.volume = volume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
164 self.type = type
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
165 self.mvtEquivalent = mvtEquivalent
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
166
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
167 def getTVUVolume(self):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
168 return self.mvtEquivalent*self.volume.getPCEVolume()
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
169
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
170 class IntersectionApproach:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
171 def __init__(self, leftTurnVolume, throughVolume, rightTurnVolume):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
172 self.leftTurnVolume = leftTurnVolume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
173 self.throughVolume = throughVolume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
174 self.rightTurnVolume = rightTurnVolume
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
175
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
176 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
177 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
178
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
179 class LaneGroup:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
180 '''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
181
34
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
182 def __init__(self, movements, nLanes):
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
183 self.movements = movements
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
184 self.nLanes = nLanes
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
185
34
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
186 def getTVUVolume(self):
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
187 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
188
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
189 def checkProtectedLeftTurn(leftMvt, opposedThroughMvt):
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
190 '''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
191 The lane groups should contain left and through movement'''
34
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
192 return leftMvt.volume >= 200 or leftMvt.volume*opposedThroughMvt.volume/opposedThroughMvt.nLanes > 50000
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
193
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
194 def optimalCycle(lostTime, criticalCharge, rounding=True):
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
195 if rounding:
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
196 return ceil((1.5*lostTime+5)/(1-criticalCharge))
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
197 else:
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
198 return (1.5*lostTime+5)/(1-criticalCharge)
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
199
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
200 class Cycle:
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
201 '''Class to compute optimal cycle and the split of effective green times'''
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
202 def __init__(self, phases, lostTime, saturationVolume):
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
203 '''phases is a list of phases
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
204 a phase is a list of lanegroups'''
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
205 self.phases = phases
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
206 self.lostTime = lostTime
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
207 self.saturationVolume = saturationVolume
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
208
34
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
209 def computeCycle(self):
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
210 self.criticalCharges = []
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
211 for phase in self.phases:
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
212 self.criticalCharges.append(max([lg.getTVUVolume() for lg in phase])/(lg.nLanes*self.saturationVolume))
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
213
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
214 self.criticalCharge = sum(self.criticalCharges)
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
215 self.C0 = optimalCycle(self.lostTime, self.criticalCharge)
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
216 return self.C0
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
217
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
218 def computeEffectiveGreen(self):
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
219 from numpy import round
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
220 self.computeCycle() # in case it was not done before
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
221 effectiveGreenTime = self.C0-self.lostTime
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
222 self.effectiveGreens = [round(c*effectiveGreenTime/self.criticalCharge,1) for c in self.criticalCharges]
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
223 return self.effectiveGreens
388a5a25fe92 Code for lab5 works
Nicolas Saunier <nico@confins.net>
parents: 33
diff changeset
224
33
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
225
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
226 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
227 '''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
228 Deceleration is positive
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
229 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
230 if deceleration > 0:
36
571b11304ec9 corrected bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 34
diff changeset
231 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
232 else:
4bd7cc69b6cd added traffic engineering utilities with first version of LaneGroups
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
233 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
234 return None
37
911b52744ceb added uniform delay function
Nicolas Saunier <nico@confins.net>
parents: 36
diff changeset
235
911b52744ceb added uniform delay function
Nicolas Saunier <nico@confins.net>
parents: 36
diff changeset
236 def uniformDelay(cycleLength, effectiveGreen, saturationDegree):
911b52744ceb added uniform delay function
Nicolas Saunier <nico@confins.net>
parents: 36
diff changeset
237 '''Computes the uniform delay'''
911b52744ceb added uniform delay function
Nicolas Saunier <nico@confins.net>
parents: 36
diff changeset
238 return 0.5*cycleLength*(1-float(effectiveGreen)/cycleLength)/(1-float(effectiveGreen*saturationDegree)/cycleLength)
87
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
239
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
240 #########################
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
241 # misc
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
242 #########################
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
243
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
244 def timeChangingSpeed(v0, vf, a, TPR):
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
245 return TPR+(vf-v0)/a
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
246
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
247 def distanceChangingSpeed(v0, vf, a, TPR):
f234154207d4 distance and time to change speed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 73
diff changeset
248 return TPR*v0+(vf*vf-v0*v0)/(2*a)