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)

Installing Pyomo Trunk Using Pip

Need to try out a new feature or fix that will be in the next Pyomo release?  Then you will want to install Pyomo from trunk. In most cases, this can be accomplished using the pyomo_install script available at https://software.sandia.gov/trac/pyomo/downloader.  This post will review how to use this script, but also introduce another method for installing from trunk that uses the pip command.

Using pyomo_install:

To install from trunk using the pyomo_install script, one must execute a shell command such as the following:

$ python pyomo_install --trunk --venv pyomo_python

The --venv option in this command tells the install script to create a new python installation in a directory named pyomo_python.  If the installation was successful, the pyomo_python directory will contain a sub-directory named bin.  Inside bin there will be a new python executable along with other Pyomo commands.  The python executable inside this directory needs to be used when launching scripts that import Pyomo packages.  You might find it preferable to just make this your default python by adding this bin directory to the beginning of your PATH environment variable.  Be aware that installing trunk requires that the svn command (subversion) be available in your command shell.

Using pip:

On some systems, including Windows, you might run into issues with the above installation method.  A nice alternative to using the pyomo_install script is to use the Python-based package manager pip.  With more recent versions of Python, pip comes installed by default.  If you have an older version Python (some versions of 2.7 and earlier), you must first obtain pip by downloading and executing the get-pip.py script found at https://pip.pypa.io/en/stable/installing.  In addition to pip, the git command also needs to be available from your command shell.

Once the pip and git commands are available, installation of Pyomo trunk proceeds as follows:

  1. Install the master branch of PyUtilib from GitHub using pip:

    $ pip install git+https://github.com/PyUtilib/pyutilib

  2. Install the master branch of Pyomo from GitHub using pip:

    $ pip install git+https://github.com/Pyomo/pyomo

If in the future you ever need to remove Pyomo and Pyutilib, simply execute the command:

$ pip uninstall Pyomo Pyutilib

To update to a new Pyomo release, execute the command:

$ pip install -U Pyomo

If you are on Windows, the Pyomo installation process will create a sub-directory named 'Scripts' inside of the directory where Python is installed on your system.  Although your system Python will already be able to execute Pyomo scripts, the Scripts sub-directory will contain the other various Pyomo tools that you might find useful (such as the pyomo command).  Adding this directory to your PATH will make these tools available the next time you open a command shell.  The screenshot below shows how I edited my PATH environment variable on Windows 8 for a Python2.7 installation located in C:\Python27.

(Update: This post was edited after the git "pyomo" development branch of Pyutilib was permanently merged with the git master branch of Pyutilib)

(Update: This post was edited after Pyomo was moved onto GitHub)

Pyomo-fest Aachen, Germany

Users and potential users are invited to attend a workshop concerning Pyomo. We can change the schedule a little to accommodate participants but the plan is as follows:
Wednesday, 16. March: Tutorials for new users and review of new features for advanced users
Thursday, 17. March: User presentations and advanced topics
Friday, 18. March: Collaboration and advanced topics

The workshop will be held at RWTH Aachen:
Aixtron A
Ground floor, Room number Se1
Kackerstraße 15, Aachen, Germany

If you plan to participate, please send an email to DLWoodruff@UCDavis.edu

Organizers:
Alexander Mitsos; Aachen University; alexander.mitsos@avt.rwth-aachen.de
Bill Hart; Sandia National Laboratories; wehart@sandia.gov
Carl Laird; Purdue University; lairdc@purdue.edu
John Siirola; Sandia National Laboratories; jdsiiro@sandia.gov
Jean-Paul Watson: Sandia National Laboratories; jwatson@sandia.gov
David Woodruff; UC Davis; dlwoodruff@ucdavis.edu

Checking GLPK installation

To use the solver glpk with Pyomo, it must be installed so the command glpsol --help can be given on the command line from any directory. That is pretty easy to do on a Unix machine, but a little trickier on Windows. In this video we show you how it looks if you have glpk on the machine, but not fully installed. The video does not show you how to fully install it. That is not really a Pyomo issue and there is more than one way, but you can google that. Here's a video link:
 http://dlwoodruff.ucdavis.edu/badGLPK.mp4

Announcing Release of Pyomo 4.1

We are pleased to announce the release of Pyomo 4.1 (4.1.10519). Pyomo is a collection of Python software packages that supports a diverse set of optimization capabilities for formulating and analyzing optimization models.

The following are highlights of this release:

- Modeling
   * API changes for model transformations
   * Revised API for SOSConstraint, Suffix and Block components
   * Optimization results are now loaded into models
   * Removed explicit specification of model preprocessing

