Good day,
I am currently a new user GAMS. I am solving a problem that have to do the use of logic fucntion as shown in the code below.(The problem starts from the objective function definition).
The problem is an example of the TRAVELKLING SALESMAN PROBLEM in optimization.
Your urgent help or advice will be appreciated.
Sets
i Stations /E1, E2, E3, E4, E5, E6/;
Alias(i,j)
Parameters
xCoord(i) /E1 20, E2 40, E3 180, E4 130, E5 160, E6 80/
yCoord(i) /E1 90, E2 10, E3 30, E4 40, E5 10, E6 80/;
Scalar n /6/;
* Calculate the distances between stations
Table Dist(i, j) Distance
E1 E2 E3 E4 E5 E6
E1 0 0 0 0 0 0
E2 0 0 0 0 0 0
E3 0 0 0 0 0 0
E4 0 0 0 0 0 0
E5 0 0 0 0 0 0
E6 0 0 0 0 0 0;
* Fill the table with Euclidean distances
Loop(i,
Loop(j,
Dist(i,j) = sqrt((xCoord(i) - xCoord(j))**2 + (yCoord(i) - yCoord(j))**2);
);
);
Binary Variables
x(i, j) "1 if edge (i, j) is used, 0 otherwise";
Variables
Z "Total distance traveled";
* Objective function
Equations
obj "Objective function"
visit(i) "Ensure each station is visited exactly once";
obj..
Z =E= sum((i,j)$(i <> j), Dist(i,j) * x(i,j));
visit(i)..
sum(j$(j <> i), x(i,j)) =E= 1;
* Each station should be left exactly once
visit2(j)..
sum(i$(i <> j), x(i,j)) =E= 1;
* Subtour elimination constraints
Equations
subtour(i) "Subtour elimination constraints";
* Define a variable for subtour elimination
Positive Variables
u(i) "Number of stations visited before station i";
subtour(i)..
u(i) - u(j) + n * x(i,j) <= n - 1;
Model TSP /all/;
Solve TSP using mip minimizing Z;
Display x.l, Z.l;```