Crate evolutionary

source ·
Expand description

Evolutionary

A fully extensible Rust framework for using paralyzed genetic algorithms to solve problems.

Currently, it supports coding in Bin, Real, Permuted Integers, Integers and any other coding you may want to implement. It also has built in implementation of the following genetic operators:

You can code your own selection, crossover or mutation implementing the traits and passing them to the EvolutionBuilder.

Example:

use evolutionary::prelude::*;

// First you'll need to code your Fitness function:
#[derive(Clone)]
pub struct MaxFitness;

// To do this you need to implement the Fitness trait.
impl Fitness<Bin> for MaxFitness {
    fn calculate_fitness(&self, individual: &Bin) -> f64 {
        let mut sum = 0.;

        for i in 0..individual.get_chromosome().len() {
            if individual.get_gene(i) {
                sum += 1.;
            }
        }

        sum
    }
}

// Then you will be able to build a evolution object using the `EvolutionBuiler`
// and setting all the required parameters:
fn main() {
    let mut evolution = EvolutionBuilder::new(30, 10, GeneCod::Bin, ())
        .with_fitness(MaxFitness)
        .with_selection(TournamentSelection::default())
        .with_crossover(NPointsCrossover::default())
        .with_mutation(BitFlipMutation::default())
        .with_title("Max".to_string())
        .with_stop_condition(move |_, iterations, _| iterations >= 100)
        .build().unwrap();

    evolution.run();

    assert_eq!(*evolution.current_best().get_chromosome(), vec![true; 10]);
    assert_eq!(evolution.current_best_fitness(), 10.0);
}

Find this and other examples in the examples folder.

Modules

Structs

  • This is the struct that does the evolution magic. It has the methods needed to start and iterate through the evolution.
  • This is the helper struct to create a new Evolution object. The fitness, selection, crossover, mutation and stop_condition are required.

Traits

  • Trait that defines the crossover method. You can implement your own crossover method by implementing this trait.
  • Trait that defines the fitness function and that will be used by the evolutionary algorithm. You need to implement your own fitness function for each problem you’re trying to solve. The fitness function is directly related to the type of individual you’re using and how your mapping the answers.
  • Trait that must be implemented by a struct to be considered a individual.
  • Mutation Trait
  • Trait that defines the selection method. You can implement your own selection method by implementing this trait.