RUST genetic algorithms library
Description
This library provides a simple framework to implement genetic algorithms (GA) with Rust.
This library also supports multithreading.
Table of content
Documentation
See docs.rs
Features
Traits
This version uses traits for generic implementations.
These traits are within the traits
module:
GeneT
: This trait must be implemented on your own gene representation.new()
: This is the constructor function.get_id()
: Optional. This function must return the id of the gene.
GenotypeT
: This trait must be implemented on your own genotype representation.Gene
: This is theGeneT
associated type.new()
: This is the constructor function.new_gene()
: Optional. Must returnSelf::Gene
.get_dna()
: Must return the array of genes (GeneT
).set_dna(dna: &[Self::Gene])
: Must set the array of genes (GeneT
).set_gene(gene_index: usize, gene: Self::Gene)
: Optional. This method will replace a gene in the defined gene_index position.calculate_fitness()
: Optional. This function must calculate the fitness of the indivudual (or the genotype) in f64.get_fitness()
: Returns the fitness previously calculated bycalculate_fitness()
.set_fitness(fitness: f64)
: Sets the fitness value.get_age()
: Returns the age of the genotype.set_age(age: i32)
: Sets the age of the genotype.
Operators
Within the module operations
we have the following operators:
- Crossover
- Cycle
- Multipoint
- Uniform
- Mutation
- Swap
- Inversion
- Scramble
- Selection
- Random
- Roulette Wheel
- Stochastic Universal Sampling
- Tournament
- Survivor
- Fitness based
- Age based
Population
In genetic algorithms, operators are applied over a population of individuals, and over a set of rules (not yet implemented).
Within the population
module, Population
structure will define the population.
Runner
Because genetic algorithms run over different generations, in this library there is a start
function within module ga
that facilitates the process.
This function will need the GaConfiguration
structure which contains the operators to use, the maximum number of generations, the problem solver (Maximization or Minimization), etc, and the Population
structure, which is in the population
module.
GA Configuration
Within this library you can configure the way to run genetic algorithms through the configuration structure GaConfiguration
.
This structure contains the following attributes:
number_of_threads
: Optional. It indicates how many threads will be executed at the same time.limit_configuration
: It configures the limits of the Genetic Algorithms with theLimitConfiguration
structure.selection_configuration
: It configures the selection method with theSelectionConfiguration
structure.crossover_configuration
: It configures the crossover method with theCrossoverConfiguration
structure.mutation_configuration
: It configures the mutation method with theMutationConfiguration
structure.survivor
: Indicates what survivor operator to use.log_level
: Optional. It configures the maximum log level we want to have. If this value is none, logs are disabled.
SelectionConfiguration
:
number_of_couples
: Optional. This attribute is only valid for stochastic universal sampling. It indicates the number of couples to select from the population.method
: Indicates what selection operator to use.
CrossoverConfiguration
:
number_of_points
: Optional. This attribute is only valid for crossover multipoint, and it indicates how many points will be made within the dna in crossover operations.probability
: Optional. Indicates the probability of two parents for being crossed. This number must be between 0.0 and 1.0 both inclusive.method
: Indicates what crossover operator to use.
MutationConfiguration
:
probability
: Optional. Indicates the probability for a given child to be mutated. This number must be between 0.0 and 1.0 both inclusive.method
: Indicates what mutation operator to use.
LimitConfiguration
:
problem_solving
: You can select from a Minimization problem or a Maximization problem.max_generations
: In case of not getting the optimal result, this attribute indicates the maximum number of generations to execute before stopping.fitness_target
: Optional. The fitness of the best individual.get_best_individual_by_generation
: Optional. Indicates to the runner to return the best individual by generation.
Example
A simple example of use could be the minimization of a genotype whose gene has only an id.
Creation of the gene and genotype structure
Use the traits.
use genetic_algorithms::{ga::run, operations::{Selection, Crossover, Mutation, Survivor}, population::Population, traits::GenotypeT, configuration::{GaConfiguration, ProblemSolving, LimitConfiguration}};
Define the gene structure.
Define the genotype structure, and the fitness calculation.
Define the configuration of the GA.
let configuration = GaConfiguration;
Define the DNA, the individuals and the population.
let dna_1 = vec!;
let dna_2 = vec!;
let dna_3 = vec!;
let dna_4 = vec!;
let dna_5 = vec!;
let dna_6 = vec!;
let dna_7 = vec!;
let dna_8 = vec!;
let dna_9 = vec!;
let dna_10 = vec!;
let individuals = vec!;
let mut population = new;
Finally, run the GA.
population = run;
Other examples
- Travelling salesman problem: https://en.wikipedia.org/wiki/Travelling_salesman_problem
Usage
Add this to your Cargo.toml
:
[]
= "1.0.1"