Crates.io | watchmaker |
lib.rs | watchmaker |
version | 2.0.0 |
source | src |
created_at | 2022-05-01 11:28:22.22431 |
updated_at | 2022-05-08 12:29:29.960122 |
description | A genetic algorithm implementation in Rust. |
homepage | https://github.com/thomasbratt/watchmaker |
repository | https://github.com/thomasbratt/watchmaker |
max_upload_size | |
id | 578564 |
size | 30,399 |
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
. 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:
use watchmaker::*;
fn main() {
let result = search(
Box::new(ExampleGenetic::new(make_random())),
Some(Box::new(|x| {
println!("progress:{:?}", x);
})),
make_random(),
&Settings::default(),
);
println!("{:?}", result);
}
#[derive(Clone, Debug, PartialEq)]
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<ExampleGenome> for ExampleGenetic {
fn initialize(&mut self) -> ExampleGenome {
ExampleGenome(self.random.gen_range(0.0..1_000.0))
}
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.
TournamentSelector
Not accepting pull requests yet :) See roadmap.
MIT permissive license. See LICENSE for full license details.