Linear Assignment Problem Solvers
lapx is a Python library providing efficient linear assignment problem solvers, including both single and batch solvers, based on the Jonker-Volgenant algorithm. It extends and enhances Tomas Kazmar's original `lap` library, offering improved stability, performance, and broader platform support (Windows, Linux, macOS) for Python 3.7+. The library is actively maintained, with the current version being 0.9.4, and receives frequent minor and patch releases.
Warnings
- gotcha The package `lapx` should be imported using `import lap`. Attempting to `import lapx` will result in an `ModuleNotFoundError`.
- breaking Installing both `lap` (original package) and `lapx` simultaneously will lead to conflicts, as both provide the `lap` import. The package installed last will override the other.
- gotcha Since v0.7.1, the `lapjvs()` function defaults `prefer_float32` to `True`. While this generally improves speed without altering return results, users expecting `float64` precision by default for intermediate calculations (as in older versions) should explicitly set `prefer_float32=False`.
- gotcha Starting from v0.8.0, dedicated batch processing functions (e.g., `lapjvx_batch`, `lapjvs_batch`) were introduced. While older single-assignment functions might still process batches in some contexts, using the explicit `_batch` functions is recommended for optimal performance and correct handling of batch inputs.
Install
-
pip install lapx
Imports
- lap
import lap
- lapjv
import lap result = lap.lapjv(...)
- lapjvs
import lap result = lap.lapjvs(...)
Quickstart
import lap
import numpy as np
# Example cost matrix
cost_matrix = np.array([[1, 2, 3],
[3, 1, 2],
[2, 3, 1]])
# Solve the linear assignment problem using lapjv
cost, x, y = lap.lapjv(cost_matrix)
print(f"Optimal assignment cost: {cost}")
print(f"Row assignments (x, maps row index to column index): {x}")
print(f"Column assignments (y, maps column index to row index): {y}")