Skip to content

Commit

Permalink
Curve Fit - linear 3-point use quasinewton BFGS solver.
Browse files Browse the repository at this point in the history
Switches the non-linear least squares solver to Quasi-newton BFGS
solver, because it converges significantly faster.

Note: as a result of this speed the quality of the curve with a large
amount of randomness is less accurate - but I feel this is a good
trade-off for speed.

GitHub issue #268
  • Loading branch information
david-cattermole committed Nov 2, 2024
1 parent f0de6a2 commit 01f913b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
25 changes: 17 additions & 8 deletions lib/rust/mmscenegraph/src/math/curve_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ pub fn nonlinear_line_n3(
// solved.
let initial_parameters_vec = vec![point_a.y, point_b.y, point_c.y];
let initial_parameters: Array1<f64> = Array1::from(initial_parameters_vec);
println!("initial_parameters={initial_parameters:?}");
debug!("initial_parameters={initial_parameters:?}");

// Define the problem
let reference_values: Vec<(f64, f64)> = x_values
Expand All @@ -406,16 +406,25 @@ pub fn nonlinear_line_n3(
&reference_values,
);

// Set up the subproblem
let subproblem: argmin::solver::trustregion::CauchyPoint<f64> =
argmin::solver::trustregion::CauchyPoint::new();

// Set up solver
let solver = argmin::solver::trustregion::TrustRegion::new(subproblem);
// Set up solver.
let epsilon = 1e-9;
let condition =
argmin::solver::linesearch::condition::ArmijoCondition::new(1e-5)?;
let linesearch =
argmin::solver::linesearch::BacktrackingLineSearch::new(condition)
.rho(0.5)?;
let solver = argmin::solver::quasinewton::BFGS::new(linesearch)
.with_tolerance_cost(epsilon)?;

// Run solver
let initial_hessian: Array2<f64> = Array2::eye(3);
let result = argmin::core::Executor::new(problem, solver)
.configure(|state| state.param(initial_parameters).max_iters(30))
.configure(|state| {
state
.param(initial_parameters)
.inv_hessian(initial_hessian)
.max_iters(50)
})
.run()?;
debug!("Solver Result: {result}");

Expand Down
6 changes: 3 additions & 3 deletions lib/rust/mmscenegraph/tests/curvefit_linear_3_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ fn linear_3_point_variance4() -> Result<()> {
)?;

assert_relative_eq!(point_a.x(), 1001.0, epsilon = 1.0e-1);
assert_relative_eq!(point_a.y(), -1.21533949192, epsilon = 0.75);
assert_relative_eq!(point_a.y(), -1.21533949192, epsilon = 2.8);

assert_relative_eq!(point_b.x(), 1051.0, epsilon = 1.0e-9);
assert_relative_eq!(point_b.y(), 4.5, epsilon = 2.0);
assert_relative_eq!(point_b.y(), 4.5, epsilon = 1.8);

assert_relative_eq!(point_c.x(), 1101.0, epsilon = 1.0e-1);
assert_relative_eq!(point_c.y(), 1.855, epsilon = 2.5);
assert_relative_eq!(point_c.y(), 1.855, epsilon = 3.1);

Ok(())
}
Binary file modified lib/rust/mmscenegraph/tests/data/linear_3_point_variance1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified lib/rust/mmscenegraph/tests/data/linear_3_point_variance2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified lib/rust/mmscenegraph/tests/data/linear_3_point_variance3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified lib/rust/mmscenegraph/tests/data/linear_3_point_variance4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01f913b

Please sign in to comment.