Conditional equations

Problems with modeling
Post Reply
Nikou
User
User
Posts: 33
Joined: 4 years ago

Conditional equations

Post by Nikou »

Hello

I would like to conditionally write constraints. In order to explain what I mean, consider the following example

Let i, s be sets.

s = {a,b}
i = {1,2}

The statement eq(i,s).. generates the equations eq(1,a), eq(1,b), eq(2,a), eq(2,b)

I would like to tell GAMS to only generate equations by only considering 1 element of s (I don't know which one).

The result would be either

eq(1,a), eq(2,a) or eq(1,b), eq(2,b)

So, I would like to implement the following :

Code: Select all

SCALAR written ;
written = 0 ;
loop(i,
  written = 0 ;   
  loop(s,
    if (written = 0,
      write equation eq(i,s)..
      written = 1 ;
    )
  )
)
How can I do this?
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Conditional equations

Post by abhosekar »

These things are much easier in GAMS than you would expect. You need a set and $ condition. You can read about set manipulation here: https://www.gams.com/latest/docs/UG_SetDefinition.html
and $ condition here: https://www.gams.com/latest/docs/UG_Con ... rCondition

Code: Select all

set flag(s);
flag(s) = no;

* you can change it to b if you need
flag("a") = yes;

eq(i, s) $flag(s).. writeequation as usual
- Atharv
Nikou
User
User
Posts: 33
Joined: 4 years ago

Re: Conditional equations

Post by Nikou »

Thank you very much for your reply.

My question is somehow more complex.

I am generating equations eq(i,j,s), where i,j,s are sets.

It happens that equations eq(i,j,"s1") and eq(i,j,"s2") are identical.

This is because the values of s are not included in the output of the gams equation statement.

How can I make sure that these equations are generated only once?
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Conditional equations

Post by abhosekar »

Do you mean s is not included in the equation itself? Then have you tried defining equation over (i, j) instead of (i, j, s) ?

Please clarify your question if this doesn’t answer your question.

- Atharv
Post Reply