- Solvers
   * Resolved many issues with writing and solving MPECs
   * Changes to MPEC meta-solver names
   * The solution output for runph has been changed to

- Other
   * Pyomo subcommands can now use configuration files (e.g. pyomo solve config.json)
   * New JSON/YAML format for parameter data
   * Added a script to install pyomo.extras

See http://pyomo.org for installation options and documentation for getting started with Pyomo.

Enjoy!

About Pyomo

The Pyomo home page provides resources for Pyomo users:

Pyomo development is hosted by Sandia National Laboratories and COIN-OR:

See the Pyomo Forum for online discussions of Pyomo:

 

Video Instructions for Installing on Windows

I created video with instructions for installing Pyomo on Windows. The video is too long, but might help if you are having trouble. The exact behavior of things depends on your browser and its configuration. Note that when I click on get-pip.py the programs comes into a browser tab rather than being downloaded to the Downloads directory so I simply save the file to the downloads directory. Maybe your browser will behave differently. Ultimately, the goal is to download get-pip.py and run it and then download get-pyomo.py and run it. The way I prefer to run them is from the command prompt. Here is the URL for the video: http://dlwoodruff.ucdavis.edu/PyomoWindows.mp4

Trunk under Python 3.4 on Ubuntu

This is only for trunk users of Pyomo on Ubuntu (and maybe Debian). If you don't know why you want trunk, then you probably don't want it and you can totally ignore this post.

Here is what I did to install pyomo trunk using python3.4 on Ubuntu 14.04; March 2015

Caveats:

0. If you want python2.7 don't use these steps.

1. There is no reason to think these would be anything like the steps on a mac

2. I am tired of working on this, so I have not done the experiments to find out which steps could be omitted or simplified

3. Obviously, if you have already installed numpy, etc. into 3.4 or you don't want numpy etc. you can skip those steps

4. There might be typos (I did not paste these in)

sudo rm -r /usr/bin/python

sudo ln -s /usr/bin/python3.4 /usr/bin/python

sudo apt-get install python3-numpy

sudo apt-get install python3-scipy

sudo apt-get install python3-matplotlib

sudo apt-get install python3-pip

==== (you can get xlrd later if you need it)

=== Edit pyomo_install to comment out two lines and add one line:

    ### install_pip(upgrade=options.zipfile is None, user=False, quiet=not options.verbose)

    ### pip = find_pip(pyomo_install_tempdir)

    pip = "pip"

python pyomo_install –trunk --venv=pyomo

sudo rm -r /usr/bin/python

sudo ln -s /usr/bin/python2.7 /usr/bin/python

=== make sure your .bashrc file puts pyomo/bin very early in the path

=== open a new terminal

Announcing Release of Pyomo 4.0.9682

We are pleased to announce the release of Pyomo 4.0 (4.0.9682). Pyomo is a collection of Python software packages that supports a diverse set of optimization capabilities for formulating and analyzing optimization models.

This release patch release for Pyomo 4.0.9638.  The following are highlights of this release:

- Modeling
   * Added a ComplementarityList component

- Solvers
   * PySP test baseline updates

- Other
   * Bug fix for pickling results objects
   * Performance fix for ordered sets
   * Resolving a bug that disabled pyodbc support.
   * Added preliminary support for pypyodbc.
   * Fix to enable sets to be initialized with a tuple
   * Re-enabled the 'import' and 'export' Pyomo data commands, which
     are still deprecated.
   * Various performance enhancements to avoid iterating over list of
     dictionary keys.

See http://pyomo.org for installation options and documentation for getting started with Pyomo.

Enjoy!

 - Pyomo Developer Team
 - pyomo-developers@googlecode.com
 - http://pyomo.org


-----------
About Pyomo
-----------

The Pyomo home page provides resources for Pyomo users:

 * http://pyomo.org

Pyomo development is hosted by Sandia National Laboratories and COIN-OR:

 * https://software.sandia.gov/pyomo
 * https://projects.coin-or.org/Pyomo

See the Pyomo Forum for online discussions of Pyomo:

 * http://groups.google.com/group/pyomo-forum/

 

Accessing solver status and termination conditions

Pyomo has been designed to provide high level scripting capabilities for users to quickly and easily write meta-algorithms using off the shelf solvers. Users writing these scripts often need to make decisions based on the status or the termination condition obtained from a solver. To this aim, Pyomo provides a standardized set of objects that can be used to query solver information regardless of the solver being used. The following code snippet provides a typical scenario where SolverStatus and TerminationCondition objects can be useful. 


