|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Common Development Commands |
| 6 | + |
| 7 | +```bash |
| 8 | +# Build the project |
| 9 | +cargo build |
| 10 | +cargo build --release |
| 11 | + |
| 12 | +# Run all tests |
| 13 | +cargo test |
| 14 | + |
| 15 | +# Run a specific test |
| 16 | +cargo test test_name |
| 17 | + |
| 18 | +# Run tests with verbose output |
| 19 | +cargo test -- --nocapture |
| 20 | + |
| 21 | +# Run examples (in release mode for performance) |
| 22 | +cargo run --example evolve_nqueens --release |
| 23 | +cargo run --example hill_climb_nqueens --release |
| 24 | +cargo run --example permutate_knapsack --release |
| 25 | + |
| 26 | +# Run benchmarks |
| 27 | +cargo bench |
| 28 | +cargo bench bench_name |
| 29 | + |
| 30 | +# Generate documentation |
| 31 | +cargo doc --open |
| 32 | + |
| 33 | +# Check code without building |
| 34 | +cargo check |
| 35 | + |
| 36 | +# Format code |
| 37 | +cargo fmt |
| 38 | + |
| 39 | +# Lint code |
| 40 | +cargo clippy |
| 41 | +``` |
| 42 | + |
| 43 | +## High-Level Architecture |
| 44 | + |
| 45 | +This is a Rust genetic algorithm library with three core abstractions: |
| 46 | + |
| 47 | +### 1. **Genotype** (Search Space) |
| 48 | +Located in `src/genotype/`, defines the representation of solutions: |
| 49 | +- `BinaryGenotype`: Boolean alleles (Vec<bool>) |
| 50 | +- `BitGenotype`: Bit-packed boolean alleles (more memory efficient) |
| 51 | +- `ListGenotype<T>`: List of values from a fixed set of alleles |
| 52 | +- `UniqueGenotype<T>`: Permutation of unique values |
| 53 | +- `RangeGenotype<T>`: Numeric values within ranges |
| 54 | +- `DynamicMatrixGenotype<T>` / `StaticMatrixGenotype<T>`: Matrix representations for GPU-friendly operations |
| 55 | + |
| 56 | +### 2. **Fitness** (Search Goal) |
| 57 | +Located in `src/fitness/`, evaluates chromosome quality: |
| 58 | +- Trait `Fitness` with method `calculate_for_chromosome()` |
| 59 | +- Returns `FitnessValue` (f64) to be maximized or minimized |
| 60 | +- Can be parallelized with `with_par_fitness()` |
| 61 | +- Cache wrapper available for expensive fitness calculations |
| 62 | + |
| 63 | +### 3. **Strategy** (Search Method) |
| 64 | +Located in `src/strategy/`, implements the search algorithm: |
| 65 | + |
| 66 | +#### **Evolve** (`src/strategy/evolve/`) |
| 67 | +Classical genetic algorithm with: |
| 68 | +- **Select**: Tournament or Elite selection to choose parents |
| 69 | +- **Crossover**: Uniform, SinglePoint, MultiPoint, etc. to combine parents |
| 70 | +- **Mutate**: SingleGene, MultiGene with configurable rates |
| 71 | +- **Extensions**: MassExtinction, MassGenesis, MassDegeneration for diversity management |
| 72 | + |
| 73 | +#### **HillClimb** (`src/strategy/hill_climb/`) |
| 74 | +Local search when crossover is inefficient: |
| 75 | +- Variants: Stochastic, SteepestAscent |
| 76 | +- Works with neighbor generation instead of crossover |
| 77 | + |
| 78 | +#### **Permutate** (`src/strategy/permutate/`) |
| 79 | +Exhaustive search for small spaces with 100% guarantee |
| 80 | + |
| 81 | +## Key Design Patterns |
| 82 | + |
| 83 | +1. **Builder Pattern**: All major components use builders for configuration |
| 84 | +2. **Trait-based Architecture**: Core behaviors defined as traits (Genotype, Fitness, Select, Crossover, Mutate) |
| 85 | +3. **Parallelization**: Built on rayon for automatic parallelization |
| 86 | +4. **Reporter Pattern**: Strategies accept reporters for monitoring progress |
| 87 | + |
| 88 | +## Testing Approach |
| 89 | + |
| 90 | +- Integration tests in `tests/` directory organized by module |
| 91 | +- Use `.with_rng_seed_from_u64(0)` for deterministic test results |
| 92 | +- Benchmarks in `benches/` using criterion |
| 93 | +- Examples serve as both documentation and integration tests |
| 94 | + |
| 95 | +## Performance Considerations |
| 96 | + |
| 97 | +- Prefer `BitGenotype` over `BinaryGenotype` for large boolean chromosomes |
| 98 | +- Use `with_par_fitness()` for expensive fitness calculations |
| 99 | +- Matrix genotypes enable GPU-friendly memory layout |
| 100 | +- Monitor with reporters to identify bottlenecks (fitness vs framework overhead) |
0 commit comments