Question in regards to using $if for $includes Topic is solved

Problems with syntax of GAMS
Post Reply
henryhu.89
User
User
Posts: 15
Joined: 3 years ago

Question in regards to using $if for $includes

Post by henryhu.89 »

Hello,

I'm a new GAMs user and was trying to setup a simple system using excel to control some $include codes in my GAMs program. Here's what I did:

1.) In excel setup a column of names in an excel document I call "dashboard.xlsx" (I also setup an index sheet):
CONTROLS name
filename1 1
filename2 1
filename3 1
filename4 0
filename5 0
filename6 0

The filenames1-6 are names of GAMs code files I would like to run $include on. But I want control over which $includes run via this excel sheet.

2.) Next in GAMs I read in the above as a parameter:

set func functions /filename1, filename2, filename3, filename4, filename5, filename6/;
parameter cfunctions(func) Read in items to be used to control model flow;
$call "gdxxrw input=dashboard.xlsx output=dashboard.gdx index=Index!a2"
$gdxin dashboard.gdx
$load cfunctions

3.) using "display" to check, I find that the set "cfunctions" only reads in "filename1, filename2, filename3" as those have a "1" value, but filenames4-6 are omitted. I believe this is what I want.

4.) Finally I was attempting to use $if statements to run $include:

$if cfunctions("filename1")=1
$include filename1.gms
$elseif cfunctions("filename2")=1
$include filename2.gms
$elseif cfunctions("filename3")=1
$include filename3.gms
$elseif cfunctions("filename4")=1
$include filename4.gms
$elseif cfunctions("filename5")=1
$include filename5.gms
$elseif cfunctions("filename6")=1
$include filename6.gms

(This is just one variant of the $if statements I have tried)

However, I get error $282, or "gams could not open the $include file". My question is, is it possible to use excel to control $includes? If so what am I doing incorrectly? I'd appreciate any tips/tricks, thank you!
Attachments
image.png
image.png (4.19 KiB) Viewed 3023 times
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Question in regards to using $if for $includes

Post by bussieck »

You have to understand the difference between GAMS compile and execution time in order to implement such logic: https://www.gams.com/34/docs/UG_GamsCal ... ll_TwoPass. The trouble is that you don't have convenient access to values of indexed symbols (like your cfunctions) at compile time. Here is a solution: read 6 scalars from the spreadsheet (in the code I used explicit ranges in the gdxxrw command line but you can obviously put this info in the index sheet as well):

Code: Select all

$call "gdxxrw input=dashboard.xlsx output=dashboard.gdx par=f1 rng=Sheet1!b2 rdim=0 cdim=0 par=f2 rng=Sheet1!b3 rdim=0 cdim=0 ..."
$gdxin dashboard.gdx
scalar f1,f2,f3,f4,f5,f6;
$load f1 f2 f3 f4 f5 f6
$ifE f1=1 $include filename1.gms
$ifE f2=1 $include filename2.gms
$ifE f3=1 $include filename3.gms
...
You can also try to chop the indexed cfunction apart in an embedded Python code compile time section and fill the scalars from that:

Code: Select all

$call "gdxxrw input=dashboard.xlsx output=dashboard.gdx  index=Index!a2"
$gdxin dashboard.gdx
$load cfunctions

scalar f1,f2,f3,f4,f5,f6;
$onEmbeddedCode Python:
c = dict(gams.get('cfunctions'))
gams.set('f1', [c.get('filename1',0)])
gams.set('f2', [c.get('filename2',0)])
...
$offEmbeddedCode f1 f2 f3 f4 f5 f6
$ifE f1=1 $include filename1.gms
$ifE f2=1 $include filename2.gms
$ifE f3=1 $include filename3.gms
...
-Michael
henryhu.89
User
User
Posts: 15
Joined: 3 years ago

Re: Question in regards to using $if for $includes

Post by henryhu.89 »

Hello Michael,

Thanks so much for the response! It's interesting that "$" controls are executed in the compile phase. This might explain a few other things I have been working on.

I tried both your methods and they work perfectly, I appreciate the help, thanks!

Best,
Henry Hu
Post Reply