The original Martello and Toth Fortran routines are available in the 'knapsack' package of the Optimist R-Forge project (and not in the 'adagio' package). Unfortunately, these Fortran codes cannot be distributed through CRAN. The reason being that they are published under the ACM license, not compatible with GPL. I asked Prof. Silvano Martello whether he would be willing to change the license, but he could not or did not want to do so (as he explicitly told me).
To give you a start: I guess you have installed R or, even better, R and RStudio. When you have started R, first you have to install the package once and to load it everytime you start R anew with:
(You will need to have a Fortran compiler available, but I guess you have that.)
> install.packages("knapsack", repos="http://R-Forge.R-project.org")
> library(knapsack)
Then you can call help
or example
on, e.g., the knapsack function. The functions implemented at the moment are knapsack
and subsetsum
. The help pages will show you how to apply these routines. p
and w
(the profits and weights) must be vectors of integer values of equal length, with p[i]/w[i]
a strictly decreasing sequence:
> p = c(15, 100, 90, 60, 40, 15, 10, 1)
> w = c( 2, 20, 20, 30, 40, 30, 60, 10)
> cap = 102
Now you can call the knapsack function and display the result:
> res = knapsack(p, w, cap)
> res
# [1] 1 2 3 4 6
There is also the subsetsum routine. Other codes from the book of Martello and Toth have not yet been wrapped in the 'knapsack' package. But it could easily been done if you are interested. These additional routines are solving the bin packing, assignment, and change making problems.