How to: create a sequenced (asterisked) set when the final value isnt known until runtime

Problems with syntax of GAMS
Post Reply
AndrewC
User
User
Posts: 17
Joined: 5 years ago

How to: create a sequenced (asterisked) set when the final value isnt known until runtime

Post by AndrewC »

Hello

We would like to use the * set definition, for example
set abc / t1*t100/;

Unfortunately we wont know that the final value is to be 100 until during the execution, it could be 100, it could be 500. We have saved the value we want it to be into a scalar during execution, for example:

Is there a way to do this? Basically we want something along the lines of (psuedo code):
:
run some gams code
:
calculate myScalar
set abc / t1*t<myScalar?>/;


One idea I had was to use an include file during execution, but couldn't get it to work - the file doesn't seem to be produced. Something like:

scalar iTemp;
iTemp = sum(a, myParameter(a));
put_utility 'shell' / 'echo t1*t', iTemp, ' >expandedTrdBlk.inc';
set trdBlk /
$include "expandedTrdBlk.inc";
/;


All help appreciated

Cheers
AndyC
AndrewC
User
User
Posts: 17
Joined: 5 years ago

Re: How to: create a sequenced (asterisked) set when the final value isnt known until runtime

Post by AndrewC »

SOLVED

My idea of creating an inc file during runtime does work, but my syntax was slightly incorrect which meant the file wasn't created - I forgot to explicitly specify the number of decimals for the scalar.

Below works for me.

scalar iTemp;
iTemp = sum(a, myParameter(a));
put_utility 'shell' / 'echo t1*t', iTemp:0:0, ' >expandedTrdBlk.inc';
set mySet /
$include "expandedTrdBlk.inc";
/;

Cheers
Fred
Posts: 372
Joined: 7 years ago

Re: How to: create a sequenced (asterisked) set when the final value isnt known until runtime

Post by Fred »

Andy,

I doubt that this works as you expect.

Sets are declared at compilation time. The_put_utility statement is executed at execution time. Hence, the file expandedTrdBlk.inc is also created at execution time and does not exist at compile time when you do the $include "expandedTrdBlk.inc";
Maybe the file is sitting in the right location from a previous run but if you delete it, you should run into a compilation error that says "282 Unable to open include file".

You can read about compilation and execution phase here: https://www.gams.com/latest/docs/UG_Gam ... ll_TwoPass

In general, static sets are declared and defined at compile time. Hence, considering the result of an execution time calculation isn't possible straightforward.
One work around could be to use a dynamic set instead of a static set. That would require to have a super set (t in the example below) that is definitely large enough. Then you can compute dynamic subset myset(t) with cardinality iTemp at execution time.

Code: Select all

set t        'superset'       / t1*t1000 /
    myset(t) 'dynamic subset'
    a        'dummy set'      / a1*a5 /;
parameter
    myParameter(a) 'dummy parameter with random values';
myParameter(a) = uniformInt(10,20);

scalar iTemp;
iTemp = sum(a, myParameter(a));
myset(t) = ord(t) <= iTemp;
display myParameter, iTemp, myset;
I hope this helps!

Best,
Fred
Post Reply