| Title: | Heuristics for the Quadratic Assignment Problem (QAP) |
|---|---|
| Description: | Implements a simulated annealing heuristic for the Quadratic Assignment Problem (QAP). Originally formulated as a facility location problem in operations research, the QAP also has applications in data analysis. The problem is NP-hard. |
| Authors: | Michael Hahsler [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-2716-1405>), Franz Rendl [ctb, cph] |
| Maintainer: | Michael Hahsler <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1-3 |
| Built: | 2026-10-01 19:53:05 UTC |
| Source: | https://github.com/mhahsler/qap |
Solve a quadratic assignment problem (QAP) with a simulated annealing
heuristic. qap.obj() calculates the objective value of an assignment.
qap(A, B, method = NULL, ...) qap.obj(A, B, o)qap(A, B, method = NULL, ...) qap.obj(A, B, o)
A |
A numeric matrix of flows between facilities. For |
B |
A numeric matrix of distances between locations. For |
method |
Solver name. Currently only |
... |
Additional arguments passed to the simulated annealing solver. See Details. |
o |
A permutation of |
The problem is to assign facilities to locations to minimize total transportation or
interaction costs.
Required flows between the facilities are represented by the matrix and distances between locations
is given in matrix . The QAP seeks an assignment that minimizes the
sum of flows times distance.
For an assignment represented by a permutation matrix used to
assign the facilities to the locations in the order given by the
permutation,
the objective can be written as
where is the set of all valid permutation matrices.
Note that for symmetric distances, the distance matrix does not need to be transposed.
The QAP originated as a facility location problem (Koopmans and Beckmann, 1957) and also has applications in data analysis (Hubert and Schultz, 1976). It is NP-hard. The solver uses the simulated annealing heuristic of Burkard and Rendl (1984), based on Rendl's Fortran implementation from QAPLIB. The authors suggest it can produce heuristic solutions for problems with up to 256 objects, although the implementation does not enforce this limit.
Additional solver arguments are:
repPositive integer number of restarts; default 1L.
miterPositive integer number of iterations at a fixed temperature; default
2 * nrow(A).
fiterFactor of at least 1 by which miter grows
after each cooling step; default 1.1.
ftFactor by which the temperature decreases after each
cooling step; default 0.5 (strictly between 0 and 1).
maxstepsPositive integer maximum number of cooling steps; default
50L.
verbosePrint progress; default FALSE.
qap() returns an integer vector of facility to location
assignments with the objective value in its "obj" attribute.
qap.obj() returns the objective value for permutation o.
Burkard, R. E. and Rendl, F. (1984). A thermodynamically motivated simulation procedure for combinatorial optimization problems. European Journal of Operational Research, 17(2), 169-174. doi:10.1016/0377-2217(84)90231-5
Koopmans, T. C. and Beckmann, M. (1957). Assignment problems and the location of economic activities. Econometrica, 25(1), 53-76. doi:10.2307/1907742
Hubert, L. and Schultz, J. (1976). Quadratic assignment as a general data analysis strategy. British Journal of Mathematical and Statistical Psychology, 29(2), 190-241. doi:10.1111/j.2044-8317.1976.tb00714.x
p <- read_qaplib(system.file("qaplib", "had12.dat", package = "qap")) a <- qap(p$A, p$B, verbose = TRUE) a qap.obj(p$A, p$B, a) (attr(a, "obj") - p$opt) / p$opt * 100p <- read_qaplib(system.file("qaplib", "had12.dat", package = "qap")) a <- qap(p$A, p$B, verbose = TRUE) a qap.obj(p$A, p$B, a) (attr(a, "obj") - p$opt) / p$opt * 100
Read a problem instance and, when available, its solution from QAPLIB files.
read_qaplib(file)read_qaplib(file)
file |
Path to a QAPLIB problem file with a |
If a .sln file with the same base name exists in the same
directory, the function also reads its solution and objective value.
Zero-based solutions are converted to R's one-based indexing.
The package includes QAPLIB instances and solutions in its qaplib
directory.
A list with A (the flow matrix), B (the distance matrix),
solution (a known solution, if available), and opt (its objective
value, if available). The last two components are NULL when no solution
file exists.
Burkard, R. E., Çela, E., Karisch, S. E., and Rendl, F. QAPLIB: A Quadratic Assignment Problem Library.
p <- read_qaplib(system.file("qaplib", "had12.dat", package = "qap")) p dir(system.file("qaplib", package = "qap"), pattern = "\\.dat$")p <- read_qaplib(system.file("qaplib", "had12.dat", package = "qap")) p dir(system.file("qaplib", package = "qap"), pattern = "\\.dat$")