mod observer;
pub(crate) mod working_set;
pub use observer::{InitialPoint, ObservedSolver, SolverObserver, SolverStep, StepKind};
pub use working_set::{WorkingSetDiagnostics, WorkingSetPolicy};
pub(crate) mod coordinate;
pub(crate) mod hybrid;
pub(crate) mod irls;
pub(crate) mod proximal;
pub use crate::path::{PathSolver, RegularizationPathSolver};
pub use hybrid::{HybridCoordinateDescent, HybridStep};
pub use irls::Irls;
pub use proximal::{ProximalDesign, ProximalGradient, ProximalVector};
use crate::error::{invalid, unsupported};
use crate::{FitError, L1};
pub trait Solver<P> {
type Fit;
type Error;
fn solve(&self, problem: &P) -> Result<Self::Fit, Self::Error>;
}
#[derive(Clone, Copy, Debug)]
pub struct CoordinateDescent {
stopping_criterion: Option<StoppingCriterion>,
max_iterations: usize,
screening: bool,
working_set_policy: WorkingSetPolicy,
}
impl CoordinateDescent {
pub fn new() -> Self {
Self {
stopping_criterion: None,
max_iterations: 10_000,
screening: true,
working_set_policy: WorkingSetPolicy::Auto,
}
}
pub fn tolerance(mut self, tolerance: f64) -> Self {
self.stopping_criterion = Some(StoppingCriterion::kkt_violation(tolerance));
self
}
pub fn terminate_on(mut self, criterion: StoppingCriterion) -> Self {
self.stopping_criterion = Some(criterion);
self
}
pub fn max_iterations(mut self, max_iterations: usize) -> Self {
self.max_iterations = max_iterations;
self
}
pub fn screening(mut self, enabled: bool) -> Self {
self.screening = enabled;
self
}
pub fn working_sets(mut self, enabled: bool) -> Self {
self.working_set_policy = WorkingSetPolicy::enabled(enabled);
self
}
pub fn working_set_policy(mut self, policy: WorkingSetPolicy) -> Self {
self.working_set_policy = policy;
self
}
}
impl Default for CoordinateDescent {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StoppingCriterion {
DualityGap {
absolute: f64,
relative: f64,
},
KktViolation {
absolute: f64,
},
ProximalGradientMapping {
absolute: f64,
},
}
impl StoppingCriterion {
pub const fn duality_gap(relative: f64) -> Self {
Self::DualityGap {
absolute: 0.0,
relative,
}
}
pub const fn kkt_violation(absolute: f64) -> Self {
Self::KktViolation { absolute }
}
pub const fn proximal_gradient_mapping(absolute: f64) -> Self {
Self::ProximalGradientMapping { absolute }
}
}
impl CoordinateDescent {
pub(crate) fn criterion(&self, penalty: L1) -> StoppingCriterion {
self.stopping_criterion.unwrap_or_else(|| {
if penalty.lambda == 0.0 {
StoppingCriterion::kkt_violation(1e-6)
} else {
StoppingCriterion::duality_gap(1e-6)
}
})
}
pub(crate) fn validate<E>(&self, criterion: StoppingCriterion) -> Result<(), FitError<E>> {
self.working_set_policy.validate()?;
match criterion {
StoppingCriterion::ProximalGradientMapping { .. } => {
return Err(unsupported(
"CoordinateDescent",
"proximal-gradient mapping is not implemented for coordinate descent",
"select KKT violation or duality gap",
));
}
StoppingCriterion::KktViolation { absolute } => {
if !absolute.is_finite() || absolute <= 0.0 {
return Err(invalid(
"KKT tolerance must be finite and strictly positive",
));
}
}
StoppingCriterion::DualityGap { absolute, relative } => {
if !absolute.is_finite()
|| !relative.is_finite()
|| absolute < 0.0
|| relative < 0.0
|| (absolute == 0.0 && relative == 0.0)
{
return Err(invalid(
"duality-gap tolerances must be finite, nonnegative, and at least one positive",
));
}
}
}
if self.max_iterations == 0 {
return Err(invalid("max_iterations must be strictly positive"));
}
Ok(())
}
}