Gams Tips and Tricks
Gams Tips and Tricks
Gams Tips and Tricks
Below, a (random) list of useful tips and tricks for GAMS users is given. This list is oriented
at graduate students who start using GAMS but already know the general syntax. The list is
constructed largely as the result of concrete questions asked by GAMS users. Experienced
users will find many well-known modelling features, but may find some items they didnt
know yet.
In principle, almost everything can be found in the GAMS User Guide, which (among other
interesting documentation) can be found on http://www.gams.com/docs/document.htm. Some
new features are not in the official User guide yet; you can get these when you follow the very
useful advanced GAMS course by Bruce McCarl.
If you have any (good new) tips or tricks, or if you have found a bug / ambiguity in the
existing tips and tricks, send an email to [email protected].
1. Browse through the GAMS mailing list archive; it contains a lot of usefull tips and
tricks (including some of the hints below):
http://www.gams.de/3docs/gams_archives/external.html gives a summary of the main
topics and the full archive can be browsed at
http://www.listserv.gmd.de/archives/gams-l.html
2. GAMS comes with a library of 160 models; take a look at these models when building
your own model (the library can be found on the hard disk in the MODLIB
subdirectory and on the GAMS homepage).
3. First solve a model using zero iterations to check whether the benchmark (i.e. the base
data you provide) is replicated by the model. This is to make sure that there are no
errors in the calibration of the model.
<modelname>.iterlim = 0;
<modelname>.iterlim = 2000;
Or in case you know (by Walras law) that the level of C2 has to equal the level of X2:
5. Always check whether your model has been normally completed and found an optimal
solution:
Both should equal 1 if the model is linear; solvestat should equal 2 for non-linear
models. You can use an abort statement if one or both is incorrect:
6. Provide starting values! If you have some clue about good values for your variables,
give that information to the GAMS model. It will avoid the often-found zero-
solution, that an optimum is found where all variables equal zero. Moreover, it will
help in avoiding infeasibilities.
You could compare specifying a model without starting values as asking a person what
"bank" means without telling them what language it is. Where should that person start
looking? "Bank" can mean many different things across different languagesTo
provide starting values, give the name of the variable, add ".L" and then specify the
indices of theat variable (remember that ".L" should come before the indices!):
VARIABLE X(J);
X.L(J) = 1;
X.L("1") = 2;
$ontext
version: xx
date: dd-mm-yy
characteristics:
problem1: none?!
$offtext
8. You can limit the output listing file by including the following statements:
$offsymxref
$offlisting
option limcol = 0;
option limrow = 0;
variable.lo = -inf;
variable.up = inf;
10. When you are debugging your model, try shortening the execution time by
using $exit to exclude the last part of your code: $exit will stop the GAMS
execution, command after this line (perhaps including elaborate sensititivty analysis)
will not be executed.
11. If you have infeasibilities, there are (at least) three things you can do to aleviate
your problems:
1. Remove all unneccesary upper and lower bounds and fixed variables from your
code;
2. Provide better starting values;
3. Change equalities into inequalities (it is always a good thing to use inequalities in
stead of equalities).
12. Learn to use vector notation instead of scalars; it makes larger models much
more campact:
Instead of:
VARIABLES Y1 Producer 1
Y2 Producer 2;
For two sectors, this does not make much difference. But imagine writing out 60
sectors or more. It is much easier to use the vector notation from the start, when your
model is still small, than to have to rewrite it when you enlarge the model.
Example:
put i.ts /;
i1 first element
i2 second element
i3 I3
14. To get a good insight of the impacts of a parameter value on the model results
(i.e. a sensitivity analysis), build a loop of solves for different parameter values; for
example:
SCALARS
COUNT
LOWEST /0.1/
HIGHEST /0.9/;
LOOP(RANGE,
COUNT = (ORD(RANGE)-1)/(CARD(RANGE)-1);
XVALUE(RANGE) = (1-COUNT)*LOWEST + COUNT*HIGHEST;
RESULTS(RANGE) = ;
);
DISPLAY RESULTS;
15. How to get information on your screen in run-time? If you run several
simulations within the same GAMS model (for instance, you do a loop of solves), you
can
display "%system.filesys%/";
LOOP(t,
);
If you use the GAMS-IDE, make sure that the DOS window is visible (select File /
Options / Execute and choose Normal for the DOS window).
16. If you want to get graphs of model results right after the model solve, take a
look at the GNUPLOT utility which is available on the Internet (you can access it
from the GAMS homepage); youll have to install this free utility separately.
17. Many users know the $include command to include pieces of GAMS code,
but if you want to pass a parameter to the file that is to be included, use the
$batinclude command.
18. If you have a model that costs a lot of time to calculate, then think about using
the save and restart feature. This is especially handy if you have solved your model
and are thinking about ways to organise the output. You caan run your model, save the
work files from this run and restart from these workfiles for output handling. For
example, if you have a GAMS-file (say, part1.gms) in which the data is read, the
variables and equations are specified and your model is solved, then save the workfiles
as, say, model1. Then, write the output handling code in a separate file (say,
part2.gms), using the restart option. If you want to change anything in part2.gms,
you do not have to solve the model again, you can just include the workfiles. This can
save a lot of time (especially for larger models) and you can play around with how you
want your output organised at little cost. Note that this save and restart feature can also
be handy in other circumstances, like saving after solving a base scenario and then
solving several scenarios using the restarted base. The code in the example above
looks like this:
19. In GAMS version 2.50 on newer, you can direct output to an Excel file via the
XLimport, XLexport and XLdump routines; see
http://nash.colorado.edu/tomruth/xllink/xllink.htm for more details. Alternatively, you
could use put statements to make output file 'manually' (see the GAMS user guide for
more details).
Note that the second equation can only be made conditional upon the parameter a, not
on variable v1.
The lower bound on the initial objective variable (in case of a minimisation
problem this is obviously an upper bound) should depend on the level of
variability you find in the "near-optimal solution space".
24. One user of the GAMS reader misspecified the capital equation in a neo-
classical growth model as follows.
Can you find the difference between the two? The impact is enormous. I was surprised
that the top specification did not give an error message.
The lesson to be learnt: always be careful with those brackets. I love them because
they force you to think very precisely about what you want to model.
25. When you model equations that contain lags or leads, e.g. EQ1(t).. A(t+1)
=E= A(t)+B(t);, and you don't restrict this equation, you will calculate A(t+1) also
for the last period t. Since this next period A is not within the model horizon, you are
not interested in it and hence the calculation is unnecesary. You can save computing
time if you restrict the function not to calculate the after-horizon period:
EQ1(t)$(ord(t) le card(t)-1).. A(t+1) =E= A(t)+B(t);
Use the constraint both on the definition and declaration of the equation!
This is especially relevant if the model is large and the equation is relatively difficult (e.g. a
lot of summing and multiplication).