formula run
This commit is contained in:
parent
5c99e80670
commit
ae8e51c053
4 changed files with 25 additions and 0 deletions
20
src/formula.rs
Normal file
20
src/formula.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use crate::node::Node;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Formula {
|
||||
tree: Node,
|
||||
x: f64,
|
||||
rating: f64,
|
||||
}
|
||||
|
||||
impl Formula {
|
||||
pub fn run(&mut self, inputs: Vec<f64>) -> Vec<f64> {
|
||||
let mut outputs: Vec<f64> = vec![];
|
||||
for input in inputs {
|
||||
self.x = input;
|
||||
outputs.push(self.tree.get_value());
|
||||
}
|
||||
outputs
|
||||
}
|
||||
pub fn mutate(&mut self) {}
|
||||
}
|
||||
|
|
@ -1 +1,2 @@
|
|||
mod formula;
|
||||
mod node;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
#[derive(Clone, Copy)]
|
||||
pub enum NodeHandler {
|
||||
Number { number: f64 },
|
||||
Function { function: fn(Vec<f64>) -> f64 },
|
||||
Variable { getter: fn() -> f64 },
|
||||
Empty,
|
||||
}
|
||||
|
||||
impl NodeHandler {
|
||||
|
|
@ -10,6 +12,7 @@ impl NodeHandler {
|
|||
NodeHandler::Number { number } => number.clone(),
|
||||
NodeHandler::Function { function } => function(inputs),
|
||||
NodeHandler::Variable { getter } => getter(),
|
||||
NodeHandler::Empty => 0f64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ mod functions;
|
|||
mod handler;
|
||||
mod node_modifier;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Node {
|
||||
children: Vec<Node>,
|
||||
handler: NodeHandler,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue