Tuesday, August 4, 2009

GAMS: writing to a GDX file from different spots

For a large GAMS model I need to write to a GDX file from different places in the model. At compile time this can be done in some cases, e.g.:

$gdxout x.gdx
$unload a
… do stuff …
$unload b

However compile-time gdx writing is only useful in exceptional cases. At execution time we are not allowed to append to a gdx file. The code:

execute_unload “x.gdx”,a;
… do stuff …
execute_unload “x.gdx”,b;

will cause x.gdx only to contain symbol b.

Unfortunately there is no good alternative for me:

  • the model is too big and complex to save all data in extra parameters (so we can do a execute_unload once).
  • the tool gdxmerge does not really merge unrelated gdx files very well: all symbols get an extra index which cause large problems in subsequent processing.
  • writing a gams model that reads all these gdx files and then writes one gdx file is a lot of work, and would require a lot of memory if we call this from the main gams model.