the question was to find the minimum distance-quantity value, however, the code keep showing up 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.

I really cannot find what's the problem. The code is showed as following:

#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) 
               
2

There are 2 best solutions below

0
On

I am unsure about how the data you are importing is formulated but distance_factorydc, distance_dccustomer, and customerdemand variables seem to be the problems in the model. Make sure the indexes in the model are referring to correct rows and columns. You may also convert them and formulate them in the model as vectors.

0
On

add_constraint(sum_expr(F[j, k],j=1:4) >= sum_expr(customerdemand[k],k=1:30))

looks suspicious to me:

  • F[j,k] should be something like F[i,j]
  • The k in F[j,k] is not under control as it is inside sum_expr(customerdemand[k],k=1:30)

This code needs a bit more care.