Solving knapsack and bin packing problems with Python
Project description
mknapsack
Solving knapsack problems with Python using algorithms by Martello and Toth:
- Single 0-1 knapsack problem: MT1, MT2, MT1R (real numbers)
- Bounded knapsack problem: MTB2
- Unbounded knapsack problem: MTU1, MTU2
- Multiple 0-1 knapsack problem: MTM, MTHM
- Change-making problem: MTC2
- Bounded change-making problem: MTCB
- Generalized assignment problem: MTG, MTHG
- Bin packing problem: MTP
- Subset sum problem: MTSL
Documentation is available here.
Installation
pip install mknapsack
Example usage
Single 0-1 Knapsack Problem
from mknapsack import solve_single_knapsack
# Given ten items with the following profits and weights:
profits = [78, 35, 89, 36, 94, 75, 74, 79, 80, 16]
weights = [18, 9, 23, 20, 59, 61, 70, 75, 76, 30]
# ...and a knapsack with the following capacity:
capacity = 190
# Assign items into the knapsack while maximizing profits
res = solve_single_knapsack(profits, weights, capacity)
If your inputs are real numbers, you may set parameter method='mt1r'
.
Bounded Knapsack Problem
from mknapsack import solve_bounded_knapsack
# Given ten item types with the following profits and weights:
profits = [78, 35, 89, 36, 94, 75, 74, 79, 80, 16]
weights = [18, 9, 23, 20, 59, 61, 70, 75, 76, 30]
# ...and the number of items available for each item type:
n_items = [1, 2, 3, 2, 2, 1, 2, 2, 1, 4]
# ...and a knapsack with the following capacity:
capacity = 190
# Assign items into the knapsack while maximizing profits
res = solve_bounded_knapsack(profits, weights, capacity, n_items)
Unbounded Knapsack Problem
from mknapsack import solve_unbounded_knapsack
# Given ten item types with the following profits and weights:
profits = [16, 72, 35, 89, 36, 94, 75, 74, 100, 80]
weights = [30, 18, 9, 23, 20, 59, 61, 70, 75, 76]
# ...and a knapsack with the following capacity:
capacity = 190
# Assign items repeatedly into the knapsack while maximizing profits
res = solve_unbounded_knapsack(profits, weights, capacity, n_items)
Multiple 0-1 Knapsack Problem
from mknapsack import solve_multiple_knapsack
# Given ten items with the following profits and weights:
profits = [78, 35, 89, 36, 94, 75, 74, 79, 80, 16]
weights = [18, 9, 23, 20, 59, 61, 70, 75, 76, 30]
# ...and two knapsacks with the following capacities:
capacities = [90, 100]
# Assign items into the knapsacks while maximizing profits
res = solve_multiple_knapsack(profits, weights, capacities)
Change-Making Problem
from mknapsack import solve_change_making
# Given ten item types with the following weights:
weights = [18, 9, 23, 20, 59, 61, 70, 75, 76, 30]
# ...and a knapsack with the following capacity:
capacity = 190
# Fill the knapsack while minimizing the number of items
res = solve_change_making(weights, capacity)
Bounded Change-Making Problem
from mknapsack import solve_bounded_change_making
# Given ten item types with the following weights:
weights = [18, 9, 23, 20, 59, 61, 70, 75, 76, 30]
# ...and the number of items available for each item type:
n_items = [1, 2, 3, 2, 1, 1, 1, 2, 1, 2]
# ...and a knapsack with the following capacity:
capacity = 190
# Fill the knapsack while minimizing the number of items
res = solve_bounded_change_making(weights, n_items, capacity)
Generalized Assignment Problem
from mknapsack import solve_generalized_assignment
# Given seven item types with the following knapsack dependent profits:
profits = [[6, 9, 4, 2, 10, 3, 6],
[4, 8, 9, 1, 7, 5, 4]]
# ...and the following knapsack dependent weights:
weights = [[4, 1, 2, 1, 4, 3, 8],
[9, 9, 8, 1, 3, 8, 7]]
# ...and two knapsacks with the following capacities:
capacities = [11, 22]
# Assign items into the knapsacks while maximizing profits
res = solve_generalized_assignment(profits, weights, capacities)
Bin Packing Problem
from mknapsack import solve_bin_packing
# Given six items with the following weights:
weights = [4, 1, 8, 1, 4, 2]
# ...and bins with the following capacity:
capacity = 10
# Assign items into bins while minimizing the number of bins required
res = solve_bin_packing(weights, capacity)
Subset Sum Problem
from mknapsack import solve_subset_sum
# Given six items with the following weights:
weights = [4, 1, 8, 1, 4, 2]
# ...and a knapsack with the following capacity:
capacity = 10
# Choose items to fill the knapsack to the fullest
res = solve_subset_sum(weights, capacity)
References
- Knapsack problems: algorithms and computer implementations by S. Martello and P. Toth, 1990
- Original Fortran77 source code by S. Martello and P. Toth
Jesse Myrberg (jesse.myrberg@gmail.com)
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
mknapsack-1.1.9.tar.gz
(164.5 kB
view hashes)
Built Distributions
Close
Hashes for mknapsack-1.1.9-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe5b71d5572cb5c6dec0b62f794121ae4fd6dfa08fbc8c46a2f08d23a9b73686 |
|
MD5 | f7ae72fc06650e4371c4c4d85b050df7 |
|
BLAKE2b-256 | 9eae046d88a333021bbc9d897454e4138c1040693337e27a984713295db7611f |
Close
Hashes for mknapsack-1.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a48eb7ccc443cb97f045dd0a5ddc677b1f4c682611212c21dabafc89fdacebd0 |
|
MD5 | f9094f321ab57abd763986a9213785ff |
|
BLAKE2b-256 | 7fe116d9a130c1561ee1e48bebb593970ad1983b3f8d5532023c12c79b838611 |
Close
Hashes for mknapsack-1.1.9-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 191c11dc5f2e7f719035f55e5de54e3245bc20015427def8320fa3abfa45239a |
|
MD5 | 6531c97b306a293bf1167e18001a2e8f |
|
BLAKE2b-256 | e2120e2025d5d0ab2563759627221b72152af445ce8c5186fcd4a1c876acfdd5 |
Close
Hashes for mknapsack-1.1.9-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdc324e32111682870c7a358b96c41772ce4305785a63b21710dd30a20c057e8 |
|
MD5 | 2277e3de70b57034cd0cac3764c5f9e0 |
|
BLAKE2b-256 | f1b749c4f9c322bf8b1b8f6c27e3915f4cdc08d6ed1d3e02973f023fad0d1c69 |
Close
Hashes for mknapsack-1.1.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a4e16bf5b4c043a232163293333671d75147bb6f65c8ff5952980bb2edb682e |
|
MD5 | 8d4bfcacec7b87a40986bc023905192a |
|
BLAKE2b-256 | 4ccc4baca0033b8aad9ffb8088be4f80636a9cb46fa8b99254bcaa2f543d821e |
Close
Hashes for mknapsack-1.1.9-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e150e79e0af08fc983dd4356504edab5e111caaf52fb5b72a8322112aacd7926 |
|
MD5 | abf87b15533fd1069d5338e5c6ca9307 |
|
BLAKE2b-256 | a2bda170920fcb695bdd3673a12b202bec0a0020599964df1fa3f7a7bf689f0b |
Close
Hashes for mknapsack-1.1.9-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1d3a93b2a53805939d9c794e50e482a8417a757fb1a4447c0d0b7f975ecf03b |
|
MD5 | 5204b20d02c89c8d7fa355903512e370 |
|
BLAKE2b-256 | c1f55b72e4cb5f9eb0f6455d9ffb7980bd2e15fc940b2d9f4bd7926bc3fb6d42 |
Close
Hashes for mknapsack-1.1.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96ae83ebb74b70bd38fa2cee6b40368d0b255f1d2de581db503b5b56747d9c44 |
|
MD5 | 84e2ed283872557f2e4d200c45462fb5 |
|
BLAKE2b-256 | 44174dc26a98f88928960c5005a688b956be6808f21afc078fec697cd2020385 |
Close
Hashes for mknapsack-1.1.9-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fbbc09d68f9d66bdaa5a76cfa72fc15570d5507a8bb13f4cc5badf1a27c753e |
|
MD5 | 8382bd00b54d66edf255cd4b3251e3c6 |
|
BLAKE2b-256 | 7ebb7d821eda38ed16b3f3f4edc238d5f77d6d8d6b0bb26939b8de67bb1e77f9 |
Close
Hashes for mknapsack-1.1.9-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ea08c8f7aecc78f7f77e43c00b955b30818504ba3d90e97b9e698f793a795d7 |
|
MD5 | 67ef7ece5c68e18987307a1002be6d09 |
|
BLAKE2b-256 | e2d040613e7cb539bed9fe16a0b3aae060761a43b11fb17812b5adae546a6542 |
Close
Hashes for mknapsack-1.1.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f935342846b251af7795c4a6991e62115cb7235da32561d6a81497b1305bc7a3 |
|
MD5 | add3a9d7bb0e2e82b8c39c78532e07c0 |
|
BLAKE2b-256 | b32d9a653c1574cb2193d1dd73e70f1b035747e6cf9f385a25e9b0f1201dc930 |
Close
Hashes for mknapsack-1.1.9-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 711b2f47b0cb3291d8e6b310f8851dc5a3072f71ebb35076ac7658102e695aa7 |
|
MD5 | 5eed557e82674f5f96a9b171b4d48d4b |
|
BLAKE2b-256 | 0e9c6bb863ede84a2d44d3ce7925e298578c113ed6f4da6f786153bd616c6258 |