A Python project for optimizing the 8 Queens Puzzle using the Genetic Algorithm implemented in PyGAD.
The project uses the Kivy cross-platform Python framework for building the GUI of the 8 queens puzzle. The GUI helps to visualize the solutions reached while the genetic algorithm (GA) is optimizing the problem to find the best solution.
For implementing the genetic algorithm, the PyGAD library is used. Check its documentation here: https://pygad.readthedocs.io
For Windows, issue the following command:
pip install pygad
For Linux and Mac, replace
pip by use
pip3 because the library only supports Python 3.
pip3 install pygad
PyGAD is developed in Python 3.7.3 and depends on NumPy for creating and manipulating arrays and Matplotlib for creating figures. The exact NumPy version used in developing PyGAD is 1.16.4. For Matplotlib, the version is 3.1.0.
The project comes with a GUI built in Kivy, a cross-platform Python framework for building natural user interfaces. Before using the project, install Kivy:
pip install kivy
Because the project is built using Python 3, use
pip3 instead of
pip for Mac/Linux:
pip3 install kivy
The main file for this project is called
main.py which holds the code for building the GUI and instantiating PyGAD for running the genetic algorithm.
After running the
main.py file successfully, the window will appear as given in the figure below. The GUI uses a GridLayout for creating an 8x8 grid. This grid represents the board of the 8 queen puzzle.
The objective of the GA is to find the best locations for the 8 queens so that no queen is attacking another horizontally, vertically, or diagonally. This project assumes that no 2 queens are in the same row. As a result, we are sure that no 2 queens will attack each other horizontally. This leaves us to the 2 other types of attacks (vertically and diagonally).
The bottom part of the window has 3 Button widgets and 1 Label widget. From left to right, the description of the 3 Button widgets is as follows:
- The Initial Population button creates the initial population of the GA.
- The Show Best Solution button shows the best solution in the last generation the GA stopped at.
- The Start GA button starts the GA iterations/generations.
The Label widget just prints some informational messages to the user. For example, it prints the fitness value of the best solution when the user presses the Show Best Solution button.
Steps to Use the Project
Follow these steps to use the project:
- Run the main.py file.
- Press the Initial Population Button.
- Press the Start GA Button.
After pressing the Start GA button, the GA uses the initial population and evolves its solutions until reaching the best possible solution.
Behind the scenes, some important stuff was built that includes building the Kivy GUI, instantiating PyGAD, preparing the the fitness function, preparing the callback function, and more. For more information, please check the tutorial titled 8 Queen Puzzle Optimization Using a Genetic Algorithm in Python.
After running the
main.py file and pressing the Initial Population button, the next figure shows one possible initial population in which 6 out of 8 queens are attacking each other.
In the Label, the fitness value is calculated as
1.0/number of attacks. In this case, the fitness value is equal to 1.0/6.0 which is 0.1667.
The next figures shows how the GA evolves the solutions until reaching the best solution in which 0 attacks exists.
0 Attacks (Optimal Solution)