## Incremental summing in equation

Problems with syntax of GAMS
bjb997
User Posts: 2
Joined: 6 months ago

### Incremental summing in equation

I am very new to GAMS and I am hoping to get some help setting up an equation.

Essentially, i am trying to implement the following equation:

y(1) = x(1) <= 15
y(2) = x(1) + x(2) <=15
y(3) = x(1) + x(2) + x(3) <= 15
y(i) = x(1) + x(2) + x(3).... x(i) <= 15
etc.

In this instance, x is the variable and y is the equation I am using for my model. The following code doesn't work as it simply sums all the way until the ith value of x

Code: Select all

`` y(i)..	sum(i,x(i)) =l= 15 	``
i.e y(1) = x(1) + x(2) + x(3) .... x(i) <= 15

I have also tried using a loop but GAMS does not allow equations in loops.

What is the correct syntax to go about this problem? Thanks in advanced for your help.

Fred
Posts: 151
Joined: 2 years ago

### Re: Incremental summing in equation

Hi,

The following should do the trick:

Code: Select all

``````set i /i1*i5 /;
alias (i,ii);
variable x(i);
equation y(i);
y(i)..	sum(ii\$(ord(ii)<=ord(i)),x(ii)) =l= 15;
[...]
``````
The following links to the documentation explain the alias and ord operator:
https://www.gams.com/latest/docs/UG_Set ... mesForASet
https://www.gams.com/latest/docs/UG_Ord ... rdOperator

The equation as you wrote it should actually lead to a compilation error because you cannot sum over i in the equation body if i is already the controlling index of the equation.

I hope this helps!

Fred

bjb997
User Posts: 2
Joined: 6 months ago

### Re: Incremental summing in equation

Hi Fred,

That has done the trick!

Thanks very much for your assistance and prompt reply. Much appreciated.

Cheers,
Brendan