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, 33026.02it/s]
Equilibrium Assignment - Iteration: 1/10 - RGap: inf: 10%|█ | 1/10 [00:00<00:00, 1677.05it/s]
Equilibrium Assignment : 20%|██ | 2/10 [00:00<00:00, 2925.92it/s]
All-or-Nothing - Traffic Class: car : 20%|██ | 2/10 [00:00<00:00, 2460.72it/s]
Equilibrium Assignment - Iteration: 2/10 - RGap: 0.897805: 20%|██ | 2/10 [00:00<00:00, 1109.90it/s]
Equilibrium Assignment : 30%|███ | 3/10 [00:00<00:00, 1592.57it/s]
All-or-Nothing - Traffic Class: car : 30%|███ | 3/10 [00:00<00:00, 1535.81it/s]
Equilibrium Assignment - Iteration: 3/10 - RGap: 0.527933: 30%|███ | 3/10 [00:00<00:00, 990.62it/s]
Equilibrium Assignment : 40%|████ | 4/10 [00:00<00:00, 1282.17it/s]
All-or-Nothing - Traffic Class: car : 40%|████ | 4/10 [00:00<00:00, 1249.61it/s]
Equilibrium Assignment - Iteration: 4/10 - RGap: 0.288744: 40%|████ | 4/10 [00:00<00:00, 888.06it/s]
Equilibrium Assignment : 50%|█████ | 5/10 [00:00<00:00, 1089.37it/s]
All-or-Nothing - Traffic Class: car : 50%|█████ | 5/10 [00:00<00:00, 1072.93it/s]
Equilibrium Assignment - Iteration: 5/10 - RGap: 0.196074: 50%|█████ | 5/10 [00:00<00:00, 853.96it/s]
Equilibrium Assignment : 60%|██████ | 6/10 [00:00<00:00, 1008.81it/s]
All-or-Nothing - Traffic Class: car : 60%|██████ | 6/10 [00:00<00:00, 995.96it/s]
Equilibrium Assignment - Iteration: 6/10 - RGap: 0.223256: 60%|██████ | 6/10 [00:00<00:00, 818.29it/s]
Equilibrium Assignment : 70%|███████ | 7/10 [00:00<00:00, 942.30it/s]
All-or-Nothing - Traffic Class: car : 70%|███████ | 7/10 [00:00<00:00, 932.93it/s]
Equilibrium Assignment - Iteration: 7/10 - RGap: 0.171572: 70%|███████ | 7/10 [00:00<00:00, 825.07it/s]
Equilibrium Assignment : 80%|████████ | 8/10 [00:00<00:00, 933.03it/s]
All-or-Nothing - Traffic Class: car : 80%|████████ | 8/10 [00:00<00:00, 924.29it/s]
Equilibrium Assignment - Iteration: 8/10 - RGap: 0.0844232: 80%|████████ | 8/10 [00:00<00:00, 829.55it/s]
Equilibrium Assignment : 90%|█████████ | 9/10 [00:00<00:00, 924.29it/s]
All-or-Nothing - Traffic Class: car : 90%|█████████ | 9/10 [00:00<00:00, 916.81it/s]
Equilibrium Assignment - Iteration: 9/10 - RGap: 0.0782634: 90%|█████████ | 9/10 [00:00<00:00, 805.38it/s]
Equilibrium Assignment : 100%|██████████| 10/10 [00:00<00:00, 887.42it/s]
All-or-Nothing - Traffic Class: car : 100%|██████████| 10/10 [00:00<00:00, 879.25it/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.191 seconds)