Syntax Problem Topic is solved

Problems with syntax of GAMS
Post Reply
ManT
User
User
Posts: 12
Joined: 5 years ago

Syntax Problem

Post by ManT »

Hello,

I have a table:

Table branch(l,n,np,*) branch characteristics
X rating exist branchNum
L1.n1.n2 0.0281 2.5 1 1
L2.n2.n3 0.0108 3.5 1 2
L3.n3.n4 0.0297 2.4 1 3
L4.n4.n5 0.0297 2.4 1 4
L5.n5.n1 0.0064 4 1 5
L6.n1.n4 0.0304 1.5 1 6;

I have a binary variable x(l,t)

I would like to make a summation for all lines not connected to N4. So in layman terms, it would be L1, L2, and L5

A naive approach of doing it:

Parameter branchNum(l);
branchNum(l) = 0;
branchNum(l) = sum((n,np),branch(l,n,np,'LineNumber'));

SUM(l,x(l,t)$(branchNum(l) ne 1) $ (branchNum(l) ne 2) $ (branchNum(l) ne 5) );

The works for a small case and to some extent, not programmatic.

How do I write a gams code for summing only lines that are not connected to Node 4 in the table?

May be: sum(l,x(l,t)$(sum((n,np)$sameas(n,'n4') ????????????????????????????????????? This is wrong. I need help

Much appreciated
User avatar
bussieck
Moderator
Moderator
Posts: 1038
Joined: 7 years ago

Re: Syntax Problem

Post by bussieck »

It's unclear what np is, but I assume this is an alias to n. Posting complete examples really helps. Here are two solutions. One is a direct on and the other uses more symbols but can easily serve to calculate similar sums for other nodes, not just n4:

Code: Select all

set l /L1*L6/, n /n1*n5 /, t /t1/; alias (n,np);
Table branch(l,n,np,*) branch characteristics
         X      rating exist  branchNum
L1.n1.n2 0.0281 2.5    1      1
L2.n2.n3 0.0108 3.5    1      2
L3.n3.n4 0.0297 2.4    1      3
L4.n4.n5 0.0297 2.4    1      4
L5.n5.n1 0.0064 4      1      5      
L6.n1.n4 0.0304 1.5    1      6
;

binary variable x(l,t);
x.l(l,t) = 1;
*x.l(l,t) = uniformInt(0,1);

scalar sumXLnoN4;
sumXLnoN4 = sum((l,t)$(sum(n$branch(l,n,'n4','exist'),1)=0 and sum(n$branch(l,'n4',n,'exist'),1)=0), x.l(l,t));
display x.l, sumXLnoN4;

set nl(n,l) 'lines l connected to node n';
nl(n,l) = sum(np, branch(l,n,np,'exist') or branch(l,np,n,'exist'));
parameter sumX(n) 'sum of x(l,t) of lines l not connected to node n';
sumX(n) = sum((l,t)$(not nl(n,l)), x.l(l,t));
sumXLnoN4 = sumX('n4');
display x.l, nl, sumX, sumXLnoN4;
-Michael
ManT
User
User
Posts: 12
Joined: 5 years ago

Re: Syntax Problem

Post by ManT »

Brilliant. Thank you. I am eternally grateful.
Post Reply