lpSolve icon indicating copy to clipboard operation
lpSolve copied to clipboard

Nonsensical sensitivity range - 0/1 knapsack example

Open efh0888 opened this issue 2 years ago • 0 comments

# simple 0/1 knapsack
item_values <- c(2, 3, 1, 4)
item_weights <- matrix(c(3, 4, 6, 5,
                         1, 0, 0, 0,
                         0, 1, 0, 0,
                         0, 0, 1, 0,
                         0, 0, 0, 1),
                       ncol = 4,
                       byrow = TRUE)
weight_limit <- c(8,
                  rep(1, 4))

lp_solution_2 <- lpSolve::lp("max", 
                             objective.in = item_values, 
                             const.mat = item_weights, 
                             const.dir = rep("<=", 5), 
                             const.rhs = weight_limit, 
                             compute.sens = TRUE,
                             all.int = TRUE)

lp_solution_2$solution  # 1 0 0 1

# sensitivity
sens_ind <- 1:4
data.frame(
  item = sens_ind,
  value_from = round(lp_solution_2$sens.coef.from[sens_ind], 2),
  value_to = round(lp_solution_2$sens.coef.to[sens_ind], 2),
  dual = lp_solution_2$duals[sens_ind]
)

efh0888 avatar Feb 06 '23 18:02 efh0888