This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Build the project
cargo build
cargo build --release
# Run all tests
cargo test
# Run a specific test
cargo test test_name
# Run tests with verbose output
cargo test -- --nocapture
# Run examples (in release mode for performance)
cargo run --example evolve_nqueens --release
cargo run --example hill_climb_nqueens --release
cargo run --example permutate_knapsack --release
# Run benchmarks
cargo bench
cargo bench bench_name
# Generate documentation
cargo doc --open
# Check code without building
cargo check
# Format code
cargo fmt
# Lint code
cargo clippyThis is a Rust genetic algorithm library with three core abstractions:
AI agent guide: See AGENTS.md for decision matrices, constructor parameter reference, copy-paste templates, and troubleshooting.
Located in src/genotype/, defines the representation of solutions:
BinaryGenotype: Boolean alleles (Vec)ListGenotype<T>: List of values from a fixed set of allelesUniqueGenotype<T>: Positional permutation (swap-only mutation, default T = usize)RangeGenotype<T>: Numeric values within a range (default f32)MultiListGenotype<T>: Per-gene allele listsMultiUniqueGenotype<T>: Multiple unique setsMultiRangeGenotype<T>: Per-gene ranges with heterogeneous MutationType support
Located in src/fitness/, evaluates chromosome quality:
- Trait
Fitnesswith methodcalculate_for_chromosome() - Returns
FitnessValue(isize, not f64! — isize enables equality checks for staleness detection) to be maximized or minimized - Can be parallelized with
with_par_fitness() - Cache wrapper available for expensive fitness calculations
Located in src/strategy/, implements the search algorithm:
Classical genetic algorithm with:
- Select: Tournament or Elite selection to choose parents
- Crossover: Uniform, SinglePoint, MultiPoint, etc. to combine parents
- Mutate: SingleGene, MultiGene with configurable rates
- Extensions: MassExtinction, MassGenesis, MassDegeneration for diversity management
Local search when crossover is inefficient:
- Variants: Stochastic, SteepestAscent
- Works with neighbor generation instead of crossover
Exhaustive search for small spaces with 100% guarantee
- Builder Pattern: All major components use builders for configuration
- Trait-based Architecture: Core behaviors defined as traits (Genotype, Fitness, Select, Crossover, Mutate)
- Parallelization: Built on rayon for automatic parallelization
- Reporter Pattern: Strategies accept reporters for monitoring progress
- Integration tests in
tests/directory organized by module - Use
.with_rng_seed_from_u64(0)for deterministic test results - Benchmarks in
benches/using criterion - Examples serve as both documentation and integration tests
- Use
with_par_fitness()for expensive fitness calculations - Monitor with reporters to identify bottlenecks (fitness vs framework overhead)
- Framework overhead is mostly Crossover; practical overhead is mostly Fitness