Page 1 of 1

string operations in GAMS

Posted: Wed Jul 19, 2017 1:02 pm
by LaurentFranckx
Dear all
I am new in GAMS, after having spent several years working with R and Python, and I still have the disagreeable habit of trying to implement R/Python approaches in GAMS ;) . My apologies for this.
One type of operation that I haven't found yet in GAMS are string manipulations, such as concatenating string or splitting strings with a known structure.
For example, suppose that I am running a model over a series of scenarios ("BAU", "HIGHTAX", "LOWTAX"), regions ("NORTH", "SOUTH", "EAST", "WEST") and years ("2030","2040","2050"). I would like the name of my output files to reflect the specific combination of scenario-region-year that I have just run, e.g. "LOWTAX_NORTH_2050". Is there I way I can automatize this task in GAMS?
I am aware of the fact that I can append content to an existing file with the PUT facility, but this would not be my preferred solution.

Re: string operations in GAMS

Posted: Thu Jul 20, 2017 2:17 pm
by gamsadmin
Hi Laurent

One way would be a batch file in a command window and the use of control variables.

You would have something like this in your "run.bat" (something you could easily generate with a python or R script)

call gams mymodel --scenario=BAU --regions=NORTH --years=2030
call gams mymodel --scenario=BAU --regions=NORTH --years=2040
...

In your code you can then use the control variables like this in for example gdxxrw commands

... %scenario%_%regions%_%years%.xlsx

This would then produce for the first call: BAU_NORTH_2030.xlsx

Cheers
Renger

Re: string operations in GAMS

Posted: Thu Jul 20, 2017 4:03 pm
by LaurentFranckx
Dear Renger
I'll try that one out, thank you.

Re: string operations in GAMS

Posted: Wed Aug 07, 2019 1:11 am
by joshdr83
Is there a way to do this from within the GAMS code itself?

For instance I would like to do something like this (does not work):

Code: Select all

sets
    t                       'time index'                /000001*105120/ ; 

scalar                    
    site_number             'site number'              /813715/ ;

parameter
    s(t)                    'solar capacity factor at timepoint t (5-min data)' 
/
$ondelim
$include /path_to_file/Site_%site_number%_SolarPV_Axis1_SolarPV_Lat_order.csv
$offdelim
/ ;

Where the $include statement was able to find the file: /path_to_file/Site_813715_SolarPV_Axis1_SolarPV_Lat_order.csv

The reason that I want to do this is that I am trying to convert this GAMS code into a MIRO app and I need to be able to access the scalar site_number from the MIRO app interface.

Thanks -Josh

Re: string operations in GAMS

Posted: Wed Aug 07, 2019 7:56 am
by Robin
Hi Josh,

The %site_number% notation is only used for double-dash parameters or compile time variables. These are set at compile time (i.e. before the solve statement). I can highly recommend the corresponding section in the documentation: https://www.gams.com/latest/docs/UG_Gam ... pleExample. Compile-time variables are referenced with the %...% notation. %site_number% will be replaced at compile time by its value.

So if you want to access variables in compile time ($include is a compile time command), you can't use a normal GAMS scalar for this as you did in your example, but the said compile time variable / double-dash parameter instead.

Example: Run the following example with the double-dash parameter --site_number=11. The $log statement then has the following output: /path_to_file/Site_11_SolarPV_Axis1_SolarPV_Lat_order.csv;.

Code: Select all

sets
    t                       'time index'                /000001*105120/ ; 

scalar                    
    site_number             'site number'              /813715/ ;

parameter
    s(t)                    'solar capacity factor at timepoint t (5-min data)'
    
$log /path_to_file/Site_%site_number%_SolarPV_Axis1_SolarPV_Lat_order.csv;
Double-dash parameters can also be set from within MIRO: https://gams.com/miro/model.html#command-line

Re: string operations in GAMS

Posted: Wed Aug 07, 2019 9:05 am
by Renger
Hi Josh

You could write it like this:

Code: Select all

$setlocal site_number 813715

sets
    t                       'time index'                /000001*105120/ ; 

parameter
    s(t)                    'solar capacity factor at timepoint t (5-min data)' 
/
$ondelim
$include /path_to_file/Site_%site_number%_SolarPV_Axis1_SolarPV_Lat_order.csv
$offdelim
/ ;
You can then run the gams file either like this or as Robin suggested with --site_number 813715.

Cheers
Renger

Re: string operations in GAMS

Posted: Wed Aug 07, 2019 10:06 pm
by joshdr83
Hello -- thanks for the replies.

I figured it out, if I do the following:

Code: Select all

$if not set site_number $set site_number 813715

sets
    t                           time index                                               /000001*105120/ 
    hour_time(t)                time index in hours                                      /000001*008760/ ;

parameter
    s(t)                    solar capacity factor at timepoint t (5-min data) 
/
$ondelim
$include /path_to_file/Site_%site_number%_SolarPV_Axis1_SolarPV_Lat_order.csv
$offdelim
/ ;
And set up a double-dash parameter named "site_number" in Miro, it will work. Miro just needed a default file to import to the Configuration page, and the "if not set" command let it have it.

Thanks for the help!