Metadata-Version: 2.4
Name: pytraf
Version: 0.3.1
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering
Requires-Dist: numpy>=1.23
Requires-Dist: mkdocs>=1.6,<2 ; extra == 'docs'
Requires-Dist: mkdocs-material>=9.7,<10 ; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.26 ; extra == 'docs'
Requires-Dist: ruff ; extra == 'docs'
Requires-Dist: pandas>=1.5 ; extra == 'pandas'
Requires-Dist: pytest>=7 ; extra == 'test'
Requires-Dist: pandas>=1.5 ; extra == 'test'
Provides-Extra: docs
Provides-Extra: pandas
Provides-Extra: test
License-File: LICENSE
Summary: Path-based traffic assignment with turn delays and multiple vehicle classes.
Keywords: traffic assignment,user equilibrium,transportation,gradient projection,GMNS
Author: Haris Ballis
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://harisbal.github.io/pytraf/changelog/
Project-URL: Documentation, https://harisbal.github.io/pytraf/
Project-URL: Issues, https://github.com/harisbal/pytraf/issues
Project-URL: Repository, https://github.com/harisbal/pytraf

# PyTraf

[![Release](https://img.shields.io/github/v/release/harisbal/pytraf)](https://github.com/harisbal/pytraf/releases)
[![Docs](https://img.shields.io/badge/docs-harisbal.github.io%2Fpytraf-blue)](https://harisbal.github.io/pytraf/)
[![License: MIT](https://img.shields.io/badge/license-MIT-green)](https://github.com/harisbal/pytraf/blob/main/LICENSE)

Path-based traffic assignment with turn delays and multiple vehicle classes.

**Documentation: <https://harisbal.github.io/pytraf/>**

PyTraf is an open-source package for **static user-equilibrium traffic assignment** on road networks.
Given a network and an origin–destination demand matrix, it computes the link and path flows that
satisfy Wardrop's first principle, solving the problem in path space with gradient projection.

Its main features are:

- **Path-based equilibration** by gradient projection, with Frank-Wolfe as a reference method. Path
  flows are retained, so select-link and route-level analyses need no post-processing.
- **Turn-aware routing**: turn prohibitions, fixed turn penalties and flow-dependent turn delays, handled
  exactly by labelling links rather than nodes in the shortest-path search.
- **Multi-class assignment**, with class-specific demand, passenger-car equivalents, generalised link
  costs (e.g. tolls) and link restrictions.
- **Background traffic**: fixed preloads on links and turns, such as buses or through traffic.
- **Tabular results**: link, turn and path results as pandas DataFrames, or as NumPy arrays.
- **Standard input formats**: TNTP benchmark files, GMNS datasets, or plain tables (pandas DataFrames,
  NumPy arrays).
- **Validation** against published equilibrium solutions of the standard test networks.
- **Performance**: a multi-threaded Rust core, usable from Python or directly from Rust.

## Install

```sh
pip install pytraf
pip install pandas        # optional: for the DataFrame inputs and tables
```

Wheels for Linux, macOS and Windows need Python 3.9 or newer and nothing else. To build from source you need
Rust ([rustup](https://rustup.rs)) and [maturin](https://www.maturin.rs):

```sh
uv venv .venv && source .venv/bin/activate        # or python -m venv
uv pip install numpy pandas pytest maturin
maturin develop --release                          # builds and installs `pytraf`
pytest                                             # optional: run the Python tests
```

## Python quick start

```python
import pytraf

net = pytraf.Network.from_tntp("data/tntp/SiouxFalls/SiouxFalls_net.tntp")
demand = pytraf.Demand.from_tntp(net, "data/tntp/SiouxFalls/SiouxFalls_trips.tntp")

result = net.assign(demand, gap=1e-6)
result.converged, result.iterations, result.relative_gap
result.links.flow         # numpy array, one entry per link
result.diagnostics.objective   # Beckmann objective (single class, PCE 1)
result.links.table()      # DataFrame: flow, volume/capacity and time per link
result.paths.table()      # DataFrame: every path with its flow, time and links
print(result.summary())   # a short account of the run
```

From your own tables (DataFrames or dicts of arrays):

```python
net = pytraf.Network.from_tables(links_df, turns_df)           # see docs/guide/python.md for columns
demand = pytraf.Demand.from_table(net, trips_df)
result = net.assign(demand, gap=1e-6)
```

Several classes:

```python
cars   = pytraf.TrafficClass(car_demand)
trucks = pytraf.TrafficClass(truck_demand, pce=2.5,
                           extra_link_cost=toll_in_minutes,        # per link, same units as time
                           allowed_links=~truck_banned)            # bool per link
result = net.assign([cars, trucks], gap=1e-6)
result.links.by_class["trucks"]   # vehicles of one class, by name or position
result.links.volume              # PCE-weighted volume that drives travel times
```

A worked example is in [Getting started](https://harisbal.github.io/pytraf/getting-started/); every table column is in
[the Python guide](https://harisbal.github.io/pytraf/guide/python/).

## Rust quick start

```rust
use pytraf_core::*;
use pytraf_io::tntp::{read_net, read_trips};

let net = read_net("data/tntp/SiouxFalls/SiouxFalls_net.tntp")?;
let trips = read_trips("data/tntp/SiouxFalls/SiouxFalls_trips.tntp")?;
let demand = OdDemand::from_triples(
    net.network.num_nodes(), &trips.origins, &trips.destinations, &trips.flows)?;
let turns = TurnTable::build(&net.network, &net.is_endpoint, &[], TurnOptions::default())?;
let vdf = Bpr::new(net.free_flow_time, net.capacity, net.b, net.power)?;

let result = gradient_projection(&net.network, &turns, &vdf, &demand,
                                 GradientProjectionOptions::default())?;
println!("{} sweeps, gap {:.2e}", result.iterations, result.relative_gap);
```

`gradient_projection_multiclass` takes a slice of `ClassSpec`s and a turn delay function.
The core crate does no file IO: it takes typed arrays, so Rust and Python users call the same code.

## Does it get the right answer?

It reproduces the published solutions of the standard benchmark networks (Sioux Falls, Anaheim) and is
checked against independent models; see [docs/benchmarks.md](https://harisbal.github.io/pytraf/benchmarks/).

## Layout

```
crates/core     algorithms, no IO: network, turns, shortest paths, solvers, path store
crates/io       TNTP reader (and the benchmark example)
crates/python   PyO3 bindings (the `pytraf._pytraf` extension)
python/pytraf     Python package: DataFrame helpers and type stubs
python/tests    pytest suite
data/tntp       benchmark networks from github.com/bstabler/TransportationNetworks
docs            the documentation site source (MkDocs): guides, concepts, design, benchmarks
```

## Documentation

The full documentation is a MkDocs site built from `docs/`:

```sh
uv pip install -e ".[docs]"
mkdocs serve                 # live preview at http://127.0.0.1:8000
mkdocs build --strict        # static site in site/
cargo doc --workspace --no-deps --open     # Rust API reference
```

## Limitations

PyTraf solves the static problem: no queue spillback, and delays depend only on a link's or turn's own
traffic. See [the roadmap](https://harisbal.github.io/pytraf/design/roadmap/).

## Next steps

Planned extensions, roughly in order:

- **Larger networks**: lower path memory and faster flow updates, for models with several thousand zones.
- **OpenStreetMap networks**, converted to GMNS or flat tables in the Python layer.
- **Link-direction optimisation**, built on the assignment engine.
- **Interacting junction models** for signalised and priority junctions.

**Stochastic user equilibrium** is deliberately deferred: PyTraf keeps the deterministic equilibrium for
now. When it is added, path-based logit SUE fits the existing solver best. The full list, with the
current limitations, is in the [roadmap](https://harisbal.github.io/pytraf/design/roadmap/).

## Contributing

Bug reports, questions and pull requests are welcome on
[GitHub](https://github.com/harisbal/pytraf/issues). For larger changes, please open an issue first to
discuss the approach.

- **Tests.** Check new solver features against something independent of the code: a published solution,
  a closed-form equilibrium or an equivalent model.
- **Design decisions** are recorded in [docs/design/decisions](https://github.com/harisbal/pytraf/tree/main/docs/design/decisions); add one for any change to the model or the API.
- **Before opening a pull request**, run:

```sh
cargo fmt --check && cargo clippy --workspace --all-targets
cargo test --workspace
maturin develop --release && pytest
mkdocs build --strict
```

Benchmarks: `cargo run --release -p pytraf-io --example order_benchmark -- Anaheim 1e-4 1e-6`.

**Releases.** The version lives in one place, `[workspace.package]` in `Cargo.toml`; the crates, the
Python package (`pytraf.__version__`) and the docs read it from there. To release, bump it, update
`version` and `date-released` in `CITATION.cff` and the citation in this README, then tag `vX.Y.Z` and
publish a GitHub release.

## Citing PyTraf

If you use PyTraf in your research, please cite it as below (GitHub's "Cite this repository" button gives
the same from [`CITATION.cff`](https://github.com/harisbal/pytraf/blob/main/CITATION.cff)):

> Ballis, H. (2026). *PyTraf: Path-based static traffic assignment* (Version 0.3.1)
> [Computer software]. https://github.com/harisbal/pytraf

```bibtex
@software{ballis_pytraf_2026,
  author  = {Ballis, Haris},
  title   = {{PyTraf}: Path-based static traffic assignment},
  year    = {2026},
  version = {0.3.1},
  url     = {https://github.com/harisbal/pytraf}
}
```

## License

MIT. The benchmark networks in `data/tntp` come from
[TransportationNetworks](https://github.com/bstabler/TransportationNetworks) and keep their own terms.

