model: sets: cities/1..9/:level; link(cities, cities): distance, x; !距离矩阵; endsets data: distance = 0 200 660 170 127 225 490 420 330 200 0 820 300 90 60 310 225 151 660 820 0 530 770 120 1110 1050 960 170 300 530 0 280 350 600 550 450 127 90 770 280 0 110 370 310 210 225 60 120 350 110 0 280 250 120 490 310 1110 600 370 280 0 290 190 420 225 1050 550 310 250 290 0 160 330 151 960 450 210 120 190 160 0 ; enddata n = @size(cities); !目标函数; min=@sum(link:distance * x); @For(cities(k): !进入城市k; @sum(cities(i) | i #ne# k: x(i,k)) = 1; !离开城市k; @sum(cities(j) | j #ne# k: x(k,j)) = 1; ); !保证不出现子圈; @for(cities(i) | i #gt# 1: @for(cities(j) | j #gt# 1 #and# i#ne#j: level(i)-level(j) + n*x(i,j) <= n-1); ); !限制u的范围以加速模型的求解,保证所加限制并不排除掉TSP问题的最优解; @for(cities(i) | i #gt# 1: level(i) <= n-2); !定义x为 0\1变量; @for(link:@bin(x)); end
时间: 2024-12-15 18:10:36