Thursday, August 20, 2009

GAMS: debugging tools

Earlier this week I was sitting next to an experienced GAMS modeler working together on a model, but I was nevertheless able to show him some tricks:
  • Use $stop to stop GAMS at a certain point and use GDX=X command line parameter to inspect the symbols as they are when stopped.
  • The IDE gdx viewer is more powerful than many users know.
  • If inside a loop, use
    abort$1 "stopped for debugging";
  • If inside a loop and you want to stop at a certain iteration use something like:
    abort$(sameas(i,"iter10")) "stopped for debugging";
  • Always simplify equations as much as possible by create intermediate sets and parameters. Equations are much more difficult to debug than sets or parameters.
  • Loops are difficult to debug, not in the least because of an old issue in GAMS:
    loop(i,
    display i;
    );

    will display something else than expected: it will not show the current i but rather the complete set i. In addition when we use the abort$1 trick we don't see at what point we stopped (I would like to see a loop-stack). Sometimes we need to introduce another singleton set -- a set with one element -- that keeps track of the current state of the loop set in order to create a simulated loop-stack.
Note: a bug in GAMS may introduce a symbol SameAs in the gdx file. This is largely a harmless bug.