{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Route Choice with Turn Penalties\n\nIn this example, we show how route choice accounts for turn penalties during\nchoice set generation and path-size logit calculations.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n  * :doc:`../../route_choice`\n  * `turn_restrictions`\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. seealso::\n    Several functions, methods, classes and modules are used in this example:\n\n    * :class:`aequilibrae.routing.Network`\n    * :func:`aequilibrae.route_choice.RouteChoice`\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Imports\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport pandas as pd\n\nfrom aequilibrae.routing import Network\nfrom aequilibrae.route_choice import RouteChoice"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a small network with two routes from node 10 to node 40.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "links = pd.DataFrame(\n    {\n        \"link_id\": [71, 12, 55, 24],\n        \"a_node\": [10, 20, 10, 30],\n        \"b_node\": [20, 40, 30, 40],\n        \"direction\": [1, 1, 1, 1],\n        \"time\": [1.0, 1.0, 2.0, 2.0],\n    }\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add a five-unit turn penalty to the direct route before building the graph.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "turns = pd.DataFrame(\n    {\n        \"from_node\": [10],\n        \"via_node\": [20],\n        \"to_node\": [40],\n        \"penalty\": [5.0],\n    }\n)\nnetwork = Network(links, centroids=np.array([10, 20, 30, 40]), turn_restrictions=turns)\ngraph = network.build_graph(\"c\", cost=\"time\", compact=True, block_centroid_flows=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate the route choice set. The direct route has a cost of 2 plus the\nturn penalty, so the alternative route with a cost of 4 is more likely.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rc = RouteChoice(graph)\nrc.set_choice_set_generation(\"bfsle\", max_routes=2, max_depth=5)\nrc.execute_single(10, 40, demand=1.0)\n\nroute_choice_results = rc.get_results()\nprint(route_choice_results)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can also recalculate path-size logit results for routes imported from\nanother source. Route link IDs use a positive sign for the AB direction and\na negative sign for the BA direction.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "imported_routes = pd.DataFrame(\n    {\n        \"origin id\": [10, 10],\n        \"destination id\": [40, 40],\n        \"route set\": [[71, 12], [55, 24]],\n        \"route directions\": [[1, 1], [1, 1]],\n    }\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "``recompute_psl`` validates origin and destination endpoints, link\nconnectivity and turn restrictions before calculating costs, overlap and\nprobabilities.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "imported_results = rc.recompute_psl(imported_routes)\nprint(imported_results)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}