.. 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_turn_restrictions.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_turn_restrictions.py: .. _plot_assignment_turn_restrictions: Traffic assignment with turn penalty and prohibition ==================================================== In this example, we run static traffic assignment for Sioux Falls in two scenarios: 1. Base case (no custom turn restrictions) 2. Custom turn controls with: - a turn penalty from link 7 to link 36 - a turn prohibition from link 24 to link 19 All turn directions are 1 and node-based U-turn transitions are allowed. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. admonition:: References * :doc:`../../static_traffic_assignment` .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.routing.Network.build_graph` * :func:`aequilibrae.traffic.TrafficClass` * :func:`aequilibrae.traffic.TrafficAssignment` .. GENERATED FROM PYTHON SOURCE LINES 31-32 Imports .. GENERATED FROM PYTHON SOURCE LINES 32-70 .. code-block:: Python from dataclasses import replace from os.path import join from tempfile import gettempdir from uuid import uuid4 import matplotlib.pyplot as plt import numpy as np import pandas as pd from aequilibrae.traffic import TrafficAssignment from aequilibrae.traffic import TrafficClass from aequilibrae.traffic.vdf import bpr from aequilibrae.examples import create_example def run_assignment(network, matrix): """Runs a single-class traffic assignment and returns link flow results.""" traffic_class = TrafficClass( name="car", mode="c", cost="free_flow_time", demand=matrix, graph_options={"block_centroid_flows": False}, ) assignment = TrafficAssignment( network=network, classes=[traffic_class], time_field="free_flow_time", capacity_field="capacity", vdf=bpr, name_mapping={"alpha": "b", "beta": "power"}, algorithm="bfw", ) assignment.execute(max_iterations=100, rgap=1e-2) return assignment.results() .. GENERATED FROM PYTHON SOURCE LINES 71-72 We create the Sioux Falls example project inside our temp folder. .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python fldr = join(gettempdir(), uuid4().hex) project = create_example(fldr, "sioux_falls") .. GENERATED FROM PYTHON SOURCE LINES 76-77 Load the network and demand matrix. .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: Python network = replace(project.road.build_network(), allow_uturns=True) matrix = project.matrices.get_matrix("demand.omx", subset=["matrix"]) .. GENERATED FROM PYTHON SOURCE LINES 82-83 Base assignment. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python base_flows = run_assignment(network, matrix) .. rst-class:: sphx-glr-script-out .. code-block:: none Equilibrium Assignment : 0%| | 0/100 [00:00= 0].plot( ax=axes[2], linewidth=3 * np.abs(plot_df.loc[plot_df["difference"] >= 0, "difference"]) / max_diff, color="tab:red", ) plot_df.loc[plot_df["difference"] < 0].plot( ax=axes[2], linewidth=3 * np.abs(plot_df.loc[plot_df["difference"] < 0, "difference"]) / max_diff, color="tab:purple", ) axes[2].set_title("Flow difference (restricted - base)") axes[2].set_axis_off() plt.tight_layout() .. image-sg:: /_auto_examples/traffic_assignment/images/sphx_glr_plot_assignment_turn_restrictions_001.png :alt: Base assignment flow, With turn penalty/prohibition, Flow difference (restricted - base) :srcset: /_auto_examples/traffic_assignment/images/sphx_glr_plot_assignment_turn_restrictions_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 162-163 Close the project. .. GENERATED FROM PYTHON SOURCE LINES 163-164 .. code-block:: Python project.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.703 seconds) .. _sphx_glr_download__auto_examples_traffic_assignment_plot_assignment_turn_restrictions.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_turn_restrictions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_assignment_turn_restrictions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_assignment_turn_restrictions.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_