diff --git a/src/learner.rs b/src/learner.rs index c955bbc..9d2bac7 100644 --- a/src/learner.rs +++ b/src/learner.rs @@ -12,6 +12,7 @@ pub struct Learner { expected_outputs: Vec, formulas_per_iteration: usize, iterations: usize, + stop_epsilon: f64, } impl Learner { @@ -20,6 +21,7 @@ impl Learner { expected_outputs: Vec, formulas_per_iteration: Option, iterations: Option, + stop_epsilon: Option, ) -> Self { Self { best_algorithm: Formula::new(), @@ -27,6 +29,7 @@ impl Learner { expected_outputs, formulas_per_iteration: formulas_per_iteration.unwrap_or(200), iterations: iterations.unwrap_or(200), + stop_epsilon: stop_epsilon.unwrap_or(1e-8f64), } } pub fn calculate_formula(&mut self) -> Formula { @@ -35,6 +38,10 @@ impl Learner { &self.expected_outputs, &self.best_algorithm.run(self.inputs.clone()), ); + if 1. - current_best.unwrap_or(0.) <= self.stop_epsilon { + println!("employ"); + break; + } let found_best = self.iterate(); if found_best.1 > current_best.unwrap_or(0.) { self.best_algorithm = found_best.0; @@ -60,6 +67,9 @@ impl Learner { if found_best.1 > current_best.unwrap_or(0.) { self.best_algorithm = found_best.0; } + if 1. - found_best.1 <= self.stop_epsilon { + break; + } } self.best_algorithm.clone() } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b3a92dd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,27 @@ +use fapprox::learner::Learner; + +fn main() { + let mut learner = Learner::new( + vec![ + 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.0, 12., 13., 14., 15., + ], + vec![ + 0.0, 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.0, 12., 13., 14., 15., + ] + .iter() + .map(|x: &f64| x.sin()) + .map(|x| x + 1.) + .collect(), + Some(500), + Some(500), + None, + ); + let formula = learner.calculate_formula(); + formula.display_tree(); + println!( + "{:?}", + formula.run(vec![ + 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.0, 12., 13., 14., 15., + ]) + ) +}