Skip to main content

Tooling for sampling and solving instances of the 0-1 Knapsack Problem.

Project description

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.

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.

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.

# 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pykp-2.1.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pykp-2.1.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file pykp-2.1.0.tar.gz.

File metadata

  • Download URL: pykp-2.1.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pykp-2.1.0.tar.gz
Algorithm Hash digest
SHA256 3d85c8207a4d9b066f70d1bed04db420cc91af49b0cfce25dce8b9fa139e6fe2
MD5 5fb86a28a5cca6366411b42972a88e02
BLAKE2b-256 4bef8f964cc22260e4b2a6c370af8cb10a7c63cf29f6fcde927b736d200123cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pykp-2.1.0.tar.gz:

Publisher: continuous-delivery.yml on HRSAndrabi/pykp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pykp-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: pykp-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pykp-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2ae126025bbc730c2059ff11923f3a8086472f9a7792144b3085faded5a04b4
MD5 494f1da122a0205a1005a9c1933f5448
BLAKE2b-256 bd5c0573f60b91ac1288ab7f4ff90a754fd1edfcc2f277a2634511f1842f8d63

See more details on using hashes here.

Provenance

The following attestation bundles were made for pykp-2.1.0-py3-none-any.whl:

Publisher: continuous-delivery.yml on HRSAndrabi/pykp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page