dirkschumacher / ompr

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

Error in add_variable, object not found #427

Closed kstegeman closed 2 years ago

kstegeman commented 2 years ago

When trying to model the MIP, I cannot add my variable y and I don't know why this is happening

model <- MIPModel() %>% add_variable(x[i, j], i = 1:n, j = 1:m, type = "binary")

add_variable(y[j], j = 1:m, type = "binary")

I have not piped it currently. Adding variable x is no problem, for y it gives me the following error: Error in addvariable(.model = .model, .variable = lazyeval::as.lazy(substitute(.variable), : object 'y' not found

kstegeman commented 2 years ago

When running the full model:

# next the MILP model is created 
model <- MIPModel() %>%
  # 1 if i gets assigned to facility j
  add_variable(x[i, j], i = 1:n, j = 1:m, type = "binary") %>%

  # 1 if facility j is a collection point
  add_variable(y[j], j = 1:m, type = "binary") %>%

  # 1 if facility j is a pick-up point
  add_variable(u[j], j = 1:m, type = "binary")  %>%

  #C1
  add_variable(C1[j], j=1:m, type = "continuous") %>%

  #C
  add_variable(C2[j], j=1:m, type = "continuous") %>%

  # minimize the total cost
  set_objective(
    (sum
     (ifelse((1-sum_expr(x[i,j], i = 1:n))>= 1,kR, 0))
     )+ 
                S*sum_expr(y[j], j = 1:m) +
                a*sum_expr(C1[j], j = 1:m) +
                T*sum_expr(u[j], j = 1:m) +
                b*sum_expr(C2[j], j = 1:m), "min") 

It gives me the following error Error in check_for_unknown_vars_impl(model, the_ast) : The expression contains a variable that is not part of the model.

However I'm positive I have defined all variables and parameters