Ошибка в check_for_unknown_vars_impl(модель, the_ast): выражение содержит переменную, которая не является частью модели

#r #integer #linear-programming #integer-programming

#r #целое число #линейное программирование #целочисленное программирование

Вопрос:

вопрос заключался в том, чтобы найти минимальное значение расстояния-количества, однако код продолжает показывать следующую ошибку

Ошибка в check_for_unknown_vars_impl(модель, 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, type = "continuous", lb = 0) %>%

# minimize the total cost
set_objective(sum_expr(distance_factorydc[i,j]* F[i, j], i = 1:3, j = 1:4)  
              sum_expr(distance_dccustomer[j,k]* C[j, k], j = 1:4, 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, k],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, j], i = 1:3)  >= sum_expr(C[j, k] , k = 1:30), j=1:4) 
               
  

Ответ №1:

Я не уверен в том, как сформулированы данные, которые вы импортируете, но distance_factorydc, distance_dccustomer, and customerdemand переменные, похоже, являются проблемами в модели. Убедитесь, что индексы в модели ссылаются на правильные строки и столбцы. Вы также можете преобразовать их и сформулировать в модели в виде векторов.

Ответ №2:

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)

Этот код требует немного больше внимания.