Linear Assignment Problem solver (LAPJV/LAPMOD)

0.5.13 · active · verified Sun Apr 12

The 'lap' library provides a fast Python solver for the Linear Assignment Problem (LAP) using the Jonker-Volgenant algorithm for both dense (LAPJV) and sparse (LAPMOD) cost matrices. It's implemented from scratch based on original research papers. Currently at version 0.5.13, it sees active but infrequent releases, usually a few per year, ensuring ongoing maintenance and compatibility updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `lap.lapjv` for dense cost matrices and `lap.lapmod` for sparse matrices. `lapjv` can handle non-square matrices directly with `extend_cost=True`. `lapmod` is typically optimized for large, sparse *square* matrices; for non-square sparse problems, additional pre-processing or wrapper libraries might be needed.

import lap
import numpy as np

# Example for LAPJV (dense matrix)
# C is the cost matrix, e.g., representing costs for assigning rows to columns
C_dense = np.random.rand(4, 5) # 4 rows, 5 columns
cost, x, y = lap.lapjv(C_dense, extend_cost=True)
print(f"LAPJV Cost: {cost}\nRow assignments (x): {x}\nColumn assignments (y): {y}")

# Example for LAPMOD (sparse matrix) - typically faster for larger, sparse matrices
# For simplicity, using a dense matrix here, but 'lapmod' is optimized for sparse inputs.
# Note: lapmod primarily expects square matrices for direct use, consider extensions for non-square.
C_sparse_example = np.array([
    [1, np.inf, 3, np.inf],
    [np.inf, 2, np.inf, 4],
    [5, np.inf, 6, np.inf],
    [np.inf, 7, np.inf, 8]
])

cost_mod, x_mod, y_mod = lap.lapmod(C_sparse_example)
print(f"\nLAPMOD Cost: {cost_mod}\nRow assignments (x): {x_mod}\nColumn assignments (y): {y_mod}")

view raw JSON →