使用ompr在R中分配产品到生产线。错误:表达式包含不属于模型的变量
我正在使用R s ompr包处理分配问题。目的是将产品分配到生产线。我为此编写了以下代码:使用ompr在R中分配产品到生产线。错误:表达式包含不属于模型的变量,r,optimization,allocation,ompr,R,Optimization,Allocation,Ompr,我正在使用R s ompr包处理分配问题。目的是将产品分配到生产线。我为此编写了以下代码: 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)
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.
volume如果我们能够重现这个问题,可能会有所帮助。我知道这是一个相当古老的问题,但是当您遇到这个错误时,您可以运行traceback()
,它将显示导致错误的约束/变量/目标。我尝试使用您描述的维度的样本数据,但没有得到错误。我下一步要看的是数据的格式是否正确。volume
和capacity
分别是长度k
和l
的向量,并且产品线矩阵是否有54行11列?另一件要检查的事情是它是否是一个tible而不是data.frame。索引时,TIBLES返回一个TIBLE,而不是单元格值。
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)