Skip to main content

pounce_algorithm/conv_check/
trait.rs

1//! `ConvCheck` trait — port of `IpConvCheck.hpp`.
2//!
3//! Upstream `ConvergenceCheck::CheckConvergence` reads the NLP error
4//! and iter count off `IpData()`/`IpCq()`. The default trait method
5//! pushes that read to the caller (the main loop in `ipopt_alg.rs`)
6//! so simple convergence policies stay pure scalar state machines
7//! over `(nlp_err, iter_count)`. The richer
8//! [`ConvCheck::check_convergence_with_state`] entry point exposes
9//! the live `(IpoptData, IpoptCq)` so policies that need iterate
10//! components — notably the restoration-side
11//! `RestoFilterConvergenceCheck::TestOrigProgress` — can read them.
12//! Default impl just delegates to the scalar method, preserving
13//! backwards compatibility for every existing impl.
14
15use crate::ipopt_cq::IpoptCqHandle;
16use crate::ipopt_data::IpoptDataHandle;
17use pounce_common::types::{Index, Number};
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum ConvergenceStatus {
21    Continue,
22    Converged,
23    /// Converged to the looser `acceptable_*` tolerance band rather
24    /// than the tight `tol` — upstream `CONVERGED_TO_ACCEPTABLE_POINT`.
25    /// Maps to `SolverReturn::StopAtAcceptablePoint` →
26    /// `ApplicationReturnStatus::SolvedToAcceptableLevel`.
27    ConvergedToAcceptable,
28    MaxIterExceeded,
29    /// `max_cpu_time` budget reached. Maps to
30    /// `SolverReturn::CpuTimeExceeded` → `MaximumCpuTimeExceeded`.
31    CpuTimeExceeded,
32    /// `max_wall_time` budget reached. Maps to
33    /// `SolverReturn::WallTimeExceeded` → `MaximumWallTimeExceeded`.
34    WallTimeExceeded,
35    /// Rapid infeasibility detection fired — the iterate is
36    /// converging to a stationary point of the constraint violation
37    /// with the violation bounded away from zero. Maps to
38    /// `SolverReturn::LocalInfeasibility`.
39    LocallyInfeasible,
40    Failed,
41}
42
43pub trait ConvCheck {
44    fn check_convergence(&mut self, nlp_err: Number, iter_count: Index) -> ConvergenceStatus;
45
46    /// State-aware convergence check. The main loop calls this on
47    /// every iteration so policies that need access to the iterate
48    /// (e.g. `RestoConvCheckAdapter`'s orig-NLP `inf_pr` evaluation
49    /// for the kappa-reduction early-exit) can read `data.curr` and
50    /// the cq layer. Default impl delegates to
51    /// [`Self::check_convergence`], so scalar-only policies don't
52    /// need to override.
53    /// Whether this policy ever refused a termination certificate it judged
54    /// masked by an extreme objective scale (gh #200).
55    ///
56    /// The main loop reads it on the failure exits: a run that was held back
57    /// from stopping must not end up *worse off* than it would have been, so
58    /// when it later stalls the stored acceptable point is restored rather
59    /// than a bare failure surfaced. Policies that never veto keep the
60    /// default `false` and the failure paths behave exactly as before.
61    fn certificate_vetoed(&self) -> bool {
62        false
63    }
64
65    /// Whether this policy ever refused an *acceptable-level* termination it
66    /// judged masked (gh #200). Undone differently from a strict refusal — see
67    /// `OptErrorConvCheck::acceptable_veto_fired`.
68    fn acceptable_certificate_vetoed(&self) -> bool {
69        false
70    }
71
72    fn check_convergence_with_state(
73        &mut self,
74        nlp_err: Number,
75        iter_count: Index,
76        _data: &IpoptDataHandle,
77        _cq: &IpoptCqHandle,
78    ) -> ConvergenceStatus {
79        self.check_convergence(nlp_err, iter_count)
80    }
81
82    /// Whether the current iterate passes the *strict* per-component convergence
83    /// tolerances — the [`Self::check_convergence_with_state`] strict test with
84    /// the masked-certificate veto (gh #200) removed. In other words: would this
85    /// iterate have certified `Success` were it not for the objective-scale
86    /// masking?
87    ///
88    /// gh #327 reads it on the veto's fallback path to distinguish a point the
89    /// veto refused *only because of masking* — a would-be strict certificate,
90    /// e.g. the true optimum a continued run reached but could never certify
91    /// there — from one that never converged at all (an unbounded / diverging
92    /// iterate whose gradient never vanishes). Only the former may displace the
93    /// point the baseline stopped at. Default `false` for policies that expose no
94    /// strict test.
95    fn current_passes_strict(
96        &self,
97        _nlp_err: Number,
98        _data: &IpoptDataHandle,
99        _cq: &IpoptCqHandle,
100    ) -> bool {
101        false
102    }
103
104    /// Whether the supplied `nlp_err` is at or below the acceptable
105    /// tolerance — port of upstream
106    /// `OptimalityErrorConvergenceCheck::CurrentIsAcceptable`. Used by
107    /// the main loop to gate `StoreAcceptablePoint` /
108    /// `RestoreAcceptablePoint`. Default returns `false` so policies
109    /// that don't track an acceptable level (e.g. resto-of-resto inner
110    /// adapters) silently skip the rollback machinery.
111    fn current_is_acceptable(&self, _nlp_err: Number) -> bool {
112        false
113    }
114
115    /// State-aware acceptance check. Mirrors upstream
116    /// `OptimalityErrorConvergenceCheck::CurrentIsAcceptable` which
117    /// reads the per-component residuals and current `f` to gate the
118    /// `acceptable_dual_inf_tol` / `acceptable_constr_viol_tol` /
119    /// `acceptable_compl_inf_tol` / `acceptable_obj_change_tol`
120    /// triplet. Default delegates to the scalar [`Self::current_is_acceptable`].
121    fn current_is_acceptable_with_state(
122        &self,
123        nlp_err: Number,
124        _data: &IpoptDataHandle,
125        _cq: &IpoptCqHandle,
126    ) -> bool {
127        self.current_is_acceptable(nlp_err)
128    }
129
130    /// Record the current objective at the iterate the main loop just
131    /// stashed as the latest "acceptable point" — mirrors upstream
132    /// `OptimalityErrorConvergenceCheck::SetCurrAcceptableF`. The
133    /// recorded value feeds the `acceptable_obj_change_tol` stability
134    /// cross-check on subsequent iterates. Default no-op for policies
135    /// that don't track acceptable points.
136    fn set_curr_acceptable_obj(&mut self, _obj: Number) {}
137
138    /// Outer NLP convergence tolerance, as used by the main loop's
139    /// almost-feasible bypass guard (port of
140    /// `IpBacktrackingLineSearch.cpp:580`). Default `1e-8` matches
141    /// upstream's default `tol`.
142    fn tol_or_default(&self) -> Number {
143        1e-8
144    }
145
146    /// Primal-feasibility tolerance `constr_viol_tol`, in the unscaled max-norm
147    /// space `curr_unscaled_primal_infeasibility_max` reports — the option that
148    /// declares what a *violated* constraint is.
149    ///
150    /// Read by the status-decision sites that have to answer "is this violation
151    /// real" (gh #508). That question is about the constraint violation, so it
152    /// must be asked with the constraint-violation tolerance; asking it with
153    /// `tol` — a tolerance on the **KKT error**, a different quantity in
154    /// different units — makes the answer move when the user retunes
155    /// convergence and stand still when they retune feasibility. Default `1e-4`
156    /// matches upstream's default `constr_viol_tol`; policies that track no such
157    /// tolerance keep it.
158    fn constr_viol_tol_or_default(&self) -> Number {
159        1e-4
160    }
161
162    /// Acceptable-level primal-feasibility band `acceptable_constr_viol_tol`, in
163    /// the unscaled max-norm space `curr_unscaled_primal_infeasibility_max`
164    /// reports. Read by the best-acceptable fallback's feasibility-aware ranking
165    /// (gh #267), which caps it at the upstream default so a user-widened band
166    /// cannot let the fallback spend feasibility to buy objective. Default
167    /// `1e-2` matches upstream's default `acceptable_constr_viol_tol`; policies
168    /// that track no such tolerance keep it.
169    fn acceptable_constr_viol_tol_or_default(&self) -> Number {
170        1e-2
171    }
172
173    /// Live-update a named convergence tolerance mid-solve, for the
174    /// debugger's in-place option hot-swap. Returns `true` if `name`
175    /// matched a tolerance this policy owns (so the caller can report
176    /// whether it took). Default: this policy exposes no live
177    /// tolerances → `false`.
178    fn set_tolerance(&mut self, _name: &str, _value: Number) -> bool {
179        false
180    }
181}