How to retrieve log files for GLPK and Gurobi with Pyomo

In this post, I briefly summarise the options I use for retrieving log files for the two solvers GLPK and Gurobi to an arbitrary path when solved from a Python script. Its skeleton looks like this:

import coopr.environ
from coopr.opt.base import SolverFactory
# ...

model = create_your_model()
prob = model.create()
optim = SolverFactory('glpk') 
result = optim.solve(prob)

Unfortunately, the solve arguments are not treated equally for all solvers. Continue reading

Plotting a map directly from postgis using python – Part 2: Plotting a geometry from WKT

If you know how to retrieve geometries as WKT from a postgis database you can go on, otherwise you should start with the first part of this tutorial.

To test the given example you must download the Postgresql dump (geotest_germany) and load it to your local database. The postgis extension has to be installed. Continue reading

Plotting a map directly from postgis using python – Part 1: Retrieve the geometry from postgis

Postgis is a powerful tool to handle spatial data for energy models. To illustrate the results it is useful to plot them into a map. shows how to plot shapefiles with the python module ‘basmap’ (Matplotlib Basemap tutorial 10: Shapefiles Unleached). This post will present the basic steps to create the left plot of the following figure directly from a postgis database. For further information check the tutorials of Thomas Lecocq or leave a comment.


LEFT: Advanced example, RIGHT: Basic example (Left: Average temperature in Germany 2012 from the coastdat2 dataset)

Continue reading

Writing a Pyomo model to LP file with human readable variable names

The fantastic Pyomo package allows to formulate AMPL-style mathematical optimization problems directly in Python. Leveraging the underlying Coopr package, the problems are compatible with a wide range of open source and commercial solvers.


Since the last update (I am now using VOTD.8748), the model writer that converts the Python model object into a plain LP file changed: instead of using the labels used in the code to create the model like this: Continue reading

GNU MathProg example: Storage sizing and operation

This post presents a small, “hello world”-type linear optimization model. This model finds the optimal storage size and use for given demand/supply/price timeseries. It is written in GNU MathProg, a free implementation of the algrebaic modeling language AMPL. As a consequence, there are multiple ways of feeding this model into a solver. Usually, one would use the dekstop version of the GNU GLPK solver. Small models (like this one) can also be solved by a Javascript version that runs (without installation) directly in the browser. Continue reading

How to autoreload packages while using them in IPython

I use IPython to interactively use and debug code that I edit in a text editor at the same time. Unfortunately, Python does not automatically reload packages and functions after an initial import foo, simply for performance reasons.

Fortunately, there is a solution: the IPython extension autoreload does what its name says: either all (how I use it) or only selected (imported using magic function %aimport) are refreshed, whenever I hit the Enter key. Continue reading