Sum with index as a function

Problems with syntax of GAMS
Post Reply
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Sum with index as a function

Post by mjcecili »

I would like to do a sum like the following in gams:

Equation(i) ... Sum ( (j = i - a + 1)$(ord(j) GE 1)$(ord(j) LE i), x(j) ) = b(i) ;

My index j is in terms of i.

How may I do this? Thanks. :)
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Sum with index as a function

Post by Renger »

Hi

Here is a little example that you can use to implement your equation. I assume that a is a parameter. (you can't use a variable in a $-condition in an equation)

Code: Select all

set j /1*20/, i /1*20/;

parameter a /-2/, b(i), x(j);

x(j) = uniform(1,10);
b(i) =  Sum (j$(j.val = i.val - a + 1) , x(j));

display b, x;
Note that you can use i.val as long as the set consists of numbers. Otherwise, you have to use ord(i).
You can add additional constraints after the dollar sign (connect with AND or OR).

Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Sum with index as a function

Post by mjcecili »

Hi Renger,

I really appreciate your help. I advanced a lot with this, however I would like to add a condition for the index, and I try the following but it shows error:

set j /1*20/, i /1*20/;

parameter a /-2/, b(i), x(j);

x(j) = uniform(1,10);
b(i) = Sum ( j$(j.val = i.val - a + 1)$(ord(j) GE 2) , x(j));

display b, x;


I know you mentioned something about in your message, I have tried on different ways but it shows error. Thanks in advance.
dunyayigezerken
User
User
Posts: 29
Joined: 5 years ago

Re: Sum with index as a function

Post by dunyayigezerken »

Hello,

Maybe you can try this:

Code: Select all


set j /1*20/, i /1*20/;

parameter a /-2/, b(i), x(j);

x(j) = uniform(1,10);
b(i) = Sum ( j$(j.val = i.val - a + 1 and ord(j) GE 2) , x(j));

display b, x;

mjcecili
User
User
Posts: 12
Joined: 4 years ago

Re: Sum with index as a function

Post by mjcecili »

Hello,

Thanks a lot, it works now! :D
dunyayigezerken
User
User
Posts: 29
Joined: 5 years ago

Re: Sum with index as a function

Post by dunyayigezerken »

You are welcome :D
Post Reply