.. 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 4532.416460 0.0 4532.416460 0.0 0.0 0.0 6.000844 0.0 6.000844 1.000141 0.0 1.000141 0.174995 0.0 0.174995 4532.416460 0.0 4532.416460
2 8124.962104 0.0 8124.962104 0.0 0.0 0.0 4.008716 0.0 4.008716 1.002179 0.0 1.002179 0.347169 0.0 0.347169 8124.962104 0.0 8124.962104
3 4528.444976 0.0 4528.444976 0.0 0.0 0.0 6.000841 0.0 6.000841 1.000140 0.0 1.000140 0.174842 0.0 0.174842 4528.444976 0.0 4528.444976
4 6001.323525 0.0 6001.323525 0.0 0.0 0.0 6.609756 0.0 6.609756 1.321951 0.0 1.321951 1.210388 0.0 1.210388 6001.323525 0.0 6001.323525
5 8128.933588 0.0 8128.933588 0.0 0.0 0.0 4.008733 0.0 4.008733 1.002183 0.0 1.002183 0.347339 0.0 0.347339 8128.933588 0.0 8128.933588
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
72 9643.868005 0.0 9643.868005 0.0 0.0 0.0 12.303788 0.0 12.303788 3.075947 0.0 3.075947 1.928774 0.0 1.928774 9643.868005 0.0 9643.868005
73 7855.662507 0.0 7855.662507 0.0 0.0 0.0 3.717544 0.0 3.717544 1.858772 0.0 1.858772 1.546844 0.0 1.546844 7855.662507 0.0 7855.662507
74 11101.449987 0.0 11101.449987 0.0 0.0 0.0 17.563454 0.0 17.563454 4.390863 0.0 4.390863 2.180493 0.0 2.180493 11101.449987 0.0 11101.449987
75 10255.489839 0.0 10255.489839 0.0 0.0 0.0 11.738819 0.0 11.738819 3.912940 0.0 3.912940 2.099230 0.0 2.099230 10255.489839 0.0 10255.489839
76 7923.866336 0.0 7923.866336 0.0 0.0 0.0 3.777973 0.0 3.777973 1.888986 0.0 1.888986 1.560274 0.0 1.560274 7923.866336 0.0 7923.866336

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.855131 0.328177
2 3 0.476738 0.186185
3 4 0.239622 0.229268
4 5 0.139851 0.314341
... ... ... ... ...
95 96 0.001999 0.011309
96 97 0.001431 0.006948
97 98 0.001405 0.014356
98 99 0.001814 0.012088
99 100 0.001577 0.007687

100 rows × 4 columns



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