Skip to main content

PyTorch-based framework for differentiable evolutionary computation and swarm intelligence

Project description

EvoGrad: Metaheuristics in a Differentiable Wonderland

Python 3.9+ PyTorch 2.0+ License: Apache-2.0

EvoGrad is a PyTorch-based framework for differentiable Evolutionary Computation and Swarm Intelligence. It bridges classical population-based optimisation with modern differentiable programming by enabling gradient flow through evolutionary operators.

๐ŸŒŸ Key Features

  • Fully Differentiable: All operators support gradient computation via reparameterisation tricks (Gumbel-Softmax, Binary-Concrete, pathwise gradients)
  • GPU Accelerated: Native PyTorch implementation for seamless CPU/GPU/MPS execution
  • Modular Design: Dependency injection pattern inspired by pymoo for flexible operator composition
  • Learnable Hyperparameters: Automatically tune algorithm parameters via backpropagation
  • Four Algorithms: GA, DE, PSO, and CMA-ES with multiple variants

๐Ÿ“ฆ Installation

# From PyPI (the import name is `evograd`)
pip install evograd-diff

Or install directly from the repository:

pip install "git+https://github.com/andreatangherloni/EvoGrad.git"

For local development:

git clone https://github.com/andreatangherloni/EvoGrad.git
cd EvoGrad
pip install -e .

๐Ÿš€ Quick Start

import torch
from evograd.core import Problem, minimize, MaxEvaluations
from evograd.algorithms import GA, DE, PSO, CMAES

# Define an optimisation problem
problem = Problem(
    objective=lambda x: (x**2).sum(dim=-1),  # Sphere function
    n_var=30,
    xl=-100.0,
    xu=100.0,
)

# Run with Genetic Algorithm
ga = GA(pop_size=100, differentiable=True)
result = minimize(problem, ga, termination=MaxEvaluations(10000), seed=42)
print(f"GA Best: {result.best_fitness:.6f}")

# Run with Differential Evolution
de = DE(pop_size=100, variant="DE/rand/1/bin", adaptive=True)
result = minimize(problem, de, termination=MaxEvaluations(10000), seed=42)
print(f"DE Best: {result.best_fitness:.6f}")

# Run with Particle Swarm Optimisation
pso = PSO(pop_size=100, adaptive=True, differentiable=True)
result = minimize(problem, pso, termination=MaxEvaluations(10000), seed=42)
print(f"PSO Best: {result.best_fitness:.6f}")

# Run with CMA-ES
cmaes = CMAES(sigma=0.5, adaptive=True)
result = minimize(problem, cmaes, termination=MaxEvaluations(10000), seed=42)
print(f"CMA-ES Best: {result.best_fitness:.6f}")

๐Ÿ”ง Algorithms and Operating Modes

Genetic Algorithm (GA)

The GA uses operator-level differentiability. Each operator (selection, crossover, mutation, survival) can independently be set to differentiable mode:

from evograd.algorithms import GA
from evograd.operators import (
    RouletteSelection,
    SBXCrossover,
    PolynomialMutation,
    MergeSurvival,
)

# Classical GA (no gradients)
ga = GA(pop_size=100, differentiable=False)

# Fully differentiable GA with custom operators
ga = GA(
    pop_size=100,
    selection=RouletteSelection(differentiable=True, learn_temperature=True),
    crossover=SBXCrossover(differentiable=True, learn_eta=True, learn_prob=True),
    mutation=PolynomialMutation(differentiable=True, learn_eta=True, learn_prob=True),
    survival=MergeSurvival(selection=RouletteSelection(differentiable=True)),
    differentiable=True,  # Makes population learnable
)
Parameter Effect
differentiable=False Classical GA with discrete operators
differentiable=True Population is an nn.Parameter (learnable via backprop)
Operator differentiable=True Operator uses Gumbel-Softmax/Binary-Concrete for gradient flow
Operator learn_*=True Operator hyperparameters become learnable nn.Parameter

Differential Evolution (DE)

DE uses algorithm-level flags for adaptive hyperparameters and differentiable population:

from evograd.algorithms import DE, de_rand_1_bin, de_best_1_bin

# Classical DE
de = DE(pop_size=100, variant="DE/rand/1/bin", F=0.5, CR=0.9)

# Adaptive DE (learnable F, CR, selection temperature)
de = DE(pop_size=100, variant="DE/best/1/bin", adaptive=True)

# Differentiable population
de = DE(pop_size=100, variant="DE/rand/1/bin", differentiable=True)

# Both adaptive and differentiable
de = DE(pop_size=100, variant="DE/current-to-best/1/bin", adaptive=True, differentiable=True)
adaptive differentiable Effect
False False Classical DE
True False F, CR, temperatures learnable via backprop
False True Population learnable via backprop
True True Both hyperparameters and population learnable

