01 Formulations For The TSP With AMPL
01 Formulations For The TSP With AMPL
This is all the data you need to solve the problem exactly. The problem is easy to describe, but very hard to solve exactly. A graphical display is on the next page.
90
60 North
2 4 8 7 6
30
9
30
East
60
90
1948: Popularised at RAND Corp., a U.S. military thinktank, which helped developed OR.
1954: Solution of a large-scale traveling-salesman problem, Dantzig, Fulkerson & Johnson, J. of Ops Research of America. At RAND, they solved a 49-city TSP to optimality. DF&J thought a nearly optimal tour could be improved, and then optimality could be guaranteed, by adding just a few cuts. DF&J were the first to use cuts and B&B for integer programming.
1971 Cook, 1972 Karp, 1973 Levin: computational complexity. How hard is a problem to solve, as a function of the input data? If Problem 1 converts easily to Problem 2 & vice versa, then Problem 1 is as easy or hard as Problem 2.
Some problems are easy: shortest path, min spanning tree, assignment. impossible: given a computer program & input, will it stop? Stephen Cook hard: TSP because no good algorithms have been found. Many hard problems, such as job shop scheduling, can be converted algebraically to & from the TSP. A good TSP algorithm will be good for other hard problems. The TSP is easy to state, takes no math background to understand, and no great talent to find feasible solutions. Its fun, and invites recreational problem solvers.
Explanation: 1) Maximise value of assignments. 2) Assign each teacher i to one course. 3) Assign each course j to one teacher.
Almost the TSP. Is AP a possible formulation for the TSP? Indices: i, j = city. Parameter: cij = cost to go from city i to city j. Variables: xij = 1 if we drive from city i to city j, else 0.
We have subtours.
3 1
10
2
4 5
8
7
Oops. How do we get rid of these? 1. Can you write AMPL code to print the tour from an optimal solution? 2. Can you write AMPL code to print a subtour of an infeasible solution? 3. Do we need an integer solution to find subtours?
3 The Dantzig, Fulkerson & Johnson (DFJ) model. Indices, parameters, & decision variables as before. 2 Minimise total cost: min ij cij xij , Enter each city once: i xij = 1 for all j. 4 Leave each city once: j xij = 1 for all i. Subtour breaking constraints: i,jS xij |S| 1, for every subset S. Binary integrality: xij {0, 1} for all i, j.
2n
subtour constraints.
11
For the subtour shown, add: x3,2 + x2,4 + x4,3 2. What are the others? After solving again with the new constraints, more subtours appear. For a large TSP, we may need many subtour breaking constraints. In the worst case, we may need 2n subtour breaking constraints. Next week, we will see a way to generate these constraints. The solution becomes fractional, so we also need to do B&B. However, every solution gives a lower bound on the optimum.
12
3 2
Minimise total cost: min ij cij xij , 4 Enter each city once: i xij = 1 for all j. Leave each city once: j xij = 1 for all i. Subtour breaking: ui + 1 uj + n(1 xij), for i = 2, , n, i j, j = 2,,n, xij {0, 1} for all i, j; ui 0 for all i. Fewer constraints, but harder to solve! The LP relaxation is not as tight. Okay for small problems, but is bad for large ones. Related variations are a bit tighter.
Ref: C. E. Miller, A. W. Tucker, and R. A. Zemlin, Integer programming formulations and traveling salesman problems, J. ACM, 7 (1960), pp. 326329.
13
14
subject to NoSubtour1 {(i,j) in Arcs: i<>j and i>=2 and j>=2}: u[i] - u[j] + N*x[i,j] <= N - 1; subject to NoSubtour2 {i in Nodes: i >= 2}: u[i] <= N - 1 - (N - 2)*x[1,i]; subject to NoSubtour3 {i in Nodes: i >= 2}: u[i] >= 1 + (N - 2)*x[i,1];
AMPL solution
u[1] u[4] u[12] u[6] u[2] u[11] u[9] u[3] u[7] u[10] u[5] u[8] 0 1 2 3 4 5 6 7 8 9 10 11 1,4 4,12 12,6 6,2 2,11 11,9 9,3 3,7 7,10 10,5 5,8 8,1
min 500 x1,1 + 70.9 x1,2 + 41.6 x1,3 + 29.1 x1,4 + 17.1 x1,5 + 56.1 x1,6 + 55.6 x1,7 + 7.3 x1,8 ... + 104 x12,10 + 39.4 x12,11 + 500 x12,12 subject to 3] x2,1 + x3,1 + x4,1 + x5,1 + x6,1 + x7,1 + x8,1 + x9,1 + x10,1 + x11,1 + x12,1 = 1 4] x1,2 + x1,3 + x1,4 + x1,5 + x1,6 + x1,7 + x1,8 + x1,9 + x1,10 + x1,11 + x1,12 = 1 ... 25] x1,12 + x2,12 + x3,12 + x4,12 + x5,12 + x6,12 + x7,12 + x8,12 + x9,12 + x10,12 + x11,12 = 1 26] x12,1 + x12,2 + x12,3 + x12,4 + x12,5 + x12,6 + x12,7 + x12,8 + x12,9 + x12,10 + x12,11 = 1 27] 12 x2,2 <= 11 28] 12 x2,3 + u2 - u3 <= 11 29] 12 x2,4 + u2 - u4 <= 11 30] 12 x2,5 + u2 - u5 <= 11 If we go from city 2 to ... city 5, then u2 + 1= u5. 143] 12 x12,8 - u8 + u12 <= 11 144] 12 x12,9 - u9 + u12 <= 11 145] 12 x12,10 - u10 + u12 <= 11 146] 12 x12,11 - u11 + u12 <= 11 147] 12 x12,12 <= 11 end inte x1,1 inte x1,2 ...
15
16
1
$3
$3
Indices: i, j = city. 9 Parameter: cij = cost to go from city i to city j. Variables: xij = 1 if we drive from city i to city j, else 0, defined only for i<j. Half as many variables as the asymmetric! Minimise total cost: Enter each city once: Subtour breaking: Binary integrality: min ij>i cij xij , j<i xji + j>i xij = 2 for all i. i,jS xij |S| 1, for each subset S. xij {0, 1} for all i, j. 1 $8 $3 9
17
The variables into city 5 are: x15, x25, x35, x45, x65, x75, x85, x95. The variables out of city 5 are: x51, x52, x53, x54, x56, x57, x58, x59. Since costs are symmetric, cij = cji, let's drop half the variables. For xij, require i < j. Allow only the variables going out. We need only variables x15, x25, x35, x45, x56, x57, x58, x59. The meaning is not Go in or come out, but use this arc. The summation makes sure that we cover only the variables we need. x15 + x25 + x35 + x45 + x56 + x57 + x58 + x59 = 2.
18
1. Min total cost: min ij cij xij , 2. Arrive in each city: ji yji 1, for i = 2,,n, 3. Flow with gain of f: ji yij ji yji = f, for i = 1,,n, 4. Only n positive vars: ij xij n, cardinality constraint, 5. Force the yij vars: yij (1 + nf)xij for all i, j, 6. Nonnegative, binary: yij 0 for all i, j, xij {0, 1} for all i, j. This formulation flows f material from city to city. As f approaches 0, the solution approaches the optimum. Hard to solve because of (4), the cardinality constraint. Other rows can be added to tighten the model.
Ref: JA Svestka, A continuous variable representation of the TSP, Math Prog, v15, 1978, pp211-213.
# TSP, Svestka formulation. Ref: JA Svestka, A continuous variable # representation of the TSP, Math Prog, v15, 1978, pp211 -213. param N integer > 2; # Number of nodes set Nodes ordered := {1..N}; set Arcs := {i in Nodes, j in Nodes: i <> j};
19
Demandy {i in Nodes: i>=2}: sum{(i,j) Flowy {i in Nodes: i>=2}: in Arcs} y[i,j] - sum {(i,j) in Arcs} Totalx: sum {(i,j) in Arcs} x[i,j] <= Arcgain {(i,j) in Arcs}: y[i,j] <= (1
# These rows tighten the model. subject to Demandx {i in Nodes: i>=2}: sum{(i,j) in Arcs} x[j,i] = 1; subject to Flowx {i in Nodes: i >= 2}: sum {(i,j) in Arcs} x[i,j] = sum {(i,j) in Arcs} x[j,i]; subject to Startx: sum {i in Nodes: i >= 2} x[1,i] = 1; subject to Starty: sum {i in Nodes: i >= 2} y[1,i] = 1;
y1,8 y8,7 y7,5 y5,4 y4,3 1.00 1.01 1.02 1.03 1.04 x1,8 x8,7 x7,5 x5,4 x4,3
y6,9 y9,12 y12,11 y11,10 y10,2 1.06 1.07 1.08 1.09 1.1 x6,9 x9,12 x12,11 x11,10 x10,2
AMPL solution
20
Minimise total cost: min ijt cij xijt , Flow in = flow out: i xi,j,t k xj,k,t+1 = 0 for all j, and t=1,,n. Go to each city once: jt xijt = 1, for i=1,,n. Binary integrality: xijt {0, 1} for all i, j, t.
This is tighter than the earlier formulations. Unfortunately this requires n3 variables. A 50-city problem would require 125,000 variables! Next: solution methods. Assignment + cuts. Assignment + B&B. Min spanning tree.
21
# TSP steps. Ref Linear Progg & Extensions, Dantzig, 1963; param N integer > 2; # Number of nodes set Nodes ordered := {1..N}; set Arcs := {i in Nodes, j in Nodes, k in Nodes: i <> j};
param C{i in Nodes, j in Nodes: i <> j}; var x {(i,j,k) in Arcs} binary;
minimize Tourlength: sum{(i,j,k) in Arcs: i<>j} C[i,j]*x[i,j,k]; subject to Demand {i in Nodes}: sum{(i,j,k) in Arcs} x[i,j,k] = 1; subject to End {i in Nodes}: sum {j in Nodes: j <> i} x[j,i,N] = sum {k in Nodes: k <> i} x[i,k,1]; subject to Step {t in Nodes, j in Nodes: t <= N - 1}: sum {i in Nodes: i <> j} x[i,j,t] = sum {k in Nodes: k <> j} x[j,k,t+1];
AMPL solution
12,9,6