Genome restructurized
This commit is contained in:
parent
964a8c49ad
commit
23d9ab696c
5 changed files with 109 additions and 20 deletions
31
rust-pvz-genetics/src/genetics/edge.rs
Normal file
31
rust-pvz-genetics/src/genetics/edge.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use std::cmp::Ordering;
|
||||
use crate::genetics::gene::Gene;
|
||||
|
||||
#[derive(PartialEq,Eq,Clone)]
|
||||
pub struct Edge {
|
||||
pub from: Gene,
|
||||
pub to: Gene
|
||||
}
|
||||
|
||||
impl Edge{
|
||||
pub fn new(from: Gene, to: Gene) -> Self {
|
||||
Self {
|
||||
from,
|
||||
to
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Edge {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Edge {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
if self.from == other.to {return Ordering::Greater;}
|
||||
else if self.to == other.from {return Ordering::Less;}
|
||||
else {return Ordering::Equal;}
|
||||
}
|
||||
}
|
||||
13
rust-pvz-genetics/src/genetics/gene.rs
Normal file
13
rust-pvz-genetics/src/genetics/gene.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#[derive(PartialEq,Eq,Clone)]
|
||||
pub struct Gene {
|
||||
pub place: String,
|
||||
pub kind: GeneType
|
||||
}
|
||||
|
||||
#[derive(PartialEq,Eq,Clone)]
|
||||
pub enum GeneType {
|
||||
Producer,
|
||||
Transport,
|
||||
Consumer,
|
||||
Modifier,
|
||||
}
|
||||
53
rust-pvz-genetics/src/genetics/genome.rs
Normal file
53
rust-pvz-genetics/src/genetics/genome.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use crate::genetics::{gene::Gene, edge::Edge};
|
||||
|
||||
pub struct PlantGenome {
|
||||
genes: Vec<Gene>,
|
||||
edges: Vec<Edge>
|
||||
}
|
||||
|
||||
impl PlantGenome {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
genes: Vec::new(),
|
||||
edges: Vec::new()
|
||||
}
|
||||
}
|
||||
fn insert_gene(&mut self, gene: Gene, parent: Option<Gene>) -> Option<PlantGenomeInsertError> {
|
||||
if let None = parent && self.genes.len() > 0 {
|
||||
return Some(PlantGenomeInsertError::RootExists);
|
||||
}
|
||||
if let Some(_) = self.genes.iter().find(|g| **g == gene){
|
||||
return Some(PlantGenomeInsertError::GeneExists);
|
||||
}
|
||||
|
||||
if let Some(parent_gene) = parent {
|
||||
let edge = Edge::new(parent_gene,gene.clone());
|
||||
if let Some(_) = self.edges.iter().find(|e| **e == edge) {
|
||||
return Some(PlantGenomeInsertError::EdgeExists);
|
||||
}
|
||||
self.edges.push(edge);
|
||||
}
|
||||
|
||||
self.genes.push(gene);
|
||||
self.update_graph();
|
||||
return None;
|
||||
}
|
||||
fn update_graph(&mut self) {
|
||||
// Sort by parent to guarantee root at [0]
|
||||
self.edges.sort();
|
||||
}
|
||||
pub fn crossingover(&mut self, other: &mut Self) {
|
||||
|
||||
self.update_graph();
|
||||
other.update_graph();
|
||||
}
|
||||
pub fn mutation(&mut self) {
|
||||
self.update_graph();
|
||||
}
|
||||
}
|
||||
|
||||
pub enum PlantGenomeInsertError {
|
||||
GeneExists,
|
||||
EdgeExists,
|
||||
RootExists,
|
||||
}
|
||||
|
|
@ -1,21 +1,12 @@
|
|||
use std::collections::HashMap;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod plant_templates;
|
||||
pub mod edge;
|
||||
pub mod gene;
|
||||
pub mod genome;
|
||||
|
||||
#[derive(Clone,Copy,PartialEq,Eq)]
|
||||
pub struct GeneID;
|
||||
|
||||
pub trait Gene {}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PlantGenome {
|
||||
pub edges: HashMap<GeneID,Vec<GeneID>>,
|
||||
pub vertices: HashMap<GeneID,Box<dyn Gene>>
|
||||
}
|
||||
|
||||
impl PlantGenome {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
edges: HashMap::new(),
|
||||
vertices: HashMap::new()
|
||||
}
|
||||
}
|
||||
pub mod prelude {
|
||||
pub use crate::genetics::edge::*;
|
||||
pub use crate::genetics::genome::*;
|
||||
pub use crate::genetics::gene::*;
|
||||
}
|
||||
|
|
|
|||
1
rust-pvz-genetics/src/genetics/tests.rs
Normal file
1
rust-pvz-genetics/src/genetics/tests.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
Loading…
Add table
Add a link
Reference in a new issue