You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
6.5 KiB

3 months ago
  1. /* TSP, Traveling Salesman Problem */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* The Traveling Salesman Problem (TSP) is stated as follows.
  4. Let a directed graph G = (V, E) be given, where V = {1, ..., n} is
  5. a set of nodes, E <= V x V is a set of arcs. Let also each arc
  6. e = (i,j) be assigned a number c[i,j], which is the length of the
  7. arc e. The problem is to find a closed path of minimal length going
  8. through each node of G exactly once. */
  9. param n, integer, >= 3;
  10. /* number of nodes */
  11. set V := 1..n;
  12. /* set of nodes */
  13. set E, within V cross V;
  14. /* set of arcs */
  15. param c{(i,j) in E};
  16. /* distance from node i to node j */
  17. var x{(i,j) in E}, binary;
  18. /* x[i,j] = 1 means that the salesman goes from node i to node j */
  19. minimize total: sum{(i,j) in E} c[i,j] * x[i,j];
  20. /* the objective is to make the path length as small as possible */
  21. s.t. leave{i in V}: sum{(i,j) in E} x[i,j] = 1;
  22. /* the salesman leaves each node i exactly once */
  23. s.t. enter{j in V}: sum{(i,j) in E} x[i,j] = 1;
  24. /* the salesman enters each node j exactly once */
  25. /* Constraints above are not sufficient to describe valid tours, so we
  26. need to add constraints to eliminate subtours, i.e. tours which have
  27. disconnected components. Although there are many known ways to do
  28. that, I invented yet another way. The general idea is the following.
  29. Let the salesman sell, say, cars, starting the travel from node 1,
  30. where he has n cars. If we require the salesman to sell exactly one
  31. car in each node, he will need to go through all nodes to satisfy
  32. this requirement, thus, all subtours will be eliminated. */
  33. var y{(i,j) in E}, >= 0;
  34. /* y[i,j] is the number of cars, which the salesman has after leaving
  35. node i and before entering node j; in terms of the network analysis,
  36. y[i,j] is a flow through arc (i,j) */
  37. s.t. cap{(i,j) in E}: y[i,j] <= (n-1) * x[i,j];
  38. /* if arc (i,j) does not belong to the salesman's tour, its capacity
  39. must be zero; it is obvious that on leaving a node, it is sufficient
  40. to have not more than n-1 cars */
  41. s.t. node{i in V}:
  42. /* node[i] is a conservation constraint for node i */
  43. sum{(j,i) in E} y[j,i]
  44. /* summary flow into node i through all ingoing arcs */
  45. + (if i = 1 then n)
  46. /* plus n cars which the salesman has at starting node */
  47. = /* must be equal to */
  48. sum{(i,j) in E} y[i,j]
  49. /* summary flow from node i through all outgoing arcs */
  50. + 1;
  51. /* plus one car which the salesman sells at node i */
  52. solve;
  53. printf "Optimal tour has length %d\n",
  54. sum{(i,j) in E} c[i,j] * x[i,j];
  55. printf("From node To node Distance\n");
  56. printf{(i,j) in E: x[i,j]} " %3d %3d %8g\n",
  57. i, j, c[i,j];
  58. data;
  59. /* These data correspond to the symmetric instance ulysses16 from:
  60. Reinelt, G.: TSPLIB - A travelling salesman problem library.
  61. ORSA-Journal of the Computing 3 (1991) 376-84;
  62. http://elib.zib.de/pub/Packages/mp-testdata/tsp/tsplib */
  63. /* The optimal solution is 6859 */
  64. param n := 16;
  65. param : E : c :=
  66. 1 2 509
  67. 1 3 501
  68. 1 4 312
  69. 1 5 1019
  70. 1 6 736
  71. 1 7 656
  72. 1 8 60
  73. 1 9 1039
  74. 1 10 726
  75. 1 11 2314
  76. 1 12 479
  77. 1 13 448
  78. 1 14 479
  79. 1 15 619
  80. 1 16 150
  81. 2 1 509
  82. 2 3 126
  83. 2 4 474
  84. 2 5 1526
  85. 2 6 1226
  86. 2 7 1133
  87. 2 8 532
  88. 2 9 1449
  89. 2 10 1122
  90. 2 11 2789
  91. 2 12 958
  92. 2 13 941
  93. 2 14 978
  94. 2 15 1127
  95. 2 16 542
  96. 3 1 501
  97. 3 2 126
  98. 3 4 541
  99. 3 5 1516
  100. 3 6 1184
  101. 3 7 1084
  102. 3 8 536
  103. 3 9 1371
  104. 3 10 1045
  105. 3 11 2728
  106. 3 12 913
  107. 3 13 904
  108. 3 14 946
  109. 3 15 1115
  110. 3 16 499
  111. 4 1 312
  112. 4 2 474
  113. 4 3 541
  114. 4 5 1157
  115. 4 6 980
  116. 4 7 919
  117. 4 8 271
  118. 4 9 1333
  119. 4 10 1029
  120. 4 11 2553
  121. 4 12 751
  122. 4 13 704
  123. 4 14 720
  124. 4 15 783
  125. 4 16 455
  126. 5 1 1019
  127. 5 2 1526
  128. 5 3 1516
  129. 5 4 1157
  130. 5 6 478
  131. 5 7 583
  132. 5 8 996
  133. 5 9 858
  134. 5 10 855
  135. 5 11 1504
  136. 5 12 677
  137. 5 13 651
  138. 5 14 600
  139. 5 15 401
  140. 5 16 1033
  141. 6 1 736
  142. 6 2 1226
  143. 6 3 1184
  144. 6 4 980
  145. 6 5 478
  146. 6 7 115
  147. 6 8 740
  148. 6 9 470
  149. 6 10 379
  150. 6 11 1581
  151. 6 12 271
  152. 6 13 289
  153. 6 14 261
  154. 6 15 308
  155. 6 16 687
  156. 7 1 656
  157. 7 2 1133
  158. 7 3 1084
  159. 7 4 919
  160. 7 5 583
  161. 7 6 115
  162. 7 8 667
  163. 7 9 455
  164. 7 10 288
  165. 7 11 1661
  166. 7 12 177
  167. 7 13 216
  168. 7 14 207
  169. 7 15 343
  170. 7 16 592
  171. 8 1 60
  172. 8 2 532
  173. 8 3 536
  174. 8 4 271
  175. 8 5 996
  176. 8 6 740
  177. 8 7 667
  178. 8 9 1066
  179. 8 10 759
  180. 8 11 2320
  181. 8 12 493
  182. 8 13 454
  183. 8 14 479
  184. 8 15 598
  185. 8 16 206
  186. 9 1 1039
  187. 9 2 1449
  188. 9 3 1371
  189. 9 4 1333
  190. 9 5 858
  191. 9 6 470
  192. 9 7 455
  193. 9 8 1066
  194. 9 10 328
  195. 9 11 1387
  196. 9 12 591
  197. 9 13 650
  198. 9 14 656
  199. 9 15 776
  200. 9 16 933
  201. 10 1 726
  202. 10 2 1122
  203. 10 3 1045
  204. 10 4 1029
  205. 10 5 855
  206. 10 6 379
  207. 10 7 288
  208. 10 8 759
  209. 10 9 328
  210. 10 11 1697
  211. 10 12 333
  212. 10 13 400
  213. 10 14 427
  214. 10 15 622
  215. 10 16 610
  216. 11 1 2314
  217. 11 2 2789
  218. 11 3 2728
  219. 11 4 2553
  220. 11 5 1504
  221. 11 6 1581
  222. 11 7 1661
  223. 11 8 2320
  224. 11 9 1387
  225. 11 10 1697
  226. 11 12 1838
  227. 11 13 1868
  228. 11 14 1841
  229. 11 15 1789
  230. 11 16 2248
  231. 12 1 479
  232. 12 2 958
  233. 12 3 913
  234. 12 4 751
  235. 12 5 677
  236. 12 6 271
  237. 12 7 177
  238. 12 8 493
  239. 12 9 591
  240. 12 10 333
  241. 12 11 1838
  242. 12 13 68
  243. 12 14 105
  244. 12 15 336
  245. 12 16 417
  246. 13 1 448
  247. 13 2 941
  248. 13 3 904
  249. 13 4 704
  250. 13 5 651
  251. 13 6 289
  252. 13 7 216
  253. 13 8 454
  254. 13 9 650
  255. 13 10 400
  256. 13 11 1868
  257. 13 12 68
  258. 13 14 52
  259. 13 15 287
  260. 13 16 406
  261. 14 1 479
  262. 14 2 978
  263. 14 3 946
  264. 14 4 720
  265. 14 5 600
  266. 14 6 261
  267. 14 7 207
  268. 14 8 479
  269. 14 9 656
  270. 14 10 427
  271. 14 11 1841
  272. 14 12 105
  273. 14 13 52
  274. 14 15 237
  275. 14 16 449
  276. 15 1 619
  277. 15 2 1127
  278. 15 3 1115
  279. 15 4 783
  280. 15 5 401
  281. 15 6 308
  282. 15 7 343
  283. 15 8 598
  284. 15 9 776
  285. 15 10 622
  286. 15 11 1789
  287. 15 12 336
  288. 15 13 287
  289. 15 14 237
  290. 15 16 636
  291. 16 1 150
  292. 16 2 542
  293. 16 3 499
  294. 16 4 455
  295. 16 5 1033
  296. 16 6 687
  297. 16 7 592
  298. 16 8 206
  299. 16 9 933
  300. 16 10 610
  301. 16 11 2248
  302. 16 12 417
  303. 16 13 406
  304. 16 14 449
  305. 16 15 636
  306. ;
  307. end;