An Introduction to the Quadratic Assignment Problem
The quadratic assignment problem (QAP) is an interesting and challenging class of combinatorial optimization problems. Let’s explore QAPs and how to solve them.
Linear Assignment Problems
Let’s start with QAP’s younger sibling, the linear assignment problem (LAP).
In an LAP we are trying to assign objects in one set to an equal number of objects in another set, where we are given the cost of assigning any pair of objects.
LAP: Mathematical Formulation
Here’s how we write it mathematically:
We’re trying to solve for x. represents whether box i on the left matches box j on the right.
LAP is also referred to as minimum cost weighted bipartite matching.
Linear Assignment Problems are easy to solve
In 1946, Birkhoff showed that the permutation matrices constitute the extreme points of the set of doubly stochastic matrices, that is, matrices whose row and column sums are 1. This means that we can drop the condition that is integer. That in turn means that it is easy to solve LAP efficiently, using approaches such as the Hungarian algorithm.
Quadratic Assignment Problems
QAP is just like LAP, except the cost of an assignment depends on two factors, not just one.
The standard way to talk about QAP is to think of a bunch of facilities that ship material between each other, and a set of locations where the facilities can be located. The shipment cost then depends on the product of how much material goes between facilities and how far away they are. This is what makes QAP “quadratic”.
Another example is to think about a bartender placing the bottles to minimize movement while mixing drinks, as I wrote about here. Or assigning characters to keys on a typewriter as I wrote about here.
The best way to get some intuition about QAP is to play around with one, so here you go:
QAP: Mathematical Formulation
We can write QAP mathematically like this:
is the flow of material between facilities i and k. is the distance between locations j and l.
When I want to talk about how big a QAP is, I will refer to N, the number of facilities and locations.
QAPs are very hard to solve
The Birkhoff trick I mentioned for LAP does not apply to QAP. QAP is NP-hard.
The number of possible solutions for QAP grows exponentially as problem size increases. For N=3, there are 6, because I can pick a facility and assign it to any of three available locations. Then for the next, there are only two locations left. For N=4 we then have 24 = 4! possibilities. And so on.
What does that mean? Well, when…
- N=14 there are more possible solutions than people on Earth (~8 billion);
- N=21, more than grains of sand on all the world’s beaches (~1018);
- N=59, more than atoms in the observable universe (1080).
Divide and Conquer using Partial Assignments
So good luck with brute force. We need ways of ruling out huge swaths of possible solutions.
One thought is to divide and conquer. Split the space of possible solutions into pieces, and find ways of proving that some of these pieces cannot lead to optimal solutions. A simple way to split QAPs into pieces is through partial assignments.
Suppose you are staring at a size N QAP. An interesting fact about QAP is that if you assign one facility to a location, you end up with a smaller QAP.
Let Q = (A, B, C) be a QAP of size n.
Assigning i → j yields a QAP of size n-1:
where = M with row i and column j removed, and = row i of A with i-th element removed.
Enumeration Trees
Since assigning i → j gives us a smaller QAP, if I pick a facility i and range over all locations j, I can form N smaller QAPs that cover all possibilities for the original QAP. Similarly, I can pick a fixed location j and range over all facilities. This is called branching.
Let be the set of row and column indexes.
Selecting i ∈ I is called branching. Φ(Y) → i is a branching method.
Repeatedly applying Φ generates a tree of QAPs, with trivial subproblems at the leaves.
The size of the tree is exponential in n.
Branch and Bound: Trimmed Enumeration Trees
Since the size of the enumeration tree is exponential in n, it buys us nothing unless we can prune branches from consideration.
This is where bounds come in. Suppose we have in hand a potential solution for our QAP, and now we want to prove that it really is optimal (or find something better).
Suppose also we have a way to produce a lower bound on the solution of a partial solution, for example by solving a simplified (relaxed) version of it. If the lower bound exceeds the value of the best known solution, then there is no way the partial solution can lead to something better. So we prune (aka “fathom”) it.
Bounds are the trick that allows us to make the search tractable, but only if the bounds are tight, i.e. they help us fathom frequently.
Here is a visual of what we’re trying to do:
The pseudocode looks something like the following:
def branch_and_bound(P):
v = initial_solution(P)
S = {P}
while not empty(S):
Q = take(S)
if easy(Q):
v = enumerate(Q, v)
else:
z, Y = bound(Q) # Y = branching context
if z <= v: # no fathom
S += branch(Q, Y)
return v
Branch and Bound: Four Things Matter
To make this real we have to make choices about how to branch and how to bound. It comes down to four things:
| Thing | Aspiration | Metric |
|---|---|---|
| Bound Quality | Fathom as often as possible | (z - w) / w |
| Bound Speed | Calculate as fast as possible | cpu time |
| Branching Quality | Make future nodes as easy as possible | ?? |
| Branching Speed | Create new nodes as fast as possible | cpu time |
This is what my thesis was about.
Quadratic Programming Bound: pretty fast, pretty good
The most important part of my thesis was proposing a new bound for QAP based on convex quadratic programming.
This is the relaxed version of the problem as described in Anstreicher, Brixius (2001):
For complicated but not-too-hard-to-compute .
When you solve this, you get a lower bound, which we call QPB. We found that QPB was a reasonably tight bound - it helped us fathom more quickly than many bounds.
We also found a trick that allowed us to compute a “close enough” version of QPB by iteratively solving LAPs, which I mentioned earlier are very cheap to compute.
Finally, QPB produces a dual matrix U which is helpful for branching. QPB helps with all four things. In summary:
| Attribute | Implication |
|---|---|
| Good quality | Iterative solve → can stop anytime |
| Can solve fast using LAP | Yields dual matrix U → good branching |
QPB vs GLB for nug18
A classic lower bound for QAP is the Gilmore-Lawler bound (GLB). When we compare QPB and GLB when we use them on a standard test problem (nug18), we see the advantages that QPB brings:
| QPB | GLB | |
|---|---|---|
| Total Time | 39.8741 seconds | 244.089 seconds |
| Nodes | 265917 / 215368 / 0 | 34717569 / 25994653 / 5 |
| root bound | 1688.79 | 1554 |
| fw iterations | 7668323 | 0 |
If we track what happens at each level of the branch-and-bound tree, here is what we find. Stronger bounds mean that we fathom more at the top of the tree. This prevents us from creating so many children farther down. If you can’t fathom aggressively, you lose because the exponential overwhelms you.
| Level | QPB nodes | % fathom | % c.elim | Time |
|---|---|---|---|---|
| 0 | 1 | 0% | 0% | 0.18 s |
| 1 | 18 | 0% | 1.3% | 1.94 s |
| 2 | 302 | 0.3% | 11.9% | 2.34 s |
| 3 | 4,245 | 39.7% | 34.5% | 3.04 s |
| 4 | 25,165 | 65% | 49.1% | 6.43 s |
| 5 | 62,741 | 76.3% | 58.2% | 10.53 s |
| 6 | 80,792 | 83.4% | 64% | 9.10 s |
| 7 | 57,990 | 87.4% | 68.1% | 4.56 s |
| 8 | 25,682 | 90.1% | 71.6% | 1.41 s |
| 9 | 7,202 | 92% | 71.9% | 0.29 s |
| 10 | 1,447 | 91.1% | 72.8% | 0.05 s |
| 11 | 281 | 92.5% | 71.4% | 0.01 s |
| 12 | 42 | 90.5% | 75% | 0.00 s |
| 13 | 6 | 66.7% | 70% | 0.00 s |
Node and time bars use a log scale, shared by QPB and GLB. % fathom is the share of a level’s nodes that the bound fathomed; % c.elim is the share of candidate children eliminated before they were created.
Branching using dual information
We haven’t said anything yet about how to make a good branching decision. In essence, you want to pick the facility (or location) that will result in partial assignments that are as easy as possible to solve.
If we had a crystal ball, we could do that. But we don’t. The next best thing is a “score” that is a proxy for partial assignment difficulty.
Many bounds produce dual matrices U that can serve as such a score.
Suppose .
Let's minimize the number of subproblems created, using net bound improvement as a tiebreaker.
This is a very efficient branching rule because the dual matrix U “comes for free” when you compute QPB.
Strong branching uses bounds from subproblems
The problem with dual branching is that while it is very cheap to obtain, the score is not always a great predictor of subproblem difficulty.
A better score is obtained by just computing the bound for the subproblem. The closer it is to the incumbent solution, the easier the subproblem is likely to be. Doing this is called strong branching.
Let
Strong branching: Φ defined as before, using Z in place of U. Computational cost is O(B n²) where B is the cost of the bound.
Lookahead branching combines strong branching and dual branching:
- Z at level n+1
- U at level n+2 (which comes for free when computing Z)
Strong branching reduces tree size
Strong branching works in reducing tree size. If we apply both strong branching and dual branching, and compare the number of nodes that are created in the branch and bound tree, we see that the ratios are very favorable:
| problem | strong / dual |
|---|---|
| nug12 | 0.08 |
| nug15 | 0.06 |
| nug16b | 0.09 |
| nug18 | 0.13 |
| had16 | 0.31 |
| had18 | 0.25 |
Branching choices can have a huge impact
Branching choices can have a big impact on the size of the branch and bound tree. I looked at what happens when we make 650 different branching choices at level 4 of the branch-and-bound tree for test problem nug16b. Many of the branching choices lead to runtime costs of 2x or more, as compared to the best possible branching choice.
Spending time to make good branching choices often pays dividends. The worst branching choice leads to 7.5x more runtime, as compared to the best.
Putting it all together: nug30
The centerpiece of our work with my advisor Kurt Anstreicher and our friends and colleagues Jeff Linderoth, Jean-Pierre Goux, and Steve Wright, was to solve the famous “nug30” QAP.
The QAPs in the “play” app are all “nug” QAPs; smaller versions of “the big one”.
Nug stands for Christopher Nugent, who with Thomas Vollmann and John Ruml introduced these QAPs in a paper from 1968. The size 12 instance, nug12, is special. It was actually introduced by Frederick S. Hillier in 1963 in the context of “departments” and “site locations”. The other nug instances are extensions of the size 12 instance. Hillier was an eminent figure in operations research, and passed away only recently.
In 2000, Kurt, Jeff, JP and I solved nug30 for the first time, leveraging all the tricks above:
- QPB, a bound that balanced quality and speed;
- Clever tricks to make smart branching decisions;
- Clever tricks to speed up bound computation;
- Leveraging symmetry in the problem (which I discussed in my “QAP for bartenders” post)
And one more: distributed computing, which was in its infancy at that time. The branch-and-bound tree, even with all of the tricks above, had over 10 billion nodes. We used 653 workstations and 6.9 CPU-years of time to solve nug30 in 7 days of wall clock time.
| Level | QPB nodes | % fathom | % c.elim | Time |
|---|---|---|---|---|
| 0 | 1 | 0% | 0% | 25.08 s |
| 1 | 9 | 0% | 0% | 608.7 s |
| 2 | 225 | 0% | 0% | 4.89 h |
| 3 | 6,120 | 0% | 4% | 4.61 d |
| 4 | 157,947 | 1.7% | 18.2% | 34 d |
| 5 | 3.3M | 15.8% | 38.9% | 229 d |
| 6 | 42.5M | 39.5% | 53.6% | 684 d |
| 7 | 286M | 61.4% | 62.5% | 2.62 y |
| 8 | 956M | 74.3% | 64.2% | 362 d |
| 9 | 1.94B | 82.1% | 63.4% | 480 d |
| 10 | 2.66B | 85.4% | 66.4% | 522 d |
| 11 | 2.61B | 87.4% | 68.3% | 381 d |
| 12 | 1.98B | 90.9% | 71.1% | 225 d |
| 13 | 939M | 92.1% | 73.3% | 88 d |
| 14 | 339M | 92.7% | 75.1% | 26.5 d |
| 15 | 99M | 93% | 76.9% | 6.64 d |
| 16 | 24.2M | 93.2% | 78.3% | 32.8 h |
| 17 | 5M | 93.4% | 79.4% | 5.71 h |
| 18 | 885,554 | 93.6% | 80.3% | 50.7 min |
| 19 | 135,201 | 93.7% | 80.8% | 389.9 s |
| 20 | 18,058 | 93.1% | 81.4% | 43.97 s |
| 21 | 2,317 | 90.9% | 81.8% | 5.07 s |
| 22 | 344 | 86.1% | 80% | 0.83 s |
| 23 | 77 | 83.1% | 79.1% | 0.23 s |
| 24 | 19 | 73.7% | 73.3% | 0.04 s |
| 25 | 8 | 75% | 80% | 0.02 s |
| 26 | 2 | 0% | 75% | 0.00 s |
| 27 | 2 | 0% | – | 0.00 s |
Node and time bars use a log scale. % fathom is the share of a level’s nodes that the bound fathomed; % c.elim is the share of candidate children eliminated before they were created.