1

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.

Community
  • 1
  • 1
Rajat
  • 249
  • 1
  • 11

2 Answers2

2

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;
Martin Bonde
  • 536
  • 3
  • 11
  • 1
    Just ran into this one by chance. The proposed solution is actually wrong. $(card(t)) is true for every t. This was probably meant to be $(ord(t)=card(t)). – Lutz Sep 14 '22 at 16:27
  • Oops, good catch! I have edited the answer accordingly. Goes to show that I as a dayly GAMS user can get by without ever touching ord or card (mostly relying on .val) – Martin Bonde Sep 15 '22 at 17:28
  • Note that the syntax in the second example is actually wrong now... :) You have the data statement /t6/ as part of the assignment now. – Lutz Sep 16 '22 at 06:45
2

You could also define an upper bound with the help of the "last" attribute of the set t:

Set t /t1*t6/;

Variable var(t);

var.up(t)$(t.last) = 20;

Best, Lutz

Lutz
  • 2,197
  • 1
  • 10
  • 12