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.
Sum with index as a function
Re: Sum with index as a function
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)
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
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;
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
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Re: Sum with index as a function
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.
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.
-
- User
- Posts: 29
- Joined: 5 years ago
Re: Sum with index as a function
Hello,
Maybe you can try this:
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;
Re: Sum with index as a function
Hello,
Thanks a lot, it works now!
Thanks a lot, it works now!
-
- User
- Posts: 29
- Joined: 5 years ago
Re: Sum with index as a function
You are welcome