.. 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.paths.Graph` * :func:`aequilibrae.paths.TrafficClass` * :func:`aequilibrae.paths.TrafficAssignment` * :func:`aequilibrae.matrix.AequilibraeMatrix` .. GENERATED FROM PYTHON SOURCE LINES 26-39 .. code-block:: Python # Imports 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.paths import Graph from aequilibrae.paths import TrafficAssignment from aequilibrae.paths.traffic_class import TrafficClass .. GENERATED FROM PYTHON SOURCE LINES 41-42 We load the example file from the GMNS GitHub repository .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. 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 49-50 Let's use a temporary folder to store our data .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python folder = os.path.join(gettempdir(), uuid4().hex) .. GENERATED FROM PYTHON SOURCE LINES 53-56 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 56-60 .. 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 61-63 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 63-67 .. 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 68-69 Now let's create an AequilibraE Matrix with out data .. GENERATED FROM PYTHON SOURCE LINES 69-79 .. code-block:: Python aemfile = os.path.join(folder, "demand.aem") aem = AequilibraeMatrix() kwargs = {'file_name': aemfile, 'zones': zones, 'matrix_names': ['matrix']} aem.create_empty(**kwargs) aem.matrix['matrix'][:,:] = mtx[:,:] aem.index[:] = index[:] .. GENERATED FROM PYTHON SOURCE LINES 80-82 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 82-88 .. 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) .. GENERATED FROM PYTHON SOURCE LINES 89-94 .. 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 95-97 Now we'll import the geometry (as lon/lat) for our network, this is required if you plan to use the `A*` path finding, otherwise it can safely be skipped. .. GENERATED FROM PYTHON SOURCE LINES 97-103 .. 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") .. GENERATED FROM PYTHON SOURCE LINES 104-106 Let's build our Graph! In case you're in doubt about AequilibraE Graph, :ref:`click here ` to read more about it. .. GENERATED FROM PYTHON SOURCE LINES 108-122 .. code-block:: Python g = Graph() g.cost = network['free_flow_time'].values g.capacity = network['capacity'].values g.free_flow_time = network['free_flow_time'].values g.network = network g.prepare_graph(index) g.set_graph("free_flow_time") g.cost = np.array(g.cost, copy=True) g.set_skimming(["free_flow_time"]) g.set_blocked_centroid_flows(False) g.network["id"] = g.network.link_id g.lonlat_index = geom.loc[g.all_nodes] .. GENERATED FROM PYTHON SOURCE LINES 123-124 Let's prepare our matrix for computation .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python aem.computational_view(["matrix"]) .. GENERATED FROM PYTHON SOURCE LINES 127-129 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 129-143 .. code-block:: Python assigclass = TrafficClass("car", g, aem) assig = TrafficAssignment() assig.set_classes([assigclass]) assig.set_vdf("BPR") assig.set_vdf_parameters({"alpha": "b", "beta": "power"}) assig.set_capacity_field("capacity") assig.set_time_field("free_flow_time") assig.set_algorithm("fw") assig.max_iter = 100 assig.rgap_target = 1e-6 assig.execute() .. rst-class:: sphx-glr-script-out .. code-block:: none car : 0%| | 0/24 [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 4477.023480 0.0 4477.023480 0.0 0.0 0.0 6.000804 0.0 6.000804 1.000134 0.0 1.000134 0.172857 0.0 0.172857 4477.023480 0.0 4477.023480
2 8113.018303 0.0 8113.018303 0.0 0.0 0.0 4.008665 0.0 4.008665 1.002166 0.0 1.002166 0.346659 0.0 0.346659 8113.018303 0.0 8113.018303
3 4522.842064 0.0 4522.842064 0.0 0.0 0.0 6.000837 0.0 6.000837 1.000139 0.0 1.000139 0.174626 0.0 0.174626 4522.842064 0.0 4522.842064
4 5949.982375 0.0 5949.982375 0.0 0.0 0.0 6.555373 0.0 6.555373 1.311075 0.0 1.311075 1.200033 0.0 1.200033 5949.982375 0.0 5949.982375
5 8067.199720 0.0 8067.199720 0.0 0.0 0.0 4.008471 0.0 4.008471 1.002118 0.0 1.002118 0.344701 0.0 0.344701 8067.199720 0.0 8067.199720
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
72 9602.723810 0.0 9602.723810 0.0 0.0 0.0 12.162985 0.0 12.162985 3.040746 0.0 3.040746 1.920545 0.0 1.920545 9602.723810 0.0 9602.723810
73 7889.143252 0.0 7889.143252 0.0 0.0 0.0 3.747012 0.0 3.747012 1.873506 0.0 1.873506 1.553437 0.0 1.553437 7889.143252 0.0 7889.143252
74 11104.868697 0.0 11104.868697 0.0 0.0 0.0 17.580169 0.0 17.580169 4.395042 0.0 4.395042 2.181165 0.0 2.181165 11104.868697 0.0 11104.868697
75 10214.023750 0.0 10214.023750 0.0 0.0 0.0 11.598339 0.0 11.598339 3.866113 0.0 3.866113 2.090742 0.0 2.090742 10214.023750 0.0 10214.023750
76 7857.484295 0.0 7857.484295 0.0 0.0 0.0 3.719138 0.0 3.719138 1.859569 0.0 1.859569 1.547203 0.0 1.547203 7857.484295 0.0 7857.484295

76 rows × 18 columns



.. GENERATED FROM PYTHON SOURCE LINES 148-149 And at the Assignment report .. GENERATED FROM PYTHON SOURCE LINES 149-150 .. code-block:: Python assig.report() .. raw:: html
iteration rgap alpha warnings
0 1 inf 1.000000
1 2 0.855075 0.328400
2 3 0.476346 0.186602
3 4 0.235513 0.241148
4 5 0.152364 0.262997
... ... ... ... ...
95 96 0.001411 0.007731
96 97 0.001614 0.009985
97 98 0.001502 0.018416
98 99 0.001889 0.008428
99 100 0.001400 0.012648

100 rows × 4 columns



.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.792 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 `_