.. 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.paths.graph` * :func:`aequilibrae.paths.traffic_class.TrafficClass` * :func:`aequilibrae.paths.traffic_assignment.TrafficAssignment` .. GENERATED FROM PYTHON SOURCE LINES 29-39 .. code-block:: Python # Imports from os.path import join from tempfile import gettempdir from uuid import uuid4 from aequilibrae.utils.create_example import create_example from aequilibrae.paths import TrafficAssignment, TrafficClass .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: Python # We create the example project inside our temp folder fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr) logger = project.logger .. GENERATED FROM PYTHON SOURCE LINES 49-51 Traffic assignment ------------------ .. GENERATED FROM PYTHON SOURCE LINES 53-54 We build all graphs .. GENERATED FROM PYTHON SOURCE LINES 54-68 .. code-block:: Python project.network.build_graphs() # We get warnings that several fields in the project are filled with NaNs. # This is true, but we won't use those fields. # We grab the graph for cars graph = project.network.graphs["c"] # Let's say we want to minimize the free_flow_time graph.set_graph("free_flow_time") # And we will allow paths to be computed going through other centroids/centroid connectors # required for the Sioux Falls network, as all nodes are centroids graph.set_blocked_centroid_flows(False) .. GENERATED FROM PYTHON SOURCE LINES 69-70 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 70-73 .. code-block:: Python proj_matrices = project.matrices proj_matrices.list() .. GENERATED FROM PYTHON SOURCE LINES 74-75 We get the demand matrix, and prepare it for computation .. GENERATED FROM PYTHON SOURCE LINES 75-78 .. code-block:: Python demand = proj_matrices.get_matrix("demand_omx") demand.computational_view(["matrix"]) .. GENERATED FROM PYTHON SOURCE LINES 79-80 Let's perform the traffic assignment .. GENERATED FROM PYTHON SOURCE LINES 80-109 .. code-block:: Python # Create the assignment class assigclass = TrafficClass(name="car", graph=graph, matrix=demand) assig = TrafficAssignment() # We start by adding the list of traffic classes to be assigned assig.add_class(assigclass) # Then we set these parameters, which an only be configured after adding one class to the assignment assig.set_vdf("BPR") # This is not case-sensitive # Then we set the volume delay function and its parameters assig.set_vdf_parameters({"alpha": "b", "beta": "power"}) # The capacity and free flow travel times as they exist in the graph assig.set_capacity_field("capacity") assig.set_time_field("free_flow_time") # And the algorithm we want to use to assign assig.set_algorithm("bfw") # Let's set parameters that make this example run very fast assig.max_iter = 10 assig.rgap_target = 0.01 # we then execute the assignment assig.execute() .. GENERATED FROM PYTHON SOURCE LINES 110-111 After finishing the assignment, we can skim the last iteration .. GENERATED FROM PYTHON SOURCE LINES 111-117 .. 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"].names) .. GENERATED FROM PYTHON SOURCE LINES 118-119 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 119-121 .. code-block:: Python assig.save_skims("base_year_assignment_skims", which_ones="final", format="omx") .. GENERATED FROM PYTHON SOURCE LINES 122-123 Close the project .. GENERATED FROM PYTHON SOURCE LINES 123-124 .. code-block:: Python project.close() .. _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 `_