loop over two sets

Problems with syntax of GAMS
yanzhiping
User
User
Posts: 85
Joined: 8 months ago

loop over two sets

Postby yanzhiping » 8 months ago

Does anyone know how to loop over two sets in GAMS?
I have a code as following, but it is not working.

set
A /a,b,c/;
A1 /a1,b1/;
table
B(A, A1)
a.a1 1
a.b1 1
b.a1 2
b.b1 3
c.a1 2
c.b1 3;
parameter
C(A,A1)
;
loop((A,A1),
C(A,A1) =ceil(B(A, A1)/2);
);

Display C;

yanzhiping
User
User
Posts: 85
Joined: 8 months ago

Re: loop over two sets

Postby yanzhiping » 8 months ago

I made a mistake. I think it works.

cladelpino
User
User
Posts: 55
Joined: 9 months ago

Re: loop over two sets

Postby cladelpino » 8 months ago

Loop part is correct, declarations are not. I took the code to correct syntax (other ways are also possible):

sets
A /a,b,c/
A1 /a1,b1/;
parameter
B(A, A1) /
a.a1 1
a.b1 1
b.a1 2
b.b1 3
c.a1 2
c.b1 3/;
parameter
C(A,A1)
;
loop((A,A1),
C(A,A1) =ceil(B(A, A1)/2);
);

Display C;

cladelpino
User
User
Posts: 55
Joined: 9 months ago

Re: loop over two sets

Postby cladelpino » 8 months ago

Besides: the loop is actually unnecesary.

C(A,A1) =ceil(B(A, A1)/2);

will just do the trick

yanzhiping
User
User
Posts: 85
Joined: 8 months ago

Re: loop over two sets

Postby yanzhiping » 8 months ago

If we have an example as below, how can we simply the loop below?

Code: Select all

set Year
     dset;
parameter A(Year);

Loop((Year),
         If (ord(Year)<card(Year),
                 A(Year) = (card(Year)-1)*card(dset) - (ord(Year)-1)*card(dset);
         Else
                 A(Year) = 0;
         );
);
It seems the following doesn't work.

Code: Select all

A(Year$ord(Year)<card(Year)) = (card(Year)-1)*card(dset) - (ord(Year)-1)*card(dset);

cladelpino
User
User
Posts: 55
Joined: 9 months ago

Re: loop over two sets

Postby cladelpino » 8 months ago

this is the correct way:

A(Year)$(ord(Year)<card(Year)) = (card(Year)-1)*card(dset) - (ord(Year)-1)*card(dset);

yanzhiping
User
User
Posts: 85
Joined: 8 months ago

Re: loop over two sets

Postby yanzhiping » 8 months ago

thanks, this works for me.


Who is online

Users browsing this forum: No registered users and 4 guests