TSP - output the path

Here’s an example that you can adapt to your needs. Supposing that you write

set N;
var x {N,N};

in your model and

set N := A B C D ;

in your data, you could use the following AMPL statements to determine and display the path:

set PATH ordered within N;

let PATH := {j in N: card {i in N: x[i,j] = 1} = 0};
let {i in 2..card(N)} PATH := PATH union {j in N: x[last(PATH),j] = 1};

print {i in PATH} i & (if i!=last(PATH) then " ->" else "");

There are four nodes but only 3 arcs in the solution, so the first let statement initializes set PATH to contain the one node (A) that does not have any arcs leading into it. Then the second let statement follows the path for 3 steps.

1 Like