Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution ordering in R 3.x #404

Closed
dkutner opened this issue Jan 30, 2022 · 2 comments · Fixed by #407
Closed

Solution ordering in R 3.x #404

dkutner opened this issue Jan 30, 2022 · 2 comments · Fixed by #407
Labels

Comments

@dkutner
Copy link

dkutner commented Jan 30, 2022

I'm running into an issue where ompr 1.0.1 sorts the resulting data frame by i treated as a character in R version 3.6.2. Here x[10] should be 2 with the remaining values 1, but instead x[2] is 2. The same code in R 4.0.3 behaves as expected.

> library(dplyr)
> 
> model <- ompr::MIPModel() %>% 
+     ompr::add_variable(x[i], i = 1:10, type = "continuous", ub = 2) %>% 
+     ompr::add_constraint(x[i] <= 1, i = 1:9) %>% 
+     ompr::set_objective(sum_over(x[i], i = 1:10))
> soln <- ompr::solve_model(model, ompr.roi::with_ROI(solver = "glpk"))
> ompr::get_solution(soln, x[i])
   variable  i value
1         x  1     1
10        x  2     2
2         x  3     1
3         x  4     1
4         x  5     1
5         x  6     1
6         x  7     1
7         x  8     1
8         x  9     1
9         x 10     1
@dirkschumacher
Copy link
Owner

Thanks a lot for reporting this. Will look at it asap (probably tomorrow).

@dirkschumacher
Copy link
Owner

dirkschumacher commented Jan 31, 2022

Ok, the problem is due to stringsAsFactors not being explicitly set to FALSE when created the data.frame. As the CI tests only run on R 4.x, the tests did not fail. They would have failed if R 3.x was enabled.

What I will do to fix and prevent it:

  • Release a fixed version to CRAN
  • Add R 3.4 on Ubuntu as another automated testing Target.
  • Add an explicit test to {ompr.roi}.
  • At {ompr.roi} add R 3.4 as a target as well and run the tests every week, so we detect regressions that might not have been covered by {ompr} tests.
library(magrittr)
library(ROI.plugin.glpk)
model <- ompr::MIPModel() %>% 
   ompr::add_variable(x[i], i = 1:10, type = "continuous", ub = 2) %>% 
   ompr::add_constraint(x[i] <= 1, i = 1:9) %>% 
   ompr::set_objective(sum_over(x[i], i = 1:10))
 soln <- ompr::solve_model(model, ompr.roi::with_ROI(solver = "glpk"))
 ompr::get_solution(soln, x[i])
#>    variable  i value
#> 1         x  1     1
#> 2         x  2     1
#> 3         x  3     1
#> 4         x  4     1
#> 5         x  5     1
#> 6         x  6     1
#> 7         x  7     1
#> 8         x  8     1
#> 9         x  9     1
#> 10        x 10     2

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

dirkschumacher added a commit that referenced this issue Jan 31, 2022
stringsAsFactors was not set explicitly to FALSE
dirkschumacher added a commit that referenced this issue Jan 31, 2022
@dirkschumacher dirkschumacher mentioned this issue Jan 31, 2022
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants