The Genetic Algorithm in Solving the Quadratic Assignment Problem

The Quadratic Assignment Problem is one of the fundamental problems from the group of combinatorial optimization problems. It is an NP-hard problem, i.e. one for which finding an optimal solution is impossible in polynomial time. This problem appears in many practical issues in economics, ergonomics, electronics, architecture, and IT. First introduced by Koopmans and Beckmann in 1957.

The problem models the following real-life problem:

There are n facilities and the same amount of locations. The distances between the cities and the flows between the facilities are known. The problem is to assign all facilities to different locations so that the assignment cost is the lowest.

Consider a task with five factories and five cities. The distances between cities and flows between factories are shown in the graphs below.

Distances between cities

We assume that the route between the cities in both directions is the same.

Flows between factories

The line between a pair of facilities indicates that flow is required between them. The thickness of the line corresponds to the value of this flow. The flow can be considered, for example, as the number of supplies transported between two facilities.

Proposal for solution

Consider a situation in which factories are already located in cities. What is the cost of the assignment?

The total cost of assigning factories to cities is the sum of the products of the flow function values between the facilities and their distances:

The cost of factories located in such a way is 4195.

Intuition tells us to place factories where the flow is high in cities closer to each other. Are these the most optimal locations for our factories in the proposed solution? To check this, you need to find out if there is another better solution than the proposed one.

QAP formal definition

The formal definition of QAP is as follows:

There are two sets


Then the quadratic assignment problem is to:

The above formula describes the cost function for the QAP problem.

To solve the task, we will use a genetic algorithm that works well for solving these types of problems. Genetic algorithms search the space of alternative solutions to find the best solution. Knowing what the cost function is, one knows what to consider when determining individuals’ quality in a population.

Basic concepts of the genetic algorithm

Population — a set of individuals (chromosomes) from one environment. It is a space of alternative solutions, randomly generated at the beginning.

Chromosome — an individual of a population. It is an ordered sequence of genes. The chromosome can be considered as one of the possible solutions.

Gene — the smallest part of a chromosome that makes one or more traits inheritable.

Selection — a selection of individuals best suited to the reproductive process.

Crossover — a random intersection of two chromosomes at one random point and the swapping of divided parts (genes).

Mutation — a sudden, random change in the order of genes on a chromosome.

Genetic Algorithm

Step 1. Generate an initial random population of individuals.

First, one needs to generate a random population. By defining an individual’s size and the population’s size, generate an initial space with solutions.

Step 2. Assess the fitness of all individuals in the population.

To determine which individual in the population is better, you need to calculate its fitness value. A better-adapted individual in the population is one that represents a lower assignment cost. The quotient of the entire population’s cost and the individual’s cost is the fitness function’s value.

First, you need to calculate the cost of each individual in the population.

Then, knowing each individual’s cost, you calculate each individual’s fitness score by dividing the entire population’s cost by its cost.

Step 3. Use the selection method to select the best-suited individuals for the reproductive process.

Two selection methods for selecting the best chromosomes are outlined below. In each of the methods, weaker, less adapted individuals “die”, and the stronger, with a higher value of the fitness function, have a reproduction chance.

Roulette wheel selection

Imagine a roulette wheel whose disc is divided into several slices of different sizes. Each individual receives an area of ​​size directly proportional to its fitness function value. The roulette wheel is moving. When it stops, the indicator shows the selected individual who has a chance of reproducing. The larger the area of ​​the cutout an individual received, the greater the probability of choosing it. The probability function defines the area’s size, the value of which is within the range [0, 1].

If by f(i) denote the value of the fitness function of the i-th individual, then you can calculate the probability of selecting it from the formula:

Tournament selection

It consists of drawing an n-element group from the population (usually 2 or 3 individuals) and then selecting an individual with the best adaptation. We repeat the entire operation as many times as there are individuals in the population. Tournament selection is free from the roulette wheel method’s inconvenience, where the maximization of the fitness function is required. The only thing that matters is information about one solution’s “superiority” over another in a tournament.

Step 4. Perform evolutionary operations on selected individuals.


From the population, select a random even number of candidates for reproduction. Pair them, and cross. The crossing consists of cutting two individuals at a random point and exchanging the divided parts (genes). In this way, new individuals are born.

Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

The new offspring are added to the population.


A mutation, as in the case of DNA, is the random change of a gene. The results of such an operation are difficult to predict. Still, it can lead to creating a chromosome that will prove to be an excellent solution, which does not necessarily come from a combination of individuals from the previous generation.

Step 5. Choose the best individual from the population.

The best chromosome in the resulting generation is the result. If the result is not satisfactory, go to step 2, otherwise exit.

Final result

Source code with examples:

At the link below, you will find the code and numerous examples with which you can experiment by selecting the mutation and crossover probability parameters and adjusting the population size to the task size.


While writing this article, I focused on presenting one of the approaches to solving this type of problem and showing from the practical side how the genetic algorithm works.


Sandstream Development

Tech lessons learned while making our products

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store