Traffic Assignment Procedure ============================ Along with a network data model, traffic assignment is the most technically challenging portion to develop in a modelling platform, especially if you want it to be *fast*. In AequilibraE, we aim to make it as fast as possible, without making it overly complex to use, develop and maintain, although we know that *complex* is subjective. Running traffic assignment in AequilibraE consists in creating the traffic classes that are going to be assigned, passing them to a traffic assignment object with the assignment parameters, and running the assignment. ``TrafficClass`` ---------------- The ``TrafficClass`` object defines a single user class independently of the network. It holds the mode, cost field, demand matrix, optional skimming fields, and any fixed-cost or select-link configuration. The required information is: * **mode**: the network mode identifier (for example ``"c"`` for cars) * **cost**: the free-flow generalised cost field used for route choice * **demand**: the ``AequilibraeMatrix`` or ``AequilibraeSparseMatrix`` containing OD demand for that class * **name**: an optional class label; when omitted, it defaults to the mode .. testsetup:: from pathlib import Path from tempfile import TemporaryDirectory _assignment_docs_tmp = TemporaryDirectory() project_path = str(Path(_assignment_docs_tmp.name) / "project") .. doctest:: >>> from aequilibrae.traffic import TrafficClass >>> from aequilibrae.examples import create_example >>> project = create_example(project_path) # Get the matrices for cars and trucks >>> matrix_car = project.matrices.get_matrix("demand_mc.omx", subset=["car"]) >>> matrix_truck = project.matrices.get_matrix("demand_mc.omx", subset=["trucks"]) # Create the traffic classes >>> tc_car = TrafficClass(mode="c", cost="free_flow_time", demand=matrix_car, ... name="car", skimming=("free_flow_time", "distance"), ... graph_options={"block_centroid_flows": False}) >>> tc_truck = TrafficClass(mode="T", cost="free_flow_time", demand=matrix_truck, ... name="truck", skimming=("free_flow_time", "distance"), ... graph_options={"block_centroid_flows": False}) Routing policies such as ``block_centroid_flows`` are supplied through ``graph_options``. It accepts ``compact``, ``remove_dead_ends``, ``block_centroid_flows`` and ``allow_uturns``. When ``allow_uturns`` is omitted, the network's setting is used. The assignment builds the required graphs internally from the network. It is also possible to set the common assignment parameters during class definition: * **pce**: passenger-car equivalent for multi-class assignment; default is ``1.0``. * **vot**: value of time used to convert fixed costs to time units; default is ``1.0``. * **fixed_cost_field**: link field providing a monetary or other fixed cost in units compatible with ``vot``. ``TrafficClass`` converts it to time by multiplying by ``fixed_cost_multiplier`` and dividing by ``vot``. * **fixed_cost_multiplier**: scale applied to the fixed cost field. .. doctest:: >>> tc_truck = TrafficClass( ... mode="T", ... cost="free_flow_time", ... demand=matrix_truck, ... name="truck", ... pce=2.5, ... vot=0.35, ... fixed_cost_field="distance", ... fixed_cost_multiplier=1.0, ... skimming=("free_flow_time", "distance"), ... graph_options={"block_centroid_flows": False}, ... ) ``TrafficAssignment`` --------------------- .. doctest:: >>> from aequilibrae.traffic import TrafficAssignment >>> from aequilibrae.traffic.vdf import bpr >>> network = project.road.build_network() >>> assignment = TrafficAssignment( ... network=network, ... project=project, ... classes=[tc_car, tc_truck], ... time_field="free_flow_time", ... capacity_field="capacity", ... vdf=bpr, ... algorithm="bfw", ... name_mapping={"alpha": "b", "beta": "power"}, ... ) AequilibraE's traffic assignment is organised around a single constructor that retains a standalone network and current class configuration, or copies source data from a project network. It then prepares each class's routing graph during execution. Do not mutate a standalone network while an assignment uses it. * **network**: the source network object or project network containing the links and modes * **project**: the project used when saving results or skims from an independent network * **classes**: one or more fully-specified ``TrafficClass`` objects * **vdf**: the volume-delay function (VDF) to be used, being one of ``BPR``, ``BPR2``, ``CONICAL``, ``INRETS``, or ``AKCELIK``. Or a custom ``VDF`` object. * **name_mapping**: parameter names for the VDF, either as network fields or numeric constants * **time_field**: link field representing free-flow travel time * **capacity_field**: link field representing capacity * **algorithm**: assignment algorithm, such as ``"bfw"``, ``"fw"``, ``"msa"``, or ``"all-or-nothing"`` .. doctest:: >>> assignment = TrafficAssignment( ... network=network, ... project=project, ... classes=[tc_car, tc_truck], ... time_field="free_flow_time", ... capacity_field="capacity", ... vdf=bpr, ... algorithm="bfw", ... name_mapping={"alpha": 0.15, "beta": 4.0}, ... ) Skimming while assigning ~~~~~~~~~~~~~~~~~~~~~~~~ AequilibraE can skim during assignment and keeps both the latest all-or-nothing skims and the blended skims for the accepted solution. This is the case because, strictly speaking, the equilibrium travel time is the one resulting at the end of the last assignment iteration, while the most correct distance and toll skims, for example, are those resulting from the blended skim of all iterations. The user can select the fields they want to skim, as well save the skims to disk (and the project) at the end of the assignment procedure, where skims are tagged with suffixes *"_final"* and *"_blended"* for easy identification. The ``skimming`` fields are set on each ``TrafficClass`` before constructing the assignment, as above. .. doctest:: >>> _ = assignment.execute(max_iterations=10, cores=1) >>> assignment.save_skims("one_matrix_name", which_ones="all") ['one_matrix_name_car.omx', 'one_matrix_name_truck.omx'] Combined link results ~~~~~~~~~~~~~~~~~~~~~ ``assignment.results()`` and ``execute()`` return class load columns named ``_ab``, ``_ba`` and ``_tot``, without traffic-class prefixes. Result column names must be unique across all classes and shared network metrics, such as ``PCE_tot``. Conflicts are checked before execution, ignoring case to match SQLite result tables. Unassigned demand ~~~~~~~~~~~~~~~~~ Unassigned demand has an unreachable destination and is not loaded on the network. ``assignment.class_results["car"].unassigned_demand`` gives the total for the accepted solution, excluding intrazonal demand. Assigning sparse matrices ^^^^^^^^^^^^^^^^^^^^^^^^^ Modern Activity-Based models (and even some trip-based and tour-based ones) result on incredibly sparse demand matrices, which opens up a significant opportunity to save time during assignment by using early-exiting during the path-computation phase of assignment. AequilibraE is capable of leveraging this opportunity, and it does so automatically whenever the user **does NOT set skimming fields on the traffic classes**. In this case, AequilibraE has a convenient method to skim the final iteration of the assignment as it had been computed during the assignment itself. This method call requires a new iteration of path computation to be made, but assignments with highly sparse matrices and more than 10 iterations would still experience a significant speedup, which comes at the cost of not having blended skims as a sub-product of the assignment. Time savings of up to 40% should be achievable in cases of micro-simulated ABMs with a large number of zones and over 100 iterations of assignment. :: >>> skims = assignment.skim_congested(skim_fields=["distance"], return_matrices=True) >>> assignment.save_skims("another_matrix_name") ['another_matrix_name_car.omx', 'another_matrix_name_truck.omx'] The list of fields defined by the user for skimming is added to the congested time and the assignment cost from the last iteration of the assignment by default. These matrices are named *__congested_time__* and *__assignment_cost__* respectively. The returned dictionary maps traffic class names to ``SkimmingOutputs`` objects. Congested time includes link travel time and turn delay, but excludes fixed costs. Assignment cost includes those fixed costs, converted to time units with the traffic classes value of time. See the the example :ref:`example_assign_sparse` for a more practical explanation of this feature. Volume-delay function ~~~~~~~~~~~~~~~~~~~~~ AequilibraE supports five Volume Delay Functions (VDFs): BPR, BPR2, Conical, INRETS, and Akcelik. Each VDF has distinct characteristics that make it suitable for different modelling scenarios. For detailed information on VDF formulations, parameter guidance, and usage recommendations, see the dedicated :ref:`volume_delay_functions` page, which includes: * Mathematical formulas and technical descriptions * Default parameter values and calibration guidance * Behavioural charts showing V/C ratios from 0 to 3 * Origin and background for each function * Recommendations for choosing the right VDF **Quick reference:** * **BPR**: Standard highway assignment (α=0.15, β=4.0) * **BPR2**: Enhanced over-capacity penalty (α=0.15, β=4.0) * **Conical**: Smooth theoretical function (α=0.15, β=4.0) * **INRETS**: Urban arterials (α=1.0) * **Akcelik**: Signalised intersections (α=0.25, τ=0.8) Setting Preloads ---------------- We can also optionally include a preload vector for constant flows which are not being otherwise modelled. For example, this can be used to account for scheduled public transport vehicles, adding an equivalent load to each link along the route accordingly. AequilibraE supports various conditions for which PT trips to include in the preload, and allows the user to specify the PCE for each type of vehicle in the public transport network. To create a preload for public transport vehicles operating between 8 AM to 10 AM, do the following: .. doctest:: # Times are specified in seconds from midnight >>> transit = project.scenario.create_transit_database() >>> preload = transit.build_pt_preload(start=8*3600, end=10*3600) Pass a nonempty preload table to the assignment constructor using ``preload=preload``. The table must contain ``link_id``, ``direction`` and ``preload`` columns; its flow values are in PCE units. Executing an Assignment ----------------------- Finally, run traffic assignment! .. doctest:: >>> results = assignment.execute(max_iterations=10, cores=1) >>> project.close() References ---------- .. [1] Hampton Roads Transportation Planning Organization, Regional Travel Demand Model V2 (2020). Available in: https://www.hrtpo.org/uploads/docs/2020_HamptonRoads_Modelv2_MethodologyReport.pdf .. [2] Spiess, H. (1990) "Technical Note—Conical Volume-Delay Functions."Transportation Science, 24(2): 153-158. Available in: https://doi.org/10.1287/trsc.24.2.153 .. [3] Zill, J., Camargo, P., Veitch, T., Daisy, N. (2019) "Toll Choice and Stochastic User Equilibrium: Ticking All the Boxes", Transportation Research Record, 2673(4):930-940. Available in: https://doi.org/10.1177%2F0361198119837496