Pure-numpy genetic algorithm framework for single-objective (GA) and multi-objective (NSGA-II) optimization
Project description
ctrl-freak
An extensible genetic algorithm framework for single and multi-objective optimization, built on pure numpy.
Installation
uv add ctrl-freak
or:
pip install ctrl-freak
Quick Start
import numpy as np
from ctrl_freak import nsga2, ga
# === Multi-Objective: NSGA-II ===
def init(rng):
return rng.uniform(0, 1, size=5)
def evaluate_multi(x):
f1 = x[0]
f2 = 1 - np.sqrt(x[0]) + x[1:]@x[1:]
return np.array([f1, f2])
def crossover(p1, p2):
return (p1 + p2) / 2
def mutate(x):
return np.clip(x + np.random.normal(0, 0.1, size=x.shape), 0, 1)
result = nsga2(
init=init,
evaluate=evaluate_multi,
crossover=crossover,
mutate=mutate,
pop_size=100,
n_generations=50,
seed=42,
)
# Extract Pareto front
pareto_front = result.pareto_front
print(f"Found {len(pareto_front)} Pareto-optimal solutions")
# === Single-Objective: Standard GA ===
def evaluate_single(x):
return float(np.sum(x ** 2)) # Sphere function
result = ga(
init=init,
evaluate=evaluate_single,
crossover=crossover,
mutate=mutate,
pop_size=100,
n_generations=100,
seed=42,
)
print(f"Best fitness: {result.best[1]:.6f}")
Features
- Single-objective optimization with
ga(). - Multi-objective optimization with
nsga2(). - Pluggable parent-selection and survival strategies.
- Pure numpy primitives for Pareto dominance, non-dominated sorting, and crowding distance.
- Parallel evaluation through
n_workers.
See the full documentation for API details, user contracts, examples, and extension points.
Benchmarks
ctrl-freak ships a validation benchmark suite that checks ga() and nsga2()
against pymoo and DEAP on standard problems with known optima. With the genetic
algorithm held identical across all three libraries (ported SBX, aligned mutation
and selection, identical evaluation budget), ctrl-freak's results are statistically
indistinguishable from both baselines on the single-objective error metrics (all
six functions) and on multi-objective convergence (ZDT1, ZDT2, ZDT3, and DTLZ2). On
the two hardest problems (ZDT4, ZDT6) none of the three libraries converges at this
budget, and ctrl-freak is at least as good as both. The goal is parity, not
superiority.
See the canonical report in benchmarks/README.md and the citable Validation page.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ctrl_freak-0.2.0.tar.gz.
File metadata
- Download URL: ctrl_freak-0.2.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a066e9f4485b30405a6184943508f9ffc01f5d9c68a3f926b28b9938833b38d3
|
|
| MD5 |
f3cdc40afbbf174ba5b90ca5e3d002be
|
|
| BLAKE2b-256 |
f9b3bfac11fdd18e0728271b392b6c4cff37ce54a6b05d0396752ec2dd288047
|
File details
Details for the file ctrl_freak-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ctrl_freak-0.2.0-py3-none-any.whl
- Upload date:
- Size: 37.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d097566eb2f81c16c64d9454afeb9b3bdf56b700d8b821960cdce249c3f5d120
|
|
| MD5 |
6783ce872ff9bc2e89aa2cf0273bfed3
|
|
| BLAKE2b-256 |
ee41042b97c102a221ee6a406d4e4c578cf079265888f22066f7f4947479b8eb
|