dirkschumacher / ompr

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

Error in [[: subscript out of bounds #340

Closed sugs01 closed 2 years ago

sugs01 commented 3 years ago

Dear all, I have a question: The code is normal when run directly, but run wrong when in shiny, could you help me resolve the error. Thanks! image

The code is as follow:

mm <- nrow(sales_customer_operation_sm)
nn <- ncol(sales_customer_operation_sm)
result <- 
   MIPModel() %>%
      add_variable(x[i, j], i = 1:mm, j = 1:nn, type = 'binary') %>%
      set_objective(sum_expr(as.numeric(sales_customer_operation_sm[i, j]) * x[i, j], i = 1:mm, j = 1:nn), sense = 'min') %>%
      add_constraint(sum_expr(x[i, j], j = 1:nn) == 1, i = 1:mm) %>%
      add_constraint(sum_expr(x[i, j], i = 1:mm) >= 4, j = 1:nn) %>%
      add_constraint(sum_expr(x[i, j], i = 1:mm) <= 100, j = 1:nn) %>%
      set_bounds(x[i, j], lb = 0, up = 1, i = 1:mm, j = 1:nn) %>%
      solve_model(with_ROI('glpk', verbose = TRUE))
 mm <- reactive({nrow(sales_customer_operation_sm())})
    nn <- reactive({ncol(sales_customer_operation_sm())})
    result <- reactive({
        MIPModel() %>%
            add_variable(x[i, j], i = 1:mm(), j = 1:nn(), type = 'binary') %>%
            set_objective(sum_expr(as.numeric(sales_customer_operation_sm()[i, j]) * x[i, j], i = 1:mm(), j = 1:nn()), sense = 'min') %>%
            add_constraint(sum_expr(x[i, j], j = 1:nn()) == 1, i = 1:mm()) %>%
            add_constraint(sum_expr(x[i, j], i = 1:mm()) >= input$reference_customer_number_range[1], j = 1:nn()) %>%
            add_constraint(sum_expr(x[i, j], i = 1:mm()) <= input$reference_customer_number_range[2], j = 1:nn()) %>%
            # set_bounds(x[i, j], lb = 0, up = 1, i = 1:mm(), j = 1:nn()) %>%
            solve_model(with_ROI('glpk', verbose = TRUE))
    })

"sales_customer_operation_sm" is a data matrix, as follow:

1229 1177 19 1147
156 48 1168 115
114 0 1159 157
114 0 1159 157
689 582 833 457
114 0 1159 157
114 0 1159 157
1211 1169 104 1154
1223 1109 1312 953
0 114 1211 270
85 34 1159 190
1125 1116 2217 1096
0 114 1211 270
1188 1141 51 1118
1211 1159 0 1130
85 34 1159 190
0 114 1211 270
85 34 1159 190
1188 1141 51 1118
85 34 1159 190
153 220 1118 364
650 556 1399 425
0 114 1211 270
0 114 1211 270
0 114 1211 270
156 48 1168 115
0 114 1211 270
1211 1159 0 1130
1223 1109 1312 953
114 0 1159 157
114 0 1159 157
689 582 833 457
dirkschumacher commented 3 years ago

Thank you! I updated your post to make the code more readable. Could you try to create a minimal, reproducible example using the reprex package to illustrate the problem?

dirkschumacher commented 2 years ago

Feel free to repost your question in the Q&A category of the discussions