Traffic Simulation
A simple traffic simulation in Python
What is this?
This page shows how to use the Traffic simulator. To run it, you'll need Python 3.4 or higher. You can download and install the latest Python interpreter here.
Once installed, run the program by typing the following command:
python[3] traffic.py [-b B] [-c C] [-t T]
where:
- [-b B] (optional): sets the braking probability of the model. The parameter B must be a number between 0 and 1. If not provided, a default brake probability of 0.4 will be used by the model. Example:
python[3] traffic.py -b 0.25
- [-c C] (optional): sets the collision probability of the model. The parameter C must be a number between 0 and 1. If not provided, a default collision probability of 0.1 will be used by the model. Example:
python[3] traffic.py -c 0.75
- [-t T] (optional): sets the number of iterations that the simulation will run. The parameter T must be a positive number. If not provided, the simulation will run for 50 iterations before stopping. Example:
python[3] traffic.py -t 100
Note: depending on your system, the [3] at the end of the python
command may or may not be needed. Generally, UNIX-like systems (including most Linux distros and Mac OS X) come with Python 2 pre-installed, so it is needed to specify that you want to run Python 3 (i.e., you need to type python3
instead of just python
). Windows systems do not come with Python, so unless you have installed it before, the "3" is not needed.
You can also view this manual directly from the program by typing
python[3] traffic.py -h
The model
The script models a microscopic approach to the traffic simulation. The simulation is composed of small entities representing cars. Each "car" a
has a velocity v
and a position x
. The position and velocity of each cars varies in each step of time t
.
The model uses 4 simple rules to work:
if v < V_MAX then v <-- v + 1
with probability pc, v <-- min(v, dist(ai, ai + 1))
with probability pb, v <-- v - 1
xt + 1 <-- (xt + v)
The first rule sets the maximum speed of each vehicle (just as every road, street, avenue and highway has a speed limit). The value V_MAX
is set by the user (in the program, is 9).
The second rule applies the collision probability pc
. That is, with a given probability (set by the user) a car may not preserve its security distance with the next car, and they collide. If two cars collide, neither of them moves during a stablished amount of time (in the program, is set to 5 iterations).
The third rule applies the braking probability pb
. That is, with a given probability (set by the user) a car may decrease its velocity instead of increase it (just as you de-accelerate when you see traffic ahead, or when you're about to turn around a corner).
The fourth and final rule just updates the position of each car (you advance a number of cells equal to the velocity you currently has).
Using just this four simple rules, the model is able to show interesting behaviors as showed in the next section.
Screenshots
Download the program
You can download the full script here. Feel free to examine, modify and re-distribute the code, but do not forget to cite the original source!