hill_descent_lib
A Rust genetic algorithm library for n-dimensional optimization problems.
Quick Start
use ;
use RangeInclusive;
// Define your fitness function (lower scores are better)
;
Features
- N-dimensional optimization - Handles any number of parameters
- Spatial regions - Divides search space into adaptive regions for efficient exploration
- Genetic algorithm - Uses crossover, mutation, and selection for evolution
- Deterministic - Seeded RNG ensures reproducible results
- Zero-copy parallelism - Leverages Rayon for efficient multi-core processing
- Flexible fitness functions - Easy-to-implement trait for custom optimization problems
- Optional tracing - Built-in logging support for debugging (feature:
enable-tracing)
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage Examples
Basic 2D Optimization
use ;
use RangeInclusive;
;
Higher Dimensional Problems
use ;
use RangeInclusive;
;
Custom Function Floor
By default, the algorithm assumes the optimal fitness is 0. For functions with different optima:
use ;
use RangeInclusive;
;
Using the Tracing Feature
For debugging and analysis, enable the optional tracing feature:
[]
= { = "0.1.0", = ["enable-tracing"] }
use ;
How It Works
The library implements a genetic algorithm with spatial partitioning:
- Initialization - Creates a population of random organisms within specified bounds
- Regions - Divides the search space into adaptive regions based on organism distribution
- Evaluation - Each organism is scored using your fitness function
- Selection - Better-performing organisms in each region get more reproduction opportunities
- Reproduction - Sexual reproduction with crossover and mutation creates offspring
- Adaptation - Regions dynamically adjust based on population distribution and fitness landscape
The algorithm automatically manages:
- Region boundaries and subdivision
- Carrying capacity per region based on fitness
- Organism aging and death
- Mutation rates and adjustment parameters
- Search space expansion when needed
API Overview
Core Types
setup_world()- Initialize the optimization environmentGlobalConstants- Configuration (population size, region count, seed)World- Main optimization container with methods:training_run()- Run one generationget_best_score()- Get current best fitnessget_best_organism()- Get the best organismget_state()- Get JSON representation of world state
Traits to Implement
SingleValuedFunction- Define your fitness functionsingle_run(&self, params: &[f64]) -> f64- Requiredfunction_floor(&self) -> f64- Optional (default: 0.0)
Performance Characteristics
- Parallelism: Region processing uses Rayon for multi-core efficiency
- Memory: Allocates based on population size and dimensionality
- Determinism: Seeded RNG ensures reproducible runs with same configuration
- Scalability: Tested with 100+ dimensions and populations of 10,000+
Common Use Cases
- Parameter optimization for machine learning models
- Neural network weight initialization and tuning
- Function minimization/maximization problems
- Engineering design optimization
- Scientific computing and simulation tuning
- Hyperparameter search
Comparison to Other Libraries
Unlike gradient-based optimizers, hill_descent_lib:
- ✅ Doesn't require differentiable functions
- ✅ Handles discrete and continuous parameters
- ✅ Explores multiple regions simultaneously
- ✅ Less likely to get stuck in local minima
- ❌ Generally requires more function evaluations
- ❌ Not as fast for smooth, convex problems
Documentation
Full API documentation is available on docs.rs.
Examples
See the examples/ directory for more usage patterns:
simple_optimization.rs- Basic 2D examplecustom_function.rs- Implementing custom fitness functionsmulti_dimensional.rs- High-dimensional optimization
Run examples with:
Minimum Supported Rust Version (MSRV)
Rust 2024 edition (Rust 1.82.0+)
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Acknowledgments
Developed on Windows with cross-platform compatibility in mind.
Repository
Source code: https://github.com/cainem/hill_descent