Page 1 of 1

nested GAMS calls with save and restart

Posted: Mon Oct 23, 2017 1:48 pm
by LaurentFranckx
Dear all

I am trying to implement nested GAMS calls.

To be more concrete, I have a top-level GAMS file, say, root.GMS, which calls a PowerShell script, say myscript.ps1, with the following code:

Code: Select all

execute "powershell -File C:\gams\MyModel\myscript.ps1" 
This PowerShell script loops through a series of parameters to call a set of other GAMS scripts:

Code: Select all

$Years = (2013..2030)
foreach ($CurrentYr in $Years ) { 
        gams branch.GMS --year=$CurrentYr 
}
The problem is that branch.GMS does not recognize the parameters that are used in root.GMS.

One obvious solution would be to split root.GMS in two, and to use the save and restart facilities. The first part would include all the code up to the call to myscript.ps1 (call this root1.GMS), and the second part would include all the code from myscript.ps1. (call this root2.GMS).

The following PowerShell code should work then (assuming no parameters are defined in branch.GMS that are needed in root2.GMS):

Code: Select all

gams root1.GMS  s=root1
$Years = (2013..2030)
foreach ($CurrentYr in $Years ) { 
        gams branch.GMS --year=$CurrentYr r=root1
}
gams root2.GMS  r=root1
However, this solution would quickly become unwieldy with several layers of nesting. Is there any other approach that would allow me to pass on the parameters defined in root1.GMS to branch.GMS without abandoning the nesting structure?

Re: nested GAMS calls with save and restart

Posted: Sun Oct 29, 2017 11:11 am
by Renger
Hi

Perhaps you could dump all the information you need in a gdx file and load the necessary parameters in the next nest.
You only have to define these parameters in the next step.

Cheers
Renger

Re: nested GAMS calls with save and restart

Posted: Mon Oct 30, 2017 8:57 am
by LaurentFranckx
@Renger Thanks for the tip. Technically, that would be solution, but it is also error prone: if you add new parameters or variables, you could easily lose track of them. Now that I think of it, this is precisely a key advantage of the 'save' and 'restart' commands.
I have worked around the problem by implementing the solution I had discussed in my first post: I now call all GAMS scripts from a single PowerScript script. It's not very elegant, but it does work.