Sum of Rows = Sum of Columns

Problems with syntax of GAMS
Post Reply
ayledith
User
User
Posts: 9
Joined: 4 years ago

Sum of Rows = Sum of Columns

Post by ayledith »

I have a variable xij, and I want to code essentially: ∑xi(j=n)=∑x(i=n)j for all values of n. I tried using the line eq6(i,j).. sum(i, Q(i,j)) =e= sum(j, Q(i,j)); which is not correct.
Another way of saying this is that I want to code Input = Output

What is the correct way to write this in GAMS?
User avatar
Renger
Posts: 639
Joined: 7 years ago

Re: Sum of Rows = Sum of Columns

Post by Renger »

Hi
The problem is that you use both i and j in two different ways: as an index for the equation and as summation index.
You need an alias:

Code: Select all

alias(i,ai), (j,aj);
eq6(i,j).. sum(ai, Q(ai,j)) =e= sum(aj, Q(i,aj));
This resolves the confusion on what to do with "i" and "j".
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
ayledith
User
User
Posts: 9
Joined: 4 years ago

Re: Sum of Rows = Sum of Columns

Post by ayledith »

Is there also a way to write Q(i,j) = Q(j,i), IE Flow from i to j equals flow from j to i?
User avatar
dirkse
Moderator
Moderator
Posts: 215
Joined: 7 years ago
Location: Fairfax, VA

Re: Sum of Rows = Sum of Columns

Post by dirkse »

Hi,

Often, if you have a square matrix Q(i,j), the sets i and j are the same, e.g. you have alias(i,j). In this case, to force Q to be symmetric, you could do:

Code: Select all

equation forceSym(i,j);
forceSym(i,j)$[ord(i) < ord(j)].. Q(i,j) =e= Q(j,i);
Note that if you have such a constraint, you could enforce the eq6 mentioned earlier in this thread on just the lower or upper triangle of the matrix:

Code: Select all

eq6(i,j)$[ord(i) <= ord(j)].. sum(ai, Q(ai,j)) =e= sum(aj, Q(i,aj));
-Steve
ayledith
User
User
Posts: 9
Joined: 4 years ago

Re: Sum of Rows = Sum of Columns

Post by ayledith »

I think I may be missing something. The line "forceSym(i,j)$[ord(i) < ord(j)].. Q(i,j) =e= Q(j,i);" results in the error: "Domain violation for set"
User avatar
dirkse
Moderator
Moderator
Posts: 215
Joined: 7 years ago
Location: Fairfax, VA

Re: Sum of Rows = Sum of Columns

Post by dirkse »

It would make it easier to help if you followed the posting rules:
app.php/rules
Post Reply