darwin-rs
This library allows you to write evolutionary algorithms (EA) using the Rust programming language.
Written by Willi Kappler, License: MIT - Version 0.1.1 (2016.06.12)
Documentation: darwin-rs
The example folder contains three examples:
- TSP (traveling salesman problem): the classic type of problem for EA (see two pictures above)
- Sudoku: a sudoku solver using EA
- Queens: solving the queens problem with EA. Although not as fast as this one ;-)
Usage:
Add the following to the Cargo.toml in your project:
[]
= "0.1"
And this in the rust source code of your application:
extern crate darwin_rs;
use ;
Basically you have to implement the trait Individual
for your data structure:
These three methods are needed:
new(): creates new instance of your struct.
mutate(&mut self): mutates the content of the struct.
calculate_fitness(&self) -> f64: this calculates the fitness value, that is how close is this individual struct instance to the perfect solution ? Lower values means better fit (or less error).
After that you have to create a new instance of the simulation and provide the settings:
let my_builder = new
.factor
.threads
.individuals
.increasing_exp_mutation_rate
.finalize;
match my_builder
factor(): Sets the termination condition: if the improvement factor is better or equal to this value, the simulation stops.
threads(): Number of threads to use for the simulation.
individuals(): How many individuals (= distinct copies of your data structure) should the simulation have ?
increasing_exp_mutation_rate(): Sets the mutation rate for each individual: Use exponential mutation rate.
finalize(): Finish setup and do sanity check. Returns Ok(Simulation)
if there are no errors in the configuration.
Then just do a match on the result of finalize()
and call simulation.run()
to start the simulation. After the finishing it, you can access some statistics (total_time_in_ms
, improvement_factor
, iteration_counter
) and the population of course:
for wrapper in my_simulation.population
Each individual is wrapped inside a Wrapper
struct that contains additional information needed for the simulation: fitness and the number of mutations.
See also the example folder for full working programs.
Used crates:
- time: time usage statistics
- jobsteal: parallelization
- quick-error: easy error type creation
Similar crates:
TODO:
- Add more documentation comments for library
- Add test cases
- Add more examples (ocr, ...)
- Maybe use phantom type for builder pattern to detect configuration error at compile type ? (https://www.reddit.com/r/rust/comments/2pgrz7/required_parameters_utilizing_the_builder_pattern/)
- Add super optimization (only allow changes that have an improvement) ?
- Add possibility to load and save population / individuals in order to cancel / resume simulation (serde)
Any feedback is welcome!