Skip to main content

pounce_algorithm/sqp/
result.rs

1//! `SqpResult` / `SqpStatus` / `SqpError` — return types for
2//! `SqpAlgorithm::optimize`.
3
4use pounce_common::Number;
5use pounce_qp::{QpError, WorkingSet};
6use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum SqpStatus {
10    /// KKT residuals all below their tolerances.
11    Optimal,
12    /// `max_iter` reached without convergence.
13    MaxIter,
14    /// QP subproblem returned an `Infeasible` status (elastic
15    /// mode certified the QP infeasible).
16    InfeasibleSubproblem,
17    /// Line search failed to find an acceptable step (Phase 5b
18    /// commit 5+; not produced by the c3 always-full-step loop).
19    LineSearchFailed,
20}
21
22impl fmt::Display for SqpStatus {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            SqpStatus::Optimal => write!(f, "optimal"),
26            SqpStatus::MaxIter => write!(f, "max-iter"),
27            SqpStatus::InfeasibleSubproblem => write!(f, "infeasible-subproblem"),
28            SqpStatus::LineSearchFailed => write!(f, "line-search-failed"),
29        }
30    }
31}
32
33#[derive(Debug)]
34pub enum SqpError {
35    /// Hard QP-solver failure (singular, dimension mismatch, etc.).
36    QpFailure(QpError),
37    /// Caller-supplied dimensions disagree.
38    DimensionMismatch(String),
39}
40
41impl fmt::Display for SqpError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            SqpError::QpFailure(e) => write!(f, "QP subproblem failure: {e}"),
45            SqpError::DimensionMismatch(s) => write!(f, "dimension mismatch: {s}"),
46        }
47    }
48}
49
50impl From<QpError> for SqpError {
51    fn from(e: QpError) -> Self {
52        SqpError::QpFailure(e)
53    }
54}
55
56#[derive(Debug, Clone)]
57pub struct SqpResult {
58    pub x: Vec<Number>,
59    pub lambda_g: Vec<Number>,
60    pub lambda_x: Vec<Number>,
61    pub obj: Number,
62    pub status: SqpStatus,
63    pub n_iter: u32,
64    pub n_qp_solves: u32,
65    /// Final stationarity residual (max-norm of `∇f + Jᵀ λ_g + λ_x`).
66    pub final_stationarity: Number,
67    /// Final constraint violation (max-norm of `c(x*)` for
68    /// equalities plus bound-violation slack).
69    pub final_constr_viol: Number,
70    /// Final QP working set, suitable for warm-starting the next
71    /// `optimize_with_warm_start` call (§6 design-note contract).
72    /// `None` only when no QP was solved (e.g. cold-start declared
73    /// the iterate optimal at the very first KKT check).
74    pub working_set: Option<WorkingSet>,
75}