{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Running IPF with NumPy array\n\nIn this example, we show how to use ``aequilibrae.distribution.ipf_core``, a high-performance \nalternative for all those who want to (re)balance values within a matrix making direct use of\ngrowth factors. ``ipf_core`` was built to suit countless applications rather than being limited\nto trip distribution.\n\nWe demonstrate the usage of ``ipf_core`` with a 4x4 matrix with 64-bit data, which is indeed very\nsmall. Additionally, a more comprehensive discussion of the algorithm's performance\nwith a 32-bit or 64-bit seed matrices is provided in :doc:`../IPF_benchmark`.\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  * :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.distribution.ipf_core`\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Imports\nimport numpy as np\n\nfrom aequilibrae.distribution.ipf_core import ipf_core"
      ]
    },
    {
      "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\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Given our use of default parameter values in the other application of IPF, we should set\n`tolerance` value to obtain the same result.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "num_iter, gap = ipf_core(matrix, future_prod, future_attr, tolerance=0.0001)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's print our updated matrix\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "matrix"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Notice that the matrix value was updated, and results are the same as in `plot_ipf_without_model`\n- and this is no coincidence. Under the hood, when we call ``aequilibrae.distribution.Ipf``, we \nare actually calling the ``ipf_core`` method.\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.10.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}