Seeking help for strange behavior of multiple solves of a model

Problems with modeling
Post Reply
henryhu.89
User
User
Posts: 15
Joined: 3 years ago

Seeking help for strange behavior of multiple solves of a model

Post by henryhu.89 »

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:

Code: Select all

*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):

Code: Select all

$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!
smann
User
User
Posts: 1
Joined: 3 years ago

Re: Seeking help for strange behavior of multiple solves of a model

Post by smann »

You are right, your repeated set definitions overwrite the previous ones, so you end up with only 'D' in usedset.

You could try to call gams with a double-dash parameter from the command line to define the set you would like to use.

GAMS Calls:

Code: Select all

gams model.gms --setToUse=A
gams model.gms --setToUse=B
gams model.gms --setToUse=C
gams model.gms --setToUse=D
Model Code:

Code: Select all

Set usedset /%setToUse%/;
$include %setToUse%/model_%setToUse%.gms
Best,
Stefan
henryhu.89
User
User
Posts: 15
Joined: 3 years ago

Re: Seeking help for strange behavior of multiple solves of a model

Post by henryhu.89 »

Thanks for the confirmation and your time! Yes, I have considered using a .bat file with double dashed variables, however, this is unfortunately a collaborative project and I have no control over the .bat files (so I need to adapt my model to whatever it gives me).

Outside of a .bat file solution with double-dashed parameters, is there a way to setup a shell-program and individually execute the four models one-by-one? (so compile + execute for model1, then move on to the next). Thanks again!

Edit: as a quick update, I managed to get it to work using the "$call" command instead! Maybe the solution was just that simple and my eyes too blind...
Post Reply