Gurobi 9.1.1 Model() class produces unexpected result with lubridate (R) functions
AnsweredI have written a simple R markdown below to explain this issue. I am calling gurobipy in R using reticulate. I format dates in R using year, week number, and day of week. I do not understand why the output to the same R code changes doing the above mentioned date formatting after I call Model() class.
For the year 2016, week number 00, and day of week Tuesday I get "2016-01-05" as output. After I call Model(), I get NA for the same combination.
I would really appreciate if someone can help me understand why I should expect this behavior as I am not privy to Model() class's code.
```{r}
reticulate::use_python("<my-python-path>", required = TRUE)
library(lubridate)
library(tidyverse)
library(reticulate)
```
# Problem
Gurobipy's `Model` class alters behavior of date formatting in R when used with `lubridate` functions `ymd` and/or `mdy_hms`.
## Reproducible example
### Before calling Model()
We want to correct a date `2016-01-01` to align with `Tuesday` as the day of the week. Below code achieves that using %Y (year), %U (week number), and %A (day of week). The value of `start_date_sample` after running the below chunk is `2016-01-05`
```{r}
sample_string <- "1/1/2016 12:00:00 AM"
sample_date <- ymd(mdy_hms(sample_string))
sample_dow <- "Tuesday"
year_week_day <- str_glue("{getyear}-{getweek}-{getday}",
getyear = year(sample_date),
getweek = format(sample_date, "%U"),
getday = sample_dow
)
start_date_sample <- as.Date(year_week_day, format = "%Y-%U-%A")
year_week_day
start_date_sample
```
#### Python version
Same format argument gives a different result in python.
```{python}
import datetime
d = "2016-00-Tuesday"
datetime.datetime.strptime(d, "%Y-%U-%A")
```
### Call Model() of gurobipy
```{python}
from gurobipy import *
m = Model()
```
### After calling Model()
Below chunk gives output of `start_date_sample` as NA. If you refresh the session, comment `sample_date` assignment line and uncomment the line that is already commented, you will get the desired output of `2016-01-05`.
```{r}
sample_string <- "1/1/2016 12:00:00 AM"
sample_date <- ymd(mdy_hms(sample_string))
#sample_date <- as.Date(sample_string, format = "%m/%d/%Y %H:%M:%S")
sample_dow <- "Tuesday"
year_week_day <- str_glue("{getyear}-{getweek}-{getday}",
getyear = year(sample_date),
getweek = format(sample_date, "%U"),
getday = sample_dow
)
start_date_sample <- as.Date(year_week_day, format = "%Y-%U-%A")
year_week_day
start_date_sample
```
#### Python version
Python output is unchanged after calling `Model()`.
```{python}
import datetime
d = "2016-00-Tuesday"
datetime.datetime.strptime(d, "%Y-%U-%A")
```
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
I suspect the Gurobi library is silently changing your locale. This will be fixed in the next major or minor Gurobi release. As a workaround, you can try querying the locale information before calling gurobipy.Model():
Sys.getlocale(category="LC_ALL")
After creating the model, reset the locale to the appropriate value. For example:
Sys.setlocale(category="LC_ALL", locale="en_US.UTF-8")
0 -
Thanks a lot Eli for your prompt response! This indeed resolved my issue. I will keep an eye out on the next releases and will use this solution in the interim.
0 -
Hi Upasana,
Gurobi 9.5 was recently released. Included in this release is a fix for the bug you observed. There is no need to reset the locale explicitly anymore.
We hope this fix works well for you. Thank you for reporting this bug and please let us know if you see any other issues in the future.
Best regards,
Maliheh
0
Post is closed for comments.
Comments
4 comments