Solving the 8 Queen Problem using Genetic Algorithm in Python

Programming a Genetic Algorithm to Solve the 8 Queen Problem in Python

A genetic algorithm is an informed search heuristic that uses mutation, crossover and selection to evolve a population of potential solutions (board states). It then evaluates each state to find one in which no queen can capture any other.

This approach has a time complexity of O(N!). It also has a space complexity of O(N) if we use a 1D array to represent the board.


The problem is to arrange queens on a board so that they can all be placed without attacking each other. The total number of arrangements is 88 248 = 16777216 ways. This is not a small number, and finding a solution using a brute force algorithm would take a very long time.

There are many different approaches to solving this problem. One way is to use a genetic algorithm, which is an informed search heuristic that is based on biological evolution. This heuristic uses the principles of natural selection, mutation, and crossover to iteratively enhance a population of potential solutions.

The genetic algorithm starts with a random population of potential solutions (board states). Each solution is represented by an array containing the queen’s positions, where the index indicates the column and the value represents the row. The best solution is then selected based on its fitness value, which is calculated as a ratio of the number of queens that are not in mutual attack.


The first step in solving the 8 queen problem is to define the variables. There are a number of different variables that can be used, such as row, column, and color. Each variable has a value, which determines whether the queen can be placed in that location.

The variables can be manipulated to create different solutions for the problem. For example, the color of a queen can be changed to make it easier to identify. Changing the row or column can also change the solution.

The program then creates a class called QueenSprite, which contains the properties of the queen chess. The QueenSprite has an image property, a posn property, and a dragging property. The dragging property determines whether the QueenSprite can be dragged with the mouse. If the dragging property is set to False, the program will reset the QueenSprite object at its current position. The posn property will then be set to the current cell position.


From line 95 the code is entered into an event loop to catch keyboard and mouse events. When the mouse button is pressed it checks whether the Queen chess placed in current cell conflicts with any existing ones (horizontal, vertical or diagonal).

If a queen can’t be placed at a given row index, then the algorithm backtracks and changes the position of the previous queen to match the new one. This process is repeated for every other queen in the row until a suitable place can be found.

The next step involves using Genetic Algorithms to optimize the population of solutions to find a good solution to the problem. The UI has three TextInput widgets for accepting values that govern the GA operation. The first accepts the initial population size, and the second and third one accept values for defining the crossover and mutation operations respectively. The Show Best Solution and Start GA buttons initiate the GA iterations/generations.


The following application adds a few widgets for user control. The first two TextInput widgets accept values for the GA. The left one accepts the number of solutions within the population, while the right one accepts the number of generations to run it for.

The solution to this problem uses a backtracking depth-first search algorithm. It examines the possible positions of a queen in a row, and rejects those that lead to diagonal attacking positions. It does this for each of the 8 rows in the board. If it cannot find a solution for a given column, it backs up to a previous good state and tries again.

This program is an example of a genetic algorithm, an informed search heuristic greatly inspired by natural selection and biological evolution. It iteratively enhances a randomized population of potential solutions (board states) with the genetic operations mutation and crossover, until a satisfactory solution is found. The Label widget prints information about this solution, including its fitness value and number of attacks (6).

Zoom back to the home screen