RUST genetic algorithms library
Description
This library provides a simple framework for implementing genetic algorithms (GA) with Rust.
This library also supports multithreading.
Table of content
Documentation
See docs.rs
Features
Traits
This release uses traits for generic implementations.
These traits are inside 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 replaces a gene at the specified gene_index position.calculate_fitness()
: Optional. This function must calculate the fitness of the individual (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 operations
module 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, the Population
structure will define the population.
Runner
Since genetic algorithms run over several generations, there is a start
function in this library within the ga
module that facilitates the process.
This function needs the GaConfiguration
structure, which contains the operators to be used, the maximum number of generations, the problem solver (Maximization or Minimization), etc., and the Population
structure, which is in the population
module.
Initialization
If it is desired to perform the initialization of the population randomly through this library, it can be done through the function random_initialization
. This function requires the following parameters:
alleles
: This is an array containing the elements that can be combined in the population. They have to comply with theGenotypeT
trait.population_size
: Indicates the size of the population we want to have.genes_per_individual
: Sets the number of genes that each individual in the population has.needs_unique_ids
: This variable indicates whether each gene of each individual has to contain a unique ID. This is useful mainly for thecycle
crossover method.alleles_can_be_repeated
: Within an individual, the same allele can be repeated several times. This variable indicates whether this behavior is desired, or whether it is preferred that the alleles be unique within each individual.
GA Configuration
Within this library, you can configure the way genetic algorithms are executed by using the configuration structure GaConfiguration
.
This structure has the following attributes:
adaptive_ga
: Specifies if the Genetic Algorithms are adaptive or not.number_of_threads
: Optional. Indicates how many threads will be executed simultaneously.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
: Specifies which survivor operator to use.log_level
: Optional. It configures the maximum log level we want to have. If this value is none, logs will be disabled.
SelectionConfiguration
:
number_of_couples
: Optional. This attribute applies only to stochastic universal sampling. It specifies the number of pairs to select from the population.method
: Specifies which selection operator to use.
CrossoverConfiguration
:
number_of_points
: Optional. This attribute is only valid for crossover multipoint and indicates how many points are made within the DNA during crossover operations.probability_max
: Optional. Specifies the maximum probability that two parents are crossed. This number must be between 0.0 and 1.0, both inclusive. In case of adaptive genetic algorithms, this parameter is mandatory and must be greater thanprobability_min
.probability_min
: Optional. Specifies the minimum probability that two parents are crossed. This number must be between 0.0 and 1.0, both inclusive. In case of adaptive genetic algorithms, this parameter is mandatory and must be lower thanprobability_max
.method
: Specifies which crossover operator to use.
MutationConfiguration
:
probability
: Optional. Specifies the probability that a given child will be mutated. This number must be between 0.0 and 1.0, both inclusive.method
: Specifies which mutation operator to use.
LimitConfiguration
:
problem_solving
: You can choose between a minimization problem and a maximization problem.max_generations
: If the result is not optimal, this attribute indicates the maximum number of generations to run before stopping.fitness_target
: Optional. The fitness of the best individual.get_best_individual_by_generation
: Optional. Tells the runner to return the best individual by generation.
Example
A simple example of use could be minimizing a genotype whose gene has only one 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 Alleles, and initialize the population.
let binding = vec!;
let alleles = binding.as_slice;
static GENES_PER_INDIVIDUAL: i32 = 6;
static POPULATION_SIZE: i32 = 100;
static NEEDS_UNIQUE_IDS: bool = false;
static ALLELES_CAN_BE_REPEATED: bool = false;
static NUMBER_OF_THREADS: i32 = 8;
let population = ;
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.1.0"