Package: mknapsack
mknapsackSolving knapsack problems with Python using algorithms byMartello and Toth:Single 0-1 knapsack problem: MT1, MT2, MT1R (real numbers)Bounded knapsack problem: MTB2Unbounded knapsack problem: MTU1, MTU2Multiple 0-1 knapsack problem: MTM, MTHMChange-making problem: MTC2Bounded change-making problem: MTCBGeneralized assignment problem: MTG, MTHGBin packing problem: MTPSubset sum problem: MTSLDocumentation is availablehere.Installationpip install mknapsackExample usageSingle 0-1 Knapsack Problemfrommknapsackimportsolve_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 profitsres=solve_single_knapsack(profits,weights,capacity)If your inputs are real numbers, you may set parametermethod='mt1r'.Bounded Knapsack Problemfrommknapsackimportsolve_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 profitsres=solve_bounded_knapsack(profits,weights,capacity,n_items)Unbounded Knapsack Problemfrommknapsackimportsolve_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 profitsres=solve_unbounded_knapsack(profits,weights,capacity,n_items)Multiple 0-1 Knapsack Problemfrommknapsackimportsolve_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 profitsres=solve_multiple_knapsack(profits,weights,capacities)Change-Making Problemfrommknapsackimportsolve_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 itemsres=solve_change_making(weights,capacity)Bounded Change-Making Problemfrommknapsackimportsolve_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 itemsres=solve_bounded_change_making(weights,n_items,capacity)Generalized Assignment Problemfrommknapsackimportsolve_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 profitsres=solve_generalized_assignment(profits,weights,capacities)Bin Packing Problemfrommknapsackimportsolve_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 requiredres=solve_bin_packing(weights,capacity)Subset Sum Problemfrommknapsackimportsolve_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 fullestres=solve_subset_sum(weights,capacity)ReferencesKnapsack problems: algorithms and computer implementationsby S. Martello and P. Toth, 1990Original Fortran77 source codeby S. Martello and P. TothJesse Myrberg (jesse.myrberg@gmail.com)