Note
Go to the end to download the full example code.
Assigning sparse matrices#
Modern Activity-Based models (and even some trip-based and tour-based ones) result on incredibly sparse demand matrices, which opens up a significant opportunity to save time during assignment by using early-exiting during the path-computation phase of assignment.
To take advantage of this, while still computing assignment skims, AequilibraE has a built-in method to skim the last iteration after the assignment is done.
Technical references
See also
Several functions, methods, classes and modules are used in this example:
aequilibrae.routing.graph()
# Imports
from os.path import join
from tempfile import gettempdir
from uuid import uuid4
from scipy.sparse import csr_array
from aequilibrae.matrix import from_dict
from aequilibrae.traffic import TrafficAssignment, TrafficClass
from aequilibrae.traffic.vdf import bpr
from aequilibrae.examples import create_example
from aequilibrae.utils import basic_config
# We create the example project inside our temp folder
fldr = join(gettempdir(), uuid4().hex)
project = create_example(fldr)
# We'll also apply a basic logging configuration.
basic_config()
<AequilibraETQDMStreamHandler (NOTSET)>
Traffic assignment#
Let’s get the demand matrix directly from the project record, and inspect what matrices we have in the project.
proj_matrices = project.matrices
proj_matrices.get_available_matrices_and_metadata()
{'skims.omx': {'procedure': None, 'procedure_id': None, 'timestamp': None, 'description': 'Example skim'}, 'demand.omx': {'procedure': None, 'procedure_id': None, 'timestamp': '2020-11-24 08:47:18', 'description': 'Original data imported to OMX format'}, 'demand_mc.omx': {'procedure': None, 'procedure_id': None, 'timestamp': '2021-02-24 00:51:35', 'description': None}}
We get the demand matrix, and prepare it for computation
demand = proj_matrices.get_matrix("demand.omx", subset=["matrix"])
# Copy the named matrices into sparse float64 storage. In practise these would
# be stored in the sparse OMX extension rather than as a dense matrix, then
# converted to a sparse format.
demand = from_dict(
{name: csr_array(values, dtype="float64") for name, values in demand.items()},
index=demand.index,
)
Let’s perform the traffic assignment
# Create the assignment class
assigclass = TrafficClass(
name="car", mode="c", cost="free_flow_time", demand=demand, skimming=[],
graph_options={"block_centroid_flows": False},
)
# Configure the VDF, network fields, and assignment algorithm.
assig = TrafficAssignment(
network=network,
project=project,
classes=[assigclass],
vdf=bpr,
name_mapping={"alpha": "b", "beta": "power"},
time_field="free_flow_time",
capacity_field="capacity",
algorithm="bfw",
)
assig.execute(max_iterations=10, rgap=0.01)
Equilibrium Assignment : 0%| | 0/10 [00:00<?, ?it/s]
All-or-Nothing - Traffic Class: car : 10%|█ | 1/10 [00:00<00:00, 53773.13it/s]
Equilibrium Assignment - Iteration: 1/10 - RGap: inf: 10%|█ | 1/10 [00:00<00:00, 2410.52it/s]
Equilibrium Assignment : 20%|██ | 2/10 [00:00<00:00, 4034.92it/s]
All-or-Nothing - Traffic Class: car : 20%|██ | 2/10 [00:00<00:00, 3623.59it/s]
Equilibrium Assignment - Iteration: 2/10 - RGap: 0.897805: 20%|██ | 2/10 [00:00<00:00, 1784.43it/s]
Equilibrium Assignment : 30%|███ | 3/10 [00:00<00:00, 2535.85it/s]
All-or-Nothing - Traffic Class: car : 30%|███ | 3/10 [00:00<00:00, 2440.91it/s]
Equilibrium Assignment - Iteration: 3/10 - RGap: 0.527933: 30%|███ | 3/10 [00:00<00:00, 1627.80it/s]
Equilibrium Assignment : 40%|████ | 4/10 [00:00<00:00, 2097.68it/s]
All-or-Nothing - Traffic Class: car : 40%|████ | 4/10 [00:00<00:00, 2051.00it/s]
Equilibrium Assignment - Iteration: 4/10 - RGap: 0.288744: 40%|████ | 4/10 [00:00<00:00, 1478.17it/s]
Equilibrium Assignment : 50%|█████ | 5/10 [00:00<00:00, 1803.69it/s]
All-or-Nothing - Traffic Class: car : 50%|█████ | 5/10 [00:00<00:00, 1774.69it/s]
Equilibrium Assignment - Iteration: 5/10 - RGap: 0.196074: 50%|█████ | 5/10 [00:00<00:00, 1380.34it/s]
Equilibrium Assignment : 60%|██████ | 6/10 [00:00<00:00, 1623.39it/s]
All-or-Nothing - Traffic Class: car : 60%|██████ | 6/10 [00:00<00:00, 1602.20it/s]
Equilibrium Assignment - Iteration: 6/10 - RGap: 0.223256: 60%|██████ | 6/10 [00:00<00:00, 1316.62it/s]
Equilibrium Assignment : 70%|███████ | 7/10 [00:00<00:00, 1510.53it/s]
All-or-Nothing - Traffic Class: car : 70%|███████ | 7/10 [00:00<00:00, 1493.24it/s]
Equilibrium Assignment - Iteration: 7/10 - RGap: 0.171572: 70%|███████ | 7/10 [00:00<00:00, 1319.50it/s]
Equilibrium Assignment : 80%|████████ | 8/10 [00:00<00:00, 1486.88it/s]
All-or-Nothing - Traffic Class: car : 80%|████████ | 8/10 [00:00<00:00, 1469.30it/s]
Equilibrium Assignment - Iteration: 8/10 - RGap: 0.0844232: 80%|████████ | 8/10 [00:00<00:00, 1324.90it/s]
Equilibrium Assignment : 90%|█████████ | 9/10 [00:00<00:00, 1471.46it/s]
All-or-Nothing - Traffic Class: car : 90%|█████████ | 9/10 [00:00<00:00, 1458.38it/s]
Equilibrium Assignment - Iteration: 9/10 - RGap: 0.0782634: 90%|█████████ | 9/10 [00:00<00:00, 1288.35it/s]
Equilibrium Assignment : 100%|██████████| 10/10 [00:00<00:00, 1415.75it/s]
All-or-Nothing - Traffic Class: car : 100%|██████████| 10/10 [00:00<00:00, 1404.89it/s]
After finishing the assignment, we can skim the last iteration
skims = assig.skim_congested(["distance"], return_matrices=True)
# Skims are returned as a dictionary, with the class names as keys
# Let's see all skims we have inside it:
print(skims["car"].field_names)
('distance', '__congested_time__', '__assignment_cost__')
We can save the skims, but we need to choose to only save the final ones, as the blended were not generated
assig.save_skims("base_year_assignment_skims", which_ones="final", format="omx")
['base_year_assignment_skims_car.omx']
Close the project
Total running time of the script: (0 minutes 0.168 seconds)