Knapsack problem solver
Project description
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])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file knapsack-pip-0.2.tar.gz
.
File metadata
- Download URL: knapsack-pip-0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cc9f70d6fa8005254782266026a2fd045908f2e75320dd795840b6e276cf872 |
|
MD5 | f1d646c048bcfe4ed172f89af7673a25 |
|
BLAKE2b-256 | 0bd7036ba30da0e26e54093c5e2e8050ae7e328c6eca4ce37dd93b939f6d0745 |