Note
Go to the end to download the full example code.
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.
References
See also
Several functions, methods, classes and modules are used in this example:
# Imports
from uuid import uuid4
from tempfile import gettempdir
from pathlib import Path
from aequilibrae.examples import create_example
from aequilibrae.traffic import TrafficAssignment, TrafficClass
from aequilibrae.traffic.vdf import bpr
# We create the example project inside our temp folder.
fldr = Path(gettempdir()) / uuid4().hex
project = create_example(fldr, "sioux_falls")
Working with scenarios#
Let’s first see what scenarios exist in our project
The root scenario is always present and represents the base model. Let’s examine the current scenario’s network
print(f"Current scenario network has {len(project.tables.links.data)} links")
print(f"Current scenario network has {len(project.tables.nodes.data)} nodes")
Current scenario network has 76 links
Current scenario network has 24 nodes
Creating new scenarios#
We can create empty scenarios or clone existing ones
# 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")
Let’s see our updated scenario list
Switching between scenarios#
Each scenario operates independently with its own data
# Switch to the cloned scenario
project.use_scenario("limited_capacity")
print(f"This scenario has {len(project.tables.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")
This scenario has 76 links
Let’s perform a traffic assignment in this scenario with lowered capacity
# Load the network snapshot
network = project.road.build_network()
# Get the demand matrix
mat = project.matrices.get_matrix("demand.omx", subset=["matrix"])
# Create traffic assignment with alternative parameters
traffic = TrafficClass(
name="car", mode="c", cost="distance", demand=mat, graph_options={"block_centroid_flows": False}
)
assignment = TrafficAssignment(
network=network, classes=[traffic], time_field="distance", capacity_field="capacity",
vdf=bpr, name_mapping={"alpha": 0.15, "beta": 4.0}, algorithm="msa", project=project,
)
assignment.execute(max_iterations=10)
# Save results specific to this scenario
assignment.save_results("alternative_assignment")
print(f"Assignment completed. Total flow: {assignment.class_results['car'].total_link_loads.sum():.2f}")
Equilibrium Assignment : 0%| | 0/10 [00:00<?, ?it/s]
All-or-Nothing - Traffic Class: car : 10%|█ | 1/10 [00:00<00:00, 53773.13it/s]
Equilibrium Assignment - Iteration: 1/10 - RGap: inf: 10%|█ | 1/10 [00:00<00:00, 2264.74it/s]
Equilibrium Assignment : 20%|██ | 2/10 [00:00<00:00, 3975.64it/s]
All-or-Nothing - Traffic Class: car : 20%|██ | 2/10 [00:00<00:00, 3637.73it/s]
Equilibrium Assignment - Iteration: 2/10 - RGap: 0.835622: 20%|██ | 2/10 [00:00<00:00, 2343.84it/s]
Equilibrium Assignment : 30%|███ | 3/10 [00:00<00:00, 3278.51it/s]
All-or-Nothing - Traffic Class: car : 30%|███ | 3/10 [00:00<00:00, 3113.81it/s]
Equilibrium Assignment - Iteration: 3/10 - RGap: 0.961328: 30%|███ | 3/10 [00:00<00:00, 2346.68it/s]
Equilibrium Assignment : 40%|████ | 4/10 [00:00<00:00, 2998.61it/s]
All-or-Nothing - Traffic Class: car : 40%|████ | 4/10 [00:00<00:00, 2907.16it/s]
Equilibrium Assignment - Iteration: 4/10 - RGap: 0.800405: 40%|████ | 4/10 [00:00<00:00, 2460.36it/s]
Equilibrium Assignment : 50%|█████ | 5/10 [00:00<00:00, 2984.84it/s]
All-or-Nothing - Traffic Class: car : 50%|█████ | 5/10 [00:00<00:00, 2917.17it/s]
Equilibrium Assignment - Iteration: 5/10 - RGap: 0.734284: 50%|█████ | 5/10 [00:00<00:00, 2554.70it/s]
Equilibrium Assignment : 60%|██████ | 6/10 [00:00<00:00, 2994.86it/s]
All-or-Nothing - Traffic Class: car : 60%|██████ | 6/10 [00:00<00:00, 2942.34it/s]
Equilibrium Assignment - Iteration: 6/10 - RGap: 0.6008: 60%|██████ | 6/10 [00:00<00:00, 2599.78it/s]
Equilibrium Assignment : 70%|███████ | 7/10 [00:00<00:00, 2961.78it/s]
All-or-Nothing - Traffic Class: car : 70%|███████ | 7/10 [00:00<00:00, 2907.81it/s]
Equilibrium Assignment - Iteration: 7/10 - RGap: 0.456658: 70%|███████ | 7/10 [00:00<00:00, 2624.96it/s]
Equilibrium Assignment : 80%|████████ | 8/10 [00:00<00:00, 2944.66it/s]
All-or-Nothing - Traffic Class: car : 80%|████████ | 8/10 [00:00<00:00, 2904.39it/s]
Equilibrium Assignment - Iteration: 8/10 - RGap: 0.289471: 80%|████████ | 8/10 [00:00<00:00, 2662.21it/s]
Equilibrium Assignment : 90%|█████████ | 9/10 [00:00<00:00, 2948.66it/s]
All-or-Nothing - Traffic Class: car : 90%|█████████ | 9/10 [00:00<00:00, 2913.39it/s]
Equilibrium Assignment - Iteration: 9/10 - RGap: 0.21859: 90%|█████████ | 9/10 [00:00<00:00, 2690.38it/s]
Equilibrium Assignment : 100%|██████████| 10/10 [00:00<00:00, 2944.61it/s]
All-or-Nothing - Traffic Class: car : 100%|██████████| 10/10 [00:00<00:00, 2911.70it/s]
Assignment completed. Total flow: 1160411.11
Switch to empty scenario for modifications
project.use_scenario("test_modifications")
print(f"Empty scenario has {len(project.tables.links.data)} links")
# This scenario starts with an empty network, suitable for building from scratch
# or testing specific network configurations
Empty scenario has 0 links
Scenario isolation demonstration#
Let’s switch back to root and show that scenarios are isolated
project.use_scenario("root")
print(f"Back to root scenario with {len(project.tables.links.data)} links")
# Check results - only root scenario results should be visible
root_results = project.results.list()
print(f"Root scenario has {len(root_results)} result tables")
# Switch to alternative scenario and check its results
project.use_scenario("limited_capacity")
alt_results = project.results.list()
print(f"Alternative scenario has {len(alt_results)} result tables")
# Each scenario maintains its own results database
alternative_assignment_exists = "alternative_assignment" in alt_results["table_name"].values
print(f"Alternative assignment result exists in this scenario: {alternative_assignment_exists}")
Back to root scenario with 76 links
Root scenario has 0 result tables
Alternative scenario has 1 result tables
Alternative assignment result exists in this scenario: True
Best practices for scenario management#
# Always return to root when doing project-wide operations
project.use_scenario("root")
# List scenarios for reference
final_scenarios = project.list_scenarios()
print("\nFinal scenario summary:")
for _, scenario in final_scenarios.iterrows():
project.use_scenario(scenario['scenario_name'])
link_count = len(project.tables.links.data)
result_count = len(project.results.list())
print(f" {scenario['scenario_name']}: {link_count} links, {result_count} results")
print(f" Description: {scenario['description']}")
Final scenario summary:
root: 76 links, 0 results
Description: The default, and root, scenario for an AequilbraE project. The name "root" is treat as special case.
test_modifications: 0 links, 0 results
Description: Scenario for testing network modifications
limited_capacity: 76 links, 1 results
Description: Testing different assignment parameters
Clean up
project.use_scenario("root") # Always end on root scenario
project.close()
Total running time of the script: (0 minutes 0.761 seconds)