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.

lap08 · 8 + 8 boxes, 26 arcs
Cost so far
0
Match every box on the left to one on the right. The best possible cost is 23.
Tap a box on one side and then a box on the other, or drag from one to the other. Dashed arcs show which pairs may be matched; select a box to see what each costs. Matching a pair again unmatches it. Buttons: random, greedy, optimal and clear.

LAP: Mathematical Formulation

Here’s how we write it mathematically:

min⁡ ∑i,jncijxijs.t. ∑inxij=1,j=1..n∑jnxij=1,i=1..nxij∈{0,1}X∈Π

We’re trying to solve for x. xij 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 xij 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:

nug08 · 8 facilities, 8 locations
Cost so far
0
Place the facilities. The best possible cost is 214.
Unassigned
Drag a facility onto a location, or tap a facility and then a location. Dropping onto a taken location swaps the two. Lines join facilities that exchange flow, heavier for more; the cost adds flow times distance over every pair, so heavy lines should be short. Buttons: random, greedy, optimal and clear.

QAP: Mathematical Formulation

We can write QAP mathematically like this:

minX∈Π⁡∑i,j,k,lnaikbjlxijxkl+∑i,jncijxij

aik is the flow of material between facilities i and k. bjl 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 Qij of size n-1:

Qij=(Aij,Bij,Cij,dij)Aij=A(ii)Bij=B(jj)Cij=C(ij)+2aˆibˆjdij=aiibjj+cij

where M(ij) = M with row i and column j removed, and aˆi = row i of A with i-th element removed.

A QAP Q at the root of a tree with four children, one of them labelled Q superscript ij. Below a rule: select i / j; Q →  Q^ij .

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 I=IR∪IC 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:

A branch-and-bound tree: z = bound(Q) at the root; branch along its edges; two children crossed out and labelled fathom; two leaves labelled enumerate. Below, the set S of six open subproblems, and v, the best solution so far.

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:

ThingAspirationMetric
Bound QualityFathom as often as possible(z - w) / w
Bound SpeedCalculate as fast as possiblecpu time
Branching QualityMake future nodes as easy as possible??
Branching SpeedCreate new nodes as fast as possiblecpu 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):

QPB:min⁡ 𝐯𝐞𝐜(X)TQ𝐯𝐞𝐜(X)+C∙X+⟨λ(Aˆ),λ(Bˆ)⟩−s.t. Xe=XTe=e, X≥0

For complicated but not-too-hard-to-compute Q,Aˆ,Bˆ.

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:

AttributeImplication
Good qualityIterative solve → can stop anytime
Can solve fast using LAPYields 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:

QPBGLB
Total Time39.8741 seconds244.089 seconds
Nodes265917 / 215368 / 034717569 / 25994653 / 5
root bound1688.791554
fw iterations76683230

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.

nug18 · 14 levels · QPB
Nodes
265,914
CPU time
39.88 s

QPB nodes, fathom rate, children eliminated and CPU time by level of the branch-and-bound tree for nug18
LevelQPB nodes% fathom% c.elimTime
010%0%0.18 s
1180%1.3%1.94 s
23020.3%11.9%2.34 s
34,24539.7%34.5%3.04 s
425,16565%49.1%6.43 s
562,74176.3%58.2%10.53 s
680,79283.4%64%9.10 s
757,99087.4%68.1%4.56 s
825,68290.1%71.6%1.41 s
97,20292%71.9%0.29 s
101,44791.1%72.8%0.05 s
1128192.5%71.4%0.01 s
124290.5%75%0.00 s
13666.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 uij≤bound(Qij).

Let's minimize the number of subproblems created, using net bound improvement as a tiebreaker.

Φ(Y)=arg⁡lexmini⁡(fi,si)fijr={1if zij≥bound(Qij)+uij0otherwisesijr=∑juij

Dual branching. Row branching: each row of the dual matrix U gives a score (F, S) for branching on that row; row i is highlighted with its score (F_i, S_i). Column branching: column j of U gives (F_j, S_j).

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 uij 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 zij=bound(Qij)

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)
A triangle labelled B&B tree, with an arrow down its side reading higher depth, lower gap, more nodes.
Use higher quality, higher cost branching methods selectively.

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:

Bar chart of node count with strong branching divided by node count with dual branching: nug12 0.08, nug15 0.06, nug16b 0.09, nug18 0.13, had16 0.31, had18 0.25.
Node count: strong / dual branching.
problemstrong / dual
nug120.08
nug150.06
nug16b0.09
nug180.13
had160.31
had180.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.

Histogram of about 650 branching choices at level 4 of nug16b by runtime ratio from the best choice, from 1× to 8×; most fall between 1.5× and 3.5×, with a long tail to about 8×.

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.

nug30 · 28 levels · QPB
Nodes
11,892,208,412
CPU time
10.9 y
QPB nodes, fathom rate, children eliminated and CPU time by level of the branch-and-bound tree for nug30
LevelQPB nodes% fathom% c.elimTime
010%0%25.08 s
190%0%608.7 s
22250%0%4.89 h
36,1200%4%4.61 d
4157,9471.7%18.2%34 d
53.3M15.8%38.9%229 d
642.5M39.5%53.6%684 d
7286M61.4%62.5%2.62 y
8956M74.3%64.2%362 d
91.94B82.1%63.4%480 d
102.66B85.4%66.4%522 d
112.61B87.4%68.3%381 d
121.98B90.9%71.1%225 d
13939M92.1%73.3%88 d
14339M92.7%75.1%26.5 d
1599M93%76.9%6.64 d
1624.2M93.2%78.3%32.8 h
175M93.4%79.4%5.71 h
18885,55493.6%80.3%50.7 min
19135,20193.7%80.8%389.9 s
2018,05893.1%81.4%43.97 s
212,31790.9%81.8%5.07 s
2234486.1%80%0.83 s
237783.1%79.1%0.23 s
241973.7%73.3%0.04 s
25875%80%0.02 s
2620%75%0.00 s
2720%–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.