Skip to main content

Crate genoxide

Crate genoxide 

Source
Expand description

§genoxide

Optimization for Rust (and Python): evolutionary, local, gradient-based, Bayesian and multi-objective methods in one library. A seed gives the same results, to the bit, on every platform and thread count, parallel or not.

ProblemMethod
Yes / no choices, integers, orders (subsets, schedules, tours)the genetic algorithm Ga, LocalSearch
Real numbers in a box, no gradientCmaes, De, Pso, Es, NelderMead
Smooth functions with a gradientLbfgsb, and FirstOrder (Adam, momentum) for millions of variables
Many variables, few inequality constraints, with gradientsMma (MMA and GCMMA)
An expensive function: tens to a few hundred evaluations, in batches or asynchronously, with constraints, of real or integer genesBo, Bayesian optimization, with the Gaussian processes of model::gp
A smooth problem solved in stages (a smoothing, sharpness or penalty changed step by step)Continuation around a local method, its state kept
Several objectives at onceNsga2, Nsga3, Moead, SmsEmoa
Programs and formulasgp: tree genetic programming
A neural network’s weightsnn with Cmaes

For AI coding assistants, AGENTS.md is a complete guide in one page, with a program for every method, each run in CI.

Alpha, pre-1.0: the API changes between 0.x versions. See the roadmap for what is planned.

use genoxide::prelude::*;

// OneMax: find the genome 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());

The building blocks:

  • Error: all errors: invalid settings are errors, not panics
  • StreamRng: portable, seedable random numbers with independent streams
  • Fitness and Objective: totally ordered fitness values, with an invalid state and constraint violations (constraint, Deb’s feasibility rules)
  • genome: genomes and the spaces they live in, e.g. bit-packed Binary
  • Individual and Population: genomes with their fitness and age
  • operator: selection, crossover and mutation
  • algorithm: algorithms as ask / tell state machines: the genetic algorithm Ga, LocalSearch (hill climbing, simulated annealing, tabu search), evolution strategies Es, CMA-ES Cmaes, differential evolution De, particle swarm optimization Pso, and the island model Islands, and SteadyGa for asynchronous evaluation
  • Engine: runs an algorithm with stop conditions, parallel or batch evaluation and cancellation; AsyncEngine evaluates asynchronously
  • gradient: gradients for gradient-based methods, supplied with the fitness (Differentiable) or by finite differences
  • gp: tree genetic programming, strongly typed: programs and formulas as genomes, with subtree crossover and mutation
  • nn: neural networks whose weights are a genome, a multilayer perceptron and an Elman recurrent network, for neuroevolution
  • multi: multi-objective optimization: NSGA-II, NSGA-III, SPEA2, MOEA/D, SMS-EMOA, the MultiEngine, Pareto dominance and non-dominated sorting
  • observer: statistics, hall of fame, progress lines and custom callbacks
  • problems: single-objective test problems from the literature, with their bounds, known optima and references, and control tasks that balance poles on a cart
  • prelude: everything above in one import
  • math: sin, cos, exp, powf, powi and the like, the same to the bit on every platform, for fitness functions that must give the same results everywhere

Cargo features:

  • parallel (default): parallel fitness evaluation with rayon
  • tracing: a span per run, an event per generation and one at the end, with the target genoxide, from the three engines
  • serde: Serialize and Deserialize for algorithms and their parts, and checkpoint, to save a run and resume it exactly
  • cli: the genoxide program, which runs an optimization described in a TOML or JSON file with any program as the fitness function, see docs/cli.md

For Python, the genoxide package on PyPI runs the algorithms with fitness functions in Python and numpy. Its source is in python/.

Re-exports§

pub use algorithm::Algorithm;
pub use algorithm::Ga;
pub use engine::Engine;
pub use engine::Evaluated;
pub use engine::Outcome;
pub use engine::Stop;
pub use engine::StopReason;
pub use error::Error;
pub use error::Result;
pub use fitness::Fitness;
pub use fitness::Objective;
pub use individual::Individual;
pub use population::Population;
pub use rng::StreamRng;

Modules§

algorithm
Algorithms as ask / tell state machines.
checkpointserde
Checkpoints: an algorithm saved during a run, to resume the run later with exactly the results it would have had without the interruption.
constraint
Constraint handling: measuring constraint violations, and penalty functions.
engine
Running an algorithm: fitness evaluation, stop conditions, observers and cancellation.
error
Errors returned by genoxide.
fitness
Fitness values and the optimization objective.
genome
Genomes (the encoded solutions) and representations (the spaces they live in).
gp
Tree genetic programming, strongly typed: programs and formulas as genomes.
gradient
Gradients: supplied with the fitness, or estimated by finite differences.
individual
A genome with its fitness and age.
math
Portable math: the same bits on every platform, at native speed.
model
Surrogate models: cheap approximations of an expensive function, fitted to its evaluations.
multi
Multi-objective optimization: solutions scored on several objectives at once, and the Pareto front of the best trade-offs between them.
neat
NEAT, NeuroEvolution of Augmenting Topologies (Stanley and Miikkulainen 2002): networks whose structure evolves with their weights, from minimal networks up.
nn
Neural networks whose weights are a genome: a multilayer perceptron and an Elman recurrent network, for neuroevolution with a fixed topology.
observer
Observers: statistics, hall of fame and custom callbacks, notified after every generation.
operator
Genetic operators: selection, crossover and mutation.
population
A population of individuals.
prelude
Everything needed for most programs, in one import.
problems
Single-objective test problems: fitness functions with their search space, their known optimum and the paper that defines them.
rng
Portable, seedable random number generation with independent streams.