Supported Variants:

  • DE/rand/1/bin, DE/rand/1/exp, DE/rand/2/bin, DE/rand/2/exp
  • DE/best/1/bin, DE/best/1/exp, DE/best/2/bin, DE/best/2/exp
  • DE/current-to-best/1/bin, DE/current-to-best/1/exp
  • DE/current-to-rand/1

Particle Swarm Optimisation (PSO)

PSO uses the same algorithm-level flags as DE:

from evograd.algorithms import PSO, pso_constriction, pso_adaptive

# Classical PSO
pso = PSO(pop_size=100, inertia=0.7, c1=1.5, c2=1.5)

# Adaptive PSO (learnable inertia, c1, c2)
pso = PSO(pop_size=100, adaptive=True)

# Per-particle adaptive coefficients
pso = PSO(pop_size=100, adaptive=True, per_particle_coeffs=True)

# Constriction factor PSO
pso = pso_constriction(pop_size=100)

# Fully differentiable
pso = PSO(pop_size=100, adaptive=True, differentiable=True)
adaptive differentiable Effect
False False Classical PSO
True False Inertia, c1, c2 learnable via backprop
False True Particle positions learnable via backprop
True True Both coefficients and positions learnable

CMA-ES

CMA-ES supports adaptive coefficients and restart strategies (IPOP/BIPOP):

from evograd.algorithms import CMAES, cmaes_ipop, cmaes_bipop

# Classical CMA-ES
cmaes = CMAES(pop_size=50, sigma=0.5)

# Adaptive CMA-ES (learnable cc, cs, c1, cmu, damps)
cmaes = CMAES(pop_size=50, sigma=0.5, adaptive=True)

# Differentiable mean
cmaes = CMAES(pop_size=50, sigma=0.5, differentiable=True)

# IPOP-CMA-ES (increasing population restarts)
cmaes = cmaes_ipop(restarts=9, incpopsize=2)

# BIPOP-CMA-ES (alternating small/large populations)
cmaes = cmaes_bipop(restarts=9)
adaptive differentiable Effect
False False Classical CMA-ES
True False Adaptation coefficients learnable via backprop
False True Distribution mean ฮผ learnable via backprop
True True Both coefficients and mean learnable

Restart Strategies:

  • IPOP: Restart with doubled population after convergence
  • BIPOP: Alternate between small (focused) and large (exploratory) populations

๐Ÿ“š Operators Library

EvoGrad provides a comprehensive library of evolutionary operators:

Selection

Operator Description Differentiable
RandomSelection Uniform random selection โœ—
RouletteSelection Fitness-proportionate (Gumbel-Softmax) โœ“
TournamentSelection Tournament with soft winner (Gumbel-Softmax) โœ“
RankSelection Rank-based probabilities โœ“
StochasticUniversalSampling SUS with soft selection โœ“

Crossover

Operator Description Differentiable
SBXCrossover Simulated Binary Crossover โœ“
BinomialCrossover DE-style binomial โœ“
ExponentialCrossover DE-style exponential โœ“
BlendCrossover BLX-ฮฑ crossover โœ“
ArithmeticCrossover Weighted average โœ“
UniformCrossover Gene-wise uniform swap โœ“
SimulatedBinaryCrossover Alias for SBX โœ“

Mutation

Operator Description Differentiable
PolynomialMutation Polynomial bounded mutation โœ“
GaussianMutation Additive Gaussian noise โœ“
UniformMutation Uniform random replacement โœ“
AdaptiveMutation Self-adaptive mutation rates โœ“

Survival

Operator Description
MergeSurvival (ฮผ+ฮป) with optional elitism
ReplacementSurvival (ฮผ,ฮป) generational replacement
AgingSurvival Age-based replacement
FitnessSurvival Pure fitness-based truncation

Repair

Operator Description
BoundsRepair Clamp to bounds
ReflectionRepair Bounce off boundaries
WrapRepair Toroidal wrap-around
RandomRepair Random resampling

๐ŸŽฏ Advanced Usage

Training Neural Networks with EvoGrad

import torch
import torch.nn as nn
from evograd.algorithms import CMAES
from evograd.core import Problem
from evograd.core.termination import MaxEvaluations


# Define a simple MLP
class MLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(10, 64),
            nn.Tanh(),
            nn.Linear(64, 1),
        )
    
    def forward(self, x):
        return self.net(x)

# Flatten parameters for optimisation
model = MLP()
n_params = sum(p.numel() for p in model.parameters())

def loss_fn(params):
    # Reshape flat params back to model
    idx = 0
    for p in model.parameters():
        numel = p.numel()
        p.data.copy_(params[idx:idx+numel].view(p.shape))
        idx += numel
    
    # Compute loss on dummy data
    x = torch.randn(32, 10)
    y = torch.randn(32, 1)
    pred = model(x)
    return ((pred - y)**2).mean()

# Batch evaluation
def batch_loss(pop):
    return torch.stack([loss_fn(p) for p in pop])

problem = Problem(
    objective=batch_loss,
    n_var=n_params,
    xl=-10.0,
    xu=10.0,
)

