#' The knapsack - Greedy Algorithm
#'
#' @description knapsack - Greedy Algorithm
#'
#' @param x as dataframe
#' @param W as numeric
#' @param fast as logical
#'
#' @return list containing value and elements
#' @export greedy_knapsack
#' @useDynLib lab6
#' @import Rcpp
greedy_knapsack <- function(x,W, fast = FALSE ){
if(is.numeric(W)== F || is.data.frame(x) ==F){
stop("Please enter valid inputs")
}
else if(W <= 0){
stop("Please enter weight larger than 0")
}
require(Rcpp)
cppFunction("double vectorSum(NumericVector x){
return std::accumulate(x.begin(), x.end(), 0.0);
}")
x$elements <- as.numeric(rownames(x))
x$vw <- x$v/x$w
x <- x[order(-x$vw),]
x <- x[which(x$w <= W),]
x$weight_sum <- cumsum(x$w)
x <- x[which(x$weight_sum <= W),]
if(fast == TRUE){
knapsackvalue <- vectorSum(x$v)
}else{
knapsackvalue <- sum(x$v)
}
elements <- x$elements
result <- list("value" = round(knapsackvalue), "elements" = elements)
return(result)
}
#RNGkind(sample.kind = "Rounding")
#set.seed(42)
#n <- 2000
#knapsack_objects <- data.frame(w=sample(1:4000, size = n, replace = TRUE), v=runif(n = n, 0, 10000))
#greedy_knapsack(x = knapsack_objects[1: 8,], W = 3500, fast=TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.