Expand description
§genetic-rs
A small framework for managing genetic algorithms.
§Features
First off, this crate comes with the builtin, genrand, crossover, knockout, and speciation features by default. If you want the simulation to be parallelized (which is most usecases), add the rayon feature. There are also some convenient macros with the derive feature.
§Ecosystem
This framework was created with a high degree of modularity in mind, allowing other crates to contribute to the ecosystem. Here’s a list of some good crates:
- neat - Handles complex reproduction and prediction logic for the NEAT algorithm, allowing you to create AI simulations with ease. It also functions as a pretty good example for the more complex usecases of the crate’s traits.
- genetic-rs-extras - A companion crate with quality-of-life improvements and utility features.
If you have a genetic-rs-based crate and you’d like it to be featured here, submit a PR or discussion post and I’ll consider it.
§How to Use
Here’s a simple genetic algorithm:
use genetic_rs::prelude::*;
// `Mitosis` can be derived if both `Clone` and `RandomlyMutable` are present.
#[derive(Clone, Debug, Mitosis)]
#[mitosis(use_randmut = true)]
struct MyGenome {
field1: f32,
}
// required in all of the builtin Repopulators as requirements of `Mitosis` and `Crossover`
impl RandomlyMutable for MyGenome {
type Context = (); // empty context for a simple mutation
fn mutate(&mut self, _ctx: &(), rate: f32, rng: &mut impl Rng) {
self.field1 += rng.random::<f32>() * rate;
}
}
// allows us to use `Vec::gen_random` for the initial population. note that with the `rayon` feature, we can also use `Vec::par_gen_random`.
impl GenerateRandom for MyGenome {
fn gen_random(rng: &mut impl Rng) -> Self {
Self { field1: rng.random() }
}
}
fn my_fitness_fn(ent: &MyGenome) -> f32 {
// this just means that the algorithm will try to create as big a number as possible due to fitness being directly taken from the field.
// in a more complex genetic algorithm, you will want to utilize `ent` to test them and generate a reward.
ent.field1
}
fn main() {
let mut rng = rand::rng();
let mut sim = GeneticSim::new(
// you must provide a random starting population.
// size will be preserved in builtin repopulators, but it is not required to keep a constant size if you were to build your own.
// in this case, the compiler can infer the type of `Vec::gen_random` because of the input of `my_fitness_fn`.
Vec::gen_random(&mut rng, 100),
FitnessEliminator::new_without_observer(my_fitness_fn),
MitosisRepopulator::new(0.25, ()), // 25% mutation rate, empty context
);
// perform evolution (100 gens)
sim.perform_generations(100);
dbg!(sim.genomes);
}That is the minimal code for a working genetic algorithm with just the derive feature (+ defaults). You can read the docs or check the examples for more complicated systems. I highly recommend looking into crossover reproduction, as it tends to produce better results than mitosis.
§License
This project falls under the MIT license.
Re-exports§
pub extern crate genetic_rs_common;
Modules§
- builtin
builtin - Built-in nextgen functions and traits to go with them.
- prelude
- speciation
speciation - Common speciation code used for speciated eliminators and repopulators.
Structs§
- Genetic
Sim - This struct is the main entry point for the simulation. It handles the state and evolution of the genomes based on what eliminator and repopulator it receives.
Traits§
- Eliminator
- Tests and eliminates the unfit from the simulation.
- Generate
Random genrand - Helper trait used in the generation of random starting populations
- Generate
Random Collection genrand - Blanket trait used on collections that contain objects implementing
GenerateRandom - Generate
Random Collection Parallel genrandandrayon - Rayon version of the
GenerateRandomCollectiontrait - Repopulator
- Refills the population of the simulation based on survivors.
Derive Macros§
- Crossover
deriveandcrossover - Generate
Random deriveandgenrand - Mitosis
derive - Randomly
Mutable derive