use crate::{Chromosome, Gene, Valid, random_provider};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display};
#[derive(Clone, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(transparent)]
pub struct BitGene {
allele: bool,
}
impl BitGene {
pub fn new() -> Self {
BitGene {
allele: random_provider::bool(0.5),
}
}
}
impl Gene for BitGene {
type Allele = bool;
fn allele(&self) -> &bool {
&self.allele
}
fn allele_mut(&mut self) -> &mut bool {
&mut self.allele
}
fn new_instance(&self) -> BitGene {
BitGene::new()
}
fn with_allele(&self, allele: &bool) -> BitGene {
BitGene { allele: *allele }
}
}
impl Valid for BitGene {}
impl Display for BitGene {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", if self.allele { 1 } else { 0 })
}
}
impl From<BitGene> for bool {
fn from(gene: BitGene) -> bool {
gene.allele
}
}
impl From<bool> for BitGene {
fn from(allele: bool) -> BitGene {
BitGene { allele }
}
}
#[derive(Clone, PartialEq, Default, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BitChromosome {
genes: Vec<BitGene>,
}
impl BitChromosome {
pub fn new(length: usize) -> Self {
let genes = (0..length).map(|_| BitGene::new()).collect();
BitChromosome { genes }
}
}
impl Chromosome for BitChromosome {
type Gene = BitGene;
fn as_slice(&self) -> &[Self::Gene] {
&self.genes
}
fn as_mut_slice(&mut self) -> &mut [Self::Gene] {
&mut self.genes
}
}
impl Valid for BitChromosome {
fn is_valid(&self) -> bool {
true
}
}
impl From<BitGene> for BitChromosome {
fn from(gene: BitGene) -> Self {
BitChromosome { genes: vec![gene] }
}
}
impl From<Vec<BitGene>> for BitChromosome {
fn from(genes: Vec<BitGene>) -> Self {
BitChromosome { genes }
}
}
impl From<Vec<bool>> for BitChromosome {
fn from(alleles: Vec<bool>) -> Self {
BitChromosome {
genes: alleles.into_iter().map(BitGene::from).collect(),
}
}
}
impl FromIterator<BitGene> for BitChromosome {
fn from_iter<I: IntoIterator<Item = BitGene>>(iter: I) -> Self {
BitChromosome {
genes: iter.into_iter().collect(),
}
}
}
impl IntoIterator for BitChromosome {
type Item = BitGene;
type IntoIter = std::vec::IntoIter<BitGene>;
fn into_iter(self) -> Self::IntoIter {
self.genes.into_iter()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_into() {
let gene = BitGene::new();
let copy = gene.clone();
let allele: bool = gene.into();
assert_eq!(allele, copy.allele);
}
#[test]
fn test_from() {
let gene = BitGene::new();
let copy = gene.clone();
let allele: BitGene = copy;
assert_eq!(allele, gene);
}
#[test]
fn test_from_allele() {
let gene = BitGene::new();
let copy = gene.clone();
let allele = gene.allele();
let new_gene = gene.with_allele(allele);
assert_eq!(new_gene, copy);
}
#[test]
#[cfg(feature = "serde")]
fn test_bit_gene_serialization() {
let gene = BitGene::new();
let serialized = serde_json::to_string(&gene).expect("Failed to serialize BitGene");
let deserialized: BitGene =
serde_json::from_str(&serialized).expect("Failed to deserialize BitGene");
let chromosome = BitChromosome::new(10);
let serialized_chromosome =
serde_json::to_string(&chromosome).expect("Failed to serialize BitChromosome");
let deserialized_chromosome: BitChromosome = serde_json::from_str(&serialized_chromosome)
.expect("Failed to deserialize BitChromosome");
assert_eq!(gene, deserialized);
assert_eq!(chromosome, deserialized_chromosome);
}
}