Gams code snippet for checking consistency of a matrix

In an earlier post, I showed how to include code snippets in your program. One of the most important principles of programming is the DRY principle: Don’ Repeat Yourself. Code snippets can be used to do stuff you have to code over and over again.
One such a task for me is checking if the row and column totals in a matrix are equal (for example in the input output table you use, you want to check if supply is equal to demand).
 
Here is the code snippet:
 
 
Now you can batinclude (meaning that you can pass additional information to this included file with arguments %1, %2 etc.).
 
Here a simple example of how to use this code snippet:
 
 
Some remarks:
1. The code snippet in this file is saved in the file sn_checkcons.gms in the same directory as the other files.
You might want to have a directory that is accessible for all your projects as I do.
 
I just add the following code at the beginning of the project file:
 
 
The gams snippets are saved in the directory D:\Snippets\Gams and I can include the snippets with
 
 
where the three dots are the arguments.
 
2. The arguments are the set used and the name of the matrix you want to check. In the code snippet everywhere gams finds %1 this is replaced with the set name (in the example “i”) and %2 with the matrix name (in the example “matrix”).
 
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*