Route Choice with Turn Penalties#

In this example, we show how route choice accounts for turn penalties during choice set generation and path-size logit calculations.

See also

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

Imports

import numpy as np
import pandas as pd

from aequilibrae.routing import Network
from aequilibrae.route_choice import RouteChoice

Create a small network with two routes from node 10 to node 40.

links = pd.DataFrame(
    {
        "link_id": [71, 12, 55, 24],
        "a_node": [10, 20, 10, 30],
        "b_node": [20, 40, 30, 40],
        "direction": [1, 1, 1, 1],
        "time": [1.0, 1.0, 2.0, 2.0],
    }
)

Add a five-unit turn penalty to the direct route before building the graph.

turns = pd.DataFrame(
    {
        "from_node": [10],
        "via_node": [20],
        "to_node": [40],
        "penalty": [5.0],
    }
)
network = Network(links, centroids=np.array([10, 20, 30, 40]), turn_restrictions=turns)
graph = network.build_graph("c", cost="time", compact=True, block_centroid_flows=False)

Generate the route choice set. The direct route has a cost of 2 plus the turn penalty, so the alternative route with a cost of 4 is more likely.

rc = RouteChoice(graph)
rc.set_choice_set_generation("bfsle", max_routes=2, max_depth=5)
rc.execute_single(10, 40, demand=1.0)

route_choice_results = rc.get_results()
print(route_choice_results)
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]

   origin id  destination id  cost  ...  probability route set route directions
0         10              40   7.0  ...     0.047426  [71, 12]           [1, 1]
1         10              40   4.0  ...     0.952574  [55, 24]           [1, 1]

[2 rows x 8 columns]

We can also recalculate path-size logit results for routes imported from another source. Route link IDs use a positive sign for the AB direction and a negative sign for the BA direction.

imported_routes = pd.DataFrame(
    {
        "origin id": [10, 10],
        "destination id": [40, 40],
        "route set": [[71, 12], [55, 24]],
        "route directions": [[1, 1], [1, 1]],
    }
)

recompute_psl validates origin and destination endpoints, link connectivity and turn restrictions before calculating costs, overlap and probabilities.

imported_results = rc.recompute_psl(imported_routes)
print(imported_results)
   origin id  destination id route set  ...  mask  path overlap  probability
0         10              40  [71, 12]  ...  True           1.0     0.047426
1         10              40  [55, 24]  ...  True           1.0     0.952574

[2 rows x 8 columns]

Total running time of the script: (0 minutes 0.535 seconds)

Gallery generated by Sphinx-Gallery