Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pkalivas committed Nov 23, 2024
1 parent cd766ee commit 9eb9a7e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
17 changes: 17 additions & 0 deletions radiate/src/engines/genome/genes/bit_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ use crate::RandomProvider;

use super::gene::{Gene, Valid};

/// A gene that represents a single bit. The `allele` is a `bool` that is randomly assigned.
/// The `allele` is either `true` or `false`. This is the simplest form of a gene and
/// in traditional genetic algorithms is the the gene that is used to represent the individuals.
///
/// # Example
/// ``` rust
/// use radiate::*;
///
/// // Create a new BitGene with a random allele.
/// let gene = BitGene::new();
///
/// // Get the allele of the BitGene.
/// let allele = gene.allele();
///
/// // Create a new BitGene from the allele.
/// let gene = gene.from_allele(allele);
/// ```
pub struct BitGene {
allele: bool,
}
Expand Down
16 changes: 16 additions & 0 deletions radiate/src/engines/genome/genes/char_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ use super::gene::{Gene, Valid};

const ALPHABET: &str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"$%&/()=?`{[]}\\+~*#';.:,-_<>|@^' ";

/// A gene that represents a single character. The `allele` is a `char`
/// that is randomly selected from the `ALPHABET` constant.
///
/// # Example
/// ``` rust
/// use radiate::*;
///
/// // Create a new CharGene with a random allele from the ALPHABET constant.
/// let gene = CharGene::new();
///
/// // Get the allele of the CharGene.
/// let allele = gene.allele();
///
/// // Create a new CharGene from the allele.
/// let gene = gene.from_allele(allele);
/// ```
pub struct CharGene {
pub allele: char,
}
Expand Down
25 changes: 25 additions & 0 deletions radiate/src/engines/genome/genes/float_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ use crate::RandomProvider;

use super::gene::{BoundGene, Gene, NumericGene, Valid};

/// A `Gene` that represents a floating point number.
/// The `allele` is the in the case of the `FloatGene` a f32. The `min` and `max` values
/// default to f32::MIN and f32::MAX respectively. The `min` and `max` values are used to
/// generate a random number between the `min` and `max` values, which is the `allele` of the `FloatGene`.
/// The `upper_bound` and `lower_bound` are used to set the bounds of the `FloatGene` when it is used
/// in a `BoundGene` context (crossover or mutation). The `upper_bound` and `lower_bound` default to f32::MAX and f32::MIN respectively.
///
/// # Example
/// ``` rust
/// use radiate::*;
///
/// // Create a new FloatGene with a min value of 0 and a max value of 1 meaning the
/// // allele will be a random number between 0 and 1.
/// // The upper_bound and lower_bound are set to f32::MAX and f32::MIN respectively.
/// let gene = FloatGene::new(0_f32, 1_f32);
///
/// // Create a new FloatGene with a min of 0 and a max of 1 and set the upper_bound
/// // and lower_bound to 0 and 100 respectively.
/// let gene = FloatGene::new(0_f32, 1_f32).with_bounds(100_f32, 0_f32);
/// ```
pub struct FloatGene {
pub allele: f32,
pub min: f32,
Expand All @@ -22,6 +42,11 @@ impl FloatGene {
}
}

/// Implement the `Valid` trait for the `FloatGene`.
///
/// The `is_valid` method checks if the `allele` of the `FloatGene` is between the `min` and `max` values.
/// The `GeneticEngine` will check the validity of the `Chromosome` and `Phenotype` and remove any
/// invalid individuals from the population, replacing them with new individuals at the given generation.
impl Valid for FloatGene {
fn is_valid(&self) -> bool {
self.allele >= self.min && self.allele <= self.max
Expand Down

0 comments on commit 9eb9a7e

Please sign in to comment.