Search code examples
gams-math

Accessing the last element of a variable in GAMS


I have a set:

Set t /t1*t6/;

Let us consider there is a variable called var. I have a constraint that the last element of var is less than 20.

Variable var(t);

Equation const;

const..

var('t6') < 20;

I would like to replace 't6' in the last line by something like card(t), so that if the size of t changes then I do not have to change it manually.


Solution

  • You can use a dollar condition to limit the equation to the last period:

    const(t)$(ord(t) = card(t)).. var(t) < 20;
    

    Or you could define a singleton subset for your end condition like so:

    singleton set tEnd(t) /t6/;
    
    const.. var(tEnd) < 20;