问题描述
check_for_unkNown_vars_impl(model,the_ast)中的错误:表达式包含的变量不属于模型。
我真的找不到问题所在。代码如下所示:
#load the data of distance from factory to distribution center
distance_factorydc <- read_excel("Autoparts24.xlsx",sheet="Factories_to_DCs")
distance_factorydc<-distance_factorydc[,-1]
#load the data of distance from distribution center to customers
distance_dccustomer<- read_excel("Autoparts24.xlsx",sheet="DCs_to_Customers")
distance_dccustomer<-distance_dccustomer[-c(5:6),-1]
#the demand of the customer
customerdemand<- read_excel("Autoparts24.xlsx",sheet="DCs_to_Customers")
customerdemand<-customerdemand[6,-1]
# the ILP model is created
model <- MIPModel() %>%
# set F as a continuous variables
# Fij is the amount that shipped from factory i to distribution center j
add_variable(F[i,j],i = 1:3,j = 1:4,type = "continuous",lb = 0) %>%
# set C as a continuous variables
# Cjk is the amount that shipped from distribution center j to customer k
add_variable(C[j,k],j =1:4,k = 1:30,lb = 0) %>%
# minimize the total cost
set_objective(sum_expr(distance_factorydc[i,j]* F[i,j = 1:4) +
sum_expr(distance_dccustomer[j,k]* C[j,k = 1:30),"min")%>%
# the total amount that shipped from distribution center to customers
# should >= the total demand of the customer
add_constraint(sum_expr(F[j,j=1:4) >= sum_expr(customerdemand[k],k=1:30)) %>%
# the total amount that shipped from factory to distribution center should be the same as
# the amount that shipped from distribution center to customers
add_constraint(sum_expr(F[i,i = 1:3) >= sum_expr(C[j,j=1:4)
解决方法
我不确定要导入的数据的格式,但是distance_factorydc,distance_dccustomer,and customerdemand
变量似乎是模型中的问题。确保模型中的索引引用正确的行和列。您也可以将它们转换为矢量,然后在模型中公式化。
add_constraint(sum_expr(F[j,k],j=1:4) >= sum_expr(customerdemand[k],k=1:30))
对我来说可疑:
-
F[j,k]
应该类似于F[i,j]
-
k
中的F[j,k]
不受控制,因为它位于sum_expr(customerdemand[k],k=1:30)
内
此代码需要多加注意。