Compare commits

..

No commits in common. "5c99e80670be7326967f9c89fcae9c3965aa81da" and "6d6141471fab51884cbc31bbd8d1a0254aa31352" have entirely different histories.

2 changed files with 4 additions and 34 deletions

View file

@ -8,19 +8,13 @@ mod node_modifier;
pub struct Node {
children: Vec<Node>,
handler: NodeHandler,
max_children_count: Option<usize>,
}
impl Node {
pub fn new(
children: Option<Vec<Node>>,
handler: NodeHandler,
max_children_count: Option<usize>,
) -> Self {
pub fn new(children: Option<Vec<Node>>, handler: NodeHandler) -> Self {
Self {
children: children.unwrap_or(vec![]),
handler: handler,
max_children_count,
}
}
pub fn get_value(&self) -> f64 {
@ -41,21 +35,18 @@ impl Node {
Node {
children: vec![],
handler: NodeHandler::Number { number: n },
max_children_count: None,
}
}
pub fn function(func: fn(Vec<f64>) -> 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),
}
}
}

View file

@ -1,14 +1,10 @@
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<fn(Vec<f64>) -> f64>,
@ -61,33 +57,16 @@ impl<'a> NodeModifier<'a> {
}
}
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));
}
}
pub fn add_node(&mut self, node: Node) {
self.picked_node.children.push(node);
Ok(())
}
pub fn insert_node(
&mut self,
mut node: Node,
between: Option<usize>,
) -> 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));
}
}
pub fn insert_node(&mut self, mut node: Node, between: Option<usize>) {
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) {