Forecasting#

In this example, we present a full forecasting workflow for the Sioux Falls example model.

We start creating the skim matrices, running the assignment for the base-year, and then distributing these trips into the network. Later, we estimate a set of future demand vectors which are going to be the input of a future year assignment with select link analysis.

See also

Several functions, methods, classes and modules are used in this example:

# Imports
from os.path import join
from tempfile import gettempdir
from uuid import uuid4

import pandas as pd

from aequilibrae.examples import create_example
from aequilibrae.utils import basic_config
# We create the example project inside our temp folder
fldr = join(gettempdir(), uuid4().hex)

project = create_example(fldr)
fldr
'/tmp/cbeaa4cdb1934db0995dce52eebe6317'
# We'll also apply a basic logging configuration.

basic_config()

project.parameters
{'run': {'example_function_with_kwargs': {'arg1': 'parameters.yml argument'}, 'matrix_summary': None, 'graph_summary': None, 'results_summary': None}, 'assignment': {'equilibrium': {'maximum_iterations': 250, 'rgap': 0.0001}}, 'vdfs': {'default': 'bpr', 'bpr_sioux_falls': {'function': 'bpr', 'spec': {'alpha': {'fillNA': 0.15, 'bounds': [0, inf]}, 'beta': {'fillNA': 1.0, 'bounds': [0, inf]}}}}, 'distribution': {'gravity': {'max error': 0.0001, 'max iterations': 100, 'max trip length': -1}, 'ipf': {'balancing tolerance': 0.001, 'convergence level': 0.0001, 'max iterations': 5000}}, 'network': {'links': {'fields': {'one-way': [{'link_id': {'description': 'Link ID. THIS FIELD CANNOT BE CHANGED', 'type': 'integer'}}, {'a_node': {'description': 'Node of origin of the link. THIS FIELD CANNOT BE CHANGED', 'type': 'integer'}}, {'b_node': {'description': 'Node of destinaiton of the link. THIS FIELD CANNOT BE CHANGED', 'type': 'integer'}}, {'direction': {'description': 'Direction of the link. THIS FIELD CANNOT BE CHANGED', 'type': 'integer'}}, {'distance': {'description': 'Length in meters for the link. THIS FIELD CANNOT BE CHANGED', 'type': 'numeric'}}, {'modes': {'description': 'Set of modes allowed in this link. THIS FIELD CANNOT BE CHANGED', 'type': 'text'}}, {'link_type': {'description': 'Classification of link (local, arterial, etc). THIS FIELD CANNOT BE CHANGED', 'osm_source': 'highway', 'type': 'varchar'}}, {'name': {'description': 'name', 'osm_source': 'name', 'type': 'text'}}], 'two-way': [{'lanes': {'description': 'lanes', 'osm_behaviour': 'divide', 'osm_source': 'lanes', 'type': 'integer'}}, {'capacity': {'description': 'capacity', 'type': 'numeric'}}, {'speed': {'description': 'speed', 'osm_behaviour': 'copy', 'osm_source': 'maxspeed', 'type': 'numeric'}}]}, 'link_types': [{'centroid_connector': {'description': 'VIRTUAL centroid connectors only', 'lane_capacity': 10000, 'lanes': 10, 'link_type_id': 'z'}}, {'default': {'description': 'Default link type. Due to lack of information', 'lane_capacity': 900, 'lanes': 2, 'link_type_id': 'y'}}]}, 'modes': [{'car': {'description': 'All motorized vehicles', 'letter': 'c'}}, {'transit': {'description': 'All links capable of supporting transit-vehicles', 'letter': 't'}}, {'walk': {'description': 'Linkes where walking is permitted', 'letter': 'w'}}, {'bicycle': {'description': 'All Bicycles', 'letter': 'b'}}], 'nodes': {'fields': [{'node_id': {'description': 'node_id. THIS FIELD CANNOT BE CHANGED', 'type': 'integer'}}, {'is_centroid': {'description': 'is_centroid', 'type': 'integer'}}]}, 'osm': {'all_link_types': ['secondary_link', 'escalator', 'trail', 'cycleway', 'path', 'trunk_link', 'secondary', 'escape', 'track', 'road', 'motorway_link', 'primary', 'corridor', 'residential', 'footway', 'motorway', 'primary_link', 'unclassified', 'bus_guideway', 'tertiary_link', 'living_street', 'pedestrian', 'bridleway', 'elevator', 'motor', 'trunk', 'tertiary', 'service', 'steps', 'proposed', 'raceway', 'construction', 'abandoned', 'platform', 'unclassified'], 'modes': {'bicycle': {'link_types': ['cycleway', 'corridor', 'pedestrian', 'path', 'track', 'trail', 'unclassified'], 'mode_filter': {'bicycle': 'no'}, 'unknown_tags': True}, 'car': {'link_types': ['motor', 'motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'unclassified', 'residential', 'motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link', 'living_street', 'service', 'escape', 'road'], 'mode_filter': {'motor_vehicle': 'no'}, 'unknown_tags': True}, 'transit': {'link_types': ['motor', 'motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'unclassified', 'residential', 'motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link', 'living_street', 'service', 'escape', 'road', 'bus_guideway'], 'unknown_tags': True}, 'walk': {'link_types': ['cycleway', 'footway', 'steps', 'corridor', 'pedestrian', 'elevator', 'escalator', 'path', 'track', 'trail', 'bridleway', 'unclassified'], 'mode_filter': {'pedestrian': 'no'}, 'unknown_tags': True}}}, 'gmns': {'critical_dist': 2, 'node': {'equivalency': {'node_id': 'node_id'}, 'fields': {'node_id': {'description': 'Unique key', 'type': 'integer', 'required': True}, 'name': {'description': 'Node name', 'type': 'text', 'required': False}, 'x_coord': {'description': 'Coordinate system specified in GMNS config file', 'type': 'numeric', 'required': True}, 'y_coord': {'description': 'Coordinate system specified in GMNS config file', 'type': 'numeric', 'required': True}, 'z_coord': {'description': 'Altitude in short length units', 'type': 'numeric', 'required': False}, 'node_type': {'description': 'What it represents', 'type': 'text', 'required': False}, 'ctrl_type': {'description': 'From GMNS ControlType_Set', 'type': 'text', 'required': False}, 'zone_id': {'description': 'TAZ or city, or census tract', 'type': 'integer', 'required': False}, 'parent_node_id': {'description': 'The parent of this node', 'type': 'integer', 'required': False}}}, 'link': {'equivalency': {'link_id': 'link_id', 'a_node': 'from_node_id', 'b_node': 'to_node_id', 'direction': 'directed', 'distance': 'length', 'speed': 'free_speed', 'capacity': 'capacity', 'lanes': 'lanes', 'name': 'name', 'link_type': 'facility_type', 'modes': 'allowed_uses', 'geometry': 'geometry'}, 'fields': {'link_id': {'description': 'Primary key', 'type': 'integer', 'required': True}, 'name': {'description': 'Name', 'type': 'text', 'required': False}, 'from_node_id': {'description': 'Foreign key (Nodes table)', 'type': 'integer', 'required': True}, 'to_node_id': {'description': 'Foreign key (Nodes table)', 'type': 'integer', 'required': True}, 'directed': {'description': 'Whether the link is directed (travel only occurs from the from_node to the to_node) or undirected.', 'type': 'integer', 'required': True}, 'dir_flag': {'description': 'Shapepoints direction', 'type': 'integer', 'required': False}, 'length': {'description': 'GMNS length in long_length units', 'type': 'numeric', 'required': False}, 'geometry_id': {'description': 'Foreign key from geometry table', 'type': 'integer', 'required': False}, 'geometry': {'description': 'Link geometry, in well-known text (WKT) format.', 'type': 'text', 'required': False}, 'parent_link_id': {'description': 'The parent of this link', 'type': 'integer', 'required': False}, 'grade': {'description': 'Percent grade of the link', 'type': 'integer', 'required': False}, 'facility_type': {'description': 'Facility type (e.g., freeway, arterial, etc.)', 'type': 'text', 'required': False}, 'capacity': {'description': 'Capacity (veh / hr / lane)', 'type': 'numeric', 'required': False}, 'free_speed': {'description': 'Free flow speed in long_length units per hour', 'type': 'numeric', 'required': False}, 'lanes': {'description': 'Number of lanes in the direction of travel', 'type': 'inetger', 'required': False}, 'bike_facility': {'description': 'Type of bicycle accommodation', 'type': 'text', 'required': False}, 'ped_facility': {'description': 'Type of pedestrian accommodation', 'type': 'text', 'required': False}, 'parking': {'description': 'Type of parking', 'type': 'text', 'required': False}, 'allowed_uses': {'description': 'Set of allowed uses', 'type': 'text', 'required': False}, 'toll': {'description': 'Toll in currency units', 'type': 'numeric', 'required': False}, 'jurisdiction': {'description': 'Owner/operator of the link', 'type': 'text', 'required': False}, 'row_width': {'description': 'Width of the entire right-of-way (both directions)', 'type': 'numeric', 'required': False}}}, 'use_definition': {'fields': {'use': {'description': 'Short name of the vehicle', 'type': 'text', 'required': True}, 'persons_per_vehicle': {'description': 'Average persons per vehicle. Used to compute person-based performance measures (0 for non-travel uses)', 'type': 'numeric', 'required': True}, 'pce': {'description': 'Passenger car equivalents, used for capacity calculations (0 for non-travel uses)', 'type': 'numeric', 'required': True}, 'special_conditions': {'description': 'Treatment of these special conditions may be needed in a future version of GMNS', 'type': 'text', 'required': False}, 'description': {'description': 'A longer description of the mode', 'type': 'text', 'required': False}}, 'equivalency': {'mode_name': 'use'}}}}, 'osm': {'overpass_endpoint': 'http://overpass-api.de/api', 'nominatim_endpoint': 'https://nominatim.openstreetmap.org/', 'accept_language': 'en', 'max_attempts': 50, 'timeout': 540, 'max_query_area_size': 2500000000, 'sleeptime': 10}, 'system': {'cpus': 0, 'default_directory': 'C:\\', 'logging': True, 'logging_directory': '/temp/', 'spatialite_path': 'C:\\mod_spatialite-NG-win-amd64', 'temp directory': '/temp'}}

