.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/route_choice/plot_route_choice_basics.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_route_choice_plot_route_choice_basics.py: .. _example_usage_route_choice: Route Choice ============ In this example, we show how to perform route choice set generation using BFSLE and Link penalisation, for a city in La Serena Metropolitan Area in Chile. .. GENERATED FROM PYTHON SOURCE LINES 11-14 .. admonition:: References * :doc:`../../route_choice` .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.routing.graph` * :func:`aequilibrae.route_choice.route_choice` * :func:`aequilibrae.matrix.aequilibrae_matrix` .. GENERATED FROM PYTHON SOURCE LINES 24-34 .. code-block:: Python # Imports from uuid import uuid4 from tempfile import gettempdir from os.path import join from aequilibrae.examples import create_example from aequilibrae.utils import basic_config .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python # We create the example project inside our temp folder fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "coquimbo") .. rst-class:: sphx-glr-script-out .. code-block:: none Environment variable 'AEQ_SPATIALITE_DIR' was provided (C:\path\to\existing\download), but mod_spatialite could not be loaded from this directory. Trying system path Environment variable 'AEQ_SPATIALITE_DIR' was provided (C:\path\to\existing\download), but mod_spatialite could not be loaded from this directory. Trying system path .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. 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 49-51 Model parameters ---------------- .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 55-58 We'll set the parameters for our route choice model. These are the parameters that will be used to calculate the utility of each path. In our example, the utility is equal to :math:`distance * theta`, and the path overlap factor (PSL) is equal to :math:`beta`. .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. code-block:: Python # Distance factor theta = 0.00011 # PSL parameter beta = 1.1 .. GENERATED FROM PYTHON SOURCE LINES 66-67 Let's select a set of nodes of interest .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python nodes_of_interest = (71645, 74089, 77011, 79385) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Let's load the source network snapshot .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python network = project.road.build_network() # Several fields in the project are filled with NaNs. # This is true, but we won't use those fields. .. GENERATED FROM PYTHON SOURCE LINES 76-77 We also see what modes are available .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python network.modes .. rst-class:: sphx-glr-script-out .. code-block:: none ('b', 'c', 't', 'w') .. GENERATED FROM PYTHON SOURCE LINES 80-81 Add the utility field to the source network before graph preparation. .. GENERATED FROM PYTHON SOURCE LINES 81-86 .. code-block:: Python network.links["utility"] = network.links.distance * theta graph = network.build_graph( mode="c", cost="utility", centroids=np.array(nodes_of_interest), compact=True ) .. GENERATED FROM PYTHON SOURCE LINES 87-90 Mock demand matrix ------------------ We'll create a mock demand matrix with demand 1 for every zone and prepare it for computation. .. GENERATED FROM PYTHON SOURCE LINES 90-96 .. code-block:: Python from aequilibrae.matrix import AequilibraeMatrix mat = AequilibraeMatrix(index=graph.centroids, index_name="zones") mat["demand"] = np.full((graph.num_zones, graph.num_zones), 10.0) mat["5x demand"] = np.full((graph.num_zones, graph.num_zones), 50.0) .. GENERATED FROM PYTHON SOURCE LINES 97-100 Create plot function -------------------- Before dive into the Route Choice class, let's define a function to plot assignment results. .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python import folium .. GENERATED FROM PYTHON SOURCE LINES 103-124 .. code-block:: Python def plot_results(link_loads): link_loads = link_loads[link_loads["demand_tot"] > 0] max_load = link_loads["demand_tot"].max() links = project.tables.links.data loaded_links = links.merge(link_loads, on="link_id", how="inner") loads_lyr = folium.FeatureGroup("link_loads") # Maximum thickness we would like is probably a 10, so let's make sure we don't go over that factor = 10 / max_load return loaded_links.explore( color="red", style_kwds={ "style_function": lambda x: { "weight": x["properties"]["demand_tot"] * factor, } }, ) .. GENERATED FROM PYTHON SOURCE LINES 125-128 Route Choice class ------------------ Here we'll construct and use the Route Choice class to generate our route sets .. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: Python from aequilibrae.route_choice import RouteChoice .. GENERATED FROM PYTHON SOURCE LINES 131-133 Graph construction has already prepared the compressed-link to network-link mapping. RouteChoice reuses that mapping and snapshots the routing costs. .. GENERATED FROM PYTHON SOURCE LINES 133-138 .. code-block:: Python rc = RouteChoice(graph) # Let's check the default parameters for the Route Choice class print(rc.default_parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'generic': {'seed': 0, 'max_routes': 0, 'max_depth': 0, 'max_misses': 100, 'penalty': 1.01, 'disutility_cutoff_constant': inf, 'disutility_cutoff_coefficient': inf, 'beta': 1.0, 'store_results': True, 'a_star': False, 'heuristic': 'euclidean', 'heuristic_scale': None}, 'link-penalisation': {}, 'bfsle': {'penalty': 1.0}} .. GENERATED FROM PYTHON SOURCE LINES 139-140 Let's add the demand. If it's not provided, link loading cannot be performed. .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: Python rc.set_demand(mat) .. GENERATED FROM PYTHON SOURCE LINES 143-144 It is highly recommended to set either ``max_routes`` or ``max_depth`` to prevent runaway results. .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: Python rc.set_choice_set_generation("bfsle", max_routes=5) .. GENERATED FROM PYTHON SOURCE LINES 147-149 We can now perform a computation for single OD pair if we'd like. Here we do one between the first and last centroid as well as an assignment. .. GENERATED FROM PYTHON SOURCE LINES 149-152 .. code-block:: Python results = rc.execute_single(77011, 74089, demand=1.0) print(results[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none 0/1 ODs processed : 0%| | 0/1 [00:00
origin id destination id cost mask path overlap probability route set route directions
0 77011 74089 1.890631 True 0.317463 0.200271 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
1 77011 74089 1.909910 True 0.320813 0.198520 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
2 77011 74089 1.896079 True 0.288041 0.180723 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
3 77011 74089 1.930091 True 0.396769 0.240617 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
4 77011 74089 1.886882 True 0.284053 0.179868 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...


.. GENERATED FROM PYTHON SOURCE LINES 158-160 .. code-block:: Python plot_results(rc.get_load_results()) .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 161-165 Batch operations ---------------- To perform a batch operation we need to prepare the object first. We can either provide a list of tuple of the OD pairs we'd like to use, or we can provided a 1D list and the generation will be run on all permutations. .. GENERATED FROM PYTHON SOURCE LINES 165-167 .. code-block:: Python rc.prepare() .. GENERATED FROM PYTHON SOURCE LINES 168-169 Now we can perform a batch computation with an assignment .. GENERATED FROM PYTHON SOURCE LINES 169-173 .. code-block:: Python rc.execute(perform_assignment=True) res = rc.get_results() res.head() .. rst-class:: sphx-glr-script-out .. code-block:: none 0/16 ODs processed : 0%| | 0/16 [00:00
origin id destination id cost mask path overlap probability route set route directions
0 71645 74089 0.607017 True 0.382436 0.207157 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...
1 71645 74089 0.606968 True 0.246758 0.133670 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...
2 71645 74089 0.622079 True 0.542334 0.289380 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...
3 71645 74089 0.611944 True 0.442367 0.238443 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, ...
4 71645 74089 0.604384 True 0.241848 0.131349 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...


.. GENERATED FROM PYTHON SOURCE LINES 174-175 Since we provided a matrix initially we can also perform link loading based on our assignment results. .. GENERATED FROM PYTHON SOURCE LINES 175-177 .. code-block:: Python rc.get_load_results() .. raw:: html
demand_ab demand_ba 5x demand_ab 5x demand_ba demand_tot 5x demand_tot
link_id
1 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.0 0.0
12 0.0 0.0 0.0 0.0 0.0 0.0
13 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ...
34938 0.0 0.0 0.0 0.0 0.0 0.0
34939 0.0 0.0 0.0 0.0 0.0 0.0
34940 0.0 0.0 0.0 0.0 0.0 0.0
34941 0.0 0.0 0.0 0.0 0.0 0.0
34942 0.0 0.0 0.0 0.0 0.0 0.0

19983 rows × 6 columns



.. GENERATED FROM PYTHON SOURCE LINES 178-179 We can plot these as well .. GENERATED FROM PYTHON SOURCE LINES 179-181 .. code-block:: Python plot_results(rc.get_load_results()) .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 182-187 Select link analysis -------------------- We can also enable select link analysis by providing the links and the directions that we are interested in. Here we set the select link to trigger when (7369, 1) and (20983, 1) is utilised in "sl1" and "sl2" when (7369, 1) is utilised. .. GENERATED FROM PYTHON SOURCE LINES 187-190 .. code-block:: Python rc.set_select_links({"sl1": [[(7369, 1), (20983, 1)]], "sl2": [[(7369, 1)]]}) rc.execute(perform_assignment=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 0/16 ODs processed : 0%| | 0/16 [00:00
demand_sl1_ab demand_sl1_ba 5x demand_sl1_ab 5x demand_sl1_ba demand_sl2_ab demand_sl2_ba 5x demand_sl2_ab 5x demand_sl2_ba demand_sl1_tot 5x demand_sl1_tot demand_sl2_tot 5x demand_sl2_tot
link_id
1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
12 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
13 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ...
34938 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
34939 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
34940 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
34941 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
34942 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

19983 rows × 12 columns



.. GENERATED FROM PYTHON SOURCE LINES 196-199 We can also access the OD matrices for this link loading. These labelled matrices contain SciPy CSR arrays for ease of use. They're stored in a dictionary keyed by select link set name, with demand fields inside each matrix. .. GENERATED FROM PYTHON SOURCE LINES 199-201 .. code-block:: Python rc.get_select_link_od_matrix_results() .. rst-class:: sphx-glr-script-out .. code-block:: none {'sl1': , 'sl2': } .. GENERATED FROM PYTHON SOURCE LINES 202-205 .. code-block:: Python od_matrix = rc.get_select_link_od_matrix_results()["sl1"]["demand"] od_matrix.toarray() .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0. , 0. , 0. , 3.04610785], [0. , 0. , 0. , 0. ], [0. , 0. , 0. , 2.42146568], [0. , 0. , 0. , 0. ]]) .. GENERATED FROM PYTHON SOURCE LINES 206-207 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.479 seconds) .. _sphx_glr_download__auto_examples_route_choice_plot_route_choice_basics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_route_choice_basics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_route_choice_basics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_route_choice_basics.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_