use crate::{numeric::cast, panoc::PANOCCache};
use lbfgs::LbfgsPrecision;
use num::Float;
use std::iter::Sum;
fn default_initial_penalty<T: Float>() -> T {
cast::<T>(10.0)
}
#[derive(Debug)]
pub struct AlmCache<T = f64>
where
T: Float + LbfgsPrecision + Sum<T>,
{
pub(crate) panoc_cache: PANOCCache<T>,
pub(crate) y_plus: Option<Vec<T>>,
pub(crate) xi: Option<Vec<T>>,
pub(crate) delta_y_norm: T,
pub(crate) delta_y_norm_plus: T,
pub(crate) f2_norm: T,
pub(crate) f2_norm_plus: T,
pub(crate) w_alm_aux: Option<Vec<T>>,
pub(crate) w_pm: Option<Vec<T>>,
pub(crate) iteration: usize,
pub(crate) inner_iteration_count: usize,
pub(crate) last_inner_problem_norm_fpr: T,
pub(crate) available_time: Option<std::time::Duration>,
}
impl<T> AlmCache<T>
where
T: Float + LbfgsPrecision + Sum<T>,
{
pub fn new(panoc_cache: PANOCCache<T>, n1: usize, n2: usize) -> Self {
AlmCache {
panoc_cache,
y_plus: if n1 > 0 {
Some(vec![T::zero(); n1])
} else {
None
},
xi: if n1 + n2 > 0 {
let mut xi_init = vec![default_initial_penalty(); 1];
xi_init.append(&mut vec![T::zero(); n1]);
Some(xi_init)
} else {
None
},
w_alm_aux: if n1 > 0 {
Some(vec![T::zero(); n1])
} else {
None
},
w_pm: if n2 > 0 {
Some(vec![T::zero(); n2])
} else {
None
},
iteration: 0,
delta_y_norm: T::zero(),
delta_y_norm_plus: T::infinity(),
f2_norm: T::zero(),
f2_norm_plus: T::infinity(),
inner_iteration_count: 0,
last_inner_problem_norm_fpr: -T::one(),
available_time: None,
}
}
pub fn reset(&mut self) {
self.panoc_cache.reset();
self.iteration = 0;
self.f2_norm = T::zero();
self.f2_norm_plus = T::zero();
self.delta_y_norm = T::zero();
self.delta_y_norm_plus = T::zero();
self.inner_iteration_count = 0;
}
}