Using Pyomo from R through the magic of Reticulate

Author

Notesofdabbler

Published

July 1, 2020

Pyomo is a python based open-source package for modeling optimization problems. It makes it easy to represent optimization problems and can send it to different solvers (both open-source and commercial) to solve the problem and return the results in python. The advantage of pyomo compared to commercial software such as GAMS and AMPL is the ability to code using standard python syntax (with some modifications for pyomo constructs). Another open source package for modeling optimization problems is JuMP in Julia language.

My goal in this blog is to see how far I can get in terms of using Pyomo from R using the reticulate package. The simplest option would be to develop the model in pyomo and call it from R using reticulate. However, it still requires writing the pyomo model in python. I want to use reticulate to write the pyomo model using R. The details of the blog post (along with code) are in this location.

Summary

Here I covered two examples to show how to develop a pyomo model from R using the reticulate package. While it might still be easier to develop the pyomo model in python (since it was meant to be that way), I found that it is possible to develop pyomo models in R also fairly easily albeit with some modifications (some maybe less elegant compred to the python counterpart). It may still be better to develop more involved pyomo models in python but reticulate offers a way to develop simple to intermediate levels models directly in R. I am summarizing key learnings:

  • Need to overload arithmetic operators to enable things like addition etc. between pyomo objects
  • Use the option convert = FALSE to retain pyomo objects as python objects potentially avoid issues that are hard to troubleshoot.
  • Lack of list comprehension in R makes some of the constraint specifications more verbose but still works.
  • Need to be careful about indexing (sometimes need to explicitly specify a tuple and sometimes not)