nsaunier/traffic-intelligence
developement for CIV8740 midterm
Commit c15b9fcdbcb1 · Nicolas Saunier · 2025-02-24 16:30 -0500
Comments
No comments yet.
Diff
diff --git a/trafficintelligence/traffic_engineering.py b/trafficintelligence/traffic_engineering.py
--- a/trafficintelligence/traffic_engineering.py
+++ b/trafficintelligence/traffic_engineering.py
@@ -3,9 +3,10 @@
from math import ceil
from numpy import e, log, arange
+from numpy.random import triangular
from scipy import stats
-from matplotlib.pyplot import figure,plot,xlabel,ylabel, xlim, ylim
+from matplotlib.pyplot import figure, plot, xlabel, ylabel, xlim, ylim
from trafficintelligence import prediction
@@ -28,7 +29,7 @@
return headways
class RoadUser(object):
- '''Simple example of inheritance to plot different road users '''
+ '''Simple example of inheritance to plot different road users (no history)'''
def __init__(self, position, velocity):
'Both fields are 2D numpy arrays'
self.position = position.astype(float)
@@ -38,13 +39,11 @@
self.position += deltaT*self.velocity
def draw(self, init = False):
- from matplotlib.pyplot import plot
if init:
self.plotLine = plot(self.position[0], self.position[1], self.getDescriptor())[0]
else:
self.plotLine.set_data(self.position[0], self.position[1])
-
class PassengerVehicle(RoadUser):
def getDescriptor(self):
return 'dr'
@@ -57,6 +56,56 @@
def getDescriptor(self):
return 'og'
+class SLUser(object):
+ '''Class for single lane road users
+
+ Warning: does not work with decimal time, time must be integer based'''
+ def __init__(self, t0, x0, v0, sigma):
+ 'sigma is the width of the triangular distribution around 1 for speed noise in ]0,0.1]'
+ self.t = [t0]
+ self.x = [x0]
+ self.v = [v0]
+ self.sigma = max(0.001,min(0.1,sigma))
+ self.left = 1.-self.sigma
+ self.right = 1.+self.sigma
+
+ def getT(self):
+ return self.t
+
+ def existsAt(self, t):
+ return t in self.t
+
+ def getX(self):
+ return self.x
+
+ def getXAt(self, t):
+ if self.existsAt(t):
+ i = self.t.index(t)
+ return self.x[i]
+ else:
+ return None
+
+ def getV(self):
+ return self.v
+
+ def getVAt(self, t):
+ if self.existsAt(t):
+ i = self.t.index(t)
+ return self.v[i]
+ else:
+ return None
+
+ def update(self, a, deltat):
+ self.t.append(self.t[-1]+1)
+ self.v.append(max(0,self.v[-1]+a*deltat*triangular(self.left,1., self.right)))
+ self.x.append(self.x[-1]+self.v[-1]*deltat)
+
+ def plotX(self, deltat):
+ plot([t*deltat for t in self.t], self.x)
+
+ def plotV(self, deltat):
+ plot([t*deltat for t in self.t], self.v)
+
#########################
# queueing models
#########################