如何在R的OMPR软件包中定义3D变量?

问题描述

我正在尝试使用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,type = "binary") %>% # 3 for selection of DC of a specific size
    add_variable(aa[l,l = 1:k,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,q] * colwise(VCustDemand[i+(q-1)*32]),q=1:r) + #Secondary shipments total
       sum_expr(colwise(Vfix_fac[p+o*(j-1)]) * z[p,j = 1:n ) + #DC Fixed costs
        sum_expr(colwise(VCustDemand[i+(q-1)*32]) * x[i,q] * colwise(Vvar_fac_storage[q+(j-1)*r]),q=1:r ) + #Varilable storage costs
        sum_expr(colwise(VCustDemand[i+(q-1)*32]) * x[i,q] * colwise(Vvar_fac_handling[q+(j-1)*r]),q=1:r )  + #DC Variable handling costs
        sum_expr(colwise(Vprimary_log[l+(j-1)*k*r+(q-1)*k]) * aa[l,q=1:r),# + #Primary logistics costs
#        sum_expr(aa[l,q]*colwise(Vplant_var[l+(q-1)*k]),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 = 1:n) == 1,q=1:r) %>% #2. Each Customer is mapped to one DCs for 1 SKU
    add_constraint((sum_expr(x[i,q=1:r)/999) <= y[j],j = 1:n) %>% #3. Grouping constraint connecting #1 & #2
    add_constraint(aa[l,q] >= 0,q = 1:r) %>% #4. Positive outflow from Plant s
    add_constraint(sum_expr(aa[l,l = 1:k) == sum_expr(colwise(VCustDemand[i+(q-1)*32]) * x[i,i = 1:m),q = 1:r)%>% #4 plant outflow should be equal to DC inflow for each SKU
    add_constraint(sum_expr(aa[l,j = 1:n) <= Vtbl_plant_capacity[l+(q-1)*k],q = 1:r) %>%  ##5 Constraint on plant capacity for every SKU
    add_constraint(sum_expr(aa[l,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,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,p = 1:o ) >=
                     sum_expr(colwise(VCustDemand[1:(m*r)]) * x[i,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,l = 1:2) == sum_expr(colwise(VCustDemand[i+(1-1)*32]) * x[i,q = 1)   %>%
    add_constraint(sum_expr(aa[l,l = 1:2) == sum_expr(colwise(VCustDemand[i+(2-1)*32]) * x[i,q = 2)   %>%
    add_constraint(sum_expr(aa[l,l = 1:2) == sum_expr(colwise(VCustDemand[i+(3-1)*32]) * x[i,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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)