Traffic Flow Example
Traffic Flow optimization chooses one route per car through a road network so that total congestion across all road segments is minimised. It models real-world traffic routing and network load balancing.
import getpass
import os
from dotenv import load_dotenv
from luna_quantum.algorithms import SCIP
from luna_usecases.traffic_flow import (
TrafficFlowCollection,
TrafficFlowData,
TrafficFlowFormulation,
TrafficFlowInstance,
)
load_dotenv()
if "LUNA_API_KEY" not in os.environ:
os.environ["LUNA_API_KEY"] = getpass.getpass("Enter your Luna API key: ")
Create Data
Define 3 cars each with 2 route options over a 5-segment road network.
car_routes = [
[[0, 1, 2], [0, 3, 4]], # Car 0: route via 0-1-2 or 0-3-4
[[1, 3], [2, 4]], # Car 1: route via 1-3 or 2-4
[[0, 2, 4], [1, 3]], # Car 2: route via 0-2-4 or 1-3
]
data = TrafficFlowData.from_values(car_routes=car_routes)
print(data.to_string())
Plot Data
Visualize car route options on the road network.
Create Formulation
Assign routes to cars minimizing total road segment congestion.
Traffic Flow Formulation:
Cars: 3
Unique segments: 5
Decision Variables:
y[c,r] in {0,1} for each car c and route r
y[c,r] = 1 if car c takes route r
Total: 6 binary variables
Objective:
minimize sum_s load_s^2
where load_s = sum_{{(c,r) using s}} y[c,r]
Constraints:
1. One route per car (3 constraints):
sum_r y[c,r] == 1 for all c = 0, ..., 2
Create Instance
Combine data and formulation into a solvable instance.
Data:Traffic Flow Data:
Cars: 3
Total routes: 6
Unique segments: 5
Formulation:Traffic Flow Formulation:
Cars: 3
Unique segments: 5
Decision Variables:
y[c,r] in {0,1} for each car c and route r
y[c,r] = 1 if car c takes route r
Total: 6 binary variables
Objective:
minimize sum_s load_s^2
where load_s = sum_{{(c,r) using s}} y[c,r]
Constraints:
1. One route per car (3 constraints):
sum_r y[c,r] == 1 for all c = 0, ..., 2
Formulate Model
Translate the instance into a mathematical optimization model.
Solve and Interpret
Solve the model with SCIP and interpret the raw result into a use-case-specific solution.
scip = SCIP()
job = scip.run(model)
sol = job.result()
uc_solution = instance.interpret(sol)
print(uc_solution.to_string())
/Users/maximilianjanetschek/PycharmProjects/luna-usecases/.venv/lib/python3.13/site-packages/rich/live.py:260:
UserWarning: install "ipywidgets" for Jupyter support
warnings.warn('install "ipywidgets" for Jupyter support')
Plot Solution
Visualize the optimal solution.
Collections
Generate a benchmark collection of random instances for batch processing.
collection = TrafficFlowCollection.from_random(min_cars=2, max_cars=4, n_segments=5, seed=42)
model = collection.instances[0].formulate()
print(model)