pykp


Namepykp JSON
Version 1.2.0 PyPI version JSON
download
home_pageNone
SummaryTooling for sampling and solving instances of the 0-1 Knapsack Problem.
upload_time2024-11-18 12:32:24
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
licenseMIT
keywords knapsack optimisation sampling algorithms
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyKP

PyKP is a free and open-source library for sampling and solving instances of the knapsack problem. It provides tools to define knapsack instances, solve them eficiently, and analyse computational complexity metrics. You can also use `pykp` to randomly sample knapsack problem instances based on specified distributions.

## Features

- Define knapsack problem instances with custom items, weights, and values.
- Solve knapsack instances using branch-and-bound and other methods.
- Compute optimal and feasible solutions for different knapsack configurations.
- Analyse computational complexity metrics.
- Generate synthetic knapsack instances with custom weight, density, and solution value ranges.

## Installation

PyKP support Python version 3.12 and higher. To install PyKP, run

```
pip install pykp
```

## Usage

### Defining and Solving a Knapsack Problem

To start, define a knapsack problem with a set of items and solve it using the Knapsack class.

```python
import numpy as np
from pykp import Knapsack
from pykp import Item

# Define items for the knapsack
items = np.array([
    Item(value=10, weight=5),
    Item(value=15, weight=10),
    Item(value=7, weight=3)
])

# Initialise a Knapsack instance
capacity = 15
knapsack = Knapsack(items=items, capacity=capacity)
knapsack.solve()

# Display the optimal solution
print("Optimal Solution Value:", knapsack.optimal_nodes[0].value)
```

### Generating Knapsack Instances with Sampler

The `Sampler` class allows you to generate knapsack instances based on specific ranges for item densities (value/weight ratio) and optimal solution values.

```python
from pykp import Sampler

# Initialise a Sampler instance with desired ranges
sampler = Sampler(
    num_items=5,
    normalised_capacity=0.6,
    density_range=(0.5, 1.5),
    solution_value_range=(100, 200)
)

# Generate a sampled knapsack instance
sampled_knapsack = sampler.sample()
print("Sampled Knapsack Capacity:", sampled_knapsack.capacity)
```

### Analysing Knapsack Solutions

The package provides methods to analyse the optimal solutions and other feasible arrangements.

```python
# Display a summary of the knapsack solutions
print(sampled_knapsack.summary())
```

## License

This project is licensed under the MIT License.

## Contributing

Contributions are welcome. Please fork the repository and submit a pull request.

## Contact

