.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/aequilibrae_project/plot_scenarios.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_aequilibrae_project_plot_scenarios.py: .. _example_usage_scenarios: Project Scenarios ================= In this example, we show how to use AequilibraE's scenario system to manage multiple model variants within a single project, using different example networks to demonstrate scenario isolation and management. .. GENERATED FROM PYTHON SOURCE LINES 11-14 .. admonition:: References * :doc:`../../aequilibrae_project` .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. seealso:: Several functions, methods, classes and modules are used in this example: * :func:`aequilibrae.project.project.Project.list_scenarios` * :func:`aequilibrae.project.project.Project.use_scenario` * :func:`aequilibrae.project.project.Project.create_empty_scenario` * :func:`aequilibrae.project.project.Project.clone_scenario` .. GENERATED FROM PYTHON SOURCE LINES 25-35 .. code-block:: Python # Imports from uuid import uuid4 from tempfile import gettempdir from pathlib import Path from aequilibrae.utils.create_example import create_example from aequilibrae import TrafficAssignment, TrafficClass .. GENERATED FROM PYTHON SOURCE LINES 37-43 .. code-block:: Python # We create the example project inside our temp folder. fldr = Path(gettempdir()) / uuid4().hex project = create_example(fldr, "sioux_falls") .. GENERATED FROM PYTHON SOURCE LINES 44-47 Working with scenarios ---------------------- Let's first see what scenarios exist in our project .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python project.list_scenarios() .. raw:: html
scenario_name description
0 root The default, and root, scenario for an Aequilb...


.. GENERATED FROM PYTHON SOURCE LINES 51-53 The root scenario is always present and represents the base model. Let's examine the current scenario's network .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python print(f"Current scenario network has {len(project.network.links.data)} links") print(f"Current scenario network has {len(project.network.nodes.data)} nodes") .. rst-class:: sphx-glr-script-out .. code-block:: none Current scenario network has 76 links Current scenario network has 24 nodes .. GENERATED FROM PYTHON SOURCE LINES 58-61 Creating new scenarios ---------------------- We can create empty scenarios or clone existing ones .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. code-block:: Python # Create an empty scenario to manually populate with a future/different network project.create_empty_scenario("test_modifications", "Scenario for testing network modifications") # Clone the root scenario to preserve the original network project.clone_scenario("limited_capacity", "Testing different assignment parameters") .. GENERATED FROM PYTHON SOURCE LINES 69-70 Let's see our updated scenario list .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python project.list_scenarios() .. raw:: html
scenario_name description
0 root The default, and root, scenario for an Aequilb...
1 test_modifications Scenario for testing network modifications
2 limited_capacity Testing different assignment parameters


.. GENERATED FROM PYTHON SOURCE LINES 74-77 Switching between scenarios --------------------------- Each scenario operates independently with its own data .. GENERATED FROM PYTHON SOURCE LINES 77-86 .. code-block:: Python # Switch to the cloned scenario project.use_scenario("limited_capacity") print(f"This scenario has {len(project.network.links.data)} links") # Modify the network with project.db_connection as conn: conn.execute("UPDATE links SET capacity_ab=capacity_ab/2, capacity_ba=capacity_ba/2 WHERE link_id > 20 AND link_id < 50") .. rst-class:: sphx-glr-script-out .. code-block:: none This scenario has 76 links .. GENERATED FROM PYTHON SOURCE LINES 87-88 Let's perform a traffic assignment in this scenario with lowered capacity .. GENERATED FROM PYTHON SOURCE LINES 88-118 .. code-block:: Python # Build the network graph project.network.build_graphs(fields=["distance", "capacity_ab", "capacity_ba"], modes=["c"]) graph = project.network.graphs["c"] graph.set_graph("distance") graph.set_blocked_centroid_flows(False) # Get the demand matrix mat = project.matrices.get_matrix("demand_omx") mat.computational_view() # Create traffic assignment with alternative parameters assigclass = TrafficClass("car", graph, mat) assignment = TrafficAssignment(project) assignment.add_class(assigclass) assignment.set_vdf("BPR") assignment.set_vdf_parameters({"alpha": 0.15, "beta": 4.0}) assignment.set_capacity_field("capacity") assignment.set_time_field("distance") assignment.max_iter = 10 assignment.set_algorithm("msa") assignment.execute() # Save results specific to this scenario assignment.save_results("alternative_assignment") print(f"Assignment completed. Total flow: {assigclass.results.total_link_loads.sum():.2f}") .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/aequilibrae/aequilibrae/aequilibrae/paths/graph.py:218: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment. Such chained assignment never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write). Try using '.loc[row_indexer, col_indexer] = value' instead, to perform the assignment in a single step. See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#chained-assignment build_compressed_graph(self, remove_dead_ends) car : 0%| | 0/24 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_scenarios.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_scenarios.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_