微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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 举报,一经查实,本站将立刻删除。