More on Sparse Variable/Constraint Generation #412
-
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:
In this case 3 plants produce 6 common products that are delivered to 2 warehouses. However not all plant, product, warehouse combinations are feasible. For this example, I am thinking that an ideal MIPModel solution for generating those variables would simply be:
Where
But it is not clear to me how a Comments? Ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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)) |>
variable_keys()
#> [1] "x[pl02,pr01,wh01]" "x[pl03,pr01,wh01]" "x[pl01,pr02,wh01]"
#> [4] "x[pl02,pr02,wh01]" "x[pl01,pr03,wh01]" "x[pl03,pr03,wh01]"
#> [7] "x[pl01,pr04,wh01]" "x[pl02,pr04,wh01]" "x[pl03,pr04,wh01]"
#> [10] "x[pl02,pr05,wh01]" "x[pl03,pr05,wh01]" "x[pl01,pr06,wh01]"
#> [13] "x[pl02,pr06,wh01]" "x[pl03,pr06,wh01]" "x[pl02,pr01,wh02]"
#> [16] "x[pl03,pr01,wh02]" "x[pl01,pr02,wh02]" "x[pl02,pr03,wh02]"
#> [19] "x[pl03,pr03,wh02]" "x[pl01,pr04,wh02]" "x[pl03,pr04,wh02]"
#> [22] "x[pl01,pr05,wh02]" "x[pl02,pr05,wh02]" "x[pl03,pr05,wh02]"
#> [25] "x[pl01,pr06,wh02]" "x[pl02,pr06,wh02]"
mat4
#> plant prod ware active cost
#> 1 pl02 pr01 wh01 1 129
#> 2 pl03 pr01 wh01 1 195
#> 3 pl01 pr02 wh01 1 277
#> 4 pl02 pr02 wh01 1 200
#> 5 pl01 pr03 wh01 1 153
#> 6 pl03 pr03 wh01 1 278
#> 7 pl01 pr04 wh01 1 32
#> 8 pl02 pr04 wh01 1 210
#> 9 pl03 pr04 wh01 1 117
#> 10 pl02 pr05 wh01 1 70
#> 11 pl03 pr05 wh01 1 210
#> 12 pl01 pr06 wh01 1 293
#> 13 pl02 pr06 wh01 1 70
#> 14 pl03 pr06 wh01 1 60
#> 15 pl02 pr01 wh02 1 215
#> 16 pl03 pr01 wh02 1 103
#> 17 pl01 pr02 wh02 1 203
#> 18 pl02 pr03 wh02 1 269
#> 19 pl03 pr03 wh02 1 143
#> 20 pl01 pr04 wh02 1 61
#> 21 pl03 pr04 wh02 1 224
#> 22 pl01 pr05 wh02 1 262
#> 23 pl02 pr05 wh02 1 84
#> 24 pl03 pr05 wh02 1 101
#> 25 pl01 pr06 wh02 1 207
#> 26 pl02 pr06 wh02 1 47 Created on 2022-02-03 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
add_variable_
is deprecated.You can use "parallel sequences" from the {listcomp} package: