.. 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_set.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_set.py: .. _example_usage_route_choice_generation: Route Choice set generation =========================== In this example, we show how to generate route choice sets for estimation of route choice models, using a 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-20 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.paths.RouteChoice` .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python # Imports from uuid import uuid4 from tempfile import gettempdir from os.path import join import folium import numpy as np from aequilibrae.utils.create_example import create_example .. GENERATED FROM PYTHON SOURCE LINES 35-41 .. code-block:: Python # We create the example project inside our temp folder fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "coquimbo") .. GENERATED FROM PYTHON SOURCE LINES 42-45 Model parameters ---------------- Let's select a set of nodes of interest .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python od_pairs_of_interest = [(71645, 79385), (77011, 74089)] nodes_of_interest = (71645, 74089, 77011, 79385) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Let's build all graphs .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. 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. .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:248: UserWarning: Found centroids not present in the graph! [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133] warnings.warn("Found centroids not present in the graph!\n" + str(centroids[~present_centroids])) /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:248: UserWarning: Found centroids not present in the graph! [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133] warnings.warn("Found centroids not present in the graph!\n" + str(centroids[~present_centroids])) .. GENERATED FROM PYTHON SOURCE LINES 55-56 We grab the graph for cars .. GENERATED FROM PYTHON SOURCE LINES 56-66 .. code-block:: Python graph = project.network.graphs["c"] # we also see what graphs are available project.network.graphs.keys() graph.set_graph("distance") # We set the nodes of interest as centroids to make sure they are not simplified away when we create the network graph.prepare_graph(np.array(nodes_of_interest)) .. GENERATED FROM PYTHON SOURCE LINES 67-70 Route Choice class ------------------ Here we'll construct and use the Route Choice class to generate our route sets .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: Python from aequilibrae.paths import RouteChoice .. GENERATED FROM PYTHON SOURCE LINES 73-75 This object construct might take a minute depending on the size of the graph due to the construction of the compressed link to network link mapping that's required. This is a one time operation per graph and is cached. .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python rc = RouteChoice(graph) .. GENERATED FROM PYTHON SOURCE LINES 78-82 It is highly recommended to set either ``max_routes`` or ``max_depth`` to prevent runaway results. We'll also set a 5% penalty (``penalty=1.05``), which is likely a little too large, but it creates routes that are distinct enough to make this simple example more interesting. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: Python rc.set_choice_set_generation("bfsle", max_routes=5, penalty=1.05) rc.prepare(od_pairs_of_interest) rc.execute(perform_assignment=True) choice_set = rc.get_results() .. GENERATED FROM PYTHON SOURCE LINES 89-90 If we were interested in storing the route choice result, we could also write them to disk using the ``save_path_files`` method. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python # rc.save_path_files(path) .. GENERATED FROM PYTHON SOURCE LINES 94-95 From those path files we could also preform a full assignment or select link analysis by using the ``execute_from_path_files`` method. .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: Python # rc.execute_from_path_files(path) .. GENERATED FROM PYTHON SOURCE LINES 99-100 Or if we had externally computed route choice sets, we can use AequilibraEs assignment procedures by loading them with the ``execute_from_pandas` method. .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: Python # rc.execute_from_pandas(path_files_df) .. GENERATED FROM PYTHON SOURCE LINES 104-106 Plotting choice sets -------------------- .. GENERATED FROM PYTHON SOURCE LINES 108-109 Now we will plot the paths we just created for the second OD pair .. GENERATED FROM PYTHON SOURCE LINES 109-125 .. code-block:: Python # We get the data we will use for the plot: links, nodes and the route choice set plot_routes = choice_set[(choice_set["origin id"] == 77011)]["route set"].values links = project.network.links.data # For ease of plot, we create a GeoDataFrame for each route in the choice set route_1 = links[links.link_id.isin(plot_routes[0])] route_2 = links[links.link_id.isin(plot_routes[1])] route_3 = links[links.link_id.isin(plot_routes[2])] route_4 = links[links.link_id.isin(plot_routes[3])] route_5 = links[links.link_id.isin(plot_routes[4])] nodes = project.network.nodes.data nodes = nodes[nodes["node_id"].isin([77011, 74089])] .. GENERATED FROM PYTHON SOURCE LINES 126-137 .. code-block:: Python map = route_1.explore(color="red", style_kwds={"weight": 3}, name="route_1") map = route_2.explore(m=map, color="blue", style_kwds={"weight": 3}, name="route_2") map = route_3.explore(m=map, color="green", style_kwds={"weight": 3}, name="route_3") map = route_4.explore(m=map, color="purple", style_kwds={"weight": 3}, name="route_4") map = route_5.explore(m=map, color="orange", style_kwds={"weight": 3}, name="route_5") map = nodes.explore(m=map, color="black", style_kwds={"radius": 5, "fillOpacity": 1.0}, name="network_nodes") folium.LayerControl().add_to(map) map .. raw:: html
Make this Notebook Trusted to load map: File -> Trust Notebook


.. GENERATED FROM PYTHON SOURCE LINES 138-139 .. code-block:: Python project.close() .. rst-class:: sphx-glr-script-out .. code-block:: none This project at /tmp/3c4b770e1fbc45b8aef85de426e95538 is already closed .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.981 seconds) .. _sphx_glr_download__auto_examples_route_choice_plot_route_choice_set.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_set.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_route_choice_set.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_route_choice_set.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_