GAMS Sum over a subset

I have the following sets, variables, and parameters in a GAMS model.

alias (h,t);

Positive variables are $$p_d(h)$$, $$p_c(h)$$ and $$e(h)$$. Parameters are $$\eta = 0.9$$ and $$e(0) = 0$$.

I want to write the following equation in GAMS: $$e(h) = e(0)-\sum_{t=1}^h p_d(t)+\eta\cdot\sum_{t=1}^hp_c(t).$$

• You could try something like this: eq(h).. e(h) =e= e("0") - sum(t, pd(t)) + n*sum(t, pc(t)); but, if the element in the e(0) is a element of the $h$, you should define it and then use a filter on the other sets into the constraint. Apr 25 '20 at 20:02
• Thank you, I tried that. It throws an error that number (primary) expected. Also, it doesn't define the relationship that, when h=1, t=1; when h=2, t=1,2 etc. I think some conditional operator is required for the indexing, but not sure how to implement that. e(0) is treated as a parameter. Apr 25 '20 at 23:04
• Would you try using dollar operator to define the conditions into the statement? For example, eq(h)$(ord(h)>1)... . Apr 26 '20 at 11:15 • I think you will need to define a whole set consists of the$0$element. Next, define a subset with your favourite elements. (E.g. set Total / 0, 1*3 /; set h(total) / 1*3 /;) Apr 26 '20 at 11:23 2 Answers Eq(h) .. e(h) =e= E0 - sum(t$(ord(t) <=Ord(h) ), pd(t)+n*sum(t, pc(t));

It's better to define E0 as a scalar in GAMS. you should also define alias(h,t);

In GAMS, you are not allowed to perform a sum in the same set as the one the equation is defined.

The answer by Optimization team is correct, although it has a small mistake in the formulation.

For this case:

EQ(h)..    e(h) =E= e("0") - sum(t$(ord(t) le ord(h)), pd(t)) + n*sum(t$(ord(t) le ord(h)), pc(t));

Or, if you put the sums together:

EQ(h)..    e(h) =E= e("0") + sum(t\$(ord(t) le ord(h)), n*pc(t) - pd(t));