{"id":15015506,"url":"https://github.com/ahmedfgad/8queensgenetic","last_synced_at":"2025-04-12T09:19:04.554Z","repository":{"id":44380110,"uuid":"188434587","full_name":"ahmedfgad/8QueensGenetic","owner":"ahmedfgad","description":"Optimizing the 8 Queens Puzzle using the Genetic Algorithm (PyGAD).","archived":false,"fork":false,"pushed_at":"2023-04-08T19:41:37.000Z","size":37,"stargazers_count":21,"open_issues_count":0,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-12T09:18:54.440Z","etag":null,"topics":["8-queens-problem","cross-platform","genetic-algorithm","kivy","numpy","optimization","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahmedfgad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":null,"open_collective":"pygad","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://donate.stripe.com/eVa5kO866elKgM0144","http://paypal.me/ahmedfgad"]}},"created_at":"2019-05-24T14:19:40.000Z","updated_at":"2025-04-10T09:38:52.000Z","dependencies_parsed_at":"2022-07-14T12:17:30.810Z","dependency_job_id":null,"html_url":"https://github.com/ahmedfgad/8QueensGenetic","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"a874e76dbdbe8a9e651e801b026b947cdae7d2a3"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedfgad%2F8QueensGenetic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedfgad%2F8QueensGenetic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedfgad%2F8QueensGenetic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedfgad%2F8QueensGenetic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahmedfgad","download_url":"https://codeload.github.com/ahmedfgad/8QueensGenetic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248543836,"owners_count":21121838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["8-queens-problem","cross-platform","genetic-algorithm","kivy","numpy","optimization","python"],"created_at":"2024-09-24T19:47:33.590Z","updated_at":"2025-04-12T09:19:04.524Z","avatar_url":"https://github.com/ahmedfgad.png","language":"Python","funding_links":["https://opencollective.com/pygad","https://donate.stripe.com/eVa5kO866elKgM0144","http://paypal.me/ahmedfgad"],"categories":[],"sub_categories":[],"readme":"# 8QueensGenetic\nA Python project for optimizing the 8 Queens Puzzle using the Genetic Algorithm implemented in PyGAD.\n\nThe project uses the [Kivy cross-platform Python framework](https://github.com/kivy/kivy) 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.\n\nFor implementing the genetic algorithm, the `PyGAD` library is used. Check its documentation here: https://pygad.readthedocs.io\n\n**IMPORTANT** *If you are coming for the code of the tutorial [8 Queen Puzzle Optimization Using a Genetic Algorithm in Python](https://heartbeat.fritz.ai/8-queen-puzzle-optimization-using-a-genetic-algorithm-in-python-1d9ca769ede8), then it has been moved to the [TutorialProject](https://github.com/ahmedfgad/8QueensGenetic/tree/master/TutorialProject) directory on 17 June 2020.*\n\n# PyGAD Installation\n\nTo install [PyGAD](https://pypi.org/project/pygad), simply use pip to download and install the library from [PyPI](https://pypi.org/project/pygad) (Python Package Index). The library lives a PyPI at this page https://pypi.org/project/pygad.\n\nFor Windows, issue the following command:\n\n```python\npip install pygad\n```\n\nFor Linux and Mac, replace `pip` by use `pip3` because the library only supports Python 3.\n\n```python\npip3 install pygad\n```\n\nPyGAD 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.\n\n## Project GUI\n\nThe project comes with a GUI built in Kivy, a cross-platform Python framework for building natural user interfaces. Before using the project, install Kivy:\n\n```python\npip install kivy\n```\n\nBecause the project is built using Python 3, use `pip3` instead of `pip` for Mac/Linux:\n\n```python\npip3 install kivy\n```\n\nCheck this [Stackoverflow answer](https://stackoverflow.com/a/44220712) to install other libraries that are essential to run Kivy: https://stackoverflow.com/a/44220712\n\nThe 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. \n\nAfter 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.\n\n![main](https://user-images.githubusercontent.com/16560492/58335124-2f1e4e00-7e41-11e9-9328-fc3b5cd95f41.jpg)\n\nThe 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).\n\nThe 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:\n\n* The **Initial Population** button creates the initial population of the GA.\n* The **Show Best Solution** button shows the best solution in the last generation the GA stopped at.\n* The **Start GA** button starts the GA iterations/generations.\n\nThe 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.\n\n## Steps to Use the Project\n\nFollow these steps to use the project:\n\n1. Run the **main.py** file.\n2. Press the **Initial Population** Button.\n3. Press the **Start GA** Button.\n\nAfter pressing the **Start GA** button, the GA uses the initial population and evolves its solutions until reaching the best possible solution. \n\nBehind 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](https://heartbeat.fritz.ai/8-queen-puzzle-optimization-using-a-genetic-algorithm-in-python-1d9ca769ede8).\n\n## 6 Attacks\n\nAfter 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. \n\n![1  6 attacks](https://user-images.githubusercontent.com/16560492/58335727-840e9400-7e42-11e9-830d-6d6b9bdad67a.jpg)\n\nIn 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.\n\nThe next figures shows how the GA evolves the solutions until reaching the best solution in which 0 attacks exists.\n\n## 5 Attacks\n![2  5 attacks](https://user-images.githubusercontent.com/16560492/58336029-321a3e00-7e43-11e9-860e-99aaf16a1d67.jpg)\n\n## 4 Attacks\n![3  4 attacks](https://user-images.githubusercontent.com/16560492/58336030-321a3e00-7e43-11e9-972d-1e948fbd62ee.jpg)\n\n## 3 Attacks\n![4  3 attacks](https://user-images.githubusercontent.com/16560492/58336031-321a3e00-7e43-11e9-9b6b-83b9de252186.jpg)\n\n## 2 Attacks\n![5  2 attacks](https://user-images.githubusercontent.com/16560492/58336032-32b2d480-7e43-11e9-87d1-48dfdd305cc6.jpg)\n\n## 1 Attack\n![6  1 attack](https://user-images.githubusercontent.com/16560492/58336033-32b2d480-7e43-11e9-9626-080f5e922825.jpg)\n\n## 0 Attacks (Optimal Solution)\n![7  0 attack](https://user-images.githubusercontent.com/16560492/58336034-32b2d480-7e43-11e9-801d-38bf028c7359.jpg)\n\n## IMPORTANT\nIt 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.\n\n# For More Information\n\nThere are different resources that can be used to get started with the building CNN and its Python implementation. \n\n## Tutorial: 8 Queen Puzzle Optimization Using a Genetic Algorithm in Python\n\nIn 1 May 2019, I wrote a tutorial discussing this project. The tutorial is titled [**8 Queen Puzzle Optimization Using a Genetic Algorithm in Python**](https://heartbeat.fritz.ai/8-queen-puzzle-optimization-using-a-genetic-algorithm-in-python-1d9ca769ede8) which is published at Heartbeat. Check it at these links:\n\n- [Heartbeat](https://heartbeat.fritz.ai/8-queen-puzzle-optimization-using-a-genetic-algorithm-in-python-1d9ca769ede8): https://heartbeat.fritz.ai/8-queen-puzzle-optimization-using-a-genetic-algorithm-in-python-1d9ca769ede8\n\n[![Tutorial Cover Image](https://miro.medium.com/max/3240/1*4tHGUbApzoB5rKHIJi9zmg.jpeg)](https://heartbeat.fritz.ai/8-queen-puzzle-optimization-using-a-genetic-algorithm-in-python-1d9ca769ede8)\n\n## Book: Practical Computer Vision Applications Using Deep Learning with CNNs\n\nYou 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**](https://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665) which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.\n\nFind the book at these links:\n\n- [Amazon](https://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665)\n- [Springer](https://link.springer.com/book/10.1007/978-1-4842-4167-7)\n- [Apress](https://www.apress.com/gp/book/9781484241660)\n- [O'Reilly](https://www.oreilly.com/library/view/practical-computer-vision/9781484241677)\n- [Google Books](https://books.google.com.eg/books?id=xLd9DwAAQBAJ)\n\n![Fig04](https://user-images.githubusercontent.com/16560492/78830077-ae7c2800-79e7-11ea-980b-53b6bd879eeb.jpg)\n\n# Citing PyGAD - Bibtex Formatted Citation\n\nIf you used PyGAD, please consider adding a citation to the following paper about PyGAD:\n\n```\n@misc{gad2021pygad,\n      title={PyGAD: An Intuitive Genetic Algorithm Python Library}, \n      author={Ahmed Fawzy Gad},\n      year={2021},\n      eprint={2106.06158},\n      archivePrefix={arXiv},\n      primaryClass={cs.NE}\n}\n```\n\n# Contact Us\n\n* E-mail: ahmed.f.gad@gmail.com\n* [LinkedIn](https://www.linkedin.com/in/ahmedfgad)\n* [Amazon Author Page](https://amazon.com/author/ahmedgad)\n* [Heartbeat](https://heartbeat.fritz.ai/@ahmedfgad)\n* [Paperspace](https://blog.paperspace.com/author/ahmed)\n* [KDnuggets](https://kdnuggets.com/author/ahmed-gad)\n* [TowardsDataScience](https://towardsdatascience.com/@ahmedfgad)\n* [GitHub](https://github.com/ahmedfgad)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedfgad%2F8queensgenetic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahmedfgad%2F8queensgenetic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedfgad%2F8queensgenetic/lists"}