dirkschumacher / ompr

R package to model Mixed Integer Linear Programs
https://dirkschumacher.github.io/ompr/
Other
264 stars 35 forks source link

Again, my curious interest is in pre-processing sparse data objects outside of MIPModel if possible. I created a simple sparse supply chain dataframe as an example: #437

Closed sbmack closed 1 year ago

sbmack commented 1 year ago
    Again, my curious interest is in pre-processing sparse data objects outside of MIPModel if possible. I created a simple sparse supply chain dataframe as an example:
nums <- str_pad(1:20, 2, pad = "0")
plant <- paste0("pl", nums[1:3])
prod <- paste0('pr', nums[1:6])
ware <- paste0('wh', nums[1:2])

mat1 <- expand.grid(plant, prod, ware)
set.seed(7)
active <- sample(0:1, nrow(mat1), replace = TRUE, prob = c(0.3, 0.7))
mat2 <- cbind(mat1, active)
colnames(mat3) <- c('plant', 'prod', 'ware', 'active')
mat3 <- dplyr::filter(mat2, active !=0)
nrows <- nrow(mat3)
cost <- sample(30:300, nrows, replace = TRUE)
mat4 <- cbind(mat3, cost)

In this case 3 plants produce 6 common products that are delivered to 2 warehouses. However not all plant, product, warehouse combinations are feasible. mat4 is a sparse dataframe of feasible combinations with a cost vector attached.

For this example, I am thinking that an ideal MIPModel solution for generating those variables would simply be:

model <- MIPModel() %>%
  add_variable(x[mat4[, 1:3]])

Where x is the sparse index set read in directly. I read the help for add_variable:

add_variable(.model, .variable, ..., type = "continuous", lb = -Inf, ub = Inf)

add_variable_(
  .model,
  .variable,
  ...,
  type = "continuous",
  lb = -Inf,
  ub = Inf,
  .dots
)

But it is not clear to me how a .dots filter should be constructed with the current github version of MIPModel to generate a sparse variable set as in this simple sample case.

Comments? Ideas?

Originally posted by @sbmack in https://github.com/dirkschumacher/ompr/discussions/412