From 67bc9724b6be6849a0fbc66de37da5623147d2ad Mon Sep 17 00:00:00 2001 From: Ahmed Gad Date: Fri, 12 Mar 2021 19:01:13 -0500 Subject: [PATCH] Add files via upload --- docs/source/Footer.rst | 275 +++++++++++++++++++---- docs/source/README_pygad_ReadTheDocs.rst | 170 ++++++++------ 2 files changed, 331 insertions(+), 114 deletions(-) diff --git a/docs/source/Footer.rst b/docs/source/Footer.rst index d7535de..903ce29 100644 --- a/docs/source/Footer.rst +++ b/docs/source/Footer.rst @@ -770,6 +770,16 @@ Stackoverflow Questions about PyGAD .. _header-n277: +`How to solve TSP problem using pyGAD package? `__ +--------------------------------------------------------------------------------------------------------------------------------------------- + +.. _header-n278: + +`Multi-Input Multi-Output in Genetic algorithm (python) `__ +-------------------------------------------------------------------------------------------------------------------------------------------------------------- + +.. _header-n279: + Submitting Issues ================= @@ -779,14 +789,14 @@ to discuss that issue. One way is `submitting an issue `__ into this GitHub project -(https://github.com/ahmedfgad/GeneticAlgorithmPython) in case something -is not working properly or to ask for questions. +(`github.com/ahmedfgad/GeneticAlgorithmPython `__) +in case something is not working properly or to ask for questions. If this is not a proper option for you, then check the `Contact Us `__ section for more contact details. -.. _header-n281: +.. _header-n283: Ask for Feature =============== @@ -798,12 +808,14 @@ algorithm. To ask for a new feature, either `submit an issue `__ into this GitHub project -(https://github.com/ahmedfgad/GeneticAlgorithmPython) or send an e-mail -to ahmed.f.gad@gmail.com. +(`github.com/ahmedfgad/GeneticAlgorithmPython `__) +or send an e-mail to ahmed.f.gad@gmail.com. -Also check the **Contact Us** section for more contact details. +Also check the `Contact +Us `__ +section for more contact details. -.. _header-n285: +.. _header-n287: Projects Built using PyGAD ========================== @@ -812,7 +824,9 @@ If you created a project that uses PyGAD, then we can support you by mentioning this project here in PyGAD's documentation. To do that, please send a message at ahmed.f.gad@gmail.com or check the -**Contact Us** section for more contact details. +`Contact +Us `__ +section for more contact details. Within your message, please send the following details: @@ -822,22 +836,174 @@ Within your message, please send the following details: - Preferably, a link that directs the readers to your project -.. _header-n296: +.. _header-n298: + +Tutorials about PyGAD +===================== + +.. _header-n299: + +`Working with Different Genetic Algorithm Representations in Python `__ +-------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +Depending on the nature of the problem being optimized, the genetic +algorithm (GA) supports two different gene representations: binary, and +decimal. The binary GA has only two values for its genes, which are 0 +and 1. This is easier to manage as its gene values are limited compared +to the decimal GA, for which we can use different formats like float or +integer, and limited or unlimited ranges. + +This tutorial discusses how the +`PyGAD `__ library supports the two GA +representations, binary and decimal. + +.. _header-n302: + +`5 Genetic Algorithm Applications Using PyGAD `__ +------------------------------------------------------------------------------------------------------------------------- + +This tutorial introduces PyGAD, an open-source Python library for +implementing the genetic algorithm and training machine learning +algorithms. PyGAD supports 19 parameters for customizing the genetic +algorithm for various applications. + +Within this tutorial we'll discuss 5 different applications of the +genetic algorithm and build them using PyGAD. + +.. _header-n305: + +`Train Neural Networks Using a Genetic Algorithm in Python with PyGAD `__ +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +The genetic algorithm (GA) is a biologically-inspired optimization +algorithm. It has in recent years gained importance, as it’s simple +while also solving complex problems like travel route optimization, +training machine learning algorithms, working with single and +multi-objective problems, game playing, and more. + +Deep neural networks are inspired by the idea of how the biological +brain works. It’s a universal function approximator, which is capable of +simulating any function, and is now used to solve the most complex +problems in machine learning. What’s more, they’re able to work with all +types of data (images, audio, video, and text). + +Both genetic algorithms (GAs) and neural networks (NNs) are similar, as +both are biologically-inspired techniques. This similarity motivates us +to create a hybrid of both to see whether a GA can train NNs with high +accuracy. + +This tutorial uses `PyGAD `__, a Python +library that supports building and training NNs using a GA. +`PyGAD `__ offers both classification and +regression NNs. + +.. _header-n310: + +`How To Train Keras Models Using the Genetic Algorithm with PyGAD `__ +-------------------------------------------------------------------------------------------------------------------------------------------------------- + +PyGAD is an open-source Python library for building the genetic +algorithm and training machine learning algorithms. It offers a wide +range of parameters to customize the genetic algorithm to work with +different types of problems. + +PyGAD has its own modules that support building and training neural +networks (NNs) and convolutional neural networks (CNNs). Despite these +modules working well, they are implemented in Python without any +additional optimization measures. This leads to comparatively high +computational times for even simple problems. + +The latest PyGAD version, 2.8.0 (released on 20 September 2020), +supports a new module to train Keras models. Even though Keras is built +in Python, it's fast. The reason is that Keras uses TensorFlow as a +backend, and TensorFlow is highly optimized. + +This tutorial discusses how to train Keras models using PyGAD. The +discussion includes building Keras models using either the Sequential +Model or the Functional API, building an initial population of Keras +model parameters, creating an appropriate fitness function, and more. + +|image1| + +.. _header-n316: + +`Train PyTorch Models Using Genetic Algorithm with PyGAD `__ +--------------------------------------------------------------------------------------------------------------------------------------------- + +`PyGAD `__ is a genetic algorithm Python +3 library for solving optimization problems. One of these problems is +training machine learning algorithms. + +PyGAD has a module called +`pygad.kerasga `__. It trains +Keras models using the genetic algorithm. On January 3rd, 2021, a new +release of `PyGAD 2.10.0 `__ brought a +new module called +`pygad.torchga `__ to train +PyTorch models. It’s very easy to use, but there are a few tricky steps. + +So, in this tutorial, we’ll explore how to use PyGAD to train PyTorch +models. + +|image2| + +.. _header-n321: PyGAD in Other Languages ======================== -.. _header-n297: +.. _header-n322: + +French +------ + +`Cómo los algoritmos genéticos pueden competir con el descenso de +gradiente y el +backprop `__ + +Bien que la manière standard d'entraîner les réseaux de neurones soit la +descente de gradient et la rétropropagation, il y a d'autres joueurs +dans le jeu. L'un d'eux est les algorithmes évolutionnaires, tels que +les algorithmes génétiques. + +Utiliser un algorithme génétique pour former un réseau de neurones +simple pour résoudre le OpenAI CartPole Jeu. Dans cet article, nous +allons former un simple réseau de neurones pour résoudre le OpenAI +CartPole . J'utiliserai PyTorch et PyGAD . + +|image3| + +.. _header-n327: + +Spanish +------- + +`Cómo los algoritmos genéticos pueden competir con el descenso de +gradiente y el +backprop `__ + +Aunque la forma estandar de entrenar redes neuronales es el descenso de +gradiente y la retropropagacion, hay otros jugadores en el juego, uno de +ellos son los algoritmos evolutivos, como los algoritmos geneticos. + +Usa un algoritmo genetico para entrenar una red neuronal simple para +resolver el Juego OpenAI CartPole. En este articulo, entrenaremos una +red neuronal simple para resolver el OpenAI CartPole . Usare PyTorch y +PyGAD . + +|image4| + +.. _header-n332: Korean ------ -.. _header-n298: +.. _header-n333: `[PyGAD] Python 에서 Genetic Algorithm 을 사용해보기 `__ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -|image1| +|image5| 파이썬에서 genetic algorithm을 사용하는 패키지들을 다 사용해보진 않았지만, 확장성이 있어보이고, 시도할 일이 있어서 살펴봤다. @@ -853,12 +1019,12 @@ Korean 사실 완전히 흐름이나 각 parameter에 대한 이해는 부족한 상황 -.. _header-n305: +.. _header-n340: Turkish ------- -.. _header-n306: +.. _header-n341: `PyGAD ile Genetik Algoritmayı Kullanarak Keras Modelleri Nasıl Eğitilir `__ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -867,8 +1033,6 @@ This is a translation of an original English tutorial published at Paperspace: `How To Train Keras Models Using the Genetic Algorithm with PyGAD `__ -|image2| - PyGAD, genetik algoritma oluşturmak ve makine öğrenimi algoritmalarını eğitmek için kullanılan açık kaynaklı bir Python kitaplığıdır. Genetik algoritmayı farklı problem türleri ile çalışacak şekilde özelleştirmek @@ -892,18 +1056,18 @@ Keras modellerini oluşturmayı, Keras model parametrelerinin ilk popülasyonunu oluşturmayı, uygun bir uygunluk işlevi oluşturmayı ve daha fazlasını içerir. -.. _header-n313: +|image6| + +.. _header-n348: Hungarian --------- -.. _header-n314: +.. _header-n349: `Tensorflow alapozó 10. Neurális hálózatok tenyésztése genetikus algoritmussal PyGAD és OpenAI Gym használatával `__ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -|image3| - Hogy kontextusba helyezzem a genetikus algoritmusokat, ismételjük kicsit át, hogy hogyan működik a gradient descent és a backpropagation, ami a neurális hálók tanításának általános módszere. Az erről írt cikkemet itt @@ -926,18 +1090,18 @@ hálózatunk 386 állítható paraméterrel rendelkezik, ezért a DNS-ünk itt populációnk egy 10x386 elemű mátrix lesz. Ezt adjuk át az 51. sorban az initial_population paraméterben. -.. _header-n319: +|image7| + +.. _header-n354: Russian ------- -.. _header-n320: +.. _header-n355: `PyGAD: библиотека для имплементации генетического алгоритма `__ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -|image4| - PyGAD — это библиотека для имплементации генетического алгоритма. Кроме того, библиотека предоставляет доступ к оптимизированным реализациям алгоритмов машинного обучения. PyGAD разрабатывали на Python 3. @@ -959,7 +1123,9 @@ PyGAD разрабатывали на Python 3.7.3. Зависимости вк из изкейсов использования инструмента — оптимизация весов, которые удовлетворяют заданной функции. -.. _header-n327: +|image8| + +.. _header-n362: For More Information ==================== @@ -967,7 +1133,7 @@ For More Information There are different resources that can be used to get started with the genetic algorithm and building it in Python. -.. _header-n329: +.. _header-n364: Tutorial: Implementing Genetic Algorithm in Python -------------------------------------------------- @@ -989,9 +1155,9 @@ tutorial `__ -|image6| +|image10| -.. _header-n350: +.. _header-n385: Tutorial: Build Neural Networks in Python ----------------------------------------- @@ -1028,9 +1194,9 @@ available at these links: - `KDnuggets `__ -|image7| +|image11| -.. _header-n360: +.. _header-n395: Tutorial: Optimize Neural Networks with Genetic Algorithm --------------------------------------------------------- @@ -1048,9 +1214,9 @@ available at these links: - `KDnuggets `__ -|image8| +|image12| -.. _header-n370: +.. _header-n405: Tutorial: Building CNN in Python -------------------------------- @@ -1074,9 +1240,9 @@ tutorial `__ -|image10| +|image14| -.. _header-n393: +.. _header-n428: Book: Practical Computer Vision Applications Using Deep Learning with CNNs -------------------------------------------------------------------------- @@ -1121,7 +1287,7 @@ Find the book at these links: .. figure:: https://user-images.githubusercontent.com/16560492/78830077-ae7c2800-79e7-11ea-980b-53b6bd879eeb.jpg :alt: -.. _header-n408: +.. _header-n443: Contact Us ========== @@ -1145,23 +1311,34 @@ Contact Us .. figure:: https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png :alt: -.. |image1| image:: https://user-images.githubusercontent.com/16560492/108586306-85bd0280-731b-11eb-874c-7ac4ce1326cd.jpg +Thank you for using +`PyGAD `__ :) + +.. |image1| image:: https://user-images.githubusercontent.com/16560492/111009628-2b372500-8362-11eb-90cf-01b47d831624.png + :target: https://blog.paperspace.com/train-keras-models-using-genetic-algorithm-with-pygad +.. |image2| image:: https://user-images.githubusercontent.com/16560492/111009678-5457b580-8362-11eb-899a-39e2f96984df.png + :target: https://neptune.ai/blog/train-pytorch-models-using-genetic-algorithm-with-pygad +.. |image3| image:: https://user-images.githubusercontent.com/16560492/111009275-3178d180-8361-11eb-9e86-7fb1519acde7.png + :target: https://www.hebergementwebs.com/nouvelles/comment-les-algorithmes-genetiques-peuvent-rivaliser-avec-la-descente-de-gradient-et-le-backprop +.. |image4| image:: https://user-images.githubusercontent.com/16560492/111009257-232ab580-8361-11eb-99a5-7226efbc3065.png + :target: https://www.hebergementwebs.com/noticias/como-los-algoritmos-geneticos-pueden-competir-con-el-descenso-de-gradiente-y-el-backprop +.. |image5| image:: https://user-images.githubusercontent.com/16560492/108586306-85bd0280-731b-11eb-874c-7ac4ce1326cd.jpg :target: https://data-newbie.tistory.com/m/685 -.. |image2| image:: https://user-images.githubusercontent.com/16560492/108586601-85be0200-731d-11eb-98a4-161c75a1f099.jpg +.. |image6| image:: https://user-images.githubusercontent.com/16560492/108586601-85be0200-731d-11eb-98a4-161c75a1f099.jpg :target: https://erencan34.medium.com/pygad-ile-genetik-algoritmay%C4%B1-kullanarak-keras-modelleri-nas%C4%B1l-e%C4%9Fitilir-cf92639a478c -.. |image3| image:: https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png +.. |image7| image:: https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png :target: https://thebojda.medium.com/tensorflow-alapoz%C3%B3-10-24f7767d4a2c -.. |image4| image:: https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png +.. |image8| image:: https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png :target: https://neurohive.io/ru/frameworki/pygad-biblioteka-dlya-implementacii-geneticheskogo-algoritma -.. |image5| image:: https://user-images.githubusercontent.com/16560492/78830052-a3c19300-79e7-11ea-8b9b-4b343ea4049c.png +.. |image9| image:: https://user-images.githubusercontent.com/16560492/78830052-a3c19300-79e7-11ea-8b9b-4b343ea4049c.png :target: https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad -.. |image6| image:: https://user-images.githubusercontent.com/16560492/82078259-26252d00-96e1-11ea-9a02-52a99e1054b9.jpg +.. |image10| image:: https://user-images.githubusercontent.com/16560492/82078259-26252d00-96e1-11ea-9a02-52a99e1054b9.jpg :target: https://www.linkedin.com/pulse/introduction-optimization-genetic-algorithm-ahmed-gad -.. |image7| image:: https://user-images.githubusercontent.com/16560492/82078281-30472b80-96e1-11ea-8017-6a1f4383d602.jpg +.. |image11| image:: https://user-images.githubusercontent.com/16560492/82078281-30472b80-96e1-11ea-8017-6a1f4383d602.jpg :target: https://www.linkedin.com/pulse/artificial-neural-network-implementation-using-numpy-fruits360-gad -.. |image8| image:: https://user-images.githubusercontent.com/16560492/82078300-376e3980-96e1-11ea-821c-aa6b8ceb44d4.jpg +.. |image12| image:: https://user-images.githubusercontent.com/16560492/82078300-376e3980-96e1-11ea-821c-aa6b8ceb44d4.jpg :target: https://www.linkedin.com/pulse/artificial-neural-networks-optimization-using-genetic-ahmed-gad -.. |image9| image:: https://user-images.githubusercontent.com/16560492/82431022-6c3a1200-9a8e-11ea-8f1b-b055196d76e3.png +.. |image13| image:: https://user-images.githubusercontent.com/16560492/82431022-6c3a1200-9a8e-11ea-8f1b-b055196d76e3.png :target: https://www.linkedin.com/pulse/building-convolutional-neural-network-using-numpy-from-ahmed-gad -.. |image10| image:: https://user-images.githubusercontent.com/16560492/82431369-db176b00-9a8e-11ea-99bd-e845192873fc.png +.. |image14| image:: https://user-images.githubusercontent.com/16560492/82431369-db176b00-9a8e-11ea-99bd-e845192873fc.png :target: https://www.linkedin.com/pulse/derivation-convolutional-neural-network-from-fully-connected-gad diff --git a/docs/source/README_pygad_ReadTheDocs.rst b/docs/source/README_pygad_ReadTheDocs.rst index a1bf768..d04a434 100644 --- a/docs/source/README_pygad_ReadTheDocs.rst +++ b/docs/source/README_pygad_ReadTheDocs.rst @@ -1,4 +1,4 @@ -.. _header-n0: +.. _header-n599: ``pygad`` Module ================ @@ -9,7 +9,7 @@ This section of the PyGAD's library documentation discusses the Using the ``pygad`` module, instances of the genetic algorithm can be created, run, saved, and loaded. -.. _header-n4: +.. _header-n603: ``pygad.GA`` Class ================== @@ -19,7 +19,7 @@ class named ``GA`` for building the genetic algorithm. The constructor, methods, function, and attributes within the class are discussed in this section. -.. _header-n6: +.. _header-n605: ``__init__()`` -------------- @@ -302,7 +302,7 @@ that the best solution in the initial population. The parameters are validated within the constructor. If at least a parameter is not validated, an exception is thrown. -.. _header-n82: +.. _header-n681: Class Attributes ---------------- @@ -316,7 +316,7 @@ Class Attributes - ``supported_int_float_types``: A list of the supported types for all numbers. It just concatenates the previous 2 lists. -.. _header-n90: +.. _header-n689: Other Instance Attributes & Methods ----------------------------------- @@ -328,7 +328,7 @@ attributes and methods added to the instances of the **pygad.GA** class: The next 2 subsections list such attributes and methods. -.. _header-n93: +.. _header-n692: Other Attributes ~~~~~~~~~~~~~~~~ @@ -376,7 +376,7 @@ Other Attributes Note that the attributes with its name start with ``last_generation_`` are updated after each generation. -.. _header-n120: +.. _header-n719: Other Methods ~~~~~~~~~~~~~ @@ -421,7 +421,7 @@ Other Methods The next sections discuss the methods available in the **pygad.GA** class. -.. _header-n143: +.. _header-n742: ``initialize_population()`` --------------------------- @@ -448,7 +448,7 @@ This method assigns the values of the following 3 instance attributes: 3. ``initial_population``: Keeping the initial population. -.. _header-n159: +.. _header-n758: ``cal_pop_fitness()`` --------------------- @@ -462,7 +462,7 @@ constructor for each solution. It returns an array of the solutions' fitness values. -.. _header-n163: +.. _header-n762: ``run()`` --------- @@ -506,7 +506,7 @@ After the ``run()`` method completes, the following takes place: - The ``run_completed`` attribute is set to ``True``. -.. _header-n182: +.. _header-n781: Parent Selection Methods ------------------------ @@ -524,49 +524,49 @@ All of such methods return an array of the selected parents. The next subsections list the supported methods for parent selection. -.. _header-n191: +.. _header-n790: ``steady_state_selection()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Selects the parents using the steady-state selection technique. -.. _header-n193: +.. _header-n792: ``rank_selection()`` ~~~~~~~~~~~~~~~~~~~~ Selects the parents using the rank selection technique. -.. _header-n195: +.. _header-n794: ``random_selection()`` ~~~~~~~~~~~~~~~~~~~~~~ Selects the parents randomly. -.. _header-n197: +.. _header-n796: ``tournament_selection()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~ Selects the parents using the tournament selection technique. -.. _header-n199: +.. _header-n798: ``roulette_wheel_selection()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Selects the parents using the roulette wheel selection technique. -.. _header-n201: +.. _header-n800: ``stochastic_universal_selection()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Selects the parents using the stochastic universal selection technique. -.. _header-n203: +.. _header-n802: Crossover Methods ----------------- @@ -583,7 +583,7 @@ All of such methods return an array of the produced offspring. The next subsections list the supported methods for crossover. -.. _header-n212: +.. _header-n811: ``single_point_crossover()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -591,7 +591,7 @@ The next subsections list the supported methods for crossover. Applies the single-point crossover. It selects a point randomly at which crossover takes place between the pairs of parents. -.. _header-n214: +.. _header-n813: ``two_points_crossover()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -599,7 +599,7 @@ crossover takes place between the pairs of parents. Applies the 2 points crossover. It selects the 2 points randomly at which crossover takes place between the pairs of parents. -.. _header-n216: +.. _header-n815: ``uniform_crossover()`` ~~~~~~~~~~~~~~~~~~~~~~~ @@ -607,7 +607,7 @@ which crossover takes place between the pairs of parents. Applies the uniform crossover. For each gene, a parent out of the 2 mating parents is selected randomly and the gene is copied from it. -.. _header-n218: +.. _header-n817: ``scattered_crossover()`` ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -615,7 +615,7 @@ mating parents is selected randomly and the gene is copied from it. Applies the scattered crossover. It randomly selects the gene from one of the 2 parents. -.. _header-n220: +.. _header-n819: Mutation Methods ---------------- @@ -629,7 +629,7 @@ All of such methods return an array of the mutated offspring. The next subsections list the supported methods for mutation. -.. _header-n227: +.. _header-n826: ``random_mutation()`` ~~~~~~~~~~~~~~~~~~~~~ @@ -643,7 +643,7 @@ specified by the 2 attributes ``random_mutation_min_val`` and ``random_mutation_max_val``. The random value is added to the selected gene. -.. _header-n230: +.. _header-n829: ``swap_mutation()`` ~~~~~~~~~~~~~~~~~~~ @@ -651,7 +651,7 @@ gene. Applies the swap mutation which interchanges the values of 2 randomly selected genes. -.. _header-n232: +.. _header-n831: ``inversion_mutation()`` ~~~~~~~~~~~~~~~~~~~~~~~~ @@ -659,7 +659,7 @@ selected genes. Applies the inversion mutation which selects a subset of genes and inverts them. -.. _header-n234: +.. _header-n833: ``scramble_mutation()`` ~~~~~~~~~~~~~~~~~~~~~~~ @@ -667,7 +667,7 @@ inverts them. Applies the scramble mutation which selects a subset of genes and shuffles their order randomly. -.. _header-n236: +.. _header-n835: ``adaptive_mutation()`` ~~~~~~~~~~~~~~~~~~~~~~~ @@ -675,7 +675,7 @@ shuffles their order randomly. Applies the adaptive mutation which selects a subset of genes and shuffles their order randomly. -.. _header-n238: +.. _header-n837: ``best_solution()`` ------------------- @@ -699,7 +699,7 @@ It returns the following: - ``best_match_idx``: Index of the best solution in the current population. -.. _header-n252: +.. _header-n851: ``plot_result()`` ----------------- @@ -722,7 +722,7 @@ Starting from PyGAD 2.5.0, a new optional parameter named ``linewidth`` is added to specify the width of the curve in the plot. It defaults to ``3.0``. -.. _header-n264: +.. _header-n863: ``save()`` ---------- @@ -734,7 +734,7 @@ Accepts the following parameter: - ``filename``: Name of the file to save the instance. No extension is needed. -.. _header-n270: +.. _header-n869: Functions in ``pygad`` ====================== @@ -743,7 +743,7 @@ Besides the methods available in the **pygad.GA** class, this section discusses the functions available in pygad. Up to this time, there is only a single function named ``load()``. -.. _header-n272: +.. _header-n871: ``pygad.load()`` ---------------- @@ -760,7 +760,7 @@ Accepts the following parameter: Returns the genetic algorithm instance. -.. _header-n279: +.. _header-n878: Steps to Use ``pygad`` ====================== @@ -785,7 +785,7 @@ To use the ``pygad`` module, here is a summary of the required steps: Let's discuss how to do each of these steps. -.. _header-n299: +.. _header-n898: Preparing the ``fitness_func`` Parameter ---------------------------------------- @@ -845,7 +845,7 @@ an exception is thrown. By creating this function, you almost did an awesome step towards using PyGAD. -.. _header-n315: +.. _header-n914: Preparing Other Parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -873,7 +873,7 @@ Here is an example for preparing the other parameters: mutation_type = "random" mutation_percent_genes = 10 -.. _header-n318: +.. _header-n917: The ``callback_generation`` Parameter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -910,7 +910,7 @@ generation. After the parameters are prepared, we can import PyGAD and build an instance of the **pygad.GA** class. -.. _header-n325: +.. _header-n924: Import the ``pygad`` -------------------- @@ -924,7 +924,7 @@ The next step is to import PyGAD as follows: The **pygad.GA** class holds the implementation of all methods for running the genetic algorithm. -.. _header-n329: +.. _header-n928: Create an Instance of the ``pygad.GA`` Class -------------------------------------------- @@ -948,7 +948,7 @@ for creating the initial population. mutation_type=mutation_type, mutation_percent_genes=mutation_percent_genes) -.. _header-n332: +.. _header-n931: Run the Genetic Algorithm ------------------------- @@ -972,7 +972,7 @@ by doing the following tasks: 4. Repeat the process for the specified number of generations. -.. _header-n345: +.. _header-n944: Plotting Results ---------------- @@ -988,7 +988,7 @@ generations. .. figure:: https://user-images.githubusercontent.com/16560492/78830005-93111d00-79e7-11ea-9d8e-a8d8325a6101.png :alt: -.. _header-n349: +.. _header-n948: Information about the Best Solution ----------------------------------- @@ -1018,7 +1018,7 @@ fitness** is reached could be fetched. if ga_instance.best_solution_generation != -1: print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation)) -.. _header-n361: +.. _header-n960: Saving & Loading the Results ---------------------------- @@ -1051,7 +1051,7 @@ any property. print(loaded_ga_instance.best_solution()) -.. _header-n368: +.. _header-n967: Crossover, Mutation, and Parent Selection ========================================= @@ -1061,7 +1061,7 @@ the crossover & mutation operators. More features will be added in the future. To ask for a new feature, please check the **Ask for Feature** section. -.. _header-n370: +.. _header-n969: Supported Crossover Operations ------------------------------ @@ -1075,7 +1075,7 @@ The supported crossover operations at this time are: 3. Uniform: Implemented using the ``uniform_crossover()`` method. -.. _header-n379: +.. _header-n978: Supported Mutation Operations ----------------------------- @@ -1090,7 +1090,7 @@ The supported mutation operations at this time are: 4. Scramble: Implemented using the ``scramble_mutation()`` method. -.. _header-n390: +.. _header-n989: Supported Parent Selection Operations ------------------------------------- @@ -1112,7 +1112,7 @@ The supported parent selection techniques at this time are: 6. Tournament: Implemented using the ``tournament_selection()`` method. -.. _header-n405: +.. _header-n1004: Life Cycle of PyGAD =================== @@ -1207,7 +1207,7 @@ argument, here is the output. on_stop() -.. _header-n412: +.. _header-n1011: Adaptive Mutation ================= @@ -1269,7 +1269,7 @@ The next figure summarizes the previous steps. This strategy is applied in PyGAD. -.. _header-n434: +.. _header-n1033: Use Adaptive Mutation in PyGAD ------------------------------ @@ -1373,7 +1373,7 @@ Here is an example that uses adaptive mutation. ga_instance.plot_result(title="PyGAD with Adaptive Mutation", linewidth=5) -.. _header-n463: +.. _header-n1062: Limit the Gene Value Range ========================== @@ -1452,7 +1452,7 @@ take any floating-point value from the range that starts from 1 gene_space = [{'low': 1, 'high': 5}, {'low': 0.3, 'high': 1.4}, {'low': -0.2, 'high': 4.5}] -.. _header-n490: +.. _header-n1089: Stop at Any Generation ====================== @@ -1481,7 +1481,7 @@ exceeds 70, then the string ``"stop"`` is returned. if ga_instance.best_solution()[1] >= 70: return "stop" -.. _header-n495: +.. _header-n1094: Prevent Duplicates in Gene Values ================================= @@ -1641,7 +1641,7 @@ non-duplicating genes that may make a room for a unique value in one the duplicates. The resultant gene is then ``[3 4 2 0]``. **But this option is not yet supported in PyGAD.** -.. _header-n509: +.. _header-n1108: Parallel Processing in PyGAD ============================ @@ -1680,7 +1680,7 @@ Fazekas `__: `How Genetic Algorithms Can Compete with Gradient Descent and Backprop `__ -.. _header-n520: +.. _header-n1198: Examples ======== @@ -1688,7 +1688,7 @@ Examples This section gives the complete code of some examples that use ``pygad``. Each subsection builds a different example. -.. _header-n522: +.. _header-n1121: Linear Model Optimization ------------------------- @@ -1790,7 +1790,7 @@ which optimizes a linear model. Its complete code is listed below. loaded_ga_instance = pygad.load(filename=filename) loaded_ga_instance.plot_result() -.. _header-n525: +.. _header-n1124: Reproducing Images ------------------ @@ -1811,7 +1811,7 @@ available at these links: - `LinkedIn `__: https://www.linkedin.com/pulse/reproducing-images-using-genetic-algorithm-python-ahmed-gad -.. _header-n533: +.. _header-n1132: Project Steps ~~~~~~~~~~~~~ @@ -1833,7 +1833,7 @@ The steps to follow in order to reproduce an image are as follows: The next sections discusses the code of each of these steps. -.. _header-n549: +.. _header-n1148: Read an Image ~~~~~~~~~~~~~ @@ -1863,7 +1863,7 @@ range from which the random values are selected during mutation and also the range of the values used in the initial population. So, be consistent. -.. _header-n556: +.. _header-n1155: Prepare the Fitness Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1913,7 +1913,7 @@ its code is listed below. return numpy.reshape(a=vector, newshape=shape) -.. _header-n562: +.. _header-n1161: Create an Instance of the ``pygad.GA`` Class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1949,7 +1949,7 @@ the full list of parameters. random_mutation_min_val=0.0, random_mutation_max_val=1.0) -.. _header-n567: +.. _header-n1166: Run PyGAD ~~~~~~~~~ @@ -1960,7 +1960,7 @@ Simply, call the ``run()`` method to run PyGAD. ga_instance.run() -.. _header-n570: +.. _header-n1169: Plot Results ~~~~~~~~~~~~ @@ -1977,7 +1977,7 @@ Here is the plot after 20,000 generations. .. figure:: https://user-images.githubusercontent.com/16560492/82232124-77762c00-992e-11ea-9fc6-14a1cd7a04ff.png :alt: -.. _header-n575: +.. _header-n1174: Calculate Some Statistics ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1999,7 +1999,7 @@ Here is some information about the best solution. matplotlib.pyplot.title("PyGAD & GARI for Reproducing Images") matplotlib.pyplot.show() -.. _header-n578: +.. _header-n1177: Evolution by Generation ~~~~~~~~~~~~~~~~~~~~~~~ @@ -2055,3 +2055,43 @@ Here is how the image is evolved from generation 0 to generation .. figure:: https://user-images.githubusercontent.com/16560492/82232405-e0f63a80-992e-11ea-984f-b6ed76465bd1.png :alt: + +.. _header-n1201: + +Clustering +---------- + +For a 2-cluster problem, the code is available +`here `__. +For a 3-cluster problem, the code is +`here `__. +The 2 examples are using artificial samples. + +Soon a tutorial will be published at +`Paperspace `__ to explain how +clustering works using the genetic algorithm with examples in PyGAD. + +.. figure:: https://user-images.githubusercontent.com/16560492/111010269-02b02a80-8364-11eb-87fd-82d6dc175c57.gif + :alt: + +.. _header-n1216: + +CoinTex Game Playing using PyGAD +-------------------------------- + +The code is available the `CoinTex GitHub +project `__. +CoinTex is an Android game written in Python using the Kivy framework. +Find CoinTex at `Google +Play `__: +https://play.google.com/store/apps/details?id=coin.tex.cointexreactfast + +Check this `Paperspace +tutorial `__ +for how the genetic algorithm plays CoinTex: +https://blog.paperspace.com/building-agent-for-cointex-using-genetic-algorithm. +Check also this `YouTube video `__ showing +the genetic algorithm while playing CoinTex. + +.. figure:: https://user-images.githubusercontent.com/16560492/86600094-dad02200-bf9f-11ea-9513-5b57739b0f58.gif + :alt: