Define set with integer scalar

Problems with syntax of GAMS
Post Reply
Luca
User
User
Posts: 23
Joined: 6 years ago

Define set with integer scalar

Post 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
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Define set with integer scalar

Post 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
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Define set with integer scalar

Post 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
Luca
User
User
Posts: 23
Joined: 6 years ago

Re: Define set with integer scalar

Post 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
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Define set with integer scalar

Post 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
Post Reply