fapprox/src/formula.rs
2025-11-09 11:58:50 +05:00

59 lines
1.6 KiB
Rust

use crate::node::Node;
use crate::node::node_modifier::NodeModifier;
#[derive(Clone)]
pub struct Formula {
tree: Node,
}
impl Formula {
pub fn new() -> Self {
let mut formula = Self {
tree: Node::empty(),
};
formula.tree.modify_node().add_node(Node::variable());
formula
}
pub fn run(&self, inputs: Vec<f64>) -> Vec<f64> {
let mut outputs: Vec<f64> = vec![];
for input in inputs {
outputs.push(self.tree.get_value(input));
}
outputs
}
pub fn mutate(&mut self) {}
pub fn modify_tree(&mut self) -> NodeModifier {
self.tree.modify_tree()
}
pub fn display_tree(&self) {
self.display_recursion(0, vec![&self.tree]);
}
fn display_recursion(&self, indent_level: u8, nodes: Vec<&Node>) {
for i in 0..nodes.len() {
let node = nodes[i];
if indent_level != 0 {
for j in 0..(indent_level) {
if j == indent_level - 1 {
print!("|__________");
} else {
print!(" ");
}
}
}
println!("{node}");
if node.children.len() == 0 {
continue;
}
let mut next_nodes: Vec<&Node> = Vec::new();
for node in &node.children {
next_nodes.push(node);
}
self.display_recursion(indent_level + 1, next_nodes);
}
}
pub fn as_text(&self) -> String {
self.tree.as_text()
}
}