1

我正在尝试使用 R 的 OMPR 包解决 MILP 供应网络优化问题。我需要在模型中定义一个 3D 二进制变量 (x[i,j,q]) 以按每个 SKU (q) 将客户 (i) 映射到 DC (j)。有谁知道我该怎么做?

使用我当前的代码,我得到了

Error - in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class ‘structure("LinearVariableCollection", package = "ompr")’ to a data.frame 

我不再收到此错误,但模型未提供正确的输出(已针对 excel 的开放求解器进行验证)。约束 #4 似乎存在一些问题。看起来 VCustDemand 和变量 x[i,j,q] 之间的乘法没有以正确的顺序发生。

model <- MILPModel() %>%
    
    add_variable(y[j],    j = 1:n,              type = "binary")%>% #1: if warehouse j is opened
    add_variable(x[i, j, q],  i = 1:m, j =1:n, q=1:r ,  type = "binary")%>% #2: if i gets assigned  to warehouse j for Product q
    add_variable(z[p, j], p = 1:o, j = 1:n,  type = "binary") %>% # 3 for selection of DC of a specific size
    add_variable(aa[l,j,q], l = 1:k, j =1:n, q=1:r, type = "integer",lb = 0, ub = 500000) %>% # 4 for plant getting mapped to a warehouse for product q
    
    set_objective(
      sum_expr(colwise(Vsec_log[i+(j-1)*m*r+(q-1)*m]) * x[i, j, q] * colwise(VCustDemand[i+(q-1)*32]) , i = 1:m ,j = 1:n, q=1:r) + #Secondary shipments total
       sum_expr(colwise(Vfix_fac[p+o*(j-1)]) * z[p, j] , p = 1:o, j = 1:n ) + #DC Fixed costs
        sum_expr(colwise(VCustDemand[i+(q-1)*32]) * x[i,j,q] * colwise(Vvar_fac_storage[q+(j-1)*r]), i = 1:m , j = 1:n, q=1:r ) + #Varilable storage costs
        sum_expr(colwise(VCustDemand[i+(q-1)*32]) * x[i,j,q] * colwise(Vvar_fac_handling[q+(j-1)*r]), i = 1:m , j = 1:n, q=1:r )  + #DC Variable handling costs
        sum_expr(colwise(Vprimary_log[l+(j-1)*k*r+(q-1)*k]) * aa[l,j,q] , l = 1:k, j = 1:n, q=1:r), # + #Primary logistics costs
#        sum_expr(aa[l,j,q]*colwise(Vplant_var[l+(q-1)*k]) , l = 1:k, j = 1:n, q = 1:r), #Plant variable costs
      sense = "min") %>%
    
    add_constraint(sum_expr(y[j]  , j = 1:n) == 6)%>% #1. No. of open DCs
    add_constraint(sum_expr(x[i, j, q], j = 1:n) == 1, i = 1:m, q=1:r) %>% #2. Each Customer is mapped to one DCs for 1 SKU
    add_constraint((sum_expr(x[i, j, q], i = 1:m, q=1:r)/999) <= y[j], j = 1:n) %>% #3. Grouping constraint connecting #1 & #2
    add_constraint(aa[l,j,q] >= 0, l = 1:k, j = 1:n, q = 1:r) %>% #4. Positive outflow from Plant s
    add_constraint(sum_expr(aa[l,j,q], l = 1:k) == sum_expr(colwise(VCustDemand[i+(q-1)*32]) * x[i,j,q], i = 1:m), j = 1:n, q = 1:r)%>% #4 plant outflow should be equal to DC inflow for each SKU
    add_constraint(sum_expr(aa[l,j,q], j = 1:n) <= Vtbl_plant_capacity[l+(q-1)*k], l = 1:k, q = 1:r) %>%  ##5 Constraint on plant capacity for every SKU
    add_constraint(sum_expr(aa[l,j,q], j = 1:n, q = 1:r) <= Vtbl_Total_plant_capacity[l], l = 1:k) %>%##6 Constraint on overall plant capacity across SKUs
    add_constraint(sum_expr(z[p,j], p = 1:o) == y[j], j = 1:n)   %>% #7. Select one facility per capacity size
    add_constraint(sum_expr(colwise(Vfix_fac_cap[p]) * z[p,j], p = 1:o ) >=
                     sum_expr(colwise(VCustDemand[1:(m*r)]) * x[i,j,q], i = 1:m, q =1:r), j = 1:n) #8. Total DC outflow should be <= Total DC capacity

'''
If I replace the constraint by 
add_constraint(sum_expr(aa[l,j,q], l = 1:2) == sum_expr(colwise(VCustDemand[i+(1-1)*32]) * x[i,j,q], i = 1:m), j = 1:n, q = 1)   %>%
    add_constraint(sum_expr(aa[l,j,q], l = 1:2) == sum_expr(colwise(VCustDemand[i+(2-1)*32]) * x[i,j,q], i = 1:m), j = 1:n, q = 2)   %>%
    add_constraint(sum_expr(aa[l,j,q], l = 1:2) == sum_expr(colwise(VCustDemand[i+(3-1)*32]) * x[i,j,q], i = 1:m), j = 1:n, q = 3)   %>%
'''
The code seems to work

VCustDemand is a 2D data containing customer demand in the following format -

ProductLine  CustomerCity Demand
SKU1         Agra         1755
SKU1         Ahemdabad    7279
SKU1         Delhi        1830
SKU2         Agra         1408
SKU2         Ahemdabad    9111
SKU2         Delhi        4970
SKU3         Agra         1469
SKU3         Ahemdabad    414
SKU3         Delhi        229
4

0 回答 0