Wednesday, October 2, 2024

Prevent Loops in GAMS

 




This book [1] on DEA models has an accompanying website with all the GAMS models [2]. 

Of course, I'll be doing some nitpicking on the GAMS code. 

In some of the GAMS code, unneeded loops are used. From [3]:

 


The inner loop is not needed. We can write this as:



Many beginning GAMS modelers use too many explicit loops. Most likely because they are used to writing for-loops in traditional programming languages. GAMS does implicit loops, so there are very few occasions where loops are needed. The outer loop is such an example. For me, seeing models with unnecessary loops just hurts my eyes. 

There are two main arguments against loops:
  1. They are slow
  2. They make the code less compact
This concept is the same for programming languages like R and Python where vectorization is key to performance. Although the DEA model is correct and performance is not an issue on these small data sets, we really should not clutter our code with superfluous loops. Also, in this case, the author is not very consistent: the assignment to slacks is nicely vectorized. 

Some modeling exercises (not totally trivial, good for learning some GAMS):

  1. Rewrite the model without using any loops.
  2. Improve the reporting by sorting the results on efficiency. 

If you don't know how to approach these tasks, see [4].


References


  1. Ali Emrouznejad, Konstantinos Petridis, Vincent Charles, Data Envelopment Analysis with GAMS, A Handbook on Productivity Analysis and Performance Measurement, Springer, 2023
  2. https://dataenvelopment.com/gams/
  3. https://dataenvelopment.com/gams/chapter-2-fig-2-04/
  4. Solving DEA models with GAMS, https://yetanothermathprogrammingconsultant.blogspot.com/2024/09/solving-dea-models-with-gams.html

No comments:

Post a Comment