Equation Set Condition

Problems with syntax of GAMS
Post Reply
sfpaula
User
User
Posts: 1
Joined: 4 years ago

Equation Set Condition

Post by sfpaula »

Hi everyone,

I know it could be a "stupid question" but I could find anything about write this type of conditional. :?:

I will try to simplify my problem by the next example:

I have a matrix which I need to put a condition for an specific positions. The mathematical expression would be something like that:
X(i,j)=A(i,j) ∀𝑗,i≠1,i≠4;
X(i,j)=B(i,j) ∀𝑗,i=1,i=4;

My gms code would be :

Code: Select all

Sets
     i   modulo   /1*5/
     j      nodo    /1*5/;
table A(i,j)
    1  2   3   4   5
1   1  2   3   4   5
2   6  7   8   9   10  
3   11 12  13  14  15
4   16 17  18  19  20
5   21 22  23  24  25
;

table B(i,j)
        1  2   3   4   5
1       0  1   2   3   4
2       0  1   2   3   4
3       0  1   2   3   4
4       0  1   2   3   4
5       0  1   2   3   4
;

Variable
X(i,j)
s;

Equations
EQ1, EQ2, obj;

EQ1(i,j)$(j ne 1)and(j ne 4)..  X(i,j)=e=A(i,j);
EQ2(i,j)$(j = 1)and(j = 4)..  X(i,j)=e=B(i,j);

obj..s=e=sum((i,j),x(i,j));

Model MTR /all/ ;

Solve MTR using lp maximizing s;

Ps: when we have a equations system with the same number of equations and variables, Are there any other way to solve the problem? I mean, the LP maximizing is a optimization proces and it wouldn't be necessary in that system. In this example, I have put the variable "s" because I don't know other way to solve the system although my goal would be take the variable "X". I know I could do this easy example without doing equations( using IF and PARAMETERS), but I must resolve with equations in my real problem.


Thanks everybody :D
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Equation Set Condition

Post by dirkse »

Hello,

What you have will work in principle, although you should use ord() and an 'or' instead of 'and':

Code: Select all

EQ2(i,j)$(ord(j) = 1) or (ord(j) = 4)..  X(i,j)=e=B(i,j);
to get the complement of the tuples from EQ1. If it is really your intention to cover all the i,j tuples using one of the two equations then you might want to use a subset. For example:

Code: Select all

set ij(i,j);
ij(i,j) = [sameas(j,'1') or sameas(j,'4)] and not sameas(i,'2');
display ij;
EQ1(i,j)$ij(i,j)..  X(i,j)=e=A(i,j);
EQ2(i,j)$[not ij(i,j)]..  X(i,j)=e=B(i,j);
With the subset you can display the ij set and see exactly what is happening.

Also worth noting: if you are just fixing, you don't need to use an equation. You could just do:

Code: Select all

X.fx(i,j)$ij(i,j) = A(i,j);
X.fx(i,j)$[not ij(i,j)] = B(i,j);
If you have a square system of equations and no objective you could use the CNS or MCP model types.

https://www.gams.com/latest/docs/UG_Mod ... search=CNS
https://www.gams.com/latest/docs/UG_Mod ... search=MCP

-Steve
Post Reply