Tuesday, January 5, 2016

GAMS to Python (2)

A simple, convenient way to move model results from a GAMS model into Python for further postprocessing is via a SQLite database. This looks like a roundabout way but in practice it works quite well.

To export GAMS data to a SQLite database first save all data to a GDX file and then convert this to SQLite. This can be automated using two lines of GAMS code:

execute_unload 'results.gdx';
execute 'gdx2sqlite -i results.gdx -o results.db -fast'

There are some advantages to save just everything in the database:

  • All the names (tables, columns) are the same as in the GAMS model.
  • Everything is saved. If you need additional data, it is already there. No need to rerun the GAMS model.
  • These steps are quite fast.
  • A SQLite database is just a standalone file. You can email it to colleagues.

On the Python side it is quite easy to read data from an SQLite database:


The location and size of the circles are taken from the GAMS result set. Inspecting data in a tabular form is also very easy:


For exploratory work the ipython notebook interface is very convenient.

See also: http://yetanothermathprogrammingconsultant.blogspot.com/2014/09/gams-to-python.html