dirkschumacher / ompr

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

There are variables in your environment that interfere with your defined model variables: x. This can lead to unexpected #334

Closed Khaled9015 closed 3 years ago

Khaled9015 commented 3 years ago

When I run this simple example:-

library(magrittr) library(ompr) library(ROI) library(ROI.plugin.glpk) model <- MIPModel() %>% add_variable( x, type = "continuous") %>% set_objective(x, sense = "max") %>% add_constraint(x <= 5) model model %>% solve_model(with_ROI(solver = "glpk", verbose = TRUE))

I get this error:

There are variables in your environment that interfere with your defined model variables: x. This can lead to unexpected behaviour.There are variables in your environment that interfere with your defined model variables: x. This can lead to unexpected behaviour.There are variables in your environment that interfere with your defined model variables: x. This can lead to unexpected behaviour.Mixed integer linear optimization problem Variables: Continuous: 1 Integer: 0 Binary: 0 Model sense: maximize Constraints: 1

---- GLPK Simplex Optimizer, v4.47 1 row, 1 column, 0 non-zeros ~ 0: obj = 0.000000000e+000 infeas = 2.630e+001 PROBLEM HAS NO FEASIBLE SOLUTION ---- Status: infeasible Objective value: 31.30082 ### GLKP is working fine library(Rglpk) obj <- c(1) mat <- matrix(c(1), nrow = 1) dir <- c("<=" ) rhs <- c(5) types <- c("C" ) max <- TRUE Rglpk_solve_LP(obj, mat, dir, rhs, types = types, max = max) ### give this output $optimum [1] 5 $solution [1] 5 $status [1] 0 $solution_dual [1] 0 $auxiliary $auxiliary$primal [1] 5 $auxiliary$dual [1] 1 $sensitivity_report [1] NA ### sessionInfo() R version 4.0.1 (2020-06-06) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19042) Matrix products: default locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C LC_TIME=English_United States.1252 attached base packages: [1] tools stats graphics grDevices utils datasets methods base other attached packages: [1] Rglpk_0.6-4 slam_0.1-48 ROI.plugin.glpk_1.0-0 magrittr_2.0.1 ompr.roi_0.8.0.9000 ompr_0.8.1.9000 ROI_1.0-0 forcats_0.5.1 [9] stringr_1.4.0 dplyr_1.0.5 purrr_0.3.4 readr_1.4.0 tidyr_1.1.3 tibble_3.1.0 ggplot2_3.3.3 tidyverse_1.3.0 [17] sp_1.4-5 loaded via a namespace (and not attached): [1] colorspace_2.0-0 ellipsis_0.3.1 leaflet_2.0.4.1 rprojroot_2.0.2 fs_1.5.0 httpcode_0.3.0 rstudioapi_0.13 farver_2.1.0 [9] rstan_2.21.2 remotes_2.3.0 fansi_0.4.2 lubridate_1.7.10 xml2_1.3.2 codetools_0.2-16 cachem_1.0.4 knitr_1.31 [17] pkgload_1.2.1 geojsonlint_0.4.0 jsonlite_1.7.2 packrat_0.6.0 broom_0.7.6 dbplyr_2.1.1 compiler_4.0.1 httr_1.4.2 [25] keyring_1.1.0 backports_1.2.1 assertthat_0.2.1 Matrix_1.2-18 fastmap_1.1.0 lazyeval_0.2.2 cli_2.4.0 htmltools_0.5.1.1 [33] prettyunits_1.1.1 gtable_0.3.0 glue_1.4.2 V8_3.4.0 Rcpp_1.0.6 cellranger_1.1.0 vctrs_0.3.7 crul_1.1.0 [41] svglite_2.0.0 crosstalk_1.1.1 xfun_0.22 ps_1.6.0 testthat_3.0.2 rvest_1.0.0 lifecycle_1.0.0 devtools_2.4.0 [49] scales_1.1.1 hms_1.0.0 parallel_4.0.1 inline_0.3.17 yaml_2.2.1 curl_4.3 memoise_2.0.0 gridExtra_2.3 [57] loo_2.4.1 StanHeaders_2.21.0-7 stringi_1.5.3 jsonvalidate_1.1.0 highr_0.8 desc_1.3.0 pkgbuild_1.2.0 matrixStats_0.58.0 [65] rlang_0.4.10 pkgconfig_2.0.3 systemfonts_1.0.1 evaluate_0.14 lattice_0.20-41 htmlwidgets_1.5.3 labeling_0.4.2 processx_3.5.1 [73] tidyselect_1.1.0 geojsonsf_2.0.1 R6_2.5.0 generics_0.1.0 DBI_1.1.1 openrouteservice_0.4.1 pillar_1.5.1 haven_2.3.1 [81] withr_2.4.1 modelr_0.1.8 crayon_1.4.1 utf8_1.2.1 rmarkdown_2.7 progress_1.2.2 usethis_2.0.1 grid_4.0.1 [89] readxl_1.3.1 data.table_1.14.0 callr_3.6.0 reprex_2.0.0 digest_0.6.27 webshot_0.5.2 numDeriv_2016.8-1.1 RcppParallel_5.0.3 [97] stats4_4.0.1 munsell_0.5.0 registry_0.5-1 viridisLite_0.3.0 kableExtra_1.3.4 sessioninfo_1.1.1
Version
crayon 1.4.1
data.table 1.14.0
ellipsis 0.3.1
glue 1.4.2
hms 1.0.0
lazyeval 0.2.2
lifecycle 1.0.0
ompr 0.8.1.9000
pkgconfig 2.0.3
prettyunits 1.1.1
progress 1.2.2
R6 2.5.0
Rcpp 1.0.6
registry 0.5-1
Rglpk 0.6-4
rlang 0.4.10
ROI 1.0-0
slam 0.1-48
vctrs 0.3.7
graphics 4.0.1
grDevices 4.0.1
grid 4.0.1
lattice 0.20-41
Matrix 1.2-18
methods 4.0.1
stats 4.0.1
utils 4.0.1
sbmack commented 3 years ago

There are variables in your environment that interfere with your defined model variables: x.

That refers to global variables/objects in your R (RStudio) environment. If you manually clear the workspace and run your model the warning should not appear. Here are a couple of helpful links:

https://community.rstudio.com/t/how-to-clear-the-r-environment/14303/6)

https://stackoverflow.com/questions/8813753/what-is-the-difference-between-gc-and-rm

Khaled9015 commented 3 years ago

Thanks it worked

rm(list=ls()) gc()

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

result <- MIPModel() %>% add_variable(x, type = "integer") %>% add_variable(y, type = "continuous", lb = 0) %>% set_bounds(x, lb = 0) %>% set_objective(x + y, "max") %>% add_constraint(x + y <= 11.25) %>% solve_model(with_ROI(solver = "glpk")) get_solution(result, x) get_solution(result, y)

dirkschumacher commented 3 years ago

It seems to be fixed