Route Choice#

In this example, we show how to perform route choice set generation using BFSLE and Link penalisation, for a city in La Serena Metropolitan Area in Chile.

References

See also

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

  • aequilibrae.routing.graph()

  • aequilibrae.route_choice.route_choice()

  • aequilibrae.matrix.aequilibrae_matrix()

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

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, "coquimbo")
Environment variable 'AEQ_SPATIALITE_DIR' was provided (C:\path\to\existing\download), but mod_spatialite could not be loaded from this directory. Trying system path
Environment variable 'AEQ_SPATIALITE_DIR' was provided (C:\path\to\existing\download), but mod_spatialite could not be loaded from this directory. Trying system path
# We'll also apply a basic logging configuration.

basic_config()
<AequilibraETQDMStreamHandler (NOTSET)>

Model parameters#

import numpy as np

We’ll set the parameters for our route choice model. These are the parameters that will be used to calculate the utility of each path. In our example, the utility is equal to \(distance * theta\), and the path overlap factor (PSL) is equal to \(beta\).

# Distance factor
theta = 0.00011

# PSL parameter
beta = 1.1

Let’s select a set of nodes of interest

nodes_of_interest = (71645, 74089, 77011, 79385)

Let’s load the source network snapshot

network = project.road.build_network()
# Several fields in the project are filled with NaNs.
# This is true, but we won't use those fields.

We also see what modes are available

network.modes
('b', 'c', 't', 'w')

Add the utility field to the source network before graph preparation.

network.links["utility"] = network.links.distance * theta
graph = network.build_graph(
    mode="c", cost="utility", centroids=np.array(nodes_of_interest), compact=True
)

Mock demand matrix#

We’ll create a mock demand matrix with demand 1 for every zone and prepare it for computation.

from aequilibrae.matrix import AequilibraeMatrix

mat = AequilibraeMatrix(index=graph.centroids, index_name="zones")
mat["demand"] = np.full((graph.num_zones, graph.num_zones), 10.0)
mat["5x demand"] = np.full((graph.num_zones, graph.num_zones), 50.0)

Create plot function#

Before dive into the Route Choice class, let’s define a function to plot assignment results.

import folium
def plot_results(link_loads):

    link_loads = link_loads[link_loads["demand_tot"] > 0]
    max_load = link_loads["demand_tot"].max()
    links = project.tables.links.data
    loaded_links = links.merge(link_loads, on="link_id", how="inner")

    loads_lyr = folium.FeatureGroup("link_loads")

    # Maximum thickness we would like is probably a 10, so let's make sure we don't go over that
    factor = 10 / max_load

    return loaded_links.explore(
        color="red",
        style_kwds={
            "style_function": lambda x: {
                "weight": x["properties"]["demand_tot"] * factor,
            }
        },
    )

Route Choice class#

Here we’ll construct and use the Route Choice class to generate our route sets

from aequilibrae.route_choice import RouteChoice

Graph construction has already prepared the compressed-link to network-link mapping. RouteChoice reuses that mapping and snapshots the routing costs.

rc = RouteChoice(graph)

# Let's check the default parameters for the Route Choice class
print(rc.default_parameters)
{'generic': {'seed': 0, 'max_routes': 0, 'max_depth': 0, 'max_misses': 100, 'penalty': 1.01, 'disutility_cutoff_constant': inf, 'disutility_cutoff_coefficient': inf, 'beta': 1.0, 'store_results': True, 'a_star': False, 'heuristic': 'euclidean', 'heuristic_scale': None}, 'link-penalisation': {}, 'bfsle': {'penalty': 1.0}}

Let’s add the demand. If it’s not provided, link loading cannot be performed.

It is highly recommended to set either max_routes or max_depth to prevent runaway results.

rc.set_choice_set_generation("bfsle", max_routes=5)

We can now perform a computation for single OD pair if we’d like. Here we do one between the first and last centroid as well as an assignment.

results = rc.execute_single(77011, 74089, demand=1.0)
print(results[0])
0/1 ODs processed                                 :   0%|          | 0/1 [00:00<?, ?it/s]
0/1 ODs processed                                 : 100%|██████████| 1/1 [00:00<00:00,  2.01it/s]
1/1 ODs processed                                 : 100%|██████████| 1/1 [00:00<00:00,  2.01it/s]