cmaes = CMAES(pop_size=50, sigma=1.0, adaptive=True)
result = minimize(problem, cmaes, MaxEvaluations(10000))
print(f"Final loss: {result.best_fitness:.6f}")

Callbacks for Logging

from evograd.core import minimize
from evograd.utils import HistoryCallback, PrintCallback

callbacks = [
    PrintCallback(every=10),  # Print progress every 10 generations
    HistoryCallback(),        # Record full history
]

result = minimize(problem, algorithm, termination=MaxEvaluations(10000), callback=callbacks)

# Access history
print(f"Fitness over time: {result.history['best_fitness']}")

๐Ÿ—๏ธ Architecture

evograd/
โ”œโ”€โ”€ algorithms/
โ”‚   โ””โ”€โ”€ cmaes.py          # CMA-ES with IPOP/BIPOP
โ”‚   โ”œโ”€โ”€ de.py             # Differential Evolution
โ”‚   โ”œโ”€โ”€ ga.py             # Genetic Algorithm
โ”‚   โ”œโ”€โ”€ pso.py            # Particle Swarm Optimisation
โ”œโ”€โ”€ core/
โ”‚   โ”œโ”€โ”€ algorithm.py      # Base Algorithm class
โ”‚   โ”œโ”€โ”€ maximize.py       # Optimisation loop (maximisation)
โ”‚   โ”œโ”€โ”€ minimize.py       # Optimisation loop (minimisation)
โ”‚   โ”œโ”€โ”€ problem.py        # Problem definition
โ”‚   โ”œโ”€โ”€ result.py         # Result container
โ”‚   โ””โ”€โ”€ termination.py    # Stopping criteria
โ”œโ”€โ”€ operators/
โ”‚   โ”œโ”€โ”€ crossover.py      # Crossover operators
โ”‚   โ”œโ”€โ”€ mutation.py       # Mutation operators
โ”‚   โ”œโ”€โ”€ sampling.py       # Sampling operators
โ”‚   โ”œโ”€โ”€ selection.py      # Selection operators
โ”‚   โ”œโ”€โ”€ survival.py       # Survival/replacement
โ”‚   โ””โ”€โ”€ repair.py         # Constraint handling
โ””โ”€โ”€ utils/
    โ”œโ”€โ”€ callbacks.py      # Logging utilities
    โ”œโ”€โ”€ device.py         # Device management
    โ””โ”€โ”€ duplicates.py     # Duplicate elimination

๐Ÿ”ฌ How It Works

EvoGrad makes evolutionary algorithms differentiable through:

  1. Reparameterisation Trick: Convert random sampling into deterministic transformations of parameter-free noise:

    x = g_ฮธ(ฮต), ฮต ~ p(ฮต)  โ†’  โˆ‡_ฮธ L โ‰ˆ โˆ‡_ฮธ f(g_ฮธ(ฮต))
    
  2. Gumbel-Softmax: Differentiable approximation for categorical selection:

    # Soft selection (differentiable)
    probs = softmax((log_probs + gumbel_noise) / temperature)
    selected = probs @ population  # Weighted combination
    
  3. Binary-Concrete: Differentiable approximation for binary masks (mutation/crossover):

    # Soft mask (differentiable)
    mask = sigmoid((log(u) - log(1-u) + logits) / temperature)
    # Straight-through estimator for hard decisions
    hard_mask = (mask > 0.5).float() - mask.detach() + mask
    
  4. Pathwise Gradients: For continuous distributions (Gaussian sampling in CMA-ES):

    # x = ฮผ + ฯƒ * L @ z, z ~ N(0, I)
    z = torch.randn(pop_size, n_var)
    x = mean + sigma * (L @ z.T).T  # Fully differentiable
    

๐Ÿ“Š Benchmarks

TODO

๐Ÿ“– Citation

TBA

๐Ÿ“„ License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ™ Acknowledgements

  • Inspired by pymoo for API design
  • Built with PyTorch for automatic differentiation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

evograd_diff-0.1.1.tar.gz (3.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

evograd_diff-0.1.1-py3-none-any.whl (2.7 MB view details)

Uploaded Python 3

File details

Details for the file evograd_diff-0.1.1.tar.gz.

File metadata

  • Download URL: evograd_diff-0.1.1.tar.gz
  • Upload date:
  • Size: 3.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for evograd_diff-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4db22f106283efb706309888e461340dc742bb59f7c2a838965c660745604f39
MD5 1efef40d3c40aad3a1e7cfc6b409784b
BLAKE2b-256 9ed48d4f035c021abcd01897c92d3d2409ec3a41196e3c16b487f0b31e11920e

See more details on using hashes here.

File details

Details for the file evograd_diff-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: evograd_diff-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for evograd_diff-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 defa3b327f94f483b88c73f2d600318afd668d6dfafc2a7ef448012f25f00f2d
MD5 20d08726a7641a0790abe6563f2923d6
BLAKE2b-256 bc4c6c714c1064a9fbf5190be2ebd63798fa44cd9c566945929457a30a36a2a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page