examples/tsp.mod
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

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