TSP的AMPL代码

AMPL代码:

 #旅行售货员问题 param N:=30; param locx{1..N}:=Uniform01(); param locy{1..N}:=Uniform01(); var u{1..N}; param dist{1..N,1..N}; var x{1..N,1..N} binary; for{i in 1..N,j in 1..N: i<>j} let dist[i,j]:=sqrt((locx[i]-locx[j])^2+(locy[i]-locy[j])^2); minimize obj:sum{ i in 1..N,j in 1..N: i<>j} dist[i,j]* x[i,j]; #进入城市k C1{k in 1..N}: sum{i in 1..N: i <> k} x[i,k] =1; #离开城市k C2{k in 1..N}: sum{i in 1..N: i <> k} x[k,i] =1; #保证不出现子圈 C3{i in 1..N, j in 1..N: i>1 and j>1 and i <> j}: u[i]-u[j]+N*x[i,j]<=N-1; #限制u的范围以加速模型的求解,保证所加限制并不排除掉TSP问题的最优解 C4{i in 1..N: i>1}: u[i]<=N-2; option solver cplex; solve; printf '最短距离:%f/n',obj; param curnode; param i; let curnode:=1; printf "最优回路:%d ",curnode; for{1.. N} { let i:=1; repeat while x[curnode,i]=0 { let i:=i+1; } let curnode:=i; printf "--> %d ",curnode; }

 

输出结果:

CPLEX 12.1.0: optimal integer solution; objective 4.984886258
65986 MIP simplex iterations
1603 branch-and-bound nodes
26 cover cuts
1 flow-cover cut
12 clique cuts
45 Gomory cuts
57 implied-bound cuts
16 zero-half cuts
66 mixed-integer rounding cuts
最短距离:4.984886
最优回路:1 --> 21 --> 9 --> 25 --> 18 --> 15 --> 26 --> 8 --> 10 --> 24 --> 29 --> 30 --> 19 --> 6 --> 16 --> 28 --> 3 --> 4 --> 20 --> 13 --> 11 --> 23 --> 2 --> 5 --> 22 --> 27 --> 14 --> 17 --> 12 --> 7 --> 1

你可能感兴趣的:(TSP的AMPL代码)