Note
Go to the end to download the full example code.
Running IPF with NumPy array#
In this example, we show how to use aequilibrae.distribution.ipf_core
, a high-performance
alternative for all those who want to (re)balance values within a matrix making direct use of
growth factors. ipf_core
was built to suit countless applications rather than being limited
to trip distribution.
We demonstrate the usage of ipf_core
with a 4x4 matrix with 64-bit data, which is indeed very
small. Additionally, a more comprehensive discussion of the algorithm’s performance
with a 32-bit or 64-bit seed matrices is provided in ../IPF_benchmark.
The data used in this example comes from Table 5.6 in Ortúzar & Willumsen (2011).
References
See also
Several functions, methods, classes and modules are used in this example:
aequilibrae.distribution.ipf_core()
# Imports
import numpy as np
from aequilibrae.distribution.ipf_core import ipf_core
matrix = np.array([[5, 50, 100, 200], [50, 5, 100, 300], [50, 100, 5, 100], [100, 200, 250, 20]], dtype="float64")
future_prod = np.array([400, 460, 400, 702], dtype="float64")
future_attr = np.array([260, 400, 500, 802], dtype="float64")
Given our use of default parameter values in the other application of IPF, we should set tolerance value to obtain the same result.
num_iter, gap = ipf_core(matrix, future_prod, future_attr, tolerance=0.0001)
Let’s print our updated matrix
matrix
array([[ 5.19523253, 43.60117896, 97.1907419 , 254.02026689],
[ 44.70942645, 3.75225496, 83.64095928, 327.90930057],
[ 76.67583276, 128.70094592, 7.17213428, 187.45277887],
[133.41950826, 223.94562016, 311.99616454, 32.61765367]])
Notice that the matrix value was updated, and results are the same as in Running IPF without an AequilibraE model
- and this is no coincidence. Under the hood, when we call aequilibrae.distribution.Ipf
, we
are actually calling the ipf_core
method.
Total running time of the script: (0 minutes 0.002 seconds)