Updating to Pyomo 4.x

This post covers some of the major changes that have occurred in Pyomo over the last few release cycles and how to go about updating code that was written with versions of Pyomo older than 4.x.

  •  As of the 4.0 release, Coopr has been renamed to Pyomo. This change requires users to update the package names used to import Pyomo components. Fixing import names is fairly simple. Just find and replace all occurrences of coopr with pyomo. That is, rename coopr.<sub-package> to pyomo.<sub-package>. The one exception, that is also the most common import, is coopr.pyomo. This package now resides under the pyomo.core namespace. However, it is recommended to import pyomo.environ rather than pyomo.core in order to ensure that all Pyomo functionality is properly loaded. Example:

Old Coopr Code:

 from coopr.pyomo import *
 from coopr.opt import SolverFactory

Pyomo 4.x Code:

 from pyomo.environ import *
 from pyomo.opt import SolverFactory
  • Pyomo sub-packages have been consolidated. This change does not require any updates to user code, but it does affect how Pyomo is organized inside of your Python's site-packages directory. It can make for some interesting errors if an older Pyomo or Coopr installation is not fully removed before installing the latest version. The best thing to do is download and run pyomo_uninstall.py if you are unsure how old your installation is. This script will make sure all old and new versions of Pyomo, Coopr, and Pyutilib are completely removed from whatever Python you execute it with. If you already have a Pyomo 4.x version installed, updating to a new release is as simple as executing pip install -U Pyomo from your command shell.
     
  • The solve method on solver plugins now automatically loads results into Pyomo models. This means that there is no need to call load with the results object after the solve completes inside of a Pyomo script. To recover the old behavior, simply add load_solutions=False to the call to solve and use solutions.load_from in place of load to store a solution in a Pyomo model. Example:

Old Coopr Code:

 results = opt.solve(instance)
 instance.load(results)

Pyomo 4.x Code:

 results = opt.solve(instance, load_solutions=False)
 instance.solutions.load_from(results)
  • The call to the preprocess method is no longer necessary prior to solving a Pyomo model that was modified after its initial construction. In old code, this method call was necessary, for example, after adding constraints or fixing variables. If you see it in your updated code, simply delete it. Example:

Old Coopr Code:

 instance.x[1].fix(5.0)
 instance.preprocess()
 results = opt.solve(instance)

Pyomo 4.x Code:

 instance.x[1].fix(5.0)
 results = opt.solve(instance)