lingo实现的TSP算法

model:
sets:
city/1..6/:u;
link(city,city):dist,x;!距离矩阵,x为决策变量;
endsets
data:
dist=0 702 454 842 2396 1196
     702 0 324 1093 2136 764
     454 324 0 1137 2180 798
     842 1093 1137 0 1616 1857
     2396 2136 2180 1616 0 2900
     1196 764 798 1857 2900 0;    
enddata
min=@sum(link:dist*x);
n=@size(city);
@for(link:@bin(x));
!不能从自己到自己;
@for(city(i):x(i,i)=0);
!每个城市出发一次回来一次;
@for(city(k):@sum(city(i):x(i,k))=1;
             @sum(city(j):x(k,j))=1;);
!防止结果出现子巡回;
@for(city(i):u(i)<=n-1);
@for(city(i):@for(city(j)|j#ne#i#and#j#gt#1:
                  u(i)-u(j)+n*x(i,j)<=n-1));
end

你可能感兴趣的:(数学建模)