(array([24222, 30332, 30333, 10435, 30068, 30069, 14198, 14199, 31161,
       30928, 31622, 24112, 24113,  6221,  6222,  6223,  6224,  6225,
        6226,  6227,  6228,  6229,  6230,  6231,  6232,  6233,  6234,
        6235,  3611,  3610,  3609,  3608,  3607,  3606,  3605,  3604,
        3603,  3602,  3601,  3600,  3599,  3598, 23740, 23739, 23738,
       33040, 33041,  5152,  5153,  5154,  5155,  5156,  5157, 19979,
       19977, 19976, 19975, 19974, 19973, 19972, 19971, 19970, 22082,
       22080,  5351,  5352,  2280,  2281,  2282,   575,   576,   577,
         578,   579,   536,   537,   538,   539,   540,   541, 15406,
       15407, 15408,   553,   552,   633,   634,   635,   630,   631,
         632,   623,   624,   625,   626,   471,  5359,  5360, 34690,
       34689,   661,  5361,  5362,   690,   687,   688,  1444,  1445,
         472, 14364, 18042, 18043, 18044, 18045, 18046, 18047, 18048,
       18049,   697,   698,   709, 19608, 26312, 26311, 26310, 26309,
       26308, 26307, 26306, 26305, 26304, 26303, 26302, 26301, 26300,
       34079, 34147, 29962, 26422, 26421, 26420,   765,   764,   763,
         762,   761,   760,   736, 10973, 10974, 10975,   725, 10972,
         727,   728, 26424,   733,   734, 29899, 20970, 20969, 20968,
       20967, 20966, 20965, 20964, 20963, 20962,  9584,  9583, 20981,
       21398, 20982, 34208,    35,    36,    59,    60,    61, 22363,
       22364, 22365, 22366, 22367, 28958, 28959, 28960, 28961, 28962,
       28805, 28806, 28807, 28808, 28809, 28810, 28827, 28828, 28829,
       28830, 28874], dtype=uint64), array([-1,  1,  1, -1,  1,  1,  1,  1,  1,  1, -1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, -1, -1, -1, -1, -1, -1,
       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1,  1,  1,  1,  1,  1,
        1,  1,  1, -1, -1, -1, -1, -1, -1, -1, -1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1,
        1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1,  1,  1,  1,  1, -1,
        1,  1,  1,  1,  1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1,  1,
       -1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1], dtype=int8))

Because we asked it to also perform an assignment we can access the various results from that.

res = rc.get_results()
res.head()
origin id destination id cost mask path overlap probability route set route directions
0 77011 74089 1.890631 True 0.317463 0.200271 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
1 77011 74089 1.909910 True 0.320813 0.198520 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
2 77011 74089 1.896079 True 0.288041 0.180723 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
3 77011 74089 1.930091 True 0.396769 0.240617 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...
4 77011 74089 1.886882 True 0.284053 0.179868 [24222, 30332, 30333, 10435, 30068, 30069, 141... [-1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, ...


plot_results(rc.get_load_results())
Make this Notebook Trusted to load map: File -> Trust Notebook


Batch operations#

To perform a batch operation we need to prepare the object first. We can either provide a list of tuple of the OD pairs we’d like to use, or we can provided a 1D list and the generation will be run on all permutations.

Now we can perform a batch computation with an assignment

rc.execute(perform_assignment=True)
res = rc.get_results()
res.head()
0/16 ODs processed                                :   0%|          | 0/16 [00:00<?, ?it/s]
0/16 ODs processed                                : 100%|██████████| 16/16 [00:00<00:00, 32.08it/s]
16/16 ODs processed                               : 100%|██████████| 16/16 [00:00<00:00, 32.08it/s]
origin id destination id cost mask path overlap probability route set route directions
0 71645 74089 0.607017 True 0.382436 0.207157 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...
1 71645 74089 0.606968 True 0.246758 0.133670 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...
2 71645 74089 0.622079 True 0.542334 0.289380 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...
3 71645 74089 0.611944 True 0.442367 0.238443 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, ...
4 71645 74089 0.604384 True 0.241848 0.131349 [19550, 19549, 19548, 19547, 19546, 19545, 209... [-1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1,...


Since we provided a matrix initially we can also perform link loading based on our assignment results.

demand_ab demand_ba 5x demand_ab 5x demand_ba demand_tot 5x demand_tot
link_id
1 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.0 0.0
12 0.0 0.0 0.0 0.0 0.0 0.0
13 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ...
34938 0.0 0.0 0.0 0.0 0.0 0.0
34939 0.0 0.0 0.0 0.0 0.0 0.0
34940 0.0 0.0 0.0 0.0 0.0 0.0
34941 0.0 0.0 0.0 0.0 0.0 0.0
34942 0.0 0.0 0.0 0.0 0.0 0.0

19983 rows × 6 columns



We can plot these as well

plot_results(rc.get_load_results())
Make this Notebook Trusted to load map: File -> Trust Notebook