A genetic algorithm library implementation in Rust.
rustup
https://rustup.rs/Cargo.toml
file from: https://crates.io/crates/watchmakerGenetic
trait for your search problem and call watchmaker::search
.rust
pub fn search<G>(
mut genetic: Box<dyn Genetic<G>>,
mut progress: Option<Progress<G>>,
mut random: Random,
settings: &Settings,
) -> Result<Success<G>, Failure>
Example that searches for an optimal floating point value:
```rust use watchmaker::*;
fn main() { let result = search( Box::new(ExampleGenetic::new(makerandom())), Some(Box::new(|x| { println!("progress:{:?}", x); })), makerandom(), &Settings::default(), ); println!("{:?}", result); }
pub struct ExampleGenome(pub f64);
pub const TARGET: f64 = 100.0;
pub struct ExampleGenetic { random: Random, }
impl ExampleGenetic { pub fn new(random: Random) -> Self { Self { random } } }
impl Genetic
fn evaluate(&mut self, genome: &ExampleGenome) -> f64 {
(TARGET - genome.0).abs()
}
fn crossover(&mut self, lhs: &ExampleGenome, rhs: &ExampleGenome) -> ExampleGenome {
ExampleGenome((lhs.0 + rhs.0) / 2.0)
}
fn mutate(&mut self, original: &ExampleGenome) -> ExampleGenome {
ExampleGenome(original.0 + self.random.gen_range(-10.0..10.0))
}
} ```
cargo test
or cargo build
cargo run --example peak
cargo run --example weasel
cargo run --example hyperparameter_grid_search
The tests are in a separate tests crate. This arrangement allows code reuse between tests, examples and benches without affecting the core crate.
Note major version increment with each major release. API changes will not be backwards compatible between major releases.
[ ] v3.x.x
Fourth published version; Long Term Support
Will take contributions, bug fixes from this point on.
[ ] Unpublished; unsupported; beta quality
Add hyperparameter detection feature
Add crate level docs
[x] v2.x.x
Fourth published version; unsupported; beta quality
Add tests for TournamentSelector
[x] Unpublished; unsupported; beta quality
Split out algorithm that produces new generations
Fix bug with non-monotonic best cost / genomes
[x] Unpublished; unsupported; beta quality
Better typing around Progress
Link to examples, with description and sample output
[x] v1.x.x
Second published version; unsupported; beta quality
Fix license - does not appear as 'standard' on crates.io
[x] v0.1.0
First published version; unsupported; beta quality
Not accepting pull requests yet :) See roadmap.
MIT permissive license. See LICENSE for full license details.