when I try to run the following script, error message:The expression contains a variable that is not part of the model.
appr <- c(0.15, 0.11, 0.10, 0.13, 0.18, 0.20, 0.18, 0.11, 0.07, 0.06, 0.07, 0.08, 0.12, 0.13,
0.06, 0.03, 0.04, 0.06, 0.07, 0.08, 0.09, 0.07, 0.04, 0.04, 0.05, 0.07, 0.07, 0.07)
leng <- c(1:28)
Payment_Model <- MIPModel() %>%
add_variable(x[i], i = 1:7, type = "binary") %>%
add_variable(x[j], j = 8:14, type = "binary") %>%
add_variable(x[k], k = 15:21, type = "binary") %>%
add_variable(x[l], l = 22:28, type = "binary") %>%
add_variable(d[t], t = 1:3, type = "binary") %>%
set_objective(sum_expr(x[i] * appr[i], i = 1:7) + sum_expr(x[j] * appr[j], j = 8:14)
+ sum_expr(x[k] * appr[k], k = 15:21) + sum_expr(x[l] * appr[l], l = 22:28), "max") %>%
add_constraint(sum_expr(x[i], i = 1:7) == 1) %>%
add_constraint(sum_expr(x[j], j = 8:14) == 1) %>%
add_constraint(sum_expr(x[k], k = 15:21) == 1) %>%
add_constraint(sum_expr(x[l], l = 22:28) == 1) %>%
add_constraint(sum_expr(x[j]*leng[j], j=8:14) >= 7 + 0.01 + sum_expr(x[i]*leng[i], i=1:7) - 10000*(1-d[1])) %>%
add_constraint(sum_expr(x[j]*leng[j], j=8:14) <= 7 + sum_expr(x[i]*leng[i], i=1:7) + 10000*d[1]) %>%
add_constraint(sum_expr(x[k]*leng[k], k=15:21) >= 7 + 0.01 + sum_expr(x[j]*leng[j], j=8:14) - 10000*(1-d[2])) %>%
add_constraint(sum_expr(x[k]*leng[k], k=15:21) <= 7 + sum_expr(x[j]*leng[j], j=8:14) + 10000*d[2]) %>%
add_constraint(sum_expr(x[l]*leng[l], l=22:28) >= 7 + 0.01 + sum_expr(x[k]*leng[k], k=15:21) - 10000*(1-d[3])) %>%
add_constraint(sum_expr(x[l]*leng[l], l=22:28) <= 7 + sum_expr(x[k]*leng[k], k=15:21) + 10000*d[3]) %>%
add_constraint(sum_expr(x[l]*leng[l], l = 22:28) - 7*(d[1]+d[2]+d[3]) <= 16)
Payment_Model
I try to maximize the approval rate during four schedules (see objective), and the parameter table is within a 4-week or 28-day window. Four schedules need to be in (1:7, 8:14, 15:21, 22:28 separately, as first 4 constraints)
I am struggling on the last constraint, how to calculate total length. Here, length means days until the last attempt. 1, 8, 15, 22 mean 'Monday's. For example, attempts happen on days (2, 8), length = 8 (first Tuesday and second Monday); attempts happen on (2, 10), length = 3 (first Tuesday and Wednesday), because if the first Tuesday is scheduled, we could schedule on the same Wednesday, although 10 is originally meaning for the second 'Wednesday'.
And this constraint requests total length for four schedules <= 16. I used big M (10000 here, and slack variable d1 ~ d3 as binary)
What;s the problem of my script? Thanks!
omprI believe?x[i]is attempt but what is length?sum_expr().