One of the important rules of good programming is the DRY principle: Don’t Repeat Yourself. After repeating myself too often, I decided to start working with code snippets and macros for often used code. Here is an example of code I use very often. Suppose you want to aggregate your data from your input-output table to more aggregated version, for example, from 87 sectors to only 5 sectors, so you can test your model more easily). In GAMS you do this with a mapping, where you map the 87 sectors to the 5 sectors. This mapping can then be used for aggregating your IOT easily.
What often happens is that you forget to map one element, or you map one element twice. Here is a simple example:
We have two sets: the first set “dis” is the disaggregated and the second set “agg” is the aggregated set. You want to map the first set to the second:
The elements “a” and “b” for set “dis” are mapped to “1” from set “agg” and “c” to “2”. Assume that in you make a mistake and forget to map “c” and you map “b” twice:
In “real world” work this can happen very easily. Tom Rutherford wrote some nice code to check for errors like these. As I always use this error checking for mappings and I don’t want to repeat myself, I wrote the following code snippet saved in the file sn_checkmapping.gms. Try to understand what is going on here (I won’t try to explain it) or just use if for your work.
Checking the mapping from my example is now easy (I don’t have to adjust any code):
$batinclude sn_checkmapping dis agg mapping
The two sets and the mapping are used as arguments (%1,%2 and %3) in the code snippet. The output in your listing file will look like this:
We forgot to map c and mapped b twice.
You can save code snippets and batinclude files like that in a common directory and use it in all your projects.