GenePlace enum

This commit is contained in:
rendo 2026-04-01 11:10:56 +05:00
commit 85faa9be22
3 changed files with 53 additions and 20 deletions

View file

@ -5,7 +5,7 @@ use crate::genetics::flow::Flow;
#[derive(PartialEq,Eq,Clone)]
pub struct Gene {
pub plant_source: GeneSource,
pub place: String,
pub place: GenePlace,
pub kind: GeneType
}
@ -21,6 +21,15 @@ pub enum GeneSource {
PotatoMine,
}
#[derive(PartialEq,Eq,Clone,Copy)]
pub enum GenePlace {
Root,
Stem,
Head,
Face,
Leaf,
}
/// GeneType is an enum that defines behaviour of gene.
#[derive(PartialEq,Eq,Clone)]
pub enum GeneType {
@ -75,10 +84,10 @@ pub enum ProduceDecision {
}
impl Gene {
pub fn new(plant_source: GeneSource, place_str: impl Into<String>, kind: GeneType) -> Self {
pub fn new(plant_source: GeneSource, place: GenePlace, kind: GeneType) -> Self {
Self {
plant_source,
place: place_str.into(),
place,
kind,
}
}
@ -96,6 +105,19 @@ impl Display for GeneSource {
}
}
impl Display for GenePlace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"{}",match self {
GenePlace::Root => "Root",
GenePlace::Stem => "Stem",
GenePlace::Head => "Head",
GenePlace::Face => "Face",
GenePlace::Leaf => "Leaf",
});
Ok(())
}
}
impl Display for Gene {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"({}) {}",self.plant_source,self.place);
@ -142,3 +164,14 @@ impl GeneType {
}
}
impl GenePlace {
pub fn broad(&self) -> GenePlace {
match self {
GenePlace::Root => GenePlace::Root,
GenePlace::Stem => GenePlace::Stem,
GenePlace::Head => GenePlace::Head,
GenePlace::Face => GenePlace::Face,
GenePlace::Leaf => GenePlace::Leaf,
}
}
}

View file

@ -48,10 +48,10 @@ impl PairGenomeModificator {
let mut categories_b: HashMap<String, Vec<usize>> = HashMap::new();
for (i, (gene, _)) in self.genome_a.graph.iter().enumerate() {
categories_a.entry(gene.place.clone()).or_default().push(i);
categories_a.entry(gene.place.to_string()).or_default().push(i);
}
for (i, (gene, _)) in self.genome_b.graph.iter().enumerate() {
categories_b.entry(gene.place.clone()).or_default().push(i);
categories_b.entry(gene.place.to_string()).or_default().push(i);
}
for (category, indices_a) in &categories_a {

View file

@ -1,12 +1,12 @@
use crate::genetics::{flow::Flow, gene::{GeneSource, GeneType,Gene}, genome::Genome};
use crate::genetics::{flow::Flow, gene::{GenePlace,GeneSource, GeneType,Gene}, genome::Genome};
pub fn peashooter_template() -> Genome {
Genome::from_edges(vec![
Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})),
Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()),
Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()),
Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})),
Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{}))
Gene::new(GeneSource::Peashooter,GenePlace::Root, GeneType::pure_producer(|_|{Flow::empty()})),
Gene::new(GeneSource::Peashooter,GenePlace::Stem, GeneType::fill_all()),
Gene::new(GeneSource::Peashooter,GenePlace::Head,GeneType::random_distribution()),
Gene::new(GeneSource::Peashooter,GenePlace::Leaf,GeneType::modifier(|_|{Flow::empty()})),
Gene::new(GeneSource::Peashooter,GenePlace::Face,GeneType::consumer(|_|{}))
],vec![
(0,1),
(1,2),
@ -17,10 +17,10 @@ pub fn peashooter_template() -> Genome {
pub fn sunflower_template() -> Genome {
Genome::from_edges(vec![
Gene::new(GeneSource::Sunflower,"root", GeneType::Structural),
Gene::new(GeneSource::Sunflower,"stem", GeneType::Structural),
Gene::new(GeneSource::Sunflower,"head", GeneType::modifier(|_|{Flow::empty()})),
Gene::new(GeneSource::Sunflower,"face", GeneType::consumer(|_|{})),
Gene::new(GeneSource::Sunflower,GenePlace::Root, GeneType::Structural),
Gene::new(GeneSource::Sunflower,GenePlace::Stem, GeneType::Structural),
Gene::new(GeneSource::Sunflower,GenePlace::Head, GeneType::modifier(|_|{Flow::empty()})),
Gene::new(GeneSource::Sunflower,GenePlace::Face, GeneType::consumer(|_|{})),
], vec![
(0,1),
(1,2),
@ -30,11 +30,11 @@ pub fn sunflower_template() -> Genome {
pub fn cherry_bomb_template() -> Genome {
Genome::from_edges(vec![
Gene::new(GeneSource::CherryBomb,"root", GeneType::modifier(|_|{Flow::empty()})),
Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})),
Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})),
Gene::new(GeneSource::CherryBomb,"face", GeneType::Structural),
Gene::new(GeneSource::CherryBomb,"face", GeneType::Structural),
Gene::new(GeneSource::CherryBomb,GenePlace::Root, GeneType::modifier(|_|{Flow::empty()})),
Gene::new(GeneSource::CherryBomb,GenePlace::Head, GeneType::consumer(|_|{})),
Gene::new(GeneSource::CherryBomb,GenePlace::Head, GeneType::consumer(|_|{})),
Gene::new(GeneSource::CherryBomb,GenePlace::Face, GeneType::Structural),
Gene::new(GeneSource::CherryBomb,GenePlace::Face, GeneType::Structural),
], vec![
(0,1),
(0,2),