For questions or feedback, please reach out at hrs.andrabi@gmail.com.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pykp",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "knapsack, optimisation, sampling, algorithms",
    "author": null,
    "author_email": "Hassan Andrabi <hrs.andrabi@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/3d/05/1d29c6913c85f2cd43f69c0995adc9d3f6f5594c52b434d168d2e4077ce6/pykp-1.2.0.tar.gz",
    "platform": null,
    "description": "# PyKP\n\nPyKP is a free and open-source library for sampling and solving instances of the knapsack problem. It provides tools to define knapsack instances, solve them eficiently, and analyse computational complexity metrics. You can also use `pykp` to randomly sample knapsack problem instances based on specified distributions.\n\n## Features\n\n- Define knapsack problem instances with custom items, weights, and values.\n- Solve knapsack instances using branch-and-bound and other methods.\n- Compute optimal and feasible solutions for different knapsack configurations.\n- Analyse computational complexity metrics.\n- Generate synthetic knapsack instances with custom weight, density, and solution value ranges.\n\n## Installation\n\nPyKP support Python version 3.12 and higher. To install PyKP, run\n\n```\npip install pykp\n```\n\n## Usage\n\n### Defining and Solving a Knapsack Problem\n\nTo start, define a knapsack problem with a set of items and solve it using the Knapsack class.\n\n```python\nimport numpy as np\nfrom pykp import Knapsack\nfrom pykp import Item\n\n# Define items for the knapsack\nitems = np.array([\n    Item(value=10, weight=5),\n    Item(value=15, weight=10),\n    Item(value=7, weight=3)\n])\n\n# Initialise a Knapsack instance\ncapacity = 15\nknapsack = Knapsack(items=items, capacity=capacity)\nknapsack.solve()\n\n# Display the optimal solution\nprint(\"Optimal Solution Value:\", knapsack.optimal_nodes[0].value)\n```\n\n### Generating Knapsack Instances with Sampler\n\nThe `Sampler` class allows you to generate knapsack instances based on specific ranges for item densities (value/weight ratio) and optimal solution values.\n\n```python\nfrom pykp import Sampler\n\n# Initialise a Sampler instance with desired ranges\nsampler = Sampler(\n    num_items=5,\n    normalised_capacity=0.6,\n    density_range=(0.5, 1.5),\n    solution_value_range=(100, 200)\n)\n\n# Generate a sampled knapsack instance\nsampled_knapsack = sampler.sample()\nprint(\"Sampled Knapsack Capacity:\", sampled_knapsack.capacity)\n```\n\n### Analysing Knapsack Solutions\n\nThe package provides methods to analyse the optimal solutions and other feasible arrangements.\n\n```python\n# Display a summary of the knapsack solutions\nprint(sampled_knapsack.summary())\n```\n\n## License\n\nThis project is licensed under the MIT License.\n\n## Contributing\n\nContributions are welcome. Please fork the repository and submit a pull request.\n\n## Contact\n\nFor questions or feedback, please reach out at hrs.andrabi@gmail.com.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Tooling for sampling and solving instances of the 0-1 Knapsack Problem.",
    "version": "1.2.0",
    "project_urls": {
        "Documentation": "https://github.com/HRSAndrabi/pykp",
        "Homepage": "https://github.com/HRSAndrabi/pykp",
        "Issues": "https://github.com/HRSAndrabi/pykp/issues",
        "Repository": "https://github.com/HRSAndrabi/pykp"
    },
    "split_keywords": [
        "knapsack",
        " optimisation",
        " sampling",
        " algorithms"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "12b866d6d1ca7592b9eaf2b22ff331644fbced83171ed62cf545ab9b6d41deff",
                "md5": "ba453cd4846e6a129c6e16cc2166dc14",
                "sha256": "7fa7a56c4873c526fad74d348183823bb304169cf5e358cf3ded6adac5124640"
            },
            "downloads": -1,
            "filename": "pykp-1.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ba453cd4846e6a129c6e16cc2166dc14",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 11913,
            "upload_time": "2024-11-18T12:32:22",
            "upload_time_iso_8601": "2024-11-18T12:32:22.363814Z",
            "url": "https://files.pythonhosted.org/packages/12/b8/66d6d1ca7592b9eaf2b22ff331644fbced83171ed62cf545ab9b6d41deff/pykp-1.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3d051d29c6913c85f2cd43f69c0995adc9d3f6f5594c52b434d168d2e4077ce6",
                "md5": "4c3f4cfe609a646d0fe9fa23459ae0b1",
                "sha256": "71c1bb176c40acbf8d1c6e568f864a271cb7e1cced9abb62074f9a640e5d7a09"
            },
            "downloads": -1,
            "filename": "pykp-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4c3f4cfe609a646d0fe9fa23459ae0b1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 13788,
            "upload_time": "2024-11-18T12:32:24",
            "upload_time_iso_8601": "2024-11-18T12:32:24.351855Z",
            "url": "https://files.pythonhosted.org/packages/3d/05/1d29c6913c85f2cd43f69c0995adc9d3f6f5594c52b434d168d2e4077ce6/pykp-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-18 12:32:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "HRSAndrabi",
    "github_project": "pykp",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pykp"
}
        
Elapsed time: 5.03987s