Parametrize a filename

Let me preface this question by noting that I’m pretty new to GAMS.

I have a GAMS file which makes multiple calls to load and unload data from GDX files and I’d like to have a single line somewhere in the GAMS code to change the filenames referenced.

In particular, I want to include a specified parameter in a filename.

So for example I specify a parameter ‘scenario’ as ‘low’ somehow and then in all subsequent references the filename reflects this.

Something like:

execute_unload “C:\File directory\outputs_%scenario%.gdx”

To get :

“C:\File directory\outputs_low.gdx”

Is there a way to do this?

Cheers

Josh


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Hi Josh



You could use the following at the beginning of your gams file:

$if not set scenario $setglobal scenario low





Now you can run your model either:

  •      Without setting the scenario. In this case it will take the scenario “low”. If you want to change this scenario, you change it to “high”.
    
  •      with a batch file in the command window with the following text in your batch file (for example run.bat)
    

o gams mymodel – scenario=low

o gams mymodel --scenario=high

  •      in a dos window either as:
    

o gams mymodel (takes the default value for the scenario, e.g. low)

o gams mymode –scenario=high (for another scenario)

  •       or in the ide by putting –scenario=high in the parameters field (left of the dos command button). Without setting the parameter, it will run the default scenario.
    

Hope this helps

Renger


\


Modelworks

Gewerbestrasse 15

3600 Thun – Switzerland

+41 79 818 53 73

Info@modelworks.ch

blog.modelworks.ch







From: gamsworld@googlegroups.com [mailto:gamsworld@googlegroups.com] On Behalf Of Josh Craig
Sent: Monday, November 10, 2014 04:47
To: gamsworld@googlegroups.com
Subject: Parametrize a filename





Let me preface this question by noting that I’m pretty new to GAMS.



I have a GAMS file which makes multiple calls to load and unload data from GDX files and I’d like to have a single line somewhere in the GAMS code to change the filenames referenced.



In particular, I want to include a specified parameter in a filename.



So for example I specify a parameter ‘scenario’ as ‘low’ somehow and then in all subsequent references the filename reflects this.



Something like:



execute_unload “C:\File directory\outputs_%scenario%.gdx”



To get :



“C:\File directory\outputs_low.gdx”



Is there a way to do this?



Cheers



Josh


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Josh,

you can use put commands for this, you can loop over the elements of a
set for the scenarios. This example below would actually write the
code, so you would have to run the resulting file. I suggest you to
use this after having ready all the previous elements of the model,
until its declaration. Then at the end you move the output into a new
file in each loop. With the details you have sent, an example could
be:

SET scenario /low, high, morescenarios/;
FILE josh /josh.gms/; PUT josh; josh.LW=0; josh.NW=0;
$onput
$onecho > exercise1.gms
$include CURRENT.gms
$include YOUR_MODEL_UNTIL_DECLARATION.gms
solve modelname using SOLVER maximizing OBJVAR;
execute_unload ‘output.gdx’ var1.L var2.L;
$offecho
$offput
LOOP(scenario,
PUT “$echo “,'”',“SET CURRENT(scenario) /”,scenario.TL,”/;“, '” >
CURRENT.gms’ /;
PUT “$call gams exercise1.gms " /;
PUT “$call mv output.gdx > output_”,scenario.TL,”.gdx" / /;
);

After running it, look for “josh.gms”

This initially seems complicated, but it is very useful, there is a
lot of documentation about using put commands.

I shall give credit to Uwe Schneider, who helped me to solve a similar
problem a few months ago (any mistake in the code above is only my
responsability)

Good luck,

Best,
Roger

\

Thanks Renger. $setglobal is just what I was looking for!

On Monday, November 10, 2014 6:56:35 PM UTC+11, Renger van Nieuwkoop wrote:

Hi Josh



You could use the following at the beginning of your gams file:

$if not set scenario $setglobal scenario low





Now you can run your model either:

  •      Without setting the scenario. In this case it will take the scenario “low”. If you want to change this scenario, you change it to “high”.
    
  •      with a batch file in the command window with the following text in your batch file (for example run.bat)
    

o gams mymodel – scenario=low

o gams mymodel --scenario=high

  •      in a dos window either as:
    

o gams mymodel (takes the default value for the scenario, e.g. low)

o gams mymode –scenario=high (for another scenario)

  •       or in the ide by putting –scenario=high in the parameters field (left of the dos command button). Without setting the parameter, it will run the default scenario.
    

Hope this helps

Renger


\


Modelworks

Gewerbestrasse 15

3600 Thun – Switzerland

+41 79 818 53 73

In…@modelworks.ch

blog.modelworks.ch







From: gams...@googlegroups.com [mailto:gams...@googlegroups.com] On Behalf Of Josh Craig
Sent: Monday, November 10, 2014 04:47
To: gams...@googlegroups.com
Subject: Parametrize a filename





Let me preface this question by noting that I’m pretty new to GAMS.



I have a GAMS file which makes multiple calls to load and unload data from GDX files and I’d like to have a single line somewhere in the GAMS code to change the filenames referenced.



In particular, I want to include a specified parameter in a filename.



So for example I specify a parameter ‘scenario’ as ‘low’ somehow and then in all subsequent references the filename reflects this.



Something like:



execute_unload “C:\File directory\outputs_%scenario%.gdx”



To get :



“C:\File directory\outputs_low.gdx”



Is there a way to do this?



Cheers



Josh


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
To post to this group, send email to gams...@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Thanks Roger. I like the idea of being able to loop through scenarios.

On Monday, November 10, 2014 8:54:57 PM UTC+11, Roger Cremades wrote:

Josh,

you can use put commands for this, you can loop over the elements of a
set for the scenarios. This example below would actually write the
code, so you would have to run the resulting file. I suggest you to
use this after having ready all the previous elements of the model,
until its declaration. Then at the end you move the output into a new
file in each loop. With the details you have sent, an example could
be:

SET scenario /low, high, morescenarios/;
FILE josh /josh.gms/; PUT josh; josh.LW=0; josh.NW=0;
$onput
$onecho > exercise1.gms
$include CURRENT.gms
$include YOUR_MODEL_UNTIL_DECLARATION.gms
solve modelname using SOLVER maximizing OBJVAR;
execute_unload ‘output.gdx’ var1.L var2.L;
$offecho
$offput
LOOP(scenario,
PUT “$echo “,'”',“SET CURRENT(scenario) /”,scenario.TL,”/;“, '” >
CURRENT.gms’ /;
PUT “$call gams exercise1.gms " /;
PUT “$call mv output.gdx > output_”,scenario.TL,”.gdx" / /;
);

After running it, look for “josh.gms”

This initially seems complicated, but it is very useful, there is a
lot of documentation about using put commands.

I shall give credit to Uwe Schneider, who helped me to solve a similar
problem a few months ago (any mistake in the code above is only my
responsability)

Good luck,

Best,
Roger


To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at http://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.