Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
pkalivas committed Dec 18, 2024
2 parents 82c63fa + 5fe913f commit e3a2a75
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 30 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019-2024 Peter Kalivas
Copyright (c) 2019-2025 Peter Kalivas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions radiate/src/engines/codexes/bit_codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use super::Codex;
/// let bit_string: Vec<bool> = codex.decode(&genotype)[0].clone();
/// ```
pub struct BitCodex {
pub num_chromosomes: usize,
pub num_genes: usize,
num_chromosomes: usize,
num_genes: usize,
}

impl BitCodex {
Expand Down
4 changes: 2 additions & 2 deletions radiate/src/engines/codexes/char_codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use super::Codex;
/// and `num_genes` genes per chromosome. The `decode` function creates a `String` from the `Genotype` where the `String`
/// contains the alleles of the `CharGenes` in the chromosome.
pub struct CharCodex {
pub num_chromosomes: usize,
pub num_genes: usize,
num_chromosomes: usize,
num_genes: usize,
}

impl CharCodex {
Expand Down
12 changes: 6 additions & 6 deletions radiate/src/engines/codexes/float_codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use crate::{Chromosome, FloatChromosome};
/// The lower and upper bounds of the `FloatGenes` can be set with the `with_bounds` function.
/// The default bounds are `f32::MIN` and `f32::MAX`.
pub struct FloatCodex {
pub num_chromosomes: usize,
pub num_genes: usize,
pub min: f32,
pub max: f32,
pub lower_bound: f32,
pub upper_bound: f32,
num_chromosomes: usize,
num_genes: usize,
min: f32,
max: f32,
lower_bound: f32,
upper_bound: f32,
}

impl FloatCodex {
Expand Down
18 changes: 8 additions & 10 deletions radiate/src/engines/codexes/fn_codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ use crate::{Chromosome, Codex, Genotype};
///
#[derive(Default)]
pub struct FnCodex<C: Chromosome, T> {
pub encoder: Option<Box<dyn Fn() -> Genotype<C>>>,
pub decoder: Option<Box<dyn Fn(&Genotype<C>) -> T>>,
encoder: Option<Box<dyn Fn() -> Genotype<C>>>,
decoder: Option<Box<dyn Fn(&Genotype<C>) -> T>>,
}

impl<C: Chromosome, T> FnCodex<C, T> {
Expand Down Expand Up @@ -71,18 +71,16 @@ impl<C: Chromosome, T> FnCodex<C, T> {

impl<C: Chromosome, T> Codex<C, T> for FnCodex<C, T> {
fn encode(&self) -> Genotype<C> {
if self.encoder.is_none() {
panic!("Encoder function is not set");
match &self.encoder {
Some(encoder) => encoder(),
None => panic!("Encoder function is not set"),
}

self.encoder.as_ref().unwrap()()
}

fn decode(&self, genotype: &Genotype<C>) -> T {
if self.decoder.is_none() {
panic!("Decoder function is not set");
match &self.decoder {
Some(decoder) => decoder(genotype),
None => panic!("Decoder function is not set"),
}

self.decoder.as_ref().unwrap()(genotype)
}
}
12 changes: 6 additions & 6 deletions radiate/src/engines/codexes/int_codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub struct IntCodex<T: Integer<T>>
where
Standard: rand::distributions::Distribution<T>,
{
pub num_chromosomes: usize,
pub num_genes: usize,
pub min: T,
pub max: T,
pub lower_bound: T,
pub upper_bound: T,
num_chromosomes: usize,
num_genes: usize,
min: T,
max: T,
lower_bound: T,
upper_bound: T,
}

impl<T: Integer<T>> IntCodex<T>
Expand Down
7 changes: 7 additions & 0 deletions radiate/src/engines/genome/chromosomes/bit_chromosome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ impl Valid for BitChromosome {
self.genes.iter().all(|gene| gene.is_valid())
}
}

impl From<&[bool]> for BitChromosome {
fn from(alleles: &[bool]) -> Self {
let genes = alleles.iter().map(BitGene::from).collect();
BitChromosome { genes }
}
}
6 changes: 3 additions & 3 deletions radiate/src/engines/genome/chromosomes/char_chromosome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ impl From<&'static str> for CharChromosome {
}
}

impl From<Vec<char>> for CharChromosome {
fn from(alleles: Vec<char>) -> Self {
let genes = alleles.into_iter().map(CharGene::from).collect();
impl From<&[char]> for CharChromosome {
fn from(alleles: &[char]) -> Self {
let genes = alleles.iter().map(CharGene::from).collect();
CharChromosome { genes }
}
}
7 changes: 7 additions & 0 deletions radiate/src/engines/genome/chromosomes/float_chromosome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ impl From<Range<i32>> for FloatChromosome {
}
}

impl From<&[f32]> for FloatChromosome {
fn from(alleles: &[f32]) -> Self {
let genes = alleles.iter().map(FloatGene::from).collect();
FloatChromosome { genes }
}
}

impl Debug for FloatChromosome {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "FloatChromosome: [")?;
Expand Down
10 changes: 10 additions & 0 deletions radiate/src/engines/genome/chromosomes/int_chromosome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ where
self.genes.iter().all(|gene| gene.is_valid())
}
}

impl<T: Integer<T>> From<&[T]> for IntChromosome<T>
where
Standard: rand::distributions::Distribution<T>,
{
fn from(alleles: &[T]) -> Self {
let genes = alleles.iter().map(IntGene::from).collect();
IntChromosome { genes }
}
}
7 changes: 7 additions & 0 deletions radiate/src/engines/genome/genes/bit_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ impl From<BitGene> for bool {
gene.allele
}
}

impl From<bool> for BitGene {
fn from(allele: bool) -> BitGene {
BitGene { allele }
}
}

impl From<&bool> for BitGene {
fn from(allele: &bool) -> BitGene {
BitGene { allele: *allele }
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
6 changes: 6 additions & 0 deletions radiate/src/engines/genome/genes/char_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ impl From<char> for CharGene {
}
}

impl From<&char> for CharGene {
fn from(allele: &char) -> Self {
CharGene { allele: *allele }
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
12 changes: 12 additions & 0 deletions radiate/src/engines/genome/genes/float_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ impl From<f32> for FloatGene {
}
}

impl From<&f32> for FloatGene {
fn from(allele: &f32) -> Self {
FloatGene {
allele: *allele,
min: f32::MIN,
max: f32::MAX,
upper_bound: f32::MAX,
lower_bound: f32::MIN,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions radiate/src/engines/genome/genes/int_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ where
}
}

impl<T: Integer<T>> From<&T> for IntGene<T>
where
Standard: rand::distributions::Distribution<T>,
{
fn from(allele: &T) -> Self {
IntGene::new(*allele)
}
}

impl From<IntGene<i8>> for i8 {
fn from(gene: IntGene<i8>) -> Self {
gene.allele
Expand Down
2 changes: 2 additions & 0 deletions radiate/src/engines/genome/genes/permutation_gene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ impl<A: PartialEq + Clone> Gene for PermutationGene<A> {
}

fn with_allele(&self, allele: &Self::Allele) -> Self {
// Find the index of the allele in the alleles vector - this implies that `self.alleles`
// is a set of unique values.
let index = self.alleles.iter().position(|x| x == allele).unwrap();
PermutationGene {
index,
Expand Down

0 comments on commit e3a2a75

Please sign in to comment.