## Solving equations iteratively (faster) in GAMS

Problems with modeling
jonathanl91
User
Posts: 5
Joined: 4 months ago

### Solving equations iteratively (faster) in GAMS

I am doing a Gravity model in GAMS to estimate the trade between countries. That is, I want to do the following equation:

Where A_i^m and B_j^m are unknown. They are calculated in the following way:

That is, they should be solved iteratively. I am doing that in GAMS using loops.

Consider the following data:

Code: Select all

``````   set I "Iteration"       / I01 * I100 /
C "Commodity"       / C01 /
A "Export country"  / USA,Japan,Germany /
D "Import country"  / USA,Japan,Germany /
T "Time"            / 2018,2019 /;

parameters
tc(A,D,C) "Transport distance in 1.000 km (intranational distance is set to 1000000)"
/    USA     .USA        .C01 1000000
USA     .Japan      .C01 10.14
USA     .Germany    .C01 7.85
Japan   .Japan      .C01 1000000
Japan   .USA        .C01 10.14
Japan   .Germany    .C01 9.04
Germany .Germany    .C01 1000000
Germany .USA        .C01 7.85
Germany .Japan      .C01 10.14 /

x(A,C,T) "Export"
/    USA        .C01    .2018   5000
USA        .C01    .2019   6000
Japan      .C01    .2018   2000
Japan      .C01    .2019   2500
Germany    .C01    .2018   3000
Germany    .C01    .2019   3500 /

y(D,C,T) "Import"
/    USA        .C01    .2018   4000
USA        .C01    .2019   5000
Japan      .C01    .2018   3000
Japan      .C01    .2019   3500
Germany    .C01    .2018   3000
Germany    .C01    .2019   3500 /
fc(A,D,C) "Function of transport distance";

fc(A,D,C) = exp(tc(A,D,C)*(-1));
loop((A,D)\$(ord(A)=ord(D)),
fc(A,D,C) = 0;
;)``````
Then I am solving A_i^m and B_j^m and calculating x_{ij}^m (called "Trade" in the code below) in the following way:

Code: Select all

``````    parameters Av(A,C,I),Bv(D,C,I),AvT(A,C),BvT(D,C),trade(A,D,C,T);

loop(T,
loop(C,
Av(A,C,"I01") = 1;
loop(I\$(I.pos>1 and I.pos<100),
Bv(D,C,I) = 1 / (sum(A,fc(A,D,C) * Av(A,C,I-1) * x(A,C,T)));
Av(A,C,I) = 1 / (sum(D,fc(A,D,C) * Bv(D,C,I) * y(D,C,T)));
AvT(A,C) = Av(A,C,I);
BvT(D,C) = Bv(D,C,I);
);
Av(A,C,"I01") = AvT(A,C);
Bv(D,C,"I01") = BvT(D,C);
);