Assigning value of a variable to another with programming flow control Topic is solved

Problems with syntax of GAMS
Post Reply
parag_patil
User
User
Posts: 17
Joined: 3 months ago
Location: Ahmedabad
Contact:

Assigning value of a variable to another with programming flow control

Post by parag_patil » 2 months ago

Hello,

I want to assign the existing values of variables to another variables using either of the following:
  • loop statement
  • for statement
  • if-else statement Or
  • while statement
I do not want to use conditional ($) statements, because the number of elements is large

Code: Select all

sets
tt /tt1*tt5/
t /t1*t10/
;

Variables

X(tt) , Y(time);
X.fx('tt1') = 1;
X.fx('tt2') = 2;
X.fx('tt3') = 3;
X.fx('tt4') = 4;
X.fx('tt5') = 5;


Y.fx('t1') = X.l('tt1');
Y.fx('t2') = X.l('tt1');
Y.fx('t3') = X.l('tt2');
Y.fx('t4') = X.l('tt2');
Y.fx('t5') = X.l('tt3');
Y.fx('t6') = X.l('tt3');
Y.fx('t7') = X.l('tt4');
Y.fx('t8') = X.l('tt4');
Y.fx('t9') = X.l('tt5');
Y.fx('t10') = X.l('tt5');

Thanks

User avatar
bussieck
Moderator
Moderator
Posts: 614
Joined: 4 years ago

Re: Assigning value of a variable to another with programming flow control

Post by bussieck » 2 months ago

Use parallel assignment statement and the power of sets where ever possible and avoid regular programming constructs. Here your program how it ought to be written:

Code: Select all

sets tt /tt1*tt5/, t /t1*t10/;
Variables X(tt) , Y(t);

X.fx(tt) = ord(tt);
set tmap(t,tt); tmap(t,tt) = ceil(ord(t)/2)=ord(tt);
Y.fx(t) = sum(tmap(t,tt), X.l(tt));
-Michael

parag_patil
User
User
Posts: 17
Joined: 3 months ago
Location: Ahmedabad
Contact:

Re: Assigning value of a variable to another with programming flow control

Post by parag_patil » 2 months ago

Thanks.

The answer was helpful.

Post Reply