PyGAD - Python Genetic Algorithm!¶
PyGAD is anopen-source Python library for building the genetic algorithm andoptimizing machine learning algorithms. It works withKeras andPyTorch.
Try theOptimization Gadget, a freecloud-based tool powered by PyGAD. It simplifies optimization byreducing or eliminating the need for coding while providinginsightful visualizations.
PyGAD supportsdifferent types of crossover, mutation, and parent selection operators.PyGAD allowsdifferent types of problems to be optimized using the genetic algorithmby customizing the fitness function. It works with both single-objectiveand multi-objective optimization problems.

Logo designed byAsmaaKabil
Besides building the genetic algorithm, it builds and optimizes machinelearning algorithms. Currently,PyGAD supports building andtraining (using genetic algorithm) artificial neural networks forclassification problems.
The library is under active development and more features addedregularly. Please contact us if you want a feature to be supported.
Donation & Support¶
You can donate to PyGAD via:
Credit/Debit Card:https://donate.stripe.com/eVa5kO866elKgM0144
PayPal: Use either this link:paypal.me/ahmedfgad or the e-mailaddressahmed.f.gad@gmail.com
Interac e-Transfer: Use e-mail addressahmed.f.gad@gmail.com
Buy a product atTeespring:pygad.creator-spring.com
Installation¶
To installPyGAD, simply use pip todownload and install the library fromPyPI (Python Package Index). Thelibrary lives a PyPI at this pagehttps://pypi.org/project/pygad.
Install PyGAD with the following command:
pip3installpygad
Quick Start¶
To get started withPyGAD, simplyimport it.
importpygad
UsingPyGAD, a wide range ofproblems can be optimized. A quick and simple problem to be optimizedusing thePyGAD is finding the bestset of weights that satisfy the following function:
y=f(w1:w6)=w1x1+w2x2+w3x3+w4x4+w5x5+w6x6where(x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7)andy=44
The first step is to prepare the inputs and the outputs of thisequation.
function_inputs=[4,-2,3.5,5,-11,-4.7]desired_output=44
A very important step is to implement the fitness function that will beused for calculating the fitness value for each solution. Here is one.
If the fitness function returns a number, then the problem issingle-objective. If alist,tuple, ornumpy.ndarray isreturned, then it is a multi-objective problem (applicable even if asingle element exists).
deffitness_func(ga_instance,solution,solution_idx):output=numpy.sum(solution*function_inputs)fitness=1.0/numpy.abs(output-desired_output)returnfitness
Next is to prepare the parameters ofPyGAD. Here is an example for a setof parameters.
fitness_function=fitness_funcnum_generations=50num_parents_mating=4sol_per_pop=8num_genes=len(function_inputs)init_range_low=-2init_range_high=5parent_selection_type="sss"keep_parents=1crossover_type="single_point"mutation_type="random"mutation_percent_genes=10
After the parameters are prepared, an instance of thepygad.GA classis created.
ga_instance=pygad.GA(num_generations=num_generations,num_parents_mating=num_parents_mating,fitness_func=fitness_function,sol_per_pop=sol_per_pop,num_genes=num_genes,init_range_low=init_range_low,init_range_high=init_range_high,parent_selection_type=parent_selection_type,keep_parents=keep_parents,crossover_type=crossover_type,mutation_type=mutation_type,mutation_percent_genes=mutation_percent_genes)
After creating the instance, therun() method is called to start theoptimization.
ga_instance.run()
After therun() method completes, information about the bestsolution found by PyGAD can be accessed.
solution,solution_fitness,solution_idx=ga_instance.best_solution()print("Parameters of the best solution :{solution}".format(solution=solution))print("Fitness value of the best solution ={solution_fitness}".format(solution_fitness=solution_fitness))prediction=numpy.sum(numpy.array(function_inputs)*solution)print("Predicted output based on the best solution :{prediction}".format(prediction=prediction))
Parametersofthebestsolution:[3.92692328-0.115549462.398733813.29579039-0.740914761.05468517]Fitnessvalueofthebestsolution=157.37320042925006Predictedoutputbasedonthebestsolution:44.00635432206546
There is more to do using PyGAD. Read its documentation to explore thefeatures of PyGAD.
PyGAD’s Modules¶
PyGAD has the following modules:
The main module has the same name as the library
pygadwhich isthe main interface to build the genetic algorithm.The
nnmodule builds artificial neural networks.The
gannmodule optimizes neural networks (for classificationand regression) using the genetic algorithm.The
cnnmodule builds convolutional neural networks.The
gacnnmodule optimizes convolutional neural networks usingthe genetic algorithm.The
kerasgamodule to trainKeras modelsusing the genetic algorithm.The
torchgamodule to trainPyTorchmodels using the genetic algorithm.The
visualizemodule to visualize the results.The
utilsmodule contains the operators (crossover, mutation,and parent selection) and the NSGA-II code.The
helpermodule has some helper functions.
The documentation discusses these modules.
PyGAD Citation - Bibtex Formatted¶
If you used PyGAD, please consider citing its paper with the followingdetails:
@article{gad2023pygad,title={Pygad:Anintuitivegeneticalgorithmpythonlibrary},author={Gad,AhmedFawzy},journal={MultimediaToolsandApplications},pages={1--14},year={2023},publisher={Springer}}
pygad Module¶
pygad Module TOC
pygadModulepygad.GAClass- Functions in
pygad - Steps to Use
pygad - Life Cycle of PyGAD
- Examples
More About pygad Module¶
More About pygad Module TOC
- More About PyGAD
- Multi-Objective Optimization
- Limit the Gene Value Range using the
gene_spaceParameter - More about the
gene_spaceParameter - Gene Constraint
sample_sizeParameter- Stop at Any Generation
- Stop Criteria
- Elitism Selection
- Random Seed
- Continue without Losing Progress
- Change Population Size during Runtime
- Prevent Duplicates in Gene Values
- More about the
gene_typeParameter - Parallel Processing in PyGAD
- Print Lifecycle Summary
- Logging Outputs
- Solve Non-Deterministic Problems
- Reuse the Fitness instead of Calling the Fitness Function
- Why the Fitness Function is not Called for Solution at Index 0?
- Batch Fitness Calculation
- Use Functions and Methods to Build Fitness and Callbacks
utils Module¶
visualize Module¶
visualize Module TOC
helper Module¶
pygad.nn Module¶
pygad.gann Module¶
pygad.gann Module TOC
pygad.gannModulepygad.gann.GANNClass- Functions in the
pygad.gannModule - Steps to Build and Train Neural Networks using Genetic Algorithm
- Prepare the Training Data
- Create an Instance of the
pygad.gann.GANNClass - Fetch the Population Weights as Vectors
- Prepare the Fitness Function
- Prepare the Generation Callback Function
- Create an Instance of the
pygad.GAClass - Run the Created Instance of the
pygad.GAClass - Plot the Fitness Values
- Information about the Best Solution
- Making Predictions using the Trained Weights
- Calculating Some Statistics
- Examples
pygad.cnn Module¶
pygad.gacnn Module¶
pygad.gacnn Module TOC
pygad.gacnnModulepygad.gacnn.GACNNClass- Functions in the
pygad.gacnnModule - Steps to Build and Train CNN using Genetic Algorithm
- Prepare the Training Data
- Building the Network Architecture
- Building Model
- Model Summary
- Create an Instance of the
pygad.gacnn.GACNNClass - Fetch the Population Weights as Vectors
- Prepare the Fitness Function
- Prepare the Generation Callback Function
- Create an Instance of the
pygad.GAClass - Run the Created Instance of the
pygad.GAClass - Plot the Fitness Values
- Information about the Best Solution
- Making Predictions using the Trained Weights
- Calculating Some Statistics
- Examples
pygad.kerasga Module¶
pygad.kerasga Module TOC
pygad.kerasgaModule- Steps Summary
- Create Keras Model
pygad.kerasga.KerasGAClass- Functions in the
pygad.kerasgaModule - Examples
pygad.torchga Module¶
pygad.torchga Module TOC
pygad.torchgaModule- Steps Summary
- Create PyTorch Model
pygad.torchga.TorchGAClass- Functions in the
pygad.torchgaModule - Examples
Releases¶
Releases
- Release History
- PyGAD 1.0.17
- PyGAD 1.0.20
- PyGAD 2.0.0
- PyGAD 2.1.0
- PyGAD 2.2.1
- PyGAD 2.2.2
- PyGAD 2.3.0
- PyGAD 2.4.0
- PyGAD 2.5.0
- PyGAD 2.6.0
- PyGAD 2.7.0
- PyGAD 2.7.1
- PyGAD 2.7.2
- PyGAD 2.8.0
- PyGAD 2.8.1
- PyGAD 2.9.0
- PyGAD 2.10.0
- PyGAD 2.10.1
- PyGAD 2.10.2
- PyGAD 2.11.0
- PyGAD 2.12.0
- PyGAD 2.13.0
- PyGAD 2.14.0
- PyGAD 2.14.2
- PyGAD 2.14.3
- PyGAD 2.15.0
- PyGAD 2.15.1
- PyGAD 2.15.2
- PyGAD 2.16.0
- PyGAD 2.16.1
- PyGAD 2.16.2
- PyGAD 2.16.3
- PyGAD 2.17.0
- PyGAD 2.18.0
- PyGAD 2.18.1
- PyGAD 2.18.2
- PyGAD 2.18.3
- PyGAD 2.19.0
- PyGAD 2.19.1
- PyGAD 2.19.2
- PyGAD 3.0.0
- PyGAD 3.0.1
- PyGAD 3.1.0
- PyGAD 3.2.0
- PyGAD 3.3.0
- PyGAD 3.3.1
- PyGAD 3.4.0
- PyGAD 3.5.0
- PyGAD Projects at GitHub
- Stackoverflow Questions about PyGAD
- How do I proceed to load a ga_instance as “.pkl” format in PyGad?
- Binary Classification NN Model Weights not being Trained in PyGAD
- How to solve TSP problem using pyGAD package?
- How can I save a matplotlib plot that is the output of a function in jupyter?
- How do I query the best solution of a pyGAD GA instance?
- Multi-Input Multi-Output in Genetic algorithm (python)
- Submitting Issues
- Ask for Feature
- Projects Built using PyGAD
- Tutorials about PyGAD
- Adaptive Mutation in Genetic Algorithm with Python Examples
- Clustering Using the Genetic Algorithm in Python
- Working with Different Genetic Algorithm Representations in Python
- 5 Genetic Algorithm Applications Using PyGAD
- Train Neural Networks Using a Genetic Algorithm in Python with PyGAD
- Building a Game-Playing Agent for CoinTex Using the Genetic Algorithm
- How To Train Keras Models Using the Genetic Algorithm with PyGAD
- Train PyTorch Models Using Genetic Algorithm with PyGAD
- A Guide to Genetic ‘Learning’ Algorithms for Optimization
- PyGAD in Other Languages
- Research Papers using PyGAD
- More Links
- For More Information
- Tutorial: Implementing Genetic Algorithm in Python
- Tutorial: Introduction to Genetic Algorithm
- Tutorial: Build Neural Networks in Python
- Tutorial: Optimize Neural Networks with Genetic Algorithm
- Tutorial: Building CNN in Python
- Tutorial: Derivation of CNN from FCNN
- Book: Practical Computer Vision Applications Using Deep Learning with CNNs
- Contact Us