changeset 1297:c15b9fcdbcb1

developement for CIV8740 midterm
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 24 Feb 2025 16:30:02 -0500
parents d073524de272
children f4d4bb9ec34f
files trafficintelligence/traffic_engineering.py
diffstat 1 files changed, 53 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/traffic_engineering.py	Fri Feb 21 17:09:19 2025 -0500
+++ b/trafficintelligence/traffic_engineering.py	Mon Feb 24 16:30:02 2025 -0500
@@ -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
 #########################