0

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!

7
  • It'd help if you describe the constraint in natural or mathematical language. That way users don't have to guess/deduce what you need. Also add packages being used, ompr I believe? Commented Sep 9, 2019 at 20:02
  • @Shree Yes, packages 'ompr' and 'ompr.roi'. Just modified the question to have constraint explained. Thanks! Commented Sep 9, 2019 at 20:46
  • Thanks for adding details but it's still not clear. What doe this mean "attempts happen on days (2, 8), length = 8; attempts happen on (2, 10), length = 3"? I guess x[i] is attempt but what is length? Commented Sep 9, 2019 at 20:52
  • Yes, first step, x2= 1, then length = 2 (First Tuesday). Second step, x8 = 1, scheduled on second Monday, total length equals 8-0 = 8 (Length is days till the second Monday). Total length can be read as days till the last (fourth schedule) Commented Sep 9, 2019 at 20:55
  • Sorry, I give up. You are not providing any context at all for others to understand the problem. I'd suggest clearly defining the problem statement in natural language i.e. inputs, objective, constraints, and decision variables. However, here's my advice: It seems that you need to apply the last constraint over some specific index groupings. Just create that index-set outside of the model and then use it inside sum_expr(). Commented Sep 9, 2019 at 21:03

1 Answer 1

0

Figured out:

  Payment_Model <- MIPModel() %>%

  add_variable(x[i], i = 1:28, type = "binary") %>%
  add_variable(d[t], t = 1:3, type = "binary") %>%

  set_objective(sum_expr(x[i] * appr[i], i = 1: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) - sum_expr(7*d[t], t=1:3) <= 16)


Payment_Model

solution_Sunday <- solve_model(Payment_Model, 
                        with_ROI(solver = "glpk",
                                 verbose = TRUE))


get_solution(solution_Sunday, x[i])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.