Registry / ai-ml / pygad
library3.7.0pypypi✓ verified 88d ago

PyGAD is a Python library for building genetic algorithms and training machine learning models (Keras & PyTorch). Current version 3.6.0, released March 2025, with a cadence of several minor releases per year. Supports single- and multi-objective optimization, custom operators, and integration with deep learning frameworks.

pip install pygad
INSTALL
IMPORT
SIG · PYGAD
P
pygad
ai-mlpythonv3.7.0
Install
3.7s avg
Import
312ms
Disk
90MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v3.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.306s · 90.9MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.7s · import 0.318s · 87MB
90MB installed
● package 90MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GA
✓ import pygad
GA is the class name, but you import the module and use pygad.GA.

Minimal example: maximize sum of binary array (genetic algorithm optimization).

import pygad import numpy as np # Define fitness function (must accept two arguments: solution and solution_index) def fitness_func(solution, solution_idx): return -np.sum(solution) # Define gene space num_genes = 6 gene_space = {'low': 0, 'high': 2, 'step': 1} # integers 0 or 1 # Create GA instance ga_instance = pygad.GA( num_generations=10, num_parents_mating=4, fitness_func=fitness_func, sol_per_pop=8, num_genes=num_genes, gene_space=gene_space, parent_selection_type="sss", keep_parents=1, crossover_type="single_point", mutation_type="random", mutation_num_genes=1 ) # Run GA ga_instance.run() # Show best solution solution, solution_fitness, _ = ga_instance.best_solution() print(f"Best solution: {solution}") print(f"Fitness: {solution_fitness}")
Debug
Known issues
breakingIn PyGAD 3.0.0+, the fitness function signature changed: it now receives two arguments (function, solution_idx). The first argument is the solution array, the second is the solution index.
fix
Update fitness function from fitness_func(solution) to fitness_func(solution, solution_idx).
affects: <3.0.0
gotchaWhen using 'gene_space' with a step, the gene type is inferred: if step is integer, genes are integers; if step is float, genes are floats. Mixing types can cause unexpected results.
fix
Ensure gene_space step matches desired data type.
affects: all
deprecatedThe 'delay_after_gen' parameter was removed in v3.4.0. Use 'on_generation' callback to add delay.
fix
Replace delay_after_gen with a custom on_generation function containing time.sleep().
affects: >=3.4.0
Errors
Common errors & fixes
TypeError: 'GA' object is not callable
In versions <3.0.0, the GA class was instantiated directly; in 3.0.0+, the module import changed.
fix
Use 'import pygad' and then 'ga = pygad.GA(...)'.
Fitness function must have 2 arguments, but it has 1
After PyGAD 3.0.0, fitness function signature changed to (solution, solution_idx).
fix
Change your fitness function to accept a second argument: def fitness_func(solution, solution_idx):
Upgrade
Version history
3.7.0latest on PyPI · released Jun 5, 2026
Audit
Dependencies
numpyrequiredCore dependency for numerical operations.
pygadrequiredLibrary itself.
Agent activity
19 hits · last 30 days
node
14
OpenAI (training)
1
Resources
pygad — pip install pygad · libregistry