R:如何在投资回报率中将MIP_GAP选项交给glpk



我正在通过R中的roi包使用glpk求解器来求解mip模型。整数优化收敛得非常快,达到真正最优值的 0.1% 以内,但如果有很多整数要求解,则需要很长时间。

出于这个原因,我想将相对 mip 间隙容差(如此处 https://rdrr.io/cran/glpkAPI/man/glpkConstants.html 所述(设置为比默认值更大的值,以便求解器收敛得更快。我知道这可能会产生次优解决方案,但我想了解近似解决方案。

将 mip 间隙选项交给 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选项。

有什么想法吗?多谢。

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

最新更新