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

Method to convert to ROI::OP class #9

Closed
dirkschumacher opened this issue Aug 15, 2017 · 10 comments
Closed

Method to convert to ROI::OP class #9

dirkschumacher opened this issue Aug 15, 2017 · 10 comments

Comments

@dirkschumacher
Copy link
Owner

Create a method to translate an ompr model to a ROI::OP object as suggested in dirkschumacher/ompr#93

@prubin73
Copy link

Right now an optimization model (object created by MIPModel()) apparently has three list entries (variables, objective, constraints). You could add a fourth (model) to hold the OP instance, created lazily (either by solve or write, whichever comes first). With that in place, ompr could define a method ompr::write (replacing the base::write method) or a method ompr::write.model that would call ROI::write.op.

Something similar could be done with a read method (calling ROI::read.op), but that would require parsing the OP object for variables, constraints etc.

@prubin73
Copy link

prubin73 commented Aug 15, 2017

This could also give an easy way to print a model. ROI dispatches the generic print method to print.OP and returns multiple lines of text. Assuming you added a list entry named op to optimization_model, I think the lines

m <- MIPModel()
# ...
s <- capture.output(print(m$op))
s[1] <- "New Title"
s

would return a printable version of the ompr model. (The new title would replace the current reference to an ROI optimization problem.)

@dirkschumacher
Copy link
Owner Author

I would not like not add this to ompr directly. This really belongs in this package here:

It would look something like this:

m <- MIPModel()
# ...
s <- as_OP(m) # this is a ROI::OP object
print(s)

@prubin73
Copy link

I would certainly be happy with a MIPModel to OP converter. One key question: would you generate the OP object, cache it as part of m and expose a pointer to it, or would s be disconnected from m (meaning that changes made to s by the user would not affect m)?

@dirkschumacher
Copy link
Owner Author

Yes, s would be disconnected from m. What do you think of as_ROI_OP as a name?

@prubin73
Copy link

I could live with either as_ROI_OP or as_ROI_model. The latter is more typing but does not require the user to know that the ROI package uses OP as their class name. Your original as_OP is clearly less typing, but it might be confused with ROI::as.OP by some users.

Outside the tidyverse packages, I think the more common punctuation for "as" methods is a period rather than an underscore, so maybe as.ROI.OP or as.ROI.model?

@dirkschumacher
Copy link
Owner Author

I added a first function as_ROI_model. This function is used internally, so it should not be worse, than the usual transformation :)

Also sorry that it took so long.

@prubin73
Copy link

prubin73 commented Sep 5, 2017

Thanks. I just tested it, and it works fine. No worries about the delay. Meanwhile, I have failed miserably trying to find a writer plugin for ROI/CPLEX. Apparently they have some (sparsely documented) methods for registering and then calling OP readers and writers, but there don't seem to be any coded yet. Perhaps they're expecting third parties to code them? So while I can see the full structure of the model (coefficients, bounds, ...), I cannot easily print or export the model. :-(

Maybe someday ...

@dirkschumacher
Copy link
Owner Author

By printing you mean exporting to LP format?

@harryprince
Copy link

I find writing the .mps file is not easy, in that, I mock this example for anyone who wanna transfer MIP modeling in .mps file format.

e.g.


 op_mdl <- ompr::MIPModel() %>%
   ompr::add_variable(x[i, k], i = 1:n, k = 1:m, type = "binary") %>% # 
   ompr::set_objective(ompr::sum_over(x[i, k] * ord_w[i, k] * idst_w[i, k] , i = 1:n, k = 1:m), "max") %>%
   ompr.roi::as_ROI_model()
   
 write_mps <- function(op_mdl, path="~/tmp.mps"){
     tmpfile <- tempfile()

     op_mdl %>%
     ROI::ROI_write(file=tmpfile,type="mps_free",solver = "lpsolve")
     
     readr::write_file(x = 
         readLines(tmpfile) %>% paste0(collapse = "\n"),
         file = path)
     
     rm(tmpfile)
 }
    
  write_mps(op_mdl , path="~/tmp.mps")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants