Source-linked AI summary
PyGAD: An Intuitive Genetic Algorithm Python Library
Ahmed Fawzy Gad
TL;DR
Genetic-algorithm experimentation requires managing many configurable lifecycle components, while existing libraries can impose boilerplate and complexity. The paper introduces PyGAD, an intuitive Python library with a three-step interface and broad customization, and reports favorable execution-time and optimization outcomes in the supplied examples.
Problem
Genetic-algorithm tools can require substantial user effort, boilerplate, and specialized knowledge, motivating an easier experimentation interface.
Method
PyGAD provides a customizable genetic-algorithm library organized around a fitness function, pygad.GA instantiation, and run() execution, with modules for NumPy-built and Keras or PyTorch models.
Results
PyGAD found the best solution after 31 generations with fitness 182.698 and averaged 0.14 seconds per run, versus 0.65 for DEAP and 0.052 for LEAP.
Takeaways & Limitations
PyGAD gives users control over the genetic-algorithm lifecycle and configuration while simplifying optimization through a short, intuitive interface.
Abstract
from arXiv · showhide
This paper introduces PyGAD, an open-source easy-to-use Python library for building the genetic algorithm. PyGAD supports a wide range of parameters to give the user control over everything in its life cycle. This includes, but is not limited to, population, gene value range, gene data type, parent selection, crossover, and mutation. PyGAD is designed as a general-purpose optimization library that allows the user to customize the fitness function. Its usage consists of 3 main steps: build the fitness function, create an instance of the pygad.GA class, and calling the pygad.GA.run() method. The library supports training deep learning models created either with PyGAD itself or with frameworks like Keras and PyTorch. Given its stable state, PyGAD is also in active development to respond to the user's requested features and enhancement received on GitHub https://github.com/ahmedfgad/GeneticAlgorithmPython. PyGAD comes with documentation https://pygad.readthedocs.io for further details and examples.
I. INTRODUCTION
Genetic algorithms evolve populations of candidate solutions through selection, crossover, and mutation. PyGAD is introduced as an intuitive, customizable Python library intended to make genetic-algorithm experimentation easier.
- Evolutionary algorithms improve solution quality across iterations by evolving either a single solution or a population of solutions.
- Genetic algorithms select fitter solutions as parents, combine their genes through crossover, and mutate offspring before repeating across generations.The process continues for a limited number of generations or until a satisfactory solution is found.
- Many genetic-algorithm parameters require tuning, motivating easy-to-use tools for building and experimenting with genetic algorithms.
- PyGAD is an open-source Python library for single-objective genetic-algorithm optimization with a broad range of customizable parameters.The paper reports over 185K installations at the time of writing.
- PyGAD’s lifecycle can be customized, supports decimal and binary representations and NumPy numeric gene types, and can train Keras and PyTorch models.
- The paper introduces PyGAD after reviewing existing Python genetic-algorithm libraries and their objectives and limitations.
A. DEAP
DEAP is a widely used, flexible evolutionary library, but the paper describes substantial user effort and boilerplate for constructing, customizing, and visualizing genetic-algorithm experiments.
- DEAP is a common Python optimization library that supports genetic algorithms, NSGA-II, particle swarm optimization, and evolution strategies.The paper associates its popularity with installations, GitHub activity, and approximately 4.2K stars.
- DEAP’s creator-and-toolbox structure requires more than beginner-level Python knowledge and takes time to understand.
- Users must write an evolutionary loop covering fitness calculation, parent selection, crossover, mutation, and repeated generations.The paper characterizes this interface as non-friendly for users who want to focus on experiments.
- DEAP’s ready-to-use algorithms save time but perform specific tasks and expose few parameters, limiting customization.Examples include eaSimple, eaMuPlusLambda, and varOr.
- Building mixed-data-type populations requires registering gene types, individuals, and populations before constructing the population.The paper also reports no support for sparse discrete gene-value sets or selecting an exact number of genes to mutate.
- Traditional DEAP mutation gives solutions equal mutation probability regardless of fitness, which the paper says can distort high-quality solutions.
- DEAP lacks built-in result visualization, so users must manually create plots after evolution.
B. Pyevolve
Pyevolve provides a pure-Python genetic-algorithm interface but is described as verbose, limited in extensibility, and no longer maintained. The comparison reports substantially more code for Pyevolve than DEAP on OneMax.
- Pyevolve is a pure-Python genetic-algorithm library whose workflow creates a fitness function and chromosome, sets parameters, instantiates GSimpleGA, and calls evolve().
- Pyevolve was less popular than DEAP and had a limited community, based on installations, GitHub stars, and citations.The paper reports 50K total installations and 301 GitHub stars.
- Pyevolve uses boilerplate configuration code that makes scripts longer even for simple problems, while mutation applies equally to all solutions.
- Pyevolve supports predefined integer and real gene types, but creating new types requires understanding additional library classes.
- 59 lines for DEAP versus 378 for Pyevolve were needed to solve OneMax, according to the reported comparison.
- Pyevolve was no longer maintained, with its latest release at the end of 2014 and most recent GitHub commit at the end of 2015.
D. LEAP
LEAP provides core abstractions for evolutionary algorithms but retains an evolution loop that users must manage, while its documentation identifies missing features and limited adoption.
- LEAP design: LEAP organizes genetic-algorithm construction around Individual, Decoder, and Problem classes, with the decoder converting genes before fitness evaluation.The decoder translates each individual’s genes into a form used to calculate fitness for the current problem.
- LEAP design: Using IdentityDecoder() as the usual default means genes are translated to themselves, although the paper argues users should work directly on genes or decode within the fitness function.The paper characterizes mandatory decoding as a design issue for problems that do not need gene transformation.
- Limitations: LEAP documentation reports missing features including mixed data representation, and the library lacks a lifecycle for tracing each generation.These limitations constrain representation flexibility and generation-level monitoring.
- Limitations: Users must write the evolution loop and explicitly increment the generation counter; omitting util.inc_generation() can cause an infinite evolution loop.The paper identifies this as especially problematic for less experienced users and as inconsistent with LEAP’s goal of serving users of existing tools.
- Adoption: The compared library has 3.4K installations and 39 GitHub stars since publication, whereas PyGAD has over 185K installations over one year.The cited figures are presented as adoption comparisons between the library discussed and PyGAD.
A. PyGAD Overview
PyGAD is designed to make genetic-algorithm optimization simple while preserving extensive user control. Its customizable lifecycle, modular neural-network support, documentation, and translated tutorials extend the library’s usability and reach.
- Objectives: PyGAD pursues two objectives: simplicity for users with limited knowledge and control over the genetic algorithm’s configurable behavior.Descriptive names and straightforward configuration parameters support simplicity, while the library exposes broad customization.
- Lifecycle: Users do not need to build the evolution loop because PyGAD provides an elastic lifecycle that can be altered, including enabling, disabling, or replacing operators.The lifecycle can support custom mutation and crossover operators for research purposes.
- Modules: PyGAD includes modules for constructing and training fully connected and convolutional neural networks, including networks built with NumPy-based components.The module list includes pygad.nn, pygad.gann, pygad.cnn, and pygad.gacnn.
- Documentation: Detailed documentation provides feature explanations, examples, and tutorials describing source code from projects built with PyGAD.Additional project resources are listed in the appendices.
- Documentation: Some English PyGAD articles and tutorials have been translated into Korean, Turkish, Hungarian, Chinese, and Russian.The translations are collected in the documentation’s PyGAD in Other Languages section.
- Documentation: The documentation includes a Release History section summarizing changes and additions in each release.This gives users a release-by-release record of library development.
B. PyGAD Usage
PyGAD usage follows three core steps: define a fitness function, instantiate pygad.GA with configuration parameters, and call run() to evolve solutions. The library supports result inspection, plotting, and concise optimization scripts.
- PyGAD usage consists of building the fitness function, instantiating pygad.GA, and calling run() to start evolution.
- The fitness function receives a solution vector and its population index, then returns a numeric maximization fitness value.In the example, fitness is based on the absolute error from a target and is inverted to support maximization.
- The pygad.GA constructor groups configuration parameters, including generations, population size, mating parents, genes, and the fitness function.
- A basic example evolves 10 solutions with 5 mating parents, 3 genes per solution, and 100 generations.
- The best_solution() method returns the best parameters, fitness, and population index, while plot_result() visualizes fitness across generations.The example reports the best solution after 31 generations with a fitness value of 182.698.
- PyGAD solves OneMax in 15 lines of code, compared with 45 for DEAP and 34 for LEAP.
- Across three 100-generation runs, PyGAD averaged 0.14 seconds, compared with 0.65 for DEAP and 0.052 for LEAP.The cited comparison also reports that LEAP did not find the optimal solution after 1,000 generations.
C. PyGAD Lifecycle
PyGAD organizes genetic-algorithm execution as a traceable, customizable lifecycle from population initialization through successive generations and termination.
- PyGAD’s lifecycle tracks and controls the stages of evolution, with side blocks representing operations between states.
- The GA constructor validates parameters and initializes attributes, including the NumPy array holding the population solutions.
- Calling run() starts the lifecycle, which contains seven states with corresponding callbacks.
- The callbacks cover startup, fitness evaluation, parent selection, crossover, mutation, generation completion, and stopping.
- Users can perform preprocessing and postprocessing through on_start() and on_stop(), respectively.
- Custom crossover and mutation operators can be defined through callback functions when unsupported operators are needed.
- Returning "stop" from on_generation() ends run() before all generations are completed.
D. PyGAD Features
PyGAD emphasizes intuitive use while exposing broad control over genetic-algorithm configuration, gene representation, initialization, callbacks, visualization, and supporting resources.
- PyGAD aims to optimize different problem types through three simple, self-explanatory steps.
- The pygad.GA constructor groups parameters for focused configuration, including user-defined gene spaces and gene data types.
- Users can specify an initial population or let PyGAD generate one randomly when no initial population is provided.
- PyGAD provides lifecycle tracking and callback control, alongside built-in visualization of fitness changes by generation.
- Documentation, examples, projects, blog posts, and online communities provide resources for getting started with PyGAD.
IV. CONCLUSION
The paper presents PyGAD as an intuitive single-objective genetic-algorithm library with extensive customization, lifecycle control, and support for concise optimization workflows.
- PyGAD is a Python library for single-objective optimization using the genetic algorithm.
- Its three-step interface combines fitness-function creation, pygad.GA instantiation, and run() invocation.
- PyGAD supports extensive customization, including gene value spaces, gene data types, model training, and duplicate rejection.
- Its lifecycle tracks the evolution process from initialization through completion.
- The library provides a simpler interface for less experienced Python users and uses few lines of code for optimization.
APPENDIX A PYGAD SUPPLEMENTAL RESOURCES
Appendix A collects tutorials and articles covering PyGAD applications, genetic-algorithm representations, mutation, comparisons, clustering, and model training.
- Applications and methods: The appendix lists tutorials on applying PyGAD to games, clustering, genetic-algorithm representations, and multiple optimization tasks.Listed examples include CoinTex, clustering, and different genetic-algorithm representations.
- Deep learning: Several resources explain training neural networks and Keras or PyTorch models with PyGAD.The listed articles cover neural networks, Keras models, and PyTorch models.
- Comparisons: The supplemental materials include articles comparing genetic algorithms with gradient descent, backpropagation, and gradient boosting.The comparison resources include both general optimization methods and gradient boosting.
- Mutation: Adaptive mutation is covered in a dedicated PyGAD tutorial with examples.
APPENDIX B PROJECTS WITH PYGAD
Appendix B lists projects built using PyGAD and identifies their source-code repositories and example applications.
- Project list: The appendix introduces a list of projects built using PyGAD with their source code.
- Games: The listed projects include game-playing agents for CoinTex and Flappy Bird.
- Puzzles and images: Other projects use PyGAD to solve the 8-queen puzzle and reproduce gray and RGB images.