Traffic assignment with skimming#

In this step, we’ll set the skims for the variable free_flow_time, and execute the traffic assignment for the base-year.

from dataclasses import replace
from aequilibrae.traffic import TrafficAssignment, TrafficClass, VDF

Let’s get the demand matrix directly from its file, and inspect what matrices we have in the project.

proj_matrices = project.matrices
proj_matrices.get_available_matrices_and_metadata()
{'skims.omx': {'procedure': None, 'procedure_id': None, 'timestamp': None, 'description': 'Example skim'}, 'demand.omx': {'procedure': None, 'procedure_id': None, 'timestamp': '2020-11-24 08:47:18', 'description': 'Original data imported to OMX format'}, 'demand_mc.omx': {'procedure': None, 'procedure_id': None, 'timestamp': '2021-02-24 00:51:35', 'description': None}}

Load the demand matrix core for computation.

demand = proj_matrices.get_matrix("demand.omx", subset=["matrix"])

Let’s perform the traffic assignment

# Create the assignment class
assigclass = TrafficClass(
    name="car",
    mode="c",
    cost="free_flow_time",
    demand=demand,
    skimming=["free_flow_time", "distance"],
    # Every node in Sioux Falls is a centroid, so paths must pass through centroids.
    graph_options={"block_centroid_flows": False},
)


from aequilibrae.routing.cython.vdf_core import akcelik, bpr, bpr2, conical, delta_akcelik, delta_bpr, delta_bpr2, delta_conical, delta_inrets, inrets

bpr_spec = {
    "alpha": {"fill_NA": 0.15, "bounds": (0.0, float("inf"))},
    "beta": {"fill_NA": 4.0, "bounds": (1.0, float("inf"))},
}
vdf = VDF("bpr_example", bpr, bpr_spec, delta_bpr)

