Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 197:2788b2827670 | 198:c91c8fd8bf1e |
|---|---|
| 3 | 3 |
| 4 from math import ceil | 4 from math import ceil |
| 5 | 5 |
| 6 __metaclass__ = type | 6 __metaclass__ = type |
| 7 | 7 |
| 8 | |
| 9 ######################### | |
| 10 # Simulation | |
| 11 ######################### | |
| 12 | |
| 13 class Vehicle: | |
| 14 'Generic vehicle class' | |
| 15 def __init__(self, position = 0, speed = 0, acceleration = 0, prt = 2.5, leader = None, log=True): | |
| 16 self.position = position | |
| 17 self.speed = speed | |
| 18 self.acceleration = acceleration | |
| 19 self.prt = prt | |
| 20 self.leader = leader | |
| 21 self.log = log | |
| 22 if log: | |
| 23 self.positions = [position] | |
| 24 self.speeds = [speed] | |
| 25 self.accelerations = [acceleration] | |
| 26 # todo add microModel | |
| 27 | |
| 28 def updatePosition(self, dt): | |
| 29 speed = self.speed | |
| 30 self.speed += self.acceleration*dt | |
| 31 self.position += speed*dt | |
| 32 if self.log: | |
| 33 self.positions.append(self.position) | |
| 34 self.speeds.append(self.speed) | |
| 35 self.accelerations.append(self.acceleration) | |
| 36 | |
| 37 def updateAcceleration(self, dt): | |
| 38 '''Updates acceleration and speed as a function of leader | |
| 39 and other factors''' | |
| 40 pass | |
| 41 | |
| 42 def update(self, dt): | |
| 43 self.updatePosition(dt) | |
| 44 self.updateAcceleration(dt) # function of leader | |
| 45 | |
| 46 def printStats(self): | |
| 47 print('{0} {1} {2}'.format(self.position, self.speed, self.acceleration)) | |
| 8 | 48 |
| 9 ######################### | 49 ######################### |
| 10 # fundamental diagram | 50 # fundamental diagram |
| 11 ######################### | 51 ######################### |
| 12 | 52 |
