Skip to main content

knapsack-pip: A 0-1 knapsack solver

This is a library for solving knapsack problems.

Use this solver for maximization or minimization of 0-1 knapsack problems a Branch and Bound algorithm.

Non negative weights and profits can also be included.

Installation

This library can be installed via pip. Use command: pip install knapsack-pip

Usage

So far, there are different knapsack solvers for different algorithms:

  • BBKnapsack: a knapsack solver using branch and bound with a stack. To import: from knapsack01.BBKnapsack import BBKnapsack
  • HSKnapsack: a knapsack solver using the Horowitz-Sahni branch and bound algorithm. To import: from knapsack01.HSKnapsack import HSKnapsack

The output includes the maximum or minimum profit and its corresponding solution as a list of the same length as list of items (solution[i] = 1 if ith item is included in the solution and solution[i] = 0 otherwise)

Example of maximization

from knapsack01.BBKnapsack import BBKnapsack

capacity = 50
weights = [31, 10, 20, 20, 5, 3, -6]
profits = [70, 20, 39, 37, 7, 5, 10]

my_knapsack1 = BBKnapsack(capacity, profits, weights)
max_profit, max_solution = my_knapsack1.maximize()
# (126, [1, 0, 1, 0, 1, 0, 1])

Example of minimization

from knapsack01.BBKnapsack import BBKnapsack

capacity = 50
weights = [31, 10, 20, 20, 5, 3, -6]
profits = [70, 20, 39, 37, 7, 5, 10]

my_knapsack2 = BBKnapsack(capacity, profits, weights)
min_profit, min_solution = my_knapsack2.minimize()
# (96, [0, 1, 1, 1, 0, 1, 0])

Metadata

Release files for knapsack-pip 0.2

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

Source distribution (sdist)

Source distribution for knapsack-pip 0.2
File Size Uploaded Provenance
knapsack-pip-0.2.tar.gz 5.8 kB Without provenance Details

Release files / knapsack-pip-0.2.tar.gz

Download URL knapsack-pip-0.2.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3cc9f70d6fa8005254782266026a2fd045908f2e75320dd795840b6e276cf872
BLAKE2b-256 checksum
How to use checksums
0bd7036ba30da0e26e54093c5e2e8050ae7e328c6eca4ce37dd93b939f6d0745
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

Security for knapsack-pip 0.2

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.

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file No provenance

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