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

set \quad h/1*3/;
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).$$

  • $\begingroup$ 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. $\endgroup$
    – A.Omidi
    Apr 25, 2020 at 20:02
  • $\begingroup$ 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. $\endgroup$
    – S_Scouse
    Apr 25, 2020 at 23:04
  • $\begingroup$ Would you try using dollar operator to define the conditions into the statement? For example, eq(h)$(ord(h)>1)... . $\endgroup$
    – A.Omidi
    Apr 26, 2020 at 11:15
  • 1
    $\begingroup$ 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 /;) $\endgroup$
    – A.Omidi
    Apr 26, 2020 at 11:23

2 Answers 2


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)); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.