micro_traffic_sim_core - core for traffic simulation via cellular automata
API may change since project is in early stages.
Table of contents
- Introduction
- Project layout
- Quick start (Rust)
- Complete workflow guide
- Add crate to your project
- Basic workflow overview
- Verbose logging
- Creating the grid
- Optionally add conflict zones
- Creating vehicles statically
- Creating vehicles dynamically via trips
- Optionally add traffic lights
- Setting up the simulation session
- Running the simulation
- Extracting results
- Analyzing results
- Visualization with gnuplot
- Key modules / API pointers
Introduction
micro_traffic_sim_core is a Rust core library for microscopic traffic simulation (NaSch-like cellular automata and agent-based behaviours). The codebase contains utilities for grids, agents, intentions, conflicts, pathfinding, and session simulation.
Features:
- NaSch-like cellular automata model with configurable speed limits
- Agent-based vehicle behaviours (cooperative, aggressive)
- Multi-lane roads with lane changing
- Both single-cell and multi-cell vehicles (vehicles occupying multiple cells with single-cell head and tail)
- A* pathfinding for route calculation with depth lookup limit
- Conflict detection and resolution at merge points and intersections
- Traffic lights with configurable signal phases and groups
- Trip generators for dynamic vehicle spawning with configurable probability
- Zone types for traffic flow control (spawn, de-spawn, common, etc.)
| Simple coordinated zone | Simple ring-like grid |
|---|---|
| NaSch one-lane road | NaSch two-lane road |
|---|---|
| Merging roads |
|---|
| Multi-cell vehicles with tail mechanics demonstration |
|---|
Disclaimer: Repository documentation will have a lot of technical details in text form since this is a computational core which will not be used by end-users directly, but rather integrated into service of some sort (gRPC/REST).
Project layout
Top-level modules are exported from src/lib.rs.
Examples live under the examples/ directory. Notable examples:
examples/nasch-one-lane/main.rs- minimal one-lane NaSch exampleexamples/nasch-two-lanes/main.rs- minimal two-lane NaSch exampleexamples/nasch-lanes-merge/main.rs- merging two lanes into oneexamples/nasch-roads-merge/main.rs- merging two roads into oneexamples/ring/main.rs- ring-like road networkexamples/tutorial/main.rs- complete tutorial with grid creation, trips, traffic lights, and CSV-like outputexamples/all-tail- multi-cell vehicles demonstration with tail mechanics, merge conflicts, and crossing scenarios
Benchmarks:
Quick start (Rust)
Prerequisites
- Rust toolchain (
rustup+cargo) - Optionally:
hyperfinefor repeated benchmarking
Run example: NaSch one-lane
Build and run an example (prints CSV-like step output).
-
Run directly (debug):
-
Capture output to a file (useful for plotting):
Build release binary for the example:
Benchmark with hyperfine
Complete workflow guide
Add crate to your project
Add to Cargo.toml:
[]
= "0.1.2"
Basic workflow overview
Every simulation follows this pattern:
- Create Grid - Build road network with cells
- Create Vehicles/Trips - Define agents and their routes
- Setup Session - Initialize simulation runtime
- Run Simulation - Execute time steps
- Extract Results - Collect vehicle states and positions
- Visualize - E.g. plot results with gnuplot
The full example for grid basics is in examples/grid-basics.
The full example for whole simulation is in examples/tutorial.
Verbose logging
Before we continue it is worth to mention that the simulation session supports verbose logging at different levels. This can be very useful for debugging and understanding the simulation flow.
Currently supported levels are:
VerboseLevel::None- No loggingVerboseLevel::Main- Main simulation pipeline steps.VerboseLevel::Additional- More detailed logging: loops of main steps.VerboseLevel::Detailed- Additional detailed logging: internal computations.
How to set verbose level:
use VerboseLevel;
/* ... */
Creating the grid
The grid represents your road network as a collection of connected cells. Each cell is a discrete unit where vehicles can be positioned.
/* ... */
use new_point;
use ;
/* ... */
/* ... */
Cell attributes explained:
-
id: Unique identifier for referencing this cell from other cells (CellIDtype, which isi64) or by vehicles states in the simulation. -
point: Physical coordinates in your coordinate system (PointType- can be geographic with SRID) -
type_zone: Defines the cell's role in traffic flow (ZoneTypeenum). Basic types are:Birth- Vehicles spawn here (start of road)Death- Vehicles despawn here (end of road)Common- Regular road segment
All types are described in
zones.rs. -
speed_limit: Maximum velocity in cellular automata units (integer, cells per simulation step) -
left_cell: Cell ID for left lane changes (CellID, use-1if no connection available) -
forward_cell: Cell ID for forward movement (CellID, use-1if no connection available) -
right_cell: Cell ID for right lane changes (CellID, use-1if no connection available) -
meso_link_id: Identifier linking the cell to a mesoscopic graph (integer,-1if not applicable). Could be used for multi-resolution simulations or aggregated traffic flow analysis.
Connection rules:
- Use
-1to indicate "no connection available" for any cell reference - Left/right connections enable lane changing behavior
- Each cell can have only one forward connection, left connection, and right connection.
- Same time each cell can have multiple incoming connections from other cells, but it is recommended two have only one left/right incoming connection to avoid ambiguity in lane changing (number forward connections is unlimited in that context).
Optionally add conflict zones
Conflict zones handle situations where vehicle paths intersect, defining priority rules for resolution.
use ;
use HashMap;
Optionally add traffic lights
Traffic lights control vehicle flow through signal groups that manage different approaches.
use TrafficLight;
use TrafficLightGroup;
use SignalType;
use new_point;
use HashMap;
Creating vehicles statically
Create individual vehicles with specific starting positions and routes.
use Vehicle;
Multi-cell vehicles:
Vehicles can occupy multiple cells using with_tail_size. The tail follows the head through the road network.
// Create a vehicle with head at cell 5 and tail occupying cells 3 and 4
// Tail order: [furthest from head, ..., closest to head]
let long_vehicle = new
.with_cell // Head position
.with_tail_size // Tail size and initial tail cells
.with_destination
.with_speed
.with_speed_limit
.build;
When a multi-cell vehicle performs a lane change maneuver (LEFT/RIGHT), the tail must complete the same maneuver before the head can perform a conflicting maneuver. This prevents physically impossible situations like turning left while the tail is still turning right.
See examples/all-tail for detailed multi-cell vehicle scenarios.
Creating vehicles dynamically via trips
Create trip generators that spawn vehicles probabilistically during simulation.
use ;
use AgentType;
use BehaviourType;
Setting up the simulation session
When the grid, vehicles, trips, and traffic lights are ready, initialize the simulation session.
use GridRoads;
use Vehicle;
use Trip;
use ;
use Session;
use GridsStorage;
use VerboseLevel;
use HashMap;
Running the simulation and collecting the data
Run the simulation for a defined number of steps, collecting vehicle states at each step.
let steps = 10;
for step in 0..steps
Analyzing results
You can analyze the output data using various tools.
In the basic tutorial examples/tutorial states are aggregated in CSV-like format (with semicolon delimiter) and then visualized with gnuplot(http://www.gnuplot.info/).
In the future, I plan to add example with visualization in the browser using gRPC/WebSockets for integration with server-side.
Key modules / API pointers
- Library entry:
src/lib.rs - Geometry and spatial utilities:
- Point -
src/geom/point.rs - Utilities -
src/geom/spatial.rs
- Point -
- Grid (road network graph):
- Cell unit -
src/grid/cell.rs - Road network -
src/grid/road_network.rs
- Cell unit -
- Pathfinding:
- A* star for grid-based road network graph -
src/shortest_path/router.rs
- A* star for grid-based road network graph -
- Traffic lights and signals:
- Signals -
src/traffic_lights/signals.rs - Signal groups (in context of single junction) -
src/traffic_lights/groups.rs - Traffic light (as single junction) -
src/traffic_lights/lights.rs
- Signals -
- Agents and related functionality:
- Agents' behaviour types -
src/agents/behaviour.rs - Vehicle agents -
src/agents/vehicle.rs - Vehicles storage wrapper -
src/agents/vehicles_storage.rs
- Agents' behaviour types -
- Trips
- Trips generation -
src/trips/trip.rs
- Trips generation -
- Intentions:
- Main utilities -
src/intentions/intention.rs - Intentions storage -
src/intentions/intentions_datastorage.rs - Intentions paths processing -
src/intentions/intention_path.rs
- Main utilities -
- Conflicts & solvers:
- Conflicts -
src/conflicts - Conflict zones -
src/conflict_zones/conflict_zones.rs
- Conflicts -
- Movement
- Basic movement -
src/movement/movement.rs
- Basic movement -
- Simulation session and runtime:
- Grids storage (if future we can have multiple type of grids: for vehicles and for pedestrians) -
src/simulation/grids_storage.rs - Simulation session and steps -
src/simulation/session.rs
- Grids storage (if future we can have multiple type of grids: for vehicles and for pedestrians) -