.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/traffic_assignment/plot_assignment_without_model.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_assignment_without_model.py: .. _plot_assignment_without_model: Traffic Assignment without an AequilibraE Model =============================================== In this example, we show how to perform Traffic Assignment in AequilibraE without a model. We are using `Sioux Falls data `_, from TNTP. .. GENERATED FROM PYTHON SOURCE LINES 12-15 .. admonition:: References * :doc:`../../static_traffic_assignment` .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.routing.graph` * :func:`aequilibrae.traffic.TrafficClass` * :func:`aequilibrae.traffic.TrafficAssignment` * :func:`aequilibrae.matrix.aequilibrae_matrix` .. GENERATED FROM PYTHON SOURCE LINES 26-40 .. code-block:: Python # Imports from aequilibrae.traffic.vdf import bpr import os import pandas as pd import numpy as np from uuid import uuid4 from tempfile import gettempdir from aequilibrae.matrix import AequilibraeMatrix from aequilibrae.routing import Network from aequilibrae.traffic import TrafficAssignment from aequilibrae.traffic import TrafficClass .. GENERATED FROM PYTHON SOURCE LINES 42-43 We load the example file from the GMNS GitHub repository .. GENERATED FROM PYTHON SOURCE LINES 43-49 .. code-block:: Python net_file = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/master/SiouxFalls/SiouxFalls_net.tntp" demand_file = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/master/SiouxFalls/CSV-data/SiouxFalls_od.csv" geometry_file = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/master/SiouxFalls/SiouxFalls_node.tntp" .. GENERATED FROM PYTHON SOURCE LINES 50-51 Let's use a temporary folder to store our data .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python folder = os.path.join(gettempdir(), uuid4().hex) .. GENERATED FROM PYTHON SOURCE LINES 54-57 First we load our demand file. This file has three columns: O, D, and Ton. O and D stand for origin and destination, respectively, and Ton is the demand of each OD pair. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python dem = pd.read_csv(demand_file) zones = int(max(dem.O.max(), dem.D.max())) index = np.arange(zones) + 1 .. GENERATED FROM PYTHON SOURCE LINES 62-64 Since our OD-matrix is in a different shape than we expect (for Sioux Falls, that would be a 24x24 matrix), we must create our matrix. .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python mtx = np.zeros(shape=(zones, zones)) for element in dem.to_records(index=False): mtx[element[0]-1][element[1]-1] = element[2] .. GENERATED FROM PYTHON SOURCE LINES 69-70 Now let's create an in-memory AequilibraE matrix with our data. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python aem = AequilibraeMatrix(index=index, index_name="zones") aem["matrix"] = mtx .. GENERATED FROM PYTHON SOURCE LINES 74-76 Let's import information about our network. As we're loading data in TNTP format, we should do these manipulations. .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: Python net = pd.read_csv(net_file, skiprows=2, sep="\t", lineterminator=";", header=None) net.columns = ["newline", "a_node", "b_node", "capacity", "length", "free_flow_time", "b", "power", "speed", "toll", "link_type", "terminator"] net.drop(columns=["newline", "terminator"], index=[76], inplace=True) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/docs/source/examples/traffic_assignment/plot_assignment_without_model.py:80: Pandas4Warning: The inplace keyword in DataFrame.drop is deprecated and will be removed in a future version (PDEP-8). See https://pandas.pydata.org/docs/dev/whatsnew/v3.1.0.html#deprecation-inplace for more details. net.drop(columns=["newline", "terminator"], index=[76], inplace=True) .. GENERATED FROM PYTHON SOURCE LINES 83-88 .. code-block:: Python network = net[['a_node', 'b_node', "capacity", 'free_flow_time', "b", "power"]] network = network.assign(direction=1) network["link_id"] = network.index + 1 network = network.astype({"a_node":"int64", "b_node": "int64"}) .. GENERATED FROM PYTHON SOURCE LINES 89-92 Now we'll import the geometry (as lon/lat) for our network. For Haversine `A*` path finding, pass these coordinates explicitly to the search and scale estimator. Euclidean `A*` requires coordinates projected to a suitable local CRS instead. Geometry can otherwise be skipped. .. GENERATED FROM PYTHON SOURCE LINES 92-98 .. code-block:: Python geom = pd.read_csv(geometry_file, skiprows=1, sep="\t", lineterminator=";", header=None) geom.columns = ["newline", "lon", "lat", "terminator"] geom.drop(columns=["newline", "terminator"], index=[24], inplace=True) geom["node_id"] = geom.index + 1 geom = geom.astype({"node_id": "int64", "lon": "float64", "lat": "float64"}).set_index("node_id") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/docs/source/examples/traffic_assignment/plot_assignment_without_model.py:94: Pandas4Warning: The inplace keyword in DataFrame.drop is deprecated and will be removed in a future version (PDEP-8). See https://pandas.pydata.org/docs/dev/whatsnew/v3.1.0.html#deprecation-inplace for more details. geom.drop(columns=["newline", "terminator"], index=[24], inplace=True) .. GENERATED FROM PYTHON SOURCE LINES 99-101 Create the network snapshot from the link table. See :ref:`aequilibrae-graphs` for more information about graphs. .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: Python network = Network(network.assign(modes="c"), centroids=index, lonlat=geom) .. GENERATED FROM PYTHON SOURCE LINES 106-107 The named matrix is ready to use in the assignment. .. GENERATED FROM PYTHON SOURCE LINES 109-111 Let's perform our assignment. Feel free to try different algorithms, as well as change the maximum number of iterations and the gap .. GENERATED FROM PYTHON SOURCE LINES 111-126 .. code-block:: Python assigclass = TrafficClass( name="car", mode="c", cost="free_flow_time", demand=aem, skimming=["free_flow_time"], graph_options={"block_centroid_flows": False}, ) assig = TrafficAssignment( network=network, classes=[assigclass], vdf=bpr, name_mapping={"alpha": "b", "beta": "power"}, time_field="free_flow_time", capacity_field="capacity", algorithm="fw", ) assig.execute(max_iterations=100, rgap=1e-6) .. rst-class:: sphx-glr-script-out .. code-block:: none Equilibrium Assignment : 0%| | 0/100 [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 4488.258130 0.0 4488.258130 0.0 0.0 0.0 6.000812 0.0 6.000812 1.000135 0.0 1.000135 0.173290 0.0 0.173290 4488.258130 0.0 4488.258130
2 8124.714052 0.0 8124.714052 0.0 0.0 0.0 4.008715 0.0 4.008715 1.002179 0.0 1.002179 0.347158 0.0 0.347158 8124.714052 0.0 8124.714052
3 4534.663654 0.0 4534.663654 0.0 0.0 0.0 6.000846 0.0 6.000846 1.000141 0.0 1.000141 0.175082 0.0 0.175082 4534.663654 0.0 4534.663654
4 5957.027647 0.0 5957.027647 0.0 0.0 0.0 6.562753 0.0 6.562753 1.312551 0.0 1.312551 1.201454 0.0 1.201454 5957.027647 0.0 5957.027647
5 8078.308528 0.0 8078.308528 0.0 0.0 0.0 4.008517 0.0 4.008517 1.002129 0.0 1.002129 0.345176 0.0 0.345176 8078.308528 0.0 8078.308528
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
72 9592.510714 0.0 9592.510714 0.0 0.0 0.0 12.128313 0.0 12.128313 3.032078 0.0 3.032078 1.918502 0.0 1.918502 9592.510714 0.0 9592.510714
73 7917.186129 0.0 7917.186129 0.0 0.0 0.0 3.771985 0.0 3.771985 1.885992 0.0 1.885992 1.558959 0.0 1.558959 7917.186129 0.0 7917.186129
74 11103.650063 0.0 11103.650063 0.0 0.0 0.0 17.574209 0.0 17.574209 4.393552 0.0 4.393552 2.180925 0.0 2.180925 11103.650063 0.0 11103.650063
75 10184.740456 0.0 10184.740456 0.0 0.0 0.0 11.500157 0.0 11.500157 3.833386 0.0 3.833386 2.084748 0.0 2.084748 10184.740456 0.0 10184.740456
76 7892.807597 0.0 7892.807597 0.0 0.0 0.0 3.750260 0.0 3.750260 1.875130 0.0 1.875130 1.554159 0.0 1.554159 7892.807597 0.0 7892.807597

76 rows × 18 columns



.. GENERATED FROM PYTHON SOURCE LINES 127-128 Now let's take a look at the Assignment results .. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: Python assig.results() .. raw:: html
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 4488.258130 0.0 4488.258130 0.0 0.0 0.0 6.000812 0.0 6.000812 1.000135 0.0 1.000135 0.173290 0.0 0.173290 4488.258130 0.0 4488.258130
2 8124.714052 0.0 8124.714052 0.0 0.0 0.0 4.008715 0.0 4.008715 1.002179 0.0 1.002179 0.347158 0.0 0.347158 8124.714052 0.0 8124.714052
3 4534.663654 0.0 4534.663654 0.0 0.0 0.0 6.000846 0.0 6.000846 1.000141 0.0 1.000141 0.175082 0.0 0.175082 4534.663654 0.0 4534.663654
4 5957.027647 0.0 5957.027647 0.0 0.0 0.0 6.562753 0.0 6.562753 1.312551 0.0 1.312551 1.201454 0.0 1.201454 5957.027647 0.0 5957.027647
5 8078.308528 0.0 8078.308528 0.0 0.0 0.0 4.008517 0.0 4.008517 1.002129 0.0 1.002129 0.345176 0.0 0.345176 8078.308528 0.0 8078.308528
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
72 9592.510714 0.0 9592.510714 0.0 0.0 0.0 12.128313 0.0 12.128313 3.032078 0.0 3.032078 1.918502 0.0 1.918502 9592.510714 0.0 9592.510714
73 7917.186129 0.0 7917.186129 0.0 0.0 0.0 3.771985 0.0 3.771985 1.885992 0.0 1.885992 1.558959 0.0 1.558959 7917.186129 0.0 7917.186129
74 11103.650063 0.0 11103.650063 0.0 0.0 0.0 17.574209 0.0 17.574209 4.393552 0.0 4.393552 2.180925 0.0 2.180925 11103.650063 0.0 11103.650063
75 10184.740456 0.0 10184.740456 0.0 0.0 0.0 11.500157 0.0 11.500157 3.833386 0.0 3.833386 2.084748 0.0 2.084748 10184.740456 0.0 10184.740456
76 7892.807597 0.0 7892.807597 0.0 0.0 0.0 3.750260 0.0 3.750260 1.875130 0.0 1.875130 1.554159 0.0 1.554159 7892.807597 0.0 7892.807597

76 rows × 18 columns



.. GENERATED FROM PYTHON SOURCE LINES 131-132 And at the Assignment report .. GENERATED FROM PYTHON SOURCE LINES 132-133 .. code-block:: Python assig.report() .. raw:: html
iteration time rgap alpha warnings
0 1 0.001499 inf 1.000000
1 2 0.003168 0.897805 0.328400
2 3 0.004222 0.527933 0.186602
3 4 0.011286 0.288744 0.241148
4 5 0.013270 0.196074 0.262997
... ... ... ... ... ...
95 96 0.095150 0.001422 0.007731
96 97 0.095795 0.001630 0.009985
97 98 0.096390 0.001530 0.018416
98 99 0.097005 0.001905 0.008428
99 100 0.097335 0.001418 NaN

100 rows × 5 columns



.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.348 seconds) .. _sphx_glr_download__auto_examples_traffic_assignment_plot_assignment_without_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_assignment_without_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_assignment_without_model.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_assignment_without_model.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_