# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['kmsr']

package_data = \
{'': ['*'], 'kmsr': ['cpp/*', 'header/*']}

install_requires = \
['scikit-learn>=1.6.1,<2.0.0']

setup_kwargs = {
    'name': 'kmsr',
    'version': '0.1.4',
    'description': 'kMSR provides a selection of algorithms to solve the k-Min-Sum-Radii problem.',
    'long_description': '[![Build Status](https://github.com/algo-hhu/k-min-sum-radii/actions/workflows/mypy-flake-test.yml/badge.svg)](https://github.com/algo-hhu/k-min-sum-radii/actions)\n[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)\n[![Supported Python version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/release/python-3100/)\n[![Stable Version](https://img.shields.io/pypi/v/kmsr?label=stable)](https://pypi.org/project/kmsr/)\n\n# K-Min-Sum-Radii\n\nkMSR provides various implementations to solve the k-Min-Sum-Radii problem. The k-Min-Sum-Radii problem is a clustering problem that aims to minimize the sum of the radii of the clusters. Given a set of points, the aim is to find $k$ balls such that the the sum of the radii of the balls is minimized. The package provides the following algorithms:\n\n- `fpt-heuristic`: The algorithm is an heuristic version of what is described in [this paper](https://doi.org/10.1007/978-3-031-49815-2_9). In practice, this works well for clusters that are not too separated. The algorithm uses the parameters `epsilon`, `n_u`, and `num_test_radii` to control the trade-off between the quality of the solution and the runtime. Increase `n_u` for a more accurate solution, which will however require more computation time.\n- `heuristic`: The algorithm is a simple heuristic that explores all possible combinations for the first cluster, and then selects the remaining centers as the points farthest from the radius of the first cluster. This algorithm works well in practice but it is not practical for large datasets.\n- `gonzalez`: This is the standard [Gonzalez algorithm](https://doi.org/10.1016/0304-3975(85)90224-5) for $k$-center.\n- `kmeans`: This is the [k-means++ algorithm](https://doi.org/10.5555/1283383.1283494) for $k$-means.\n\nAlthough the last two algorithms are not specifically designed for the k-Min-Sum-Radii problem, they serve as useful baselines for comparing the performance of other algorithms. Additionally, an optimization has been integrated into all algorithms: intersecting balls are merged at the end, resulting in a more cost-effective solution.\n\nFor computing the minimum enclosing circle of a set of points we use the [miniball](https://people.inf.ethz.ch/gaertner/subdir/software/miniball.html) [header](https://github.com/algo-hhu/k-min-sum-radii/blob/main/kmsr/header/miniball.h) for C++. Their code has not been changed.\n\n**You can try kMSR out on our [Clustering Toolkit](https://clustering-toolkit.algo.hhu.de/K-Min_Sum_Radii_Clustering)!**\n\n\n## Installation\n\nWe **highly recommend** to install OpenMP. Parts of the code are parallelized and will be much faster. However, on Windows and MacOS the code also works without OpenMP. Nonetheless, the code was written for Linux and will achieve the best performance there.\n\nOn Linux, you can use the following command:\n```bash\n# Alpine\nsudo apk add openmp-dev libgomp\n# Ubuntu\nsudo apt-get install libomp-dev libgomp1\n# Debian\nsudo apt-get install gcc libomp-dev libomp5 libgomp1\n# ArchLinux\nsudo pacman -S openmp\nln -s libomp.so libomp.so.5\n```\nWe have tested this on docker, so it might be a little different on your system.\n\nOn MacOS, you can use the following command:\n```bash\n brew install llvm libomp\n```\nHowever, it might be that MacOS does not find the installed library. In [`build_extension.py`](./build_extension.py), the paths are set manually. If it does not work for you, please clone the repository and run `poetry build`. You will see a message in red if your OpenMP is not found.\n\nThen, you can install the package via pip:\n```bash\npip install kmsr\n```\n\n## Example\n\n```python\nfrom kmsr import KMSR\nfrom kmsr.plot import plot_multiple_results\nfrom sklearn.datasets import make_blobs\nfrom time import time\n\npoints, ground_truth = make_blobs(\n    n_samples=100,\n    n_features=2,\n    centers=2,\n    cluster_std=0.6,\n    shuffle=True,\n    random_state=42,\n)\n\nlabels = []\ncenters = []\nradii = []\ntitles = []\nfor algo in ["FPT-heuristic", "Heuristic", "Gonzalez", "KMeans"]:\n    kmsr = KMSR(\n        n_clusters=5,\n        algorithm=algo,\n        epsilon=0.5,\n        n_u=10000,\n        n_test_radii=10,\n        random_state=42,\n    )\n    start = time()\n    kmsr.fit(points)\n    end = time() - start\n    labels.append(kmsr.labels_)\n    centers.append(kmsr.cluster_centers_)\n    radii.append(kmsr.cluster_radii_)\n    titles.append(f"{algo}: {sum(kmsr.cluster_radii_):.3f}, Time: {end:.3f}s")\n\nplot_multiple_results(\n    points,\n    clusters=labels,\n    centers=centers,\n    radii=radii,\n    title=titles,\n)\n```\n\n<p align="center">\n  <img src="https://raw.githubusercontent.com/algo-hhu/k-min-sum-radii/main/images/example.png" alt="Comparison of the Different Methods for kMSR"/>\n</p>\n\n## Development\n\nInstall [poetry](https://python-poetry.org/docs/#installation)\n```bash\ncurl -sSL https://install.python-poetry.org | python3 -\n```\n\nInstall clang\n```bash\nsudo apt-get install clang\n```\n\nSet clang variables\n```bash\nexport CXX=/usr/bin/clang++\nexport CC=/usr/bin/clang\n```\n\nInstall the package\n```bash\npoetry install\n```\n\nIf the installation does not work and you do not see the C++ output, you can build the package to see the stack trace\n```bash\npoetry build\n```\n\nRun the tests\n```bash\npoetry run python -m unittest discover tests -v\n```\n\n## Citation\n\nIf you use this code, please cite the following bachelor thesis:\n\n```\nN. Lenßen, "Experimentelle Analyse von Min-Sum-Radii Approximationsalgorithmen". Bachelorarbeit, Heinrich-Heine-Universität Düsseldorf, 2024.\n```\n\nMoreover, depending on the selection of the `algorithm` parameter, you should also cite the [following paper](https://doi.org/10.1007/978-3-031-49815-2_9) for `algorithm=\'fpt-heuristic\'`:\n\n```\nL. Drexler, A. Hennes, A. Lahiri, M. Schmidt, and J. Wargalla, "Approximating Fair K-Min-Sum-Radii in Euclidean Space," in Lecture notes in computer science, 2023, pp. 119–133. doi: 10.1007/978-3-031-49815-2_9.\n```\n\nthe [following paper](https://doi.org/10.1016/0304-3975(85)90224-5) for `algorithm=\'gonzalez\'`:\n\n```\nT. F. Gonzalez, "Clustering to minimize the maximum intercluster distance," Theoretical Computer Science, vol. 38, pp. 293–306, Jan. 1985, doi: 10.1016/0304-3975(85)90224-5.\n```\n\nand the [following paper](https://doi.org/10.5555/1283383.1283494) for `algorithm=\'kmeans\'`:\n\n```\nD. Arthur and S. Vassilvitskii, "k-means++: the advantages of careful seeding," Symposium on Discrete Algorithms, pp. 1027–1035, Jan. 2007, doi: 10.5555/1283383.1283494.\n```\n',
    'author': 'Niklas Lenssen',
    'author_email': 'niklas.lenssen@hhu.de',
    'maintainer': 'None',
    'maintainer_email': 'None',
    'url': 'None',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'python_requires': '>=3.10,<4.0',
}
from build_extension import *
build(setup_kwargs)

setup(**setup_kwargs)
