R:如何将MIP_GAP选项移交给ROI中的glpk

问题描述 投票:1回答:1

我正在通过R中的roi包使用glpk求解器来求解Mip模型。整数优化可以非常快速地收敛到真正的最优值的0.1%以内的解决方案,但是如果要解决的整数很多,那么它将花费很多时间。

因此,我想将相对Mip间隙公差(如https://rdrr.io/cran/glpkAPI/man/glpkConstants.html所示)设置为比默认值大的值,以便求解器收敛更快。我知道这可能会产生次优的解决方案,但我想对大概的解决方案有所了解。

将mip gap选项移交给glpk时,我收到警告消息,该选项显然被忽略。我的代码和输出有一个小插图示例:

library(magrittr)
library(dplyr)
library(ompr)
library(ROI)
library(Rglpk)
library(ROI.plugin.glpk)
library(ompr.roi)

#fun <- function(big_M){
big_M<-1200
  set.seed(123)
  n_h <- 10
  n_l <- 10
  capacities_1 <- matrix(sample(0:1000, n_l*n_h/2, replace=TRUE),ncol =n_h/2,nrow=n_l)
  capacities_2 <- matrix(sample(0:100, n_l*n_h/2, replace=TRUE),ncol =n_h/2,nrow=n_l)
  capacities <- cbind(capacities_1,capacities_2)
  demand <- 0.9*matrix(apply(capacities,1,sum))
  x_min <- 50
  plant_capacities <- 1.1*matrix(apply(capacities,2,sum))
  u <- matrix(sample(1:100, n_l*n_h, replace=TRUE),ncol =n_h,nrow=n_l)*sign(capacities)
  model <-
    MIPModel() %>%
    add_variable(b[l,h], l = 1:n_l, h= 1:n_h, type = "binary") %>%
    add_variable(x[l,h], l = 1:n_l, h = 1:n_h, type = "continuous",lb =0,ub=1200) %>%
    add_constraint(x_min*b[l,h] <= x[l,h], l=1:n_l, h=1:n_h ) %>%
    add_constraint( x[l,h] <= big_M * b[l,h], l=1:n_l, h=1:n_h ) %>%
    set_objective(sum_expr(u[l,h] * x[l,h], l = 1:n_l, h = 1:n_h)) %>%
    add_constraint(sum_expr(x[l,h], l = 1:n_l) <= plant_capacities[h], h = 1:n_h ) %>%
    add_constraint(x[l,h] <= capacities[l,h] , l = 1:n_l, h = 1:n_h ) %>%
    add_constraint(sum_expr(x[l,h], h = 1:n_h) ==demand[l], l = 1:n_l ) %>%
    solve_model(with_ROI("glpk",verbose=TRUE,tm_limit = 1000 * 360,mip_gap = 1e-6))

  result_table <- get_solution(model, x[l,h])

这给了我

<SOLVER MSG>  ----
GLPK Simplex Optimizer, v4.47
320 rows, 200 columns, 700 non-zeros
      0: obj =  0.000000000e+000  infeas = 2.555e+004 (10)
*   155: obj =  1.350009700e+006  infeas = 0.000e+000 (0)
*   212: obj =  1.422776500e+006  infeas = 0.000e+000 (0)
OPTIMAL SOLUTION FOUND
GLPK Integer Optimizer, v4.47
320 rows, 200 columns, 700 non-zeros
100 integer variables, all of which are binary
Integer optimization begins...
+   212: mip =     not found yet <=              +inf        (1; 0)
+   314: >>>>>  1.387478500e+006 <=  1.388678500e+006 < 0.1% (22; 0)
+   315: >>>>>  1.388678500e+006 <=  1.388678500e+006   0.0% (9; 13)
+   315: mip =  1.388678500e+006 <=     tree is empty   0.0% (0; 43)
INTEGER OPTIMAL SOLUTION FOUND
<!SOLVER MSG> ----
Warning message:
In ROI::ROI_solve(op, solver, ...) :
  the control argument "mip_gap" is not available in solver 'glpk'

通过tm_limit交出时间限制没有问题,因此我很努力地理解为什么它不接受mip_gap选项。

有什么想法吗?非常感谢。

r linear-programming roi glpk mixed-integer-programming
1个回答
0
投票

ROI.plugin.glpk内部使用Rglpk,因此您可以使用Rglpk中记录的控制参数。这意味着您无法设置mip_gap

© www.soinside.com 2019 - 2024. All rights reserved.