watchmaker

A genetic algorithm library implementation in Rust.

CircleCI

Features

Usage

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); }

[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 for ExampleGenetic { fn initialize(&mut self) -> ExampleGenome { ExampleGenome(self.random.genrange(0.0..1000.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))
}

} ```

Development

Examples

Tests

The tests are in a separate tests crate. This arrangement allows code reuse between tests, examples and benches without affecting the core crate.

Roadmap

Note major version increment with each major release. API changes will not be backwards compatible between major releases.

Alternatives

Contributing

Not accepting pull requests yet :) See roadmap.

License

MIT permissive license. See LICENSE for full license details.

Source Code Repository

https://github.com/thomasbratt/watchmaker