## Example 1: single knapsack
w <- c( 2, 20, 20, 30, 40, 30, 60, 10)
p <- c(15, 100, 90, 60, 40, 15, 10, 1)
cap <- 102
(is <- mknapsack(w, p, cap))
which(is$ksack == 1)
# [1] 1 2 3 4 6 , capacity 102 and total profit 280
## Example 2: multiple knapsack
w <- c( 40, 60, 30, 40, 20, 5)
p <- c(110, 150, 70, 80, 30, 5)
cap <- c(85, 65)
is <- mknapsack(w, p, cap)
# kps 1: 1,4; kps 2: 2,6; value: 345
## Example 3: multiple knapsack
p <- c(78, 35, 89, 36, 94, 75, 74, 79, 80, 16)
w <- c(18, 9, 23, 20, 59, 61, 70, 75, 76, 30)
cap <- c(103, 156)
is <- mknapsack(w, p, cap)
# kps 1: 3,4,5; kps 2: 1,6,9; value: 452
if (FALSE) {
# How to Cut Your Planks with R
# R-bloggers, Rasmus Baath, 2016-06-12
#
# This is application of multiple knapsacks to cutting planks into pieces.
planks_we_have <- c(120, 137, 220, 420, 480)
planks_we_want <- c(19, 19, 19, 19, 79, 79, 79, 103, 103,
103, 135, 135, 135, 135, 160)
s <- mknapsack(planks_we_want, planks_we_want + 1, planks_we_have)
s$ksack
## [1] 5 5 5 5 3 5 5 4 1 5 4 5 3 2 4
# Solution w/o backtracking
# bin 1 : 103 | Rest: 17
# bin 2 : 135 | Rest: 2
# bin 3 : 79 + 135 | Rest: 6
# bin 4 : 103 + 135 + 160 | Rest: 22
# bin 5 : 4*19 + 2*79 + 103 + 135 | Rest: 8
#
# Solution with reversing the bins (bigger ones first)
# bin 1 : 103 | Rest: 4
# bin 2 : 2*19 + 79 | Rest: 20
# bin 3 : 79 + 135 | Rest: 6
# bin 4 : 2*19 + 79 + 135 + 160 | Rest: 8
# bin 5 : 2*103 + 2*135 | Rest: 17
#
# Solution with backtracking (compactification)
# sol = c(1, 4, 4, 1, 1, 3, 4, 5, 5, 5, 5, 4, 2, 3, 4)
# bin 1 : 2*19 + 79 | Rest: 3
# bin 2 : 135 | Rest: 2
# bin 3 : 79 + 135 | Rest: 6
# bin 4 : 2*19 + 79 + 135 + 160 | Rest: 8
# bin 5 : 3*103 + 135 | Rest: 36
}
Run the code above in your browser using DataLab