Thursday, January 10, 2019

Knapsack problem in R

Problem definition

You are going to spend a month in the wilderness. You’re taking a backpack with you, however, the maximum weight it can carry is 20 kilograms. You have a number of survival items available, each with its own number of “survival points”. You’re objective is to maximize the number of survival points.

Code

library(genalg)
library(ggplot2)

dataset <- bag="" beans="" br="" compass="" data.frame="" item="c(" pocketknife="" potatoes="" rope="" sleeping="" unions="">                      survivalpoints = c(10, 20, 15, 2, 30, 10, 30),
                      weight = c(1, 5, 10, 1, 7, 5, 1))
weightlimit <- 10="" br="">
chromosome = c(1, 0, 0, 1, 1, 0, 0)
dataset[chromosome == 1, ]

evalFunc <- br="" function="" x="">  current_solution_survivalpoints <- br="" dataset="" survivalpoints="" x="">  current_solution_weight <- br="" dataset="" weight="" x=""> 
  if (current_solution_weight > weightlimit)
    return(0) else return(-current_solution_survivalpoints)
}

iter = 100
GAmodel <- br="" iters="iter," mutationchance="0.01," popsize="200," rbga.bin="" size="7,">                    elitism = T, evalFunc = evalFunc)

cat(summary(GAmodel))
plot(GAmodel)

solution = c(1, 1, 0, 1, 1, 1, 1)
dataset[solution == 1, ]

# solution vs available
cat(paste(solution %*% dataset$survivalpoints, "/", sum(dataset$survivalpoints)))

animate_plot <- br="" function="" x="">  for (i in seq(1, iter)) {
    temp <- br="" data.frame="" eneration="c(seq(1," i="" mean="" seq="" variable="c(rep(">                                                                              i), rep("best", i)), Survivalpoints = c(-GAmodel$mean[1:i], -GAmodel$best[1:i]))
   
    pl <- aes="" br="" ggplot="" group="Variable," temp="" x="Generation," y="Survivalpoints,">                           colour = Variable)) + geom_line() + scale_x_continuous(limits = c(0,
                                                                                             iter)) + scale_y_continuous(limits = c(0, 110)) + geom_hline(y = max(temp$Survivalpoints),
                                                                                                                                                          lty = 2) + annotate("text", x = 1, y = max(temp$Survivalpoints) +
                                                                                                                                                                                2, hjust = 0, size = 3, color = "black", label = paste("Best solution:",
                                                                                                                                                                                                                                       max(temp$Survivalpoints))) + scale_colour_brewer(palette = "Set1") +
      opts(title = "Evolution Knapsack optimization model")
   
    print(pl)
  }
}

# in order to save the animation
install.packages("animation")
library(animation)
saveMovie(animate_plot(), interval = 0.1, outdir = getwd())


 

No comments: