Looking for infeasibilities in Gams

A good CGE modelers always checks the calibration of a CGE model using a simple trick: If you set the iteration limit of the model to zero and you try to solve the model, GAMS should find a solution if you correctly initialized and wrote down your model equations with the use of the benchmark data. Often, the calibration is not done properly and this can be seen (using the iteration set to zero) by looking at the infeasibilities of the equations and the variables.

If you use MPSGE to write down your model, the infeasibilities are easily interpreted. If the activity variables show infeasibilities the corresponding zero-profit condition is not met, if the price variables show infeasibilities the corresponding market clearing condition is not met. Either the equation, the initialization or the data is wrong.

Looking for infeasibilities in the listing can be daunting. A simple model can have hundreds of variables and finding the infeasibilities can become time consuming. Therefore, I wrote a small Python script that uses some regular expression magic to find the infeasibilities and show them. Below is an example. It shows all variables with an infeasibility greater than 1E-5. The script runs very fast.

Feel free to use and/or improve the code (I just started learning Python). The script looks like this (assuming that the listing file is called “BenchF.lst”):

Why I don’t like Matlab

Working with a group of researchers can be very interesting but also very frustrating if you have to use their software tools. In my case, I have to use Matlab. If you are used to R, Python, and Gams this is more than a nightmare. This post is after a frustrating day with Matlab.

Most of my researcher colleagues use in their Matlab code matrices, so no row names or column headings. What you see are just numbers. If you want to know what the number represents, you need to look at vectors that contain the sets/row names/headers. Some of my colleagues have mercy with me and use (like I do) tables. However, that is not the end of the frustration. If you want to define a table, you write something like

Now, if you believe that adding column names would need something like

you are wrong. You should use ‘VariableNames’.

Other stuff that is frustrating:

  • Reading from an excel sheet using xlsread: if you have some empty cells at the beginning of the last row, Matlab just leaves them out. In order to have this line read properly, you must use the following three lines of code (look it up in the documentation and there is no clear indication that you only can read numbers directly without NaN directly:
  • Export a table to excel: the column headings from the table are used, but the row names not. The row names have to be exported separately.

The good thing: you really learn to love R, Python, and Gams more than anything else. For Matlab users: never look over the shoulder of someone using other software 🙂