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

MIPompr模型在R中花费太多时间来解决

如何解决MIPompr模型在R中花费太多时间来解决

我正在尝试解决 R 中的设施位置问题。示例数据:

n<- 500 #number of customers
m<- 20 #number of facility centers

set.seed(1234)

fixedcost <- round(runif(m,min=5000,max=10000))

warehouse_locations <- data.frame(
  id=c(1:m),y=runif(m,22.4,22.6),x= runif(m,88.3,88.48)
)

customer_locations <- data.frame(
  id=c(1:n),y=runif(n,22.27,22.99),x= runif(n,88.12,88.95)
)

capacity <- round(runif(m,1000,4000))
demand <- round(runif(n,5,50))

具有成本函数的模型:

library(geosphere)

transportcost <- function(i,j) {
  customer <- customer_locations[i,]
  warehouse <- warehouse_locations[j,]
  (distm(c(customer$x,customer$y),c(warehouse$x,warehouse$y),fun = disthaversine)/1000)*20
}


library(ompr)
library(magrittr)
model <- MIPModel() %>%
  # 1 iff i gets assigned to SC j
  add_variable(x[i,j],i = 1:n,j = 1:m,type = "binary") %>%
  
  # 1 if SC j is built
  add_variable(y[j],type = "binary") %>%
  
  # Objective function
  set_objective(sum_expr(transportcost(i,j) * x[i,j = 1:m) + 
                  sum_expr(fixedcost[j] * y[j],j = 1:m),"min") %>%
  
  #Demand of customers shouldn't exceed total facility capacities
  add_constraint(sum_expr(demand[i] * x[i,i = 1:n) <= capacity[j] * y[j],j = 1:m) %>%
  
  # every customer needs to be assigned to a SC
  add_constraint(sum_expr(x[i,j = 1:m) == 1,i = 1:n) %>% 
  
  # if a customer is assigned to a SC,then this SC must be built
  add_constraint(x[i,j] <= y[j],j = 1:m)
model



library(ompr.roi)
library(ROI.plugin.glpk)
result <- solve_model(model,with_ROI(solver = "glpk",verbose = TRUE))

此时,正在对结果进行计算。

Results

有什么办法可以减少计算时间?如果我理解正确,那么 0.4% 是当前模型与预期结果之间的差异。即使差异远远大于这个,我也会很高兴,我会得到一个合适的模型。有什么办法可以设置吗?像 5-6% 的差异就足够了。

解决方法

您可以尝试以下 3 种方法

  1. 您可以通过重新制定最后一个约束来进行测试。

如果将客户分配给 SC,则必须构建此 SC

您可以使用以下代替当前约束 add_constraint(sum_expr(x[i,j],i = 1:n)

这应该会在不影响输出的情况下减少运行时间。

  1. 除此之外,您还可以根据您希望的最小最优差距或/和您希望模型运行的最长运行时间添加终止条件。

  2. 您也可以尝试使用其他一些求解器代替 glpk 并看到它有帮助。

,

R 和 Python 库对于 MIP 来说非常慢 尝试使用 lp solve 开源求解器

,

从@Erwin Kalvelagen 的评论中获得帮助。使用了symphony解算器并编辑了一行:

library(ROI.plugin.symphony)
result <- solve_model(model,with_ROI(solver = "symphony",verbosity=-1,gap_limit=1.5))

处理时间减少了很多,得到了答案!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。