Gene refactor
This commit is contained in:
parent
85faa9be22
commit
5abbbc3bf9
8 changed files with 114 additions and 106 deletions
34
rust-pvz-genetics/src/genetics/gene/gene_place.rs
Normal file
34
rust-pvz-genetics/src/genetics/gene/gene_place.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(PartialEq,Eq,Clone,Copy)]
|
||||||
|
pub enum GenePlace {
|
||||||
|
Root,
|
||||||
|
Stem,
|
||||||
|
Head,
|
||||||
|
Face,
|
||||||
|
Leaf,
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
rust-pvz-genetics/src/genetics/gene/gene_source.rs
Normal file
20
rust-pvz-genetics/src/genetics/gene/gene_source.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(PartialEq,Eq,Clone,Copy)]
|
||||||
|
pub enum GeneSource {
|
||||||
|
Peashooter,
|
||||||
|
Sunflower,
|
||||||
|
CherryBomb,
|
||||||
|
PotatoMine,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for GeneSource {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f,"{}",match self {
|
||||||
|
GeneSource::Peashooter => "Peashooter",
|
||||||
|
GeneSource::Sunflower => "Sunflower",
|
||||||
|
GeneSource::CherryBomb => "CherryBomb",
|
||||||
|
GeneSource::PotatoMine => "PotatoMine",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,35 +1,5 @@
|
||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use crate::genetics::flow::Flow;
|
use crate::genetics::flow::Flow;
|
||||||
|
|
||||||
#[derive(PartialEq,Eq,Clone)]
|
|
||||||
pub struct Gene {
|
|
||||||
pub plant_source: GeneSource,
|
|
||||||
pub place: GenePlace,
|
|
||||||
pub kind: GeneType
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct GeneContext {
|
|
||||||
pub flow: Flow
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq,Eq,Clone,Copy)]
|
|
||||||
pub enum GeneSource {
|
|
||||||
Peashooter,
|
|
||||||
Sunflower,
|
|
||||||
CherryBomb,
|
|
||||||
PotatoMine,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq,Eq,Clone,Copy)]
|
|
||||||
pub enum GenePlace {
|
|
||||||
Root,
|
|
||||||
Stem,
|
|
||||||
Head,
|
|
||||||
Face,
|
|
||||||
Leaf,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// GeneType is an enum that defines behaviour of gene.
|
/// GeneType is an enum that defines behaviour of gene.
|
||||||
#[derive(PartialEq,Eq,Clone)]
|
#[derive(PartialEq,Eq,Clone)]
|
||||||
pub enum GeneType {
|
pub enum GeneType {
|
||||||
|
|
@ -70,6 +40,9 @@ pub enum GeneType {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct GeneContext {
|
||||||
|
pub flow: Flow
|
||||||
|
}
|
||||||
pub enum ChildDistribution{
|
pub enum ChildDistribution{
|
||||||
Single,
|
Single,
|
||||||
Multiple{amount: usize},
|
Multiple{amount: usize},
|
||||||
|
|
@ -83,48 +56,6 @@ pub enum ProduceDecision {
|
||||||
ModifySingle,
|
ModifySingle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Gene {
|
|
||||||
pub fn new(plant_source: GeneSource, place: GenePlace, kind: GeneType) -> Self {
|
|
||||||
Self {
|
|
||||||
plant_source,
|
|
||||||
place,
|
|
||||||
kind,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for GeneSource {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f,"{}",match self {
|
|
||||||
GeneSource::Peashooter => "Peashooter",
|
|
||||||
GeneSource::Sunflower => "Sunflower",
|
|
||||||
GeneSource::CherryBomb => "CherryBomb",
|
|
||||||
GeneSource::PotatoMine => "PotatoMine",
|
|
||||||
});
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GeneType {
|
impl GeneType {
|
||||||
/// Creates pure producer with create function.
|
/// Creates pure producer with create function.
|
||||||
pub fn pure_producer(flow_function: fn(GeneContext) -> Flow) -> Self {
|
pub fn pure_producer(flow_function: fn(GeneContext) -> Flow) -> Self {
|
||||||
|
|
@ -163,15 +94,3 @@ impl GeneType {
|
||||||
ChildDistribution::All
|
ChildDistribution::All
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
34
rust-pvz-genetics/src/genetics/gene/mod.rs
Normal file
34
rust-pvz-genetics/src/genetics/gene/mod.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
pub mod gene_types;
|
||||||
|
pub mod gene_place;
|
||||||
|
pub mod gene_source;
|
||||||
|
|
||||||
|
pub use gene_types::GeneType;
|
||||||
|
pub use gene_source::GeneSource;
|
||||||
|
pub use gene_place::GenePlace;
|
||||||
|
|
||||||
|
#[derive(PartialEq,Eq,Clone)]
|
||||||
|
pub struct Gene {
|
||||||
|
pub plant_source: GeneSource,
|
||||||
|
pub place: GenePlace,
|
||||||
|
pub kind: GeneType
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Gene {
|
||||||
|
pub fn new(plant_source: GeneSource, place: GenePlace, kind: GeneType) -> Self {
|
||||||
|
Self {
|
||||||
|
plant_source,
|
||||||
|
place,
|
||||||
|
kind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
impl Display for Gene {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f,"({}) {}",self.plant_source,self.place)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::VecDeque, fmt::Display};
|
use std::fmt::Display;
|
||||||
|
|
||||||
use crate::genetics::gene::Gene;
|
use crate::genetics::gene::Gene;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::genetics::{flow::Flow, gene::{Gene, GeneSource, GeneType}, genome::Genome, manipulations::PairGenomeModificator, plant_templates::{cherry_bomb_template, peashooter_template, sunflower_template}};
|
use crate::genetics::{flow::Flow, gene::{Gene,GenePlace, GeneSource, GeneType}, genome::Genome, manipulations::PairGenomeModificator, plant_templates::{cherry_bomb_template, peashooter_template, sunflower_template}};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_display() {
|
fn test_display() {
|
||||||
let display_target = cherry_bomb_template();
|
let display_target = cherry_bomb_template();
|
||||||
println!("{}",display_target);
|
println!("{}",display_target);
|
||||||
assert!(display_target.to_string() == "(CherryBomb) root\n\t(CherryBomb) head\n\t\t(CherryBomb) face\n\t(CherryBomb) head\n\t\t(CherryBomb) face\n");
|
assert!(display_target.to_string() == "(CherryBomb) Root\n\t(CherryBomb) Head\n\t\t(CherryBomb) Face\n\t(CherryBomb) Head\n\t\t(CherryBomb) Face\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -21,11 +21,11 @@ fn test_allelic_crossingover() {
|
||||||
println!("Sunflower:\n{}",sunflower);
|
println!("Sunflower:\n{}",sunflower);
|
||||||
|
|
||||||
assert!(peashooter.to_string() == Genome::from_edges(vec![
|
assert!(peashooter.to_string() == Genome::from_edges(vec![
|
||||||
Gene::new(GeneSource::Sunflower,"root", GeneType::Structural),
|
Gene::new(GeneSource::Sunflower,GenePlace::Root, GeneType::Structural),
|
||||||
Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()),
|
Gene::new(GeneSource::Peashooter,GenePlace::Stem, GeneType::fill_all()),
|
||||||
Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()),
|
Gene::new(GeneSource::Peashooter,GenePlace::Head,GeneType::random_distribution()),
|
||||||
Gene::new(GeneSource::Sunflower,"face", GeneType::consumer(|_|{})),
|
Gene::new(GeneSource::Sunflower,GenePlace::Face, GeneType::consumer(|_|{})),
|
||||||
Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{}))
|
Gene::new(GeneSource::Peashooter,GenePlace::Face,GeneType::consumer(|_|{}))
|
||||||
],vec![
|
],vec![
|
||||||
(0,1),
|
(0,1),
|
||||||
(1,2),
|
(1,2),
|
||||||
|
|
@ -34,10 +34,10 @@ fn test_allelic_crossingover() {
|
||||||
]).unwrap().to_string());
|
]).unwrap().to_string());
|
||||||
|
|
||||||
assert!(sunflower.to_string() == Genome::from_edges(vec![
|
assert!(sunflower.to_string() == Genome::from_edges(vec![
|
||||||
Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})),
|
Gene::new(GeneSource::Peashooter,GenePlace::Root, GeneType::pure_producer(|_|{Flow::empty()})),
|
||||||
Gene::new(GeneSource::Sunflower,"stem", GeneType::Structural),
|
Gene::new(GeneSource::Sunflower,GenePlace::Stem, GeneType::Structural),
|
||||||
Gene::new(GeneSource::Sunflower,"head", GeneType::modifier(|_|{Flow::empty()})),
|
Gene::new(GeneSource::Sunflower,GenePlace::Head, GeneType::modifier(|_|{Flow::empty()})),
|
||||||
Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})),
|
Gene::new(GeneSource::Peashooter,GenePlace::Leaf,GeneType::modifier(|_|{Flow::empty()})),
|
||||||
], vec![
|
], vec![
|
||||||
(0,1),
|
(0,1),
|
||||||
(1,2),
|
(1,2),
|
||||||
|
|
@ -59,11 +59,11 @@ fn test_categoric_crossingover() {
|
||||||
println!("CherryBomb:\n{}",cherry);
|
println!("CherryBomb:\n{}",cherry);
|
||||||
|
|
||||||
assert!(peashooter.to_string() == Genome::from_edges(vec![
|
assert!(peashooter.to_string() == Genome::from_edges(vec![
|
||||||
Gene::new(GeneSource::CherryBomb,"root", GeneType::modifier(|_|{Flow::empty()})),
|
Gene::new(GeneSource::CherryBomb,GenePlace::Root, GeneType::modifier(|_|{Flow::empty()})),
|
||||||
Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()),
|
Gene::new(GeneSource::Peashooter,GenePlace::Stem, GeneType::fill_all()),
|
||||||
Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})),
|
Gene::new(GeneSource::CherryBomb,GenePlace::Head, GeneType::consumer(|_|{})),
|
||||||
Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})),
|
Gene::new(GeneSource::Peashooter,GenePlace::Leaf,GeneType::modifier(|_|{Flow::empty()})),
|
||||||
Gene::new(GeneSource::CherryBomb,"face", GeneType::Structural),
|
Gene::new(GeneSource::CherryBomb,GenePlace::Face, GeneType::Structural),
|
||||||
],vec![
|
],vec![
|
||||||
(0,1),
|
(0,1),
|
||||||
(1,2),
|
(1,2),
|
||||||
|
|
@ -72,11 +72,11 @@ fn test_categoric_crossingover() {
|
||||||
]).unwrap().to_string());
|
]).unwrap().to_string());
|
||||||
|
|
||||||
assert!(cherry.to_string() == Genome::from_edges(vec![
|
assert!(cherry.to_string() == Genome::from_edges(vec![
|
||||||
Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})),
|
Gene::new(GeneSource::Peashooter,GenePlace::Root, GeneType::pure_producer(|_|{Flow::empty()})),
|
||||||
Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})),
|
Gene::new(GeneSource::CherryBomb,GenePlace::Head, GeneType::consumer(|_|{})),
|
||||||
Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()),
|
Gene::new(GeneSource::Peashooter,GenePlace::Head,GeneType::random_distribution()),
|
||||||
Gene::new(GeneSource::CherryBomb,"face", GeneType::Structural),
|
Gene::new(GeneSource::CherryBomb,GenePlace::Face, GeneType::Structural),
|
||||||
Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{})),
|
Gene::new(GeneSource::Peashooter,GenePlace::Face,GeneType::consumer(|_|{})),
|
||||||
], vec![
|
], vec![
|
||||||
(0,1),
|
(0,1),
|
||||||
(0,2),
|
(0,2),
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
pub mod genetics;
|
pub mod genetics;
|
||||||
|
pub mod wrapper;
|
||||||
|
|
|
||||||
0
rust-pvz-genetics/src/wrapper/mod.rs
Normal file
0
rust-pvz-genetics/src/wrapper/mod.rs
Normal file
Loading…
Add table
Add a link
Reference in a new issue