Page 1 of 1

Flow equation

Posted: Fri Sep 20, 2019 8:02 pm
by Alexanre.ito
Hi,
I'm trying to write a code to calculate the flow of a snippet.
The section should receive the flow of the previous sections.
But my problem is the matrix "q(n,np)" depends on itself.
Apresentação1.jpg

Code: Select all

Set     n       nodes   / n1 * n48 /
         a(n,n)  arcos   / n1.(n2,n9), n2.(n1,n3), n3.(n2,n4), n4.(n3,n5,n10),
                           n5.(n4,n6,n11), n6.(n5,n7), n7.(n6,n8), n8.(n7,n17),
                           n9.(n1,n13), n10.(n4,n14), n11.(n5,n15), n12.(n8,n16),
                           n13.(n9,n17), n14.(n10,n20), n15.(n11,n21),
                           n16.(n12,n24), n17.(n13,n18,n25), n18.(n17,n19),
                           n19.(n18,n20), n20.(n14,n19,n21,n28),
                           n21.(n15,n20,n22,n29), n22.(n21,n23), n23.(n22,n24),
                           n24.(n16,n23,n33), n25.(n17,n26,n33), n26.(n25,n27),
                           n27.(n26,n28), n28.(n20,n27,n29,n34),
                           n29.(n21,n28,n30,n35), n30.(n29,n31), n31.(n30,n32),
                           n32.(n24,n31,n36), n33.(n25,n37), n34.(n28,n38),
                           n35.(n29,n39), n36.(n32,n40), n37.(n33,n41),
                           n38.(n34,n44), n39.(n35,n45), n40.(n36,n48),
                           n41.(n37,n42), n42.(n41,n43), n43.(n42,n44),
                           n44.(n38,n43,n45), n45.(n39,n44,n46),
                           n46.(n45,n47), n47.(n46,n48), n48.(n40,n47)/
         si(n)   saida   /n48/
         cont(n) contribution; cont(n)=yes; cont(si)=no; display cont;
         alias(n,np);


Scalar q /10/;

Variables ar(n,np)
          qn(n,np)
          xq;

binary variable ar(n,np);

Equation flow
         qalter;

qalter(n,np).. xq(n,np) =l= qn(n,np);

flow(n,np)$ar(n,np)..  qn(n,np) =e= sum(np,xq(n,np)) + q;
Gams Show the Error..

Code: Select all

 vazao(n,np)$ar(n,np)..  qn(n,np) =e= sum(np,xq(n,np)) + q;
                                            $125
                                                                  
 125  Set is under control already

**** 1 ERROR(S)   0 WARNING(S)
I tried to use the equation to maintain the linear equation

Code: Select all

qalter(n,np).. xq(n,np) =l= qn(n,np);
Please could someone give me some idea

Re: Flow equation

Posted: Sun Sep 22, 2019 9:25 am
by Renger
Hi
You can use -1 for the previous index and the $-sign to constrain the summation, like this

Code: Select all

alias(m, am);

Q(m,n) = sum(am$(ord(am) < ord(m)+1),  Q(am-1,n)X(am-1,n))+ QIN(am,n);
Note, that you have to use an alias, as you can't sum over m as it is already used for indicating the variable Q(m,n), so Gams complains over using m in two concflicting ways (error 125).

I hope this helps

Cheers
Renger

Re: Flow equation

Posted: Mon Sep 23, 2019 1:28 pm
by Alexanre.ito
Using this expretion

Code: Select all

Q(m,n) = sum(am$(ord(am) < ord(m)+1),  Q(am-1,n)X(am-1,n))
Q(m,n) and Q(am-1,n)

GAMS will use the same matrix of data, in this case, will use the same matriz of the flow data?

Re: Flow equation

Posted: Mon Sep 23, 2019 2:26 pm
by Renger
I think it should be:

Code: Select all

alias(np,anp);
flow(n,np)$ar(n,np)..  QN(n,np) =e= sum(anp$(ord(anp) < ord(np), XN(n,anp)) + q;
.
This will take all XN for which the first index is the same as in QN (i.e. 'n') and the second index anp goes from the first element of np to the one just before n.
Hope this clarifies it.
CHeers
Renger