from pyomo.opt import SolverStatus, TerminationCondition

…

results = opt.solve(instance) # Solving a model instance  
instance.load(results) # Loading solution into results object

if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
    # Do something when the solution in optimal and feasible
elif (results.solver.termination_condition == TerminationCondition.infeasible):
    # Do something when model in infeasible
else:
    # Something else is wrong
    print “Solver Status: ”,  result.solver.status


The following table provides a complete list of all available solver statuses and termination conditions.

Solver Statuses
ok Normal termination
warning Termination with unusual condition
error Terminated internally with error
aborted Terminated due to external conditions (e.g. interrupts)
unknown Unknown (an uninitialized value)
Termination Conditions
maxTimeLimit Exceeded maximum time limit allowed
maxIterations Exceeded maximum number of iterations allowed
minFunctionValue Found solution smaller than specified function value
minStepLength Step length is smaller than specified limit
globallyOptimal Found a globally optimal solution
locallyOptimal Found a locally optimal solution
optimal Found an optimal solution
maxEvaluations Exceeded maximum number of problem evaluations (e.g., branch and bound nodes)
other Other, uncategorized normal termination
unbounded Demonstrated that problem is unbounded
infeasible Demonstrated that problem is infeasible
invalidProblem The problem setup or characteristics are not valid for the solver
solverFailure Solver failed to terminate correctly
internalSolverError Internal solver error
error Other error
userInterrupt Interrupt signal generated by user
resourceInterrupt Interrupt signal in resources used by the solver
licensingProblem Problem accessing solver license

Troubles with Anaconda 2.7 and Pyro

I installed the most recent version of Anaconda 2.7 and then the trunk version of Pyomo. This results in an internal error in Pyro that is trapped by Pyomo and reported as an absence of Pyro. I don't know what the fix is because I just used my system Python to install trunk on the machine where I want to use Pyro.  If you are not going to use Pyro, then you can just ignore this blog post :)

Announcing Release of Pyomo 4.0

We are pleased to announce the release of Pyomo 4.0 (4.0.9629). Pyomo is a collection of Python software packages that supports a diverse set of optimization capabilities for formulating and analyzing optimization models.

This release rebrands Coopr as Pyomo, which reflects the fact that users have consistently confused the Coopr software and the Pyomo modeling language.  Pyomo 4.0 includes the following significant changes:

  • Pyomo provides a single source tree replacing all Coopr packages
  • The 'pyomo' command replaces the 'coopr' command
  • The 'pyomo solve' subcommand replaces the former 'pyomo' command
  • The 'pyomo.environ' package is now used to import core Pyomo capabilities
  • Robust support for Python 3.x

The following are highlights of this release:

  • Modeling
    • Added a RealInterval domain
    • Major rework of coopr.dae. Can now represent higher order and partial differential equations. Also added more discretization schemes.
  • Solvers
    • Added preliminary support for a Benders solver
    • Added support for the BARON solver
    • Preliminary support for MPEC solvers, including the PATH solver
  • Transformations
    • Added explicit support for model transformations
    • The 'pyomo solve --transform' option specified model transformations
    • Created a streamline linear dual transformation
  • Other
    • The 'pyomo help' command documents installed capabilities
    • Major rework and simplification of the 'pyomo_install' script
    • Added support for parallelism using Pyro4

See http://pyomo.org for installation options and documentation for getting started with Pyomo.

Enjoy!

  • Pyomo Developer Team
  • pyomo-developers@googlecode.com
  • http://pyomo.org

About Pyomo

The Pyomo home page provides resources for Pyomo users:

Pyomo development is hosted by Sandia National Laboratories and COIN-OR:

See the Pyomo Forum for online discussions of Pyomo:

Coopr is being renamed as Pyomo

The Coopr software will soon be renamed as Pyomo! Coopr has been an umbrella software project that includes Pyomo and other software components. However, most users first and foremost use Coopr's Pyomo modeling package. Thus, many users describe their model as a "Pyomo model" even when it relies on advanced modeling extensions in Coopr. In fact, even Coopr developers often discuss "Pyomo" developments.

Given this confusion, we have decided to clearly brand this software as Pyomo. Later this fall, Pyomo 4.0 will be released. The source repository will be changed to reflect the name change. A new Pyomo home page will be hosted at http://pyomo.org, and the Pyomo Trac site will be reworked to more clearly be a site for Pyomo developers.

There will also be some new features in Pyomo 4.0. More details coming soon!