use crate::core::constraint::BoxConstraints;
use crate::core::math::{
AddDiagonalVectorInPlace, BoxAffineScaling, Dot, GramMatrix, LinearSolveSpd, MatTransposeVec,
MaxDiagonal, NegInPlace, NormSquared, Scalar, ScaledAdd,
};
use crate::core::problem::{Jacobian, Problem, Residual};
use crate::core::solver::Solver;
use crate::core::state::NllsState;
use crate::core::termination::TerminationReason;
pub struct Trf<V, M, F = f64> {
tol_grad: F,
tau: F,
rstep: F,
theta: F,
max_inner_attempts: u32,
mu: Option<F>,
nu: F,
r_cache: Option<V>,
j_cache: Option<M>,
}
impl<V, M> Default for Trf<V, M> {
fn default() -> Self {
Self::new()
}
}
impl<V, M> Trf<V, M> {
pub fn new() -> Self {
Self {
tol_grad: 1e-8,
tau: 1e-3,
rstep: 1e-10,
theta: 0.99995,
max_inner_attempts: 50,
mu: None,
nu: 2.0,
r_cache: None,
j_cache: None,
}
}
}
impl<V, M, F: Scalar> Trf<V, M, F> {
pub fn with_tol_grad(mut self, tol: F) -> Self {
assert!(tol >= F::zero(), "tol_grad must be ≥ 0");
self.tol_grad = tol;
self
}
pub fn with_tau(mut self, tau: F) -> Self {
assert!(tau > F::zero(), "tau must be > 0");
self.tau = tau;
self
}
pub fn with_rstep(mut self, rstep: F) -> Self {
assert!(rstep > F::zero(), "rstep must be > 0");
self.rstep = rstep;
self
}
pub fn with_theta(mut self, theta: F) -> Self {
assert!(
theta > F::zero() && theta < F::one(),
"theta must be in (0, 1), got {:?}",
theta
);
self.theta = theta;
self
}
pub fn with_max_inner_attempts(mut self, n: u32) -> Self {
assert!(n > 0, "max_inner_attempts must be > 0");
self.max_inner_attempts = n;
self
}
}
impl<V, M, F: Scalar> Trf<V, M, F> {
#[deprecated(since = "0.10.0", note = "renamed to `with_tol_grad`")]
pub fn tol_grad(self, tol: F) -> Self {
self.with_tol_grad(tol)
}
#[deprecated(since = "0.10.0", note = "renamed to `with_tau`")]
pub fn tau(self, tau: F) -> Self {
self.with_tau(tau)
}
#[deprecated(since = "0.10.0", note = "renamed to `with_rstep`")]
pub fn rstep(self, rstep: F) -> Self {
self.with_rstep(rstep)
}
#[deprecated(since = "0.10.0", note = "renamed to `with_theta`")]
pub fn theta(self, theta: F) -> Self {
self.with_theta(theta)
}
#[deprecated(since = "0.10.0", note = "renamed to `with_max_inner_attempts`")]
pub fn max_inner_attempts(self, n: u32) -> Self {
self.with_max_inner_attempts(n)
}
}
impl<P, V, M, F> Solver<P, NllsState<V, F>> for Trf<V, M, F>
where
F: Scalar,
P: Residual<Param = V, Output = V> + Jacobian<Jacobian = M> + BoxConstraints<Param = V>,
V: ScaledAdd<F> + NormSquared<F> + NegInPlace + Dot<F> + BoxAffineScaling<F> + Clone,
M: GramMatrix
+ MatTransposeVec<V>
+ LinearSolveSpd<V>
+ AddDiagonalVectorInPlace<V>
+ MaxDiagonal<F>
+ Clone,
{
type Error = <P as Residual>::Error;
fn init(
&mut self,
problem: &mut Problem<P>,
mut state: NllsState<V, F>,
) -> Result<NllsState<V, F>, Self::Error> {
state.param.project_strictly_inside(
problem.inner().lower(),
problem.inner().upper(),
self.rstep,
);
let (r, j) = problem.residual_and_jacobian(&state.param)?;
state.cost = Some(F::from_f64(0.5).unwrap() * r.norm_squared());
let g = j.mat_transpose_vec(&r);
let mut d_sq = state.param.clone();
let mut c_diag = state.param.clone();
state.param.compute_cl_scaling(
&g,
problem.inner().lower(),
problem.inner().upper(),
&mut d_sq,
&mut c_diag,
);
let mut a = j.gram();
a.add_diagonal_vector_in_place(&c_diag);
let max_diag = a.max_diagonal().max(F::one());
self.mu = Some(self.tau * max_diag);
self.nu = F::from_f64(2.0).unwrap();
self.r_cache = Some(r);
self.j_cache = Some(j);
Ok(state)
}
fn next_iter(
&mut self,
problem: &mut Problem<P>,
mut state: NllsState<V, F>,
) -> Result<(NllsState<V, F>, Option<TerminationReason>), Self::Error> {
let r = match self.r_cache.take() {
Some(r) => r,
None => problem.residual(&state.param)?,
};
let j = match self.j_cache.take() {
Some(j) => j,
None => problem.jacobian(&state.param)?,
};
let g = j.mat_transpose_vec(&r);
let mut d_sq = state.param.clone();
let mut c_diag = state.param.clone();
state.param.compute_cl_scaling(
&g,
problem.inner().lower(),
problem.inner().upper(),
&mut d_sq,
&mut c_diag,
);
if self.tol_grad > F::zero() && g.cl_kkt_inf_norm(&d_sq) <= self.tol_grad {
self.r_cache = Some(r);
self.j_cache = Some(j);
return Ok((state, Some(TerminationReason::SolverConverged)));
}
let mut neg_g = g.clone();
neg_g.neg_in_place();
let m = j.gram();
let mut mu = self
.mu
.expect("mu not set: Solver::init must run before next_iter");
let mut nu = self.nu;
let two = F::from_f64(2.0).unwrap();
let half = F::from_f64(0.5).unwrap();
let one_third = F::from_f64(1.0 / 3.0).unwrap();
let h;
let mut attempts: u32 = 0;
loop {
let mut a_damped = m.clone();
let mut damping_vec = c_diag.clone();
damping_vec.scaled_add(mu, &d_sq);
a_damped.add_diagonal_vector_in_place(&damping_vec);
match a_damped.solve_spd(&neg_g) {
Ok(step) => {
h = step;
break;
}
Err(_) => {
attempts += 1;
if attempts >= self.max_inner_attempts || !mu.is_finite() {
self.mu = Some(mu);
self.nu = nu;
self.r_cache = Some(r);
self.j_cache = Some(j);
return Ok((state, Some(TerminationReason::SolverFailed)));
}
mu = mu * nu;
nu = nu * two;
}
}
}
let tau_max =
state
.param
.max_feasible_step(&h, problem.inner().lower(), problem.inner().upper());
let alpha = if tau_max >= F::one() {
F::one()
} else {
self.theta * tau_max
};
let mut x_trial = state.param.clone();
x_trial.scaled_add(alpha, &h);
let r_trial = problem.residual(&x_trial)?;
let f_trial = half * r_trial.norm_squared();
let prev_cost = state
.cost
.expect("cost not set: Solver::init must run before next_iter");
let h_t_g = h.dot(&g);
let dh_norm_sq = h.weighted_norm_squared(&d_sq);
let predicted =
-alpha * (F::one() - half * alpha) * h_t_g + half * alpha * alpha * mu * dh_norm_sq;
let half_s_t_c_s = half * alpha * alpha * h.weighted_norm_squared(&c_diag);
let actual = prev_cost - f_trial - half_s_t_c_s;
let rho = if predicted > F::zero() {
actual / predicted
} else {
F::zero()
};
if rho > F::zero() {
state.param = x_trial;
state.cost = Some(f_trial);
let factor = F::one() - (two * rho - F::one()).powi(3);
mu = mu * factor.max(one_third);
nu = two;
self.r_cache = Some(r_trial);
self.j_cache = None;
} else {
mu = mu * nu;
nu = nu * two;
self.r_cache = Some(r);
self.j_cache = Some(j);
}
self.mu = Some(mu);
self.nu = nu;
Ok((state, None))
}
}