Open-source optimization for Python

Let evolution
find a better solution.

Build genetic algorithms, search complex solution spaces, and optimize machine-learning models with a library that stays out of your way.

pip install pygad

Python 3.8+ · BSD-3-Clause · Lightweight core

SEARCH SPACEILLUSTRATION
A genetic algorithm searching for a better solutionCandidate solutions move across a fitness landscape toward a higher-scoring result. candidate solutionsfitness0120406080
GENERATION80
BEST SOLUTIONImproving ↗

An illustrative view of candidates evolving toward better fitness.

⌘ Python-first API↗ Single & multi-objective◉ Open source◎ Keras & PyTorch tools

Built for real search problems

Flexible by design.
Simple to get moving.

Start with a fitness function. Shape the search with the operators and constraints your problem needs.

02 / CUSTOMIZE

Make the search yours

Choose parent selection, crossover and mutation methods; add custom operators, constraints and gene spaces.

See the available operators ↗
03 / INTEGRATE

Optimize ML models

Use Keras and PyTorch integrations to represent model parameters as chromosomes and search for better weights.

Explore Keras integration ↗
04 / SCALE

Spend compute wisely

Batch fitness evaluations, reuse fitness values, or parallelize intensive calculations with threads or processes.

Read about performance ↗

Your first experiment

Define what
“better” means.

Write a fitness function for your problem, set the search size, and let PyGAD evolve candidate solutions.

1

Installpip install pygad

2

Define fitnessScore a candidate solution

3

Run and inspectExplore the best solution found

Follow the getting-started guide →
optimize.py
# optimize.py
import pygad

def fitness_func(ga_instance, solution, solution_idx):
    return 1.0 / (abs(sum(solution) - 42) + 1e-6)

ga = pygad.GA(
    num_generations=100,
    num_parents_mating=4,
    fitness_func=fitness_func,
    sol_per_pop=10,
    num_genes=6,
)
ga.run()

solution, fitness, _ = ga.best_solution()
print(solution, fitness)

Keep exploring

Good work starts
with a good reference.

Read the guides, browse working examples, or join the conversation around PyGAD.

PyGAD meets Vilvik

From Python code
to an evolving result.

Bring your PyGAD optimization into Vilvik’s cloud workspace. Run experiments, compare generations, tune the search, and keep the results together.

Explore Vilvik
OPTIMIZATION ENGINEPyGADPython genetic algorithms
CLOUD WORKSPACEVilvikRun · compare · refine
01
Define the goalPython fitness function
02
Agent learnsEvaluate, select, evolve
03
Compare resultsHistory & best solution
GENERATION FITNESSBEST CANDIDATE
StartFitness improves over successive generationsBest

A connected workflow for running and refining PyGAD experiments in Vilvik.

Open source moves forward together

Use it. Share it.
Help it grow.

PyGAD is available under the BSD-3-Clause license. If it helps your work, you can support its continued development.