Skip to content

More on Sparse Variable/Constraint Generation #412

Answered by dirkschumacher
sbmack asked this question in Q&A
Discussion options

You must be logged in to vote

add_variable_ is deprecated.

You can use "parallel sequences" from the {listcomp} package:

library(stringr)
library(ompr)
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)
mat3 <- dplyr::filter(mat2, active !=0)
colnames(mat3) <- c('plant', 'prod', 'ware', 'active')
nrows <- nrow(mat3)
cost <- sample(30:300, nrows, replace = TRUE)
mat4 <- cbind(mat3, cost)

MIPModel() |> 
  add_variable(x[i, j, k], list(i = mat4$plant, j = mat4$prod, k = mat4$ware)) |> 
  vari…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@sbmack
Comment options

@dirkschumacher
Comment options

Answer selected by sbmack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants