Hello,
I have set up small economic model where I feed it inputs via a .gdx file and seek to execute the model 4 times in total. This is because the model is the same for 4 regions, and the .gdx file I’ve generated through stata-python contains the required inputs for all 4 regions in one file. What I want to do is to be able to run the model 4 times, each time with a different subset of the data:
the setup is as follows:
1.) A .gdx file is generated containing data inputs for the 4 models, every variable is labelled by region (so data is tagged with A_, B_, C_, D_; there is a suffix “urban/rural” for each region).
2.) Each model is put into a separate folder (so 4 duplicates of the code, in folders A, B, C, D). The names of the files are unique just to keep me from getting confused, but the contents are identical.
3.) I then write a shell program that seeks to execute the 4 regions in sequence:
*settings
$onMultiR
option solveopt = clear;
*region A:
Set usedset /A/;
$include A/model_A.gms
*Note: I make a set called "usedset" together with GAMs embedded python code to select which parts of the data are valid for this region (example code below)
*Note: here "A/" is the name of the subfolder
*region B:
Set usedset /B/;
$include B/model_B.gms
*region C:
Set usedset /C/;
$include C/model_C.gms
*region D
Set usedset /D/;
$include D/model_D.gms
in each of the model files, I am controlling which parts of the .gdx file to use by combining the generated “usedset” with the following embedded python code (credit goes to GAMs forum admin):
$onEmbeddedCode Python:
region = list(gams.get('region'))
usedset = list(gams.get('usedset'))[0]
region = [ i for i in region if i.startswith(usedset)]
gams.set('region',region)
$offEmbeddedCode region
The .gdx reads in with all four regions and the original region set is: /A_urban, A_rural, B_urban, B_rural, C_urban, C_rural, D_urban, D_rural/;
4.) Here is where the issue arises, when I execute the shell code, it does run 4 times, but only for the last region. My suspicion is that the last line of changing the “usedset” was overwriting everything previous (Set usedset /D/;). I have tried moving the export to .txt code (gams put utility) into each of the separate model files (so model_A, model_B, model_C, model_D each have an export to text code), the issue persisted. Given the current structure of the model’s setup, is there a way to run sequentially? (I’ve tried $clear, but that doesn’t seem to do anything)
5.) Finally, I realize that a loop would be a better way to do this, but for some reason the “rank” program used in my model cannot be used in a loop (it defines some parameters), and I’m concerned of changing that file as other programs use it as well.
Any ideas or suggestions would be greatly appreciated! Thanks for your time!