.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/traffic_assignment/plot_sparse_matrix_assignment.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__auto_examples_traffic_assignment_plot_sparse_matrix_assignment.py: .. _example_assign_sparse: 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. .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. admonition:: Technical references * :doc:`../../traffic_assignment/assignment_procedures` .. GENERATED FROM PYTHON SOURCE LINES 21-27 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.routing.graph` * :func:`aequilibrae.traffic.TrafficClass` * :func:`aequilibrae.traffic.TrafficAssignment` .. GENERATED FROM PYTHON SOURCE LINES 29-44 .. code-block:: Python # 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 .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python # We create the example project inside our temp folder fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr) .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python # We'll also apply a basic logging configuration. basic_config() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 59-61 Traffic assignment ------------------ .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python network = project.road.build_network() .. GENERATED FROM PYTHON SOURCE LINES 66-67 Let's get the demand matrix directly from the project record, and inspect what matrices we have in the project. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python proj_matrices = project.matrices proj_matrices.get_available_matrices_and_metadata() .. rst-class:: sphx-glr-script-out .. code-block:: none {'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}} .. GENERATED FROM PYTHON SOURCE LINES 71-72 We get the demand matrix, and prepare it for computation .. GENERATED FROM PYTHON SOURCE LINES 72-82 .. code-block:: Python 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, ) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Let's perform the traffic assignment .. GENERATED FROM PYTHON SOURCE LINES 84-104 .. code-block:: Python # 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) .. rst-class:: sphx-glr-script-out .. code-block:: none Equilibrium Assignment : 0%| | 0/10 [00:00
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



.. GENERATED FROM PYTHON SOURCE LINES 105-106 After finishing the assignment, we can skim the last iteration .. GENERATED FROM PYTHON SOURCE LINES 106-112 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none ('distance', '__congested_time__', '__assignment_cost__') .. GENERATED FROM PYTHON SOURCE LINES 113-114 We can save the skims, but we need to choose to only save the final ones, as the blended were not generated .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python assig.save_skims("base_year_assignment_skims", which_ones="final", format="omx") .. rst-class:: sphx-glr-script-out .. code-block:: none ['base_year_assignment_skims_car.omx'] .. GENERATED FROM PYTHON SOURCE LINES 117-118 Close the project .. GENERATED FROM PYTHON SOURCE LINES 118-119 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.191 seconds) .. _sphx_glr_download__auto_examples_traffic_assignment_plot_sparse_matrix_assignment.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sparse_matrix_assignment.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sparse_matrix_assignment.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sparse_matrix_assignment.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_