This commit is contained in:
Rendo 2025-11-10 14:47:23 +05:00
commit 17314c22a9
2 changed files with 37 additions and 0 deletions

View file

@ -12,6 +12,7 @@ pub struct Learner {
expected_outputs: Vec<f64>, expected_outputs: Vec<f64>,
formulas_per_iteration: usize, formulas_per_iteration: usize,
iterations: usize, iterations: usize,
stop_epsilon: f64,
} }
impl Learner { impl Learner {
@ -20,6 +21,7 @@ impl Learner {
expected_outputs: Vec<f64>, expected_outputs: Vec<f64>,
formulas_per_iteration: Option<usize>, formulas_per_iteration: Option<usize>,
iterations: Option<usize>, iterations: Option<usize>,
stop_epsilon: Option<f64>,
) -> Self { ) -> Self {
Self { Self {
best_algorithm: Formula::new(), best_algorithm: Formula::new(),
@ -27,6 +29,7 @@ impl Learner {
expected_outputs, expected_outputs,
formulas_per_iteration: formulas_per_iteration.unwrap_or(200), formulas_per_iteration: formulas_per_iteration.unwrap_or(200),
iterations: iterations.unwrap_or(200), iterations: iterations.unwrap_or(200),
stop_epsilon: stop_epsilon.unwrap_or(1e-8f64),
} }
} }
pub fn calculate_formula(&mut self) -> Formula { pub fn calculate_formula(&mut self) -> Formula {
@ -35,6 +38,10 @@ impl Learner {
&self.expected_outputs, &self.expected_outputs,
&self.best_algorithm.run(self.inputs.clone()), &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(); let found_best = self.iterate();
if found_best.1 > current_best.unwrap_or(0.) { if found_best.1 > current_best.unwrap_or(0.) {
self.best_algorithm = found_best.0; self.best_algorithm = found_best.0;
@ -60,6 +67,9 @@ impl Learner {
if found_best.1 > current_best.unwrap_or(0.) { if found_best.1 > current_best.unwrap_or(0.) {
self.best_algorithm = found_best.0; self.best_algorithm = found_best.0;
} }
if 1. - found_best.1 <= self.stop_epsilon {
break;
}
} }
self.best_algorithm.clone() self.best_algorithm.clone()
} }

27
src/main.rs Normal file
View file

@ -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.,
])
)
}