A Python project for optimizing the 8 Queens Puzzle using the Genetic Algorithm implemented in PyGAD.

Overview

8QueensGenetic

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

IMPORTANT If you are coming for the code of the tutorial 8 Queen Puzzle Optimization Using a Genetic Algorithm in Python, then it has been moved to the TutorialProject directory on 17 June 2020.

PyGAD Installation

To install PyGAD, simply use pip to download and install the library from PyPI (Python Package Index). The library lives a PyPI at this page https://pypi.org/project/pygad.

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.

Project GUI

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

Check this Stackoverflow answer to install other libraries that are essential to run Kivy: https://stackoverflow.com/a/44220712

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.

main

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:

  1. Run the main.py file.
  2. Press the Initial Population Button.
  3. 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.

6 Attacks

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.

1  6 attacks

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.

5 Attacks

2  5 attacks

4 Attacks

3  4 attacks

3 Attacks

4  3 attacks

2 Attacks

5  2 attacks

1 Attack

6  1 attack

0 Attacks (Optimal Solution)

7  0 attack

IMPORTANT

It is very important to note that the GA does not guarantee reaching the optimal solution each time it works. You can make changes in the number of solutions per population, the number of generations, or the number of mutations. Other than doing that, the initial population might also be another factor for not reaching the optimal solution for a given trial.

For More Information

There are different resources that can be used to get started with the building CNN and its Python implementation.

Tutorial: 8 Queen Puzzle Optimization Using a Genetic Algorithm in Python

In 1 May 2019, I wrote a tutorial discussing this project. The tutorial is titled 8 Queen Puzzle Optimization Using a Genetic Algorithm in Python which is published at Heartbeat. Check it at these links:

Tutorial Cover Image

Book: Practical Computer Vision Applications Using Deep Learning with CNNs

You can also check my book cited as Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7 which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.

Find the book at these links:

Fig04

Citing PyGAD - Bibtex Formatted Citation

If you used PyGAD, please consider adding a citation to the following paper about PyGAD:

@misc{gad2021pygad,
      title={PyGAD: An Intuitive Genetic Algorithm Python Library}, 
      author={Ahmed Fawzy Gad},
      year={2021},
      eprint={2106.06158},
      archivePrefix={arXiv},
      primaryClass={cs.NE}
}

Contact Us

Owner
Ahmed Gad
Ph.D. Student at uOttawa // Machine Learning Researcher & Technical Author https://amazon.com/author/ahmedgad
Ahmed Gad
Implementation of core NuPIC algorithms in C++

NuPIC Core This repository contains the C++ source code for the Numenta Platform for Intelligent Computing (NuPIC)

Numenta 270 Nov 19, 2022
Algorithms for calibrating power grid distribution system models

Distribution System Model Calibration Algorithms The code in this library was developed by Sandia National Laboratories under funding provided by the

Sandia National Laboratories 2 Oct 31, 2022
Planning Algorithms in AI and Robotics. MSc course at Skoltech Data Science program

Planning Algorithms in AI and Robotics course T2 2021-22 The Planning Algorithms in AI and Robotics course at Skoltech, MS in Data Science, during T2,

Mobile Robotics Lab. at Skoltech 6 Sep 21, 2022
Implemented page rank program

Page Rank Implemented page rank program based on fact that a website is more important if it is linked to by other important websites using recursive

Vaibhaw 6 Aug 24, 2022
Python implementation of Aho-Corasick algorithm for string searching

Python implementation of Aho-Corasick algorithm for string searching

Daniel O'Sullivan 1 Dec 31, 2021
Provide player's names and mmr and generate mathematically balanced teams

Lollo's matchmaking algorithm Provide player's names and mmr and generate mathematically balanced teams How to use Fill the input.json file with your

4 Aug 04, 2022
Python sample codes for robotics algorithms.

PythonRobotics Python codes for robotics algorithm. Table of Contents What is this? Requirements Documentation How to use Localization Extended Kalman

Atsushi Sakai 17.2k Jan 01, 2023
RRT algorithm and its optimization

RRT-Algorithm-Visualisation This is a project that aims to develop upon the RRT

Sarannya Bhattacharya 7 Mar 06, 2022
This project is an implementation of a simple K-means algorithm

Simple-Kmeans-Clustering-Algorithm Abstract K-means is a centroid-based algorithm, or a distance-based algorithm, where we calculate the distances to

Saman Khamesian 7 Aug 09, 2022
Visualisation for sorting algorithms. Version 2.0

Visualisation for sorting algorithms v2. Upped a notch from version 1. This program provides animates simple, common and popular sorting algorithms, t

Ben Woo 7 Nov 08, 2022
implementation of the KNN algorithm on crab biometrics dataset for CS16

crab-knn implementation of the KNN algorithm in Python applied to biometrics data of purple rock crabs (leptograpsus variegatus) to classify the sex o

Andrew W. Chen 1 Nov 18, 2021
ROS Basics and TurtleSim

Homework 1: Turtle Control Package Anna Garverick This package draws given waypoints, then waits for a service call with a start position to send the

Anna Garverick 1 Nov 22, 2021
Machine Learning algorithms implementation.

Machine Learning Algorithms Machine Learning algorithms implementation. What can I find here? ML Algorithms KNN K-Means-Clustering SVM (MultiClass) Pe

David Levin 1 Dec 10, 2021
Repository for data structure and algorithms in Python for coding interviews

Python Data Structures and Algorithms This repository contains questions requiring implementation of data structures and algorithms concepts. It is us

Prabhu Pant 1.9k Jan 01, 2023
Python Client for Algorithmia Algorithms and Data API

Algorithmia Common Library (python) Python client library for accessing the Algorithmia API For API documentation, see the PythonDocs Algorithm Develo

Algorithmia 138 Oct 26, 2022
Evol is clear dsl for composable evolutionary algorithms that optimised for joy.

Evol is clear dsl for composable evolutionary algorithms that optimised for joy. Installation We currently support python3.6 and python3.7 and you can

GoDataDriven 178 Dec 27, 2022
Python Sorted Container Types: Sorted List, Sorted Dict, and Sorted Set

Python Sorted Containers Sorted Containers is an Apache2 licensed sorted collections library, written in pure-Python, and fast as C-extensions. Python

Grant Jenks 2.8k Jan 04, 2023
A collection of Python Scripts made for fun, while exploring Python 🐍

JFF-Python-Scripts A collection of Python Scripts made for fun, while exploring Python 🐍 Inspiration 💡 Many of the programs in this repository are i

Pushkar Patel 16 Oct 07, 2022
Distributed algorithms, reimplemented for fun and practice

Distributed Algorithms Playground for reimplementing and experimenting with algorithms for distributed computing. Usage Running the code for Ring-AllR

Mahan Tourkaman 1 Oct 16, 2022
A Python implementation of Jerome Friedman's Multivariate Adaptive Regression Splines

py-earth A Python implementation of Jerome Friedman's Multivariate Adaptive Regression Splines algorithm, in the style of scikit-learn. The py-earth p

431 Dec 15, 2022