Page 1 of 1

Define set with integer scalar

Posted: Tue May 29, 2018 7:01 pm
by Luca
Dear all,

I need to declare in gams a set like the following:
/1*set_dimension/
where set_dimension is a given in integer scalar. How can I do?

Thanks in advance. Best regards,
Luca

Re: Define set with integer scalar

Posted: Wed May 30, 2018 7:51 am
by Renger
Hi Luca

You can set a local (or global) variable using $setlocal ($setglobal):

Code: Select all

$if not setlocal setend $setlocal setend 10

set test /1* %setend%/;

display test;

* If you want to use the end of the set as a scalar you can also use %setend%
parameter test1;

test1 = %setend%;
display test1;
You can now run your model from the command line or in the gamside as follows

gams mymodel --setend 20

and it will use 20 instead of 10 (therefore, I use $if not setlocal ..., so I can run the model without specifiying setend using the option --setend n).

More on the difference between local and global variables can be found here: https://support.gams.com/gams:the_diffe ... _setglobal

Hope this helps
Cheers
Renger

Re: Define set with integer scalar

Posted: Wed May 30, 2018 9:13 am
by bussieck
You can even use the value of a scalar with a data statement to do this:

Code: Select all

Scalar test1 / 10 /;
$eval TEST1 test1
set test / 1*%TEST1% /
This gives you set elements 1*10. You cannot use the value of test1 after it has been assigned during execution (before the set declaration happens before the execution, see https://www.gams.com/latest/docs/UG_Gam ... ll_TwoPass. So the following still, gives you set elements 1*10, not 1*20.

Code: Select all

Scalar test1 / 10 /;
test1 = test1 * 2;
$eval TEST1 test1
set test / 1*%TEST1% /
Renger's method allows you to change the test1 from the command line. But the --setend=n creates a scoped (neither local nor global, see https://www.gams.com/latest/docs/UG_Gam ... leTimeVars for details) compile time variable, so the check inside GAMS should use set, not setLocal:

Code: Select all

$if not set setend $set setend 10

set test /1* %setend%/;

display test;

* If you want to use the end of the set as a scalar you can also use %setend%
parameter test1;

test1 = %setend%;
display test1;
-Michael

Re: Define set with integer scalar

Posted: Wed May 30, 2018 9:18 am
by Luca
Thanks very much. But I have another problem. I would like to assign the dimension of the set throut a computation depending from another variable. Something like this:

$if not setlocal output $setlocal local_var 10

scalar local_val_2 ;
local_val_2 = %local_var% * 10 ;

j a set /1*%local_var_2%/

Thanks again,
Luca

Re: Define set with integer scalar

Posted: Wed May 30, 2018 10:10 am
by bussieck
You can't. If you read what Renger and I wrote you will understand the reasons. What users often do in such a situation is to a) estimate the size of the set and build a super set and then b) use a dynamic set with the actual size:

Code: Select all

set super / 1*10000 /;
scalar num;
num = uniformInt(1,card(super));
set dyn(super); dyn(super) = ord(super)<=num;
display dyn;
-Michael