0

I am working on an allocation problem using R s ompr package. The aim is to allocate Products to production lines. I have written the following code for this:

volume <- product$VolumeQ4          #The volume of quarter 4 of each of the products
capacity <- lines$Capacity          #The capacity of the line in quarter 4
k <- length(product$k)              #The products (k) 54 in total 
l <- length(lines$l)                #The lines (l) 11 in total
product_lines_matrix                #this is a 54x11 dataframe showing if a line is capable 
                                    #to produce a product, 1 = if capable, 0 if not capable. 

model <- MIPModel() %>%
  
# 1 if product i  is assigned to line j 
  
add_variable(x[i, j], i = 1:k, j = 1:l, type = "binary") %>%
  
  # # objective is to assign all skus to lines where the line is capable
  
  set_objective(sum_expr(product_lines_matrix[i, j] * x[i, j], i = 1:k, j = 1:l), "max") %>%
  
  # each product needs to be assigned to line 
  
  add_constraint(sum_expr(x[i, j], j = 1:l) == 1, i = 1:k) %>% 
  
   # we cannot exceed the Q4 capacity of a line
  
  add_constraint(sum_expr(x[i, j]*volume[i], i = 1:k) <= capacity[j], j = 1:l) 

I am getting the following error code

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

I am not used to modeling MIP in general, let alone in R, but would appreciate any help I can get on this! Any ideas where I go wrong ?

  • It may help if we could reproduce the problem. – Erwin Kalvelagen Nov 10 '20 at 13:20
  • I know this is fairly old, but when you get this error, you can run `traceback()` and it will show which constraint/variable/objective caused the error. I tried with sample data with dimensions you described and didn't get an error. My next place to look is to see if your data is formatted in the right dimensions. Are `volume` and `capacity` vectors of length `k` and `l` respectively and does `product_lines_matrix` have 54 rows and 11 columns? Another thing to check is if it's a tibble instead of a data.frame. When indexed, tibbles return a tibble instead of the cell value. – cookesd Dec 17 '20 at 19:21

0 Answers0