dirkschumacher / ompr

R package to model Mixed Integer Linear Programs
https://dirkschumacher.github.io/ompr/
Other
268 stars 35 forks source link

Error in ompr::new_solution #391

Closed BajczA475 closed 2 years ago

BajczA475 commented 2 years ago

I get the error in the title when I try to solve the following model:

library(Matrix)
library(slam)
library(dplyr)
library(ROI)
library(ompr)
library(ompr.roi)
library(Rglpk)
library(ROI.plugin.glpk)

set.seed(100)
boats = rpois(100*100, 2)
keep = sample(c(0,1), 100*100, replace=T, prob = c(0.8, 0.2))
boat.dat = boats*keep

boats.n.ij = Matrix(boat.dat, nrow=100, ncol=100, sparse =T)
diag(boats.n.ij) = 0

boats.n.ij[1:10, 1:10]

n.ij = 100
B = 5

mod1 = MIPModel() %>% 
  add_variable(mu[i, j], type = "binary", i = 1:n.ij, j = 1:n.ij) %>%
  add_variable(x[i], type = "binary", i = 1:n.ij) %>% 
  add_variable(x[j], type = "binary", j = 1:n.ij) %>% 
  add_constraint(x[i] + x[j] >= mu[i, j], i = 1:n.ij, j = 1:n.ij) %>% 
  add_constraint(sum_expr(x[i], i = 1:n.ij) <= B)

mod.original = mod1 %>% 
  set_objective(sum_expr(mu[i, j] * boats.n.ij[i, j], i = 1:n.ij, j = 1:n.ij))

mod.original.solved = mod.original %>% 
  solve_model(with_ROI("glpk", verbose=TRUE))

The error code I get is as follows:

---- GLPK Simplex Optimizer, v4.47 10001 rows, 10100 columns, 30000 non-zeros * 0: obj = 0.000000000e+000 infeas = 0.000e+000 (0) * 500: obj = 3.190000000e+002 infeas = 0.000e+000 (0) * 1000: obj = 4.230000000e+002 infeas = 0.000e+000 (0) * 1500: obj = 4.940000000e+002 infeas = 0.000e+000 (0) * 1895: obj = 5.290000000e+002 infeas = 0.000e+000 (0) OPTIMAL SOLUTION FOUND GLPK Integer Optimizer, v4.47 10001 rows, 10100 columns, 30000 non-zeros 10100 integer variables, all of which are binary Integer optimization begins... + 1895: mip = not found yet <= +inf (1; 0) + 1901: >>>>> 5.290000000e+002 <= 5.290000000e+002 0.0% (1; 0) + 1901: mip = 5.290000000e+002 <= tree is empty 0.0% (0; 1) INTEGER OPTIMAL SOLUTION FOUND ---- Error in ompr::new_solution(status = status, model = model, objective_value = result$objval + : unused argument (additional_solver_output = list(ROI = list(status = result$status, message = result$message)))

I might be using outdated syntax? It looks like you've been updating things regularly and I might just not be keeping up! I can say I wasn't having this problem a few months ago with the same syntax.

dirkschumacher commented 2 years ago

Hmm. Seems like the two ompr packages are out of sync. Try reinstalling them:

remotes::install_github("dirkschumacher/ompr")
remotes::install_github("dirkschumacher/ompr.roi")
library(Matrix)
library(slam)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ROI)
#> ROI: R Optimization Infrastructure
#> Registered solver plugins: nlminb, alabama, glpk, lpsolve, quadprog.
#> Default solver: auto.
library(ompr)
library(ompr.roi)
library(Rglpk)
#> Using the GLPK callable library version 4.65
library(ROI.plugin.glpk)

set.seed(100)
boats = rpois(100*100, 2)
keep = sample(c(0,1), 100*100, replace=T, prob = c(0.8, 0.2))
boat.dat = boats*keep

boats.n.ij = Matrix(boat.dat, nrow=100, ncol=100, sparse =T)
diag(boats.n.ij) = 0

boats.n.ij[1:10, 1:10]
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#>                          
#>  [1,] . . . 3 . . . 2 . .
#>  [2,] . . 5 . . . . . . .
#>  [3,] . . . . . . . . . .
#>  [4,] . . 3 . . . . . . .
#>  [5,] . . . . . . . . . .
#>  [6,] 2 . . 5 . 0 . . . .
#>  [7,] . . . . . . . 1 . .
#>  [8,] . . . . . . 2 . . .
#>  [9,] . . . . . 3 . . 0 1
#> [10,] 1 . . . . . . . . .

n.ij = 100
B = 5

mod1 = MIPModel() %>% 
  add_variable(mu[i, j], type = "binary", i = 1:n.ij, j = 1:n.ij) %>%
  add_variable(x[i], type = "binary", i = 1:n.ij) %>% 
  add_variable(y[j], type = "binary", j = 1:n.ij) %>% 
  add_constraint(x[i] + y[j] >= mu[i, j], i = 1:n.ij, j = 1:n.ij) %>% 
  add_constraint(sum_expr(x[i], i = 1:n.ij) <= B)

mod.original = mod1 %>% 
  set_objective(sum_expr(mu[i, j] * boats.n.ij[i, j], i = 1:n.ij, j = 1:n.ij))

mod.original.solved = mod.original %>% 
  solve_model(with_ROI("glpk", verbose=TRUE))
#> <SOLVER MSG>  ----
#> GLPK Simplex Optimizer, v4.65
#> 10001 rows, 10200 columns, 30100 non-zeros
#> *     0: obj =  -0.000000000e+00 inf =   0.000e+00 (1682)
#> Perturbing LP to avoid stalling [247]...
#> Removing LP perturbation [2141]...
#> *  2141: obj =   3.911000000e+03 inf =   1.118e-14 (0) 3
#> OPTIMAL LP SOLUTION FOUND
#> GLPK Integer Optimizer, v4.65
#> 10001 rows, 10200 columns, 30100 non-zeros
#> 10200 integer variables, all of which are binary
#> Integer optimization begins...
#> Long-step dual simplex will be used
#> +  2141: mip =     not found yet <=              +inf        (1; 0)
#> +  2141: >>>>>   3.911000000e+03 <=   3.911000000e+03   0.0% (1; 0)
#> +  2141: mip =   3.911000000e+03 <=     tree is empty   0.0% (0; 1)
#> INTEGER OPTIMAL SOLUTION FOUND
#> <!SOLVER MSG> ----

Created on 2022-01-21 by the reprex package (v2.0.1)

BajczA475 commented 2 years ago

You're right! It looks like ompr.roi installed correctly but not ompr and I hadn't noticed. I must have had an R session running somewhere with ompr turned on. Thanks for the quick help!

The solution it finds isn't the one I want though...The idea is to limit sum(x[i]) to 5 or less using the constraint B, which is what happens, but this should then limit sum(mu[i,]) to 5 or less also, but that doesn't happen as intended...The solution should look like a tic-tac-toe pattern with B rows and B columns of mu as 1s and all others as 0s. This worked ok when I could specify two x variables but I'll need to figure out how to achieve the same outcome with an x and a y instead. Anyhow--that's for me to figure out! Thanks for the help!

dirkschumacher commented 2 years ago

Great. If you have any questions post them in the Q&A section under Discussions.