69 lines
1.7 KiB
Rust
69 lines
1.7 KiB
Rust
use crate::node::node_modifier::NodeModifier;
|
|
use handler::*;
|
|
|
|
mod functions;
|
|
mod handler;
|
|
mod node_modifier;
|
|
|
|
#[derive(Clone)]
|
|
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 {
|
|
Self {
|
|
children: children.unwrap_or(vec![]),
|
|
handler: handler,
|
|
max_children_count,
|
|
}
|
|
}
|
|
pub fn empty() -> Self {
|
|
Self {
|
|
children: vec![],
|
|
handler: NodeHandler::Empty,
|
|
max_children_count: None,
|
|
}
|
|
}
|
|
pub fn number(n: f64) -> Self {
|
|
Self {
|
|
children: vec![],
|
|
handler: NodeHandler::Number { number: n },
|
|
max_children_count: Some(0),
|
|
}
|
|
}
|
|
pub fn function(func: fn(Vec<f64>) -> f64, max_children_count: Option<usize>) -> Self {
|
|
Self {
|
|
children: vec![],
|
|
handler: NodeHandler::Function { function: func },
|
|
max_children_count,
|
|
}
|
|
}
|
|
pub fn variable() -> Self {
|
|
Self {
|
|
children: vec![],
|
|
handler: NodeHandler::Variable,
|
|
max_children_count: Some(0),
|
|
}
|
|
}
|
|
pub fn get_value(&self, passed_x: f64) -> f64 {
|
|
if self.children.len() == 0 {
|
|
return self.handler.run(vec![0f64], passed_x);
|
|
}
|
|
let mut inputs: Vec<f64> = vec![];
|
|
for node in &self.children {
|
|
inputs.push(node.get_value(passed_x));
|
|
}
|
|
|
|
return self.handler.run(inputs, passed_x);
|
|
}
|
|
pub fn modify_tree(&mut self) -> NodeModifier {
|
|
NodeModifier::from_random(self, None)
|
|
}
|
|
}
|