Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/traffic_engineering.py @ 1297:c15b9fcdbcb1
developement for CIV8740 midterm
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 24 Feb 2025 16:30:02 -0500 |
| parents | f10e84505443 |
| children | d828f3f76273 |
comparison
equal
deleted
inserted
replaced
| 1296:d073524de272 | 1297:c15b9fcdbcb1 |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 ''' Traffic Engineering Tools and Examples''' | 2 ''' Traffic Engineering Tools and Examples''' |
| 3 | 3 |
| 4 from math import ceil | 4 from math import ceil |
| 5 from numpy import e, log, arange | 5 from numpy import e, log, arange |
| 6 from numpy.random import triangular | |
| 6 from scipy import stats | 7 from scipy import stats |
| 7 | 8 |
| 8 from matplotlib.pyplot import figure,plot,xlabel,ylabel, xlim, ylim | 9 from matplotlib.pyplot import figure, plot, xlabel, ylabel, xlim, ylim |
| 9 | 10 |
| 10 from trafficintelligence import prediction | 11 from trafficintelligence import prediction |
| 11 | 12 |
| 12 ######################### | 13 ######################### |
| 13 # Simulation | 14 # Simulation |
| 26 headways.append(h) | 27 headways.append(h) |
| 27 totalTime += h | 28 totalTime += h |
| 28 return headways | 29 return headways |
| 29 | 30 |
| 30 class RoadUser(object): | 31 class RoadUser(object): |
| 31 '''Simple example of inheritance to plot different road users ''' | 32 '''Simple example of inheritance to plot different road users (no history)''' |
| 32 def __init__(self, position, velocity): | 33 def __init__(self, position, velocity): |
| 33 'Both fields are 2D numpy arrays' | 34 'Both fields are 2D numpy arrays' |
| 34 self.position = position.astype(float) | 35 self.position = position.astype(float) |
| 35 self.velocity = velocity.astype(float) | 36 self.velocity = velocity.astype(float) |
| 36 | 37 |
| 37 def move(self, deltaT): | 38 def move(self, deltaT): |
| 38 self.position += deltaT*self.velocity | 39 self.position += deltaT*self.velocity |
| 39 | 40 |
| 40 def draw(self, init = False): | 41 def draw(self, init = False): |
| 41 from matplotlib.pyplot import plot | |
| 42 if init: | 42 if init: |
| 43 self.plotLine = plot(self.position[0], self.position[1], self.getDescriptor())[0] | 43 self.plotLine = plot(self.position[0], self.position[1], self.getDescriptor())[0] |
| 44 else: | 44 else: |
| 45 self.plotLine.set_data(self.position[0], self.position[1]) | 45 self.plotLine.set_data(self.position[0], self.position[1]) |
| 46 | |
| 47 | 46 |
| 48 class PassengerVehicle(RoadUser): | 47 class PassengerVehicle(RoadUser): |
| 49 def getDescriptor(self): | 48 def getDescriptor(self): |
| 50 return 'dr' | 49 return 'dr' |
| 51 | 50 |
| 54 return 'xb' | 53 return 'xb' |
| 55 | 54 |
| 56 class Cyclist(RoadUser): | 55 class Cyclist(RoadUser): |
| 57 def getDescriptor(self): | 56 def getDescriptor(self): |
| 58 return 'og' | 57 return 'og' |
| 58 | |
| 59 class SLUser(object): | |
| 60 '''Class for single lane road users | |
| 61 | |
| 62 Warning: does not work with decimal time, time must be integer based''' | |
| 63 def __init__(self, t0, x0, v0, sigma): | |
| 64 'sigma is the width of the triangular distribution around 1 for speed noise in ]0,0.1]' | |
| 65 self.t = [t0] | |
| 66 self.x = [x0] | |
| 67 self.v = [v0] | |
| 68 self.sigma = max(0.001,min(0.1,sigma)) | |
| 69 self.left = 1.-self.sigma | |
| 70 self.right = 1.+self.sigma | |
| 71 | |
| 72 def getT(self): | |
| 73 return self.t | |
| 74 | |
| 75 def existsAt(self, t): | |
| 76 return t in self.t | |
| 77 | |
| 78 def getX(self): | |
| 79 return self.x | |
| 80 | |
| 81 def getXAt(self, t): | |
| 82 if self.existsAt(t): | |
| 83 i = self.t.index(t) | |
| 84 return self.x[i] | |
| 85 else: | |
| 86 return None | |
| 87 | |
| 88 def getV(self): | |
| 89 return self.v | |
| 90 | |
| 91 def getVAt(self, t): | |
| 92 if self.existsAt(t): | |
| 93 i = self.t.index(t) | |
| 94 return self.v[i] | |
| 95 else: | |
| 96 return None | |
| 97 | |
| 98 def update(self, a, deltat): | |
| 99 self.t.append(self.t[-1]+1) | |
| 100 self.v.append(max(0,self.v[-1]+a*deltat*triangular(self.left,1., self.right))) | |
| 101 self.x.append(self.x[-1]+self.v[-1]*deltat) | |
| 102 | |
| 103 def plotX(self, deltat): | |
| 104 plot([t*deltat for t in self.t], self.x) | |
| 105 | |
| 106 def plotV(self, deltat): | |
| 107 plot([t*deltat for t in self.t], self.v) | |
| 59 | 108 |
| 60 ######################### | 109 ######################### |
| 61 # queueing models | 110 # queueing models |
| 62 ######################### | 111 ######################### |
| 63 | 112 |