# Configure the VDF, network fields, and assignment algorithm.
assig = TrafficAssignment(
    network=network,
    project=project,
    classes=[assigclass],
    vdf=vdf,
    name_mapping={"alpha": "b", "beta": "power"},
    time_field="free_flow_time",
    capacity_field="capacity",
    algorithm="bfw",
)
assig.execute(max_iterations=1000, rgap=0.001)
Equilibrium Assignment                            :   0%|          | 0/1000 [00:00<?, ?it/s]
All-or-Nothing - Traffic Class: car               :   0%|          | 1/1000 [00:00<00:00, 31775.03it/s]
Equilibrium Assignment - Iteration: 1/1000 - RGap: inf:   0%|          | 1/1000 [00:00<00:00, 1725.34it/s]
Equilibrium Assignment                            :   0%|          | 2/1000 [00:00<00:00, 2982.09it/s]
All-or-Nothing - Traffic Class: car               :   0%|          | 2/1000 [00:00<00:00, 2669.83it/s]
Equilibrium Assignment - Iteration: 2/1000 - RGap: 0.897805:   0%|          | 2/1000 [00:00<00:00, 1168.33it/s]
Equilibrium Assignment                            :   0%|          | 3/1000 [00:00<00:00, 1663.75it/s]
All-or-Nothing - Traffic Class: car               :   0%|          | 3/1000 [00:00<00:00, 1591.16it/s]
Equilibrium Assignment - Iteration: 3/1000 - RGap: 0.527933:   0%|          | 3/1000 [00:00<00:01, 989.30it/s]
Equilibrium Assignment                            :   0%|          | 4/1000 [00:00<00:00, 1275.45it/s]
All-or-Nothing - Traffic Class: car               :   0%|          | 4/1000 [00:00<00:00, 1247.28it/s]
Equilibrium Assignment - Iteration: 4/1000 - RGap: 0.288744:   0%|          | 4/1000 [00:00<00:01, 869.38it/s]
Equilibrium Assignment                            :   0%|          | 5/1000 [00:00<00:00, 1066.22it/s]
All-or-Nothing - Traffic Class: car               :   0%|          | 5/1000 [00:00<00:00, 1048.47it/s]
Equilibrium Assignment - Iteration: 5/1000 - RGap: 0.196074:   0%|          | 5/1000 [00:00<00:01, 803.08it/s]
Equilibrium Assignment                            :   1%|          | 6/1000 [00:00<00:01, 949.83it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 6/1000 [00:00<00:01, 938.04it/s]
Equilibrium Assignment - Iteration: 6/1000 - RGap: 0.223256:   1%|          | 6/1000 [00:00<00:01, 775.93it/s]
Equilibrium Assignment                            :   1%|          | 7/1000 [00:00<00:01, 894.44it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 7/1000 [00:00<00:01, 885.49it/s]
Equilibrium Assignment - Iteration: 7/1000 - RGap: 0.171572:   1%|          | 7/1000 [00:00<00:01, 789.06it/s]
Equilibrium Assignment                            :   1%|          | 8/1000 [00:00<00:01, 892.22it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 8/1000 [00:00<00:01, 884.06it/s]
Equilibrium Assignment - Iteration: 8/1000 - RGap: 0.0844232:   1%|          | 8/1000 [00:00<00:01, 799.20it/s]
Equilibrium Assignment                            :   1%|          | 9/1000 [00:00<00:01, 889.00it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 9/1000 [00:00<00:01, 881.61it/s]
Equilibrium Assignment - Iteration: 9/1000 - RGap: 0.0782634:   1%|          | 9/1000 [00:00<00:01, 771.30it/s]
Equilibrium Assignment                            :   1%|          | 10/1000 [00:00<00:01, 850.22it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 10/1000 [00:00<00:01, 844.59it/s]
Equilibrium Assignment - Iteration: 10/1000 - RGap: 0.0930102:   1%|          | 10/1000 [00:00<00:01, 759.30it/s]
Equilibrium Assignment                            :   1%|          | 11/1000 [00:00<00:01, 829.15it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 11/1000 [00:00<00:01, 824.16it/s]
Equilibrium Assignment - Iteration: 11/1000 - RGap: 0.0692189:   1%|          | 11/1000 [00:00<00:01, 769.52it/s]
Equilibrium Assignment                            :   1%|          | 12/1000 [00:00<00:01, 834.40it/s]
All-or-Nothing - Traffic Class: car               :   1%|          | 12/1000 [00:00<00:01, 830.01it/s]
Equilibrium Assignment - Iteration: 12/1000 - RGap: 0.0402345:   1%|          | 12/1000 [00:00<00:01, 778.69it/s]
Equilibrium Assignment                            :   1%|▏         | 13/1000 [00:00<00:01, 838.68it/s]
All-or-Nothing - Traffic Class: car               :   1%|▏         | 13/1000 [00:00<00:01, 834.13it/s]
Equilibrium Assignment - Iteration: 13/1000 - RGap: 0.0329665:   1%|▏         | 13/1000 [00:00<00:01, 760.44it/s]
Equilibrium Assignment                            :   1%|▏         | 14/1000 [00:00<00:01, 814.30it/s]
All-or-Nothing - Traffic Class: car               :   1%|▏         | 14/1000 [00:00<00:01, 810.42it/s]
Equilibrium Assignment - Iteration: 14/1000 - RGap: 0.0350811:   1%|▏         | 14/1000 [00:00<00:01, 751.87it/s]
Equilibrium Assignment                            :   2%|▏         | 15/1000 [00:00<00:01, 801.40it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 15/1000 [00:00<00:01, 797.77it/s]
Equilibrium Assignment - Iteration: 15/1000 - RGap: 0.032004:   2%|▏         | 15/1000 [00:00<00:01, 745.08it/s]
Equilibrium Assignment                            :   2%|▏         | 16/1000 [00:00<00:01, 790.85it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 16/1000 [00:00<00:01, 787.51it/s]
Equilibrium Assignment - Iteration: 16/1000 - RGap: 0.0210404:   2%|▏         | 16/1000 [00:00<00:01, 751.47it/s]
Equilibrium Assignment                            :   2%|▏         | 17/1000 [00:00<00:01, 794.79it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 17/1000 [00:00<00:01, 791.72it/s]
Equilibrium Assignment - Iteration: 17/1000 - RGap: 0.0129254:   2%|▏         | 17/1000 [00:00<00:01, 758.69it/s]
Equilibrium Assignment                            :   2%|▏         | 18/1000 [00:00<00:01, 799.98it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 18/1000 [00:00<00:01, 797.30it/s]
Equilibrium Assignment - Iteration: 18/1000 - RGap: 0.012294:   2%|▏         | 18/1000 [00:00<00:01, 752.81it/s]
Equilibrium Assignment                            :   2%|▏         | 19/1000 [00:00<00:01, 791.24it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 19/1000 [00:00<00:01, 787.72it/s]
Equilibrium Assignment - Iteration: 19/1000 - RGap: 0.0123165:   2%|▏         | 19/1000 [00:00<00:01, 747.06it/s]
Equilibrium Assignment                            :   2%|▏         | 20/1000 [00:00<00:01, 783.48it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 20/1000 [00:00<00:01, 780.87it/s]
Equilibrium Assignment - Iteration: 20/1000 - RGap: 0.011735:   2%|▏         | 20/1000 [00:00<00:01, 741.73it/s]
Equilibrium Assignment                            :   2%|▏         | 21/1000 [00:00<00:01, 774.39it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 21/1000 [00:00<00:01, 770.10it/s]
Equilibrium Assignment - Iteration: 21/1000 - RGap: 0.0135925:   2%|▏         | 21/1000 [00:00<00:01, 734.19it/s]
Equilibrium Assignment                            :   2%|▏         | 22/1000 [00:00<00:01, 766.56it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 22/1000 [00:00<00:01, 764.31it/s]
Equilibrium Assignment - Iteration: 22/1000 - RGap: 0.00994976:   2%|▏         | 22/1000 [00:00<00:01, 738.94it/s]
Equilibrium Assignment                            :   2%|▏         | 23/1000 [00:00<00:01, 770.05it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 23/1000 [00:00<00:01, 767.78it/s]
Equilibrium Assignment - Iteration: 23/1000 - RGap: 0.00730011:   2%|▏         | 23/1000 [00:00<00:01, 744.07it/s]
Equilibrium Assignment                            :   2%|▏         | 24/1000 [00:00<00:01, 774.12it/s]
All-or-Nothing - Traffic Class: car               :   2%|▏         | 24/1000 [00:00<00:01, 771.40it/s]
Equilibrium Assignment - Iteration: 24/1000 - RGap: 0.00680425:   2%|▏         | 24/1000 [00:00<00:01, 740.15it/s]
Equilibrium Assignment                            :   2%|▎         | 25/1000 [00:00<00:01, 768.76it/s]
All-or-Nothing - Traffic Class: car               :   2%|▎         | 25/1000 [00:00<00:01, 766.86it/s]
Equilibrium Assignment - Iteration: 25/1000 - RGap: 0.00539346:   2%|▎         | 25/1000 [00:00<00:01, 732.82it/s]
Equilibrium Assignment                            :   3%|▎         | 26/1000 [00:00<00:01, 759.88it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 26/1000 [00:00<00:01, 758.12it/s]
Equilibrium Assignment - Iteration: 26/1000 - RGap: 0.00585136:   3%|▎         | 26/1000 [00:00<00:01, 729.85it/s]
Equilibrium Assignment                            :   3%|▎         | 27/1000 [00:00<00:01, 755.99it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 27/1000 [00:00<00:01, 754.23it/s]
Equilibrium Assignment - Iteration: 27/1000 - RGap: 0.00771341:   3%|▎         | 27/1000 [00:00<00:01, 726.85it/s]
Equilibrium Assignment                            :   3%|▎         | 28/1000 [00:00<00:01, 751.93it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 28/1000 [00:00<00:01, 750.38it/s]
Equilibrium Assignment - Iteration: 28/1000 - RGap: 0.00666555:   3%|▎         | 28/1000 [00:00<00:01, 724.99it/s]
Equilibrium Assignment                            :   3%|▎         | 29/1000 [00:00<00:01, 748.96it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 29/1000 [00:00<00:01, 747.29it/s]
Equilibrium Assignment - Iteration: 29/1000 - RGap: 0.00478169:   3%|▎         | 29/1000 [00:00<00:01, 730.29it/s]
Equilibrium Assignment                            :   3%|▎         | 30/1000 [00:00<00:01, 753.69it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 30/1000 [00:00<00:01, 752.15it/s]
Equilibrium Assignment - Iteration: 30/1000 - RGap: 0.00426152:   3%|▎         | 30/1000 [00:00<00:01, 735.89it/s]
Equilibrium Assignment                            :   3%|▎         | 31/1000 [00:00<00:01, 758.67it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 31/1000 [00:00<00:01, 757.20it/s]
Equilibrium Assignment - Iteration: 31/1000 - RGap: 0.00381267:   3%|▎         | 31/1000 [00:00<00:01, 733.32it/s]
Equilibrium Assignment                            :   3%|▎         | 32/1000 [00:00<00:01, 755.31it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 32/1000 [00:00<00:01, 753.81it/s]
Equilibrium Assignment - Iteration: 32/1000 - RGap: 0.00390184:   3%|▎         | 32/1000 [00:00<00:01, 732.17it/s]
Equilibrium Assignment                            :   3%|▎         | 33/1000 [00:00<00:01, 753.38it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 33/1000 [00:00<00:01, 751.96it/s]
Equilibrium Assignment - Iteration: 33/1000 - RGap: 0.00276918:   3%|▎         | 33/1000 [00:00<00:01, 731.02it/s]
Equilibrium Assignment                            :   3%|▎         | 34/1000 [00:00<00:01, 751.66it/s]
All-or-Nothing - Traffic Class: car               :   3%|▎         | 34/1000 [00:00<00:01, 750.40it/s]
Equilibrium Assignment - Iteration: 34/1000 - RGap: 0.0029226:   3%|▎         | 34/1000 [00:00<00:01, 732.43it/s]
Equilibrium Assignment                            :   4%|▎         | 35/1000 [00:00<00:01, 752.58it/s]
All-or-Nothing - Traffic Class: car               :   4%|▎         | 35/1000 [00:00<00:01, 751.33it/s]
Equilibrium Assignment - Iteration: 35/1000 - RGap: 0.0025376:   4%|▎         | 35/1000 [00:00<00:01, 731.34it/s]
Equilibrium Assignment                            :   4%|▎         | 36/1000 [00:00<00:01, 750.73it/s]
All-or-Nothing - Traffic Class: car               :   4%|▎         | 36/1000 [00:00<00:01, 749.47it/s]
Equilibrium Assignment - Iteration: 36/1000 - RGap: 0.00258702:   4%|▎         | 36/1000 [00:00<00:01, 728.60it/s]
Equilibrium Assignment                            :   4%|▎         | 37/1000 [00:00<00:01, 747.37it/s]
All-or-Nothing - Traffic Class: car               :   4%|▎         | 37/1000 [00:00<00:01, 745.74it/s]
Equilibrium Assignment - Iteration: 37/1000 - RGap: 0.00225626:   4%|▎         | 37/1000 [00:00<00:01, 727.18it/s]
Equilibrium Assignment                            :   4%|▍         | 38/1000 [00:00<00:01, 745.42it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 38/1000 [00:00<00:01, 743.85it/s]
Equilibrium Assignment - Iteration: 38/1000 - RGap: 0.00197123:   4%|▍         | 38/1000 [00:00<00:01, 725.85it/s]
Equilibrium Assignment                            :   4%|▍         | 39/1000 [00:00<00:01, 743.60it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 39/1000 [00:00<00:01, 742.45it/s]
Equilibrium Assignment - Iteration: 39/1000 - RGap: 0.00169449:   4%|▍         | 39/1000 [00:00<00:01, 722.77it/s]
Equilibrium Assignment                            :   4%|▍         | 40/1000 [00:00<00:01, 740.00it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 40/1000 [00:00<00:01, 738.35it/s]
Equilibrium Assignment - Iteration: 40/1000 - RGap: 0.00185543:   4%|▍         | 40/1000 [00:00<00:01, 719.94it/s]
Equilibrium Assignment                            :   4%|▍         | 41/1000 [00:00<00:01, 736.48it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 41/1000 [00:00<00:01, 735.32it/s]
Equilibrium Assignment - Iteration: 41/1000 - RGap: 0.00206881:   4%|▍         | 41/1000 [00:00<00:01, 717.48it/s]
Equilibrium Assignment                            :   4%|▍         | 42/1000 [00:00<00:01, 733.82it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 42/1000 [00:00<00:01, 732.85it/s]
Equilibrium Assignment - Iteration: 42/1000 - RGap: 0.00197741:   4%|▍         | 42/1000 [00:00<00:01, 716.62it/s]
Equilibrium Assignment                            :   4%|▍         | 43/1000 [00:00<00:01, 732.47it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 43/1000 [00:00<00:01, 731.54it/s]
Equilibrium Assignment - Iteration: 43/1000 - RGap: 0.00159214:   4%|▍         | 43/1000 [00:00<00:01, 716.30it/s]
Equilibrium Assignment                            :   4%|▍         | 44/1000 [00:00<00:01, 731.47it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 44/1000 [00:00<00:01, 730.60it/s]
Equilibrium Assignment - Iteration: 44/1000 - RGap: 0.00125555:   4%|▍         | 44/1000 [00:00<00:01, 717.83it/s]
Equilibrium Assignment                            :   4%|▍         | 45/1000 [00:00<00:01, 733.03it/s]
All-or-Nothing - Traffic Class: car               :   4%|▍         | 45/1000 [00:00<00:01, 732.12it/s]
Equilibrium Assignment - Iteration: 45/1000 - RGap: 0.0011702:   4%|▍         | 45/1000 [00:00<00:01, 716.40it/s]
Equilibrium Assignment                            :   5%|▍         | 46/1000 [00:00<00:01, 731.52it/s]
All-or-Nothing - Traffic Class: car               :   5%|▍         | 46/1000 [00:00<00:01, 730.53it/s]
Equilibrium Assignment - Iteration: 46/1000 - RGap: 0.00129903:   5%|▍         | 46/1000 [00:00<00:01, 714.97it/s]
Equilibrium Assignment                            :   5%|▍         | 47/1000 [00:00<00:01, 729.48it/s]
All-or-Nothing - Traffic Class: car               :   5%|▍         | 47/1000 [00:00<00:01, 728.66it/s]
Equilibrium Assignment - Iteration: 47/1000 - RGap: 0.00162518:   5%|▍         | 47/1000 [00:00<00:01, 714.90it/s]
Equilibrium Assignment                            :   5%|▍         | 48/1000 [00:00<00:01, 729.07it/s]
All-or-Nothing - Traffic Class: car               :   5%|▍         | 48/1000 [00:00<00:01, 728.23it/s]
Equilibrium Assignment - Iteration: 48/1000 - RGap: 0.0012404:   5%|▍         | 48/1000 [00:00<00:01, 711.31it/s]
Equilibrium Assignment                            :   5%|▍         | 49/1000 [00:00<00:01, 725.04it/s]
All-or-Nothing - Traffic Class: car               :   5%|▍         | 49/1000 [00:00<00:01, 724.13it/s]
Equilibrium Assignment - Iteration: 49/1000 - RGap: 0.00111975:   5%|▍         | 49/1000 [00:00<00:01, 710.10it/s]
Equilibrium Assignment                            :   5%|▌         | 50/1000 [00:00<00:01, 723.36it/s]
All-or-Nothing - Traffic Class: car               :   5%|▌         | 50/1000 [00:00<00:01, 722.47it/s]
Equilibrium Assignment - Iteration: 50/1000 - RGap: 0.00108492:   5%|▌         | 50/1000 [00:00<00:01, 709.02it/s]
Equilibrium Assignment                            :   5%|▌         | 51/1000 [00:00<00:01, 722.25it/s]
All-or-Nothing - Traffic Class: car               :   5%|▌         | 51/1000 [00:00<00:01, 721.43it/s]
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.259225 0.0 4532.259225 0.0 0.0 0.0 6.000844 0.0 6.000844 1.000141 0.0 1.000141 0.174989 0.0 0.174989 4532.259225 0.0 4532.259225
2 8124.401779 0.0 8124.401779 0.0 0.0 0.0 4.008714 0.0 4.008714 1.002178 0.0 1.002178 0.347145 0.0 0.347145 8124.401779 0.0 8124.401779
3 4524.401779 0.0 4524.401779 0.0 0.0 0.0 6.000838 0.0 6.000838 1.000140 0.0 1.000140 0.174686 0.0 0.174686 4524.401779 0.0 4524.401779
4 5913.685237 0.0 5913.685237 0.0 0.0 0.0 6.517765 0.0 6.517765 1.303553 0.0 1.303553 1.192713 0.0 1.192713 5913.685237 0.0 5913.685237
5 8132.259225 0.0 8132.259225 0.0 0.0 0.0 4.008747 0.0 4.008747 1.002187 0.0 1.002187 0.347481 0.0 0.347481 8132.259225 0.0 8132.259225
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
72 9645.734151 0.0 9645.734151 0.0 0.0 0.0 12.310217 0.0 12.310217 3.077554 0.0 3.077554 1.929147 0.0 1.929147 9645.734151 0.0 9645.734151
73 7848.474395 0.0 7848.474395 0.0 0.0 0.0 3.711266 0.0 3.711266 1.855633 0.0 1.855633 1.545429 0.0 1.545429 7848.474395 0.0 7848.474395
74 11066.019693 0.0 11066.019693 0.0 0.0 0.0 17.391130 0.0 17.391130 4.347782 0.0 4.347782 2.173534 0.0 2.173534 11066.019693 0.0 11066.019693
75 10214.009370 0.0 10214.009370 0.0 0.0 0.0 11.598290 0.0 11.598290 3.866097 0.0 3.866097 2.090739 0.0 2.090739 10214.009370 0.0 10214.009370
76 7833.540663 0.0 7833.540663 0.0 0.0 0.0 3.698279 0.0 3.698279 1.849139 0.0 1.849139 1.542488 0.0 1.542488 7833.540663 0.0 7833.540663

76 rows × 18 columns



After finishing the assignment, we can easily see the convergence report.

convergence_report = assig.report()
convergence_report.head()
iteration time rgap alpha warnings beta0 beta1 beta2 conjugacy_prev conjugacy_prev2 hessian_drift bfw_clamped
0 1 0.001556 inf 1.000000 1.000000 0.000000 0.0 NaN NaN NaN NaN
1 2 0.002671 0.897805 0.328400 1.000000 0.000000 0.0 NaN NaN NaN NaN
2 3 0.004031 0.527933 0.186602 1.000000 0.000000 0.0 NaN NaN NaN NaN
3 4 0.005589 0.288744 0.241148 1.000000 0.000000 0.0 9.131125e-03 0.150500 0.076780 True
4 5 0.007210 0.196074 0.818547 0.607382 0.392618 0.0 -2.891974e-18 0.021602 -0.069162 True


And we can also see the results of the assignment

results = assig.results()
results.head()
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.259225 0.0 4532.259225 0.0 0.0 0.0 6.000844 0.0 6.000844 1.000141 0.0 1.000141 0.174989 0.0 0.174989 4532.259225 0.0 4532.259225
2 8124.401779 0.0 8124.401779 0.0 0.0 0.0 4.008714 0.0 4.008714 1.002178 0.0 1.002178 0.347145 0.0 0.347145 8124.401779 0.0 8124.401779
3 4524.401779 0.0 4524.401779 0.0 0.0 0.0 6.000838 0.0 6.000838 1.000140 0.0 1.000140 0.174686 0.0 0.174686 4524.401779 0.0 4524.401779
4 5913.685237 0.0 5913.685237 0.0 0.0 0.0 6.517765 0.0 6.517765 1.303553 0.0 1.303553 1.192713 0.0 1.192713 5913.685237 0.0 5913.685237
5 8132.259225 0.0 8132.259225 0.0 0.0 0.0 4.008747 0.0 4.008747 1.002187 0.0 1.002187 0.347481 0.0 0.347481 8132.259225 0.0 8132.259225


We can export our results to CSV or get a Pandas DataFrame, but let’s put it directly into the results database

assig.save_results("base_year_assignment")
ResultRecord(scenario=None, year=None, table_name='base_year_assignment', reference_table='links', procedure='traffic assignment', procedure_id='f8de214a22d74fe791b7615f764e43b8', procedure_report='"{\\"convergence\\": {\\"iteration\\": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], \\"time\\": [0.001556120999794075, 0.0026707989998158155, 0.004030793999845628, 0.005588577999787958, 0.007209629999806566, 0.008731215999887354, 0.009852555999941615, 0.010998441999845454, 0.012651362999804405, 0.014143704999924012, 0.015290742999923168, 0.016391815999895698, 0.01806097700000464, 0.019595827999864923, 0.021101925999801097, 0.022270733999903314, 0.023395160999825748, 0.024887261999992916, 0.0264111829999365, 0.02794236699992325, 0.029595448000009128, 0.03075053099996694, 0.031894824999881166, 0.033412473999987924, 0.03506967199996325, 0.036601938999865524, 0.038077008999835016, 0.0396154759998808, 0.04069873599996754, 0.04174788099999205, 0.043256532999976116, 0.044682741999849895, 0.046100885999976526, 0.04741334199979974, 0.048841734999996333, 0.050386634999995294, 0.05185560299992176, 0.05333198499988612, 0.05494754599999396, 0.056530286999986856, 0.058109970999794314, 0.05958352799984823, 0.0610119709999708, 0.062278942999910214, 0.06376007299991215, 0.06529367199982516, 0.0667242989998158, 0.0682507749997967, 0.06997372599994378, 0.0714878389999285, 0.07224034099999699], \\"rgap\\": [Infinity, 0.89780457879145, 0.5279325275367827, 0.28874383368012313, 0.19607377855371996, 0.22325602275663892, 0.17157154728042692, 0.08442324131581802, 0.07826339212239238, 0.09301015583411931, 0.06921891008667841, 0.040234500847269324, 0.03296645686481366, 0.035081093446416724, 0.03200399984459911, 0.021040384142766117, 0.012925420204469549, 0.012294041013807001, 0.012316538874428259, 0.011735043381187355, 0.013592539642584927, 0.00994976386388666, 0.007300114596153, 0.006804251749703222, 0.005393460474102812, 0.005851357665095232, 0.007713407379077174, 0.006665546808826156, 0.004781690445278009, 0.004261520961977526, 0.0038126726071258076, 0.0039018385989099892, 0.0027691782467265474, 0.002922596383931646, 0.0025376020166008187, 0.0025870156274787927, 0.002256256926787925, 0.0019712280906955966, 0.0016944864716724052, 0.001855434633644199, 0.002068813194238471, 0.0019774079905162064, 0.0015921406213157043, 0.0012555453136291039, 0.0011701957344778826, 0.0012990275462257785, 0.0016251801857951145, 0.0012404030429357153, 0.0011197535305810323, 0.001084921254077063, 0.0008283431216585221], \\"alpha\\": [1.0, 0.32839952448634563, 0.18660240547488702, 0.24114774402917927, 0.8185470737942451, 1.0, 0.16910548905080566, 0.2373694741062248, 0.5860366441988628, 1.0, 0.11507399176716364, 0.15591705624574248, 0.2778261672706741, 0.6208345025526621, 1.0, 0.11598592580715987, 0.1073638625831236, 0.1558846600948632, 0.33733828320278225, 0.5959894379150801, 1.0, 0.07079581621265686, 0.07166629092302472, 0.16836899188970833, 0.17808313987176075, 0.3501427558560422, 0.7043109868542726, 1.0, 0.04490489890507015, 0.07014221441138183, 0.09742344627690626, 0.06258400921325322, 0.06719595392325393, 0.12319408791407051, 0.12906133722764354, 0.08532578952857073, 0.1731558811922753, 0.15293940408908577, 0.12413536741025445, 0.21741995994704033, 0.1935494212591544, 0.44975105825027883, 0.17421327791691452, 0.13748393745882995, 0.10615777662183085, 0.2592836388961607, 0.22776458945281172, 0.22479302809702625, 0.16572384651685768, 0.20326482699172146, NaN], \\"warnings\\": [\\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"Invalid BFW mu coefficient; using the Frank-Wolfe direction.\\", \\"\\", \\"\\", \\"\\", \\"Invalid BFW mu coefficient; using the Frank-Wolfe direction.\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"Invalid BFW mu coefficient; using the Frank-Wolfe direction.\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"Invalid BFW mu coefficient; using the Frank-Wolfe direction.\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"Invalid BFW mu coefficient; using the Frank-Wolfe direction.\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\", \\"\\"], \\"beta0\\": [1.0, 1.0, 1.0, 1.0, 0.6073815058784465, 0.07423949064975859, 1.0, 0.8978530762747693, 0.5210859419543844, 0.1310033991660148, 1.0, 0.9742007731752091, 0.6352581871354512, 0.2722926744813646, 0.09981147064012809, 1.0, 0.6455493939118717, 0.5446197221699854, 0.37684414353412243, 0.16621614561922343, 0.04991453282323065, 1.0, 0.7074847674334792, 0.4072593278628423, 0.40744644365281185, 0.20433348406491295, 0.06663070239531205, 0.029625006738385674, 1.0, 0.5852667007449526, 0.4424370061333522, 0.3708511650436959, 0.4551343032566526, 0.2748343405904143, 0.22452183524522543, 0.20186352946257047, 0.16232137716499528, 0.1259922663383409, 0.1615674198680147, 0.15289844737629416, 0.12132552966917991, 0.06015011468395385, 0.06792101594525256, 0.10930383488126696, 0.10902569537616233, 0.07965537234423714, 0.06287953215867469, 0.07618194736558329, 0.04815025482449651, 0.048560120740651316, NaN], \\"beta1\\": [0.0, 0.0, 0.0, 0.0, 0.3926184941215534, 0.9219957845105672, 0.0, 0.10214692372523075, 0.4789140580456156, 0.8197346588839601, 0.0, 0.025799226824790834, 0.3647418128645487, 0.5721070219950798, 0.8181558381669438, 0.0, 0.35445060608812834, 0.4000965092918508, 0.4927909805267271, 0.6548989237156498, 0.7769507091170393, 0.0, 0.29251523256652084, 0.28549566818797606, 0.592553556347188, 0.5576934139435734, 0.6817201921586137, 0.8163820339273872, 0.0, 0.4147332992550475, 0.460177344064391, 0.5738578607038743, 0.37492390353725413, 0.4607323288241859, 0.509807610503288, 0.6619456170545638, 0.34772563023431324, 0.7353255450795453, 0.8384325801319853, 0.7604848469753155, 0.8786744703308201, 0.5133108057202137, 0.6564197384787078, 0.4267799495993852, 0.6184552488281203, 0.5705998302076256, 0.8286395700832462, 0.49426833985049434, 0.6279091242565378, 0.6527313799882105, NaN], \\"beta2\\": [0.0, 0.0, 0.0, 0.0, 0.0, 0.003764724839674331, 0.0, 0.0, 0.0, 0.04926194195002498, 0.0, 0.0, 0.0, 0.15560030352355556, 0.08203269119292825, 0.0, 0.0, 0.05528376853816371, 0.1303648759391504, 0.1788849306651267, 0.1731347580597302, 0.0, 0.0, 0.30724500394918164, 0.0, 0.23797310199151364, 0.2516491054460742, 0.15399295933422713, 0.0, 0.0, 0.09738564980225695, 0.055290974252429695, 0.16994179320609326, 0.2644333305853997, 0.26567055425148656, 0.13619085348286575, 0.48995299260069153, 0.13868218858211373, 0.0, 0.08661670564839039, 0.0, 0.42653907959583237, 0.27565924557603955, 0.46391621551934775, 0.27251905579571745, 0.34974479744813725, 0.10848089775807905, 0.42954971278392234, 0.3239406209189658, 0.2987084992711381, NaN], \\"conjugacy_prev\\": [NaN, NaN, NaN, 0.009131125464184143, -2.8919736861073706e-18, -0.012511873440793726, NaN, NaN, 9.752112149740471e-17, -0.01421060232796462, NaN, NaN, -5.180557641603795e-17, -0.03721667928957669, -0.04232333304460143, NaN, NaN, -9.054458857724459e-05, -0.00599182744972153, 0.006162339055411461, -0.007986557804704938, NaN, NaN, 0.004880909102529844, 1.5231514511258254e-16, 0.00467255658374935, -0.007193655299715711, -0.004901760723609507, NaN, NaN, 0.0007232929946526874, 7.544979653403968e-05, -6.704952251526902e-05, -0.0013564303708551416, -0.00012783640107913652, 0.0007741767294959595, 0.001178485086821133, 0.0003441741181014419, -4.5556893021176925e-17, -0.00010649996786908598, -1.816473036328716e-16, -0.0020837002783275784, -0.0002144675944955004, -0.0004330480114696872, -8.092878046182701e-05, -0.00020829470406362387, -0.00046207405415329126, -0.0014479060710527714, -9.254720616895141e-05, -0.00035889450234194545, NaN], \\"conjugacy_prev2\\": [NaN, NaN, NaN, 0.15050043269340133, 0.021601664936027155, -0.2642675529346801, NaN, NaN, 0.28017408784291503, -0.05927016099392479, NaN, NaN, 0.2803824954703134, -0.08575523367277281, -0.1096874048538775, NaN, NaN, -0.0005017604791585371, -0.020422748016858314, 0.012649178133339902, -0.013055424727827317, NaN, NaN, 0.006768499271202243, 0.12252034341003173, 0.013370079227946679, -0.011095890630428802, -0.006554309703401488, NaN, NaN, 0.002301386623972259, 0.0005818622776930699, -0.0002586772162807555, -0.0033470987061794614, -0.00020690333696106846, 0.003715793060792712, 0.0025013983969461003, 0.0011484359452320892, 0.04631206018352924, -0.001260021423547524, 0.03392168236437078, -0.004718828010445438, -0.0002238746805746971, -0.0019105744808382787, -0.0003513939430952122, -0.0006232238578322755, -0.001906964014079279, -0.003441148244840033, -0.0002631320716060108, -0.0010830181436810766, NaN], \\"hessian_drift\\": [NaN, NaN, NaN, 0.07677956339453926, -0.06916185755930719, -0.289376248048675, NaN, NaN, -0.02816755779577692, -0.03801781620375066, NaN, NaN, -0.09102804077988096, -0.07210923042491592, -0.0804766525163474, NaN, NaN, -0.0009016798345824896, -0.021791667020364055, 0.011136820823544244, -0.010569437978811937, NaN, NaN, 0.005878800227037329, 0.007459167322626112, 0.010803265852573003, -0.008479651188456349, -0.008888264229722404, NaN, NaN, 0.0031040050432984195, 0.001185455735074406, -0.00045607283930179676, -0.0029182540810906614, -0.0002497897074662279, 0.006245668916215601, 0.0019077997364233428, 0.0013943363656468942, 0.004679392374055461, -0.0013019729187760843, 0.003151011683804278, -0.00281699871434828, -0.0006718164508872158, -0.003303396410221019, -0.0005017025925719387, -0.0004350362038069228, -0.0028935715748487773, -0.0046576551129433865, -0.00031388510170638476, -0.0012176889884835357, NaN], \\"bfw_clamped\\": [NaN, NaN, NaN, true, true, false, NaN, NaN, true, false, NaN, NaN, true, false, false, NaN, NaN, false, false, false, false, NaN, NaN, false, true, false, false, false, NaN, NaN, false, false, false, false, false, false, false, false, true, false, true, false, false, false, false, false, false, false, false, false, NaN]}, \\"setup\\": {\\"Algorithm\\": \\"bfw\\", \\"Classes\\": {\\"car\\": {\\"network mode\\": \\"c\\", \\"cost\\": \\"free_flow_time\\", \\"PCE\\": 1.0, \\"Value-of-time\\": 1.0, \\"Fixed cost field\\": null, \\"Fixed cost multiplier\\": 1.0, \\"matrix_totals\\": {\\"matrix\\": 360600.0}}}, \\"Computer name\\": \\"runnervm0hi8g\\", \\"Maximum iterations\\": 1000, \\"Procedure ID\\": \\"f8de214a22d74fe791b7615f764e43b8\\", \\"Target RGap\\": 0.001}}"', timestamp='2026-11-19 08:27:41.247695', description='')

And save the skims

assig.save_skims("base_year_assignment_skims", which_ones="all", format="omx")
['base_year_assignment_skims_car.omx']

Trip distribution#

First, let’s have a function to plot the Trip Length Frequency Distribution.

from math import floor, log10

import matplotlib.pyplot as plt
def plot_tlfd(demand, skim, name):
    plt.clf()
    b = floor(log10(skim.shape[0]) * 10)
    n, bins, patches = plt.hist(
        np.nan_to_num(skim.flatten(), 0),
        bins=b,
        weights=np.nan_to_num(demand.flatten()),
        density=False,
        facecolor="g",
        alpha=0.75,
    )

    plt.xlabel("Trip length")
    plt.ylabel("Probability")
    plt.title(f"Trip-length frequency distribution for {name}")
    return plt

Calibration#

We will calibrate synthetic gravity models using the skims for free_flow_time that we just generated

import numpy as np

from aequilibrae.distribution import GravityCalibration

Load the demand matrix core for computation.

demand = proj_matrices.get_matrix("demand.omx", subset=["matrix"])

We also need the skims we just saved into our project. We select the congested-time core.

imped_core = "free_flow_time_final"
imped = proj_matrices.get_matrix("base_year_assignment_skims_car.omx", subset=[imped_core])

# Where ``free_flow_time_final`` is the congested time for the last iteration.
# Use 75% of the closest zone for intrazonal impedance.
impedance_values = imped[imped_core].copy()
np.fill_diagonal(impedance_values, 0)
intrazonals = np.amin(
    impedance_values, where=impedance_values > 0, initial=impedance_values.max(), axis=1
)
intrazonals *= 0.75
np.fill_diagonal(impedance_values, intrazonals)

# Store the prepared core as a separate project matrix.
del imped[imped_core]
imped["final_time_with_intrazonals"] = impedance_values
proj_matrices.save_matrix(
    "base_year_assignment_skims_car_intrazonals.omx",
    matrix=imped,
)

Let’s calibrate our Gravity Model

for function in ["power", "expo"]:
    gc = GravityCalibration(matrix=demand, impedance=imped, function=function, nan_as_zero=True)
    gc.calibrate()
    model = gc.model
    # We save the model
    model.save(join(fldr, f"{function}_model.mod"))

    result_core = next(iter(gc.result_matrix))
    _ = plot_tlfd(
        gc.result_matrix[result_core], imped["final_time_with_intrazonals"], f"{function} model"
    )

    # We can save the result of applying the model as well
    # We can also save the calibration report
    with open(join(fldr, f"{function}_convergence.log"), "w") as otp:
        for r in gc.report:
            otp.write(r + "\n")
Trip-length frequency distribution for expo model

And let’s plot a trip length frequency distribution for the demand itself

plt = plot_tlfd(demand["matrix"], imped["final_time_with_intrazonals"], "demand")
plt.show()
Trip-length frequency distribution for demand

Forecast#

We create a set of ‘future’ vectors using some random growth factors. We apply the model for inverse power, as the trip frequency length distribution (TFLD) seems to be a better fit for the actual one.

from aequilibrae.distribution import GravityApplication, Ipf, SyntheticGravityModel

Compute future vectors#

First thing to do is to compute the future vectors from our matrix.

origins = np.sum(demand["matrix"], axis=1)
destinations = np.sum(demand["matrix"], axis=0)

# Then grow them with some random growth between 0 and 10%, and balance them
orig = origins * (1 + np.random.rand(origins.shape[0]) / 10)
dest = destinations * (1 + np.random.rand(origins.shape[0]) / 10)
dest *= orig.sum() / dest.sum()

vectors = pd.DataFrame({"origins": orig, "destinations": dest}, index=demand.index[:])

IPF for the future vectors#

Let’s balance the future vectors. The output of this step is going to be used later in the traffic assignment for future year.

args = {
    "matrix": demand,
    "vectors": vectors,
    "column_field": "destinations",
    "row_field": "origins",
    "nan_as_zero": True,
}

ipf = Ipf(project=project, **args)
ipf.fit()

Save the balanced matrix into the project’s matrix folder

ipf.save_to_project(file_name="demand_ipfd.omx")

Impedance#

Let’s get the base-year assignment skim for car we created before and prepare it for computation

imped = proj_matrices.get_matrix(
    "base_year_assignment_skims_car_intrazonals.omx", subset=["final_time_with_intrazonals"]
)

If we wanted the main diagonal to not be considered…

# np.fill_diagonal(imped["final_time_with_intrazonals"], np.nan)

Now we apply the Synthetic Gravity model

for function in ["power", "expo"]:
    model = SyntheticGravityModel()
    model.load(join(fldr, f"{function}_model.mod"))

    args = {
        "impedance": imped,
        "vectors": vectors,
        "row_field": "origins",
        "model": model,
        "column_field": "destinations",
        "nan_as_zero": True,
    }

    gravity = GravityApplication(project=project, **args)
    gravity.apply()

    # We get the output matrix and save it to OMX too,
    gravity.save_to_project(file_name=f"demand_{function}_modeled.omx")

We inspect the matrix files and verify that the new matrices are indeed there

proj_matrices.get_available_matrices_and_metadata()
{'demand_ipfd.omx': {'procedure': 'Iterative Proportional fitting', 'procedure_id': '109f9105fe1e4bb0b21e64ecbaae5b41', 'timestamp': '2026-11-19 08:27:41.676540', 'description': 'Original data imported to OMX format'}, 'base_year_assignment_skims_car.omx': {'procedure': 'Traffic Assignment', 'procedure_id': 'f8de214a22d74fe791b7615f764e43b8', 'timestamp': '2026-11-19 08:27:41.247695', 'description': 'Skimming for assignment procedure. Class car'}, 'demand_expo_modeled.omx': {'procedure': 'Synthetic gravity trip distribution', 'procedure_id': '38f1b69b66ed44cab51a5eb1a6d40310', 'timestamp': '2026-11-19 08:27:41.699080', 'description': 'Synthetic gravity trip distribution. EXPO'}, 'base_year_assignment_skims_car_intrazonals.omx': {'procedure': 'Traffic Assignment', 'procedure_id': 'f8de214a22d74fe791b7615f764e43b8', 'timestamp': '2026-11-19 08:27:41.247695', 'description': 'Skimming for assignment procedure. Class car'}, 'demand_power_modeled.omx': {'procedure': 'Synthetic gravity trip distribution', 'procedure_id': 'ef9ed344582b4fb69f70563630250393', 'timestamp': '2026-11-19 08:27:41.689611', 'description': 'Synthetic gravity trip distribution. POWER'}, 'skims.omx': {'procedure': None, 'procedure_id': None, 'timestamp': None, 'description': 'Example skim'}, 'demand.omx': {'procedure': None, 'procedure_id': None, 'timestamp': '2020-11-24 08:47:18', 'description': 'Original data imported to OMX format'}, 'demand_mc.omx': {'procedure': None, 'procedure_id': None, 'timestamp': '2021-02-24 00:51:35', 'description': None}}