diff --git a/src/node/mod.rs b/src/node/mod.rs index 987440a..0e67b0f 100644 --- a/src/node/mod.rs +++ b/src/node/mod.rs @@ -8,13 +8,19 @@ mod node_modifier; pub struct Node { children: Vec, handler: NodeHandler, + max_children_count: Option, } impl Node { - pub fn new(children: Option>, handler: NodeHandler) -> Self { + pub fn new( + children: Option>, + handler: NodeHandler, + max_children_count: Option, + ) -> Self { Self { children: children.unwrap_or(vec![]), handler: handler, + max_children_count, } } pub fn get_value(&self) -> f64 { @@ -35,18 +41,21 @@ impl Node { Node { children: vec![], handler: NodeHandler::Number { number: n }, + max_children_count: None, } } pub fn function(func: fn(Vec) -> f64) -> Node { Node { children: vec![], handler: NodeHandler::Function { function: func }, + max_children_count: Some(1), } } pub fn variable(getter: fn() -> f64) -> Node { Node { children: vec![], handler: NodeHandler::Variable { getter }, + max_children_count: Some(0), } } } diff --git a/src/node/node_modifier.rs b/src/node/node_modifier.rs index 5f8fd33..ac2f932 100644 --- a/src/node/node_modifier.rs +++ b/src/node/node_modifier.rs @@ -1,10 +1,14 @@ use crate::node::Node; +use crate::node::NodeHandler; use crate::node::functions; -use crate::node::handler::NodeHandler; use rand::random_range; const PICK_STOP_PROBABILITY: u8 = 2; +pub enum NodeManipulationError { + TooMuchChildren(Node), +} + pub struct NodeModifier<'a> { picked_node: &'a mut Node, function_mutation_pool: Vec) -> f64>, @@ -57,16 +61,33 @@ impl<'a> NodeModifier<'a> { } } - pub fn add_node(&mut self, node: Node) { + pub fn add_node(&mut self, node: Node) -> Result<(), NodeManipulationError> { + if let Some(x) = self.picked_node.max_children_count { + if self.picked_node.children.len() + 1 >= x { + return Err(NodeManipulationError::TooMuchChildren(node)); + } + } self.picked_node.children.push(node); + Ok(()) } - pub fn insert_node(&mut self, mut node: Node, between: Option) { + pub fn insert_node( + &mut self, + mut node: Node, + between: Option, + ) -> Result<(), NodeManipulationError> { + if let Some(x) = self.picked_node.max_children_count { + if self.picked_node.children.len() + 1 >= x { + return Err(NodeManipulationError::TooMuchChildren(node)); + } + } let children_count = self.picked_node.children.len(); let operated_index = between.unwrap_or(random_range(0..children_count)); let moved = self.picked_node.children.remove(operated_index); node.children.push(moved); self.picked_node.children.insert(operated_index, node); + + Ok(()) } pub fn mutate_node(&mut self) {