Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8285855
Mention release date
ahmedfgad Apr 8, 2026
30c8e3c
Refactor the validation logic
ahmedfgad Apr 9, 2026
0a518fe
Update validate mutation params
ahmedfgad Apr 9, 2026
260be03
Update validated parent selection params
ahmedfgad Apr 9, 2026
042b886
Move best_solutions validation to footer
ahmedfgad Apr 9, 2026
2f7914c
on_start error indicate 1 parameter
ahmedfgad Apr 9, 2026
b58d524
Enhancements
ahmedfgad Apr 12, 2026
7627dcb
more tests
ahmedfgad Apr 12, 2026
3c8a62b
Add dependencies for testing
ahmedfgad Apr 12, 2026
69deb4c
Separate keras and tensorflow
ahmedfgad Apr 12, 2026
ad1f5a1
Exclude tensorflow from Python 3.14
ahmedfgad Apr 12, 2026
d79b396
ignore python 3.8
ahmedfgad Apr 12, 2026
8e3c461
Return weights as list
ahmedfgad Apr 12, 2026
5d0de53
Return as NumPy array
ahmedfgad Apr 12, 2026
1c1c8fb
Remove pyc files
ahmedfgad Apr 12, 2026
9165b5c
Safe TensorFlow installation
ahmedfgad Apr 13, 2026
da471cc
Pass shape
ahmedfgad Apr 13, 2026
c112afb
Update license info
ahmedfgad Apr 13, 2026
579b006
Revert back license to work with Py 3.8
ahmedfgad Apr 13, 2026
c76e47f
Merge pull request #353 from ahmedfgad/master
ahmedfgad Apr 29, 2026
b682ec3
Fix inconsistent lists
ahmedfgad May 21, 2026
1937553
docs: build from Markdown (MyST), switch to Furo, add diagrams, regro…
ahmedfgad May 21, 2026
c69aef9
docs: plain-English pass on pygad_more.md (first half)
ahmedfgad May 21, 2026
7431422
docs: finish plain-English pass on pygad_more.md (second half)
ahmedfgad May 21, 2026
59d68d7
docs: plain-English pass on utils.md (engine, validation, mutation, p…
ahmedfgad May 21, 2026
54f1399
docs: finish plain-English pass on pygad.md (examples section)
ahmedfgad May 21, 2026
6baf5b5
docs: plain-English pass on cnn.md and gacnn.md
ahmedfgad May 21, 2026
3e8f7df
docs: plain-English pass on nn, gann, kerasga, torchga (intros and st…
ahmedfgad May 21, 2026
b8464c6
docs: plain-English pass on gann/gacnn/torchga bodies
ahmedfgad May 21, 2026
d37e878
docs: final sweep on nn/cnn/gacnn/helper plus recurring fixes
ahmedfgad May 21, 2026
0b67551
docs: finish plain-English pass on nn.md (functions, helpers, steps)
ahmedfgad May 21, 2026
cd07722
docs: polish kerasga/torchga example prose
ahmedfgad May 21, 2026
2406aa7
docs: split the releases page into Release History and Help & Resources
ahmedfgad May 21, 2026
6959c25
docs: split pygad_more into a landing page and six themed pages
ahmedfgad May 21, 2026
f5477e3
docs: split the long ML tutorial pages into one page per example
ahmedfgad May 21, 2026
e6940a9
docs: document passing a class for the fitness, operators, and callbacks
ahmedfgad May 21, 2026
bfa230a
docs: rename the custom-functions page title to include classes
ahmedfgad May 21, 2026
8df05b4
docs: scroll the Furo sidebar to the active page
ahmedfgad May 21, 2026
247a379
docs: split pygad, utils, and help into focused pages
ahmedfgad May 21, 2026
5e3c4a2
docs: turn the pygad.GA constructor reference into grouped collapsibles
ahmedfgad May 21, 2026
9905940
docs: format the pygad.GA parameter descriptions for readability
ahmedfgad May 21, 2026
af58ca2
docs: fix the English of comments and docstrings across the pygad pac…
ahmedfgad May 21, 2026
3b98796
docs: fix English in exception/warning messages and code-identifier t…
ahmedfgad May 21, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
# The Strategy Matrix defines the environments to test.
# GitHub Actions will spawn a separate, parallel job for each version in the list.
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]

Expand All @@ -35,19 +36,41 @@ jobs:

steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Setup the specific Python version defined in the current matrix iteration.
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

# Install project dependencies from requirements.txt.
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Added timeout and no-cache to prevent SSL/Network decryption errors
pip install --default-timeout=100 --no-cache-dir -r requirements.txt

if [ "${{ matrix.python-version }}" != "3.14" ] && [ "${{ matrix.python-version }}" != "3.8" ]; then
# This block runs if the version IS NOT 3.14 or 3.8
# Using a retry loop for large DL frameworks to handle transient network issues
echo "Installing deep learning libraries."
for i in {1..3}; do
pip install --default-timeout=100 --no-cache-dir tensorflow && break || \
(echo "Retry $i failed, waiting 10s..." && sleep 10)
done
pip install --upgrade keras
pip install torch
else
# This block runs if the version IS 3.14 or 3.8
echo "Skipping heavy deep learning libraries for Python ${{ matrix.python-version }}."
fi

# Verify the core deep learning frameworks
echo "Verifying installations..."
python -c "import tensorflow; print('TensorFlow version:', tensorflow.__version__)" || echo "TensorFlow not installed"
python -c "import keras; print('Keras version:', keras.__version__)" || echo "Keras not installed"
python -c "import torch; print('PyTorch version:', torch.__version__)" || echo "PyTorch not installed"

# Build the PyGAD package distribution (generating .tar.gz and .whl files).
# This ensures the package build process is valid on this Python version.
Expand All @@ -59,7 +82,7 @@ jobs:
# Install the newly built .whl file to verify the package is installable.
- name: Install PyGAD from Wheel
run: |
find ./dist/*.whl | xargs pip install
pip install dist/*.whl

- name: Install PyTest
run: pip install pytest
Expand All @@ -69,4 +92,8 @@ jobs:
# This includes our new tests for visualization, operators, parallel processing, etc.
- name: Run Tests
run: |
pytest
if [ "${{ matrix.python-version }}" == "3.14" ] || [ "${{ matrix.python-version }}" == "3.8" ]; then
pytest --ignore=tests/test_kerasga.py --ignore=tests/test_torchga.py
else
pytest
fi
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@
__pycache__/
*.py[cod]
*$py.class

# =============================================================================
# Documentation build output and local doc-build virtual environments.
# These are generated by Sphinx and should not be committed.
# =============================================================================
docs/build/
.venv-docs/
9 changes: 4 additions & 5 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ formats:
- pdf
- epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# Declare the Python requirements needed to build the documentation.
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
# python:
# install:
# - requirements: docs/requirements.txt
python:
install:
- requirements: docs/requirements.txt
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# PyGAD: Genetic Algorithm in Python
# PyGAD: Genetic Algorithm in Python

[PyGAD](https://pypi.org/project/pygad) is an open-source easy-to-use Python 3 library for building the genetic algorithm and optimizing machine learning algorithms. It supports Keras and PyTorch. PyGAD supports optimizing both single-objective and multi-objective problems.

> Try the [Optimization Gadget](https://optimgadget.com), a free cloud-based tool powered by PyGAD. It simplifies optimization by reducing or eliminating the need for coding while providing insightful visualizations.
> Try the [Optimization Gadget](https://optimgadget.com), a free cloud-based tool powered by PyGAD. It makes optimization easier by reducing or removing the need for coding, and it shows helpful visualizations.

Check documentation of the [PyGAD](https://pygad.readthedocs.io/en/latest).
Read the [PyGAD documentation](https://pygad.readthedocs.io/en/latest).

[![PyPI Downloads](https://pepy.tech/badge/pygad)](https://pepy.tech/project/pygad) [![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/pygad.svg?label=Conda%20Downloads)](
https://anaconda.org/conda-forge/PyGAD) [![PyPI version](https://badge.fury.io/py/pygad.svg)](https://badge.fury.io/py/pygad)![Docs](https://readthedocs.org/projects/pygad/badge)[![PyGAD PyTest / Python 3.13](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py313.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py313.yml) [![PyGAD PyTest / Python 3.12](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py312.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py312.yml) [![PyGAD PyTest / Python 3.11](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py311.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py311.yml) [![PyGAD PyTest / Python 3.10](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py310.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py310.yml) [![PyGAD PyTest / Python 3.9](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py39.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py39.yml) [![PyGAD PyTest / Python 3.8](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py38.yml/badge.svg)](https://github.com/ahmedfgad/GeneticAlgorithmPython/actions/workflows/main_py38.yml) [![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Translation](https://hosted.weblate.org/widgets/weblate/-/svg-badge.svg)](https://hosted.weblate.org/engage/weblate/) [![REUSE](https://api.reuse.software/badge/github.com/WeblateOrg/weblate)](https://api.reuse.software/info/github.com/WeblateOrg/weblate) [![Stack Overflow](https://img.shields.io/badge/stackoverflow-Ask%20questions-blue.svg)](
https://stackoverflow.com/questions/tagged/pygad) [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/ahmedfgad/GeneticAlgorithmPython/badge)](https://securityscorecards.dev/viewer/?uri=github.com/ahmedfgad/GeneticAlgorithmPython) [![DOI](https://zenodo.org/badge/DOI/10.1007/s11042-023-17167-y.svg)](https://doi.org/10.1007/s11042-023-17167-y)

![PYGAD-LOGO](https://user-images.githubusercontent.com/16560492/101267295-c74c0180-375f-11eb-9ad0-f8e37bd796ce.png)

[PyGAD](https://pypi.org/project/pygad) supports different types of crossover, mutation, and parent selection. [PyGAD](https://pypi.org/project/pygad) allows different types of problems to be optimized using the genetic algorithm by customizing the fitness function.
[PyGAD](https://pypi.org/project/pygad) supports different types of crossover, mutation, and parent selection. It lets you optimize many types of problems with the genetic algorithm by writing your own fitness function.

The library is under active development and more features are added regularly. If you want a feature to be supported, please check the **Contact Us** section to send a request.

Expand All @@ -25,19 +25,19 @@ The library is under active development and more features are added regularly. I

# Installation

To 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 is at PyPI at this page https://pypi.org/project/pygad.
To install [PyGAD](https://pypi.org/project/pygad), use pip to download and install the library from [PyPI](https://pypi.org/project/pygad) (Python Package Index). The library is available on PyPI at this page: https://pypi.org/project/pygad.

Install PyGAD with the following command:

```python
```
pip install pygad
```

To get started with PyGAD, please read the documentation at [Read The Docs](https://pygad.readthedocs.io/) https://pygad.readthedocs.io.
To get started with PyGAD, read the documentation at [Read the Docs](https://pygad.readthedocs.io).

# PyGAD Source Code

The source code of the PyGAD' modules is found in the following GitHub projects:
The source code of the PyGAD modules is in the following GitHub projects:

- [pygad](https://github.com/ahmedfgad/GeneticAlgorithmPython): (https://github.com/ahmedfgad/GeneticAlgorithmPython)
- [pygad.nn](https://github.com/ahmedfgad/NumPyANN): https://github.com/ahmedfgad/NumPyANN
Expand All @@ -47,13 +47,11 @@ The source code of the PyGAD' modules is found in the following GitHub projects:
- [pygad.kerasga](https://github.com/ahmedfgad/KerasGA): https://github.com/ahmedfgad/KerasGA
- [pygad.torchga](https://github.com/ahmedfgad/TorchGA): https://github.com/ahmedfgad/TorchGA

The documentation of PyGAD is available at [Read The Docs](https://pygad.readthedocs.io/) https://pygad.readthedocs.io.

# PyGAD Documentation

The documentation of the PyGAD library is available at [Read The Docs](https://pygad.readthedocs.io) at this link: https://pygad.readthedocs.io. It discusses the modules supported by PyGAD, all its classes, methods, attribute, and functions. For each module, a number of examples are given.
The PyGAD documentation is available at [Read the Docs](https://pygad.readthedocs.io) at this link: https://pygad.readthedocs.io. It explains the modules supported by PyGAD and all its classes, methods, attributes, and functions. For each module, several examples are given.

If there is an issue using PyGAD, feel free to post at issue in this [GitHub repository](https://github.com/ahmedfgad/GeneticAlgorithmPython) https://github.com/ahmedfgad/GeneticAlgorithmPython or by sending an e-mail to ahmed.f.gad@gmail.com.
If you have an issue using PyGAD, feel free to post an issue in this [GitHub repository](https://github.com/ahmedfgad/GeneticAlgorithmPython) or send an e-mail to ahmed.f.gad@gmail.com.

If you built a project that uses PyGAD, then please drop an e-mail to ahmed.f.gad@gmail.com with the following information so that your project is included in the documentation.

Expand All @@ -65,7 +63,7 @@ Please check the **Contact Us** section for more contact details.

# Life Cycle of PyGAD

The next figure lists the different stages in the lifecycle of an instance of the `pygad.GA` class. Note that PyGAD stops when either all generations are completed or when the function passed to the `on_generation` parameter returns the string `stop`.
The next figure shows the main stages in the life cycle of a `pygad.GA` instance. PyGAD stops when all generations are completed or when the function passed to the `on_generation` parameter returns the string `stop`.

![PyGAD Lifecycle](https://user-images.githubusercontent.com/16560492/220486073-c5b6089d-81e4-44d9-a53c-385f479a7273.jpg)

Expand Down Expand Up @@ -122,7 +120,7 @@ ga_instance = pygad.GA(num_generations=3,
ga_instance.run()
```

Based on the used 3 generations as assigned to the `num_generations` argument, here is the output.
Because `num_generations` is set to 3, here is the output.

```
on_start()
Expand Down Expand Up @@ -158,7 +156,7 @@ import numpy

"""
Given the following function:
y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6
y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + w6x6
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=44
What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function.
"""
Expand All @@ -168,7 +166,7 @@ desired_output = 44 # Function output.

def fitness_func(ga_instance, solution, solution_idx):
# Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its corresponding weight.
# The fitness function calculates the sum of products between each input and its corresponding weight.
output = numpy.sum(solution*function_inputs)
fitness = 1.0 / numpy.abs(output - desired_output)
return fitness
Expand Down Expand Up @@ -203,7 +201,7 @@ ga_instance = pygad.GA(num_generations=num_generations,
# Running the GA to optimize the parameters of the function.
ga_instance.run()

# After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations.
# After the generations complete, a plot is shown that summarizes how the fitness values evolve over the generations.
ga_instance.plot_fitness()

# Returning the details of the best solution.
Expand All @@ -229,7 +227,7 @@ loaded_ga_instance.plot_fitness()

# For More Information

There are different resources that can be used to get started with the genetic algorithm and building it in Python.
Here are some resources to help you get started with the genetic algorithm and build it in Python.

## Tutorial: Implementing Genetic Algorithm in Python

Expand All @@ -239,7 +237,7 @@ To start with coding the genetic algorithm, you can check the tutorial titled [*
- [Towards Data Science](https://towardsdatascience.com/genetic-algorithm-implementation-in-python-5ab67bb124a6)
- [KDnuggets](https://www.kdnuggets.com/2018/07/genetic-algorithm-implementation-python.html)

[This tutorial](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) is prepared based on a previous version of the project but it still a good resource to start with coding the genetic algorithm.
[This tutorial](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad) is based on an earlier version of the project, but it is still a good resource to start coding the genetic algorithm.

[![Genetic Algorithm Implementation in Python](https://user-images.githubusercontent.com/16560492/78830052-a3c19300-79e7-11ea-8b9b-4b343ea4049c.png)](https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad)

Expand Down
Loading
Loading