Genome restructurized
This commit is contained in:
parent
964a8c49ad
commit
23d9ab696c
5 changed files with 109 additions and 20 deletions
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,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue