Skip to main content

Crate genetic_algorithms

Crate genetic_algorithms 

Source
Expand description

Genetic Algorithms library with flexible genotypes, chromosomes, and operators.

This crate provides a modular and efficient framework for building Genetic Algorithms (GAs). It focuses on:

  • Clear abstractions (traits for genes and chromosomes).
  • Efficient data handling using Cow to avoid unnecessary clones.
  • Composable operators (selection, crossover, mutation, survivor).
  • Parallelism for fitness evaluation and reproduction.

Key concepts:

  • Genotypes: reusable gene types (e.g., Binary, Range) that implement GeneT.
  • Chromosomes: sequences of genes that implement ChromosomeT and hold fitness and age.
  • GA Orchestrator: ga::Ga coordinates the lifecycle (init, selection, crossover, mutation, survivor, evaluation).
  • Configuration: a cohesive configuration model to tune operators and limits.

Quickstart (example):

use genetic_algorithms::configuration::ProblemSolving;
use genetic_algorithms::genotypes::Range as RangeGene;
use genetic_algorithms::chromosomes::Range as RangeChromosome;
use genetic_algorithms::ga::Ga;
use genetic_algorithms::initializers::range_random_initialization;
use genetic_algorithms::operations::{Crossover, Mutation, Selection, Survivor};
use genetic_algorithms::traits::{ConfigurationT, CrossoverConfig, MutationConfig, SelectionConfig, StoppingConfig};

const N: i32 = 8;

fn fitness_fn(dna: &[RangeGene<i32>]) -> f64 {
    // ... compute fitness ...
    0.0
}

let alleles = vec![RangeGene::new(0, vec![(0, N - 1)], 0)];
let alleles_clone = alleles.clone();
let mut ga = Ga::new()
    .with_genes_per_chromosome(N)
    .with_population_size(100)
    .with_initialization_fn(move |genes_per_chromosome, _, _| {
        range_random_initialization(genes_per_chromosome, Some(&alleles_clone), Some(false))
    })
    .with_fitness_fn(fitness_fn)
    .with_selection_method(Selection::Tournament)
    .with_crossover_method(Crossover::Uniform)
    .with_mutation_method(Mutation::Swap) // Swap or value-mutation for Range<i32> chromosomes
    .with_problem_solving(ProblemSolving::Minimization)
    .with_survivor_method(Survivor::Fitness)
    .with_max_generations(5000)
    .with_fitness_target(0.0)
    .build()
    .expect("Invalid configuration");
let _population = ga.run();

Modules of interest:

  • traits: core traits for genes, chromosomes, and configuration.
  • chromosomes: concrete chromosome implementations (Binary, Range).
  • genotypes: concrete gene implementations (Binary, Range).
  • operations: selection, crossover, mutation, survivor operators.
  • ga: genetic algorithm orchestrator.
  • population: population management and best tracking.
  • fitness: helpers to wrap user fitness functions.
  • initializers: utilities to build initial DNA.

Performance notes:

  • Chromosome DNA is set via a single set_dna(Cow<[Gene]>) method to avoid extra copies.
  • Operators use pre-allocation and move semantics where possible.
  • Parallel evaluation leverages threads to scale fitness computation.

Re-exports§

pub use observer::LogObserver;
pub use observer::IslandGaObserver;
pub use observer::Nsga2Observer;
pub use observer::AllObserver;
pub use observer::CompositeObserver;
pub use observer::GaObserver;
pub use observer::NoopObserver;
pub use observer::ExtensionEvent;
pub use ga::TerminationCause;

Modules§

chromosomes
Built-in chromosome types.
configuration
GA configuration types.
error
Error types for the genetic algorithm library.
extension
Extension strategies for population diversity control.
fitness
Fitness-function helpers and wrappers.
ga
Main genetic algorithm orchestrator.
genotypes
Built-in gene (genotype) types.
initializers
Population initialization functions.
island
Island Model for parallel multi-population genetic algorithm evolution.
niching
Fitness sharing (niching) utilities for promoting population diversity.
nsga2
NSGA-II multi-objective genetic algorithm.
observer
Structured lifecycle observer for the GA execution loop.
operations
Genetic operators: selection, crossover, mutation, and survivor selection.
population
Population container for evolving chromosomes.
reporter
Lifecycle reporters for the GA execution loop.
rng
Seedable random number generation for reproducible GA runs.
stats
Per-generation statistics.
traits
Core traits that define the genetic algorithm abstractions.
validators