how to read sets and tables dynamically in a "for" loop?

Hi everyone,

I’m pretty new in GAMS.
I want to read a set k and table T(i,j,k) dynamically in each iteration of for loop from different excel/text file.
The table T(i,j,k) changes in its size in each iteration/each excel file, according to change of k in each iteration.
T(i,j,k) is a very big matrix that I could barely create and save it in a separate excel file for each iteration. (I created them in MATLAB)
I just need to read them in GAMS to run my model in each iteration and save my results.
As I searched it’s impossible to declare set or table in a loop, so what should I do?
I’ll appreciate if anyone can help me in that.

Thank you

Hi Azam

It depends a little bit on how many files you are talking about. If there are only a few of them, you can easily use some if-statements and choosing i,j, and k such that it contains all the set elements that are used in the different files.
Could you show some example code?
Cheers
Renger

Hi Renger

301 files, so if-statements may not be suitable for me. k changes in each iteration too. Each T(i,j,k) has around 74,000 rows and k columns.
I coded just one specific iteration (new10_R.gms) that works perfectly, and I’m trying to find a way to define k and T(i,j,k) in each iteration and code the big problem(new11.gms) which is not complete yet.
I’ll appreciate if anyone can guide me on that.

Thank you
Azam
new11.gms (33.7 KB)
new10_R.gms (33.2 KB)

Hi Azam

Here an idea to do this: use $setglobal (see the documentation on using this feature) and run your model for each table separately using a batch file.
If you are running on Windows, you write a batch file (e.g. run.bat) that looks like this

call gams mymodel --setdata=001
call gams mymodel --setdata=002
...
call gams mymodel --setdata=301

You could write this one easily in a few seconds with Excel.

Your model file looks now like this

$setglobal setdata

* no loops, just a single instance of your model
* Read the specific table
table  T(i,j,k)
$ondelim
$include Tijk_%setdata%.csv
$offdelim
;
* solve your model and save your results
execute_unload "results.gdx" x.L x.M o.L o.M f.L f.M ZZ;
execute 'gdxxrw.exe results.gdx var=x.L rng=%setdata!';
...

This has the advantage above using a loop that if the loop gets stuck, you lose your results. Furthermore, you can have a few of these batch files and run them at the same time (be sure to write your results for each batch file to another excel sheet).
Another thing: I would save all the stuff in an gdx file with the name of the table (e.g. 301.gdx). After finishing all the solves you can merge these files (see how to do this in the documentation of gdx). You will then have all your results indexed additionally with the table number.

I hope this helps you setting up your problem.

Cheers
Renger