Skip to content

Repository files navigation

genoxide: optimization for Rust and Python

Crates.io PyPI Docs.rs CI License Benchmarks

Optimization for Rust and Python: genetic algorithms, evolution strategies, CMA-ES, differential evolution, particle swarms, genetic programming, multi-objective optimization, local search, gradient-based methods (L-BFGS-B, Adam, MMA) and Bayesian optimization in one library. A seed gives the same results, to the bit, on every platform and thread count.

Install

cargo add genoxide

It adds the latest release to your Cargo.toml (the version is on the crates.io badge above). The Python package: pip install genoxide.

use genoxide::prelude::*;

fn main() -> genoxide::Result<()> {
    // OneMax: find the 100-bit string with the most ones
    let ga = Ga::builder(Binary::new(100)?)
        .population_size(100)
        .select(Tournament::new(3)?)
        .crossover(UniformCrossover::new())
        .mutate(BitFlip::per_gene(0.01)?)
        .seed(42)
        .build()?;

    let outcome = Engine::new(ga, |genome: &Bits| genome.count_ones() as f64)
        .stop_when(Stop::target(100.0).or(Stop::generations(1_000)))
        .run()?;

    println!("best: {} after {} generations", outcome.best_fitness(), outcome.generations());
    Ok(())
}

What's in it

  • Genomes: binary (bit-packed), integer, real, permutation, and real with a self-adaptive step size.
  • Genetic algorithms: generational, steady-state, (μ+λ), (μ,λ) and memetic schemes, with the classic operators.
  • Evolution strategies, CMA-ES, differential evolution, particle swarms: with IPOP and BIPOP restarts, JADE, SHADE and L-SHADE.
  • Local search: hill climbing, simulated annealing, tabu search, iterated local search, and the Nelder-Mead simplex method with random restarts.
  • Gradient-based: L-BFGS-B for smooth functions with bounds, from a few variables to millions, and gradient descent, momentum, Nesterov, Adam and AdamW with learning-rate schedules, with gradients supplied or by finite differences; MMA and GCMMA, the method of moving asymptotes, for millions of variables with few constraints, from supplied gradients and constraint Jacobians. Continuation runs any of them through stages of one problem, a smooth version first and sharper ones after, with the optimizer's state kept between stages.
  • Bayesian optimization: for expensive functions, where tens to a few hundred evaluations must do: a Gaussian process with a Matérn or squared exponential kernel, its hyperparameters by maximum likelihood, and the log expected improvement, expected improvement, probability of improvement or confidence bound, maximized by L-BFGS-B with their gradients; a log transform for values that span orders of magnitude. Batches of points evaluated in parallel (the Kriging believer and the constant liar), asynchronous evaluation, constraints modeled one by one (the probability of feasibility) and integer genes. The Gaussian process can be fitted and queried on its own.
  • Genetic programming: strongly typed trees of your own primitives, evolved into programs and formulas, with subtree and one-point crossover, subtree, point, hoist, shrink and constant mutation, bloat control, and fast evaluation over data.
  • Neuroevolution: multilayer perceptrons and recurrent networks whose weights evolve, NEAT, which evolves networks' structure too, and pole-balancing control tasks for them to solve.
  • Multi-objective: NSGA-II, NSGA-III, SPEA2, MOEA/D and SMS-EMOA, with quality indicators.
  • Test problems: classic continuous functions such as Rastrigin, Rosenbrock and Branin, with their bounds, known optima and references; constrained ones, CEC 2006's g01-g24 and engineering designs such as the welded beam and the pressure vessel; multi-objective ones such as ZDT, DTLZ and the constrained BNH and OSY, with their optimal fronts.
  • Engine: parallel, batch and asynchronous evaluation, island models, constraints, checkpoints, reproducible seeds.
  • Beyond Rust: a Python package, and a command-line program for fitness functions in any language.

The full list is in docs/features.md.

Python

pip install genoxide: wheels for 64-bit Linux, macOS and Windows, CPython 3.10 and later. genoxide is built and tested on 64-bit platforms only.

import genoxide as gx

ga = gx.Ga(gx.Binary(100), population_size=100, select=gx.Tournament(3),
           crossover=gx.UniformCrossover(), mutation=gx.BitFlip(rate=0.01), seed=42)
result = ga.run(lambda bits: bits.sum(), target=100, generations=1_000)

See python/README.md for the algorithms, operators and numpy fitness functions.

Benchmarks

genoxide and its Python package are benchmarked on a small, matched suite: three problems, one method each, under public rules. Every library runs a problem only with its own implementation of that problem's method, set to the same written definition, so the results compare implementations of the same algorithm rather than each library's pick of a method: a GA on OneMax 1000, DE/rand/1/bin on Rastrigin 30 (a fixed budget, measured by the time for it and the error at the end) and CMA-ES on Rosenbrock 10. Single-threaded on the same machine, 10 seeds each. More problems, and multi-objective ones, come back after these.

Expected time to target: a panel per problem, a bar per library

genoxide's own releases are compared on the same runs by the CPU instructions Callgrind counts, exact whatever the machine's load: genoxide_versions.svg (rule 10).

Interactive results, a card per problem: tachsin.gr/projects/genoxide/benchmarks. The methodology, each method's definition and the page for each library give the configurations and their differences, and results.md has the full tables.

Links

Status

Alpha, pre-1.0: the API may change between 0.x versions. See the roadmap.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in genoxide by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Optimization for Rust and Python: genetic algorithms, CMA-ES, differential evolution, multi-objective, L-BFGS-B, Adam, MMA and more. The same results on every platform.

Topics

Resources

Contributing

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages