Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix steepest descent to cache previous param #363

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions argmin/src/solver/gradientdescent/steepestdescent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use crate::core::{
ArgminFloat, CostFunction, DeserializeOwnedAlias, Error, Executor, Gradient, IterState,
LineSearch, OptimizationResult, Problem, SerializeAlias, Solver, KV,
LineSearch, OptimizationResult, Problem, SerializeAlias, Solver, State, KV,
};
use argmin_math::ArgminMul;
#[cfg(feature = "serde1")]
Expand Down Expand Up @@ -63,15 +63,18 @@ where
fn next_iter(
&mut self,
problem: &mut Problem<O>,
mut state: IterState<P, G, (), (), F>,
state: IterState<P, G, (), (), F>,
) -> Result<(IterState<P, G, (), (), F>, Option<KV>), Error> {
let param_new = state.take_param().ok_or_else(argmin_error_closure!(
NotInitialized,
concat!(
"`SteepestDescent` requires an initial parameter vector. ",
"Please provide an initial guess via `Executor`s `configure` method."
)
))?;
let param_new = state
.get_param()
.ok_or_else(argmin_error_closure!(
NotInitialized,
concat!(
"`SteepestDescent` requires an initial parameter vector. ",
"Please provide an initial guess via `Executor`s `configure` method."
)
))?
.clone();
let new_cost = problem.cost(&param_new)?;
let new_grad = problem.gradient(&param_new)?;

Expand Down Expand Up @@ -153,6 +156,20 @@ mod tests {
);
}

#[test]
fn test_next_iter_prev_param_not_erased() {
let linesearch: BacktrackingLineSearch<Vec<f64>, Vec<f64>, ArmijoCondition<f64>, f64> =
BacktrackingLineSearch::new(ArmijoCondition::new(0.2).unwrap());
let mut sd = SteepestDescent::new(linesearch);
let (state, _kv) = sd
.next_iter(
&mut Problem::new(TestProblem::new()),
IterState::new().param(vec![1.0, 2.0]),
)
.unwrap();
state.prev_param.unwrap();
}

#[test]
fn test_next_iter_regression() {
struct SDProblem {}
Expand Down