PyGAD Documentation

repository·master·Indexed 24 days ago

https://github.com/ahmedfgad/geneticalgorithmpython

PyGAD is an open-source Python 3 library for building genetic algorithms and optimizing machine learning models. It supports single-objective and multi-objective problems and provides integrations for training Keras and PyTorch models via pygad.kerasga and pygad.torchga. The library includes features for custom initial populations, lifecycle callbacks, and visualization tools for tracking fitness evolution.

Tokens
73.9K
Snippets
102
Records
258
Agent score
79%

What's inside PyGAD

  1. Overview of GeneticAlgorithmPython tutorial implementation

    master

    This repository contains a primitive implementation of a Genetic Algorithm (GA) as part of a tutorial. It is designed for educational purposes to demonstrate core GA operations like mutation and crossover.

    Key characteristics:

    • Uses decimal representation for chromosomes (binary representation might be preferred for other specific problems).
    • Provides a basic implementation of GA operations in ga.py.
    • Includes an example file demonstrating how to use the ga.py implementation.

    Note: This is a simplified implementation. For production-grade features and extensive GA variations, use the PyGAD library instead.

  2. Overview of PyGAD Modules

    master

    PyGAD is organized into several specialized modules:

    • pygad: The main interface for building genetic algorithms.
    • nn: For building artificial neural networks.
    • gann: For optimizing neural networks (classification/regression) using genetic algorithms.
    • cnn: For building convolutional neural networks.
    • gacnn: For optimizing convolutional neural networks using genetic algorithms.
    • kerasga: For training Keras models using genetic algorithms.
    • torchga: For training PyTorch models using genetic algorithms.
    • visualize: For visualizing optimization results.
    • utils: Contains operators (crossover, mutation, parent selection) and NSGA-II/NSGA-III implementations.
    • helper: Contains various helper functions.
  3. Explore PyGAD sub-projects and integrations

    master

    PyGAD is composed of several specialized open-source projects and provides integrations for training deep learning models. Key components include:

    • GeneticAlgorithmPython: The core NumPy-based implementation of the genetic algorithm.
    • NeuralGenetic: Uses GeneticAlgorithmPython and NumPyANN to train artificial neural networks.
    • CNNGenetic: Uses GeneticAlgorithmPython to train convolutional neural networks.
    • KerasGA: An integration for training Keras models using genetic algorithms.
    • TorchGA (pygad.torchga): An integration for training PyTorch models using genetic algorithms.

    Other related NumPy-based research projects include NumPyANN (forward pass for neural networks) and NumPyCNN (forward pass for convolutional neural networks).

  4. How adaptive mutation works in PyGAD

    master

    Adaptive mutation adjusts the mutation rate based on a solution's fitness relative to the population average (f_avg). This prevents high-quality solutions from being disrupted by excessive mutation while encouraging low-quality solutions to explore the parameter space more thoroughly.

    The Logic:

    1. Low-quality solutions (f < f_avg): Assigned a higher mutation rate to increase search thoroughness.
    2. High-quality solutions (f >= f_avg): Assigned a lower mutation rate to preserve beneficial genetic traits.

    In PyGAD, if a solution's fitness exactly equals the average, it is treated as high quality.

  5. How GANN population and networks work together

    master

    In pygad.gann.GANN, there is a distinction between the genetic algorithm's population and the neural network objects:

    • GANN_instance.population_networks: This holds references to the actual neural network objects. It does not hold raw weights directly.
    • ga_instance.population: This is the list of 'solutions' used by the Genetic Algorithm. In the context of GANN, a solution is a reference to the last layer of a network in the population.
    • Mapping: If you have a population of 3 networks, ga_instance.population will contain 3 elements, each being a reference to the last layer of the corresponding network in GANN_instance.population_networks. You use these references to access network details or perform predictions.

    To synchronize the weights after training generations, use pygad.gann.population_as_matrices to convert the population into weight matrices and then call GANN_instance.update_population_trained_weights().

  6. Monitor the PyGAD lifecycle and population state

    master

    When working with a pygad.GA instance, you can inspect several attributes to monitor the progress of the algorithm:

    Lifecycle Attributes

    • generations_completed: The number of the last completed generation.
    • run_completed: True if the run() method finished gracefully.
    • valid_parameters: True if all constructor parameters passed validation.
    • logger: A standard Python logging module object (available in PyGAD 3.0.0+).

    Population and Fitness Attributes

    • population: A NumPy array representing the current population.
    • pop_size: A (sol_per_pop, num_genes) tuple describing the population shape.
    • last_generation_fitness: Fitness values of the solutions in the most recent generation.
    • best_solutions_fitness: A list of the best-solution fitness values recorded per generation.
    • best_solution_generation: The generation index where the best fitness was achieved (returns -1 until run() completes).
  7. Understand why the fitness function is skipped for certain solutions

    master

    By default, PyGAD uses elitism to preserve the best solutions from one generation to the next. The keep_elitism parameter (defaulting to 1) defines how many of the best solutions from generation X are copied directly into generation X+1 at the beginning of the population (indices 0, 1, etc.).

    Because these solutions are copied without changes, PyGAD reuses their previously calculated fitness values instead of calling the fitness_func again. This is why you might notice that the fitness function is not called for the solution at index 0.

    To force PyGAD to call the fitness function for every single solution in every generation, you must configure the following:

    • Set keep_elitism=0
    • Set keep_parents=0
    • Set save_solutions=False
    • Set save_best_solutions=False
    ga_instance = pygad.GA(...,
                           keep_elitism=0,
                           keep_parents=0,
                           save_solutions=False,
                           save_best_solutions=False,
                           ...)
  8. Understand the `pygad.nn` module purpose

    master
    The pygad.nn module is used to create artificial neural networks by implementing the forward pass only. It does not include a training algorithm itself. Instead, it builds network layers and implements activation functions. To train a network created with pygad.nn, you must use the pygad.gann module, which applies a genetic algorithm to the network.
  9. Use Adaptive Mutation and User-Defined Operators

    master

    PyGAD allows for advanced control over the genetic algorithm's behavior through two main mechanisms:

    1. Adaptive Mutation: This allows you to change the mutation rate per solution based on its fitness, helping the algorithm balance exploration and exploitation.
    2. User-Defined Operators: You can plug in your own custom crossover, mutation, and parent selection methods to tailor the algorithm to specific problem domains.
  10. Configure the initial population in PyGAD

    master

    There are two ways to prepare the initial population for a pygad.GA instance:

    1. Custom Population: Prepare your own population and pass it to the initial_population parameter. This is useful when you want to start the genetic algorithm with a specific set of solutions.
    2. Automatic Generation: Assign valid integer values to the sol_per_pop (number of solutions per population) and num_genes (number of genes per solution) parameters.

    Note: If the initial_population parameter is used, the sol_per_pop and num_genes parameters are ignored.