Skip to main content

kap

Build wheels Documentation PyPI Downloads crates.io crates.io downloads docs.rs DOI License

kap: three parts partitioned into three disjoint cliques

kap solves k-partite assignment problems from pairwise cost matrices. It partitions vertices into cliques with at most one vertex per part and minimizes the total pairwise cost. It provides heuristics, lower bounds, and linear assignment in Python and Rust; Python also provides an exact solver through SciPy and HiGHS. For k > 2, the heuristics do not guarantee an optimal solution.

Read the documentation Install from PyPI

Install

Install the Python package from PyPI (Python 3.10 or newer; wheels include the Rust core):

pip install kap

Install scipy too (pip install "kap[scipy]") to use exact. Rust users can add the kap-solver crate (cargo add kap-solver) and import it as kap. For 0.2.3 and its API, see the 0.2.3 README.

Examples

Python:

import numpy as np
import kap

costs = [np.array([[4, 1], [2, 3]]) for _ in range(3)]
result = kap.ils(costs, max_iter=10, random_state=0)
print(result.cliques, result.costs)

Rust:

use kap::{k_assignment, Algo, MatrixView, Options};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let data = [4i64, 1, 2, 3];
    let matrix = MatrixView::from_row_major(&data, 2, 2)?;
    let costs = [matrix, matrix, matrix];
    let result = k_assignment(&costs, Algo::Ils, &Options::default())?;
    println!("{:?} {:?}", result.cliques, result.costs);
    Ok(())
}

Functions

Python function Purpose
ils Run iterated local search with independent chains.
memetic Evolve a population of locally optimized solutions.
local_search Improve a starting solution with dimensionwise variation.
multiple_hungarian Run Am–Fm or Shift.
k_assignment Dispatch to a selected k-assignment method.
exact Solve equal-size instances with SciPy and HiGHS.
lower_bound Bound the optimum for equal-size parts.
linear_sum_assignment Solve a rectangular linear assignment.

See the documentation for inputs, Rust APIs, algorithm limits, and benchmarks.

The native dispatcher defaults to ILS; select algo="memetic" explicitly to use memetic search. Native heuristics support optional matching via threshold=, including forbidden pairs; returned costs remain in original units. ILS and local search accept warm starts, which may be partial or empty when a threshold is used. See optional matching and warm starts for details.

References and citation

kap combines several assignment methods. The main algorithm references are:

  • Local search and ILS: D. Karapetyan and G. Gutin, “Local search heuristics for the multidimensional assignment problem,” Journal of Heuristics 17(3), 201–249, 2011. arXiv:0806.3258.
  • Shift construction: D. Karapetyan, G. Gutin, and B. Goldengorin, “Empirical evaluation of construction heuristics for the multidimensional assignment problem,” Text in Algorithmics 11, 107–122, 2009. arXiv:0906.2960.
  • Memetic search and population sizing: D. Karapetyan and G. Gutin, “A New Approach to Population Sizing for Memetic Algorithms: A Case Study for the Multidimensional Assignment Problem,” Evolutionary Computation 19(3), 345–371, 2011. arXiv:1003.4314.
  • Linear assignment: R. Jonker and A. Volgenant, “A shortest augmenting path algorithm for dense and sparse linear assignment problems,” 1987; D. F. Crouse, “On implementing 2D rectangular assignment algorithms,” 2016.
  • Multiple Hungarian methods (Am–Fm): B. Gabrovšek et al., “Multiple Hungarian Method for k-Assignment Problem,” Mathematics 8(11), 2050, 2020. doi:10.3390/math8112050.

The exact solver uses SciPy's MILP interface to HiGHS. See the algorithm reference for additional papers, implementation details, and differences from the published methods, and the lower-bound guide for the bound implementation.

To cite the software, use the Zenodo DOI and CITATION.cff. Cite the relevant algorithm papers when discussing a particular method. Release history up to this version is in CHANGELOG.md.

License

MIT.

Metadata

Release files for kap 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kap 0.3.0
File Size Uploaded Provenance Actions
kap-0.3.0.tar.gz 92.7 kB Without provenance Details for kap-0.3.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for kap 0.3.0
File Actions
kap-0.3.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for kap-0.3.0-cp310-abi3-win_amd64.whl
kap-0.3.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for kap-0.3.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
kap-0.3.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for kap-0.3.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
kap-0.3.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 10.12+ x86-64, macOS 10.12+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64 Details for kap-0.3.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Total release size: 2.7 MB

Release files / kap-0.3.0.tar.gz

Download URL kap-0.3.0.tar.gz
Size 92.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c25c141138a776297658015ddeff0b336f6ccb73ee2edf6b7ad81f9bdf8fc7b7
BLAKE2b-256 checksum
How to use checksums
7b7155e71db8fbefd7236668e00d781fb6711cbe9b6d3ce16fed4a2a884456db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / kap-0.3.0-cp310-abi3-win_amd64.whl

Download URL kap-0.3.0-cp310-abi3-win_amd64.whl
Size 439.2 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
3a1930ba364144333d09fadb3f3e7878c0abfedf418ed35540708dd491ccac63
BLAKE2b-256 checksum
How to use checksums
e73784cc4da91700fd67d66c891a5766d27a70a082136335b28ff2929a470516
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / kap-0.3.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL kap-0.3.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 571.4 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
d4ff6f52c9afc418deadcb09540e9b8347e60fecd58d01f2c9e871977d7d1719
BLAKE2b-256 checksum
How to use checksums
2fd9510fcde94e2a9b6640a60677ab8bbd798f50baef28af9b69ea0971dd656d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / kap-0.3.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL kap-0.3.0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 550.4 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
5d20e1bb38326006a729f04bd5f3affa84574d0fdacc98e18c62d1185959e7f7
BLAKE2b-256 checksum
How to use checksums
a4a6d1d7e3973ae371955604add26a0697fe4710b25907c7a082c184366a3cc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / kap-0.3.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Download URL kap-0.3.0-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
Size 1.0 MB
Tags CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e4e770740ae7162aef1aadcf397e667fdca3ed9c551a6a9d4bd6642e67313a6c
BLAKE2b-256 checksum
How to use checksums
4d55ee8814456d091f44237c2b91546990c84796a6c432e1c0db9a0b34fc8516
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for kap 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page