Simulating traffic with cellular automata. Creating roads systems and measure traffic.
Main window with editor:
Simulation window:
Create virtual environment with virtualenv
:
virtualenv .venv
Activate virtual environemnt:
source .venv/bin/activate
Or on windows:
.venv\Scripts\activate.bat
Then install packages with:
pip install -r requirements.txt
Now you have successfully configured your environment.
Run main.py
with:
python main.py
The interface has 12 elements with following functions:
- File - allows user to save the model currently on canvas or load an existing one from the hard drive
- Preview of part - previews the currently loaded block (blocks are like brushes in painting apps, but this time, they're predefined road mini models)
- Load model - button that readies the currently selected mini model for use on the canvas
- Block selection - this is where user selects the block they want to use for modelling (the blocks will be described in the next section)
- Max steps - defines the length of the simulation (in iterations)
- Cars - defines how many car models should spawn in the simulation
- Start simulation - button that runs the simulation using chosen parameters and current model on canvas
- Resize map x y - fields used for input of the desired simulation map size (in pixels/cells)
- Resize map - button
- Turn probability editing on/off - A button used for editing the probability on road cells with more than 1 direction (first click enables edit mode, the next disables it)
- Turn probability - field used to input the chance (in %) with which the car should choose the default option (first direction) on the intersection, the default option is defined by the oldest road block placed on the given cell.
- The canvas - allows the user to "paint" the desired traffic model using blocks
After the "Start simulation" button is pressed, the user will be shown real-time visualization of of the traffic movement on the model. The simulation may be canceled at any time by pressing the "Esc" key. Every iteration, the app collects data on the status of every car in the simulation, as well as global statistics. After the simulation is over, the app saves all the results to the "Results" folder and shows the user the visualization of the final state of the simulation, as well as heat maps of traffic flow and traffic jams.
As of the moment of writing this section, the app supports a choice of 20 pre-defined blocks:
- Generate straight-road - a flexible block that generates a straight road using given parameters (the road can be up to 3 pixels wide, meaning 3 cars can move through it in parallel and an indefinite length).
- cross-section - A basic cross section of 2 one-way roads.
- cross-section-X - A cross section of 2 bidirectional roads.
- traffic-circle - A basic roundabout with a width of 1 pixel
- cross-section T-up - cross section T model facing north
- cross-section T-down - cross section T model facing south
- cross-section T-left - cross section T model facing west
- cross-section T-right - cross section T model facing east
- eraser - a blank block with flexible size, can be used to erase part of the model
- Measuring flag - a block used to set up a flag of flexible size that measures traffic jams in an area
- Cross-section-X-lights - cross section X model with traffic lights
- cross-section-T-up-lights - cross section T model facing north with traffic lights
- cross-section-T-down-lights - cross section T model facing south with traffic lights
- cross-section-T-left-lights - cross section T model facing west with traffic lights
- cross-section-T-right-lights - cross section T model facing east with traffic lights
- traffic-light - a block used to set up a traffic light at selected position in the model
- priority - a block used to add or remove priority on the selected road cell
- starting_point - a block used to define the spawning point of car models, including the chance at which they'll use it instead of the other ones
- Part flow measurement - a block used to place a flag with flexible size that collects data on traffic flow in the selected area
- Double roundabout - A basic roundabout with a width of 2 pixels, also has smaller size compared to the other one
To start modelling all a user has to do is to select a block and load it. Some blocks are flexible, meaning their parameters can be adjusted. It is then possible to place as many copies of this block as necessary. When two blocks intersect on a cell, their directions are summed (for example, if 2 straight roads intersect, they form an intersection). The default action (direction) is then defined by the oldest road on a given cell, while the other direction is treated as an alternative. Each cell can have up to 2 directions to choose from. The model size can be adjusted on the go, however it's worth noting that if there are any road cells beyond the new model size(when reducing the size), they will be deleted. It is recommended to place the measuring flags after the model is finished (and optionally saved) as they cannot be deleted. Every model requires a placement of at least 1 starting point. The starting points can be added and removed using the same tool. Each of those points requires specifying an intensity, so it is recommended to either carefully plan their placement or experiment with a previously saved copy of the model. Once the model is finished, the user can specify the parameters of the simulation (maximum steps and car number). Once those are satisfactory, the simulation can be started by pressing the appropriate button. The user can either wait for the simulation to end or cancel it early in case of collecting a satisfactory amount of data. The results are then saved to a subfolder with name based on the starting time of the simulation, under the "Results" folder.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.