Using a variable as the parameter's set

Problems with syntax of GAMS
Post Reply
StuckOnGams
User
User
Posts: 3
Joined: 3 years ago

Using a variable as the parameter's set

Post by StuckOnGams »

Hello guys,

I am trying to use the result from a looped equation as the name of a parameter's set. However, I only have the variable name and it cannot be used directly. Is there a way to overcome this and deliver the same result? Thanks in advance :).

SET
i /1*2/
t /2020*2030/;

PARAMETER
L(i)
/1 2800
2 2979/

D(i)
/1 0
2 0/ ;

TABLE
STOREDVALUES (i,t)
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
1 68.5 67.4 66.5 65.8 65.6 65.0 65.3 65.7 65.2 65.2 65.1
2 41.73 41.00 40.26 39.53 38.79 38.06 37.32 36.59 35.85 35.12. 34.38 ;

EQUATION
change_in_value ;
change_in_value .. changes(i) =e= 5000 - L(i);

LOOP (i,
if (changes(i) < 2030,
D(i) = STOREDVALUES (i, "changes(i)");
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Using a variable as the parameter's set

Post by bussieck »

If you have code, please use the "[ code ][ /code ]" tags. So you basically want to access the table STOREDVALUES(i,t) with a t that is a numeric value and comes from some model (does not really matter). The fundamental issue is that GAMS distinguishes between numbers and labels and that you cannot address your table with a number. You can easily map between numbers and labels. The label suffix .val (https://www.gams.com/latest/docs/UG_Set ... 2d__21_set) will try to interpret the label as a number and with that you can build your map. So in your example, you can do

Code: Select all

loop(i,
   if (changes.l(i) < 2030,
      D(i) = sum(t$(round(changes.l(i))=t.val), STOREDVALUES(i, t));
      ...
   )
   ...
)
-Michael
StuckOnGams
User
User
Posts: 3
Joined: 3 years ago

Re: Using a variable as the parameter's set

Post by StuckOnGams »

Will do flair it next time. Thanks a lot for the help, it's a great workaround!
Post Reply