{ "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 compliment to the application in `Trip Distribution `.\n\nLet's consider that you have an OD-matrix, the future production and future attraction values.\n*How would your trip distribution matrix using IPF look like?*\nThe data used in this example comes from Table 5.6 in [ORW2011]_.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imports\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom aequilibrae.distribution import Ipf\nfrom os.path import join\nfrom tempfile import gettempdir\nfrom aequilibrae.matrix import AequilibraeMatrix, AequilibraeData" ] }, { "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 = AequilibraeData()\nvectors.create_empty(**args)\n\nvectors.productions[:] = future_prod[:]\nvectors.attractions[:] = future_attr[:]\n\nvectors.index[:] = mtx.index[:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "args = {\n \"matrix\": mtx,\n \"rows\": vectors,\n \"row_field\": \"productions\",\n \"columns\": vectors,\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.9.18" } }, "nbformat": 4, "nbformat_minor": 0 }