{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Running IPF without an AequilibraE model\n\nIn this example, we show you how to use AequilibraE's IPF function without a model.\nThis is a complement to the application in `example_usage_forecasting`.\n\nLet's consider that you have an OD-matrix, the future production and future attraction values.\n\n*How would your trip distribution matrix using IPF look like?*\n\nThe data used in this example comes from Table 5.6 in \n[Ort\u00fazar & Willumsen (2011)](https://www.wiley.com/en-us/Modelling+Transport%2C+4th+Edition-p-9780470760390).\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n  * `all_about_aeq_matrices`\n  * :doc:`../../distribution_procedures/IPF_benchmark`\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. seealso::\n    Several functions, methods, classes and modules are used in this example:\n\n    * :func:`aequilibrae.matrix.AequilibraeMatrix`\n    * :func:`aequilibrae.distribution.Ipf`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nfrom os.path import join\nfrom tempfile import gettempdir\n\nimport numpy as np\nimport pandas as pd\n\nfrom aequilibrae.distribution import Ipf\nfrom aequilibrae.matrix import AequilibraeMatrix"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "folder = gettempdir()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "matrix = np.array([[5, 50, 100, 200], [50, 5, 100, 300], [50, 100, 5, 100], [100, 200, 250, 20]], dtype=\"float64\")\nfuture_prod = np.array([400, 460, 400, 702], dtype=\"float64\")\nfuture_attr = np.array([260, 400, 500, 802], dtype=\"float64\")\n\nnum_zones = matrix.shape[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mtx = AequilibraeMatrix()\nmtx.create_empty(file_name=join(folder, \"matrix.aem\"), zones=num_zones)\nmtx.index[:] = np.arange(1, num_zones + 1)[:]\nmtx.matrices[:, :, 0] = matrix[:]\nmtx.computational_view()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "args = {\n    \"entries\": mtx.index.shape[0],\n    \"field_names\": [\"productions\", \"attractions\"],\n    \"data_types\": [np.float64, np.float64],\n    \"file_path\": join(folder, \"vectors.aem\"),\n}\n\nvectors = pd.DataFrame({\"productions\": future_prod, \"attractions\": future_attr}, index=mtx.index)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "args = {\n    \"matrix\": mtx,\n    \"vectors\": vectors,\n    \"row_field\": \"productions\",\n    \"column_field\": \"attractions\",\n    \"nan_as_zero\": True,\n}\nfratar = Ipf(**args)\nfratar.fit()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fratar.output.matrix_view"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for line in fratar.report:\n    print(line)"
      ]
    }
  ],
  "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.10.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}