{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Checking AequilibraE's log\n\nAequilibraE's log is a very useful tool to get more information about\nwhat the software is doing under the hood.\n\nInformation such as Traffic Class and Traffic Assignment stats, and Traffic Assignment\noutputs. If you have created your project's network from OSM, you will also find\ninformation on the number of nodes, links, and the query performed to obtain the data.\n\nIn this example, we'll use Sioux Falls data to check the logs, but we strongly encourage\nyou to go ahead and download a place of your choice and perform a traffic assignment!\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nfrom uuid import uuid4\nfrom tempfile import gettempdir\nfrom os.path import join\nfrom aequilibrae.examples import create_example\nfrom aequilibrae.traffic import TrafficAssignment, TrafficClass\nfrom aequilibrae.traffic.vdf import bpr"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# We create an empty project on an arbitrary folder\nfldr = join(gettempdir(), uuid4().hex)\nproject = create_example(fldr)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "network = project.road.build_network()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We get our demand matrix from the project and create a computational view\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "proj_matrices = project.matrices\ndemand = proj_matrices.get_matrix(\"demand.omx\", subset=[\"matrix\"])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's perform our traffic assignment\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "assigclass = TrafficClass(\n    name=\"car\",\n    mode=\"c\",\n    cost=\"free_flow_time\",\n    demand=demand,\n    skimming=[\"free_flow_time\", \"distance\"],\n    graph_options={\"block_centroid_flows\": False},\n)\n\n\nassig = TrafficAssignment(\n    network=network,\n    project=project,\n    classes=[assigclass],\n    vdf=bpr,\n    name_mapping={\"alpha\": 0.15, \"beta\": 4.0},\n    time_field=\"free_flow_time\",\n    capacity_field=\"capacity\",\n    algorithm=\"bfw\",\n)\nassig.execute(max_iterations=50, rgap=0.001)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "with open(join(fldr, \"aequilibrae.log\")) as file:\n    for idx, line in enumerate(file):\n        print(idx + 1, \"-\", line)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The log records the Traffic Class specifications, including the network mode,\ncost field, PCE, value of time, and matrix totals.\n\nThe log then shows the Traffic Assignment specifications.\nWe can see that the VDF parameters, VDF function, capacity and time fields, algorithm,\nmaximum number of iterations, and target gap are just like the ones we set previously.\nThe only information that might be new to you is the number of cores used for computation.\nIf you haven't set any, AequilibraE is going to use the largest number of CPU threads\navailable.\n\nEach iteration records its relative gap and step size. ``assig.report()``\nreturns the same convergence data as a DataFrame.\n\nIn case you execute a new traffic assignment using different classes or changing the\nparameters values, these new specification values would be stored in the log file as well\nso you can always keep a record of what you have been doing. One last reminder is that\nif we had created our project from OSM, the lines on top of the log would have been\ndifferent to display information on the queries done to the server to obtain the data.\n\nLog image by [OSRS Wiki](https://oldschool.runescape.wiki/index.php?curid=66905#)\n\n"
      ]
    }
  ],
  "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
}