Description Usage Arguments Value See Also Examples
View source: R/knapsack_dynamic.R
The knapsack problem is a combinatorial optimization problem. A dataframe is given having two parameters, weight and value. Each value has its own weight and we have to pack items with maximum value and within weight capacity This function should return the same results as the brute force algorithm, but unlike the brute force it should scale much better since the algorithm will run in O<Wn>.
1 | knapsack_dynamic(x,W)
|
x |
is a data frame with two columns w(weight) and v(value) |
W |
is maximum weight(capacity) of knapsack |
list
List of object containing value
giving maximum value of Knapsack out of dataframe and elements
giving weight of
selected elements from data frame x
https://en.wikipedia.org/wiki/Knapsack_problem#0.2F1_knapsack_problem
1 2 3 4 5 6 7 | RNGversion(min(as.character(getRversion()),"3.6.1"))
set.seed(42,kind="Mersenne-Twister",normal.kind = "Inversion")
n <- 2000
knapsack_objects <-data.frame(w=sample(1:4000, size = n, replace = TRUE),
v=runif(n = n, 0, 10000))
l<-knapsack_dynamic(knapsack_objects[1:12,],3500)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.