New templates
This commit is contained in:
parent
a9615c07a7
commit
aa27328286
7 changed files with 63 additions and 164 deletions
|
|
@ -1,8 +1,8 @@
|
|||
use crate::genetics::{gene::Gene, edge::Edge};
|
||||
use crate::genetics::gene::Gene;
|
||||
|
||||
pub struct PlantGenome {
|
||||
pub(crate) genes: Vec<Gene>,
|
||||
pub(crate) edges: Vec<Edge>
|
||||
pub(crate) edges: Vec<(usize,usize)>
|
||||
}
|
||||
|
||||
impl PlantGenome {
|
||||
|
|
@ -12,43 +12,30 @@ impl PlantGenome {
|
|||
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 self.genes.contains(&gene){
|
||||
return Some(PlantGenomeInsertError::GeneExists);
|
||||
pub fn from_edges(nodes: Vec<Gene>,edges: Vec<(usize,usize)>) -> Result<Self,PlantCreationError> {
|
||||
// Check for nodes amount and edges amount mismatch
|
||||
if nodes.len() != edges.len() {
|
||||
return Err(PlantCreationError::AmountMismatch);
|
||||
}
|
||||
|
||||
if let Some(parent_gene) = parent {
|
||||
let edge = Edge::new(parent_gene,gene.clone());
|
||||
if self.edges.contains(&edge) {
|
||||
return Some(PlantGenomeInsertError::EdgeExists);
|
||||
}
|
||||
self.edges.push(edge);
|
||||
if nodes.len() == 0 {
|
||||
return Err(PlantCreationError::EmptyNodes);
|
||||
}
|
||||
|
||||
self.genes.push(gene);
|
||||
self.update_graph();
|
||||
return None;
|
||||
}
|
||||
pub(crate) fn update_graph(&mut self) {
|
||||
// Sort by parent to guarantee root at [0]
|
||||
self.edges.sort();
|
||||
}
|
||||
pub fn crossingover(&mut self, other: &mut Self) {
|
||||
if edges.len() == 0 {
|
||||
return Err(PlantCreationError::EmptyEdges);
|
||||
}
|
||||
|
||||
self.update_graph();
|
||||
other.update_graph();
|
||||
}
|
||||
pub fn mutation(&mut self) {
|
||||
self.update_graph();
|
||||
return Ok(Self {
|
||||
genes: nodes,
|
||||
edges
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PlantGenomeInsertError {
|
||||
GeneExists,
|
||||
EdgeExists,
|
||||
RootExists,
|
||||
pub enum PlantCreationError {
|
||||
EmptyNodes,
|
||||
EmptyEdges,
|
||||
AmountMismatch,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue