Traffic flow models and simulator
Project description
Macroscopic Traffic Flow Models
A Python library for simulating and analyzing macroscopic traffic flow on highway networks. This package implements two widely-used traffic flow models—Cell Transmission Model (CTM) and METANET—along with network infrastructure components, ramp metering controllers (ALINEA), and visualization tools.
Features
- Traffic Flow Models: CTM (first-order) and METANET (second-order) macroscopic models
- Network Components: Flexible highway network structure with cells, onramps, and offramps
- Control Strategies: ALINEA ramp metering controller
- Visualization: Network topology plotting and simulation result visualization
Installation
The latest stable version of the package can be easily installed through pip. For more information on how to run the code in this repository, including the demo scripts, please refer to the development section below.
pip install traffic-flow-models
Usage Examples
Creating a Network
from traffic-flow-models import Network, Onramp
network = Network()
network.add_cell(length=0.5, lanes=3, lane_capacity=2000,
free_flow_speed=100, jam_density=180)
network.add_cell(length=0.5, lanes=3, lane_capacity=2000,
free_flow_speed=100, jam_density=180,
onramp=Onramp(lanes=1, lane_capacity=2000,
free_flow_speed=100, jam_density=180))
Running Simulations
from traffic-flow-models import CTM, METANET
import numpy as np
# CTM simulation
ctm = CTM()
density, flow, speed, *_ = network.simulate(
duration=1.0, dt=10.0/3600, model=ctm,
mainline_demand=lambda t: 4000,
onramp_demand=lambda t, n: np.array([0, 2000] + [0]*(n-2)),
plot_results=True
)
# METANET simulation
metanet = METANET(tau=22/3600, nu=15, kappa=10, delta=1.4, phi=10, alpha=2)
density, flow, speed, *_ = network.simulate(
duration=1.0, dt=10.0/3600, model=metanet,
mainline_demand=lambda t: 4000,
onramp_demand=lambda t, n: np.array([0, 2000] + [0]*(n-2)),
plot_results=True
)
ALINEA Ramp Metering
from traffic-flow-models import AlineaController
onramp = Onramp(lanes=1, lane_capacity=2000, free_flow_speed=100, jam_density=180,
controller=AlineaController(gain=5.0, setpoint=20.0,measurement_cell=3))
Development
Requirements: Python 3.13 or later
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install package
pip install -e .
# For development (includes pytest)
pip install -e ".[dev]"
On macOS, you may need to install Python 3.13 explicitly:
brew install python@3.13
python3.13 -m venv venv
source venv/bin/activate
pip install -e ".[dev]"
Quickstart: Run one of the demo scripts to see the package in action:
python -m src.demo.ctm_simulation
python -m src.demo.metanet_simulation
Contributions are welcome! Before opening a pull request, please ensure that:
- All tests pass (
pytest) - Code is formatted with Black
- New features include appropriate tests
Project Structure
traffic_flow_models/
├── src/
│ ├── traffic_flow_models/ # Main package
│ │ ├── model/ # Traffic flow models (CTM, METANET)
│ │ ├── network/ # Network components
│ │ └── controller/ # Control strategies (ALINEA)
│ └── demo/ # Demo scripts
└── tests/ # Unit tests
Testing
All tests in this project are written using pytest. To run the tests, execute the following command in the project root directory:
pytest
Release
To release a new version of the macroscopic traffic flow package, please make sure that all tests and builds are passing and follow these steps:
0. Prerequisites (important!)
Locally change to the master branch and make sure that it contains all the latest changes that should be part of the release
git checkout master
git pull origin master
Also, make sure you have the necessary permissions to push tags to the repository.
1. Update version in pyproject.toml
Update the version of the package in the pyproject file according to the next release version according to the conventional commit guidelines. To make sure you update the version correctly, you can run the following command:
git-cliff --bump
⚠️ CAUTION: Do not commit any changelog updates, as these will be handled automatically by the release workflow. Only commit the version change in pyproject.toml. If the version in the pyproject configuration and the changelog are not in sync, the release workflow will fail.
2. Commit the version change
git checkout master # safeguard to ensure you are on the master branch
git commit -m "chore(release): v1.0.0" # replace v1.0.0 with the current version
3. Create and push tag (triggers workflow)
git checkout master # safeguard to ensure you are on the master branch
git tag -a v1.0.0 -m "chore(release): version 1.0.0" # replace v1.0.0 with the current version
git push origin v1.0.0 # push the new tag to GitHub to trigger the release workflow
4. GitHub Actions automatically:
- Validates all versions match
- Generates changelog
- Builds distribution packages
- Publishes to PyPI
- Creates GitHub release
License
This project is licensed under the GNU Affero General Public License version 3 (AGPL-3.0). See the LICENSE file in the repository for the full license text.
References
- CTM: Daganzo, C. F. (1994). The cell transmission model: A dynamic representation of highway traffic consistent with the hydrodynamic theory. Transportation Research Part B, 28(4), 269-287.
- METANET: Messmer, A., & Papageorgiou, M. (1990). METANET: A macroscopic simulation program for motorway networks. Traffic Engineering & Control.
- ALINEA: Papageorgiou, M., Hadj-Salem, H., & Blosseville, J. M. (1991). ALINEA: A local feedback control law for on-ramp metering. Transportation Research Record, 1320, 58-64.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file traffic_flow_models-0.1.3.tar.gz.
File metadata
- Download URL: traffic_flow_models-0.1.3.tar.gz
- Upload date:
- Size: 39.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93c6281f2e3cee19e11bb05916e7da5ec33dae04f9a699af8b5b18b1c2699f17
|
|
| MD5 |
3e2c80b0cbf8836a3489ac9c14c26756
|
|
| BLAKE2b-256 |
70ddaf7d175ce030265a2cffeccc7159693627b09b27a8f1d5be055c128d33a7
|
File details
Details for the file traffic_flow_models-0.1.3-py3-none-any.whl.
File metadata
- Download URL: traffic_flow_models-0.1.3-py3-none-any.whl
- Upload date:
- Size: 44.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4deedf160f05979c3d84d580678a7cb3de88a34319d3c1daf4288ae56fcb3e8
|
|
| MD5 |
1d64fcb5aff760779ef4c93f6a4c92a9
|
|
| BLAKE2b-256 |
2af212fba75cf7173560413cf7482ab9dbe3dcc7d7bac6f5db211c17a038fabb
|