macro_traffic_sim_core
Rust implementation of the classical 4-step macroscopic traffic demand model.
What it does
The library implements the four sequential steps of aggregate travel demand forecasting:
-
Trip Generation - estimates how many trips each zone produces and attracts, using regression or cross-classification on socioeconomic data (population, employment, households, income).
-
Trip Distribution - distributes trips between origin-destination zone pairs via a gravity model. An impedance function (exponential, power, or combined) controls distance sensitivity. Furness (IPF) balancing ensures row/column totals match productions and attractions.
-
Mode Choice - splits the total OD matrix into per-mode matrices (AUTO, BIKE, WALK) using a multinomial logit model with configurable utility functions (time, distance, cost coefficients per mode).
-
Traffic Assignment - loads the AUTO OD matrix onto the network to find User Equilibrium link flows. Only AUTO is assigned because BIKE and WALK do not contribute to road congestion. Three algorithms are available:
- Frank-Wolfe - convex combinations with bisection line search
- MSA - method of successive averages (step = 1/n)
- Gradient Projection - path-based with explicit path management
Steps 2-4 run inside a feedback loop: after each assignment the congested travel times update the skim matrix, which feeds back into distribution and mode choice. This captures the interaction between congestion and route/destination/mode decisions.
Trip Generation
|
v
+---> Trip Distribution <-- skim (travel time)
| |
| v
| Mode Choice
| |
| v
+---- Assignment -----> update skim from congested costs
(repeat N times)
Network format
The library works on a mesoscopic (meso) network based on the GMNS specification:
- Road segment links - pieces of road between nodes, carrying capacity, speed, and lane count.
- Connection links - turn maneuvers at intersections. A connection link exists only if the turn is allowed. This encodes turn restrictions directly in the graph topology - routing algorithms respect them automatically without any extra logic.
The library does not include I/O or CSV parsing. You build the Network
in code or write your own loader (see the examples).
Quick start
Add the dependency:
[]
= "0.1.0"
Minimal usage:
use ModelConfig;
use Network;
use Node;
use Link;
use MultinomialLogit;
use run_four_step_model;
use ExponentialImpedance;
use RegressionGenerator;
use Zone;
// 1. Build network
let mut network = new;
network.add_node.unwrap;
// ... add more nodes and links
// 2. Define zones
let zones = vec!;
// 3. Configure and run
let config = new
.with_feedback_iterations
.with_max_iterations
.build;
let result = run_four_step_model.unwrap;
println!;
println!;
Examples
simple_network - in-memory 4-zone diamond
Builds a small network entirely in code, runs the pipeline, and prints results to stdout. No external files needed.
This example demonstrates:
- Building nodes with zone centroids and coordinates
- Creating bidirectional road segments
- Adding connection links (turn permissions) at intersections
- Balancing trip generation totals for Furness convergence
- Running the full pipeline and inspecting results
Configuration
All pipeline parameters are controlled via ModelConfig:
use ;
use VerboseLevel;
let config = new
.with_assignment_method
// BPR alpha, beta
.with_bpr
// assignment iterations
.with_max_iterations
// relative gap threshold
.with_convergence_gap
// outer loop iterations
.with_feedback_iterations
// IPF balancing iterations
.with_furness_max_iterations
// IPF convergence tolerance
.with_furness_tolerance
.with_verbose_level
.build;
Module structure
macro_traffic_sim_core
assignment/ - traffic assignment algorithms
frank_wolfe - Frank-Wolfe with bisection line search
msa - method of successive averages
gradient_projection - gradient projection (path-based)
shortest_path - Dijkstra, all-or-nothing, skim matrices
config - ModelConfig and builder
error - top-level SimError enum
gmns/ - network data model (GMNS-based)
types - NodeID, LinkID, ZoneID, AgentType, LinkType and so on.
defaults - default speed/capacity/lanes by link type
error - GraphError
meso/ - mesoscopic network
node - Node (intersection/mid-link point)
link - Link (road segment or connection/turn)
network - Network container with adjacency
mode_choice/ - multinomial logit mode split
od/ - OD matrices (dense and sparse)
pipeline/ - 4-step model orchestrator
trip_distribution/ - gravity model + Furness balancing + impedance
trip_generation/ - regression and cross-classification generators
verbose/ - structured logging (tracing-based)
zone - transport analysis zones
Key design decisions
-
Meso graph only. The library operates on the mesoscopic graph where turn restrictions are encoded in the topology. There is no macro-level graph - the meso graph is the single source of truth for all computations. It simplifies the code and avoids synchronization issues between macro and meso representations. In future I may add pipeline steps to generate the meso graph from a macro graph.
-
No I/O in the core. CSV/JSON parsing, file loading, and serialization are the caller's responsibility. The library accepts in-memory data structures. This keeps the core dependency-free (no serde, csv, etc.) and lets users bring their own formats. May be in future I will rethink this and add some basic CSV loaders.
-
BPR volume-delay function. Link travel times follow the standard BPR formula:
$$t(x) = t_0 \cdot \left(1 + \alpha \cdot \left(\frac{x}{c}\right)^\beta\right)$$
where $t_0$ is free-flow time, $x$ is volume, and $c$ is capacity.
References
-
Ortuzar J. de D., Willumsen L.G. Modelling Transport. 4th ed. Wiley, 2011. The standard textbook on the 4-step model, gravity distribution, logit mode choice, and equilibrium assignment.
-
Sheffi Y. Urban Transportation Networks: Equilibrium Analysis with Mathematical Programming Methods. Prentice-Hall, 1985. Frank-Wolfe algorithm, user equilibrium, BPR function, convergence theory. Freely available at https://sheffi.mit.edu/sites/sheffi.mit.edu/files/sheffi_urban_trans_networks_0.pdf
-
Bureau of Public Roads. Traffic Assignment Manual. U.S. Dept. of Commerce, 1964. Origin of the BPR volume-delay function.
-
TransitWiki. Four-step travel model. https://www.transitwiki.org/TransitWiki/index.php/Four-step_travel_model
-
GMNS (General Modeling Network Specification). https://github.com/zephyr-data-specs/GMNS
-
go-gmns - Go implementation of basic data in GMNS. https://github.com/LdDl/go-gmns
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.