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:

# 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]
matrix_ab matrix_ba matrix_tot Preload_AB Preload_BA Preload_tot Congested_Time_AB Congested_Time_BA Congested_Time_Max Delay_factor_AB Delay_factor_BA Delay_factor_Max VOC_AB VOC_BA VOC_max PCE_AB PCE_BA PCE_tot
link_id
1 5101.950972 0.0 5101.950972 0.0 0.0 0.0 6.001355 0.0 6.001355 1.000226 0.0 1.000226 0.196985 0.0 0.196985 5101.950972 0.0 5101.950972
2 8372.558485 0.0 8372.558485 0.0 0.0 0.0 4.009828 0.0 4.009828 1.002457 0.0 1.002457 0.357749 0.0 0.357749 8372.558485 0.0 8372.558485
3 5089.042856 0.0 5089.042856 0.0 0.0 0.0 6.001341 0.0 6.001341 1.000224 0.0 1.000224 0.196487 0.0 0.196487 5089.042856 0.0 5089.042856
4 5863.521636 0.0 5863.521636 0.0 0.0 0.0 6.466918 0.0 6.466918 1.293384 0.0 1.293384 1.182595 0.0 1.182595 5863.521636 0.0 5863.521636
5 8385.466601 0.0 8385.466601 0.0 0.0 0.0 4.009889 0.0 4.009889 1.002472 0.0 1.002472 0.358300 0.0 0.358300 8385.466601 0.0 8385.466601
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
72 8096.278487 0.0 8096.278487 0.0 0.0 0.0 8.124896 0.0 8.124896 2.031224 0.0 2.031224 1.619256 0.0 1.619256 8096.278487 0.0 8096.278487
73 8287.698073 0.0 8287.698073 0.0 0.0 0.0 4.127709 0.0 4.127709 2.063854 0.0 2.063854 1.631916 0.0 1.631916 8287.698073 0.0 8287.698073
74 9832.664655 0.0 9832.664655 0.0 0.0 0.0 12.347114 0.0 12.347114 3.086779 0.0 3.086779 1.931285 0.0 1.931285 9832.664655 0.0 9832.664655
75 9966.810409 0.0 9966.810409 0.0 0.0 0.0 10.795642 0.0 10.795642 3.598547 0.0 3.598547 2.040139 0.0 2.040139 9966.810409 0.0 9966.810409
76 7049.364159 0.0 7049.364159 0.0 0.0 0.0 3.113721 0.0 3.113721 1.556860 0.0 1.556860 1.388078 0.0 1.388078 7049.364159 0.0 7049.364159

76 rows × 18 columns



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)

Gallery generated by Sphinx-Gallery