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
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
Hi Luca
You can set a local (or global) variable using $setlocal ($setglobal):
$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_difference_between_set_and_setglobal
Hope this helps
Cheers
Renger
You can even use the value of a scalar with a data statement to do this:
Scalar test1 / 10 /;
$eval TEST1 test1
set test / 1*%TEST1% /
This gives you set elements 110. 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_GamsCall.html?search=runtime#UG_GamsCall_TwoPass. So the following still, gives you set elements 110, not 1*20.
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_GamsCall.html#UG_GamsCall_DoubleDashParametersEtc_CompileTimeVars for details) compile time variable, so the check inside GAMS should use set, not setLocal:
$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
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
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:
set super / 1*10000 /;
scalar num;
num = uniformInt(1,card(super));
set dyn(super); dyn(super) = ord(super)<=num;
display dyn;
-Michael