‘HiGHS’ optimization solver with OMPR algebraic modeling system: A Small Example

Author

notesofdabbler

Published

September 10, 2022

Code
# load libraries and source functions
library(highs)
library(ompr)
library(ompr.highs)
library(dplyr)

We show an example of using OMPR alebraic modeling system for optimization problems with HiGHS solver using R package highs. The .qmd file is in this location.

Example Problem in highs package

\[ \begin{array}{ll} \min & x_0 + x_1 + 3 \\ & x_1 \leq 7 \\ & 5 \leq x_0 + 2x_1 \leq 15 \\ & 6 \leq 3x_0 + 2x_1 \\ & 0 \leq x_0 \leq 4 \\ & 1 \leq x_1 \end{array} \]

OMPR model

mdl = MIPModel() %>%
      add_variable(x0, lb = 0, ub = 4, type = "continuous") %>%
      add_variable(x1, lb = 1, type = "continuous") %>%
      set_objective(x0+x1+3, sense = "min") %>%
      add_constraint(x1 <= 7) %>%
      add_constraint(x0 + 2*x1 <= 15) %>%
      add_constraint(x0 + 2*x1 >= 5) %>%
      add_constraint(3*x0 + 2*x1 >= 6)
# solve model
s = mdl %>% solve_model(highs_optimizer())

# get solution
s$status
[1] "optimal"
s$objective_value
[1] 2.75
s$solution
  x0   x1 
0.50 2.25 
Code
sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_1.0.10          ompr.highs_0.0.1.9000 ompr_1.0.2           
[4] highs_0.1-2          

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9        compiler_3.6.3    pillar_1.8.1      tools_3.6.3      
 [5] digest_0.6.29     jsonlite_1.8.0    evaluate_0.16     lifecycle_1.0.1  
 [9] tibble_3.1.8      checkmate_2.1.0   lattice_0.20-40   pkgconfig_2.0.3  
[13] rlang_1.0.5       Matrix_1.2-18     cli_3.3.0         yaml_2.3.5       
[17] xfun_0.32         fastmap_1.1.0     listcomp_0.4.1    stringr_1.4.1    
[21] knitr_1.40        generics_0.1.3    htmlwidgets_1.5.4 vctrs_0.4.1      
[25] grid_3.6.3        tidyselect_1.1.2  glue_1.6.2        data.table_1.14.2
[29] R6_2.5.1          fansi_1.0.3       rmarkdown_2.16    purrr_0.3.4      
[33] magrittr_2.0.3    backports_1.4.1   htmltools_0.5.3   utf8_1.2.2       
[37] stringi_1.7.8     lazyeval_0.2.2