pounce_algorithm/ipopt_alg.rs
1//! Main optimization loop — port of
2//! `Algorithm/IpIpoptAlg.{hpp,cpp}`.
3//!
4//! Phase 7 ships the loop scaffold matching `Optimize()` lines
5//! 292-563 in upstream. The body invokes:
6//!
7//! 1. `IterateInitializer::set_initial_iterates`
8//! 2. (loop) `OutputIteration` → `CheckConvergence` →
9//! `UpdateBarrierParameter` → `UpdateHessian` →
10//! `ComputeSearchDirection` → `ComputeAcceptableTrialPoint` →
11//! `AcceptTrialPoint`
12//! 3. `correct_bound_multiplier` (kappa_sigma) per `MAIN_LOOP.md`
13//! §"Bound multiplier reset" lines 1055-1134
14//! 4. exception → `SolverReturn` mapping per the table in
15//! `MAIN_LOOP.md`.
16//!
17//! The NLP handle and search-direction calculator are optional:
18//! when both are present, `iterate()` computes a real Newton step and
19//! drives the line search. Without them, `iterate()` runs the bookkeeping
20//! pieces (mu update, hessian update, conv check, kappa_sigma reset)
21//! and is exercised by structural unit tests. The full path lights up
22//! once `pounce-nlp::OrigIpoptNLP` lands.
23
24use crate::alg_builder::AlgorithmBundle;
25use crate::conv_check::r#trait::ConvergenceStatus;
26use crate::intermediate::{CtxGuard, IntermediateContext};
27use crate::ipopt_cq::IpoptCqHandle;
28use crate::ipopt_data::IpoptDataHandle;
29use crate::ipopt_nlp::IpoptNlp;
30use crate::iter_dump::IterDumper;
31use crate::iterate_dump::emit_record as emit_iterate_record;
32use crate::kkt::pd_search_dir_calc::PdSearchDirCalc;
33use crate::line_search::backtracking::Outcome;
34use crate::restoration::{RestorationOutcome, RestorationPhase};
35use pounce_common::diagnostics::DiagnosticsState;
36use pounce_common::types::{Index, Number};
37use pounce_linalg::Vector;
38use pounce_nlp::alg_types::SolverReturn;
39use pounce_nlp::return_codes::AlgorithmMode;
40use pounce_nlp::tnlp::{IpoptCq as TnlpIpoptCq, IpoptData as TnlpIpoptData, IterStats, TNLP};
41use std::cell::RefCell;
42use std::rc::Rc;
43
44/// Dual-divergence guard (pounce#246): only dual-infeasibility growth in the
45/// *elevated* regime (`inf_du` above this) counts toward the streak, so the
46/// noisy early iterations of a normal solve never build one.
47const DUAL_DIV_COUNT_FLOOR: Number = 1e2;
48/// Dual-divergence guard: the guard fires only once `inf_du` is this large in
49/// absolute terms — well above the transient peaks a converging solve reaches
50/// (e.g. the least-square-init path recovers from ~6e9 on emfl050), and below
51/// the ~1e10 where the KKT factorizations begin to choke, so the diversion to
52/// restoration happens *before* the seconds-long factorizations start.
53const DUAL_DIV_FIRE_TOL: Number = 1e8;
54
55/// gh#884 — the scale-relative search direction below which the iterate
56/// counts as **settled** for the dual-divergence-retry signature.
57///
58/// Measured on `d89771bc`, minimum over the iterates where the primal is
59/// already converged: `qpec_small`/`ncp_eq`/origin reaches `8.6e-14`
60/// through the `.nl` path and `4.3e-8` through a Rust TNLP, while
61/// `ralph1`/`direct`/origin — which *must not* fire, because no
62/// sign-feasible multiplier exists at its origin and failing there is
63/// correct — bottoms out at `7.2e-3`. Five orders of separation; `1e-5`
64/// sits near the middle of it in log terms. The census behind this, and
65/// the corpus fixtures that come closest on either side, are in
66/// `dev-notes/mpcc-biactive-dual-divergence.md`.
67const DUAL_DIV_RETRY_STEP_TOL: Number = 1e-5;
68
69/// gh#884 — the unscaled `‖∇L‖∞` floor for that signature.
70///
71/// Deliberately the same `1e2` as [`DUAL_DIV_COUNT_FLOOR`], and for the
72/// same reason: below it a solve is merely mid-flight. It is what
73/// excludes `eigena2` on the L-BFGS leg, which reaches a settled step of
74/// `7.9e-9` but at an unscaled dual of only `37`.
75pub(crate) const DUAL_DIV_RETRY_DU_FLOOR: Number = 1e2;
76
77/// gh#884 — primal infeasibility below which the primal counts as
78/// converged for that signature. The failure mode is defined by the
79/// primal being *done* while the duals run away, so this conjunct is
80/// what separates it from an ordinary struggling solve.
81const DUAL_DIV_RETRY_PRIMAL_TOL: Number = 1e-8;
82
83/// gh #534 — how many consecutive outer NLP errors the progress test reads.
84/// Four samples give three ratios: enough that a single lucky step cannot pass
85/// the test, short enough to still be inside the endgame it is meant to
86/// recognise. `eigena2`'s quoted tail is exactly four iterations long
87/// (`1.19e-5 → 2.96e-6 → 7.38e-7 → 1.84e-7`).
88const DECLINE_PROGRESS_SAMPLES: usize = 4;
89/// gh #534 — default `resto_decline_progress_ratio`: every one of those ratios
90/// must be at least this contraction for the decline to be deferred. `eigena2`
91/// quarters (ratio `0.249`) and passes; `eigenb2`'s tail *rises*
92/// (`1.88e-7, 2.69e-7, 2.89e-7, 2.93e-7`) and fails, which is the intended
93/// split — the issue calls `eigenb2` a plausible genuine stall and the guard
94/// plausibly right there.
95const DEFAULT_DECLINE_PROGRESS_RATIO: Number = 0.5;
96/// gh #534 — outer iterations a deferred continuation gets to produce a strict
97/// certificate before it is cut and the floor reported. `eigena2`'s
98/// extrapolation needs three; ten leaves room for a slower but still genuine
99/// endgame while keeping the cost of a lost bet bounded and small.
100const DECLINE_CONTINUATION_BUDGET: Index = 10;
101/// gh #534 — default for `resto_decline_deferrals`. One deferral is enough for
102/// the reported case (the continuation either converges within the budget or it
103/// does not); more entries would mostly re-bet on a point the first bet already
104/// failed to improve.
105const DEFAULT_RESTO_DECLINE_DEFERRALS: usize = 1;
106/// gh #797 — default for `neg_curv_escapes`. One is enough for the reported
107/// shape: the escape lands on a point whose reduced Hessian *is* positive
108/// definite, so the probe declines there and a second escape would have nothing
109/// to spend itself on. It is also the conservative default — each escape is a
110/// separate bet, and while none of them can return a worse point than the
111/// certificate it left, each costs its own continuation budget.
112/// Default `limited_memory_ls_failure_restarts` (gh #818): **off**. The
113/// rung is available, and it is not what fixes gh #818.
114///
115/// It shipped in the first draft of this work defaulted to one, on a
116/// measurement taken before `ALPHA_INTERP_MIN_TRIALS` existed: with the
117/// interpolation firing on every trial, a line search failed often
118/// enough that standing in front of the restoration hand-off was worth
119/// something. Gating the interpolation removed most of those failures,
120/// and re-measuring the rung on top of the gate turned the trade
121/// negative. `scripts/sweep-fixtures.sh` against `a5e0a837`, both with
122/// the gate, rung off against rung on:
123///
124/// | fixture | rung off | rung on |
125/// |---|---|---|
126/// | `pooling_rt2stp` | `ErrorInStepComputation`/716 — *unmoved from `main`* | `ErrorInStepComputation`/**744** |
127/// | `infeasible_square_scaled_1em4` | `InfeasibleProblemDetected`/24 — *unmoved from `main`* | **26** |
128/// | `deb7` | `ErrorInStepComputation`/1010 | **`RestorationFailed`**/460 |
129/// | `eigena2` | `ErrorInStepComputation`/201 | **`SolvedToAcceptableLevel`**/174 |
130/// | `issue_508_infeasible_gap_1em4` | `InfeasibleProblemDetected`/79 | 76 |
131///
132/// **This ledger is not the one that set the default.** At the gate of
133/// 5 an earlier revision shipped, the rung cost iterations on both
134/// `eigena2` and `infeasible_square_scaled_1em4` to the same verdict,
135/// and that pair is what kept it off. At 6, `eigena2` *gains* a
136/// reportable point. What still argues for off is narrower: the rung
137/// moves two fixtures off the numbers they have on `main` at no benefit
138/// (`pooling_rt2stp`, `infeasible_square_scaled_1em4`), and it changes
139/// `deb7`'s verdict rather than shortening it — a different answer, not
140/// a faster one. Turning it on is a trajectory change over the whole
141/// corpus and needs its own `scripts/sweep-fixtures.sh` run to justify;
142/// **that case has improved and is worth re-opening.** Every
143/// `issue_818_*` test in `pounce-rs` passes with the rung compiled out
144/// — the interpolation is the fix, not this.
145///
146/// Left in the tree rather than deleted because the reasoning behind it
147/// is sound and unaddressed elsewhere: a restoration phase entered at a
148/// feasible point has no constraint violation to minimize and cannot
149/// help. Some model will want it. Setting the option to a positive value
150/// enables it — and note that setting it *at all*, including to 0, opts
151/// out of the `Solved_To_Acceptable_Level` re-solve, because it is a
152/// [`TERMINATION_POLICY_OPTIONS`](crate::application) key.
153const DEFAULT_LBFGS_LS_FAILURE_RESTARTS: usize = 0;
154
155const DEFAULT_NEG_CURV_ESCAPES: usize = 1;
156/// gh #797 — outer iterations a negative-curvature escape gets to produce a
157/// certificate of its own before it is cut and the stationary point reported.
158/// Generous relative to gh #534's ten, because the escape deliberately lands
159/// far from the point it left (a full fraction-to-the-boundary step) and the
160/// continuation is a fresh endgame rather than the tail of one already in
161/// progress.
162const NEG_CURV_CONTINUATION_BUDGET: Index = 30;
163/// gh #797 — cap on the escape step as a multiple of `1 + ‖(x, s)‖∞`. The
164/// probe's direction has unit infinity-norm, so this bounds the escape by the
165/// iterate's own scale; an absolute cap would mean different things on
166/// differently scaled models. Only binds when the fraction-to-the-boundary rule
167/// does not, i.e. when nothing in the direction runs into a bound.
168const NEG_CURV_MAX_STEP_FACTOR: Number = 10.0;
169/// gh #797 — backtracking steps available to the escape, and the ratio between
170/// them. `0.5^12 ≈ 2.4e-4` of the boundary step, past which a direction that
171/// still fails the decrease test is not one worth taking.
172const NEG_CURV_BACKTRACKS: usize = 12;
173const NEG_CURV_BACKTRACK_FACTOR: Number = 0.5;
174/// gh #797 — Armijo factor on the escape's *second-order* decrease model. The
175/// gradient is (near) zero at a stationary point, so the model is
176/// `½α²dᵀ(W + Σ)d` and this is the fraction of it the trial must actually
177/// realise. Mirrors the line search's `eta_phi` in role, not in value: the
178/// quantity being tested is curvature, and a nonlinear objective gives back
179/// less of a quadratic model's prediction than a linear one gives of a
180/// first-order model's.
181const NEG_CURV_ARMIJO: Number = 0.1;
182
183/// See [`IpoptAlgorithm::handover`].
184/// Arguments: the current iterate, the iteration count, the overall NLP
185/// error and the barrier parameter.
186pub type HandoverHook = Box<
187 dyn FnMut(
188 &crate::iterates_vector::IteratesVector,
189 Index,
190 Number,
191 Number,
192 ) -> Option<crate::iterates_vector::IteratesVector>,
193>;
194
195pub struct IpoptAlgorithm {
196 pub data: IpoptDataHandle,
197 pub cq: IpoptCqHandle,
198 pub bundle: AlgorithmBundle,
199 /// Optional NLP handle. Required for any step that evaluates
200 /// problem functions or pulls bound expansion matrices (init,
201 /// search direction, line-search trial-point evaluation). Absent
202 /// in the structural unit tests of Phases 5-6.
203 pub nlp: Option<Rc<RefCell<dyn IpoptNlp>>>,
204 /// Optional TNLP handle — the user-facing problem. When present,
205 /// `iterate()` fires `TNLP::intermediate_callback` once per outer
206 /// iteration so callers can monitor progress or request early
207 /// termination (returning `false` from the callback surfaces as
208 /// `SolverReturn::UserRequestedStop`). Kept separate from `nlp`
209 /// because the algorithm-side NLP is the *compressed* `OrigIpoptNlp`
210 /// view (fixed-variable elimination, c/d split) while the callback
211 /// payload needs to expose the original-coordinate iterate.
212 pub tnlp: Option<Rc<RefCell<dyn TNLP>>>,
213 /// Set on the *restoration* inner IPM so its callback fires report
214 /// `AlgorithmMode::RestorationPhaseMode` (gh#645). Two things hang
215 /// off it, both in [`Self::fire_intermediate`]:
216 ///
217 /// 1. the mode field of the [`IterStats`] payload, which is what
218 /// tells a caller the numbers beside it (`obj`, `inf_pr`,
219 /// `inf_du`, `alpha_*`) describe the min-C1-norm feasibility
220 /// subproblem rather than the user's NLP;
221 /// 2. whether the live-inspector `IntermediateContext` is installed
222 /// — and for a restoration fire it deliberately is **not**. The
223 /// inner iterate is a `CompoundVector` over `(x_orig, n, p)`, so
224 /// it does not even have the user's `n`, and the C API's
225 /// `GetIpoptCurrent*` family checks the caller's `n`/`m` against
226 /// the *problem's* registered dimensions rather than the
227 /// context's. Installing this context would sail past that check
228 /// and read a differently-shaped `cq`. The live accessors
229 /// therefore report "no data" during restoration, which is the
230 /// truth: there is no current iterate of the user's problem
231 /// while the subproblem is being solved.
232 pub fires_as_restoration: bool,
233 /// Interior-point → active-set handover (gh#1054), installed by the
234 /// driver when `ipm_sqp_handover=yes`. Called at every convergence
235 /// check that did not converge, with the current iterate, the
236 /// iteration count, the overall NLP error and `μ`; it decides itself
237 /// whether to try (see `crate::handover`). `Some(point)` is an iterate
238 /// that already meets the stopping tolerances: it replaces `curr` and
239 /// the solve ends `Success`. `None` leaves the interior iteration
240 /// exactly as it was, which is the fallback the request asked for.
241 pub handover: Option<HandoverHook>,
242 /// Whether the live iterate handed to the intermediate callback
243 /// recovers the bound multipliers of fixed variables, as the final
244 /// solution does. Set from `fixed_variable_treatment` by the driver
245 /// (`IpoptApplication::reports_fixed_var_duals`); see
246 /// [`crate::intermediate::ipm_user_iterate`].
247 pub live_iterate_fixed_var_duals: bool,
248 /// Search-direction calculator (`PdSearchDirCalc`). Lands once a
249 /// concrete `SymLinearSolver` backend (MUMPS / FERAL) is wired
250 /// through `AlgBuilder` in Phase 7's tail.
251 pub search_dir: Option<PdSearchDirCalc>,
252 /// Restoration-phase strategy. Invoked when the line search
253 /// returns [`Outcome::Failed`] (port of upstream
254 /// `IpBacktrackingLineSearch::ActivateLineSearch`'s resto
255 /// fallback). Optional: in its absence, line-search failure maps
256 /// directly to [`SolverReturn::RestorationFailure`] so the main
257 /// loop's exit-code semantics match upstream's "no resto built"
258 /// case.
259 pub restoration: Option<Box<dyn RestorationPhase>>,
260
261 /// `kappa_sigma` for the post-AcceptTrialPoint multiplier reset
262 /// (`IpIpoptAlg.cpp:correct_bound_multiplier`, line 1055-1134).
263 pub kappa_sigma: Number,
264 /// `ftb_centrality_floor` (pounce gh#1098): γ of the
265 /// centrality-floored fraction-to-the-boundary rule in
266 /// [`crate::ftb_floor`]; `0` keeps upstream's τ rule.
267 pub ftb_centrality_floor: Number,
268 /// `recalc_y` — recompute `y_c`/`y_d` as least-square estimates
269 /// once the iterate is feasible enough, instead of carrying the
270 /// multipliers the Newton step produced. Upstream registers this
271 /// `no`, but its own option text says "If a limited memory
272 /// quasi-Newton option is chosen, this is used by default", so the
273 /// L-BFGS path auto-enables it (see
274 /// `application.rs`). Costs one extra augmented-system solve on
275 /// every iteration where it fires.
276 ///
277 /// It exists because a quasi-Newton model's multipliers are only as
278 /// good as the Hessian approximation behind them: L-BFGS can reach
279 /// a feasible primal and still fail to drive `inf_du` down, because
280 /// the dual step is computed from an approximate `W`. Re-estimating
281 /// `y` by least squares side-steps the approximation entirely.
282 /// `linear_system_scaling=slack-based` is active, so the
283 /// iterate-dependent `s`-block scaling must be refreshed each
284 /// iteration. See [`Self::push_slack_scaling`].
285 pub slack_based_scaling: bool,
286 pub recalc_y: bool,
287 /// `recalc_y_feas_tol` — the constraint-violation threshold below
288 /// which [`Self::recalc_y`] fires. Upstream default `1e-6`.
289 pub recalc_y_feas_tol: Number,
290 pub max_iter: Index,
291 /// `start_with_resto` — force the feasibility restoration phase in
292 /// the first iteration.
293 ///
294 /// This is an **outer**-loop behaviour, which is where it went wrong
295 /// before: the option was threaded from the `OptionsList` through
296 /// `AlgorithmBuilder::resto` into `RestoAlgorithmBuilder` and on into
297 /// `MinC1NrmDriver`, a field on the *inner* restoration solver, where
298 /// there is no first iteration of the outer algorithm to act on. It
299 /// was set by everything and read by nothing, so `start_with_resto
300 /// yes` was a silent no-op. `unimplemented_options.rs`'s
301 /// `the_restoration_switches_reach_the_builder` asserted only that the
302 /// value reached the builder — the very "read site populating a field
303 /// nobody consumes" its own comment names as the defect to avoid.
304 pub start_with_resto: bool,
305 /// Initial primal step length offered to the line search at the
306 /// top of each iteration. Mirrors `IpBacktrackingLineSearch`'s
307 /// fraction-to-the-boundary primal step (with τ = `data.curr_tau`).
308 /// In v1.0 the structural value here is 1.0 and the FTB cap is
309 /// applied per-component when the line-search driver computes
310 /// trial slacks; the simplification holds for non-degenerate runs.
311 pub alpha_init: Number,
312 /// Tiny-step relative tolerance — port of upstream
313 /// `IpBacktrackingLineSearch::tiny_step_tol_` (default `10·EPSILON`).
314 /// Step is "tiny" when `max_i |δx_i|/(1+|x_i|) ≤ tiny_step_tol`
315 /// (and same for s, and `c_viol ≤ 1e-4`).
316 pub tiny_step_tol: Number,
317 /// Port of upstream `IpIpoptAlg.cpp` divergence guard: when
318 /// `max_i |x_i|` exceeds this threshold the optimization aborts with
319 /// `SolverReturn::DivergingIterates`. Default `1e20` matches the
320 /// registered `diverging_iterates_tol` option. Catches MESH and
321 /// similar cases where the normal-mode IPM heads off to infinity
322 /// (orig `f` to ±1e33 by iter 90) before line-search failure forces
323 /// a degenerate restoration entry.
324 pub diverging_iterates_tol: Number,
325 /// #248 divergence persistence — consecutive iterations the primal
326 /// iterate has kept *growing* while past `diverging_iterates_tol` on a
327 /// structurally unbounded side. A genuine recession ray sustains this;
328 /// a transient ill-scaling excursion on a bounded-below problem peaks
329 /// and recedes (MINLPLib `jit1`: `|x|` climbs to ~16 then falls back to
330 /// ~2.9 at the finite optimum). Reset to zero whenever the iterate is
331 /// within the threshold or is not growing.
332 divergence_streak: u32,
333 /// Consecutive iterations for which the primal divergence guard has
334 /// been suppressed because the line search reported
335 /// [`crate::line_search::backtracking::BacktrackingLineSearch::in_watchdog`].
336 ///
337 /// The suppression is a *deferral*, and this is what bounds it. A
338 /// watchdog sequence is supposed to end within
339 /// `watchdog_trial_iter_max` (default 3) iterations, but the flag can
340 /// outlive one: `run_filter_line_search`'s `TinyStep` arm returns
341 /// without consulting `in_watchdog`, so a tiny step taken mid-watchdog
342 /// hands off to restoration with the flag still set, and
343 /// `reset_after_restoration` clears `watchdog_shortened_iter` but not
344 /// `in_watchdog`. Rather than change the line search's state machine
345 /// (a trajectory change, for a hole this guard need not depend on),
346 /// the guard simply stops deferring past
347 /// [`Self::WATCHDOG_DEFER_MAX`] and checks the iterate anyway. Reset
348 /// to zero on any iteration the guard actually runs.
349 watchdog_defer_streak: u32,
350 /// Largest `|x|` seen in the current growth run (companion to
351 /// [`Self::divergence_streak`]). Zero when no run is active.
352 divergence_prev_amax: Number,
353 /// #252 objective at the previous over-threshold iterate of the current
354 /// growth run (companion to [`Self::divergence_streak`]). A genuine
355 /// recession ray drives the (minimized) objective toward `−∞`, so a
356 /// diverging iterate only counts toward the streak when the objective is
357 /// *still descending* against this reference. A transient ill-scaling
358 /// excursion past a finite optimum grows `|x|` while the objective
359 /// *worsens* (the linear tail dominates), so it never accumulates the
360 /// streak — this is the fix for the unbounded-box (`ub = +∞`) B&B node
361 /// subproblems of jit1 that #248's growth-only check still mislabelled
362 /// `UNBOUNDED`. `+∞` when no run is active.
363 divergence_prev_f: Number,
364 /// #252 objective *decrease* at the previous step of the current growth
365 /// run (`prev_prev_f − prev_f`), the companion that lets the streak
366 /// require the descent to be *non-decelerating*. A recession ray's
367 /// per-step objective drop keeps up or accelerates as `|x|` grows
368 /// geometrically (`f` is at least linear along the ray); an excursion
369 /// converging to a finite optimum has a per-step drop that shrinks
370 /// toward zero. `NaN` (non-finite) until the run has a first finite
371 /// decrease to compare against, which bootstraps the check.
372 divergence_prev_decrease: Number,
373 /// #285 recession-ray persistence — consecutive iterations for which the
374 /// *checked recession-ray proof* ([`Self::curr_is_recession_ray`]) held
375 /// while the primal iterate kept growing. This is a second, independent
376 /// unboundedness path that catches a genuine recession ray in
377 /// `null(A_eq)` over free variables whose `|x|` grows only *linearly*
378 /// (the regularized zero-Hessian step in an equality null space marches
379 /// out at a bounded rate), so it never crosses `diverging_iterates_tol`
380 /// (`1e20`) within `max_iter` and the geometric-growth
381 /// [`Self::divergence_streak`] never accumulates. Reset to zero whenever
382 /// the proof fails or the iterate stops growing.
383 recession_streak: u32,
384 /// Largest `|x|` seen in the current recession-ray run (companion to
385 /// [`Self::recession_streak`]). Zero when no run is active.
386 recession_prev_amax: Number,
387 /// Companion threshold on the dual step — when both primal and dual
388 /// steps are tiny in two consecutive iterations the algorithm
389 /// declares convergence at the best attainable accuracy. Default
390 /// `1e-2` matches upstream.
391 pub tiny_step_y_tol: Number,
392 /// `dual_diverging_streak` (pounce#246) — number of consecutive
393 /// iterations of *growing* dual infeasibility (in the elevated regime,
394 /// `inf_du > `[`DUAL_DIV_COUNT_FLOOR`]) that must accumulate before the
395 /// dual-divergence guard fires. When the streak reaches the limit and
396 /// `inf_du > `[`DUAL_DIV_FIRE_TOL`], the outer routes to restoration.
397 ///
398 /// **`0` (off) is the default**, set from the option of the same name
399 /// (`application.rs`). It defaulted to `15` when introduced; see the option
400 /// help in `upstream_options.rs` for why that changed, and
401 /// [`Self::honour_best_acceptable_after_dual_guard`] for what protects a
402 /// solve when it is enabled. See the guard itself in [`Self::iterate`].
403 pub dual_diverging_streak: usize,
404 dual_inf_prev: Number,
405 dual_growth_streak: usize,
406 /// gh#884 — thresholds for the dual-divergence *retry* signature.
407 /// Distinct from the pounce#246 guard above in both mechanism and
408 /// consequence: that one diverts a running solve to restoration, this
409 /// one only *records* that a cold retry is worth attempting after the
410 /// solve has already given up. Set from options of the same name; see
411 /// [`DUAL_DIV_RETRY_STEP_TOL`] for the measured populations.
412 pub dual_divergence_retry_step_tol: Number,
413 /// Companion floor on the unscaled dual — see
414 /// [`DUAL_DIV_RETRY_DU_FLOOR`].
415 pub dual_divergence_retry_du_floor: Number,
416 /// Scale-relative magnitude of the most recent search direction,
417 /// `max(max_i |δx_i|/(1+|x_i|), max_i |δs_i|/(1+|s_i|))` — the
418 /// `detect_tiny_step` measure, kept as a number rather than a
419 /// predicate. `INFINITY` before the first direction is computed, so
420 /// the signature cannot fire on iteration 0.
421 last_step_rel: Number,
422 /// gh#884 — sticky: set once the four-conjunct signature is seen at a
423 /// single iterate, never cleared. Read by the application layer after
424 /// the solve to decide whether a cold retry is authorized. It never
425 /// changes a verdict by itself.
426 dual_divergence_signature: bool,
427 /// Set true when the previous iterate was tagged tiny; on the
428 /// second consecutive tiny step the loop sets `data.tiny_step_flag`
429 /// so the mu update can attempt to terminate. Mirrors
430 /// `IpBacktrackingLineSearch::tiny_step_last_iteration_`.
431 pub tiny_step_last_iteration: bool,
432 /// How long the tiny-step stop waits for a multiplier step that is not
433 /// settling (`tiny_step_dual_stall_iters`); see
434 /// [`crate::tiny_step_stall`]. Its `released()` is read by the
435 /// application layer, which retries a `StopAtTinyStep` reached this way
436 /// as it would an iteration limit.
437 pub tiny_stall: crate::tiny_step_stall::TinyStepDualStall,
438 /// Whether the current iterate passed the acceptable-level test at the
439 /// top of this iteration. An acceptable iterate has its own exit
440 /// (`acceptable_iter`), so the tiny-step stall rule leaves it to that.
441 curr_iterate_acceptable: bool,
442 /// Cycle-detection state for [`Self::invoke_restoration`]: the
443 /// outer `(x, s)` snapshot from the previous restoration entry,
444 /// cleared on any iteration that exits via a normal line-search
445 /// accept. When restoration is invoked twice in a row and the
446 /// outer iterate has not moved between entries (relative
447 /// 2-norm < 1e-10 on both `x` and `s`), the inner resto-IPM is
448 /// returning Recovered points indistinguishable from `curr` — a
449 /// cycle. Surfaces as `ErrorInStepComputation`. Mirrors the
450 /// *intent* of upstream `IpBacktrackingLineSearch.cpp:580-600`'s
451 /// almost-feasible-resto guard while staying robust against the
452 /// `inf_pr` micro-drift seen on ACOPR14 (delta ~3e-12 per entry,
453 /// inf_du essentially constant) where a scalar-`inf_pr` heuristic
454 /// fails. Productive single-restoration sequences (BT8, HIMMELBJ,
455 /// LINSPANH, LSNNODOC, ODFITS, OET3) clear the snapshot via
456 /// `Outcome::Accepted` between entries and are unaffected.
457 last_resto_entry_x: Option<Box<dyn Vector>>,
458 last_resto_entry_s: Option<Box<dyn Vector>>,
459 /// Snapshot of the *recovery* iterate from the previous
460 /// restoration. Compared against the next entry's `(x, s)` to
461 /// detect "outer made no progress between consecutive resto
462 /// invocations". When this distance is below threshold for
463 /// several consecutive entries, terminate — catching
464 /// slow-non-convergence cycles (ACOPR14, TRO3X3, ACOPR30) where
465 /// resto's *inner* moves substantively each call but the *outer*
466 /// makes no progress between calls. Cleared on any LS-accepted
467 /// step.
468 last_resto_recovery_x: Option<Box<dyn Vector>>,
469 last_resto_recovery_s: Option<Box<dyn Vector>>,
470 /// The most recent restoration recovery, kept only when it was a point
471 /// restoration handed back without releasing it — layer 2's
472 /// converged-to-feasible-point verdict, which upstream treats as a
473 /// restoration failure (gh#1042, see
474 /// [`RestorationPhase::last_recovery_unreleased`]). Unlike
475 /// [`Self::last_resto_recovery_x`] it is **not** cleared by an accepted
476 /// step: a watchdog that tries steps from the handed-back point and
477 /// rolls back to it has not left it either. Replaced at every recovery.
478 last_resto_unreleased_x: Option<Box<dyn Vector>>,
479 last_resto_unreleased_s: Option<Box<dyn Vector>>,
480 /// Unscaled max-norm violation of the original NLP at
481 /// [`Self::last_resto_unreleased_x`] — what the gh#1042 exit judges its
482 /// verdict on (gh#1076). The model's rows and box against their declared
483 /// bounds, not the slack gap `d − s` (gh#1146). Meaningful only while that
484 /// point is held.
485 last_resto_unreleased_theta_unscaled: Number,
486 /// An unreleased hand-back whose shot the outer has not yet taken
487 /// (gh#1076). Set by every unreleased recovery; cleared by the first
488 /// step the line search accepts on the *original* criterion — anything
489 /// but a soft-restoration `s` step, which is accepted precisely because
490 /// the original filter would not take it. Consumed at the next
491 /// restoration entry.
492 unreleased_shot_pending: bool,
493 /// Consecutive restoration entries at which
494 /// [`Self::unreleased_shot_pending`] was still set (gh#1076). Cleared
495 /// by any step the original globalization accepts and by a released
496 /// recovery.
497 unreleased_shot_strikes: u32,
498 /// Set when the gh#1042 exit below ended this solve (see
499 /// [`Self::unreleased_handback_exit`]). Never cleared within a solve.
500 unreleased_handback_exit: bool,
501 /// Count of consecutive restoration entries on which the outer
502 /// step (recovery → next-entry) was below the iterate-distance
503 /// threshold. Cleared on any LS-accepted step. Limit chosen to
504 /// let MAKELA3, HAIFAM, HALDMADS, ROBOT, TENBARS2 — which need
505 /// 2-3 consecutive resto entries to recover — pass through.
506 resto_no_outer_progress_count: usize,
507 /// `resto_decline_deferrals` (gh #534) — how many times the
508 /// acceptable-point restoration decline in [`Self::invoke_restoration`] may
509 /// be *deferred* on a solve whose NLP error is still contracting. `0`
510 /// restores the pre-#534 behaviour (decline immediately, always).
511 ///
512 /// See [`Self::may_defer_acceptable_decline`] for the progress test and
513 /// [`Self::honour_decline_floor`] for what makes a spent deferral harmless.
514 pub resto_decline_deferrals: usize,
515 /// `resto_decline_progress_ratio` (gh #534) — the contraction each of the
516 /// last [`DECLINE_PROGRESS_SAMPLES`]` - 1` iterations must have achieved for
517 /// the decline to be deferred. Default
518 /// [`DEFAULT_DECLINE_PROGRESS_RATIO`]. A value of `1` admits any
519 /// non-increasing window and a large one drops the progress requirement
520 /// altogether, which is the "patch the guard and see" experiment the issue
521 /// asks for, available without patching.
522 pub resto_decline_progress_ratio: Number,
523 /// The most recent outer-iteration NLP errors, oldest first, with
524 /// [`Self::nlp_err_recent_len`] entries live. Feeds the gh #534 progress
525 /// test and nothing else.
526 nlp_err_recent: [Number; DECLINE_PROGRESS_SAMPLES],
527 nlp_err_recent_len: usize,
528 /// gh #534 — deferrals of the acceptable-point decline spent so far.
529 decline_deferrals_used: usize,
530 /// gh #534 — the iterate the guard would have returned had it not been
531 /// deferred. Captured at the *first* deferral only, because that point is
532 /// precisely the answer the pre-#534 build reports; it is the floor the
533 /// continuation is never allowed to fall below.
534 decline_floor: Option<VetoSnapshot>,
535 /// gh #534 — outer iteration by which the deferred continuation must have
536 /// produced a strict certificate. Past it the continuation is cut and the
537 /// floor is reported, so the bet costs a bounded number of iterations.
538 decline_deadline_iter: Option<Index>,
539 /// `neg_curv_escapes` (gh #797) — how many times a certified stationary
540 /// point whose reduced Hessian is not positive semidefinite may be *left*
541 /// along a direction of negative curvature instead of reported. `0` restores
542 /// the pre-#797 behaviour (report the first-order certificate, whatever its
543 /// curvature).
544 ///
545 /// See [`Self::try_neg_curv_escape`] for the test and the step, and
546 /// [`Self::honour_neg_curv_floor`] for what makes a lost bet harmless.
547 pub neg_curv_escapes: usize,
548 /// `limited_memory_ls_failure_restarts` (gh #818) — how many times a
549 /// line-search failure at an *already feasible* point may re-anchor
550 /// the quasi-Newton model and retry, instead of handing off to a
551 /// restoration phase that has no constraint violation to reduce.
552 /// `0` restores the pre-#818 behaviour (always hand off).
553 ///
554 /// See [`Self::try_reanchor_before_restoration`] for the rung and
555 /// what bounds it.
556 pub lbfgs_ls_failure_restarts: usize,
557 /// gh #818 — re-anchors spent so far this solve.
558 lbfgs_ls_restarts_used: usize,
559 /// gh #797 — escapes spent so far this solve.
560 neg_curv_escapes_used: usize,
561 /// Sink for the last `IterStats` handed to the user's
562 /// `intermediate_callback` (pounce#870). A second-opinion retry that loses
563 /// needs it, so that the trace a consumer accumulated can be made to end
564 /// on the iterate actually reported. Set by `IpoptApplication`; `None`
565 /// everywhere else.
566 pub last_iter_stats_sink: Option<Rc<RefCell<Option<IterStats>>>>,
567 /// gh #797 — the certified stationary point the escape left. It is a strict
568 /// certificate, so it is the floor the continuation must beat *with a
569 /// certificate of its own* to be preferred.
570 ///
571 /// With more than one escape available this holds the **best** of the
572 /// certificates left so far, not the most recent (gh #805). The first entry
573 /// is the answer a `neg_curv_escapes = 0` build returns, and it is only
574 /// ever displaced by a point that outranks it, so the guarantee holds at
575 /// any number of escapes and the floor never moves backwards.
576 neg_curv_floor: Option<VetoSnapshot>,
577 /// gh #797 — outer iteration by which the escape's continuation must have
578 /// produced a certificate. Past it the continuation is cut and the floor
579 /// reported, so the bet costs a bounded number of iterations.
580 neg_curv_deadline_iter: Option<Index>,
581 /// Count of consecutive restoration entries on which the outer
582 /// constraint violation at entry was already below `tol` (the
583 /// outer optimality tolerance). Matches the *intent* of upstream
584 /// `IpBacktrackingLineSearch.cpp:580-600`'s almost-feasible-resto
585 /// guard while using a looser cv threshold (`tol` vs `1e-2·tol`)
586 /// — catches DECONVBNE's resto-thrash where each cycle re-enters
587 /// at cv ≈ 3e-10 < tol with bound multipliers reset to 1, the
588 /// outer's σ-blowup explodes inf_du to 1.9e7, alpha-min triggers
589 /// resto re-entry, and the (inf_pr, inf_du) post-recovery state
590 /// is essentially identical across cycles but `x` drifts enough
591 /// that [`Self::last_resto_recovery_x`]-based detection misses.
592 /// Cumulative (never cleared on LS-accept), since DECONVBNE's
593 /// cycle interleaves R-recoveries with sub-tol accepts that
594 /// accomplish no real outer progress. Fires after 3 near-feasible
595 /// entries — surfaces as `StopAtAcceptablePoint` since the
596 /// recovered point already satisfies constraint feasibility
597 /// within `tol`.
598 resto_near_feasible_count: usize,
599 /// The pending restoration entry was forced by a non-finite Hessian
600 /// (the factorization rejected the KKT before inertia was read), so it
601 /// is not a `resto_near_feasible_count` strike. Set at the
602 /// step-computation-failure hand-off, consumed by `invoke_restoration`.
603 resto_entry_nonfinite_w: bool,
604 /// Snapshot of the most recent iterate that the convergence check
605 /// flagged "acceptable" (NLP error ≤ `acceptable_tol`). Mirrors
606 /// upstream `IpBacktrackingLineSearch::acceptable_iterate_`
607 /// (`IpBacktrackingLineSearch.cpp:1286-1310`). Used by
608 /// [`Self::restore_acceptable_point`] to roll back when restoration
609 /// fails — if such an iterate exists, the algorithm exits with
610 /// `SolverReturn::StopAtAcceptablePoint` rather than
611 /// `RestorationFailure`. Cleared/refreshed on every iteration that
612 /// satisfies the acceptable predicate.
613 acceptable_iterate: Option<crate::iterates_vector::IteratesVector>,
614 /// The first iterate whose *strict* certificate the masked-scale veto
615 /// refused (gh #200), kept so the refusal can be undone verbatim if the
616 /// continued run does not do better. Deliberately not the acceptable
617 /// snapshot: that one is overwritten unconditionally and drifts.
618 vetoed: Option<VetoSnapshot>,
619 /// The iterate at which a refused *acceptable-level* termination would have
620 /// fired. Held separately from `vetoed` because it restores under a weaker
621 /// status, and claiming `Success` for it would over-report.
622 vetoed_acceptable: Option<VetoSnapshot>,
623 /// Whether a strict refusal has already been *seen*, independent of whether
624 /// a snapshot was successfully captured for it.
625 ///
626 /// This is the first-only latch, held apart from `vetoed` on purpose.
627 /// Testing `vetoed.is_none()` instead would let a refusal whose capture
628 /// failed be "completed" at a later iterate — the veto flag on the
629 /// convergence check is sticky, so it still reads true next pass, and the
630 /// fallback would then restore a point that never passed the strict test.
631 /// With the latch, a failed capture stays failed and the fallback declines.
632 ///
633 /// Declining is *not* the baseline outcome — the baseline stopped and
634 /// reported a certificate at the uncaptured iterate, and declining fails to
635 /// reproduce it. It is the least-bad handling of an unidentifiable baseline,
636 /// not a faithful one.
637 vetoed_seen: bool,
638 /// Same latch for the acceptable-level refusal.
639 vetoed_acceptable_seen: bool,
640 /// Whether the dual-divergence guard (pounce#246) actually fired this
641 /// solve. Gates the *use* of [`Self::best_acceptable`], so solves the guard
642 /// never touches behave identically — see
643 /// [`Self::honour_best_acceptable_after_dual_guard`].
644 dual_guard_fired: bool,
645 /// Best (lowest scaled objective) acceptable-quality iterate seen anywhere
646 /// in this solve. Recorded unconditionally — including *before* any
647 /// diversion, which is the point: the guard returns to the driver before
648 /// the recording site on the iteration it fires, so gating the recording on
649 /// `dual_guard_fired` would miss everything up to and including the
650 /// diversion. Only read when `dual_guard_fired`.
651 best_acceptable: Option<VetoSnapshot>,
652 /// `kkt_fidelity_tol` (pounce#173), needed here — not just at termination —
653 /// because the fallback's tiebreak has to predict the post-solve status
654 /// gate. See [`Self::honour_refused_certificate`]. Zero (the default)
655 /// disables the gate, and with it every tiebreak effect it has.
656 pub kkt_fidelity_tol: Number,
657 acceptable_iter_number: Index,
658 /// Shared per-solve diagnostics state. `None` unless the CLI
659 /// requested `--dump <cat>:<spec>`. When set, the outer loop
660 /// advances the state's iter counter and the augmented-system
661 /// solver consults it to gate KKT dumps.
662 diagnostics: Option<Rc<DiagnosticsState>>,
663 /// Optional interactive debugger. Shared (`Rc<RefCell<…>>`) so the
664 /// same debugger instance also drives the restoration inner IPM —
665 /// one debugger sees both levels. Fired at every
666 /// [`crate::debug::Checkpoint`]. See `crate::debug`.
667 debug: Option<Rc<RefCell<dyn crate::debug::DebugHook>>>,
668
669 // ---- Restoration-phase audit counters (pounce#12). ----
670 //
671 // Drained into `SolveStatistics` by `IpoptApplication::optimize_constrained`
672 // after the solve completes. Counts are cumulative across the run.
673 /// Number of `invoke_restoration` entries.
674 pub resto_calls: Index,
675 /// Sum of inner-IPM iter counts across every restoration call.
676 pub resto_inner_iters: Index,
677 /// Number of outer iters that ran in restoration mode (R-line
678 /// equivalents in `print_level=5` output).
679 pub resto_outer_iters: Index,
680 /// Cumulative wall-clock seconds spent inside `perform_restoration`.
681 pub resto_wall_secs: Number,
682
683 // ---- Per-iteration history capture (pounce#8, pounce#71). ----
684 //
685 // The per-iteration trajectory is no longer accumulated on the
686 // algorithm: `iterate()` emits a structured `pounce::iteration`
687 // event each step, and `pounce_observability::IterCollectorLayer`
688 // rebuilds the `IterRecord`s into the active `IterCaptureGuard`
689 // that `IpoptApplication` installs around the solve.
690 /// When `false`, the per-iteration table that `iterate()` writes
691 /// straight to stdout is suppressed. Wired from
692 /// `IpoptApplication`'s `print_level` option: level 0 turns this
693 /// off (matches upstream's "no console output" contract). Default
694 /// `true` so CLI / direct-driver users keep the familiar trace.
695 pub print_iter_output: bool,
696}
697
698impl IpoptAlgorithm {
699 /// Diagnostics from the safeguarded `least_square_init_primal`
700 /// initializer step (gh#605). `None` when the step was not run.
701 pub fn least_square_init_report(&self) -> Option<crate::init::default::LeastSquareInitReport> {
702 self.bundle.init.least_square_report()
703 }
704
705 /// Restoration's release test, evaluated in the outer filter's own
706 /// quantities. Mirrors upstream `IpRestoFilterConvCheck::TestOrigProgress`,
707 /// which sets the original problem's trial point to the restoration
708 /// iterate's `(x, s)` and reads `trial_barrier_obj()` and
709 /// `trial_constraint_violation()` from it: the barrier objective at the
710 /// outer `mu` (damping included) and the 1-norm violation, the same pair
711 /// [`Self`]'s filter entries and reference point were made with.
712 ///
713 /// The outer trial point is put back afterwards. Nothing else reads it
714 /// during restoration, but the line search that follows sets it anew and
715 /// must not find a stale restoration iterate there.
716 fn orig_progress_check(
717 &self,
718 accept: crate::restoration::OrigProgressCallback,
719 ) -> crate::restoration::OrigProgressCheck {
720 let data = Rc::clone(&self.data);
721 let cq = Rc::clone(&self.cq);
722 Box::new(move |x: &dyn Vector, s: &dyn Vector| {
723 let Some(curr) = data.borrow().curr.clone() else {
724 return false;
725 };
726 let mut probe = curr.with_x(Rc::from(x.make_new_copy()));
727 probe.s = Rc::from(s.make_new_copy());
728 let saved = data.borrow_mut().trial.replace(probe);
729 let (barr, theta) = {
730 let cq = cq.borrow();
731 (cq.trial_barrier_obj(), cq.trial_constraint_violation())
732 };
733 data.borrow_mut().trial = saved;
734 barr.is_finite() && accept(barr, theta)
735 })
736 }
737
738 pub fn new(data: IpoptDataHandle, cq: IpoptCqHandle, mut bundle: AlgorithmBundle) -> Self {
739 // The builder may pre-populate `bundle.search_dir` when given a
740 // `LinearBackendFactory`; lift it onto the algorithm so the
741 // iterate body can call into it directly.
742 let search_dir = bundle.search_dir.take();
743 Self {
744 data,
745 cq,
746 bundle,
747 nlp: None,
748 tnlp: None,
749 handover: None,
750 fires_as_restoration: false,
751 live_iterate_fixed_var_duals: true,
752 search_dir,
753 restoration: None,
754 kappa_sigma: 1e10,
755 ftb_centrality_floor: crate::ftb_floor::DEFAULT_FTB_CENTRALITY_FLOOR,
756 slack_based_scaling: false,
757 recalc_y: false,
758 recalc_y_feas_tol: 1e-6,
759 max_iter: 3000,
760 start_with_resto: false,
761 alpha_init: 1.0,
762 tiny_step_tol: 10.0 * Number::EPSILON,
763 diverging_iterates_tol: 1e20,
764 divergence_streak: 0,
765 watchdog_defer_streak: 0,
766 divergence_prev_amax: 0.0,
767 divergence_prev_f: Number::INFINITY,
768 divergence_prev_decrease: Number::NAN,
769 recession_streak: 0,
770 recession_prev_amax: 0.0,
771 tiny_step_y_tol: 1e-2,
772 dual_diverging_streak: 15,
773 dual_inf_prev: 0.0,
774 dual_growth_streak: 0,
775 dual_divergence_retry_step_tol: DUAL_DIV_RETRY_STEP_TOL,
776 dual_divergence_retry_du_floor: DUAL_DIV_RETRY_DU_FLOOR,
777 last_step_rel: Number::INFINITY,
778 dual_divergence_signature: false,
779 tiny_step_last_iteration: false,
780 tiny_stall: crate::tiny_step_stall::TinyStepDualStall::new(10),
781 curr_iterate_acceptable: false,
782 last_resto_entry_x: None,
783 last_resto_entry_s: None,
784 last_resto_recovery_x: None,
785 last_resto_unreleased_x: None,
786 last_resto_unreleased_s: None,
787 last_resto_unreleased_theta_unscaled: 0.0,
788 unreleased_shot_pending: false,
789 unreleased_shot_strikes: 0,
790 unreleased_handback_exit: false,
791 last_resto_recovery_s: None,
792 resto_no_outer_progress_count: 0,
793 resto_decline_deferrals: DEFAULT_RESTO_DECLINE_DEFERRALS,
794 neg_curv_escapes: DEFAULT_NEG_CURV_ESCAPES,
795 neg_curv_escapes_used: 0,
796 last_iter_stats_sink: None,
797 lbfgs_ls_failure_restarts: DEFAULT_LBFGS_LS_FAILURE_RESTARTS,
798 lbfgs_ls_restarts_used: 0,
799 neg_curv_floor: None,
800 neg_curv_deadline_iter: None,
801 resto_decline_progress_ratio: DEFAULT_DECLINE_PROGRESS_RATIO,
802 nlp_err_recent: [Number::NAN; DECLINE_PROGRESS_SAMPLES],
803 nlp_err_recent_len: 0,
804 decline_deferrals_used: 0,
805 decline_floor: None,
806 decline_deadline_iter: None,
807 resto_near_feasible_count: 0,
808 resto_entry_nonfinite_w: false,
809 acceptable_iterate: None,
810 vetoed: None,
811 vetoed_acceptable: None,
812 dual_guard_fired: false,
813 best_acceptable: None,
814 vetoed_seen: false,
815 vetoed_acceptable_seen: false,
816 kkt_fidelity_tol: 0.0,
817 acceptable_iter_number: 0,
818 diagnostics: None,
819 debug: None,
820 resto_calls: 0,
821 resto_inner_iters: 0,
822 resto_outer_iters: 0,
823 resto_wall_secs: 0.0,
824 print_iter_output: true,
825 }
826 }
827
828 /// Stash the current iterate as the "last acceptable" backup —
829 /// port of `IpBacktrackingLineSearch::StoreAcceptablePoint`
830 /// (`IpBacktrackingLineSearch.cpp:1286-1293`).
831 fn store_acceptable_point(&mut self) {
832 let d = self.data.borrow();
833 if let Some(curr) = d.curr.as_ref() {
834 self.acceptable_iterate = Some(curr.clone());
835 self.acceptable_iter_number = d.iter_count;
836 }
837 }
838
839 /// Record this outer iteration's NLP error for the gh #534 progress test.
840 ///
841 /// One push per `iterate()` call, so the samples are consecutive outer
842 /// iterations by construction. Deliberately *not* cleared when restoration
843 /// recovers: a recovery that helped shows up as continued contraction and a
844 /// recovery that hurt shows up as a jump, and the ratio test reads both
845 /// correctly without needing to know which happened.
846 fn note_nlp_err(&mut self, nlp_err: Number) {
847 push_sample(
848 &mut self.nlp_err_recent,
849 &mut self.nlp_err_recent_len,
850 nlp_err,
851 );
852 }
853
854 /// Whether the last [`DECLINE_PROGRESS_SAMPLES`] outer iterations each cut
855 /// the NLP error by at least `resto_decline_progress_ratio` (gh #534).
856 ///
857 /// The question the restoration-decline guard never asked: *is this solve
858 /// still converging?* A full window is required, so the test cannot pass on
859 /// a short history — the early iterations of every solve included.
860 ///
861 /// The test itself lives in the pure [`window_is_contracting`], for the
862 /// reason [`ranks_better_within_band`] does: what it must and must not fire
863 /// on is stated in the issue as two recorded traces, and those are provable
864 /// by deterministic unit test rather than inferable from a solve.
865 fn nlp_err_contracting(&self) -> bool {
866 if self.nlp_err_recent_len < DECLINE_PROGRESS_SAMPLES {
867 return false;
868 }
869 window_is_contracting(&self.nlp_err_recent, self.resto_decline_progress_ratio)
870 }
871
872 /// The live progress window, oldest first, for the gh #534 trace lines.
873 fn nlp_err_window_str(&self) -> String {
874 let live = &self.nlp_err_recent[..self.nlp_err_recent_len];
875 let parts: Vec<String> = live.iter().map(|e| format!("{e:.3e}")).collect();
876 format!("[{}]", parts.join(" -> "))
877 }
878
879 /// Roll the iterate back to the last acceptable snapshot — port of
880 /// `IpBacktrackingLineSearch::RestoreAcceptablePoint`
881 /// (`IpBacktrackingLineSearch.cpp:1295-1310`). Returns `true` if a
882 /// snapshot was available and applied; `false` otherwise (caller
883 /// then surfaces the original failure status).
884 fn restore_acceptable_point(&mut self) -> bool {
885 let Some(prev) = self.acceptable_iterate.clone() else {
886 return false;
887 };
888 let mut d = self.data.borrow_mut();
889 d.set_trial(prev);
890 // `accept_trial_point` promotes `trial → curr`, mirroring the
891 // upstream sequence `set_trial(...); AcceptTrialPoint();`.
892 d.accept_trial_point();
893 true
894 }
895
896 /// Whether a diverging primal iterate is consistent with the feasible
897 /// region actually being *unbounded* (issue #248).
898 ///
899 /// `DivergingIterates` is Ipopt's unboundedness verdict, but a large
900 /// `|x_i|` only proves unboundedness if variable `i` is free to escape
901 /// to infinity in the direction it is heading — i.e. it has no finite
902 /// bound on that side. This lifts a vector of ones from the compressed
903 /// lower/upper bound spaces through the `Px_L` / `Px_U` expansion
904 /// matrices to obtain full-length indicators of which variables carry a
905 /// finite bound, then returns `true` only when some component whose
906 /// magnitude exceeds `diverging_iterates_tol` is heading toward a side
907 /// with no finite bound.
908 ///
909 /// When every large component is pinned by a finite bound — in
910 /// particular when all variables are boxed, so the feasible region is a
911 /// bounded box and unboundedness is structurally impossible — this
912 /// returns `false`, and the caller reports the best iterate via the
913 /// normal convergence / restoration path instead of a spurious
914 /// `Unbounded`.
915 /// #248: consecutive growing, over-threshold iterations required before
916 /// a structurally-free divergence is reported as `DivergingIterates`.
917 /// `jit1`'s transient excursion lasts ~2 growing steps and then
918 /// recedes, so a small persistence requirement clears it without
919 /// materially delaying a genuine ray.
920 const DIVERGENCE_PERSIST_ITERS: u32 = 4;
921 /// #248: an iterate counts as "still growing" toward divergence when it
922 /// grows at least this factor over the previous over-threshold iterate.
923 /// A recession ray in an interior-point method grows geometrically; an
924 /// iterate settling onto a finite optimum above the threshold does not.
925 const DIVERGENCE_GROWTH_FACTOR: Number = 2.0;
926 /// #252: the objective descent must *keep up* — each step's drop must be
927 /// at least this fraction of the previous step's drop for the iterate to
928 /// count toward the divergence streak. A recession ray descends `f` to
929 /// `−∞` with per-step drops that grow (ratio ≥ 1) as `|x|` grows
930 /// geometrically; an excursion converging to a finite optimum decelerates
931 /// (ratio → 0). The slack below 1 tolerates ordinary interior-point noise
932 /// on a genuine ray without admitting a decelerating excursion — jit1's
933 /// node subproblems shrink the drop by 3–15× per step, far past this bar.
934 const DIVERGENCE_DESCENT_KEEPUP: Number = 0.9;
935 /// #248: absolute runaway backstop. An iterate this large is reported
936 /// unbounded regardless of persistence. It sits at or below the default
937 /// `diverging_iterates_tol = 1e20`, so the default behaviour (fire the
938 /// instant `|x|` crosses the threshold) is preserved, while a low
939 /// user threshold no longer fires on the way to a finite optimum.
940 const DIVERGENCE_ABS_RUNAWAY: Number = 1e18;
941 /// gh#1076: consecutive restoration re-entries, each after an unreleased
942 /// hand-back from which the outer accepted only soft-restoration `s`
943 /// steps, before the gh#1042 exit ends the solve. One is not enough:
944 /// `mpcc_qpec_small_biactive` under `issue_884`'s `REPRO` options takes
945 /// one `s` step from its first hand-back, re-enters, and converges after
946 /// the second (Optimal at 97, unscaled dual 1.3e-10); ending it at the
947 /// first strike sends it through a retry (69 + 19). Vanderbei brainpc0
948 /// strikes 45 times in a row, so the budget costs it two cycles.
949 const UNRELEASED_SHOT_STRIKES: u32 = 3;
950 /// Most consecutive iterations the primal divergence guard will defer
951 /// to a watchdog sequence before checking the iterate anyway. Upstream's
952 /// `watchdog_trial_iter_max` default is 3; one spare covers the
953 /// iteration on which the watchdog is armed. See
954 /// [`Self::watchdog_defer_streak`] for why the bound is not simply
955 /// "until `in_watchdog` clears".
956 const WATCHDOG_DEFER_MAX: u32 = 4;
957
958 /// #285: magnitude floor for the checked recession-ray unboundedness path.
959 /// Below this the (slightly more expensive) recession proof is not even
960 /// attempted, so it is inert on every normal, well-scaled solve. Above it,
961 /// unboundedness is only ever concluded through the full checked proof in
962 /// [`Self::curr_is_recession_ray`] — a genuinely *feasible* iterate of this
963 /// magnitude already witnesses an unbounded feasible region, and the proof
964 /// additionally certifies the escape direction. Sits far below the
965 /// `diverging_iterates_tol` (`1e20`) magnitude guard so a linearly-growing
966 /// ray (which never reaches `1e20` within `max_iter`) is still caught.
967 const RECESSION_MIN_NORM: Number = 1e10;
968 /// #285: consecutive growing, proof-passing iterations required before the
969 /// recession-ray path reports `DivergingIterates`. A bounded feasible
970 /// region cannot supply a *growing* sequence of feasible over-floor
971 /// iterates, so persistence is defense-in-depth against a lone numerical
972 /// fluke rather than a soundness requirement.
973 const RECESSION_PERSIST_ITERS: u32 = 4;
974 /// #285: relative feasibility bar for the recession proof. The current
975 /// iterate counts as feasible (hence a witness that the feasible region
976 /// reaches its magnitude) when its unscaled max-norm primal infeasibility
977 /// is at most this fraction of `|x|_∞`. The check is *relative* on purpose:
978 /// evaluating `A_eq x − b` at `|x| ~ 1e17` carries floating-point roundoff
979 /// that scales with `|x|`, while a genuinely infeasible excursion (e.g.
980 /// mid-restoration) has a residual comparable to `|x|` itself.
981 const RECESSION_FEAS_REL: Number = 1e-6;
982 /// #285: relative bar for "the escape direction lies in `null(A_eq)`".
983 /// `‖J_c x‖_∞ ≤ this · |x|_∞` certifies that moving along `d ≈ x` preserves
984 /// the (linearized) equality constraints — `A_eq d ≈ 0`.
985 const RECESSION_DIR_TOL: Number = 1e-6;
986 /// #285: relative descent bar. The objective must strictly decrease along
987 /// the escape direction with a real margin — `∇f·x ≤ −this · ‖∇f‖ ‖x‖` —
988 /// so a variable drifting orthogonally to the objective (`∇f·x ≈ 0`) can
989 /// never be mistaken for a recession ray driving `f → −∞`.
990 const RECESSION_DESC_REL: Number = 1e-6;
991
992 /// Update the divergence-persistence state for the current iterate and
993 /// return whether `DivergingIterates` should be reported now (issues
994 /// #248 / #252). `amax` is `max_i |x_i|`; `structural_free` is the result
995 /// of [`Self::divergence_is_true_unboundedness`] (already gated on
996 /// `amax > diverging_iterates_tol`); `f` is the (minimized, internally
997 /// scaled) objective at the current iterate, supplied only while
998 /// `structural_free` holds.
999 ///
1000 /// A large `|x|` is reported as unbounded only when it is heading to an
1001 /// unbounded side (`structural_free`) *and* the divergence looks like a
1002 /// genuine recession ray: the iterate keeps *growing* while the objective
1003 /// keeps *descending toward `−∞` without decelerating* — the per-step drop
1004 /// holds up as `|x|` grows geometrically — for
1005 /// [`Self::DIVERGENCE_PERSIST_ITERS`] consecutive iterations (or it has
1006 /// blown past the absolute runaway backstop). Two failure modes are thereby
1007 /// left to the normal convergence machinery instead of being mislabelled
1008 /// `UNBOUNDED`:
1009 ///
1010 /// * #248 — a transient ill-scaling excursion that peaks in `|x|` and
1011 /// recedes never sustains the growth streak.
1012 /// * #252 — an excursion that *keeps* growing in `|x|` toward an unbounded
1013 /// box side (a jit1 B&B node subproblem with `ub = +∞`), lowering `f` as
1014 /// it goes, but with a per-step objective drop that *decelerates* toward
1015 /// zero: it is settling onto a finite optimum, not riding a recession
1016 /// ray. The descent must keep up (not merely exist), so this no longer
1017 /// accumulates the streak.
1018 fn update_divergence_verdict(
1019 &mut self,
1020 amax: Option<Number>,
1021 structural_free: bool,
1022 f: Option<Number>,
1023 ) -> bool {
1024 let over = matches!(amax, Some(a) if a > self.diverging_iterates_tol) && structural_free;
1025 if !over {
1026 self.divergence_streak = 0;
1027 self.divergence_prev_amax = 0.0;
1028 self.divergence_prev_f = Number::INFINITY;
1029 self.divergence_prev_decrease = Number::NAN;
1030 return false;
1031 }
1032 let a = amax.expect("over implies amax is Some");
1033 // A recession ray in an interior-point method grows the iterate
1034 // geometrically *and* drives the objective down without bound, with a
1035 // per-step drop that keeps up as `|x|` grows. A finite-optimum
1036 // excursion may grow `|x|` and even lower `f` for a few steps, but its
1037 // per-step objective drop decelerates toward zero as it settles onto
1038 // the finite floor. Require all three — growth, descent, and
1039 // non-decelerating descent — before a step counts toward the streak.
1040 let growing = a >= self.divergence_prev_amax * Self::DIVERGENCE_GROWTH_FACTOR;
1041 // `f` is `None` only when `structural_free` is false, already handled
1042 // by the `!over` branch; treat a missing value as non-descending so a
1043 // run can never accumulate without objective evidence.
1044 let fv = f.unwrap_or(Number::INFINITY);
1045 let decrease = self.divergence_prev_f - fv;
1046 let descending = decrease > 0.0;
1047 // Non-decelerating: the drop must be at least a fixed fraction of the
1048 // previous step's drop. Bootstrapped `true` until a first finite
1049 // decrease has been recorded (`divergence_prev_decrease` non-finite),
1050 // so the run's opening steps are admitted on growth + descent alone.
1051 let keeping_up = !self.divergence_prev_decrease.is_finite()
1052 || decrease >= self.divergence_prev_decrease * Self::DIVERGENCE_DESCENT_KEEPUP;
1053 if growing && descending && keeping_up {
1054 self.divergence_streak += 1;
1055 } else {
1056 // Over the threshold on an unbounded side, but the divergence is
1057 // not sustaining a recession ray's growth-and-accelerating-descent
1058 // profile — the hallmark of a scaling excursion toward a finite
1059 // optimum. Drop the streak; a genuine ray re-accumulates it on its
1060 // next qualifying step (or trips the absolute runaway backstop).
1061 self.divergence_streak = 0;
1062 }
1063 self.divergence_prev_amax = a;
1064 self.divergence_prev_f = fv;
1065 // Record the baseline for the next step's keep-up comparison only from
1066 // a finite, real decrease; skip the `+∞` opening step and reset the
1067 // baseline whenever the objective stops descending.
1068 self.divergence_prev_decrease = if decrease.is_finite() && descending {
1069 decrease
1070 } else {
1071 Number::NAN
1072 };
1073 a >= Self::DIVERGENCE_ABS_RUNAWAY
1074 || self.divergence_streak >= Self::DIVERGENCE_PERSIST_ITERS
1075 }
1076
1077 fn divergence_is_true_unboundedness(&self, x: &dyn Vector) -> bool {
1078 self.free_to_escape_over(x, self.diverging_iterates_tol)
1079 }
1080
1081 /// Shared core of the free-variable structural check: returns `true` when
1082 /// some component of `x` with magnitude exceeding `thresh` is heading
1083 /// toward a side (positive → upper, negative → lower) that carries *no*
1084 /// finite bound, so it is free to escape to infinity. Parameterized on the
1085 /// magnitude threshold so both the `diverging_iterates_tol` (`1e20`)
1086 /// divergence guard and the lower `RECESSION_MIN_NORM` recession-ray path
1087 /// (#285) share one implementation.
1088 fn free_to_escape_over(&self, x: &dyn Vector, thresh: Number) -> bool {
1089 use pounce_linalg::DenseVector;
1090
1091 let cq = self.cq.borrow();
1092 let nlp = cq.nlp().borrow();
1093
1094 // Full-length 0/1 indicators of finite lower / upper bounds,
1095 // built by scattering ones through the bound expansion matrices.
1096 let mut ones_l = nlp.x_l().make_new();
1097 ones_l.set(1.0);
1098 let mut has_lb = x.make_new();
1099 nlp.px_l().mult_vector(1.0, &*ones_l, 0.0, &mut *has_lb);
1100
1101 let mut ones_u = nlp.x_u().make_new();
1102 ones_u.set(1.0);
1103 let mut has_ub = x.make_new();
1104 nlp.px_u().mult_vector(1.0, &*ones_u, 0.0, &mut *has_ub);
1105
1106 let downcast = |v: &dyn Vector| -> Option<Vec<Number>> {
1107 v.as_any()
1108 .downcast_ref::<DenseVector>()
1109 .map(|d| d.expanded_values())
1110 };
1111
1112 // POUNCE is dense-only; if a backing is unexpectedly non-dense we
1113 // cannot prove the divergence is spurious, so fall back to the
1114 // original (magnitude-only) verdict to avoid changing behaviour.
1115 let (Some(xv), Some(lb), Some(ub)) = (downcast(x), downcast(&*has_lb), downcast(&*has_ub))
1116 else {
1117 return true;
1118 };
1119
1120 for i in 0..xv.len() {
1121 if xv[i].abs() > thresh {
1122 let free_to_diverge = if xv[i] > 0.0 {
1123 ub[i] == 0.0
1124 } else {
1125 lb[i] == 0.0
1126 };
1127 if free_to_diverge {
1128 return true;
1129 }
1130 }
1131 }
1132 false
1133 }
1134
1135 /// #285: checked recession-ray unboundedness proof at the current iterate.
1136 ///
1137 /// Returns `true` only when the current iterate `x` (with `|x|_∞ = amax`,
1138 /// already known `> RECESSION_MIN_NORM` by the caller) *proves* the
1139 /// problem is unbounded below via a genuine recession ray — the same
1140 /// standard the LP/symmetric path holds itself to, not a magnitude
1141 /// heuristic. All of the following must hold:
1142 ///
1143 /// 1. **Feasible witness.** The iterate's unscaled primal infeasibility is
1144 /// at most `RECESSION_FEAS_REL · amax`. A genuinely feasible iterate of
1145 /// norm `≥ 1e10` witnesses that the feasible region reaches that far —
1146 /// a *bounded* region cannot contain it. (Relative bar: the residual of
1147 /// `A_eq x − b` carries roundoff that scales with `|x|`.)
1148 /// 2. **Free to escape.** Some over-floor component heads toward a side
1149 /// with no finite variable bound ([`Self::free_to_escape_over`] at
1150 /// `RECESSION_MIN_NORM`).
1151 /// 3. **Direction in `null(A_eq)`.** `‖J_c x‖_∞ ≤ RECESSION_DIR_TOL · amax`
1152 /// — moving along `d ≈ x` preserves the equality constraints.
1153 /// 4. **Inequalities not blocking.** No finitely-bounded inequality row is
1154 /// driven toward its bound along `d ≈ x`
1155 /// ([`Self::recession_blocked_by_inequality`]).
1156 /// 5. **Objective descending.** `∇f·x ≤ −RECESSION_DESC_REL · ‖∇f‖ ‖x‖` —
1157 /// the objective strictly decreases along the escape direction with a
1158 /// real (non-orthogonal) margin, so `f → −∞` along the ray.
1159 ///
1160 /// On a *bounded* problem at least one of (1)/(2)/(3)/(4)/(5) fails, so
1161 /// this can never manufacture a spurious `DivergingIterates`.
1162 fn curr_is_recession_ray(&self, x: &dyn Vector, amax: Number) -> bool {
1163 // (1) Feasible witness (relative bar).
1164 let primal_inf = self.cq.borrow().curr_unscaled_primal_infeasibility_max();
1165 if !(primal_inf.is_finite() && primal_inf <= Self::RECESSION_FEAS_REL * amax) {
1166 return false;
1167 }
1168 // (2) Some over-floor component free to escape to infinity.
1169 if !self.free_to_escape_over(x, Self::RECESSION_MIN_NORM) {
1170 return false;
1171 }
1172 // (3) Escape direction lies in the equality null space. A non-finite
1173 // (NaN) residual is treated as failing, so the direction is only
1174 // accepted on a genuinely small, finite `‖J_c x‖∞`.
1175 let jc_x_amax = self.cq.borrow().curr_jac_c_times_vec(x).amax();
1176 if !jc_x_amax.is_finite() || jc_x_amax > Self::RECESSION_DIR_TOL * amax {
1177 return false;
1178 }
1179 // (4) No finitely-bounded inequality blocks the direction.
1180 if self.recession_blocked_by_inequality(x, amax) {
1181 return false;
1182 }
1183 // (5) Objective strictly descending along the escape direction.
1184 let (dot, gnorm, xnorm) = {
1185 let cq = self.cq.borrow();
1186 let g = cq.curr_grad_f();
1187 (g.dot(x), g.nrm2(), x.nrm2())
1188 };
1189 if !(dot < 0.0 && dot <= -Self::RECESSION_DESC_REL * gnorm * xnorm) {
1190 return false;
1191 }
1192 true
1193 }
1194
1195 /// #285: does any *finitely-bounded* inequality constraint block motion
1196 /// along the escape direction `d ≈ x`? For each inequality row the
1197 /// constraint value `d(x)` changes at rate `(J_d x)_j` per unit of the
1198 /// direction; if that row has a finite upper bound and the rate is
1199 /// positive (or a finite lower bound and the rate is negative) beyond a
1200 /// relative tolerance, moving out along `d` would eventually violate it,
1201 /// so it is not a feasible recession direction. Bounds are detected via
1202 /// the `Pd_L / Pd_U` expansion matrices exactly as the variable-bound
1203 /// check uses `Px_L / Px_U`.
1204 fn recession_blocked_by_inequality(&self, x: &dyn Vector, amax: Number) -> bool {
1205 use pounce_linalg::DenseVector;
1206
1207 let cq = self.cq.borrow();
1208 // Rate of change of each inequality value along d ≈ x (length m_ineq).
1209 // Compute first so the internal `nlp.borrow_mut()` is released before
1210 // the immutable borrow below.
1211 let jd_x = cq.curr_jac_d_times_vec(x);
1212 let (has_dlb, has_dub) = {
1213 let nlp = cq.nlp().borrow();
1214 let mut ones_dl = nlp.d_l().make_new();
1215 ones_dl.set(1.0);
1216 let mut has_dlb = jd_x.make_new();
1217 nlp.pd_l().mult_vector(1.0, &*ones_dl, 0.0, &mut *has_dlb);
1218
1219 let mut ones_du = nlp.d_u().make_new();
1220 ones_du.set(1.0);
1221 let mut has_dub = jd_x.make_new();
1222 nlp.pd_u().mult_vector(1.0, &*ones_du, 0.0, &mut *has_dub);
1223 // Order matters: bind `(has_dlb, has_dub)` in that exact order so
1224 // the finite-lower / finite-upper indicators are not transposed.
1225 // #314: this pair was returned swapped, inverting the bound
1226 // semantics below — a ray *increasing* a lower-bounded row (moving
1227 // deeper into the feasible set, slack growing) was wrongly treated
1228 // as blocked, so a genuine inequality-slack recession ray was never
1229 // proven unbounded.
1230 (has_dlb, has_dub)
1231 };
1232
1233 let downcast = |v: &dyn Vector| -> Option<Vec<Number>> {
1234 v.as_any()
1235 .downcast_ref::<DenseVector>()
1236 .map(|d| d.expanded_values())
1237 };
1238 // Dense-only fallback: if we cannot inspect the rows, conservatively
1239 // treat the direction as blocked (no spurious unbounded verdict).
1240 let (Some(jd), Some(dlb), Some(dub)) =
1241 (downcast(&*jd_x), downcast(&*has_dlb), downcast(&*has_dub))
1242 else {
1243 return true;
1244 };
1245 let tol = Self::RECESSION_DIR_TOL * amax;
1246 for j in 0..jd.len() {
1247 // Increasing a row that has a finite upper bound, or decreasing a
1248 // row that has a finite lower bound, would leave the feasible set.
1249 if (jd[j] > tol && dub[j] != 0.0) || (jd[j] < -tol && dlb[j] != 0.0) {
1250 return true;
1251 }
1252 }
1253 false
1254 }
1255
1256 /// #285: update the recession-ray persistence state and return whether
1257 /// `DivergingIterates` should be reported now. `amax` is `|x|_∞`;
1258 /// `is_ray` is the result of [`Self::curr_is_recession_ray`]. The verdict
1259 /// fires once the checked proof has held for
1260 /// [`Self::RECESSION_PERSIST_ITERS`] consecutive *growing* iterations — a
1261 /// bounded region cannot supply a growing sequence of feasible over-floor
1262 /// iterates, so this is impossible to satisfy on a bounded problem.
1263 fn update_recession_verdict(&mut self, amax: Number, is_ray: bool) -> bool {
1264 if !is_ray {
1265 self.recession_streak = 0;
1266 self.recession_prev_amax = 0.0;
1267 return false;
1268 }
1269 if amax > self.recession_prev_amax {
1270 self.recession_streak += 1;
1271 } else {
1272 // Proof holds but the iterate is not growing (a stalled or rejected
1273 // step). Restart the run at the current witness rather than firing
1274 // on a plateau; a genuine ray resumes growing next step.
1275 self.recession_streak = 1;
1276 }
1277 self.recession_prev_amax = amax;
1278 self.recession_streak >= Self::RECESSION_PERSIST_ITERS
1279 }
1280
1281 /// Honour a certificate the masked-scale veto refused, when the run that
1282 /// was allowed to continue did not end in one of its own (gh #200).
1283 ///
1284 /// The veto's bargain is "never worse off": it refuses a point that had
1285 /// *already passed the strict test*, betting that continuing reaches a
1286 /// better one. This is the losing side of that bet — so hand back exactly
1287 /// what would have been returned without the veto, point and status both.
1288 ///
1289 /// Two details make that guarantee real rather than approximate:
1290 ///
1291 /// - It runs on **every** non-success exit, applied once where the driver
1292 /// loop's result is finalized. Wiring individual termination sites was
1293 /// tried and is not safe: there are sixteen, and the ones easiest to
1294 /// overlook are the ones most likely to fire here — the veto's extra
1295 /// iterations are exactly what pushes a run past `max_cpu_time`.
1296 /// - It restores the **refused iterate itself** (`vetoed`), not the last
1297 /// acceptable snapshot. `store_acceptable_point` overwrites
1298 /// unconditionally, so after the veto the stored point drifts to whatever
1299 /// the continued run last touched — which may be worse than the point
1300 /// that was refused.
1301 ///
1302 /// "Better" is **status-dominant lexicographic**: the reported status first,
1303 /// and the objective only to break a tie *within equal status*. Both halves
1304 /// matter and the order between them is not cosmetic — see the `Success`
1305 /// branch, where reading it as a plain objective comparison costs a status.
1306 fn honour_refused_certificate(&mut self, result: SolverReturn) -> SolverReturn {
1307 if matches!(result, SolverReturn::Success) {
1308 // The continued run produced a certificate of its own — but not
1309 // necessarily a better *outcome*.
1310 //
1311 // This is what makes "never worse" hold even when the bet loses in a
1312 // way that still converges: on a non-convex problem the extra travel
1313 // can reach a different, worse stationary point, and the budget cap
1314 // (`VETO_MAX_EXTRA_ITERS`) can also hand back a late-but-converged
1315 // one. Neither may silently replace a better answer the solver
1316 // already had in hand.
1317 //
1318 // The comparison is NOT objective-only. That was the original bug
1319 // here: both points passed `passes_component_tols`, which looked
1320 // like a licence to treat them as equally valid certificates and
1321 // just take the lower objective. They are not equally valid when
1322 // `kkt_fidelity_tol` is set — `apply_kkt_fidelity_gate` re-grades a
1323 // `Success` on the unscaled KKT error afterwards, on a strictly
1324 // finer criterion than the convergence test. Taking a 3-ulp
1325 // objective win at a point whose unscaled error is 5x worse traded
1326 // `Solve_Succeeded` for `Solved_To_Acceptable_Level`: a status
1327 // regression against baseline, which is the strongest form of the
1328 // guarantee breaking. So rank by the status each point will actually
1329 // be *reported* under, and only then by objective.
1330 let Some((refused, refused_status)) = self.baseline_outcome() else {
1331 return result;
1332 };
1333 self.assert_comparable_scale(&refused);
1334 let (curr_f, curr_kkt) = self.curr_obj_and_unscaled_kkt();
1335 // Rank each candidate by the status it will actually be *reported*
1336 // under, which for a `Success` means after the fidelity gate has had
1337 // its say.
1338 let continued_success = self.survives_fidelity_gate(curr_kkt);
1339 let refused_success = matches!(refused_status, SolverReturn::Success)
1340 && self.survives_fidelity_gate(refused.unscaled_kkt);
1341 let keep_refused = match (continued_success, refused_success) {
1342 // Equal reported status: the objective breaks the tie, which is
1343 // legitimate because both points are feasible to tolerance.
1344 //
1345 // Negated `<=`, not `>`: they differ at NaN, and the difference
1346 // matters. A `Converged` exit at an iterate whose objective is
1347 // NaN but whose residuals are finite and tiny is reachable (the
1348 // convergence test never inspects `f`), and `NaN > x` is false,
1349 // which would keep the NaN point over a finite refused one.
1350 // Phrased as a negated `<=`, an incomparable objective fails to
1351 // justify keeping the continued point and the refused one wins.
1352 (true, true) | (false, false) => !(curr_f <= refused.obj),
1353 // The refused point keeps a status the continued one loses.
1354 (false, true) => true,
1355 (true, false) => false,
1356 };
1357 if !keep_refused {
1358 return result;
1359 }
1360 self.restore_snapshot(&refused);
1361 // The restored point's own status, which is what the baseline
1362 // reported for it. For a strict refusal that is `Success` even when
1363 // it fails the fidelity gate — the gate re-grades the restored point
1364 // downstream, exactly as it would have re-graded the baseline's. For
1365 // an acceptable-level refusal it is `StopAtAcceptablePoint`, since
1366 // claiming `Success` for a point that only ever qualified at the
1367 // acceptable level would over-report.
1368 return refused_status;
1369 }
1370 // The continued run did not certify — but its final point can still be a
1371 // *better* would-be certificate than the one the baseline stopped at, and
1372 // restoring the chronologically-first refusal unconditionally throws it
1373 // away (gh #327). The masking veto keeps refusing at the true optimum too
1374 // (its unscaled error stays above `acceptable_tol` under an extreme
1375 // objective scale), so a run that actually reaches the optimum never gets
1376 // to certify there and instead exits non-`Success` — typically on a tiny
1377 // step once it settles. Rolling straight back to the first refusal then
1378 // hands back the point the baseline stopped at, which can be far worse:
1379 // on `min 1/x` over `[1e-12, 10]` the solve reaches x≈10 (f≈0.1) but was
1380 // rolled back to the first refusal at x≈2.84 (f≈0.35) and reported
1381 // success there.
1382 //
1383 // The extra candidate is admitted *narrowly*, and the gate is
1384 // load-bearing: the continued point may displace the refused snapshot
1385 // only if it itself passes the strict per-component tolerances — i.e. it
1386 // is a would-be strict certificate the veto refused solely because of
1387 // masking. That is precisely what tells the settled true optimum apart
1388 // from a merely lower objective reached on an unbounded ray (e.g.
1389 // `A(x−a)⁴ − K·√(1+y²)`, unbounded below in y): the diverging iterate
1390 // never passes the strict test, so it can never win here, and those runs
1391 // stay bit-for-bit as before. When the gate does open, keep whichever
1392 // point ranks better under the same feasibility-aware key the dual-guard
1393 // fallback uses, and report the baseline's restored status either way —
1394 // never worse than baseline on status, never worse (often better) on the
1395 // point.
1396 let Some((refused, restored_status)) = self.baseline_outcome() else {
1397 return result;
1398 };
1399 self.assert_comparable_scale(&refused);
1400 let curr_nlp_err = self.cq.borrow().curr_nlp_error();
1401 let curr_passes_strict =
1402 self.bundle
1403 .conv_check
1404 .current_passes_strict(curr_nlp_err, &self.data, &self.cq);
1405 let curr_f = self.cq.borrow().curr_f();
1406 let curr_viol = self.cq.borrow().curr_unscaled_primal_infeasibility_max();
1407 // The second admissible candidate: a continued run that ends *at the
1408 // acceptable level itself* (gh #533). The `curr_passes_strict` gate
1409 // exists to tell a settled optimum from a diverging ray, and on this
1410 // exit the exit itself already answers that — `StopAtAcceptablePoint` is
1411 // only reachable at a point that passed the acceptable per-component
1412 // tolerances, either by qualifying here or by being the stashed
1413 // acceptable iterate a rollback restored. A diverging iterate cannot
1414 // produce it.
1415 //
1416 // This matters because the gh #533 progress refusal is frequently paid
1417 // off by a *better acceptable point* rather than by a strict
1418 // certificate: the streak refuses while the solve is still descending,
1419 // the solve descends, and then settles somewhere better but still short
1420 // of `tol`. Without this the refused point is restored and the entire
1421 // continuation is discarded — never worse than baseline, but never
1422 // better either, which for that whole class is pure cost.
1423 //
1424 // Gated on the *restored* status also being `StopAtAcceptablePoint`, so
1425 // a strict refusal's `Success` is never reported at a point that only
1426 // ever qualified at the acceptable level.
1427 let continued_is_acceptable_exit = matches!(result, SolverReturn::StopAtAcceptablePoint)
1428 && matches!(restored_status, SolverReturn::StopAtAcceptablePoint);
1429 // Keep the continued point in place only when it is an admissible
1430 // candidate that also ranks strictly better; otherwise restore the
1431 // refused snapshot exactly as before. `ranks_better` treats a non-finite
1432 // continued objective as worst, so a NaN-objective continuation never
1433 // displaces a finite refused point (the NaN-loses convention the
1434 // `Success` branch relies on).
1435 let keep_continued = (curr_passes_strict || continued_is_acceptable_exit)
1436 && self.ranks_better(curr_f, curr_viol, refused.obj, refused.constr_viol);
1437 if !keep_continued {
1438 self.restore_snapshot(&refused);
1439 }
1440 if self.cq.borrow().curr_f().is_finite() {
1441 restored_status
1442 } else {
1443 result
1444 }
1445 }
1446
1447 /// What the baseline — the same solve with the veto disabled — would have
1448 /// returned, as (point, status), or `None` if nothing was ever refused.
1449 ///
1450 /// The **chronologically first** refusal, not the strictest one. Both arms
1451 /// follow the same trajectory until the first refusal, so that iterate is
1452 /// where the baseline stopped and what it reported. A refusal recorded later
1453 /// sits on the continued trajectory, which the baseline never walked — its
1454 /// point was never on offer, and restoring it would neither reproduce the
1455 /// baseline nor be comparable to it.
1456 ///
1457 /// Both kinds do occur, and in either order: an acceptable-level refusal
1458 /// needs `acceptable_iter` consecutive qualifying iterates, so a strict
1459 /// refusal can precede it, while a run that first drifts through the
1460 /// acceptable band can refuse there and only later pass the strict test.
1461 /// Preferring `Success` unconditionally was wrong for exactly the second
1462 /// case — it compared against a strict point from iteration 50-odd when the
1463 /// baseline had already stopped and reported acceptable at iteration 43.
1464 fn baseline_outcome(&self) -> Option<(VetoSnapshot, SolverReturn)> {
1465 // A refusal that was seen but not captured makes the baseline
1466 // unidentifiable, so decline rather than guess. Without this, a failed
1467 // strict capture alongside a successful acceptable one would present the
1468 // acceptable snapshot as the baseline outcome — but that snapshot sits
1469 // on the continued trajectory, so this would silently reintroduce the
1470 // very misidentification the chronological rule exists to prevent.
1471 // Declining loses the restore; misidentifying reports a wrong point
1472 // under a confident status.
1473 //
1474 // Unreachable today (`data.curr` is always `Some` inside `iterate()`, so
1475 // `snapshot_current` cannot fail), but the latches make the state
1476 // representable, and it must not be handled by accident.
1477 if (self.vetoed_seen && self.vetoed.is_none())
1478 || (self.vetoed_acceptable_seen && self.vetoed_acceptable.is_none())
1479 {
1480 return None;
1481 }
1482 match (&self.vetoed, &self.vetoed_acceptable) {
1483 // Ties go to the strict refusal, and the tie is reachable: both can
1484 // arm in the same call when the acceptable streak crosses on the
1485 // same iterate a strict certificate is refused. Strict is correct
1486 // there because of the baseline's own branch order — the `Converged`
1487 // gate (`opt_error.rs`, in `check_convergence_with_state`) precedes
1488 // `note_acceptable`, so the baseline returned `Converged` at that
1489 // iterate. Reordering those two branches would invert this.
1490 (Some(strict), Some(acc)) => Some(if strict.iter <= acc.iter {
1491 (strict.clone(), SolverReturn::Success)
1492 } else {
1493 (acc.clone(), SolverReturn::StopAtAcceptablePoint)
1494 }),
1495 (Some(strict), None) => Some((strict.clone(), SolverReturn::Success)),
1496 (None, Some(acc)) => Some((acc.clone(), SolverReturn::StopAtAcceptablePoint)),
1497 (None, None) => None,
1498 }
1499 }
1500
1501 /// Capture the current iterate as a veto snapshot, or `None` if there is no
1502 /// current iterate to capture.
1503 ///
1504 /// All-or-nothing by construction — see [`VetoSnapshot`].
1505 fn snapshot_current(&self, iter: Index) -> Option<VetoSnapshot> {
1506 let iterate = self.data.borrow().curr.as_ref().cloned()?;
1507 let cq = self.cq.borrow();
1508 Some(VetoSnapshot {
1509 iterate,
1510 iter,
1511 obj: cq.curr_f(),
1512 mu: self.data.borrow().curr_mu,
1513 unscaled_kkt: cq.curr_unscaled_nlp_error(),
1514 constr_viol: cq.curr_unscaled_primal_infeasibility_max(),
1515 obj_scale: cq.obj_scaling_factor(),
1516 })
1517 }
1518
1519 /// Current objective and max-norm unscaled KKT error, read together so the
1520 /// pair cannot describe different iterates.
1521 fn curr_obj_and_unscaled_kkt(&self) -> (Number, Number) {
1522 let cq = self.cq.borrow();
1523 (cq.curr_f(), cq.curr_unscaled_nlp_error())
1524 }
1525
1526 /// Guard the precondition of every scaled-objective comparison in
1527 /// [`Self::honour_refused_certificate`]: the factor must not have moved
1528 /// between the refusal and now, or the two numbers are not comparable.
1529 fn assert_comparable_scale(&self, snap: &VetoSnapshot) {
1530 debug_assert_eq!(
1531 snap.obj_scale,
1532 self.cq.borrow().obj_scaling_factor(),
1533 "objective scaling factor moved during the solve; the refused and \
1534 continued objectives are scaled differently and cannot be compared \
1535 (gh #200)"
1536 );
1537 }
1538
1539 /// Whether a point with this unscaled KKT error would keep `Solve_Succeeded`
1540 /// through [`IpoptApplication::apply_kkt_fidelity_gate`].
1541 ///
1542 /// Mirrors that gate rather than approximating it: same quantity
1543 /// (`final_unscaled_kkt_error`), same strict comparison, same "non-positive
1544 /// tolerance disables". With the default `kkt_fidelity_tol = 0` this is
1545 /// always `true`, so every caller collapses to the plain objective
1546 /// comparison and the mechanism's behaviour is unchanged.
1547 fn survives_fidelity_gate(&self, unscaled_kkt: Number) -> bool {
1548 // Phrased as the negation of the gate's own `> tol` test rather than as
1549 // `<= tol`, because the two disagree at NaN and the gate is the
1550 // authority: it demotes only on `> tol`, so a NaN error keeps `Success`
1551 // there and must keep it here. Written as `<= tol` this mirror said the
1552 // opposite, which would rank a NaN-error continued point below a refused
1553 // one. Benign in that direction — it restores the baseline point — but a
1554 // mirror that disagrees with the thing it mirrors is a latent trap.
1555 !(self.kkt_fidelity_tol > 0.0) || !(unscaled_kkt > self.kkt_fidelity_tol)
1556 }
1557
1558 /// Record the current iterate as the best acceptable-quality point seen so
1559 /// far in this solve (pounce#250 follow-up).
1560 ///
1561 /// Recording runs on **every** acceptable iterate, not only after the
1562 /// dual-divergence guard has fired. Gating it on the guard was the first
1563 /// attempt and left a hole: the guard fires and returns to the driver
1564 /// *before* this site is reached on that iteration (see the guard block in
1565 /// [`Self::iterate`]), so nothing at or before the diversion was ever
1566 /// captured. A diversion that wrecks the solve immediately — reaching no
1567 /// acceptable point afterwards — therefore had nothing to hand back, which
1568 /// is precisely the case the fallback exists for. `autocorr_bern55-06` hid
1569 /// this, because its better point happens to arrive at iteration 86, well
1570 /// after the guard fires at 23.
1571 ///
1572 /// Recording always is still behaviour-neutral, because the record is only
1573 /// ever *read* under `dual_guard_fired` — see
1574 /// [`Self::honour_best_acceptable_after_dual_guard`]. A solve the guard
1575 /// never touches computes a comparison per acceptable iterate and nothing
1576 /// else.
1577 ///
1578 /// The cost is one `f64` comparison per acceptable iterate; the iterate is
1579 /// cloned only on an actual improvement, so this does not double the
1580 /// per-iteration clone `store_acceptable_point` already pays.
1581 ///
1582 /// "Best" is a feasibility-aware ranking, **not** the lowest objective:
1583 /// candidates are ordered by [`Self::ranks_better`]'s `(feasible_enough,
1584 /// objective)` key, so objective only decides among points already inside a
1585 /// capped feasibility band. Being *bounded* by `acceptable_constr_viol_tol`
1586 /// is not the same as *not trading* feasibility within it — that band is a
1587 /// user option and can be widened to `1e1` or beyond. A pure-objective argmax
1588 /// over it has no lower bound on the feasibility it will spend, and one
1589 /// option-value away it returns a point `pounce verify` rejects under a
1590 /// `Solved_To_Acceptable_Level` status (gh #267). Whether an early
1591 /// low-objective iterate is even a candidate is the user's
1592 /// `acceptable_constr_viol_tol`; the capped feasibility key is what keeps a
1593 /// grossly-infeasible one from winning even when the band admits it.
1594 fn record_best_acceptable(&mut self, curr_f: Number) {
1595 if !curr_f.is_finite() {
1596 return;
1597 }
1598 // Same quantity the acceptable-point gate keys on, so the recorded
1599 // feasibility matches the band the candidate just passed.
1600 let curr_viol = self.cq.borrow().curr_unscaled_primal_infeasibility_max();
1601 // Reject before cloning: only a strictly better candidate — by the
1602 // feasibility-aware key, not objective alone — is worth a snapshot.
1603 if let Some(best) = self.best_acceptable.as_ref() {
1604 let (b_obj, b_viol) = (best.obj, best.constr_viol);
1605 if !self.ranks_better(curr_f, curr_viol, b_obj, b_viol) {
1606 return;
1607 }
1608 }
1609 let iter = self.data.borrow().iter_count;
1610 let Some(snap) = self.snapshot_current(iter) else {
1611 return;
1612 };
1613 // Scaled objectives are only comparable under an unchanged factor; if it
1614 // ever moved, keep the earlier point rather than compare noise.
1615 if let Some(best) = self.best_acceptable.as_ref() {
1616 if snap.obj_scale != best.obj_scale {
1617 return;
1618 }
1619 }
1620 self.best_acceptable = Some(snap);
1621 }
1622
1623 /// Cap on the feasibility band [`Self::ranks_better`] admits, matching the
1624 /// upstream default `acceptable_constr_viol_tol`. The fallback treats a point
1625 /// as "feasible enough to win on objective" only within this band, *however
1626 /// loose the user made `acceptable_constr_viol_tol`*, so widening that option
1627 /// cannot let the fallback trade feasibility for objective (gh #267).
1628 const FEASIBLE_ENOUGH_CAP: Number = 1e-2;
1629
1630 /// Whether candidate `(a_obj, a_viol)` ranks strictly better than
1631 /// `(b_obj, b_viol)` for the best-acceptable fallback (gh #267, gh #280).
1632 ///
1633 /// The key is `(band_clamped_viol, objective)` compared lexicographically,
1634 /// where each violation is clamped *up* to
1635 /// `band = min(acceptable_constr_viol_tol, FEASIBLE_ENOUGH_CAP)` before it is
1636 /// compared. Inside the band every point clamps to `band`, so they tie on
1637 /// feasibility and objective decides — objective still rules *only among
1638 /// points already feasible-enough*. Outside the band the actual violation
1639 /// decides, so the less-infeasible point always wins and a
1640 /// strictly-more-infeasible point can never rank better (gh #280 — the
1641 /// earlier `feasible_enough` partition fell through to objective-only once
1642 /// both points were outside the band). The cap keeps the band no looser than
1643 /// the upstream default: `acceptable_constr_viol_tol` is user-widenable, and
1644 /// admitting a wide band into the *objective-decides* region would let a
1645 /// grossly-infeasible low-objective iterate win. Capping the band bounds that.
1646 ///
1647 /// At default (or tighter) tolerances this is behaviour-neutral: every
1648 /// recorded point already passed the `acceptable_constr_viol_tol` gate, so
1649 /// with that band at or below the cap every candidate clamps to `band` and
1650 /// objective alone decides, exactly as before. The feasibility ordering only
1651 /// bites once the user loosens `acceptable_constr_viol_tol` past its default
1652 /// and two candidates both sit outside the cap.
1653 ///
1654 /// A non-finite objective ranks worst and can never win — feasibility never
1655 /// rescues a `NaN`/`Inf` `f`. This mirrors the `NaN`-loses convention the
1656 /// gh #200 comparisons already rely on, and it keeps a `NaN`-objective
1657 /// returned point losing to a finite recorded one in
1658 /// [`Self::honour_best_acceptable_after_dual_guard`].
1659 ///
1660 /// The ranking itself lives in the pure [`ranks_better_within_band`] so its
1661 /// never-worse-off guarantee can be proven by deterministic unit tests rather
1662 /// than inferred from a host-dependent end-to-end objective comparison (see
1663 /// gh #267, which flagged an earlier CLI test for measuring the wrong,
1664 /// host-varying property). This method only resolves the admitted band.
1665 fn ranks_better(&self, a_obj: Number, a_viol: Number, b_obj: Number, b_viol: Number) -> bool {
1666 let band = self
1667 .bundle
1668 .conv_check
1669 .acceptable_constr_viol_tol_or_default()
1670 .min(Self::FEASIBLE_ENOUGH_CAP);
1671 ranks_better_within_band(a_obj, a_viol, b_obj, b_viol, band)
1672 }
1673
1674 /// Make the dual-divergence guard's diversion non-destructive (pounce#250
1675 /// follow-up).
1676 ///
1677 /// The guard bets that routing to restoration beats grinding on, and nothing
1678 /// made losing that bet safe: it could return a materially worse point than
1679 /// the solve already had, under a status that does not admit it.
1680 ///
1681 /// WHAT THIS DOES AND DOES NOT GUARANTEE. It guarantees the diverted run
1682 /// never returns worse than the best acceptable-quality point **that same
1683 /// run visited**. It does *not* guarantee the diverted run is no worse than
1684 /// not diverting at all — that counterfactual solve never happened, and its
1685 /// points were never on offer to compare against. The distinction is not
1686 /// academic: on the Linux CI host `deb7` returns 97.56 with the guard off and
1687 /// 127.87 with it on at streak 15, and this fallback cannot close that gap,
1688 /// because 127.87 is the best acceptable point the diverted run ever reached.
1689 /// Bounding the diversion's damage is a weaker property than making the
1690 /// diversion harmless, and only the weaker one is available from inside a
1691 /// single solve. It is a large part of why the guard is off by default.
1692 ///
1693 /// The observed case is `autocorr_bern55-06`. The guard fires at iteration
1694 /// 23, the diverted run reaches the true optimum (-2304.0000278, matching
1695 /// Ipopt to 12 significant figures) and holds it from iteration 57 to 86 —
1696 /// but the dual residual sawtooths between 1e-8 and 2e-1 there, so it never
1697 /// strings together the `acceptable_iter` consecutive qualifying iterates
1698 /// that would stop the solve. It then enters restoration a second time,
1699 /// wanders into a worse basin, and terminates `StopAtAcceptablePoint` at
1700 /// -2263.46 — 1.8 % worse, with an overall NLP error of 1.0. The better
1701 /// point was *visited and passed the acceptable test*; it was simply
1702 /// overwritten, because `store_acceptable_point` keeps the latest rather
1703 /// than the best.
1704 ///
1705 /// So: on a non-`Success` exit, if the best acceptable-quality iterate seen
1706 /// anywhere in the solve beats the point being returned, hand that back
1707 /// instead. This is the same "never worse off" bargain the gh #200 veto
1708 /// makes, applied to the other bet in the algorithm.
1709 ///
1710 /// "Beats" is the feasibility-aware ranking in [`Self::ranks_better`], not a
1711 /// bare objective comparison: the recorded point wins only if it is
1712 /// feasible-enough while the returned point is not, or both are in the same
1713 /// feasibility class and it has a lower objective. Ranking by objective alone
1714 /// let a widened `acceptable_constr_viol_tol` band trade feasibility for
1715 /// objective here — restoring a lower-objective point that `pounce verify`
1716 /// rejects, under a success-mapped status (gh #267). The key prevents that:
1717 /// objective can only win among points already inside the capped acceptable
1718 /// feasibility band.
1719 ///
1720 /// Note "anywhere in the solve", not "since the guard fired":
1721 /// [`Self::record_best_acceptable`] runs unconditionally and explains why —
1722 /// points at or before the diversion have to be on offer, or a diversion that
1723 /// wrecks the solve immediately has nothing to hand back. Only this *read* is
1724 /// gated on `dual_guard_fired`.
1725 ///
1726 /// A strict `Success` is never overridden — that point carries a real
1727 /// certificate, and a lower objective at a merely-acceptable point must not
1728 /// displace it.
1729 ///
1730 /// Tuning the guard's firing threshold was tried first and rejected: no
1731 /// setting separates the models it helps from the ones it harms, and the
1732 /// effect turned out to differ by host anyway (see the option help in
1733 /// `upstream_options.rs`). Fixing the consequence is what remained available.
1734 fn honour_best_acceptable_after_dual_guard(&mut self, result: SolverReturn) -> SolverReturn {
1735 if !self.dual_guard_fired || matches!(result, SolverReturn::Success) {
1736 return result;
1737 }
1738 let Some(best) = self.best_acceptable.clone() else {
1739 return result;
1740 };
1741 let (curr_f, _) = self.curr_obj_and_unscaled_kkt();
1742 let curr_viol = self.cq.borrow().curr_unscaled_primal_infeasibility_max();
1743 let curr_scale = self.cq.borrow().obj_scaling_factor();
1744 // Only comparable under the same factor, sign included.
1745 if curr_scale != best.obj_scale {
1746 return result;
1747 }
1748 // Restore only when the recorded point ranks strictly better under the
1749 // feasibility-aware key — more feasible, or equally feasible at a lower
1750 // objective. `ranks_better` also handles the `NaN` case the previous
1751 // bare `!(curr_f <= best.obj)` did: a non-finite returned objective
1752 // ranks worst, so a finite recorded point wins and is restored.
1753 if self.ranks_better(best.obj, best.constr_viol, curr_f, curr_viol) {
1754 tracing::debug!(target: "pounce::algorithm",
1755 "[POUNCE] dual-divergence diversion ended worse than a point already \
1756 in hand (obj {:.10e} viol {:.3e} -> obj {:.10e} viol {:.3e}, iter {}); \
1757 restoring it (pounce#250, gh#267).",
1758 curr_f, curr_viol, best.obj, best.constr_viol, best.iter,
1759 );
1760 self.restore_snapshot(&best);
1761 // Swap the *point*, but never let the swap erase why the solve
1762 // stopped. A budget that was exhausted stays reported as exhausted:
1763 // a caller polling for "did I run out of time" must not be told
1764 // "solved to acceptable level" merely because a better point was
1765 // recoverable. Only the outcomes that carry no such fact of their
1766 // own are relabelled to describe what is now being returned.
1767 return match result {
1768 SolverReturn::MaxiterExceeded
1769 | SolverReturn::CpuTimeExceeded
1770 | SolverReturn::WallTimeExceeded
1771 | SolverReturn::UserRequestedStop => result,
1772 _ => SolverReturn::StopAtAcceptablePoint,
1773 };
1774 }
1775 result
1776 }
1777
1778 /// Make a refused snapshot the current iterate again.
1779 fn restore_snapshot(&mut self, snap: &VetoSnapshot) {
1780 let mut d = self.data.borrow_mut();
1781 d.set_trial(snap.iterate.clone());
1782 d.accept_trial_point();
1783 // The restored point's own barrier parameter, not the continued run's —
1784 // see `VetoSnapshot::mu`.
1785 d.curr_mu = snap.mu;
1786 }
1787
1788 /// Decide whether the acceptable-point restoration decline may be deferred
1789 /// this once (gh #534), and arm the bookkeeping that bounds the bet.
1790 ///
1791 /// Four conditions, all required:
1792 ///
1793 /// * the option leaves deferrals available at all
1794 /// (`resto_decline_deferrals`, `0` = pre-#534 behaviour);
1795 /// * the budget is not already spent;
1796 /// * the NLP error has contracted on every one of the last
1797 /// [`DECLINE_PROGRESS_SAMPLES`]` - 1` iterations
1798 /// ([`Self::nlp_err_contracting`]) — the progress test the guard lacked;
1799 /// * the iteration budget has room for a continuation, and the entry point
1800 /// can actually be captured. Without a floor there is nothing to fall
1801 /// back to, and a bet with no floor is exactly what must not be placed.
1802 ///
1803 /// The deadline is clamped below `max_iter` so a lost bet can never turn a
1804 /// reportable `StopAtAcceptablePoint` into `Maximum_Iterations_Exceeded`:
1805 /// the continuation is always cut before the iteration budget runs out. A
1806 /// *time* budget is not clamped the same way — elapsed time is an external
1807 /// fact and the deadline cannot predict it — so a solve that expires inside
1808 /// the continuation window still reports the time limit, at the floor
1809 /// iterate rather than at whatever the continuation last touched.
1810 fn may_defer_acceptable_decline(&mut self) -> bool {
1811 if self.decline_deferrals_used >= self.resto_decline_deferrals {
1812 return false;
1813 }
1814 if !self.nlp_err_contracting() {
1815 return false;
1816 }
1817 let iter = self.data.borrow().iter_count;
1818 // No room to continue: the deadline below would fire on the very next
1819 // iteration, so the deferral would buy nothing and cost a restoration.
1820 if iter.saturating_add(1) >= self.max_iter {
1821 return false;
1822 }
1823 if self.decline_floor.is_none() {
1824 let Some(snap) = self.snapshot_current(iter) else {
1825 return false;
1826 };
1827 self.decline_floor = Some(snap);
1828 }
1829 self.decline_deferrals_used += 1;
1830 self.decline_deadline_iter = Some(
1831 iter.saturating_add(DECLINE_CONTINUATION_BUDGET)
1832 .min(self.max_iter.saturating_sub(1)),
1833 );
1834 true
1835 }
1836
1837 /// The deferred continuation ran out of budget without a strict certificate
1838 /// (gh #534). Report the floor — the point the pre-#534 guard would have
1839 /// returned — unless the continuation is standing somewhere at least as
1840 /// good.
1841 fn terminate_at_decline_floor(&mut self) -> IterateOutcome {
1842 let Some(floor) = self.decline_floor.clone() else {
1843 // Unreachable in practice: the deadline is only ever set after a
1844 // floor is captured. Stopping at the current point is still the
1845 // right thing if it somehow is not — the point passed the
1846 // acceptable-level triplet when the deferral was taken.
1847 return IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint);
1848 };
1849 tracing::debug!(target: "pounce::algorithm",
1850 "[POUNCE] deferred restoration decline expired at iter {} without a strict \
1851 certificate; falling back to the floor from iter {} (gh #534).",
1852 self.data.borrow().iter_count, floor.iter,
1853 );
1854 if !self.continuation_outranks(&floor) {
1855 self.restore_snapshot(&floor);
1856 }
1857 IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint)
1858 }
1859
1860 /// Whether the current iterate is a *better* answer than the gh #534 floor.
1861 ///
1862 /// Two gates, in order. The current point must itself pass the
1863 /// acceptable-level triplet — the floor is going to be reported under
1864 /// `Solved_To_Acceptable_Level`, and a continuation that wandered off is not
1865 /// entitled to that status however attractive its objective looks. Only then
1866 /// does [`Self::ranks_better`]'s feasibility-first key decide, and only under
1867 /// an unmoved objective scaling factor, since the two objectives are
1868 /// otherwise not comparable.
1869 fn continuation_outranks(&self, floor: &VetoSnapshot) -> bool {
1870 let (curr_f, _) = self.curr_obj_and_unscaled_kkt();
1871 if !curr_f.is_finite() {
1872 return false;
1873 }
1874 let nlp_err = self.cq.borrow().curr_nlp_error();
1875 if !self
1876 .bundle
1877 .conv_check
1878 .current_is_acceptable_with_state(nlp_err, &self.data, &self.cq)
1879 {
1880 return false;
1881 }
1882 let (curr_viol, curr_scale) = {
1883 let cq = self.cq.borrow();
1884 (
1885 cq.curr_unscaled_primal_infeasibility_max(),
1886 cq.obj_scaling_factor(),
1887 )
1888 };
1889 if curr_scale != floor.obj_scale {
1890 return false;
1891 }
1892 !self.ranks_better(floor.obj, floor.constr_viol, curr_f, curr_viol)
1893 }
1894 /// Try to leave a first-order-stationary point that is not a local minimum
1895 /// (gh #797). Returns `true` when the current iterate has been replaced and
1896 /// the solve should continue instead of terminating.
1897 ///
1898 /// # What this is for
1899 ///
1900 /// The convergence check is a *first-order* test, and on a nonconvex model
1901 /// that is strictly weaker than "local minimum". `nonconvex_qp.nl` is the
1902 /// reported case: `min x₀x₁ s.t. x₀ + x₁ = 2, 0 ≤ x ≤ 4` restricted to its
1903 /// feasible segment is the concave `f(x₀) = x₀(2 - x₀)`, *maximized* at
1904 /// `(1,1)` and minimized at the two endpoints. From the bound-pushed start
1905 /// `(0.01, 0.01)` the first Newton step lands exactly on `(1,1)`, every KKT
1906 /// residual there is zero, and the solve reports `Solve_Succeeded` at
1907 /// `obj = 1` — the constrained maximum.
1908 ///
1909 /// Inertia correction does not save this. It engages (the iteration log
1910 /// shows `lg(rg)` from the second iteration on) and cannot help: `δ_x I` is
1911 /// symmetric, the model and the iterate are symmetric under `x₀ ↔ x₁`, and
1912 /// a symmetric correction applied to a zero gradient gives a zero step
1913 /// however indefinite the reduced Hessian is. The regularization makes the
1914 /// *step* well-posed; nothing in the algorithm asks whether the point it
1915 /// has converged to is a minimum.
1916 ///
1917 /// # What it does
1918 ///
1919 /// [`PdFullSpaceSolver::negative_curvature_direction`] answers that
1920 /// question with the KKT factorization already in hand, and hands back a
1921 /// measured direction `d` with `J_c d_x = 0` and `dᵀ(W + Σ)d < 0`. Since
1922 /// the gradient is (near) zero the barrier objective along `±d` is
1923 /// `φ(α) ≈ φ(0) + ½α²dᵀ(W + Σ)d`, decreasing on *both* sides, so both signs
1924 /// are tried and the better trial wins. The step is capped by the ordinary
1925 /// fraction-to-the-boundary rule and by [`NEG_CURV_MAX_STEP_FACTOR`] times
1926 /// the iterate's own scale, then backtracked until it satisfies that
1927 /// second-order decrease model with an Armijo factor — the same shape as
1928 /// the line search, on the curvature term rather than the gradient term.
1929 /// A trial whose constraint violation exceeds what the convergence check
1930 /// itself calls feasible is refused outright.
1931 ///
1932 /// # Why it cannot make an answer worse
1933 ///
1934 /// The point being left is a *strict certificate* — the solve was about to
1935 /// report `Solve_Succeeded` at it — so it is snapshotted as a floor before
1936 /// the step, exactly as gh #534's deferred restoration decline does with
1937 /// the point its guard would have returned. The continuation gets
1938 /// [`NEG_CURV_CONTINUATION_BUDGET`] outer iterations; past that, and at
1939 /// every other exit of the driver loop, [`Self::honour_neg_curv_floor`]
1940 /// hands the floor back unless the continuation is standing somewhere that
1941 /// both outranks it and carries a certificate of its own. So the escape
1942 /// costs a bounded number of iterations and can only trade the stationary
1943 /// point for a strictly better one.
1944 ///
1945 /// That holds however many escapes are spent, not only at the default of
1946 /// one: a later escape displaces the floor only with a certificate that
1947 /// outranks the one already held (gh #805), so the floor is always at
1948 /// least as good as the point a `neg_curv_escapes = 0` build reports.
1949 fn try_neg_curv_escape(&mut self, iter_count: Index) -> bool {
1950 if self.neg_curv_escapes_used >= self.neg_curv_escapes {
1951 return false;
1952 }
1953 // No room to continue: the deadline below would fire on the very next
1954 // iteration, so the escape would buy nothing and cost a factorization.
1955 if iter_count.saturating_add(1) >= self.max_iter {
1956 return false;
1957 }
1958 if self.nlp.is_none() || self.search_dir.is_none() {
1959 return false;
1960 }
1961 // `data.w` still holds `W(curr_{N-1})` — step 3 of `iterate()` runs
1962 // *after* the convergence check — and curvature at the previous iterate
1963 // is not the question being asked. Re-evaluate it here rather than
1964 // running the Hessian updater a second time at this iterate: that would
1965 // hand the limited-memory updater a zero-length curvature pair to skip
1966 // and count against `limited_memory_max_skipping`, and it would leave
1967 // `data.w` describing a different iterate than it did before, which the
1968 // post-optimal sensitivity hook reads.
1969 //
1970 // With a quasi-Newton `B` there is nothing to re-evaluate and the stale
1971 // one is used. That is not a gap being papered over: BFGS maintains `B`
1972 // positive definite by construction, so under
1973 // `hessian_approximation=limited-memory` the probe's inertia test
1974 // passes at δ_x = 0 and the escape declines — correctly, since the only
1975 // curvature information the solve has says the point is a minimum.
1976 //
1977 // That argument is about BFGS's definiteness, NOT about "not exact",
1978 // and gating on `provides_exact_hessian` conflated the two. A
1979 // finite-difference `W` is not exact but does carry genuine negative
1980 // curvature, so judging the current iterate by the previous one's
1981 // matrix let a stationary maximum be reported as optimal where the
1982 // exact path escapes it (gh#823 review, finding 1, @srikanth-gm).
1983 // `hessian_at_current` asks the question the probe actually has:
1984 // can you give me `W` here? Quasi-Newton updaters still answer `None`
1985 // and still take the stale path, for the reason above.
1986 let w_at_curr = self.bundle.hess.hessian_at_current(&self.data, &self.cq);
1987
1988 let probe = {
1989 let (Some(nlp), Some(sd)) = (self.nlp.as_ref(), self.search_dir.as_mut()) else {
1990 return false;
1991 };
1992 let mut pd = sd.pd_solver_mut();
1993 pd.negative_curvature_direction(&self.data, &self.cq, nlp, w_at_curr)
1994 };
1995 let Some(probe) = probe else {
1996 return false;
1997 };
1998
1999 let Some(floor) = self.snapshot_current(iter_count) else {
2000 // Nothing to fall back to, and a bet with no floor is exactly what
2001 // must not be placed (the gh #534 rule, for the same reason).
2002 return false;
2003 };
2004 let curr = floor.iterate.clone();
2005 let (tau, curr_barr, curr_theta) = {
2006 let d = self.data.borrow();
2007 let cq = self.cq.borrow();
2008 (
2009 d.curr_tau,
2010 cq.curr_barrier_obj(),
2011 cq.curr_constraint_violation(),
2012 )
2013 };
2014 if !curr_barr.is_finite() {
2015 return false;
2016 }
2017 // A trial may raise the violation up to what this solve's own
2018 // convergence check calls feasible, and no further.
2019 let theta_cap = curr_theta.max(self.bundle.conv_check.constr_viol_tol_or_default());
2020 // The direction has unit infinity-norm, so this caps the escape at a
2021 // multiple of the iterate's own scale rather than at an absolute
2022 // distance, which would mean different things on differently scaled
2023 // models.
2024 let step_cap = NEG_CURV_MAX_STEP_FACTOR * (1.0 + curr.x.amax().max(curr.s.amax()));
2025
2026 let mut accepted: Option<(crate::iterates_vector::IteratesVector, Number, Number)> = None;
2027 for sign in [1.0, -1.0] {
2028 let mut dir = probe.delta.deep_copy();
2029 dir.scal(sign);
2030 let dir = dir.freeze();
2031 let alpha_max = self.cq.borrow().aff_step_alpha_primal_max(&dir, tau);
2032 if !(alpha_max > 0.0) {
2033 continue;
2034 }
2035 let mut alpha = alpha_max.min(step_cap);
2036 for _ in 0..NEG_CURV_BACKTRACKS {
2037 if !(alpha > 0.0) || !alpha.is_finite() {
2038 break;
2039 }
2040 let mut trial = curr.deep_copy();
2041 trial.x.axpy(alpha, &*dir.x);
2042 trial.s.axpy(alpha, &*dir.s);
2043 let trial = trial.freeze();
2044 self.data.borrow_mut().set_trial(trial.clone());
2045 let (barr, theta) = {
2046 let cq = self.cq.borrow();
2047 (cq.trial_barrier_obj(), cq.trial_constraint_violation())
2048 };
2049 self.data.borrow_mut().trial = None;
2050 // Second-order sufficient decrease. `probe.curvature` is
2051 // negative, so this asks the trial to realise at least
2052 // `NEG_CURV_ARMIJO` of the decrease the curvature model
2053 // predicts — the guard against a direction that is only
2054 // downhill in the quadratic model and uphill in the function.
2055 let predicted = 0.5 * alpha * alpha * probe.curvature;
2056 if barr.is_finite()
2057 && theta.is_finite()
2058 && theta <= theta_cap
2059 && barr <= curr_barr + NEG_CURV_ARMIJO * predicted
2060 {
2061 let better = match accepted.as_ref() {
2062 Some((_, best, _)) => barr < *best,
2063 None => true,
2064 };
2065 if better {
2066 accepted = Some((trial, barr, alpha));
2067 }
2068 break;
2069 }
2070 alpha *= NEG_CURV_BACKTRACK_FACTOR;
2071 }
2072 }
2073
2074 let Some((trial, barr, alpha)) = accepted else {
2075 return false;
2076 };
2077
2078 tracing::debug!(target: "pounce::algorithm",
2079 "[POUNCE] iter {}: certified point has negative reduced curvature \
2080 (dᵀ(W+Σ)d = {:.3e}); escaping along it with α = {:.3e} \
2081 (barrier obj {:.10e} -> {:.10e}) and continuing (gh#797).",
2082 iter_count, probe.curvature, alpha, curr_barr, barr,
2083 );
2084
2085 self.neg_curv_escapes_used += 1;
2086 // gh #805 — the floor is the *best* certificate the escapes have left,
2087 // never simply the most recent one. Replacing it unconditionally is
2088 // what broke the guarantee above at `neg_curv_escapes >= 2`: escape 1
2089 // floors at A, the continuation certifies a different indefinite point
2090 // B, escape 2 overwrites the floor with B, and if that bet is lost the
2091 // run reports B — which nothing has ever compared against A. With
2092 // `f(B) > f(A)` that is worse than `neg_curv_escapes = 0` returns, the
2093 // one outcome the mechanism promises cannot happen. The filter method's
2094 // barrier objective is not monotone across μ updates, so the escape's
2095 // own accounting does not exclude it.
2096 //
2097 // Ranked rather than merely kept (gh #805's suggested fix), because
2098 // keeping A unconditionally has the mirror-image flaw: where B *is* the
2099 // better certificate, a lost second bet would hand back A and make
2100 // `neg_curv_escapes = 2` worse than `= 1`, which returns B. Both points
2101 // are strict certificates — the escape only fires on
2102 // `ConvergenceStatus::Converged` — so the same status-dominant ranking
2103 // [`Self::honour_neg_curv_floor`] uses on the way out decides between
2104 // them, and the floor is monotone in the number of escapes spent.
2105 //
2106 // A provable no-op at the default: the branch is only reachable with a
2107 // floor already held, which takes a second escape, which takes
2108 // `neg_curv_escapes >= 2`.
2109 let replace_floor = match self.neg_curv_floor.as_ref() {
2110 None => true,
2111 Some(held) => self.current_outranks_neg_curv_floor(held, true),
2112 };
2113 if replace_floor {
2114 self.neg_curv_floor = Some(floor);
2115 }
2116 self.neg_curv_deadline_iter = Some(
2117 iter_count
2118 .saturating_add(NEG_CURV_CONTINUATION_BUDGET)
2119 .min(self.max_iter.saturating_sub(1)),
2120 );
2121 {
2122 let mut d = self.data.borrow_mut();
2123 d.set_trial(trial);
2124 d.accept_trial_point();
2125 // Iteration-log marker. `n` is not one of upstream's codes and is
2126 // unused elsewhere in POUNCE; it is what tells a reader of the
2127 // table that the jump between two iterates was an escape and not a
2128 // line-search step.
2129 d.append_info_string("n");
2130 }
2131 // The filter's entries were computed at, and around, a point the
2132 // algorithm has just left discontinuously — the same situation a
2133 // successful restoration leaves behind, and handled the same way.
2134 self.bundle.line_search.reset();
2135 self.bundle.line_search.reset_after_restoration();
2136 true
2137 }
2138
2139 /// Rank the current iterate against a held negative-curvature floor
2140 /// (gh #797, gh #805).
2141 ///
2142 /// Rank by the status each point will actually be *reported* under, and
2143 /// only then by objective — the status-dominant order gh #200 arrived at
2144 /// the hard way. `apply_kkt_fidelity_gate` re-grades a `Success` on the
2145 /// unscaled KKT error after the driver loop returns, so with
2146 /// `kkt_fidelity_tol` set a lower objective at a coarser point is a status
2147 /// regression dressed up as a win.
2148 ///
2149 /// `current_certifies` is whether the current point comes with a
2150 /// certificate of its own: at the exit hook that is the driver's status,
2151 /// and at an escape site it is true by construction, since the escape only
2152 /// fires on `ConvergenceStatus::Converged`.
2153 ///
2154 /// Shared by the two sites that have to answer this question — the exit
2155 /// hook [`Self::honour_neg_curv_floor`], and the point at which a second
2156 /// escape decides which of two certificates to hold (gh #805) — so the two
2157 /// cannot drift into disagreeing about which point is the better answer.
2158 fn current_outranks_neg_curv_floor(
2159 &self,
2160 floor: &VetoSnapshot,
2161 current_certifies: bool,
2162 ) -> bool {
2163 let (_, curr_kkt) = self.curr_obj_and_unscaled_kkt();
2164 let current_success = current_certifies && self.survives_fidelity_gate(curr_kkt);
2165 let floor_success = self.survives_fidelity_gate(floor.unscaled_kkt);
2166 match (current_success, floor_success) {
2167 (true, true) => self.continuation_outranks(floor),
2168 // The current point reports `Solve_Succeeded` where the floor would
2169 // be re-graded down. A better status wins outright.
2170 (true, false) => true,
2171 // No certificate of its own — the escape was a bet placed *from*
2172 // one, so anything short of that loses it.
2173 (false, _) => false,
2174 }
2175 }
2176
2177 /// Make a negative-curvature escape non-destructive (gh #797).
2178 ///
2179 /// The escape is a bet placed *from a strict certificate*, which is what
2180 /// makes its accounting stricter than gh #534's: the floor is a point the
2181 /// solve was about to report `Solve_Succeeded` at, so the continuation has
2182 /// to come back with a certificate of its own at a better point to be
2183 /// preferred. Anything else — a worse point, an acceptable-level stall, a
2184 /// spent iteration budget, a restoration failure — restores the floor and
2185 /// reports it under the status it always had.
2186 ///
2187 /// Restoring reports `Success` even over a budget exit, which
2188 /// is the opposite of what [`Self::honour_decline_floor`] and
2189 /// [`Self::honour_best_acceptable_after_dual_guard`] do — and deliberately
2190 /// so. Those two hold an *acceptable-level* point and must not let it erase
2191 /// why the solve stopped. Here a pre-#797 build returns `Solve_Succeeded`
2192 /// at this exact point, and returns it *before* the continuation that spent
2193 /// the budget ever runs: the budget was spent by the bet, not by the
2194 /// caller's problem. Reproducing the baseline outcome — point and status
2195 /// both — is the guarantee, and it is the reading gh #200's hook already
2196 /// takes of a refused certificate. A user stop is the one exception, and
2197 /// it is applied in [`Self::optimize`]: the floor's point is still
2198 /// restored, under `UserRequestedStop`, because the caller asked to stop
2199 /// and a `Success` would let a driver of several solves carry on.
2200 fn honour_neg_curv_floor(&mut self, result: SolverReturn) -> SolverReturn {
2201 let Some(floor) = self.neg_curv_floor.clone() else {
2202 return result;
2203 };
2204 self.assert_comparable_scale(&floor);
2205 let keep_continuation =
2206 self.current_outranks_neg_curv_floor(&floor, matches!(result, SolverReturn::Success));
2207 if keep_continuation {
2208 tracing::debug!(target: "pounce::algorithm",
2209 "[POUNCE] the negative-curvature escape paid off: the continuation \
2210 certified a better point than the stationary one it left \
2211 (obj {:.10e} -> {:.10e}, gh#797).",
2212 floor.obj, self.curr_obj_and_unscaled_kkt().0,
2213 );
2214 return result;
2215 }
2216 tracing::debug!(target: "pounce::algorithm",
2217 "[POUNCE] the negative-curvature escape did not pay off; restoring the \
2218 certified stationary point from iter {} (obj {:.10e} viol {:.3e}) \
2219 and reporting it (gh#797).",
2220 floor.iter, floor.obj, floor.constr_viol,
2221 );
2222 self.restore_snapshot(&floor);
2223 SolverReturn::Success
2224 }
2225
2226 /// The escape's continuation ran out of budget without a certificate of its
2227 /// own (gh #797). Restore the stationary point the escape left and report
2228 /// it — that point is what a pre-#797 build returns, and it is a genuine
2229 /// strict certificate, so `Success` is the honest status for it.
2230 ///
2231 /// Takes the floor rather than cloning it, so
2232 /// [`Self::honour_neg_curv_floor`] is a no-op on the way out.
2233 fn terminate_at_neg_curv_floor(&mut self) -> IterateOutcome {
2234 if let Some(floor) = self.neg_curv_floor.take() {
2235 tracing::debug!(target: "pounce::algorithm",
2236 "[POUNCE] the negative-curvature escape spent its {} iterations \
2237 without a new certificate; restoring the stationary point from \
2238 iter {} (obj {:.10e}) and reporting it (gh#797).",
2239 NEG_CURV_CONTINUATION_BUDGET, floor.iter, floor.obj,
2240 );
2241 self.restore_snapshot(&floor);
2242 }
2243 IterateOutcome::Terminate(SolverReturn::Success)
2244 }
2245
2246 /// Make a deferred restoration decline non-destructive (gh #534).
2247 ///
2248 /// The deferral is a bet that a contracting endgame is three iterations from
2249 /// a certificate. This is what makes losing it cost only those iterations:
2250 /// whatever the continued run ends up returning, if it is not at least as
2251 /// good an answer as the floor — the point the pre-#534 guard would have
2252 /// reported — the floor is restored and reported instead.
2253 ///
2254 /// Applied once, in [`Self::optimize`], for the same reason the gh #200 and
2255 /// pounce#250 hooks are: the driver loop has many `return`s and this must
2256 /// see all of them.
2257 ///
2258 /// A strict `Success` is never overridden — that is the bet paying off, and
2259 /// a real certificate outranks any acceptable-level point by construction.
2260 /// The budget statuses keep their own status, as they do in
2261 /// [`Self::honour_best_acceptable_after_dual_guard`]: a caller polling for
2262 /// "did I run out of time" must be told so, even while the point it gets
2263 /// back is swapped for the better one.
2264 fn honour_decline_floor(&mut self, result: SolverReturn) -> SolverReturn {
2265 if matches!(result, SolverReturn::Success) {
2266 if self.decline_floor.is_some() {
2267 tracing::debug!(target: "pounce::algorithm",
2268 "[POUNCE] the deferred restoration decline paid off: the continuation \
2269 reached a strict certificate (gh #534).",
2270 );
2271 }
2272 return result;
2273 }
2274 let Some(floor) = self.decline_floor.clone() else {
2275 return result;
2276 };
2277 if self.continuation_outranks(&floor) {
2278 return result;
2279 }
2280 tracing::debug!(target: "pounce::algorithm",
2281 "[POUNCE] the deferred restoration decline did not pay off; restoring the \
2282 floor from iter {} (obj {:.10e} viol {:.3e}) and reporting it (gh #534).",
2283 floor.iter, floor.obj, floor.constr_viol,
2284 );
2285 self.restore_snapshot(&floor);
2286 match result {
2287 SolverReturn::MaxiterExceeded
2288 | SolverReturn::CpuTimeExceeded
2289 | SolverReturn::WallTimeExceeded
2290 | SolverReturn::UserRequestedStop => result,
2291 _ => SolverReturn::StopAtAcceptablePoint,
2292 }
2293 }
2294
2295 /// Terminal fallback for a near-feasible numerical breakdown (a
2296 /// restoration cycle or a failed step computation). If a finite
2297 /// acceptable iterate was recorded earlier in the solve, roll back
2298 /// to it and stop at [`SolverReturn::StopAtAcceptablePoint`] (mapped
2299 /// by the application layer to `Solved_To_Acceptable_Level`) rather
2300 /// than surfacing the hard `fallback` error. This mirrors upstream
2301 /// `IpBacktrackingLineSearch`'s `ACCEPTABLE_POINT_REACHED`
2302 /// precedence: when the line search exhausts but an acceptable point
2303 /// was stored, that point is returned instead of the failure. With
2304 /// no snapshot — or if the restored objective is non-finite — the
2305 /// original `fallback` status is surfaced unchanged, so genuinely
2306 /// failed/infeasible solves keep their honest status. Catches
2307 /// degenerate LPs (kleemin8, nsir2) whose μ-endgame reaches the
2308 /// optimum, then destabilizes on the ill-conditioned vertex and
2309 /// cycles in restoration instead of stopping at the acceptable
2310 /// iterate it already passed through.
2311 fn terminate_acceptable_or(&mut self, fallback: SolverReturn) -> IterateOutcome {
2312 if self.restore_acceptable_point() && self.cq.borrow().curr_f().is_finite() {
2313 IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint)
2314 } else {
2315 IterateOutcome::Terminate(fallback)
2316 }
2317 }
2318
2319 /// The single place this module turns a local-infeasibility conclusion
2320 /// into a returned status (gh #505).
2321 ///
2322 /// Three separate routes reach that verdict — the conv-check's rapid
2323 /// detection, restoration layer 2, and the slow-cycle exits — and the same
2324 /// defect was found in two of them independently: returning the hard
2325 /// verdict without consulting the acceptable-point stash, so a solve that
2326 /// had already passed through an acceptable iterate discarded it. Only the
2327 /// cycle exits got it right, and nothing structural said the other two were
2328 /// wrong.
2329 ///
2330 /// That is the shape of a defect that comes back. The route a solve takes
2331 /// to the verdict is an internal detail — the user sees one status either
2332 /// way — so the *decision* about what that status means must not live at
2333 /// each route. It lives here, and
2334 /// [`no_route_concludes_local_infeasibility_alone`] is a tripwire against
2335 /// a new site rebuilding the outcome inline.
2336 ///
2337 /// The cycle exits are not routed through here because their fallback is
2338 /// chosen between `LocalInfeasibility` and `ErrorInStepComputation` at the
2339 /// call site; they already reach `terminate_acceptable_or`, which is the
2340 /// behaviour this guarantees.
2341 ///
2342 /// Scope: this governs how *this module* returns the verdict. Other layers
2343 /// name `SolverReturn::LocalInfeasibility` for their own reasons — the SQP
2344 /// status map and the ℓ₁ elastic path in `application.rs`, for instance —
2345 /// and are outside both this funnel and its tripwire.
2346 fn terminate_local_infeasibility(&mut self) -> IterateOutcome {
2347 self.terminate_acceptable_or(SolverReturn::LocalInfeasibility)
2348 }
2349
2350 pub fn with_nlp(mut self, nlp: Rc<RefCell<dyn IpoptNlp>>) -> Self {
2351 self.nlp = Some(nlp);
2352 self
2353 }
2354
2355 /// Install a user-facing TNLP handle. Enables per-iteration
2356 /// `TNLP::intermediate_callback` invocation from `optimize()`.
2357 pub fn with_tnlp(mut self, tnlp: Rc<RefCell<dyn TNLP>>) -> Self {
2358 self.tnlp = Some(tnlp);
2359 self
2360 }
2361
2362 /// Build an [`IterStats`] payload from the current `IpoptData` /
2363 /// `IpoptCq` state. Mirrors the field set the upstream Ipopt main
2364 /// loop hands to `IntermediateCallback` after each `AcceptTrialPoint`.
2365 fn build_iter_stats(&self) -> IterStats {
2366 let d = self.data.borrow();
2367 let c = self.cq.borrow();
2368 let dnrm = match d.delta.as_ref() {
2369 Some(delta) => delta.x.amax().max(delta.s.amax()),
2370 None => 0.0,
2371 };
2372 IterStats {
2373 // Regular from the outer loop; restoration from the inner
2374 // sub-IPM, which the outer driver flags at construction
2375 // (gh#645). The outer loop never sets the flag, so every
2376 // fire from here is still `RegularMode` — what changed is
2377 // that restoration now fires at all.
2378 mode: if self.fires_as_restoration {
2379 AlgorithmMode::RestorationPhaseMode
2380 } else {
2381 AlgorithmMode::RegularMode
2382 },
2383 iter: d.iter_count,
2384 obj_value: c.curr_f(),
2385 inf_pr: c.curr_primal_infeasibility_max(),
2386 inf_du: c.curr_dual_infeasibility_max(),
2387 mu: d.curr_mu,
2388 d_norm: dnrm,
2389 regularization_size: d.info_regu_x,
2390 alpha_du: d.info_alpha_dual,
2391 alpha_pr: d.info_alpha_primal,
2392 ls_trials: d.info_ls_count,
2393 }
2394 }
2395
2396 /// Fire `TNLP::intermediate_callback` if a TNLP handle and NLP
2397 /// handle are installed. Wraps the call in an [`IntermediateContext`]
2398 /// guard so downstream inspector entry points (the C API's
2399 /// `GetIpoptCurrent*`) can read live state for the duration. Returns
2400 /// `true` to continue, `false` if the user requested termination.
2401 fn fire_intermediate(&self) -> bool {
2402 let timing = self.data.borrow().timing.clone();
2403 let _guard = timing.fire_intermediate.guard();
2404 let Some(tnlp) = self.tnlp.as_ref() else {
2405 return true;
2406 };
2407 let Some(nlp) = self.nlp.as_ref() else {
2408 return true;
2409 };
2410 // Nothing reads the payload, so building it is the whole cost of the
2411 // fire (gh#965). The sink below is skipped with it, which is safe:
2412 // its only reader re-emits the row into this same callback.
2413 if !tnlp.borrow().wants_intermediate_callback() {
2414 return true;
2415 }
2416 let stats = self.build_iter_stats();
2417 // Record exactly what the callback is about to receive, so a losing
2418 // retry can re-emit the winning attempt's final row (pounce#870).
2419 if let Some(sink) = self.last_iter_stats_sink.as_ref() {
2420 *sink.borrow_mut() = Some(stats);
2421 }
2422 // The live-inspector context is for iterates of the *user's*
2423 // problem only. During restoration the iterate belongs to the
2424 // feasibility subproblem and is not even the same length, so no
2425 // context is installed and `GetIpoptCurrent*` reports no data
2426 // for the duration. See `fires_as_restoration`.
2427 let _guard = (!self.fires_as_restoration).then(|| {
2428 CtxGuard::install(IntermediateContext {
2429 data: Rc::clone(&self.data),
2430 cq: Rc::clone(&self.cq),
2431 nlp: Rc::clone(nlp),
2432 })
2433 });
2434 // The materialised user-space iterate (`with_current_user_iterate`,
2435 // what `GetIpoptCurrentIterate` reads), when the TNLP will read it.
2436 // Built here, before the callback borrows the TNLP, because the
2437 // fixed-variable multiplier recovery evaluates it.
2438 let wants_iterate = !self.fires_as_restoration && tnlp.borrow().wants_current_iterate();
2439 let _iterate_guard = if wants_iterate {
2440 crate::intermediate::ipm_user_iterate(
2441 nlp,
2442 &self.data,
2443 &self.cq,
2444 self.live_iterate_fixed_var_duals,
2445 )
2446 .map(crate::intermediate::UserIterateGuard::install)
2447 } else {
2448 None
2449 };
2450 tnlp.borrow_mut().intermediate_callback(
2451 stats,
2452 &TnlpIpoptData::default(),
2453 &TnlpIpoptCq::default(),
2454 )
2455 }
2456
2457 pub fn with_search_dir(mut self, sd: PdSearchDirCalc) -> Self {
2458 self.search_dir = Some(sd);
2459 self
2460 }
2461
2462 pub fn with_restoration(mut self, resto: Box<dyn RestorationPhase>) -> Self {
2463 self.restoration = Some(resto);
2464 self
2465 }
2466
2467 /// Install the shared diagnostics state. The state is propagated
2468 /// to the augmented-system solver at the top of [`Self::optimize`]
2469 /// so dump sites can consult per-iter gating.
2470 pub fn with_diagnostics(mut self, diag: Rc<DiagnosticsState>) -> Self {
2471 self.diagnostics = Some(diag);
2472 self
2473 }
2474
2475 /// Install an interactive debugger hook. Fired at each checkpoint
2476 /// in [`Self::optimize`]; returning [`crate::debug::DebugAction::Stop`]
2477 /// ends the solve with `SolverReturn::UserRequestedStop`.
2478 pub fn with_debug_hook(mut self, hook: Rc<RefCell<dyn crate::debug::DebugHook>>) -> Self {
2479 self.debug = Some(hook);
2480 self
2481 }
2482
2483 /// Shared handle to the installed debugger, if any — used to forward
2484 /// it into the restoration inner IPM.
2485 pub fn debug_hook(&self) -> Option<Rc<RefCell<dyn crate::debug::DebugHook>>> {
2486 self.debug.as_ref().map(Rc::clone)
2487 }
2488
2489 /// Fire the debugger hook (if installed) at `cp`, building a live
2490 /// [`crate::debug::DebugCtx`] over cheap handle clones. Returns the
2491 /// requested action, defaulting to `Resume` when no hook is set.
2492 fn fire_debug(&mut self, cp: crate::debug::Checkpoint) -> crate::debug::DebugAction {
2493 use crate::debug::{DebugAction, DebugCtx};
2494 // Clone the Rc so the hook borrow is released before we touch
2495 // `self.bundle` to apply any live option changes below.
2496 let Some(hook) = self.debug.as_ref().map(Rc::clone) else {
2497 return DebugAction::Resume;
2498 };
2499 let mut ctx = DebugCtx::new(Rc::clone(&self.data), Rc::clone(&self.cq), cp);
2500 let action = hook.borrow_mut().at_checkpoint(&mut ctx);
2501 // Drain any tolerances the hook asked to hot-swap and write them
2502 // into the live convergence-check policy, so the next iteration's
2503 // termination test uses the new value (no `resolve` needed).
2504 for (name, value) in ctx.take_live_tolerances() {
2505 self.bundle.conv_check.set_tolerance(&name, value);
2506 }
2507 action
2508 }
2509
2510 /// Run the restoration phase, bracketed by the `PreRestoration` /
2511 /// `PostRestoration` debug checkpoints so a debugger can inspect the
2512 /// iterate just before entry and just after exit. With no debugger
2513 /// installed this is exactly `invoke_restoration()`.
2514 fn invoke_restoration_debugged(&mut self) -> IterateOutcome {
2515 if let Some(o) = self.debug_stop(crate::debug::Checkpoint::PreRestoration) {
2516 return o;
2517 }
2518 let outcome = self.invoke_restoration();
2519 if let Some(o) = self.debug_stop(crate::debug::Checkpoint::PostRestoration) {
2520 return o;
2521 }
2522 outcome
2523 }
2524
2525 /// Fire a sub-iteration checkpoint from inside [`Self::iterate`].
2526 /// Returns `Some(Terminate(UserRequestedStop))` if the debugger asked
2527 /// to stop, so the caller can `return` it; `None` to continue.
2528 fn debug_stop(&mut self, cp: crate::debug::Checkpoint) -> Option<IterateOutcome> {
2529 if self.debug.is_none() {
2530 return None;
2531 }
2532 if self.fire_debug(cp) == crate::debug::DebugAction::Stop {
2533 Some(IterateOutcome::Terminate(SolverReturn::UserRequestedStop))
2534 } else {
2535 None
2536 }
2537 }
2538
2539 /// Fire the terminal post-mortem checkpoint (if a debugger is set),
2540 /// carrying the solve outcome so the hook can decide whether to pause
2541 /// at the final iterate. The action is advisory — the loop returns
2542 /// `result` regardless — so the hook just gets a last look.
2543 fn fire_debug_terminal(&mut self, result: SolverReturn) {
2544 use crate::debug::{Checkpoint, DebugCtx};
2545 let Some(hook) = self.debug.as_ref() else {
2546 return;
2547 };
2548 let mut ctx = DebugCtx::new(
2549 Rc::clone(&self.data),
2550 Rc::clone(&self.cq),
2551 Checkpoint::Terminated,
2552 )
2553 .with_status(format!("{result:?}"));
2554 let _ = hook.borrow_mut().at_checkpoint(&mut ctx);
2555 }
2556
2557 /// Cheap mid-iteration time-budget check (pounce#242). Returns the
2558 /// terminal [`SolverReturn`] when the shared [`Deadline`] has been
2559 /// crossed, so the caller can bail *within* an iteration — after the
2560 /// KKT factorization, before the line search — rather than only at the
2561 /// next outer-iteration convergence check. Returns `None` (never
2562 /// terminating) when no deadline is installed, keeping the
2563 /// direct-driver / unit-test paths on their `overall_alg`-based gate.
2564 /// A `None` here is not "no budget" but "check it at the coarse site".
2565 fn deadline_status(&self) -> Option<SolverReturn> {
2566 let d = self.data.borrow();
2567 let kind = d.deadline.as_ref()?.exceeded()?;
2568 Some(match kind {
2569 pounce_common::timing::DeadlineKind::Cpu => SolverReturn::CpuTimeExceeded,
2570 pounce_common::timing::DeadlineKind::Wall => SolverReturn::WallTimeExceeded,
2571 })
2572 }
2573
2574 /// One iteration body — port of `Optimize()`'s inner loop.
2575 /// Returns either `Continue` to keep iterating or a terminal
2576 /// [`SolverReturn`] mirroring upstream's exception → return-code
2577 /// translation table (see `MAIN_LOOP.md` §"Exception mapping").
2578 fn iterate(&mut self) -> IterateOutcome {
2579 // Shared timing accumulator — cheap Rc clone so each phase can
2580 // bump its own counter without re-borrowing `data`.
2581 let timing = self.data.borrow().timing.clone();
2582
2583 // Per-iteration span so every event emitted in this body (the
2584 // structured iteration record, restoration/linear-solve spans)
2585 // is tagged with the iteration index.
2586 let _iter_span =
2587 tracing::info_span!("iteration", iter = self.data.borrow().iter_count).entered();
2588
2589 // 1. Output iteration row. Header every 10 iters; the row itself
2590 // is built plain by the strategy (so the column widths stay
2591 // exact and unit-testable) and wrapped in a tiger/rust style
2592 // at the print site (pounce#71). `anstream::stdout()` strips
2593 // the escapes automatically when stdout is redirected or
2594 // `NO_COLOR` is set, so non-TTY output is plain text.
2595 //
2596 // Print BEFORE `reset_info` so the row reflects the accepted
2597 // step from the previous iteration (alphas, ls count,
2598 // alpha_char), matching upstream's `IpIpoptAlgorithm::Optimize`
2599 // ordering.
2600 timing.output_iteration.start();
2601 self.bundle.iter_output.write_output();
2602 if self.print_iter_output {
2603 use std::io::Write as _;
2604 let (iter_count, alpha_pr, alpha_char) = {
2605 let d = self.data.borrow();
2606 (d.iter_count, d.info_alpha_primal, d.info_alpha_primal_char)
2607 };
2608 let row = self.bundle.iter_output.format_row(&self.data, &self.cq);
2609 // Iteration 0 is the initial point — no step has been taken
2610 // yet, so `alpha_primal` is 0; treat it as a full step
2611 // (neutral black) rather than a stalling alarm (red).
2612 let style_alpha = if iter_count == 0 { 1.0 } else { alpha_pr };
2613 let style = pounce_common::style::iteration_row_style(style_alpha, alpha_char);
2614 let mut out = anstream::stdout();
2615 // Write errors (e.g. a closed pipe / `head` on the output)
2616 // are deliberately ignored: a vanished terminal must not
2617 // panic the solver, unlike the old `println!`.
2618 if iter_count % 10 == 0 {
2619 let _ = write!(out, "{}", crate::output::orig::OrigIterationOutput::HEADER);
2620 }
2621 let _ = writeln!(out, "{}{}{}", style.render(), row, style.render_reset());
2622 }
2623 timing.output_iteration.end();
2624
2625 // Structured per-iteration event (pounce#71) — the single source
2626 // of truth for the per-iteration trajectory. The JSON log sink
2627 // and the solve-report collector
2628 // (`pounce_observability::IterCollectorLayer`) both derive from
2629 // it. The text console layer filters this target out (its human
2630 // form is the colored table above).
2631 //
2632 // Skipped entirely when nothing consumes it (no iter-history
2633 // capture active and JSON logging off) so the default run pays
2634 // no per-iteration field-evaluation / allocation cost.
2635 if pounce_observability::iteration_event_wanted() {
2636 // The objective / `inf_pr` the row printed (gh#979): on the main
2637 // phase the unscaled violation of the user's model rather than
2638 // the internal slack-form residual, and on restoration rows the
2639 // original NLP's values rather than the restoration problem's.
2640 // The internal residual rides alongside as `inf_pr_internal`.
2641 let printed = self
2642 .bundle
2643 .iter_output
2644 .printed_objective_inf_pr(&self.data, &self.cq);
2645 let phase = if self.bundle.iter_output.is_restoration() {
2646 "restoration"
2647 } else {
2648 "main"
2649 };
2650 let d = self.data.borrow();
2651 let c = self.cq.borrow();
2652 let inf_pr_internal = c.curr_primal_infeasibility_max();
2653 let (objective, inf_pr) =
2654 printed.unwrap_or_else(|| (c.unscaled_curr_f(), inf_pr_internal));
2655 let alpha_char = d.info_alpha_primal_char;
2656 let alpha_char_s = alpha_char.to_string();
2657 let d_norm = match &d.delta {
2658 Some(delta) => delta.x.amax().max(delta.s.amax()),
2659 None => 0.0,
2660 };
2661 tracing::info!(
2662 target: pounce_observability::ITER_TARGET,
2663 iter = d.iter_count,
2664 objective = objective,
2665 inf_pr = inf_pr,
2666 inf_pr_internal = inf_pr_internal,
2667 inf_du = c.curr_dual_infeasibility_max(),
2668 mu = d.curr_mu,
2669 d_norm = d_norm,
2670 regularization = d.info_regu_x,
2671 alpha_dual = d.info_alpha_dual,
2672 alpha_primal = d.info_alpha_primal,
2673 ls_trials = d.info_ls_count,
2674 alpha_char = alpha_char_s.as_str(),
2675 resto_kind = pounce_common::style::resto_kind_str(alpha_char),
2676 phase = phase,
2677 );
2678 }
2679
2680 // Reset per-iteration info on data (after printing previous
2681 // iter's accepted-step info; before the next line search).
2682 self.data.borrow_mut().reset_info();
2683
2684 // 2. Convergence check.
2685 timing.check_convergence.start();
2686 let nlp_err = self.cq.borrow().curr_nlp_error();
2687 let iter_count = self.data.borrow().iter_count;
2688 if !nlp_err.is_finite() {
2689 timing.check_convergence.end();
2690 return IterateOutcome::Terminate(SolverReturn::InvalidNumberDetected);
2691 }
2692 // gh #534 progress history. One sample per outer iteration, recorded
2693 // before any of the guards below can divert, so the samples the
2694 // restoration-decline test reads are consecutive by construction.
2695 self.note_nlp_err(nlp_err);
2696 // Divergence guard — port of upstream `IpIpoptAlg.cpp` post-
2697 // AcceptTrialPoint check. When `max_i |x_i|` exceeds the
2698 // registered `diverging_iterates_tol` (default `1e20`), exit
2699 // cleanly with `DivergingIterates` rather than spiralling into
2700 // a degenerate restoration whose inner sub-NLP can't recover
2701 // (MESH: orig `f` already at -3.6e33 by iter 90, restoration
2702 // entered too late to bound `x`).
2703 //
2704 // A large `|x|` alone does not prove unboundedness, though:
2705 // `DivergingIterates` is Ipopt's *unboundedness* signal (it maps
2706 // to the AMPL 300 "unbounded" range), and under severe objective
2707 // ill-scaling the normal-mode IPM can take a large but transient
2708 // excursion on a problem that is bounded below with a finite
2709 // optimum (issue #248: MINLPLib `jit1`). Only conclude divergence
2710 // when the growth is *structurally* consistent with an unbounded
2711 // feasible region — some over-threshold component heading toward a
2712 // side with no finite bound. If every large component is pinned by
2713 // a finite bound (in particular, all variables boxed), the growth
2714 // is a scaling artifact, so let the normal convergence / iteration
2715 // machinery return the best iterate instead of a spurious
2716 // `Unbounded`.
2717 // Evaluate the structural check under an immutable borrow, then
2718 // update the persistence state and take the verdict separately so
2719 // the mutable field updates don't clash with the `data` borrow.
2720 // Two independent unboundedness paths share this block:
2721 // * the `diverging_iterates_tol` (`1e20`) magnitude guard, gated on
2722 // the free-variable structural check + geometric-growth streak
2723 // (issues #248 / #252); and
2724 // * the #285 recession-ray path — a *checked proof*, active from a
2725 // far lower magnitude floor, that catches a genuine recession ray
2726 // in `null(A_eq)` over free variables whose `|x|` grows only
2727 // linearly and so never reaches `1e20` within `max_iter`.
2728 //
2729 // The whole block is skipped while the line search is inside a
2730 // watchdog trial sequence (gh #818 review). A `'w'` iterate is
2731 // provisional by construction: the acceptor *rejected* it, the
2732 // filter was not augmented, and the line search is holding a
2733 // snapshot it will revert to within `watchdog_trial_iter_max`
2734 // (default 3) iterations. Reporting `DivergingIterates` there
2735 // throws that snapshot away and calls a problem unbounded on a
2736 // point the algorithm had already decided not to keep. This is
2737 // the same false positive `DIVERGENCE_PERSIST_ITERS` was
2738 // introduced for — a transient excursion that peaks and recedes —
2739 // except that a watchdog excursion recedes *by construction*, and
2740 // `DIVERGENCE_ABS_RUNAWAY` bypasses the streak, so the streak
2741 // alone does not cover it. Skipping rather than resetting leaves
2742 // the streak state untouched, so a watchdog gamble in the middle
2743 // of a genuine ray neither accumulates nor erases evidence; a real
2744 // divergence is reported at most three iterations later, from a
2745 // committed iterate. The deferral is capped at
2746 // `WATCHDOG_DEFER_MAX` consecutive iterations so it can never be
2747 // held open by a stale `in_watchdog` — see
2748 // `Self::watchdog_defer_streak` for the path that leaks one.
2749 //
2750 // Measured on the gh #818 quadratic at `n = 8`, cond `1e12`,
2751 // `limited_memory_max_history 6`: the solve reaches iteration 352
2752 // on the *third* watchdog trial of a sequence, at `|x|_inf ~ 5e22`
2753 // with the objective climbing to `+2.0e45` — the opposite of the
2754 // `f -> -inf` that `DivergingIterates` is supposed to mean — one
2755 // iteration before `StopWatchDog` would have restored an iterate
2756 // at `f = 2.26e4`.
2757 let in_watchdog = self.bundle.line_search.in_watchdog()
2758 && self.watchdog_defer_streak < Self::WATCHDOG_DEFER_MAX;
2759 if in_watchdog {
2760 self.watchdog_defer_streak += 1;
2761 } else {
2762 self.watchdog_defer_streak = 0;
2763 }
2764 let (amax, structural_free, is_ray) = {
2765 let data = self.data.borrow();
2766 match data.curr.as_ref() {
2767 Some(curr) if !in_watchdog => {
2768 let amax = curr.x.amax();
2769 let structural = amax > self.diverging_iterates_tol
2770 && self.divergence_is_true_unboundedness(&*curr.x);
2771 let is_ray = amax > Self::RECESSION_MIN_NORM
2772 && self.curr_is_recession_ray(&*curr.x, amax);
2773 (Some(amax), structural, is_ray)
2774 }
2775 _ => (None, false, false),
2776 }
2777 };
2778 // Evaluate the (scaled) objective only while a structural divergence
2779 // is live — the streak's descent gate needs it, and it costs an
2780 // objective evaluation, so skip it on the common non-diverging path.
2781 let curr_f = structural_free.then(|| self.cq.borrow().curr_f());
2782 // Evaluate both streak updates (no short-circuit) so each keeps its
2783 // state current, then fire if either concludes divergence.
2784 // ... but only when the streaks are actually being fed. Inside a
2785 // watchdog sequence `amax` is `None`, and running the updates
2786 // would reset both streaks on a point they never saw.
2787 let (fire_magnitude, fire_recession) = if in_watchdog {
2788 (false, false)
2789 } else {
2790 (
2791 self.update_divergence_verdict(amax, structural_free, curr_f),
2792 self.update_recession_verdict(amax.unwrap_or(0.0), is_ray),
2793 )
2794 };
2795 if fire_magnitude || fire_recession {
2796 if fire_recession && !fire_magnitude {
2797 tracing::debug!(target: "pounce::algorithm",
2798 "[POUNCE] recession-ray guard fired at iter {} (|x|_inf={:.2e}); \
2799 reporting DivergingIterates (pounce#285).",
2800 self.data.borrow().iter_count,
2801 amax.unwrap_or(f64::NAN),
2802 );
2803 }
2804 timing.check_convergence.end();
2805 return IterateOutcome::Terminate(SolverReturn::DivergingIterates);
2806 }
2807 // Dual-divergence guard (pounce#246). The primal guard above only
2808 // catches `|x|` blowing up; a bad warm start can instead send the
2809 // *dual* infeasibility diverging — `inf_du` 1 -> 1e14, the inertia
2810 // regularization -> 1e14, the barrier parameter frozen, full steps
2811 // still accepted by the filter because primal feasibility inches
2812 // down — while `|x|` stays bounded. `diverging_iterates_tol` never
2813 // trips, restoration is never entered, and the solve grinds in
2814 // ever-more-ill-conditioned KKT factorizations that each take
2815 // seconds (the emfl050 warm-start overshoot: one 3.8 s factorization
2816 // per iteration, forever). Detect a sustained streak of growing dual
2817 // infeasibility in the elevated regime and route to restoration —
2818 // the same recovery the least-square-multiplier init path reaches on
2819 // its own — before the factorizations start choking. Gated on a
2820 // large absolute `inf_du` so a well-behaved solve whose dual
2821 // residual transiently rises (then falls) is never diverted:
2822 // restoration is a heavier hammer than the guard should swing at a
2823 // merely-bumpy-but-converging iterate.
2824 //
2825 // OFF BY DEFAULT (pounce#250 follow-up). The emfl050 overshoot above is
2826 // how this was justified, and it did not reproduce: that measurement was
2827 // caller-side JAX compilation, and the build predating the guard solves
2828 // both emfl050 instances to the same optimum in the same time. What is
2829 // left is an effect on four of 1284 MINLPLib models that is knife-edge
2830 // and non-monotone in `dual_diverging_streak` — a better local optimum on
2831 // deb7/deb9 at exactly 15, and pooling_rt2stp turning Solve_Succeeded
2832 // into Maximum_Iterations_Exceeded at 10 and 15 only. Kept because it
2833 // does help when it helps, but not imposed. Full account in the option
2834 // help (`upstream_options.rs`).
2835 //
2836 // Two things to know before changing this:
2837 //
2838 // * `curr_dual_infeasibility_max` is the RAW ‖∇L‖∞, not divided by the
2839 // `s_d` optimality scaling the convergence check applies, and this runs
2840 // *before* `conv_check`. So the thresholds below are not on the same
2841 // quantity the solver's own tolerances are on, and the claim that they
2842 // are scale-robust holds only while `nlp_scaling_method != none`. No
2843 // exploit is known; the margin is thinner than it looks.
2844 // * The `DivergingIterates` fallback at the end is unreachable from every
2845 // shipped front end — CLI, pounce-py and cinterface all wire a
2846 // restoration provider, so the guard can only ever route to
2847 // restoration. Do not assume it is dead code and delete the provider
2848 // check; do not assume it is live and rely on the status either.
2849 // gh#884 — the dual-divergence-at-a-settled-primal signature.
2850 //
2851 // Four conjuncts, all required **at the same iterate**. That is not
2852 // stylistic: measured on the corpus, `deb7` on the L-BFGS leg reaches
2853 // a settled step of `5.9e-13` at an unscaled dual of `8.6e-7`, and its
2854 // *maximum* unscaled dual over settled iterates is `7.2e6`. A
2855 // formulation that took the minimum step and the maximum dual over a
2856 // window would fire on it; this one does not.
2857 //
2858 // What it is for: at a biactive complementarity pair the product row's
2859 // multiplier is arbitrary rather than determined, so it runs away, `s_d`
2860 // grows with it, and the `s_d`-normalised aggregate the convergence
2861 // check reads stays clean while the honest residual is `7.9e+04`. The
2862 // one quantity that feedback loop cannot fake is the step: with the
2863 // primal settled and the duals diverging the direction collapses. So
2864 // this reads the *raw unscaled* residual, and only at iterates where
2865 // the algorithm has demonstrably stopped moving with the primal solved.
2866 // A multiplier of `1e9` on a `1e-9` gradient cannot satisfy it, because
2867 // nothing here is normalised by a multiplier — which is gh#884's
2868 // criterion 2.
2869 //
2870 // It never changes a verdict. All it does is authorize the application
2871 // layer to spend a second solve; see `run_with_dual_divergence_retry`.
2872 if !self.dual_divergence_signature && self.dual_divergence_retry_step_tol > 0.0 {
2873 let cq = self.cq.borrow();
2874 let has_rows = cq.curr_c().dim() > 0 || cq.curr_d().dim() > 0;
2875 if has_rows && self.last_step_rel <= self.dual_divergence_retry_step_tol {
2876 let inf_pr = cq.curr_primal_infeasibility_max();
2877 let unscaled_du = cq.curr_unscaled_dual_infeasibility_max();
2878 if inf_pr <= DUAL_DIV_RETRY_PRIMAL_TOL
2879 && unscaled_du >= self.dual_divergence_retry_du_floor
2880 && unscaled_du.is_finite()
2881 {
2882 self.dual_divergence_signature = true;
2883 tracing::debug!(target: "pounce::algorithm",
2884 "[POUNCE] gh#884 dual-divergence signature at iter {}: \
2885 step_rel={:.2e} inf_pr={:.2e} unscaled_inf_du={:.2e}; \
2886 a cold retry is authorized if this solve does not succeed.",
2887 self.data.borrow().iter_count,
2888 self.last_step_rel, inf_pr, unscaled_du,
2889 );
2890 }
2891 }
2892 }
2893
2894 if self.dual_diverging_streak > 0 {
2895 let inf_du = self.cq.borrow().curr_dual_infeasibility_max();
2896 if inf_du.is_finite() && inf_du > self.dual_inf_prev && inf_du > DUAL_DIV_COUNT_FLOOR {
2897 self.dual_growth_streak += 1;
2898 } else {
2899 self.dual_growth_streak = 0;
2900 }
2901 self.dual_inf_prev = inf_du;
2902 if self.dual_growth_streak >= self.dual_diverging_streak && inf_du > DUAL_DIV_FIRE_TOL {
2903 self.dual_growth_streak = 0;
2904 self.dual_inf_prev = 0.0;
2905 // Arm the "never worse off" bookkeeping for the bet about to be
2906 // placed (pounce#250 follow-up).
2907 self.dual_guard_fired = true;
2908 timing.check_convergence.end();
2909 tracing::debug!(target: "pounce::algorithm",
2910 "[POUNCE] dual-divergence guard fired at iter {} (inf_du={:.2e}); \
2911 routing to restoration (pounce#246).",
2912 self.data.borrow().iter_count, inf_du,
2913 );
2914 if self.restoration.is_some() {
2915 return self.invoke_restoration_debugged();
2916 }
2917 return IterateOutcome::Terminate(SolverReturn::DivergingIterates);
2918 }
2919 }
2920 let conv_status = self
2921 .bundle
2922 .conv_check
2923 .check_convergence_with_state(nlp_err, iter_count, &self.data, &self.cq);
2924 // Snapshot the *first* refused certificate. Baseline would have stopped
2925 // and returned exactly this point, so keeping it — and only it — is what
2926 // makes the "never worse" guarantee exact rather than approximate. A
2927 // later refusal is also a valid certificate but not necessarily a
2928 // better one, so it must not overwrite this.
2929 if !self.vetoed_seen && self.bundle.conv_check.certificate_vetoed() {
2930 // Latch on *seeing* the refusal, not on the snapshot being present:
2931 // the veto flag is sticky, so keying off `vetoed.is_none()` would
2932 // let a failed capture be completed at a later, arbitrary iterate.
2933 // See `IpoptAlgorithm::vetoed_seen`.
2934 self.vetoed_seen = true;
2935 self.vetoed = self.snapshot_current(iter_count);
2936 }
2937 if !self.vetoed_acceptable_seen && self.bundle.conv_check.acceptable_certificate_vetoed() {
2938 self.vetoed_acceptable_seen = true;
2939 self.vetoed_acceptable = self.snapshot_current(iter_count);
2940 }
2941 // gh #695: a successful verdict asserts the convergence test passed;
2942 // reporting one alongside a non-finite objective is self-contradictory,
2943 // and a caller that gates on `status` and then reads `obj_val` silently
2944 // receives `NaN`. The convergence test cannot notice on its own — it
2945 // reads gradients, residuals and complementarity, never the objective
2946 // *value* — so with finite derivatives and a satisfied equality the KKT
2947 // residuals are genuinely small and the solve converges on a point
2948 // whose objective is not a number.
2949 //
2950 // Only the *equality*-constrained shape reached here: the unconstrained
2951 // and bounds-only shapes fail in the step computation and the
2952 // inequality-constrained one already trips an invalid-number guard, so
2953 // this closes the one column of that matrix that was reporting success.
2954 // gh #292 closed the NaN-*gradient* hole and recorded `f`-returns-NaN as
2955 // the safe contrast case, which held for the shapes it exercised and not
2956 // for this one.
2957 //
2958 // `Invalid_Number_Detected` is the status Ipopt's `Eval_f` gives a
2959 // non-finite objective, which POUNCE's own inequality-constrained shape
2960 // already agreed with. The same check already guards the restoration
2961 // near-feasible exit below, for the same reason on a different path
2962 // (CUTE `himmelbj`); this extends it to the ordinary convergence exit
2963 // rather than adding a second rule.
2964 let converged_success = matches!(
2965 conv_status,
2966 ConvergenceStatus::Converged | ConvergenceStatus::ConvergedToAcceptable
2967 );
2968 if converged_success && !self.cq.borrow().curr_f().is_finite() {
2969 timing.check_convergence.end();
2970 return IterateOutcome::Terminate(SolverReturn::InvalidNumberDetected);
2971 }
2972 if conv_status == ConvergenceStatus::Continue
2973 && let Some(hook) = self.handover.as_mut()
2974 {
2975 let (curr, mu) = {
2976 let d = self.data.borrow();
2977 (d.curr.clone(), d.curr_mu)
2978 };
2979 if let Some(next) = curr.and_then(|c| hook(&c, iter_count, nlp_err, mu)) {
2980 timing.check_convergence.end();
2981 let mut d = self.data.borrow_mut();
2982 d.set_curr(next);
2983 // Same frame marker crossover sets: the point sits on the
2984 // declared bounds, not inside the relaxed ones (gh#654).
2985 d.curr_from_crossover = true;
2986 drop(d);
2987 // The handed-over point is a first-order KKT point, which a
2988 // saddle of a nonconvex model also is — and the SQP's step-3
2989 // path certifies it without any curvature test. The interior
2990 // method's own success exit runs the gh #797 escape first;
2991 // this one does too, so the handover cannot report a point
2992 // the interior method would have left (measured: on
2993 // `nonconvex_qp` / `nonconvex_qp_ineq` / `nonconvex_two_escapes`
2994 // it otherwise ended at the stationary point the IPM
2995 // escapes, objective `1` against `≈ 0`).
2996 if self.try_neg_curv_escape(iter_count) {
2997 // The iterate is the interior method's again.
2998 self.data.borrow_mut().curr_from_crossover = false;
2999 return IterateOutcome::Continue;
3000 }
3001 return IterateOutcome::Terminate(SolverReturn::Success);
3002 }
3003 }
3004 match conv_status {
3005 ConvergenceStatus::Continue => {}
3006 ConvergenceStatus::Converged => {
3007 timing.check_convergence.end();
3008 // gh #797: first-order stationarity is not a local minimum on a
3009 // nonconvex model. If the reduced Hessian here is indefinite,
3010 // leave along a direction of negative curvature instead of
3011 // certifying a constrained maximum — bounded in cost, and
3012 // floored at this very point.
3013 if self.try_neg_curv_escape(iter_count) {
3014 return IterateOutcome::Continue;
3015 }
3016 return IterateOutcome::Terminate(SolverReturn::Success);
3017 }
3018 ConvergenceStatus::ConvergedToAcceptable => {
3019 timing.check_convergence.end();
3020 return IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint);
3021 }
3022 ConvergenceStatus::MaxIterExceeded => {
3023 timing.check_convergence.end();
3024 return IterateOutcome::Terminate(SolverReturn::MaxiterExceeded);
3025 }
3026 ConvergenceStatus::CpuTimeExceeded => {
3027 timing.check_convergence.end();
3028 return IterateOutcome::Terminate(SolverReturn::CpuTimeExceeded);
3029 }
3030 ConvergenceStatus::WallTimeExceeded => {
3031 timing.check_convergence.end();
3032 return IterateOutcome::Terminate(SolverReturn::WallTimeExceeded);
3033 }
3034 ConvergenceStatus::LocallyInfeasible => {
3035 timing.check_convergence.end();
3036 // gh #505: consult the acceptable-point stash, as the
3037 // restoration-cycle exits below already do (`:2686`, `:2716`,
3038 // both via `terminate_acceptable_or`). This arm used to return
3039 // without it, so a solve that had passed through an acceptable
3040 // iterate — stashed, un-vetoed, sitting there as a rollback
3041 // target — discarded it and surfaced the hard verdict instead.
3042 // The stashing code sits *after* this match, so the firing
3043 // iteration returns before it would even consider stashing;
3044 // only iterates from earlier in the solve are on offer, which
3045 // is exactly what a rollback target is.
3046 //
3047 // This is about what to *return* once the verdict has fired,
3048 // not about when it fires. Whether the rapid detector should
3049 // have convicted this point at all is a separate question,
3050 // answered by the violation floor in `OptErrorConvCheck`
3051 // (gh #519).
3052 //
3053 // Inert on genuinely infeasible models: `store_acceptable_point`
3054 // is gated on `current_is_acceptable_with_state`, which requires
3055 // `acceptable_tol` *and* the unscaled violation against
3056 // `acceptable_constr_viol_tol`, and the scale-relative veto
3057 // blocks the stash outright for a row violated relative to its
3058 // own magnitude. Nothing is stashed on such a model, so
3059 // `terminate_acceptable_or` falls through to the verdict
3060 // unchanged. `infeasible_models_are_never_reported_solved`
3061 // (`infeasible_status_tol_invariance.rs`) is the standing guard.
3062 //
3063 // That inertness rests entirely on the stash gate having no
3064 // iteration budget (gh #693). While it carried the certificate
3065 // veto's `VETO_MAX_EXTRA_ITERS`, a solve that took more than 60
3066 // blocked iterations to convict stashed the very point the veto
3067 // exists to reject and rolled back to it here — an infeasible
3068 // model reported `Solved_To_Acceptable_Level`. See
3069 // `issue_693_relative_infeasibility_stash.rs`.
3070 return self.terminate_local_infeasibility();
3071 }
3072 ConvergenceStatus::Failed => {
3073 timing.check_convergence.end();
3074 return IterateOutcome::Terminate(SolverReturn::InternalError);
3075 }
3076 }
3077
3078 // Stash the iterate if it satisfies the per-component
3079 // `acceptable_*_tol` triplet. Mirrors upstream
3080 // `IpBacktrackingLineSearch.cpp:282-289` — checked at the top
3081 // of every line-search call so the most recent acceptable
3082 // iterate is always available as a rollback target if
3083 // restoration later fails. The recorder feeds
3084 // `acceptable_obj_change_tol`'s stability cross-check on
3085 // subsequent iterates.
3086 self.curr_iterate_acceptable = self
3087 .bundle
3088 .conv_check
3089 .current_is_acceptable_with_state(nlp_err, &self.data, &self.cq);
3090 if self.curr_iterate_acceptable {
3091 self.store_acceptable_point();
3092 let curr_f = self.cq.borrow().curr_f();
3093 self.bundle.conv_check.set_curr_acceptable_obj(curr_f);
3094 // pounce#250 follow-up: keep the *best* acceptable iterate, not just
3095 // the latest. `store_acceptable_point` overwrites unconditionally,
3096 // so once the dual-divergence guard diverts a solve the rollback
3097 // target drifts to whatever the diverted run last touched — which
3098 // may be far worse than a point already in hand. Recorded on every
3099 // acceptable iterate (including before any diversion) and read only
3100 // when the guard fired; see `honour_best_acceptable_after_dual_guard`.
3101 self.record_best_acceptable(curr_f);
3102 }
3103 timing.check_convergence.end();
3104
3105 // gh #534: a deferred restoration decline is a bet with a deadline.
3106 // Checked *after* the convergence check, so a strict certificate the
3107 // continuation reached in the meantime wins the bet rather than being
3108 // pre-empted by its own expiry; and after the acceptable stash, so a
3109 // continuation that ended somewhere better has been recorded before the
3110 // floor comparison reads it.
3111 if self.decline_deadline_iter.is_some_and(|d| iter_count > d) {
3112 return self.terminate_at_decline_floor();
3113 }
3114
3115 // gh #797: the negative-curvature escape is the same kind of bet, and
3116 // its deadline is checked in the same place and for the same reason —
3117 // after the convergence check, so a certificate the continuation
3118 // reached wins rather than being pre-empted by its own expiry.
3119 if self.neg_curv_deadline_iter.is_some_and(|d| iter_count > d) {
3120 return self.terminate_at_neg_curv_floor();
3121 }
3122
3123 // 3. Hessian update. Must run BEFORE `update_barrier_parameter`
3124 // so the adaptive-μ oracles (probing, quality-function) drive
3125 // their affine/centering solves against `W(curr_N)`, not the
3126 // stale `W(curr_{N-1})` left in `data.w` by the previous iter's
3127 // tail-end Hessian update. Upstream calls `UpdateHessian()`
3128 // first in every main-loop body (`IpIpoptAlg.cpp:386`); pounce
3129 // previously reordered this to the tail, which made iters 1+
3130 // pick μ from the prior iterate's Hessian on adaptive-mu +
3131 // quality-function — visible on CRESC50 as a catastrophic
3132 // early-iter divergence (theta=5.8e5 by iter 61 vs upstream
3133 // never entering restoration).
3134 timing.update_hessian.start();
3135 let _ = self.bundle.hess.update_hessian(&self.data, &self.cq);
3136 timing.update_hessian.end();
3137
3138 // 4. Barrier parameter. Pass nlp + search_dir through so the
3139 // adaptive μ oracles (probing, quality-function) can drive
3140 // their own affine-step solves; monotone ignores them.
3141 // Snapshot the tiny-step flag (set by the previous iteration's
3142 // tiny-step branch) and the entry mu — if μ can't reduce while
3143 // the flag is on, upstream `IpMonotoneMuUpdate.cpp:158-161`
3144 // throws TINY_STEP_DETECTED → STOP_AT_TINY_STEP, which we
3145 // realise as a clean termination here.
3146 //
3147 // Both updates terminate, by different routes (pounce#512).
3148 // Monotone has one throw site covering its whole update, so the
3149 // μ-unchanged comparison below reconstructs it exactly, gated on
3150 // `terminates_on_tiny_step()`. `IpAdaptiveMuUpdate.cpp` throws at
3151 // two specific sites (`:330-333`, `:377-380`) and merely fixes μ
3152 // and keeps iterating elsewhere, so the comparison would over-fire
3153 // there — on the no-bounds short-circuit, which returns before
3154 // upstream even reads the flag, and on a free-mode oracle that
3155 // re-picks the current μ. The adaptive update therefore raises
3156 // `request_tiny_step_stop` at its own two sites and opts out of
3157 // the comparison. (An earlier comment here claimed the adaptive
3158 // update never self-terminates; it does — `force_no_progress` is
3159 // what happens on the iterations that do *not* throw.)
3160 timing.update_barrier_parameter.start();
3161 let tiny_at_entry = self.data.borrow().tiny_step_flag;
3162 let mu_before = self.data.borrow().curr_mu;
3163 let mu_terminates_on_tiny = self.bundle.mu_update.terminates_on_tiny_step();
3164 let next_mu = self.bundle.mu_update.update_barrier_parameter(
3165 &self.data,
3166 &self.cq,
3167 self.nlp.as_ref(),
3168 self.search_dir.as_mut(),
3169 );
3170 self.data.borrow_mut().curr_mu = next_mu;
3171 timing.update_barrier_parameter.end();
3172
3173 // pounce#510 — line-search reset. Upstream's μ updates own a
3174 // `linesearch_` handle and call `linesearch_->Reset()` (which
3175 // clears the filter via `FilterLSAcceptor::Reset`,
3176 // `IpFilterLSAcceptor.cpp:524-532`) at four fixed points:
3177 // `IpAdaptiveMuUpdate.cpp:339` (fixed-mode decrease), `:386`
3178 // (free→fixed switch), `:431` (**unconditionally** on every
3179 // free-mode iteration, μ moved or not), and
3180 // `IpMonotoneMuUpdate.cpp:165` (after a monotone reduction).
3181 // Pounce's `MuUpdate` trait has no line-search handle, so each
3182 // update raises `request_ls_reset` at exactly those points and
3183 // we honour it here — the same plumbing `request_resto` uses
3184 // below.
3185 //
3186 // This used to be inferred from `next_mu != mu_before`. That
3187 // proxy is right for the monotone update but wrong for the
3188 // adaptive one, which resets every free-mode iteration
3189 // regardless of μ: whenever μ stayed numerically put (the
3190 // free-mode endgame, and any iteration after a restoration that
3191 // returns at the same μ) the filter kept entries computed
3192 // against a barrier parameter and an iterate the algorithm had
3193 // already left. On #505's reproducer that rejected every trial
3194 // step from α=2.4e-6 down to 1e-12 on the filter alone and
3195 // forced a spurious restoration.
3196 //
3197 // Both flags are consumed here, but the tiny-step stop is
3198 // answered first: at each of the two adaptive sites that raise
3199 // it, upstream's `TINY_STEP_DETECTED` throw sits *above* the
3200 // reset it would otherwise reach (`cpp:330-333` before `:339`,
3201 // `:377-380` before `:386`), so a terminating iteration never
3202 // resets the line search.
3203 let (tiny_step_stop_requested, ls_reset) = {
3204 let mut d = self.data.borrow_mut();
3205 let flags = (d.request_tiny_step_stop, d.request_ls_reset);
3206 d.request_tiny_step_stop = false;
3207 d.request_ls_reset = false;
3208 flags
3209 };
3210 if tiny_step_stop_requested
3211 || (tiny_at_entry
3212 && mu_terminates_on_tiny
3213 && (next_mu - mu_before).abs() < Number::EPSILON)
3214 {
3215 return IterateOutcome::Terminate(SolverReturn::StopAtTinyStep);
3216 }
3217 if ls_reset {
3218 self.bundle.line_search.reset();
3219 }
3220
3221 // A model coupled to the barrier parameter
3222 // (`TNLP::update_with_barrier`; the complementarity driver's coupled
3223 // continuation tightens `G·H <= τ` with μ) moves its constraint
3224 // values here, after μ and before the next step is computed: a shift
3225 // constant in x, so the derivatives stand. The NLP drops its cached
3226 // `c`/`d` and keeps the Jacobians and Hessian; one that cannot has the
3227 // current `x` re-issued under a fresh tag instead, which invalidates
3228 // everything keyed on it. The filter's entries were measured against
3229 // the old values, so the line search is reset, as after a μ decrease.
3230 // Not during restoration, whose iterate belongs to the feasibility
3231 // problem.
3232 if !self.fires_as_restoration
3233 && let Some(nlp) = self.nlp.as_ref()
3234 {
3235 let moved = self
3236 .tnlp
3237 .as_ref()
3238 .is_some_and(|t| t.borrow_mut().update_with_barrier(next_mu));
3239 if moved {
3240 if !nlp.borrow().invalidate_constraint_values() {
3241 let mut d = self.data.borrow_mut();
3242 if let Some(curr) = d.curr.clone() {
3243 let x: Rc<dyn Vector> = Rc::from(curr.x.make_new_copy());
3244 d.set_curr(curr.with_x(x));
3245 }
3246 }
3247 self.bundle.line_search.reset();
3248 }
3249 }
3250
3251 // pounce#58 — iterate-quality guard for the probing oracle.
3252 // The μ-update layer sets `request_resto` when the input
3253 // iterate is too corrupted for the probing rule to produce a
3254 // sane μ (see `mu/adaptive.rs` Probing dispatch). Restoration
3255 // re-initialises the multipliers and gives the outer loop a
3256 // clean iterate to continue from. When no restoration phase
3257 // is configured (embedded callers, tests), emit a one-line
3258 // notice and continue with the current μ — the guard has
3259 // already prevented the destabilising 4-order μ jump.
3260 let request_resto = {
3261 let mut d = self.data.borrow_mut();
3262 let f = d.request_resto;
3263 d.request_resto = false;
3264 // `start_with_resto` — upstream's "switch to the feasibility
3265 // restoration phase in the first iteration". It rides the
3266 // same request flag rather than adding a second path into
3267 // restoration, and it is consumed here so it fires exactly
3268 // once: `iter_count` is 0 only on the first pass, and
3269 // restoration advances it.
3270 f || (self.start_with_resto && d.iter_count == 0)
3271 };
3272 if request_resto {
3273 if self.restoration.is_some() {
3274 return self.invoke_restoration_debugged();
3275 } else {
3276 tracing::warn!(target: "pounce::algorithm",
3277 "[POUNCE] probing-oracle iterate-quality guard fired \
3278 at iter {}, but no restoration phase is configured; \
3279 continuing with μ={:.3e}.",
3280 self.data.borrow().iter_count,
3281 next_mu,
3282 );
3283 }
3284 }
3285
3286 // Sub-iteration checkpoint: μ has been updated for this iteration.
3287 if let Some(o) = self.debug_stop(crate::debug::Checkpoint::AfterBarrierUpdate) {
3288 return o;
3289 }
3290
3291 // 4b. `linear_system_scaling=slack-based` — refresh the
3292 // iterate-dependent part of the augmented-system scaling
3293 // before anything factorizes it.
3294 //
3295 // The other scaling methods (Ruiz, MC19) derive their
3296 // factors from the matrix they are handed and need nothing
3297 // from here. Slack-based is a function of the iterate, and
3298 // upstream's method reads `IpCq()` directly; pounce's
3299 // scaling methods live in `pounce-linsol`, below the
3300 // algorithm, so the value is computed here and pushed down.
3301 // Inert unless the option selected it.
3302 self.push_slack_scaling();
3303
3304 // 5. Search direction. Skipped without an NLP + search_dir.
3305 // (Hessian was updated in step 3 above before the barrier-μ
3306 // oracle so that adaptive-μ uses W(curr_N), not stale W.)
3307 if let (Some(nlp), Some(sd)) = (self.nlp.as_ref(), self.search_dir.as_mut()) {
3308 timing.compute_search_direction.start();
3309 // Fields are declared `Empty` and filled by the linear
3310 // solver (matrix size, factor nnz, inertia, ordering — see
3311 // `pounce_feral::record_factor_stats`) and below
3312 // (regularization), so the `linear_solve` span carries the
3313 // KKT-solve characteristics for the JSON sink (pounce#71).
3314 let ls_span = tracing::info_span!(
3315 target: "pounce::linsol",
3316 "linear_solve",
3317 n = tracing::field::Empty,
3318 matrix_nnz = tracing::field::Empty,
3319 factor_nnz = tracing::field::Empty,
3320 inertia_neg = tracing::field::Empty,
3321 fill_ratio = tracing::field::Empty,
3322 ordering = tracing::field::Empty,
3323 regularization = tracing::field::Empty,
3324 );
3325 let ls_enter = ls_span.enter();
3326 let ok = sd.compute_search_direction(&self.data, &self.cq, nlp);
3327 ls_span.record("regularization", self.data.borrow().info_regu_x);
3328 // Within-span marker so the enriched `linear_solve` fields
3329 // (filled by the solver above) surface to the JSON sink at
3330 // debug level; off at the default `info` level.
3331 tracing::debug!(target: "pounce::linsol", "kkt solve complete");
3332 drop(ls_enter);
3333 timing.compute_search_direction.end();
3334 // Fine-grained time-budget gate (pounce#244). The KKT solve now
3335 // checks the shared deadline *between* its major factorization
3336 // steps (inertia correction / iterative refinement) and aborts
3337 // cooperatively when the budget is crossed — bounding the
3338 // overshoot to roughly one factorization instead of the whole
3339 // multi-factorization sweep that #242's post-solve check let run
3340 // to completion. Whether the solve returned a completed step or
3341 // bailed mid-escalation, if the deadline tripped, stop here with
3342 // the time-limit status *before* the `!ok` branch below would
3343 // otherwise route a deadline-aborted solve into restoration.
3344 // `data.curr` is untouched by the step computation, so it still
3345 // holds the last accepted iterate.
3346 if let Some(ret) = self.deadline_status() {
3347 return IterateOutcome::Terminate(ret);
3348 }
3349 if !ok {
3350 // Mirror upstream `IpIpoptAlg.cpp:417-430`: a failed
3351 // step computation puts the algorithm in emergency
3352 // mode, which calls `BacktrackingLineSearch::
3353 // ActivateFallbackMechanism` (cpp:1312-1328). When a
3354 // restoration phase is configured, the next pass of
3355 // `ComputeAcceptableTrialPoint` sees `goto_resto` at
3356 // cpp:299-306 and hands control to restoration. Only
3357 // when neither restoration nor an acceptor-level
3358 // fallback is available does upstream throw
3359 // `STEP_COMPUTATION_FAILED`.
3360 if self.restoration.is_some() {
3361 // A non-finite Hessian (e.g. `|x|^2·|x|^0.85` evaluated
3362 // at exactly `x = 0`: `0·∞` in the second derivative)
3363 // fails the factorization before any inertia question
3364 // is asked. Restoration is the right fallback — its
3365 // Hessian drops the objective term and it moves `x`
3366 // off the point — but the entry says nothing about a
3367 // restoration cycle, so it must not be a gh#274 strike.
3368 self.resto_entry_nonfinite_w = self
3369 .data
3370 .borrow()
3371 .w
3372 .as_ref()
3373 .is_some_and(|w| !w.has_valid_numbers());
3374 return self.invoke_restoration_debugged();
3375 }
3376 return IterateOutcome::Terminate(SolverReturn::ErrorInStepComputation);
3377 }
3378 if std::env::var_os("POUNCE_DBG_DELTA").is_some() {
3379 let d = self.data.borrow();
3380 let it = d.iter_count;
3381 if let Some(delta) = d.delta.as_ref() {
3382 use crate::iterates_vector::IteratesVector;
3383 use pounce_linalg::{Vector, compound_vector::CompoundVector};
3384 let dv: &IteratesVector = delta;
3385 tracing::debug!(target: "pounce::algorithm",
3386 "[PN_DELTA] iter={} mu={:.6e} dx_amax={:.6e} ds_amax={:.6e} dyc_amax={:.6e} dyd_amax={:.6e} dzL_amax={:.6e} dzU_amax={:.6e} dvL_amax={:.6e} dvU_amax={:.6e}",
3387 it, d.curr_mu,
3388 dv.x.amax(), dv.s.amax(), dv.y_c.amax(), dv.y_d.amax(),
3389 dv.z_l.amax(), dv.z_u.amax(), dv.v_l.amax(), dv.v_u.amax()
3390 );
3391 if let Some(cdx) = dv.x.as_any().downcast_ref::<CompoundVector>() {
3392 tracing::debug!(target: "pounce::algorithm",
3393 "[PN_DELTA] iter={} dx_blocks_amax: orig={:.6e} nc={:.6e} pc={:.6e} nd={:.6e} pd={:.6e}",
3394 it,
3395 cdx.comp(0).amax(),
3396 cdx.comp(1).amax(),
3397 cdx.comp(2).amax(),
3398 cdx.comp(3).amax(),
3399 cdx.comp(4).amax(),
3400 );
3401 tracing::debug!(target: "pounce::algorithm",
3402 "[PN_DELTA] iter={} dx_blocks_nrm2: orig={:.6e} nc={:.6e} pc={:.6e} nd={:.6e} pd={:.6e}",
3403 it,
3404 cdx.comp(0).nrm2(),
3405 cdx.comp(1).nrm2(),
3406 cdx.comp(2).nrm2(),
3407 cdx.comp(3).nrm2(),
3408 cdx.comp(4).nrm2(),
3409 );
3410 tracing::debug!(target: "pounce::algorithm",
3411 "[PN_DELTA] iter={} dx_blocks_asum: orig={:.6e} nc={:.6e} pc={:.6e} nd={:.6e} pd={:.6e}",
3412 it,
3413 cdx.comp(0).asum(),
3414 cdx.comp(1).asum(),
3415 cdx.comp(2).asum(),
3416 cdx.comp(3).asum(),
3417 cdx.comp(4).asum(),
3418 );
3419 // Argmax of orig block via dot with sign — print first few values.
3420 if let Some(dv_orig) =
3421 cdx.comp(0)
3422 .as_any()
3423 .downcast_ref::<pounce_linalg::dense_vector::DenseVector>()
3424 {
3425 let v = dv_orig.values();
3426 let mut imax = 0usize;
3427 let mut amax = 0.0f64;
3428 for (i, &x) in v.iter().enumerate() {
3429 if x.abs() > amax {
3430 amax = x.abs();
3431 imax = i;
3432 }
3433 }
3434 tracing::debug!(target: "pounce::algorithm",
3435 "[PN_DELTA] iter={} dx_orig argmax: i={} v={:.17e} (n={})",
3436 it,
3437 imax,
3438 v[imax],
3439 v.len()
3440 );
3441 }
3442 }
3443 let p = &d.perturbations;
3444 tracing::debug!(target: "pounce::algorithm",
3445 "[PN_DELTA] iter={} pert: dx={:.6e} ds={:.6e} dc={:.6e} dd={:.6e}",
3446 it, p.delta_x, p.delta_s, p.delta_c, p.delta_d
3447 );
3448 drop(d);
3449 let cq = self.cq.borrow();
3450 let gf = cq.curr_grad_f();
3451 let gl = cq.curr_grad_lag_x();
3452 let cc = cq.curr_c();
3453 let cd = cq.curr_d_minus_s();
3454 let sx = cq.curr_sigma_x();
3455 let ss = cq.curr_sigma_s();
3456 tracing::debug!(target: "pounce::algorithm",
3457 "[PN_DELTA] iter={} cq: gradf_amax={:.6e} gradf_nrm2={:.6e} gradlag_amax={:.6e} gradlag_nrm2={:.6e} c_amax={:.6e} c_nrm2={:.6e} d_amax={:.6e} d_nrm2={:.6e} sigx_amax={:.6e} sigx_nrm2={:.6e} sigs_amax={:.6e} sigs_nrm2={:.6e}",
3458 it,
3459 gf.amax(), gf.nrm2(),
3460 gl.amax(), gl.nrm2(),
3461 cc.amax(), cc.nrm2(),
3462 cd.amax(), cd.nrm2(),
3463 sx.amax(), sx.nrm2(),
3464 ss.amax(), ss.nrm2(),
3465 );
3466 if let Some(cgf) = gf.as_any().downcast_ref::<CompoundVector>() {
3467 tracing::debug!(target: "pounce::algorithm",
3468 "[PN_DELTA] iter={} gradf_blocks_amax: orig={:.6e} nc={:.6e} pc={:.6e} nd={:.6e} pd={:.6e}",
3469 it,
3470 cgf.comp(0).amax(),
3471 cgf.comp(1).amax(),
3472 cgf.comp(2).amax(),
3473 cgf.comp(3).amax(),
3474 cgf.comp(4).amax(),
3475 );
3476 }
3477 if let Some(curr) = self.data.borrow().curr.clone() {
3478 tracing::debug!(target: "pounce::algorithm",
3479 "[PN_DELTA] iter={} bound_mults: zL_amax={:.6e} zU_amax={:.6e} vL_amax={:.6e} vU_amax={:.6e} s_amax={:.6e} s_nrm2={:.6e} x_amax={:.6e} x_nrm2={:.6e}",
3480 it,
3481 curr.z_l.amax(), curr.z_u.amax(),
3482 curr.v_l.amax(), curr.v_u.amax(),
3483 curr.s.amax(), curr.s.nrm2(),
3484 curr.x.amax(), curr.x.nrm2(),
3485 );
3486 if let Some(czl) = curr.z_l.as_any().downcast_ref::<CompoundVector>() {
3487 tracing::debug!(target: "pounce::algorithm",
3488 "[PN_DELTA] iter={} zL_blocks_amax: orig={:.6e} nc={:.6e} pc={:.6e} nd={:.6e} pd={:.6e}",
3489 it,
3490 czl.comp(0).amax(),
3491 czl.comp(1).amax(),
3492 czl.comp(2).amax(),
3493 czl.comp(3).amax(),
3494 czl.comp(4).amax(),
3495 );
3496 }
3497 if let Some(czu) = curr.z_u.as_any().downcast_ref::<CompoundVector>() {
3498 tracing::debug!(target: "pounce::algorithm", "[PN_DELTA] iter={} zU_ncomps={}", it, czu.n_comps());
3499 for ic in 0..czu.n_comps() {
3500 tracing::debug!(target: "pounce::algorithm",
3501 "[PN_DELTA] iter={} zU_block[{}]_amax={:.6e} dim={}",
3502 it,
3503 ic,
3504 czu.comp(ic).amax(),
3505 czu.comp(ic).dim()
3506 );
3507 }
3508 }
3509 }
3510 if let Some(csx) = sx.as_any().downcast_ref::<CompoundVector>() {
3511 tracing::debug!(target: "pounce::algorithm",
3512 "[PN_DELTA] iter={} sigx_blocks_amax: orig={:.6e} nc={:.6e} pc={:.6e} nd={:.6e} pd={:.6e}",
3513 it,
3514 csx.comp(0).amax(),
3515 csx.comp(1).amax(),
3516 csx.comp(2).amax(),
3517 csx.comp(3).amax(),
3518 csx.comp(4).amax(),
3519 );
3520 }
3521 drop(cq);
3522 let d = self.data.borrow();
3523 // Also dump curr.x_orig argmax
3524 if let Some(curr) = d.curr.as_ref() {
3525 if let Some(cx) = curr.x.as_any().downcast_ref::<CompoundVector>() {
3526 if let Some(xo) =
3527 cx.comp(0)
3528 .as_any()
3529 .downcast_ref::<pounce_linalg::dense_vector::DenseVector>()
3530 {
3531 let v = xo.values();
3532 let mut imax = 0usize;
3533 let mut amax = 0.0f64;
3534 for (i, &x) in v.iter().enumerate() {
3535 if x.abs() > amax {
3536 amax = x.abs();
3537 imax = i;
3538 }
3539 }
3540 tracing::debug!(target: "pounce::algorithm", "[PN_DELTA] iter={} curr_x_orig argmax: i={} v={:.17e} amax={:.17e} nrm2={:.17e}",
3541 it, imax, v[imax], xo.amax(), xo.nrm2());
3542 }
3543 }
3544 }
3545 }
3546 }
3547 }
3548
3549 // Capture KKT-factorization diagnostics for the debugger before
3550 // the line search runs. Only when a debugger is installed. The
3551 // inertia/status fields are cheap and always captured; the matrix
3552 // triplets and `LDLᵀ` factor are O(nnz) assemblies, so they're
3553 // captured only while the debugger is stepping (`wants_kkt_capture`)
3554 // — a detached/free-running debugger drops them to keep the run
3555 // cheap. `kkt_debug` is overwritten every iteration and never
3556 // cleared at `iter_start`, so a stepping session always has the
3557 // previous iteration's system to look back at via `viz kkt`/`viz L`.
3558 if let Some(hook) = self.debug.as_ref() {
3559 let capture_heavy = hook.borrow().wants_kkt_capture();
3560 let captured_iter = self.data.borrow().iter_count;
3561 let info = self.search_dir.as_ref().map(|sd| {
3562 let pd = sd.pd_solver_mut();
3563 let aug = pd.aug_solver();
3564 let provides = aug.provides_inertia();
3565 crate::ipopt_data::KktDebug {
3566 iter: captured_iter,
3567 dim: aug.system_dim(),
3568 n_neg: if provides {
3569 aug.number_of_neg_evals()
3570 } else {
3571 -1
3572 },
3573 provides_inertia: provides,
3574 status: format!("{:?}", aug.last_solve_status()),
3575 matrix: if capture_heavy {
3576 aug.kkt_triplets()
3577 } else {
3578 None
3579 },
3580 l_factor: if capture_heavy {
3581 aug.l_factor(true)
3582 } else {
3583 None
3584 },
3585 }
3586 });
3587 self.data.borrow_mut().kkt_debug = info;
3588 }
3589
3590 // Sub-iteration checkpoint: the Newton step `δ` (data.delta) and
3591 // the applied regularization are now available, before the line
3592 // search consumes them.
3593 if let Some(o) = self.debug_stop(crate::debug::Checkpoint::AfterSearchDirection) {
3594 return o;
3595 }
3596
3597 // Fine-grained time-budget gate (pounce#242). The KKT
3598 // factorization (and any inertia-correction / quality-escalation
3599 // refactorizations) is the single most expensive step of a large
3600 // solve, and it has just finished. Check the deadline here so an
3601 // over-budget solve returns its current best iterate *before*
3602 // spending a line search and another whole iteration — bounding
3603 // the overshoot to roughly one search-direction computation
3604 // instead of a full outer iteration. `data.curr` is untouched by
3605 // the step computation (the trial lives in `data.trial`), so it
3606 // still holds the last accepted iterate.
3607 if let Some(ret) = self.deadline_status() {
3608 return IterateOutcome::Terminate(ret);
3609 }
3610
3611 // 6. Acceptable trial point — run the line search if we have a
3612 // primal/dual step on `data.delta`. Wrap in a guard so all
3613 // early-return paths (ErrorInStepComputation, InternalError,
3614 // restoration entry) still stop the timer.
3615 let _ls_guard = timing.compute_acceptable_trial_point.guard();
3616 let have_delta = self.data.borrow().delta.is_some();
3617 if have_delta {
3618 let delta = match self.data.borrow().delta.as_ref().cloned() {
3619 Some(d) => d,
3620 None => {
3621 return IterateOutcome::Terminate(SolverReturn::ErrorInStepComputation);
3622 }
3623 };
3624 // Cap alpha by the primal fraction-to-the-boundary so the
3625 // first trial cannot push slacks past their bounds, and by
3626 // the dual FTB so bound multipliers stay positive. Mirrors
3627 // upstream `IpBacktrackingLineSearch::FindAcceptableTrialPoint`'s
3628 // calls to `IpCq.primal_frac_to_the_bound` /
3629 // `IpCq.dual_frac_to_the_bound` with τ = `curr_tau`.
3630 let tau = self.data.borrow().curr_tau;
3631 let mut alpha_p_max = self.cq.borrow().aff_step_alpha_primal_max(&delta, tau);
3632 let mut alpha_d_max = self.cq.borrow().aff_step_alpha_dual_max(&delta, tau);
3633 // gh#1098: centrality-floored step to the boundary (γ > 0).
3634 if self.ftb_centrality_floor > 0.0 {
3635 let mu = self.data.borrow().curr_mu;
3636 if let Some((ap, ad)) = crate::ftb_floor::floored_alphas(
3637 &self.cq,
3638 &delta,
3639 mu,
3640 self.ftb_centrality_floor,
3641 tau,
3642 ) {
3643 alpha_p_max = ap;
3644 alpha_d_max = ad;
3645 }
3646 }
3647
3648 // Tiny-step gate — port of `IpBacktrackingLineSearch.cpp:363`
3649 // and the handling block at lines 382-435. When the search
3650 // direction is so small that any nonzero α would just
3651 // bounce inside floating-point noise, take the FTB step
3652 // unchecked and skip the line search; that's the only way
3653 // to hit `STOP_AT_TINY_STEP` cleanly when the iterate is
3654 // already at a converged point but `nlp_error > tol` due to
3655 // scaling or unbounded duals.
3656 // gh#884 — record the direction's scale-relative magnitude for
3657 // the dual-divergence-retry signature. Done here, where `delta`
3658 // is already in hand, rather than recomputed at the gate.
3659 self.last_step_rel = self.scale_relative_step_max(&delta);
3660
3661 if self.detect_tiny_step(&delta) {
3662 let alpha_p = alpha_p_max;
3663 let alpha_d = alpha_d_max;
3664 let curr = match self.data.borrow().curr.clone() {
3665 Some(c) => c,
3666 None => return IterateOutcome::Terminate(SolverReturn::InternalError),
3667 };
3668 let trial_iv = scaled_step_unchecked(&curr, &delta, alpha_p, alpha_d);
3669 {
3670 let mut d = self.data.borrow_mut();
3671 d.set_trial(trial_iv);
3672 d.info_alpha_primal = alpha_p;
3673 d.info_alpha_dual = alpha_d;
3674 d.info_ls_count = 0;
3675 if self.tiny_step_last_iteration {
3676 d.info_alpha_primal_char = 'T';
3677 d.tiny_step_flag = true;
3678 } else {
3679 d.info_alpha_primal_char = 't';
3680 }
3681 }
3682 let dy_amax = delta.y_c.amax().max(delta.y_d.amax());
3683 let y_settled = dy_amax < self.tiny_step_y_tol;
3684 // Upstream waits for the multiplier step before it lets a
3685 // tiny primal step stop the solve, so that multipliers still
3686 // converging are not cut off. Multipliers that are not
3687 // converging never satisfy it, and the solve repeats the same
3688 // iterate until max_iter. Bound the wait by dual progress:
3689 // see `tiny_step_dual_stall_iters` in `upstream_options.rs`.
3690 let du = self.cq.borrow().curr_dual_infeasibility_max();
3691 let first = !self.tiny_stall.released();
3692 let stalled = self
3693 .tiny_stall
3694 .observe(y_settled, self.curr_iterate_acceptable, du);
3695 if stalled && first {
3696 tracing::debug!(target: "pounce::algorithm",
3697 "[POUNCE] tiny-step stall at iter {}: tiny primal steps \
3698 without dual progress (multiplier step {:.2e}); the \
3699 tiny-step stop no longer waits for it.",
3700 self.data.borrow().iter_count, dy_amax,
3701 );
3702 }
3703 self.tiny_step_last_iteration = y_settled || stalled;
3704 } else {
3705 self.tiny_step_last_iteration = false;
3706 self.tiny_stall.not_tiny();
3707 let alpha_init = self.alpha_init.min(alpha_p_max);
3708 let alpha_dual = self.alpha_init.min(alpha_d_max);
3709 let outcome = self.bundle.line_search.find_acceptable_trial_point(
3710 &self.data,
3711 &self.cq,
3712 &delta,
3713 alpha_init,
3714 alpha_dual,
3715 self.nlp.as_ref(),
3716 self.search_dir.as_mut(),
3717 );
3718 match outcome {
3719 Outcome::Accepted => {
3720 // A normal LS-accepted step breaks any in-flight
3721 // restoration cycle — clear the cycle detector
3722 // so the next resto entry starts fresh.
3723 self.last_resto_entry_x = None;
3724 self.last_resto_entry_s = None;
3725 self.last_resto_recovery_x = None;
3726 self.last_resto_recovery_s = None;
3727 self.resto_no_outer_progress_count = 0;
3728 // Intentionally *not* clearing
3729 // `resto_near_feasible_count` here: DECONVBNE's
3730 // cycle interleaves R-recoveries with 2-3
3731 // LS-accepted 'f'/'h' steps (which return
3732 // `Outcome::Accepted` but accomplish no real
3733 // outer progress — alpha drops to 1e-6 and
3734 // inf_du remains pinned at 1.9e7), so resetting
3735 // on every accept would zero the counter every
3736 // cycle and never fire. The counter persists
3737 // for the duration of the run and trips after
3738 // 3 cumulative near-feasible entries; legitimate
3739 // solves enter resto at most once at near-
3740 // feasibility (POLAK6, HAIFAM) and stay under
3741 // the limit.
3742 }
3743 Outcome::TinyStep | Outcome::Failed => {
3744 // Debugger stop: the line search rejected the step
3745 // (tiny-step floor or all backtracks failed), before
3746 // we fall into restoration. Lets a "why did the line
3747 // search give up?" inspection happen at the failing
3748 // point distinctly from the restoration entry.
3749 if let Some(o) = self.debug_stop(crate::debug::Checkpoint::StepRejected) {
3750 return o;
3751 }
3752 // Upstream `IpBacktrackingLineSearch.cpp` raises
3753 // `LINE_SEARCH_FAILED` when α drops below
3754 // `alpha_min` or all retries reject, which in
3755 // turn triggers `ActivateLineSearch` →
3756 // restoration.
3757 return self.invoke_restoration_debugged();
3758 }
3759 Outcome::Deadline => {
3760 // The time budget was crossed inside the line
3761 // search (pounce#242). No trial was promoted, so
3762 // `data.curr` still holds the best iterate; stop
3763 // with the matching time-limit status. Re-derive
3764 // wall vs CPU from the deadline (it can only still
3765 // be exceeded — time is monotonic).
3766 return IterateOutcome::Terminate(
3767 self.deadline_status()
3768 .unwrap_or(SolverReturn::WallTimeExceeded),
3769 );
3770 }
3771 }
3772 }
3773 }
3774
3775 // End the line-search/trial timer here so the bookkeeping in
3776 // steps 7-8 below is attributed to `accept_trial_point` (which
3777 // mirrors upstream's split: filter update and FTB reset are
3778 // accept-side, not line-search-side).
3779 _ls_guard.stop();
3780
3781 // 7. Accept trial point (promotes `trial` to `curr` if set).
3782 // The acceptor's filter has already been augmented (when
3783 // appropriate) inside `find_acceptable_trial_point` via
3784 // `update_for_next_iteration`, mirroring upstream's call
3785 // chain in `IpBacktrackingLineSearch.cpp:839`.
3786 let _accept_guard = timing.accept_trial_point.guard();
3787
3788 // 7a. Safe-slack bound adjustment. Before promoting `trial`, move
3789 // any `x_L/x_U/d_L/d_U` whose trial slack fell below
3790 // `eps*min(1,mu)` so the slack becomes representable (port of
3791 // the bound-adjustment block in
3792 // `IpoptAlgorithm::AcceptTrialPoint`, `IpIpoptAlg.cpp:664-706`).
3793 self.adjust_variable_bounds_for_small_slacks();
3794
3795 // An unreleased hand-back's shot is taken by the first step the
3796 // original globalization accepts (gh#1076); a soft-restoration `s`
3797 // step is accepted on the primal-dual error instead, because the
3798 // filter rejected it.
3799 if self.data.borrow().info_alpha_primal_char != 's' {
3800 self.unreleased_shot_pending = false;
3801 self.unreleased_shot_strikes = 0;
3802 }
3803 self.data.borrow_mut().accept_trial_point();
3804
3805 // 8. Bound multiplier kappa_sigma reset.
3806 self.correct_bound_multiplier();
3807
3808 // 8b. `recalc_y` — re-estimate the equality/inequality
3809 // multipliers by least squares once the iterate is feasible
3810 // enough (`IpIpoptAlg.cpp:AcceptTrialPoint`). Off unless the
3811 // user asks; see `application.rs` for why we do not turn it
3812 // on for L-BFGS the way upstream's option text says it does.
3813 //
3814 // Ordering: this runs after the kappa_sigma reset. The two
3815 // are not obviously independent — the least-square RHS is
3816 // `−∇f + Pₗz_L − Pᵤz_U` (`IpLeastSquareMults.cpp:54`), so it
3817 // reads the bound multipliers step 8 just corrected — but
3818 // running the sweep with the two swapped produces a
3819 // byte-identical corpus, so the coupling does not bite in
3820 // practice. Kept here on the argument that `y` should be
3821 // estimated against the multipliers the iteration actually
3822 // ends with.
3823 self.maybe_recalc_y();
3824
3825 // 8c. Square-problem multipliers. `IpIpoptAlg.cpp:409` runs this
3826 // between `AcceptTrialPoint` and the next `CheckConvergence`,
3827 // on every iteration, for square problems only. pounce's
3828 // `iterate()` boundary falls between those two — the outer
3829 // loop bumps `iter_count` and the next `iterate()` opens with
3830 // the convergence check — so this is the same slot, and the
3831 // `+ 1` inside is that pending bump (upstream increments
3832 // before the call, at `IpIpoptAlg.cpp:407`).
3833 if self.is_square_problem() {
3834 self.compute_feasibility_multipliers();
3835 }
3836
3837 // Sub-iteration checkpoint: the trial point was accepted; α and
3838 // the new iterate are in place (before the loop's iter bookkeeping
3839 // and the next `IterStart`).
3840 drop(_accept_guard);
3841 if let Some(o) = self.debug_stop(crate::debug::Checkpoint::AfterStep) {
3842 return o;
3843 }
3844
3845 IterateOutcome::Continue
3846 }
3847
3848 /// `max(max_i |δx_i|/(1+|x_i|), max_i |δs_i|/(1+|s_i|))` — the same
3849 /// scale-relative measure [`Self::detect_tiny_step`] thresholds, kept
3850 /// as a magnitude.
3851 ///
3852 /// Scale-relative rather than a bare `‖d‖` on purpose: a bare norm is
3853 /// a length in the model's units, so on a badly scaled model it says
3854 /// more about the units than about whether the iterate has stopped
3855 /// moving. gh#884's signature needs the latter.
3856 fn scale_relative_step_max(&self, delta: &crate::iterates_vector::IteratesVector) -> Number {
3857 let curr = match self.data.borrow().curr.clone() {
3858 Some(c) => c,
3859 None => return Number::INFINITY,
3860 };
3861
3862 let mut tmp = curr.x.make_new_copy();
3863 tmp.element_wise_abs();
3864 tmp.add_scalar(1.0);
3865 let mut tmp2 = delta.x.make_new_copy();
3866 tmp2.element_wise_divide(&*tmp);
3867 let mut worst = tmp2.amax();
3868
3869 if curr.s.dim() > 0 {
3870 let mut tmp = curr.s.make_new_copy();
3871 tmp.element_wise_abs();
3872 tmp.add_scalar(1.0);
3873 let mut tmp2 = delta.s.make_new_copy();
3874 tmp2.element_wise_divide(&*tmp);
3875 worst = worst.max(tmp2.amax());
3876 }
3877 worst
3878 }
3879
3880 /// Whether this solve ever saw gh#884's dual-divergence-at-a-settled-primal
3881 /// signature. Sticky; see [`Self::dual_divergence_signature`].
3882 /// Whether the tiny-step stall rule released the tiny-step stop in this
3883 /// solve; see `tiny_step_dual_stall_iters`.
3884 pub fn tiny_step_dual_stall(&self) -> bool {
3885 self.tiny_stall.released()
3886 }
3887
3888 pub fn dual_divergence_signature(&self) -> bool {
3889 self.dual_divergence_signature
3890 }
3891
3892 /// Whether this solve ended on the gh#1042 exit: the outer re-entered
3893 /// restoration from an unreleased converged-to-feasible hand-back
3894 /// without leaving it. Read by the second-opinion ladder, which opens
3895 /// its `mu_strategy=adaptive` rung on that `Restoration_Failed` and on
3896 /// no other (see `SecondOpinionTrigger::UnreleasedHandback`).
3897 pub fn unreleased_handback_exit(&self) -> bool {
3898 self.unreleased_handback_exit
3899 }
3900
3901 /// gh#990 item 13: whether a strict termination certificate was refused
3902 /// because the objective scaling masked it (sticky for the solve).
3903 pub fn obj_scale_certificate_refused(&self) -> bool {
3904 self.vetoed_seen
3905 }
3906
3907 /// Same, for an acceptable-level certificate.
3908 pub fn obj_scale_acceptable_refused(&self) -> bool {
3909 self.vetoed_acceptable_seen
3910 }
3911
3912 /// Port of `IpBacktrackingLineSearch::DetectTinyStep`
3913 /// (`IpBacktrackingLineSearch.cpp:1219-1278`). Returns true iff
3914 /// `max_i |δx_i|/(1+|x_i|) ≤ tiny_step_tol`,
3915 /// `max_i |δs_i|/(1+|s_i|) ≤ tiny_step_tol`, AND
3916 /// `curr_constraint_violation ≤ 1e-4`. Disabled when
3917 /// `tiny_step_tol == 0`.
3918 fn detect_tiny_step(&self, delta: &crate::iterates_vector::IteratesVector) -> bool {
3919 if self.tiny_step_tol == 0.0 {
3920 return false;
3921 }
3922 let curr = match self.data.borrow().curr.clone() {
3923 Some(c) => c,
3924 None => return false,
3925 };
3926
3927 // |x_i|+1
3928 let mut tmp = curr.x.make_new_copy();
3929 tmp.element_wise_abs();
3930 tmp.add_scalar(1.0);
3931 // |δx_i|/(|x_i|+1) ; checked via Amax of (δx ./ (|x|+1)).
3932 let mut tmp2 = delta.x.make_new_copy();
3933 tmp2.element_wise_divide(&*tmp);
3934 if tmp2.amax() > self.tiny_step_tol {
3935 return false;
3936 }
3937
3938 if curr.s.dim() > 0 {
3939 let mut tmp = curr.s.make_new_copy();
3940 tmp.element_wise_abs();
3941 tmp.add_scalar(1.0);
3942 let mut tmp2 = delta.s.make_new_copy();
3943 tmp2.element_wise_divide(&*tmp);
3944 if tmp2.amax() > self.tiny_step_tol {
3945 return false;
3946 }
3947 }
3948
3949 let cviol = self.cq.borrow().curr_constraint_violation();
3950 if cviol > 1e-4 {
3951 return false;
3952 }
3953 true
3954 }
3955
3956 /// Re-anchor the quasi-Newton model instead of handing off to
3957 /// restoration, when the line search has failed at a point
3958 /// restoration cannot improve (gh#818). Returns `true` if the model
3959 /// was re-anchored, in which case the caller retries this iterate.
3960 ///
3961 /// **The two failures the line search cannot tell apart.** When no
3962 /// trial step is acceptable, either the *point* is bad — infeasible,
3963 /// and restoration is exactly the right tool — or the *direction* is,
3964 /// because `W` is a quasi-Newton model carrying curvature the iterate
3965 /// has left behind. Upstream has one answer for both, because
3966 /// restoration is the only fallback it has. At an already-feasible
3967 /// point that answer is a no-op: the restoration NLP minimizes the
3968 /// constraint violation, and there is none to minimize, so it wanders
3969 /// at `theta ~ 1e-13` and reports `Restoration_Failed`.
3970 ///
3971 /// Measured on the `deb7` fixture under `limited-memory`: the solve
3972 /// stalls with `inf_pr ~ 1e-12` and `inf_du ~ 1e5`, enters
3973 /// restoration at a point feasible to 8e-13, and spends 340 of its
3974 /// 1242 iterations there before failing. On the unconstrained
3975 /// gh#818 quadratic under `alpha_red_factor 0.8` it is starker
3976 /// still — `theta` is identically zero, so restoration cannot move
3977 /// at all, and the solve dies at **iteration 1** with
3978 /// `Error_In_Step_Computation` and the objective still at its
3979 /// starting value.
3980 ///
3981 /// So this is a rung, not a refusal: it fires only where restoration
3982 /// has nothing to reduce, it is bounded, and every path that reached
3983 /// restoration before still reaches it once the rung is spent.
3984 ///
3985 /// **Deliberately *after* the acceptable-point decline.** The call
3986 /// site is inside [`Self::invoke_restoration`], immediately behind
3987 /// that decline, and not at the `Outcome::Failed` arm in
3988 /// [`Self::iterate`] where the hand-off is decided — `eigena2` and
3989 /// `csfi2` reach the hand-off at feasible points that already pass
3990 /// the acceptable tolerances, and those must go on being reported
3991 /// rather than re-anchored and continued. Being inside
3992 /// `invoke_restoration` means the `PreRestoration` debug checkpoint
3993 /// fires ahead of a rung that then does not enter restoration; that
3994 /// is the price of the ordering and is the checkpoint's documented
3995 /// meaning ("just before entry"), not a promise that entry follows.
3996 ///
3997 /// **And deliberately not a feasibility gate on restoration itself.**
3998 /// That was tried and rejected before (see the `constr_viol_tol`
3999 /// paragraph in [`Self::invoke_restoration`]): feasible entries are
4000 /// ordinary, and nothing observable at the doorway separates a
4001 /// restoration that recovers from one that does not. This rung does
4002 /// not decide that question — it spends one cheap retry on the
4003 /// hypothesis that the model, not the point, is at fault, and hands
4004 /// over unchanged if the retry fails too.
4005 ///
4006 /// The bound is structural as well as counted. `reanchor` returns
4007 /// `false` once the history is down to its newest pair, so a second
4008 /// failure at the same iterate finds nothing to give up and falls
4009 /// through; refilling the history takes accepted steps, so the
4010 /// counter only advances once per genuine stall.
4011 /// `limited_memory_ls_failure_restarts` caps the total.
4012 fn try_reanchor_before_restoration(&mut self) -> bool {
4013 if self.lbfgs_ls_failure_restarts == 0
4014 || self.lbfgs_ls_restarts_used >= self.lbfgs_ls_failure_restarts
4015 {
4016 return false;
4017 }
4018 // Restoration's objective is the constraint violation. Only
4019 // stand in front of it where that objective is already at its
4020 // floor, so the rung can never pre-empt a restoration that had
4021 // real work to do. `constr_viol_tol` is the same tolerance the
4022 // convergence check calls feasible.
4023 let theta = self.cq.borrow().curr_constraint_violation();
4024 if !(theta <= self.bundle.conv_check.constr_viol_tol_or_default()) {
4025 return false;
4026 }
4027 if !self.bundle.hess.reanchor() {
4028 return false;
4029 }
4030 self.lbfgs_ls_restarts_used += 1;
4031 // 'Wa' alongside the updater's own 'Wr' (the
4032 // `limited_memory_max_skipping` reset), so the two re-anchorings
4033 // are distinguishable in the iteration table rather than both
4034 // reading as "the Hessian did something".
4035 self.data.borrow_mut().append_info_string("Wa");
4036 tracing::debug!(target: "pounce::algorithm",
4037 "[POUNCE] line search failed at a feasible point (theta {:.3e}); re-anchoring \
4038 the limited-memory Hessian on its newest curvature pair and retrying instead \
4039 of entering restoration, which has nothing to reduce here (gh#818). \
4040 Restart {} of {}.",
4041 theta, self.lbfgs_ls_restarts_used, self.lbfgs_ls_failure_restarts,
4042 );
4043 true
4044 }
4045
4046 /// Drive the restoration phase after a line-search failure.
4047 /// Returns `IterateOutcome::Continue` if the restoration driver
4048 /// recovered (the algorithm carries on from the recovered iterate);
4049 /// otherwise terminates with [`SolverReturn::RestorationFailure`].
4050 /// Mirrors upstream's
4051 /// `IpBacktrackingLineSearch::ActivateLineSearch` → `PerformRestoration`
4052 /// chain.
4053 fn invoke_restoration(&mut self) -> IterateOutcome {
4054 // Consumed on every entry, whichever exit below is taken, so a
4055 // flag set by one entry can never exempt a later one.
4056 let entry_nonfinite_w = std::mem::take(&mut self.resto_entry_nonfinite_w);
4057 // Snapshot the outer reference iterate's `(theta, barr)` and
4058 // build the orig-progress callback the inner IPM will consult
4059 // at every iteration (mirrors upstream
4060 // `IpRestoFilterConvCheck::SetOrigLSAcceptor` plus
4061 // `IpFilterLSAcceptor::Reset`'s `reference_*_` snapshot).
4062 let reference_theta = self.cq.borrow().curr_constraint_violation();
4063 let reference_barr = self.cq.borrow().curr_barrier_obj();
4064
4065 if std::env::var("POUNCE_DBG_RESTO").is_ok() {
4066 let iter = self.data.borrow().iter_count;
4067 tracing::debug!(target: "pounce::algorithm",
4068 "RESTO_ENTRY iter={} theta={:.6e} barr={:.6e} near_feas_ct={}",
4069 iter, reference_theta, reference_barr, self.resto_near_feasible_count,
4070 );
4071 }
4072
4073 // Port gap: upstream refuses to enter restoration from an acceptable
4074 // point, and this was missing. `IpBacktrackingLineSearch.cpp:557-570`,
4075 // in the `if (!accept)` arm that hands off to restoration:
4076 //
4077 // if( CurrentIsAcceptable() )
4078 // {
4079 // THROW_EXCEPTION(ACCEPTABLE_POINT_REACHED,
4080 // "Restoration phase called at acceptable point.");
4081 // }
4082 //
4083 // The rationale is the obvious one: restoration reduces the constraint
4084 // violation, so from a point that already passes the acceptable-level
4085 // tolerances it has nothing to reduce, and entering can only risk a
4086 // reportable solution.
4087 //
4088 // What the gap cost, measured on mittelmann `qcqp1000-1nc` (n=1000):
4089 // the line search fails at iteration 187 on a point carrying the
4090 // published optimum (`-2.6628866e+07`, matching ipopt-ma57 to 9
4091 // significant figures) with overall NLP error `6.0e-8` — two orders
4092 // inside `acceptable_tol`. Restoration walked it to `theta 5e-3` and
4093 // ground out 2780 further iterations without recovering, so a solved
4094 // problem reported a failure.
4095 //
4096 // The predicate is upstream's, unmodified: acceptability alone. A
4097 // strict `constr_viol_tol` gate was tried on top and is both a
4098 // deviation and useless — at their restoration entries `qcqp1000-1nc`
4099 // sits at `theta = 6.0e-8`, `csfi2` at `1.5e-7`, `eigena2` at
4100 // `2.1e-10`, all strictly feasible, one by six orders. Nothing
4101 // observable at the doorway separates a restoration that recovers from
4102 // one that does not, which is why upstream does not try to.
4103 //
4104 // Placed ahead of the cycle detectors below rather than beside
4105 // upstream's `PrepareRestoPhaseStart()`: those detectors are a
4106 // pounce-side addition, and an acceptable point should be reported
4107 // regardless of cycle state. Filter augmentation is skipped on this
4108 // path, which is immaterial — the run stops here.
4109 //
4110 // `current_is_acceptable_with_state` is the full triplet, never
4111 // `theta` alone: gh #274, a perfectly feasible point can be
4112 // arbitrarily far from stationary (`min -exp(x) s.t. x >= 0` reaches
4113 // here with `inf_pr = 1.7e-10` and `inf_du = 8.8e+47`), and the
4114 // triplet carries `acceptable_dual_inf_tol` to reject it. The
4115 // finiteness check mirrors the one below (CUTE `himmelbj` reaches a
4116 // near-feasible point where `f` evaluates to NaN) and matches
4117 // upstream's own `curr_f` precondition for acceptability.
4118 let (entry_f_finite, entry_nlp_err) = {
4119 let cq = self.cq.borrow();
4120 (cq.curr_f().is_finite(), cq.curr_nlp_error())
4121 };
4122 //
4123 // What the guard still did not ask is whether the solve was *converging*
4124 // (gh #534). It reads the entry point and nothing about the trajectory
4125 // that reached it, so it stops a contracting endgame and a dead stall
4126 // with equal confidence. On `eigena2` it fires while the dual
4127 // infeasibility is quartering every iteration on unit steps
4128 // (`1.19e-5 → 2.96e-6 → 7.38e-7 → 1.84e-7`), three iterations short of a
4129 // strict certificate that costs nothing but those three iterations.
4130 // [`Self::may_defer_acceptable_decline`] adds that missing question,
4131 // and only that: when the answer is no — `eigenb2`'s tail rises, and
4132 // `csfi2`'s last two iterations are flat to three digits — the guard
4133 // fires exactly as before.
4134 if entry_f_finite
4135 && self.bundle.conv_check.current_is_acceptable_with_state(
4136 entry_nlp_err,
4137 &self.data,
4138 &self.cq,
4139 )
4140 {
4141 if self.may_defer_acceptable_decline() {
4142 tracing::debug!(target: "pounce::algorithm",
4143 "[POUNCE] deferring the restoration decline at theta {:.3e}: the entry \
4144 point passes the acceptable-level tolerances (nlp_err {:.3e}) but the \
4145 NLP error has contracted every iteration over the last {} \
4146 ({:.3e} -> {:.3e}); continuing for up to {} iterations, with that point \
4147 held as the floor (gh #534).",
4148 reference_theta, entry_nlp_err, DECLINE_PROGRESS_SAMPLES - 1,
4149 self.nlp_err_recent[0], entry_nlp_err, DECLINE_CONTINUATION_BUDGET,
4150 );
4151 } else {
4152 // The window is on the line because "why did the guard not
4153 // defer?" is the first question anyone reading this trace has
4154 // (gh #534), and reconstructing it from the iteration table
4155 // means recomputing the scaled aggregate by hand.
4156 tracing::debug!(target: "pounce::algorithm",
4157 "[POUNCE] declining restoration at theta {:.3e}: the entry point already \
4158 passes the acceptable-level tolerances (nlp_err {:.3e}); reporting it \
4159 rather than risking it in restoration. Recent NLP errors {} \
4160 (contracting: {}).",
4161 reference_theta, entry_nlp_err,
4162 self.nlp_err_window_str(), self.nlp_err_contracting(),
4163 );
4164 return IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint);
4165 }
4166 }
4167
4168 // gh#818 — one rung before the hand-off proper: re-anchor the
4169 // quasi-Newton model and retry this iterate. Placed *here*, and
4170 // not at the `Outcome::Failed` arm in `iterate`, because it has
4171 // to run behind the acceptable-point decline above: `eigena2`
4172 // and `csfi2` arrive at feasible points that already pass the
4173 // acceptable tolerances, and those must go on being reported
4174 // rather than re-anchored and continued. The gh#534 deferral
4175 // path falls through to here, which is the right order too —
4176 // the deferral has already captured its floor, so a rung taken
4177 // under a live deferral is protected by it.
4178 if self.try_reanchor_before_restoration() {
4179 return IterateOutcome::Continue;
4180 }
4181
4182 // No-progress restoration cycle detector. Two layered checks
4183 // surface as `ErrorInStepComputation` instead of cycling to
4184 // `max_iter` exhaustion (mirrors the *intent* of upstream
4185 // `IpBacktrackingLineSearch.cpp:580-600`'s almost-feasible
4186 // resto guard):
4187 //
4188 // 1. *Static cycle*: entry-to-entry — when the curr `(x, s)`
4189 // at this entry is essentially identical to the snapshot
4190 // from the previous entry, the inner resto-IPM is
4191 // returning recovered iterates indistinguishable from
4192 // entry, AND the outer didn't move either. Fires
4193 // immediately. Catches QCNEW, EQC, MESH, POLAK6, S365,
4194 // S365MOD, SIPOW2M, PFIT4.
4195 //
4196 // 2. *Slow-progress cycle*: recovery-to-entry — when curr at
4197 // this entry is essentially identical to the *recovery*
4198 // iterate from the previous resto, the outer made no
4199 // progress between resto invocations even though resto's
4200 // inner moved substantively. Counted, fires after 5
4201 // consecutive entries. Catches ACOPR14, ACOPR30, TRO3X3
4202 // while letting MAKELA3, HAIFAM, HALDMADS, ROBOT,
4203 // TENBARS2 — which need 2-3 productive resto entries
4204 // before LS accepts — pass through.
4205 //
4206 // A productive single-restoration sequence (BT8, HIMMELBJ,
4207 // LINSPANH, LSNNODOC, ODFITS, OET3) clears both snapshots via
4208 // `Outcome::Accepted` between entries and is unaffected.
4209 let curr = self
4210 .data
4211 .borrow()
4212 .curr
4213 .as_ref()
4214 .expect("curr set before invoke_restoration")
4215 .clone();
4216 // Helper: when the cycle detector fires and the orig cv is a
4217 // violation the *user* calls a violation (e.g. PFIT1's 2.73e-2),
4218 // the outer is stuck at a feasibility-stationary point and the
4219 // honest exit is `LocalInfeasibility`. Below that threshold the
4220 // iterate is primal-feasible by the user's own declaration, so there
4221 // is no infeasibility to certify — the failure is numerical, not
4222 // algorithmic, and `ErrorInStepComputation` is retained.
4223 //
4224 // The threshold is `constr_viol_tol`, and *only* `constr_viol_tol`
4225 // (gh #508). The question this ternary asks — "is this violation
4226 // real?" — is a question about the constraint violation, so it has to
4227 // be asked with the option that declares what a violated constraint
4228 // is. The previous form, `max(100·tol, 1e-4)`, was built from `tol`, a
4229 // tolerance on the **KKT error**: different quantity, different units,
4230 // and it never consulted `constr_viol_tol` at all. Two consequences,
4231 // both measured on `min (x-5)² s.t. x²+δ = 0` (infeasible for every
4232 // δ>0, reported violation exactly δ):
4233 //
4234 // * sweeping `constr_viol_tol` over four orders moved the boundary
4235 // not at all — at `constr_viol_tol = 1e-3` a violation of `1e-4`,
4236 // comfortably inside the user's declared feasibility tolerance,
4237 // still exited 500;
4238 // * sweeping `tol` moved it a great deal, and in the wrong
4239 // direction: at `tol = 1e-4` the `1e-2` threshold swallowed every
4240 // δ from `3e-4` to `1e-2` — a model infeasible by a full percent
4241 // answered "your solver broke". Loosening `tol` is the standard
4242 // user reaction to a struggling solve, so the failure widened
4243 // exactly when the user tried to help.
4244 //
4245 // No `infeas_viol_kappa` margin on top, unlike the rapid-infeasibility
4246 // pre-filter in `conv_check`. That detector fires *during* the solve
4247 // off a streak heuristic and needs the margin to avoid convicting an
4248 // iterate that is still converging; here restoration has already
4249 // demonstrably cycled, so the certainty comes from the cycle evidence
4250 // rather than from extra violation headroom. Widening to
4251 // `kappa·constr_viol_tol` would move the default threshold from `1e-4`
4252 // to `1e-2` and hand back 500 on the whole band in between.
4253 //
4254 // The comparison is `>=`, not `>`. A violation landing exactly on the
4255 // threshold is a violation at the user's declared tolerance, and the
4256 // reproducer above hits the boundary to the digit (`δ = 1e-4` at the
4257 // default `constr_viol_tol`), where `>` returned 500 for a model
4258 // infeasible by precisely the amount the user said was too much.
4259 //
4260 // The violation is measured **unscaled**. `reference_theta` is the
4261 // row-scaled residual, but the floor below is an absolute, user-facing
4262 // magnitude, so comparing the two mixes unit systems — and on a problem
4263 // whose rows are scaled down the scaled residual can never clear it.
4264 // `infeasible_equalities.nl` is the worked example: a square 2x2 system
4265 // with a true violation of 2.0 that NLP scaling reports as 6.67e-7, so
4266 // this test read `6.67e-7 > 1e-4` = false and a blatantly infeasible
4267 // model exited `Error_In_Step_Computation` (AMPL 500, Pyomo
4268 // `internalSolverError`). Square problems have no restoration-side
4269 // locally-infeasible gate — `strict` carves them out so the outer gets
4270 // another shot — so this cycle exit *is* their safety net, and it was
4271 // disabled by the unit mismatch. Same user-visible family as gh #372.
4272 //
4273 // Note this also moves from a 1-norm (`curr_constraint_violation`) to a
4274 // max-norm. Max-norm <= 1-norm, so the test is marginally stricter
4275 // about declaring infeasibility on an unscaled problem — the safe
4276 // direction for a verdict this consequential.
4277 //
4278 // `theta > 0` in front of the `>=` is not redundant: the options layer
4279 // registers `constr_viol_tol` with a *strict* lower bound of zero, but
4280 // a library embedder setting `ConvCheckOptions` directly is not bound
4281 // by that, and `0 >= 0` would turn an exactly-feasible iterate into an
4282 // infeasibility certificate. A zero violation never proves anything.
4283 //
4284 // And the violation is the **model's** — `|c(x)|` and
4285 // `dist(d(x), [d_l, d_u])` against the declared bounds and box — not
4286 // the slack reformulation's `‖(c, d − s)‖` (gh#1146). `s` is confined
4287 // to `[d_l, d_u]`, so a gap `d − s` can be any size while `d(x)` sits
4288 // inside its bounds: an iterate the user's own rows call feasible
4289 // would be certified infeasible on the slack's say-so. The model
4290 // measure is never larger than the slack one, so this can only move a
4291 // cycle exit from `LocalInfeasibility` to `ErrorInStepComputation`,
4292 // and only at a point that satisfies the model to `constr_viol_tol`.
4293 let cycle_viol_tol = self.bundle.conv_check.constr_viol_tol_or_default();
4294 let reference_theta_unscaled = self.cq.borrow().curr_declared_primal_violation_max();
4295 let cycle_exit =
4296 if reference_theta_unscaled > 0.0 && reference_theta_unscaled >= cycle_viol_tol {
4297 SolverReturn::LocalInfeasibility
4298 } else {
4299 SolverReturn::ErrorInStepComputation
4300 };
4301 let static_cycle = if let (Some(prev_x), Some(prev_s)) = (
4302 self.last_resto_entry_x.as_ref(),
4303 self.last_resto_entry_s.as_ref(),
4304 ) {
4305 let dx_rel = relative_distance(&*curr.x, &**prev_x);
4306 let ds_rel = relative_distance(&*curr.s, &**prev_s);
4307 if std::env::var_os("POUNCE_DBG_RESTO_CYCLE").is_some() {
4308 tracing::debug!(target: "pounce::algorithm",
4309 "[PN_RESTO_CYCLE] entry-vs-entry dx_rel={:.6e} ds_rel={:.6e}",
4310 dx_rel, ds_rel
4311 );
4312 }
4313 dx_rel <= 1e-10 && ds_rel <= 1e-10
4314 } else {
4315 false
4316 };
4317 if static_cycle {
4318 // Prefer the last acceptable point over the cycle error —
4319 // the borrows above are released, so the `&mut self` helper
4320 // is free to roll back.
4321 return self.terminate_acceptable_or(cycle_exit);
4322 }
4323 let recovery_cycle = if let (Some(prev_x), Some(prev_s)) = (
4324 self.last_resto_recovery_x.as_ref(),
4325 self.last_resto_recovery_s.as_ref(),
4326 ) {
4327 let dx_rel = relative_distance(&*curr.x, &**prev_x);
4328 let ds_rel = relative_distance(&*curr.s, &**prev_s);
4329 if std::env::var_os("POUNCE_DBG_RESTO_CYCLE").is_some() {
4330 tracing::debug!(target: "pounce::algorithm",
4331 "[PN_RESTO_CYCLE] entry-vs-recovery dx_rel={:.6e} ds_rel={:.6e} count={}",
4332 dx_rel, ds_rel, self.resto_no_outer_progress_count
4333 );
4334 }
4335 dx_rel <= 1e-10 && ds_rel <= 1e-10
4336 } else {
4337 false
4338 };
4339 // A handed-back converged-to-feasible point the outer could not
4340 // leave (gh#1042). Restoration ended a previous call on upstream's
4341 // `RESTORATION_CONVERGED_TO_FEASIBLE_POINT` condition: layer 2's
4342 // `ConvergedToFeasiblePoint` verdict (`IpRestoConvCheck.cpp:227-234`)
4343 // or an inner stop at its acceptable level with the original NLP
4344 // feasible to `1e2·tol` (`IpRestoMinC_1Nrm.cpp:278-287`). Either
4345 // way the point was feasible but never released by layer 1 — the
4346 // outer filter rejected it, or it did not cut the violation by
4347 // `kappa_resto`. Upstream fails restoration right there; pounce
4348 // hands the point back to give the outer phase one shot from it,
4349 // which paid on the `dallas*` family (every hand-back there is
4350 // followed by accepted steps, soft-restoration ones included).
4351 // Since the `.nl` reader merges `|x|^2·|x|^0.85` (gh#1042 R3),
4352 // `dallasl` and `dallasm` never enter restoration on either leg
4353 // and `dallass` hands back once, on L-BFGS only; the hand-backs
4354 // above survive in `tests/data/dallass_kink_unmerged.nl`.
4355 //
4356 // The shot was not taken when the outer asks for restoration again
4357 // from that very point. That is either no accepted step at all, or
4358 // steps a watchdog rolled back — so this compares positions, not
4359 // `Outcome::Accepted`. A soft-restoration step moves the iterate and
4360 // so counts as taking the shot (`dallasl`: `s s S`, then converges).
4361 //
4362 // One strike is the whole budget, and that is what separates this
4363 // from the 10-strike counter below. That counter's recoveries were
4364 // *released* — layer 1 judged them acceptable to the outer filter —
4365 // so a few immediate re-entries can still be the outer working its
4366 // way out (OET7). These were not; restoration has certified it cannot
4367 // do better than this point and the outer has shown it cannot leave
4368 // it. This is the upstream verdict, one restoration late.
4369 //
4370 // An entry forced by a non-finite Hessian is exempt, as it is from
4371 // the gh#274 strike below: the factorization rejected `W` before the
4372 // outer could try a step, so it says nothing about being stuck.
4373 //
4374 // The status is upstream's: `IpIpoptAlg.cpp:480-493` maps this
4375 // exception to `RESTORATION_FAILURE` on a non-square problem, and it
4376 // goes through the acceptable-point rollback the sibling exits use.
4377 // One refinement, and it is the gh#508 rule rather than a new one:
4378 // "feasible" in layer 2 means `θ ≤ 1e2·tol`, a KKT tolerance, and
4379 // under a loose `tol` that admits a point the *user's*
4380 // `constr_viol_tol` calls violated. Measured: `issue_508`'s
4381 // `min (x−5)² s.t. x²+δ = 0` at `tol=1e-3`, `δ = 1e-4` reaches this
4382 // exit at a violation of exactly `1e-4`, and the honest verdict there
4383 // is `Infeasible_Problem_Detected`, not AMPL 500. So the same
4384 // unscaled-vs-`constr_viol_tol` test `cycle_exit` uses picks the
4385 // infeasible arm; below it, the point is feasible by the user's own
4386 // declaration and the failure is restoration's. A point the user calls
4387 // feasible is never certified infeasible here. A square problem never
4388 // gets here — its feasible point leaves restoration as
4389 // `FeasiblePointFound`.
4390 //
4391 // The exit also sets `unreleased_handback_exit`, which the
4392 // second-opinion ladder reads: this `Restoration_Failed`, and no other,
4393 // opens `mu_strategy=adaptive` ahead of the displaced start
4394 // (`SecondOpinionTrigger::UnreleasedHandback`). Before the status was
4395 // upstream's, this exit reported `Error_In_Step_Computation`, whose one
4396 // rung is adaptive, and that rung is what recovered brainpc0/1/2.
4397 //
4398 // Measured on Vanderbei brainpc1 (FERAL 0.21): the first such
4399 // re-entry is outer iteration 378, after 13 restoration calls;
4400 // without this exit the solve ran 39 more, to 1073. brainpc0: 956
4401 // after 31 calls, against 2117 after 121.
4402 //
4403 // **The shot not taken, away from the point (gh#1076).** Comparing
4404 // positions catches an outer that cannot move. It misses one that
4405 // moves only on soft-restoration `s` steps — accepted on the
4406 // primal-dual error *because* the original filter rejects them —
4407 // and drifts back into restoration from somewhere else. That is the
4408 // same verdict: no step the outer's own globalization accepts was
4409 // ever taken from the hand-back. It became the common case once the
4410 // release test read the filter's real quantities (#1075): Vanderbei
4411 // brainpc0 then hands back a point at f ≈ 8e5 against a filter
4412 // holding f ≈ 0.38, takes `s s s` back to f ≈ 0.378, re-enters, and
4413 // repeats 45 times with the hand-back drifting (8.3e5 → 1.06e6), so
4414 // no position test fires, until a restoration sub-solve's layer-2
4415 // verdict misreads one of those feasible points as infeasible at
4416 // iteration 1678. Upstream fails restoration at the first of them
4417 // ("converged to a feasible point that is unacceptable to the
4418 // filter", 236 iterations on 3.14.19). An `S` step — soft
4419 // restoration that *also* satisfies the original criterion — or any
4420 // ordinary `f`/`h`/`w` step takes the shot (`dallasl`: `s s S`).
4421 //
4422 // The verdict is judged on the handed-back point, not the re-entry
4423 // point: the outer drifted away from a point restoration certified
4424 // feasible, so the re-entry's violation says nothing about the
4425 // model's feasibility. On the same-point arm the two coincide.
4426 let shot_not_taken = std::mem::take(&mut self.unreleased_shot_pending);
4427 let unreleased_same_point = if let (Some(px), Some(ps)) = (
4428 self.last_resto_unreleased_x.as_ref(),
4429 self.last_resto_unreleased_s.as_ref(),
4430 ) {
4431 relative_distance(&*curr.x, &**px) <= 1e-10
4432 && relative_distance(&*curr.s, &**ps) <= 1e-10
4433 } else {
4434 false
4435 };
4436 // Counted, not one-shot: see [`Self::UNRELEASED_SHOT_STRIKES`]. A
4437 // non-finite-W entry neither counts nor clears the run.
4438 if shot_not_taken && !entry_nonfinite_w {
4439 self.unreleased_shot_strikes += 1;
4440 }
4441 let shot_not_taken =
4442 shot_not_taken && self.unreleased_shot_strikes >= Self::UNRELEASED_SHOT_STRIKES;
4443 let unreleased_repeat = unreleased_same_point || shot_not_taken;
4444 if unreleased_repeat && entry_nonfinite_w {
4445 tracing::debug!(target: "pounce::algorithm",
4446 "[POUNCE] re-entered restoration from an unreleased hand-back, but on a \
4447 non-finite Hessian; exempt from the gh#1042 exit.",
4448 );
4449 }
4450 if unreleased_repeat && !entry_nonfinite_w {
4451 let handback_theta = self.last_resto_unreleased_theta_unscaled;
4452 let verdict = if handback_theta > 0.0 && handback_theta >= cycle_viol_tol {
4453 SolverReturn::LocalInfeasibility
4454 } else {
4455 SolverReturn::RestorationFailure
4456 };
4457 if unreleased_same_point {
4458 tracing::debug!(target: "pounce::algorithm",
4459 "[POUNCE] restoration handed back a converged-to-feasible point it never \
4460 released, and the outer re-entered restoration from it without leaving it \
4461 (accepted steps in between: {}); ending the solve with {:?} (gh#1042).",
4462 !recovery_cycle, verdict,
4463 );
4464 } else {
4465 tracing::debug!(target: "pounce::algorithm",
4466 "[POUNCE] restoration handed back a converged-to-feasible point it never \
4467 released, and the outer re-entered restoration without the original \
4468 globalization accepting a step from it (soft-restoration steps only); \
4469 ending the solve with {:?} (gh#1076).",
4470 verdict,
4471 );
4472 }
4473 self.unreleased_handback_exit = true;
4474 return self.terminate_acceptable_or(verdict);
4475 }
4476 if recovery_cycle {
4477 self.resto_no_outer_progress_count =
4478 self.resto_no_outer_progress_count.saturating_add(1);
4479 // 10-strike limit: tuned to give OET7-style traces room
4480 // to break through (inner inf_pr still decreasing across
4481 // strikes) while still bounding DECONVBNE-style cycles
4482 // (which need a guard but tolerate a wider window —
4483 // ~3 outer steps per cycle, so 10 strikes ≈ 30 outer
4484 // iters, well below the 2987-iter pathological run).
4485 if self.resto_no_outer_progress_count >= 10 {
4486 // Prefer the last acceptable point over the cycle error;
4487 // borrows are released, so the `&mut self` helper is free.
4488 return self.terminate_acceptable_or(cycle_exit);
4489 }
4490 } else {
4491 self.resto_no_outer_progress_count = 0;
4492 }
4493 // Near-feasible resto re-entry detector — matches the *intent*
4494 // of upstream `IpBacktrackingLineSearch.cpp:580-600`'s almost-
4495 // feasible-resto guard with a looser cv threshold. When the
4496 // outer enters restoration with the constraint violation
4497 // already at or below `tol`, the resto sub-IPM will produce a
4498 // recovered iterate that's at most marginally more feasible,
4499 // and any post-recovery σ-blowup from the next outer KKT solve
4500 // will re-trigger resto on the next iteration. Counting these
4501 // entries surfaces the cycle as `StopAtAcceptablePoint` —
4502 // primal feasibility is already met, only the dual residual
4503 // remains. Catches DECONVBNE: pounce ran 2987 iters before
4504 // this guard (cycle of ~30-inner-resto + 3 outer per cycle);
4505 // upstream solves in 505 iters via a different x trajectory.
4506 // Single-entry productive restos (BT8, HIMMELBJ, ODFITS) and
4507 // sub-tol-but-recoverable starts pass through under the 3-
4508 // strike limit.
4509 let outer_tol = self.bundle.conv_check.tol_or_default();
4510 if reference_theta <= outer_tol {
4511 if !entry_nonfinite_w {
4512 self.resto_near_feasible_count = self.resto_near_feasible_count.saturating_add(1);
4513 }
4514 if self.resto_near_feasible_count >= 3 {
4515 // Constraint feasibility is met, but a near-feasible iterate is
4516 // only "acceptable" if its objective is finite. CUTE `himmelbj`
4517 // reaches a point with cv ≈ 2e-9 where f evaluates to NaN; that
4518 // must surface as Invalid_Number_Detected rather than be
4519 // reported as Solved_To_Acceptable_Level with a `nan` objective.
4520 if !self.cq.borrow().curr_f().is_finite() {
4521 return IterateOutcome::Terminate(SolverReturn::InvalidNumberDetected);
4522 }
4523 // Constraint feasibility alone does not make a point
4524 // acceptable. `reference_theta` measures only the *primal*
4525 // residual, so a perfectly feasible iterate can still be
4526 // arbitrarily far from stationary — which is exactly what an
4527 // unbounded objective looks like from here: the constraints
4528 // stay satisfied while the iterates run off toward -inf.
4529 //
4530 // `min -exp(x) s.t. x >= 0` re-enters restoration with
4531 // `inf_pr = 1.7e-10` and `inf_du = 8.8e+47`; before gh #274
4532 // the finiteness check was the only gate, `-8.8e47` is
4533 // finite, and the solve was reported as
4534 // `Solved_To_Acceptable_Level` — which Pyomo maps into the
4535 // *solved* family, loading the diverging iterate as an
4536 // optimal solution.
4537 //
4538 // So require the point to pass the full acceptable-level
4539 // triplet (which includes `acceptable_dual_inf_tol`) before
4540 // claiming acceptability. When it does not, surface
4541 // `cycle_exit` — the same honest status the other two
4542 // restoration-cycle exits in this function use.
4543 let nlp_err = self.cq.borrow().curr_nlp_error();
4544 if !self
4545 .bundle
4546 .conv_check
4547 .current_is_acceptable_with_state(nlp_err, &self.data, &self.cq)
4548 {
4549 tracing::debug!(target: "pounce::algorithm",
4550 "[POUNCE] near-feasible restoration re-entry at theta {:.3e} \
4551 but the point fails the acceptable-level tolerances \
4552 (nlp_err {:.3e}); reporting {:?} rather than \
4553 Solved_To_Acceptable_Level (gh#274).",
4554 reference_theta, nlp_err, cycle_exit,
4555 );
4556 return IterateOutcome::Terminate(cycle_exit);
4557 }
4558 return IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint);
4559 }
4560 } else {
4561 self.resto_near_feasible_count = 0;
4562 }
4563 self.last_resto_entry_x = Some(curr.x.make_new_copy());
4564 self.last_resto_entry_s = Some(curr.s.make_new_copy());
4565
4566 // Augment the outer's filter with the resto-entry envelope —
4567 // mirrors upstream `IpBacktrackingLineSearch.cpp:566`:
4568 // `acceptor_->PrepareRestoPhaseStart()`. Adds
4569 // `((1-γ_θ)·θ_entry, φ_entry - γ_φ·θ_entry)` to the filter so
4570 // that after restoration recovers, the outer's Newton step is
4571 // forced by the filter to make real progress vs the entry
4572 // point. Without this, the outer accepts null-progress 'h'
4573 // steps and re-enters restoration on the next iteration (root
4574 // cause of DECONVBNE's 323 R-accepts vs ipopt's 21).
4575 self.bundle
4576 .line_search
4577 .acceptor_mut()
4578 .prepare_resto_phase_start(reference_theta, reference_barr);
4579
4580 let orig_progress_cb = self
4581 .bundle
4582 .line_search
4583 .acceptor()
4584 .make_orig_progress_check(reference_theta, reference_barr, 5.0)
4585 .map(|accept| self.orig_progress_check(accept));
4586
4587 let (Some(nlp), Some(sd), Some(resto)) = (
4588 self.nlp.as_ref(),
4589 self.search_dir.as_mut(),
4590 self.restoration.as_mut(),
4591 ) else {
4592 return IterateOutcome::Terminate(SolverReturn::RestorationFailure);
4593 };
4594 resto.set_orig_progress_check(orig_progress_cb);
4595 // Forward the shared debugger so it can step the inner solve.
4596 resto.set_debug_hook(self.debug.as_ref().map(Rc::clone));
4597 // Forward the user's TNLP so the callback fires from the inner
4598 // solve too (gh#645). `None` when the caller installed no
4599 // callback, which keeps the whole path inert for them.
4600 resto.set_intermediate_tnlp(self.tnlp.as_ref().map(Rc::clone));
4601 // And this solve's iteration cap, not the provider's snapshot of it.
4602 resto.set_solve_max_iter(self.max_iter);
4603 let mut pd_guard = sd.pd_solver_mut();
4604 let aug = pd_guard.aug_solver_mut();
4605 // Audit counters (pounce#12). Increment call count + outer-iter
4606 // count (one outer iter is consumed per restoration call) and
4607 // wall-time around the inner call. Inner iter count is read
4608 // after via the trait accessor.
4609 //
4610 // `outer_iter_at_entry` is captured *before* the call because the
4611 // inner IPM's counter is seeded from it (`inner.iter_count =
4612 // outer_iter + 1`, upstream `IpRestoMinC_1Nrm.cpp:181`). The
4613 // accessor hands back that seeded, absolute number; the sub-solve's
4614 // own length is the difference. Adding the raw accessor value was
4615 // gh #819's second defect — `restoration_inner_iters` was a sum of
4616 // absolute positions, a quantity with no meaning — and it is the
4617 // same misreading gh#664 records for the stall gate.
4618 let outer_iter_at_entry = self.data.borrow().iter_count;
4619 self.resto_calls = self.resto_calls.saturating_add(1);
4620 self.resto_outer_iters = self.resto_outer_iters.saturating_add(1);
4621 let resto_t0 = std::time::Instant::now();
4622 let outcome = resto.perform_restoration(&self.data, &self.cq, nlp, aug);
4623 drop(pd_guard);
4624 self.resto_wall_secs += resto_t0.elapsed().as_secs_f64();
4625 let inner_final_iter = resto.last_inner_iter_count();
4626 let recovery_unreleased = resto.last_recovery_unreleased();
4627 self.resto_inner_iters = self
4628 .resto_inner_iters
4629 .saturating_add((inner_final_iter - outer_iter_at_entry).max(0));
4630 // gh #819. Roll the reported iteration count forward over the
4631 // restoration rows on the paths that *terminate* the solve.
4632 //
4633 // `RestorationOutcome::Recovered` already does this for itself, in
4634 // `min_c_1nrm.rs`'s step 2g (`Set_iter_count(resto_iter_count - 1)`,
4635 // one short because the outer loop is about to increment). Every
4636 // other outcome returns `Terminate` from the match below without
4637 // passing through that block, so the whole sub-solve used to vanish
4638 // from the summary: on gh #815's flowsheet the log ends at row
4639 // `3000r`, above a summary that said `Number of Iterations....: 3`.
4640 //
4641 // Ipopt reports the index of the last row it printed, `r` rows
4642 // included, on every exit path. Measured on the gh #815 model and
4643 // three variants of it: last row `2418r` / reported 2418, `412r` /
4644 // 412, `1547r` / 1547, `1348r` / 1348 — exits `Restoration Failed`,
4645 // local infeasibility and `Maximum Number of Iterations Exceeded`
4646 // respectively. Assigning the absolute inner count reproduces that
4647 // rule exactly.
4648 //
4649 // Safe against trajectory: every non-`Recovered` arm below returns
4650 // `IterateOutcome::Terminate`, and `optimize_inner`'s loop breaks on
4651 // `Terminate` without consulting the counter again. Nothing reads
4652 // `iter_count` between here and the summary.
4653 if !matches!(outcome, RestorationOutcome::Recovered)
4654 && inner_final_iter > outer_iter_at_entry
4655 {
4656 self.data.borrow_mut().iter_count = inner_final_iter;
4657 }
4658 // pounce#244: the restoration inner IPM shares the outer solve's
4659 // `Deadline` (both its convergence check and — post-#244 — its KKT
4660 // solves consult it), so a budget crossing inside restoration
4661 // terminates the inner solve with a time-limit status. Surface that
4662 // as the time limit directly instead of letting the `Failed` arm map
4663 // it onto `RestorationFailure` / `StopAtAcceptablePoint`. `data.curr`
4664 // is the last accepted outer iterate — restoration stages its
4665 // recovered point onto `trial`, not `curr`, and we return before
4666 // promoting it — so this hands back a valid iterate.
4667 if let Some(ret) = self.deadline_status() {
4668 return IterateOutcome::Terminate(ret);
4669 }
4670 match outcome {
4671 RestorationOutcome::Recovered => {
4672 // Mirror upstream `IpBacktrackingLineSearch.cpp:624-631`:
4673 // a successful restoration clears the line search's
4674 // cross-iteration globalization counters. Upstream runs
4675 // restoration inside `FindAcceptableTrialPoint` so those
4676 // assignments are inline; pounce runs it here, so the
4677 // reset has to be driven from here. Without it
4678 // `watchdog_shortened_iter` survives a restoration
4679 // episode and runs of shortened steps on either side of
4680 // one accumulate as if consecutive, arming the watchdog
4681 // where upstream would not. See
4682 // `BacktrackingLineSearch::reset_after_restoration`.
4683 self.bundle.line_search.reset_after_restoration();
4684 // An unreleased hand-back from a restoration that a
4685 // non-finite Hessian forced at a point already feasible to
4686 // `tol` restarts the filter (gh#1076). Pounce enters
4687 // restoration there only to move off the kink; upstream
4688 // stops (MA57: `Invalid_Number_Detected`), and would not hand
4689 // a point that feasible to restoration in any case
4690 // (`IpBacktrackingLineSearch.cpp:580`). Restoration has no
4691 // violation to reduce, so its iterates can only *raise* the
4692 // barrier objective against the entry, and the outer filter
4693 // holds that entry's envelope at `θ ≈ 0` — which dominates
4694 // every feasible point with a higher φ. The release test is
4695 // unsatisfiable by construction, and so is every outer step
4696 // from the point handed back in its place: the one shot the
4697 // hand-back exists to give cannot be taken. Under the old
4698 // release quantities (max-norm θ against the filter's 1-norm,
4699 // plain f against the barrier objective) such points were
4700 // released by accident and the question never arose. The
4701 // reset is upstream's own `FilterLSAcceptor::Reset`, the one
4702 // a μ decrease performs. Measured on `dallass_kink_unmerged`:
4703 // Optimal at 61 with no ladder (65 on main), where the
4704 // corrected test alone ended the base solve
4705 // `Error_In_Step_Computation` at 116.
4706 //
4707 // It is scoped to the non-finite-W entry on purpose. Applied
4708 // to every feasible entry it also rescues `ssebnln` (Optimal
4709 // at 125 instead of the gh#1042 exit at 117 + 41), but it
4710 // costs `issue_884`'s reproducer its answer: under `REPRO`
4711 // options `mpcc_qpec_small_biactive` then converges with an
4712 // unscaled dual infeasibility of 3.4e-4 instead of 1.3e-10.
4713 if recovery_unreleased && entry_nonfinite_w && reference_theta <= outer_tol {
4714 tracing::debug!(target: "pounce::algorithm",
4715 "[POUNCE] unreleased hand-back from a restoration entered at a \
4716 feasible point (theta {:.3e} <= tol); restarting the filter (gh#1076).",
4717 reference_theta,
4718 );
4719 self.bundle.line_search.reset();
4720 }
4721 // The driver has staged the recovered point on
4722 // `data.trial`; apply the safe-slack bound adjustment
4723 // (as the main accept path does), then promote it and
4724 // continue iterating.
4725 self.adjust_variable_bounds_for_small_slacks();
4726 self.data.borrow_mut().accept_trial_point();
4727 // Snapshot the recovery iterate for the slow-cycle
4728 // detector at the top of the next `invoke_restoration`.
4729 // Compared against next-entry curr, dx_rel ≈ ‖α·d‖ —
4730 // measures purely the outer step. See header comment
4731 // on the cycle detector above.
4732 let recovered = self
4733 .data
4734 .borrow()
4735 .curr
4736 .as_ref()
4737 .expect("accept_trial_point sets curr")
4738 .clone();
4739 self.last_resto_recovery_x = Some(recovered.x.make_new_copy());
4740 self.last_resto_recovery_s = Some(recovered.s.make_new_copy());
4741 self.unreleased_shot_pending = recovery_unreleased;
4742 if !recovery_unreleased {
4743 self.unreleased_shot_strikes = 0;
4744 }
4745 if recovery_unreleased {
4746 self.last_resto_unreleased_x = Some(recovered.x.make_new_copy());
4747 self.last_resto_unreleased_s = Some(recovered.s.make_new_copy());
4748 // The model's violation, not the slack gap — same
4749 // reason as the cycle exit's measure (gh#1146).
4750 self.last_resto_unreleased_theta_unscaled =
4751 self.cq.borrow().curr_declared_primal_violation_max();
4752 } else {
4753 self.last_resto_unreleased_x = None;
4754 self.last_resto_unreleased_s = None;
4755 }
4756 if recovery_unreleased {
4757 tracing::debug!(target: "pounce::algorithm",
4758 "[POUNCE] restoration converged to a feasible point it did not \
4759 release; resuming the outer phase from it (gh#1042)."
4760 );
4761 }
4762 // Mirror upstream `IpoptAlgorithm::AcceptTrialPoint`
4763 // (`IpIpoptAlg.cpp:917-963`): kappa_sigma clamp on the
4764 // four bound-multiplier vectors. Upstream applies this
4765 // unconditionally inside AcceptTrialPoint, so the
4766 // post-restoration path inherits it; pounce factored
4767 // the clamp out of the data swap so we must call it
4768 // explicitly here. Without it the all-1 multiplier
4769 // reset (`bound_mult_reset_threshold`) leaves z*s far
4770 // from mu at the recovered iterate, blowing up the
4771 // next KKT solve's σ = z/s diagonal.
4772 self.correct_bound_multiplier();
4773 IterateOutcome::Continue
4774 }
4775 RestorationOutcome::Failed => {
4776 // Mirrors upstream `IpBacktrackingLineSearch.cpp:611-623`:
4777 // when `PerformRestoration` returns false, attempt to
4778 // roll back to the most recent acceptable iterate before
4779 // surfacing failure. If a snapshot is available we exit
4780 // cleanly with `StopAtAcceptablePoint` (mapped by the
4781 // application layer to `Solved_To_Acceptable_Level`),
4782 // matching the upstream `ACCEPTABLE_POINT_REACHED`
4783 // throw. Without a snapshot we surface
4784 // `RestorationFailure` — unless the restoration left the
4785 // iterate diverging (`|x|_∞ > diverging_iterates_tol`), in
4786 // which case we surface `DivergingIterates` to mirror the
4787 // outcome upstream produces on pathological problems like
4788 // MESH (where ipopt reports `Diverging_Iterates` and
4789 // pounce previously reported `Restoration_Failed` with an
4790 // obj of −3.6e+33). As in the running guard above, a large
4791 // `|x|` is only reported as unbounded when it is
4792 // structurally consistent with an unbounded feasible region
4793 // and the divergence is genuine — either it has persisted
4794 // (the running guard's growth-and-descent streak, which only
4795 // accumulates on a real recession ray; issues #248 / #252) or
4796 // blown past the absolute runaway backstop; otherwise the
4797 // failure is a plain `RestorationFailure`, never a spurious
4798 // `Unbounded`.
4799 //
4800 // gh#1000: a restoration that failed because the inner
4801 // counter — seeded from the outer one, so absolute — ran
4802 // into `max_iter` did not fail; it ran out of budget.
4803 // Upstream throws `RESTORATION_MAXITER_EXCEEDED`
4804 // (`IpRestoMinC_1Nrm.cpp`) and `IpIpoptAlg` reports
4805 // `MAXITER_EXCEEDED`, past the line search's
4806 // acceptable-point fallback. Reporting it as
4807 // `Restoration_Failed` told the caller the model defeated
4808 // the algorithm when it was the cap that stopped it.
4809 if inner_final_iter >= self.max_iter {
4810 IterateOutcome::Terminate(SolverReturn::MaxiterExceeded)
4811 } else if self.restore_acceptable_point() {
4812 IterateOutcome::Terminate(SolverReturn::StopAtAcceptablePoint)
4813 } else {
4814 let diverging = {
4815 let data = self.data.borrow();
4816 match data.curr.as_ref() {
4817 Some(curr) => {
4818 let amax = curr.x.amax();
4819 amax > self.diverging_iterates_tol
4820 && self.divergence_is_true_unboundedness(&*curr.x)
4821 && (amax >= Self::DIVERGENCE_ABS_RUNAWAY
4822 || self.divergence_streak >= Self::DIVERGENCE_PERSIST_ITERS)
4823 }
4824 None => false,
4825 }
4826 };
4827 if diverging {
4828 IterateOutcome::Terminate(SolverReturn::DivergingIterates)
4829 } else {
4830 IterateOutcome::Terminate(SolverReturn::RestorationFailure)
4831 }
4832 }
4833 }
4834 RestorationOutcome::UserRequestedStop => {
4835 // gh#645. Same discipline as the pounce#244 deadline
4836 // exit a few lines up, and for the same reason: the
4837 // recovered point is staged on `data.trial` and we
4838 // return without promoting it, so `data.curr` is still
4839 // the last iterate accepted for the *original* NLP.
4840 // That matters more than the status code to the caller
4841 // this exists for — a controller that aborts on a
4842 // deadline still has to apply something, and the
4843 // subproblem's iterate is not a point it should apply.
4844 IterateOutcome::Terminate(SolverReturn::UserRequestedStop)
4845 }
4846 RestorationOutcome::FeasiblePointFound => {
4847 // Port of `IpIpoptAlg.cpp:542` — the catch of
4848 // `FEASIBILITY_PROBLEM_SOLVED`, thrown by
4849 // `IpRestoMinC_1Nrm.cpp:269` when restoration reaches a
4850 // point feasible for a *square* original NLP. Upstream
4851 // recomputes the multipliers before returning
4852 // `FEASIBLE_POINT_FOUND`; without that step the reported
4853 // dual infeasibility is `∇f` at a point whose status says
4854 // the constraints are satisfied. On the gh#508 probe that
4855 // is the difference between Ipopt's `1.78e-15` and a bare
4856 // `10.0`.
4857 //
4858 // The driver has already promoted the recovered point to
4859 // `data.curr`, so the multipliers are computed at the
4860 // point that will be reported.
4861 if self.is_square_problem() {
4862 self.compute_feasibility_multipliers_postprocess();
4863 }
4864 IterateOutcome::Terminate(SolverReturn::FeasiblePointFound)
4865 }
4866 RestorationOutcome::LocallyInfeasible => {
4867 // Mirrors upstream's catch of `LOCALLY_INFEASIBLE` thrown
4868 // from `IpRestoConvCheck.cpp:240` — the resto sub-IPM
4869 // settled at a stationary point of `||c(x)||_1` whose
4870 // residual is still well above `tol`. Without this
4871 // detection the outer would re-enter restoration on the
4872 // unchanged iterate forever.
4873 //
4874 // gh #505: consult the acceptable-point stash, for the same
4875 // reason the conv-check arm above does and the cycle exits
4876 // already did. This is the *third* site that produced
4877 // `LocalInfeasibility`, and the only one not gated on
4878 // `infeas_max_streak` — which matters, because on the reported
4879 // instance raising that knob to 15 did not move the run by a
4880 // single iteration, so the verdict there is not the outer
4881 // detector's. Whichever route reaches it, a solve that passed
4882 // through an acceptable iterate must not discard it.
4883 //
4884 // Inert on genuinely infeasible models by the same argument as
4885 // the other two: nothing is stashed unless the whole acceptable
4886 // triplet passed, so `terminate_acceptable_or` falls through to
4887 // the verdict unchanged.
4888 self.terminate_local_infeasibility()
4889 }
4890 }
4891 }
4892
4893 /// Safe-slack bound adjustment, applied to the staged `trial`
4894 /// iterate before it is promoted to `curr`. When one or more trial
4895 /// slacks fell below `eps*min(1,mu)`, [`IpoptCalculatedQuantities::
4896 /// adjusted_trial_bounds`] returns the moved `x_L/x_U/d_L/d_U`; we
4897 /// install them on the NLP so the slack becomes representable. Port
4898 /// of the bound-adjustment block in `IpoptAlgorithm::AcceptTrialPoint`
4899 /// (`IpIpoptAlg.cpp:664-706`).
4900 fn adjust_variable_bounds_for_small_slacks(&mut self) {
4901 // Compute the moved bounds (releases the CQ/NLP borrows on return).
4902 let adjusted = {
4903 let trial_set = self.data.borrow().trial.is_some();
4904 if !trial_set {
4905 return;
4906 }
4907 self.cq.borrow().adjusted_trial_bounds()
4908 };
4909 let Some(bounds) = adjusted else {
4910 return;
4911 };
4912 tracing::debug!(
4913 target: "pounce::algorithm",
4914 "slack_move: {} slack(s) too small, adjusting variable bound(s) at iter {}",
4915 bounds.adjusted,
4916 self.data.borrow().iter_count,
4917 );
4918 let nlp = Rc::clone(self.cq.borrow().nlp());
4919 nlp.borrow_mut().adjust_variable_bounds(
4920 &*bounds.x_l,
4921 &*bounds.x_u,
4922 &*bounds.d_l,
4923 &*bounds.d_u,
4924 );
4925 }
4926
4927 /// Refresh the `s`-block factors for
4928 /// `linear_system_scaling=slack-based`.
4929 ///
4930 /// A no-op for every other scaling choice: the flag is set only when
4931 /// the builder installed a `SlackBasedTSymScalingMethod`, and the
4932 /// method itself ignores the push if it never receives one.
4933 ///
4934 /// Silently skips when the quantity cannot be formed — no NLP, no
4935 /// current iterate, no inequality rows, or a primal vector shape the
4936 /// CQ does not recognise. The scaling method then keeps behaving as
4937 /// identity, which is what `linear_system_scaling=none` would have
4938 /// done, so a missing push costs conditioning and never correctness.
4939 fn push_slack_scaling(&mut self) {
4940 if !self.slack_based_scaling {
4941 return;
4942 }
4943 if self.nlp.is_none() {
4944 return;
4945 }
4946 let nx = match self.data.borrow().curr.as_ref() {
4947 Some(c) => c.x.dim(),
4948 None => return,
4949 };
4950 let Some(s_scale) = self.cq.borrow().curr_slack_based_s_scaling() else {
4951 return;
4952 };
4953 if s_scale.is_empty() {
4954 return;
4955 }
4956 if let Some(sd) = self.search_dir.as_mut() {
4957 sd.pd_solver_mut()
4958 .aug_solver_mut()
4959 .set_slack_scaling(nx, &s_scale);
4960 }
4961 }
4962
4963 /// `recalc_y` — replace `y_c`/`y_d` with least-square estimates once
4964 /// the iterate is feasible enough. Port of the `recalc_y_` block in
4965 /// `IpIpoptAlg.cpp:AcceptTrialPoint`.
4966 ///
4967 /// Silently does nothing — leaving the Newton-step multipliers in
4968 /// place — when disabled, when the violation is still above
4969 /// `recalc_y_feas_tol`, when there is nothing to estimate, or when
4970 /// the augmented-system solve fails. A failed estimate is not an
4971 /// error: the multipliers we already have are valid, just less
4972 /// accurate, so falling back to them costs accuracy and never
4973 /// correctness. Same reasoning as the initializer's `y0` fallback in
4974 /// `init/default.rs`.
4975 fn maybe_recalc_y(&mut self) {
4976 if !self.recalc_y {
4977 return;
4978 }
4979 let Some(nlp) = self.nlp.as_ref().map(Rc::clone) else {
4980 return;
4981 };
4982 // Feasibility gate. Upstream compares against the same
4983 // `curr_constraint_violation` the convergence check uses.
4984 if self.cq.borrow().curr_constraint_violation() >= self.recalc_y_feas_tol {
4985 return;
4986 }
4987 let (n_yc, n_yd) = {
4988 let d = self.data.borrow();
4989 match d.curr.as_ref() {
4990 Some(c) => (c.y_c.dim(), c.y_d.dim()),
4991 None => return,
4992 }
4993 };
4994 if n_yc + n_yd == 0 {
4995 return;
4996 }
4997 // The augmented-system solver is owned by the search-direction
4998 // calculator, as it is for the initializer's least-square call.
4999 let Some(sd) = self.search_dir.as_mut() else {
5000 return;
5001 };
5002 let mut new_y_c = pounce_linalg::dense_vector::DenseVectorSpace::new(n_yc).make_new_dense();
5003 let mut new_y_d = pounce_linalg::dense_vector::DenseVectorSpace::new(n_yd).make_new_dense();
5004 let mut pd_guard = sd.pd_solver_mut();
5005 // This was the first call site to drop review item M3's 1e-8
5006 // perturbation (#688), on the argument that `recalc_y` overwrites
5007 // `y` every iteration — so a bias in the estimator is a *fixed
5008 // point* rather than a transient, nothing downstream corrects it,
5009 // and it lands directly in `inf_du`, the quantity the run is
5010 // judged on. gh#693 extended the same treatment to the other
5011 // three sites, so `calculate_y_eq` no longer takes a flag: every
5012 // caller now gets δ=0 with the perturbed solve as a retry.
5013 let ok = self.bundle.eq_mult.calculate_y_eq(
5014 &self.data,
5015 &self.cq,
5016 &nlp,
5017 pd_guard.aug_solver_mut(),
5018 &mut new_y_c,
5019 &mut new_y_d,
5020 );
5021 drop(pd_guard);
5022 if !ok {
5023 tracing::debug!(
5024 target: "pounce::algorithm",
5025 "recalc_y: least-square solve failed at iter {}, keeping Newton multipliers",
5026 self.data.borrow().iter_count,
5027 );
5028 return;
5029 }
5030 // Mark the iteration, exactly as upstream does
5031 // (`IpData().Append_info_string("y ")` in
5032 // `IpIpoptAlg.cpp:AcceptTrialPoint`). Without it the iteration
5033 // log gives no way to tell which iterations re-estimated `y`
5034 // and which carried the Newton multipliers — and when a solve
5035 // stalls with an oscillating `inf_du`, whether the oscillation
5036 // tracks the re-estimation is the first thing worth knowing.
5037 self.data.borrow_mut().append_info_string("y ");
5038
5039 // Share x/s/z/v; swap only the equality/inequality multipliers.
5040 let curr = match self.data.borrow().curr.clone() {
5041 Some(c) => c,
5042 None => return,
5043 };
5044 let new_iv = crate::iterates_vector::IteratesVector::new(
5045 curr.x.clone(),
5046 curr.s.clone(),
5047 Rc::new(new_y_c),
5048 Rc::new(new_y_d),
5049 curr.z_l.clone(),
5050 curr.z_u.clone(),
5051 curr.v_l.clone(),
5052 curr.v_u.clone(),
5053 );
5054 self.data.borrow_mut().set_curr(new_iv);
5055 }
5056
5057 /// Port of `IpoptCalculatedQuantities::IsSquareProblem`
5058 /// (`IpIpoptCalculatedQuantities.cpp:3732`): as many equality
5059 /// constraints as variables, so the NLP has zero degrees of freedom.
5060 /// There is nothing to optimise — only a system to solve — and the
5061 /// objective is decorative.
5062 ///
5063 /// The consequence that matters is algebraic. `J_c` is square, so the
5064 /// least-square multiplier system `J_cᵀ y = −∇f (+ bound terms)` is
5065 /// exactly solvable and the dual residual can always be driven to
5066 /// zero, however large `y` has to be. On a non-square problem it
5067 /// generally cannot, which is why this is the right gate and not a
5068 /// heuristic.
5069 fn is_square_problem(&self) -> bool {
5070 match self.data.borrow().curr.as_ref() {
5071 Some(c) => c.x.dim() == c.y_c.dim(),
5072 None => false,
5073 }
5074 }
5075
5076 /// Zero the four bound multipliers and replace `y_c`/`y_d` with the
5077 /// least-square multipliers of the resulting feasibility problem —
5078 /// the shared body of `ComputeFeasibilityMultipliers`
5079 /// (`IpIpoptAlg.cpp:893-922`) and
5080 /// `ComputeFeasibilityMultipliersPostprocess` (`cpp:964-984`), which
5081 /// upstream writes out twice.
5082 ///
5083 /// Returns the iterate that was in place beforehand, so a caller that
5084 /// must be able to undo the swap can. `None` means nothing was
5085 /// installed — no iterate, no multipliers, or the least-square solve
5086 /// failed — in which case the original iterate is left untouched.
5087 fn install_feasibility_multipliers(
5088 &mut self,
5089 ) -> Option<crate::iterates_vector::IteratesVector> {
5090 let nlp = self.nlp.as_ref().map(Rc::clone)?;
5091 let curr_backup = self.data.borrow().curr.clone()?;
5092 let (n_yc, n_yd) = (curr_backup.y_c.dim(), curr_backup.y_d.dim());
5093 if n_yc + n_yd == 0 {
5094 return None;
5095 }
5096
5097 // Zero the bound multipliers and install that iterate *before* the
5098 // solve, so the least-square RHS is the feasibility problem's
5099 // (`cpp:893-910`): the calculator reads `curr`, so the zeroing has
5100 // to be visible to it, not applied to the result afterwards.
5101 let zeroed = |v: &Rc<dyn Vector>| -> Rc<dyn Vector> {
5102 let mut t = v.make_new();
5103 t.set(0.0);
5104 Rc::from(t)
5105 };
5106 let z_l = zeroed(&curr_backup.z_l);
5107 let z_u = zeroed(&curr_backup.z_u);
5108 let v_l = zeroed(&curr_backup.v_l);
5109 let v_u = zeroed(&curr_backup.v_u);
5110 self.data
5111 .borrow_mut()
5112 .set_curr(crate::iterates_vector::IteratesVector::new(
5113 curr_backup.x.clone(),
5114 curr_backup.s.clone(),
5115 curr_backup.y_c.clone(),
5116 curr_backup.y_d.clone(),
5117 z_l.clone(),
5118 z_u.clone(),
5119 v_l.clone(),
5120 v_u.clone(),
5121 ));
5122
5123 let mut new_y_c = pounce_linalg::dense_vector::DenseVectorSpace::new(n_yc).make_new_dense();
5124 let mut new_y_d = pounce_linalg::dense_vector::DenseVectorSpace::new(n_yd).make_new_dense();
5125 let ok = match self.search_dir.as_mut() {
5126 None => false,
5127 Some(sd) => {
5128 let mut pd_guard = sd.pd_solver_mut();
5129 let ok = self.bundle.eq_mult.calculate_y_eq(
5130 &self.data,
5131 &self.cq,
5132 &nlp,
5133 pd_guard.aug_solver_mut(),
5134 &mut new_y_c,
5135 &mut new_y_d,
5136 );
5137 drop(pd_guard);
5138 ok
5139 }
5140 };
5141 if !ok {
5142 // `cpp:986` logs a warning and keeps whatever `y` was there.
5143 tracing::debug!(
5144 target: "pounce::algorithm",
5145 "square problem: least-square multiplier solve failed, keeping Newton multipliers",
5146 );
5147 self.data.borrow_mut().set_curr(curr_backup);
5148 return None;
5149 }
5150
5151 self.data
5152 .borrow_mut()
5153 .set_curr(crate::iterates_vector::IteratesVector::new(
5154 curr_backup.x.clone(),
5155 curr_backup.s.clone(),
5156 Rc::new(new_y_c),
5157 Rc::new(new_y_d),
5158 z_l,
5159 z_u,
5160 v_l,
5161 v_u,
5162 ));
5163 Some(curr_backup)
5164 }
5165
5166 /// Port of `IpoptAlgorithm::ComputeFeasibilityMultipliersPostprocess`
5167 /// (`IpIpoptAlg.cpp:949`). Same swap as
5168 /// [`Self::compute_feasibility_multipliers`], but unconditional: the
5169 /// run is over and the point has already been judged, so there is no
5170 /// convergence check to gate on and nothing to restore. Called on the
5171 /// two square-problem exits that report a feasible point
5172 /// (`cpp:484`, `cpp:542`), whose whole claim is that the constraints
5173 /// are satisfied — reporting `∇f` as the dual residual of such a point
5174 /// would contradict the status printed next to it.
5175 fn compute_feasibility_multipliers_postprocess(&mut self) {
5176 debug_assert!(self.is_square_problem());
5177 let _ = self.install_feasibility_multipliers();
5178 }
5179
5180 /// Port of `IpoptAlgorithm::ComputeFeasibilityMultipliers`
5181 /// (`IpIpoptAlg.cpp:857`). On a square problem, once the iterate is
5182 /// primal-feasible to `constr_viol_tol`, re-estimate `y_c`/`y_d` as
5183 /// the multipliers of the *feasibility* problem: zero the four bound
5184 /// multipliers and take the least-square `y` against that iterate. If
5185 /// the convergence check then accepts, keep them; otherwise restore
5186 /// the iterate untouched.
5187 ///
5188 /// Why it exists (gh#508). A square problem is a system of equations.
5189 /// If the solver has found a point satisfying them to the tolerance
5190 /// the user declared, that point *is* the answer, and the leftover
5191 /// objective gradient is not evidence of anything. Without this,
5192 /// `inf_du` carries `∇f` — on the gh#508 probe `|2(x−5)| = 10` at a
5193 /// point whose violation is `1e-4` inside a `constr_viol_tol` of
5194 /// `1e-3` — the convergence check refuses, and the rapid-infeasibility
5195 /// detector convicts a point Ipopt calls feasible.
5196 ///
5197 /// Note the double convergence check, which is upstream's too: one to
5198 /// decide whether to bother (`cpp:880`), one to decide whether to keep
5199 /// the result (`cpp:924`). Both go through
5200 /// [`ConvergenceCheck::probe_convergence`], not the real check —
5201 /// upstream can afford `CheckConvergence` here because the only state
5202 /// it carries is `acceptable_counter_`, whereas pounce's also carries
5203 /// the gh#505 rapid-infeasibility streak, the gh#200 veto budget and
5204 /// the gh#533 progress window. Advancing those three times per
5205 /// iteration instead of once is not a faithful port of anything: on
5206 /// the gh#508 probe it moved the infeasibility conviction from
5207 /// iteration 86 to 33.
5208 fn compute_feasibility_multipliers(&mut self) {
5209 debug_assert!(self.is_square_problem());
5210
5211 // Not primal feasible yet → no multipliers to compute (cpp:864).
5212 // Upstream measures this on the *unscaled* violation in the max
5213 // norm, against `constr_viol_tol`.
5214 let constr_viol_tol = self.bundle.conv_check.constr_viol_tol_or_default();
5215 if self.cq.borrow().curr_unscaled_primal_infeasibility_max() > constr_viol_tol {
5216 return;
5217 }
5218
5219 // No calculator → upstream logs and leaves `y` alone (cpp:872).
5220 if self.nlp.is_none() {
5221 return;
5222 }
5223
5224 // `iter_count + 1`: see the call site. Upstream has already
5225 // incremented when it reaches here.
5226 let iter_count = self.data.borrow().iter_count + 1;
5227 let nlp_err = self.cq.borrow().curr_nlp_error();
5228 if !nlp_err.is_finite() {
5229 return;
5230 }
5231
5232 // Already converged, or out of iterations/time → do not touch the
5233 // multipliers (cpp:884). `Continue` is the case worth acting on:
5234 // it usually means dual feasibility is what is still missing.
5235 if self
5236 .bundle
5237 .conv_check
5238 .probe_convergence(nlp_err, iter_count, &self.data, &self.cq)
5239 != ConvergenceStatus::Continue
5240 {
5241 return;
5242 }
5243
5244 let Some(curr_backup) = self.install_feasibility_multipliers() else {
5245 return;
5246 };
5247
5248 // Keep them only if they actually buy a verdict (cpp:924).
5249 let nlp_err = self.cq.borrow().curr_nlp_error();
5250 if nlp_err.is_finite()
5251 && matches!(
5252 self.bundle
5253 .conv_check
5254 .probe_convergence(nlp_err, iter_count, &self.data, &self.cq,),
5255 ConvergenceStatus::Converged | ConvergenceStatus::ConvergedToAcceptable
5256 )
5257 {
5258 // Upstream marks nothing here; `"y "` is `recalc_y`'s. Use a
5259 // distinct tag so the iteration log says which mechanism
5260 // moved the multipliers.
5261 self.data.borrow_mut().append_info_string("f ");
5262 return;
5263 }
5264
5265 tracing::debug!(
5266 target: "pounce::algorithm",
5267 "square problem: feasibility multipliers at iter {} did not converge the check, restoring",
5268 iter_count,
5269 );
5270 self.data.borrow_mut().set_curr(curr_backup);
5271 }
5272
5273 /// Port of `IpIpoptAlg::correct_bound_multiplier`
5274 /// (`IpIpoptAlg.cpp:1055-1134`). Clamp each bound multiplier
5275 /// component into `[mu/(kappa_sigma * s_i), kappa_sigma * mu / s_i]`
5276 /// for all four bound-multiplier vectors.
5277 fn correct_bound_multiplier(&mut self) {
5278 if self.kappa_sigma < 1.0 {
5279 return;
5280 }
5281 let mu = self.data.borrow().curr_mu;
5282 let curr = match self.data.borrow().curr.clone() {
5283 Some(c) => c,
5284 None => return,
5285 };
5286
5287 let cq = self.cq.borrow();
5288
5289 let z_l_new = clamp_against_slack(&*curr.z_l, &*cq.curr_slack_x_l(), mu, self.kappa_sigma);
5290 let z_u_new = clamp_against_slack(&*curr.z_u, &*cq.curr_slack_x_u(), mu, self.kappa_sigma);
5291 let v_l_new = clamp_against_slack(&*curr.v_l, &*cq.curr_slack_s_l(), mu, self.kappa_sigma);
5292 let v_u_new = clamp_against_slack(&*curr.v_u, &*cq.curr_slack_s_u(), mu, self.kappa_sigma);
5293 drop(cq);
5294
5295 let new_iv = crate::iterates_vector::IteratesVector::new(
5296 curr.x.clone(),
5297 curr.s.clone(),
5298 curr.y_c.clone(),
5299 curr.y_d.clone(),
5300 z_l_new,
5301 z_u_new,
5302 v_l_new,
5303 v_u_new,
5304 );
5305 self.data.borrow_mut().set_curr(new_iv);
5306 }
5307
5308 /// Outer entry point — port of `IpoptAlgorithm::Optimize()`. Calls
5309 /// the iterate-initializer once, then loops `iterate()` until a
5310 /// terminal status. The exception → SolverReturn mapping
5311 /// (TINY_STEP_DETECTED → STEP_BECOMES_TINY,
5312 /// RESTORATION_FAILED → RESTORATION_FAILURE, etc.) lands in
5313 /// Phase 9 alongside the restoration phase.
5314 /// Run the solve and finalize its result.
5315 ///
5316 /// A thin wrapper on purpose. The gh #200 fallback must see **every** exit
5317 /// of the driver loop, and wiring it into individual termination sites was
5318 /// tried and failed — there are sixteen, and the ones easiest to overlook
5319 /// are the ones most likely to matter. Keeping the loop in a separate
5320 /// function means every `return` inside it, present or future, flows through
5321 /// [`Self::honour_refused_certificate`] by construction rather than by the
5322 /// author remembering to.
5323 ///
5324 /// This got more important once the fallback started changing the status in
5325 /// *both* directions: it can now hand back `StopAtAcceptablePoint` for a
5326 /// `Success` it was given. Anything reading `result` before the hook is
5327 /// reading a status that is not the one reported.
5328 pub fn optimize(&mut self) -> SolverReturn {
5329 let result = self.optimize_inner();
5330 let user_stopped = matches!(result, SolverReturn::UserRequestedStop);
5331
5332 // gh #200: a refused certificate outranks any non-success verdict the
5333 // continued run reached, and an earlier refusal can outrank the
5334 // continued run's own certificate. Applied here, once.
5335 let result = self.honour_refused_certificate(result);
5336
5337 // pounce#250 follow-up: the dual-divergence guard's diversion to
5338 // restoration is a bet, and a lost bet must not return a worse point
5339 // than the solve already had in hand. Applied here, once, for the same
5340 // reason the #200 hook is — every `return` in the loop flows through
5341 // this point by construction.
5342 let result = self.honour_best_acceptable_after_dual_guard(result);
5343
5344 // gh #534: deferring the acceptable-point restoration decline is also a
5345 // bet, and this is the net under it — a continuation that did not beat
5346 // the point the guard would have returned hands that point back. Last of
5347 // the three, so it compares against whatever the hooks above settled on.
5348 let result = self.honour_decline_floor(result);
5349
5350 // gh #797: leaving a certified stationary point along a direction of
5351 // negative curvature is a bet placed *from* a certificate, so it is
5352 // settled last — whatever the hooks above arrived at, the escape either
5353 // beat the point it left with a certificate of its own or that point is
5354 // handed back.
5355 let result = self.honour_neg_curv_floor(result);
5356
5357 // A stop the caller asked for keeps its status whatever point the hooks
5358 // above settled on. They may hand back a better point than the one the
5359 // stop landed on, but `honour_neg_curv_floor` and the gh #200 hook also
5360 // report their restored point's own status, and a `Success` there tells
5361 // a caller driving a sequence of solves (the MPCC ladder) to carry on
5362 // after the user said stop.
5363 let result = if user_stopped {
5364 SolverReturn::UserRequestedStop
5365 } else {
5366 result
5367 };
5368
5369 // Terminal post-mortem checkpoint. Skipped when the user already
5370 // asked to stop (they were just at a prompt); otherwise the
5371 // debugger gets a last look at the final/failing iterate.
5372 if !matches!(result, SolverReturn::UserRequestedStop) {
5373 self.fire_debug_terminal(result);
5374 }
5375 result
5376 }
5377
5378 fn optimize_inner(&mut self) -> SolverReturn {
5379 // Top-level span for the whole solve; every iteration / linear
5380 // solve / restoration event nests under it (pounce#71).
5381 let _solve_span = tracing::info_span!("solve").entered();
5382
5383 // Shared timing accumulator — every phase below records into it.
5384 let timing = self.data.borrow().timing.clone();
5385
5386 // Install the shared accumulator on the augmented-system solver
5387 // so its factor / back-solve calls are attributed to
5388 // `linear_system_factorization` / `linear_system_back_solve`.
5389 // Same pattern for the diagnostics state when present, so KKT
5390 // dump sites can consult per-iter gating.
5391 if let Some(sd) = self.search_dir.as_mut() {
5392 sd.pd_solver_mut()
5393 .aug_solver_mut()
5394 .set_timing_stats(std::rc::Rc::clone(&timing));
5395 if let Some(diag) = self.diagnostics.as_ref() {
5396 sd.pd_solver_mut()
5397 .aug_solver_mut()
5398 .set_diagnostics(Rc::clone(diag));
5399 }
5400 }
5401
5402 // 0a. Strategy initialization — port of upstream's
5403 // `IpoptAlgorithm::InitializeImpl` calls. The mu update needs
5404 // `data.curr_mu`/`curr_tau` seeded before the iterate
5405 // initializer runs (`CalculateSafeSlack` reads them).
5406 self.bundle.mu_update.initialize(&self.data);
5407
5408 // 0b. Iterate initializer. Requires NLP; without one the caller
5409 // must have populated `data.curr` themselves.
5410 if let Some(nlp) = self.nlp.as_ref() {
5411 // The initializer needs an aug-system solver for the
5412 // least-square multiplier branch; until that's wired we
5413 // route through whatever the search-direction calculator
5414 // owns when present. For the stub flow we skip the LSM
5415 // path by giving the initializer a dummy solver only if
5416 // the search_dir is present (otherwise the init function
5417 // is responsible for not consulting it).
5418 if let Some(sd) = self.search_dir.as_mut() {
5419 timing.initialize_iterates.start();
5420 let mut pd_guard = sd.pd_solver_mut();
5421 let aug_solver = pd_guard.aug_solver_mut();
5422 let ok = self
5423 .bundle
5424 .init
5425 .set_initial_iterates(&self.data, &self.cq, nlp, aug_solver);
5426 drop(pd_guard);
5427 timing.initialize_iterates.end();
5428 if !ok {
5429 return SolverReturn::InvalidProblemDefinition;
5430 }
5431 }
5432 }
5433
5434 // 0c. Seed `IpoptData::w` with the initial-iterate Hessian.
5435 // Redundant with the iter-body `update_hessian` call (which
5436 // now runs BEFORE `update_barrier_parameter`) but kept to
5437 // cover any code path that consults `data.w` between
5438 // `set_initial_iterates` and the first `iterate()` call
5439 // (e.g. the iter-0 trace dump below).
5440 if self.data.borrow().curr.is_some() {
5441 timing.update_hessian.start();
5442 let _ = self.bundle.hess.update_hessian(&self.data, &self.cq);
5443 timing.update_hessian.end();
5444 }
5445
5446 // Track-A iterate-trace dumper. Activated by
5447 // `IPOPT_ITER_DUMP_PATH`; otherwise no-op. See `iter_dump.rs`.
5448 let mut dumper = IterDumper::from_env();
5449 // Iter 0 record — captures the initialised iterate before any
5450 // step. Mirrors upstream's "after InitializeIterates(), before
5451 // the loop" emission point.
5452 if let Some(d) = dumper.as_mut() {
5453 d.write_record(&self.data, &self.cq);
5454 }
5455
5456 // Advance the diagnostics iter counter so the first `iterate()`
5457 // body reports as iter 0 (matches `data.iter_count`). Subsequent
5458 // bumps live at the bottom of the loop alongside the iter_count
5459 // bookkeeping.
5460 if let Some(diag) = self.diagnostics.as_ref() {
5461 diag.bump_iter();
5462 // Iter-0 iterate row (issue #68). Same hook point as
5463 // the binary IterDumper above; emits only when
5464 // `--dump iterates:*` is configured.
5465 emit_iterate_record(diag.as_ref(), &self.data, &self.cq);
5466 }
5467
5468 // Iter 0 intermediate callback — upstream fires once after
5469 // `InitializeIterates` before the loop body starts so users
5470 // observe the initial point.
5471 if !self.fire_intermediate() {
5472 return SolverReturn::UserRequestedStop;
5473 }
5474 if self.fire_debug(crate::debug::Checkpoint::IterStart) == crate::debug::DebugAction::Stop {
5475 return SolverReturn::UserRequestedStop;
5476 }
5477
5478 // pounce#246: bound the initialization / restoration-entry window.
5479 // Everything above — `mu_update.initialize`, `set_initial_iterates`
5480 // (which for a bad warm start can grind in its least-square /
5481 // feasibility setup), and the initial `update_hessian` — runs
5482 // *before* the first `iterate()`, whose convergence check and
5483 // post-`compute_search_direction` gate are the earliest deadline
5484 // checks (#242/#244/#245). A solve handed a poor warm start could
5485 // therefore spend the whole budget here and only consult the
5486 // deadline once it reached the first outer-iteration /
5487 // KKT-factorization boundary. Consult it now, before the loop, so a
5488 // bad-start init stall returns promptly with the time-limit status
5489 // (best-so-far being the initialised iterate) instead of running to
5490 // a multiple of the budget. This also bounds the *restoration
5491 // entry*: the nested restoration IPM shares this `Deadline` and runs
5492 // the same `optimize_inner`, so a budget already crossed by the time
5493 // the inner solve starts up terminates it here rather than after its
5494 // own first iterate.
5495 if let Some(ret) = self.deadline_status() {
5496 return ret;
5497 }
5498
5499 let result = loop {
5500 match self.iterate() {
5501 IterateOutcome::Terminate(ret) => break ret,
5502 IterateOutcome::Continue => {
5503 // Source the local counter from `data.iter_count`
5504 // each pass so a pre-seeded counter (e.g. the inner
5505 // restoration IPM at `outer.iter + 1`, matching
5506 // upstream `IpRestoMinC_1Nrm.cpp:181`) and any
5507 // restoration step that set
5508 // `data.iter_count = inner.iter_count - 1`
5509 // (mirroring `IpRestoMinC_1Nrm.cpp:Set_iter_count`)
5510 // are honored — without this the local counter
5511 // would advance from its pre-restoration value,
5512 // ignoring the inner-IPM iterations.
5513 let mut iter_count: Index = self.data.borrow().iter_count;
5514 iter_count += 1;
5515 // Do NOT short-circuit to `MaxiterExceeded` here: bump the
5516 // counter and loop, letting the next `iterate()` run its
5517 // convergence check (`OptimalityErrorConvergenceCheck`,
5518 // which tests the component tolerances *before* its own
5519 // `iter_count >= max_iter` gate at
5520 // `conv_check/opt_error.rs`). Breaking before that call
5521 // skipped the convergence test on the iterate produced by
5522 // the final permitted step, so a solve converging on
5523 // exactly the `max_iter`-th iterate reported
5524 // `Maximum_Iterations_Exceeded` where upstream Ipopt —
5525 // which runs `CheckConvergence` at the top of its loop,
5526 // convergence-first — reports success. The check is
5527 // guaranteed to terminate the loop: once `iter_count`
5528 // reaches `max_iter`, `check_convergence_with_state`
5529 // returns either `Converged`/`ConvergedToAcceptable` or
5530 // `MaxIterExceeded`, never `Continue` (L1).
5531 self.data.borrow_mut().iter_count = iter_count;
5532 // Floor evidence for restoration's gh#661 divergence
5533 // guard: how long this solve has sat at a violation
5534 // it could not get below. Sampled here, once per
5535 // accepted iterate, from the same quantity the
5536 // `inf_pr` column reports — so it is free, and it
5537 // sees the whole outer trajectory rather than the
5538 // handful of iterations a restoration sub-solve runs.
5539 // The nested restoration IPM reaches this line too,
5540 // but writes its own `IpoptData`, so the two
5541 // trajectories never mix.
5542 let inf_pr_now = self.cq.borrow().curr_primal_infeasibility_max();
5543 self.data.borrow_mut().inf_pr_floor.observe(inf_pr_now);
5544 // Keep the diagnostics counter in lock-step with
5545 // `data.iter_count` so KKT-dump gating reflects the
5546 // about-to-execute iteration.
5547 if let Some(diag) = self.diagnostics.as_ref() {
5548 diag.bump_iter();
5549 // Per-iter iterate row (issue #68). Mirrors
5550 // the binary IterDumper hook below.
5551 emit_iterate_record(diag.as_ref(), &self.data, &self.cq);
5552 }
5553 // Per-iteration record — emitted after the
5554 // iter_count bump so the recorded `iter` field
5555 // matches `IpData().iter_count()` at the moment of
5556 // emission, identical to upstream's writer.
5557 if let Some(d) = dumper.as_mut() {
5558 d.write_record(&self.data, &self.cq);
5559 }
5560 // Per-iteration intermediate callback — fired with
5561 // an `IntermediateContext` guard so downstream
5562 // inspector entry points (the C API
5563 // `GetIpoptCurrent*` family) see live state for the
5564 // duration of the user callback.
5565 if !self.fire_intermediate() {
5566 break SolverReturn::UserRequestedStop;
5567 }
5568 if self.fire_debug(crate::debug::Checkpoint::IterStart)
5569 == crate::debug::DebugAction::Stop
5570 {
5571 break SolverReturn::UserRequestedStop;
5572 }
5573 }
5574 }
5575 };
5576
5577 result
5578 }
5579}
5580
5581/// A termination certificate the masked-scale veto refused (gh #200), with
5582/// everything the fallback needs to undo the refusal verbatim.
5583///
5584/// One struct rather than a field per component. The fallback is only correct if
5585/// these all describe *the same iterate*, and parallel `Option`s make
5586/// "objective recorded, iterate missing" representable — which was reachable:
5587/// the iterate is cloned out of `data.curr` and can come back `None`, while the
5588/// objective and barrier parameter were written unconditionally. Capture is now
5589/// all-or-nothing, so the disagreement cannot be constructed.
5590#[derive(Clone)]
5591struct VetoSnapshot {
5592 /// The refused iterate itself.
5593 iterate: crate::iterates_vector::IteratesVector,
5594 /// Iteration at which the refusal happened.
5595 ///
5596 /// Needed to identify which refusal is the *baseline-equivalent* one. The
5597 /// baseline stops at the first iterate where it would terminate, so when
5598 /// both a strict and an acceptable-level refusal are on record it is the
5599 /// chronologically earlier one that says what the baseline returned — not
5600 /// the stricter one. The later refusal sits on the continued trajectory,
5601 /// which the baseline never walked, so comparing against it compares
5602 /// against a point that was never on offer.
5603 iter: Index,
5604 /// Scaled objective there, so the refused point can be compared against
5605 /// whatever the continued run reached without re-evaluating it.
5606 obj: Number,
5607 /// Barrier parameter there.
5608 ///
5609 /// `curr_mu` lives on `IpoptData` rather than in the `IteratesVector`, so
5610 /// restoring the iterate does not rewind it, and `stats.final_mu` is read
5611 /// after the restore — leaving the continued run's barrier parameter
5612 /// reported next to the refused run's `x`. That pair feeds a warm-started
5613 /// corrector's `mu_init` and reaches callers as `info["mu"]`, so it must
5614 /// describe the point actually returned. (Not currently observable: `mu` has
5615 /// bottomed out at its floor in every fallback case reachable so far, making
5616 /// the two values coincide. Kept correct rather than left to depend on that.)
5617 mu: Number,
5618 /// Max-norm unscaled KKT error there, so the tiebreak can see what
5619 /// `apply_kkt_fidelity_gate` will see. Recorded at refusal time because the
5620 /// gate runs post-solve, long after this iterate is gone.
5621 unscaled_kkt: Number,
5622 /// Unscaled max-norm constraint violation there — the same quantity the
5623 /// `acceptable_constr_viol_tol` gate is defined against
5624 /// (`curr_unscaled_primal_infeasibility_max`, cf. gh #261). The
5625 /// best-acceptable fallback ranks candidates by `(feasible_enough,
5626 /// objective)` rather than objective alone, so a point outside a capped
5627 /// feasibility band can never displace one inside it on objective grounds;
5628 /// without this field the ranking has no feasibility term and, under a
5629 /// user-widened `acceptable_constr_viol_tol`, will trade feasibility for
5630 /// objective and hand back a verifiably infeasible point under a success
5631 /// status (gh #267). Unused by the gh #200 masked-scale paths, which key on
5632 /// objective only.
5633 constr_viol: Number,
5634 /// Objective scaling factor in force when `obj` was recorded.
5635 ///
5636 /// `obj` is a *scaled* objective, so comparing it against the continued
5637 /// run's is only meaningful under the same factor, sign included. Held so
5638 /// that assumption is asserted rather than trusted: periodic or adaptive
5639 /// rescaling is a natural thing to add for exactly the ill-scaled problems
5640 /// this mechanism targets, and it would silently turn the comparison into
5641 /// noise.
5642 obj_scale: Number,
5643}
5644
5645/// Internal result of one [`IpoptAlgorithm::iterate`] call. Mirrors the
5646/// upstream try/catch around `IpoptAlg::Optimize` — anything that's not
5647/// `Continue` carries the [`SolverReturn`] that the outer loop will
5648/// surface to `IpoptApplication`.
5649enum IterateOutcome {
5650 Continue,
5651 Terminate(SolverReturn),
5652}
5653
5654/// Feasibility-aware ranking core for the best-acceptable fallback (gh #267).
5655///
5656/// `true` iff candidate `(a_obj, a_viol)` ranks **strictly** better than
5657/// `(b_obj, b_viol)` under the `(band_clamped_viol, objective)` key, where each
5658/// violation is clamped up to `band` before it is compared. Lexicographic: the
5659/// point with the smaller clamped violation wins outright; only when the clamped
5660/// violations tie does the lower objective decide. A non-finite objective ranks
5661/// worst (never wins, always loses to a finite one); a non-finite violation is
5662/// treated as infinitely infeasible.
5663///
5664/// The clamp is what makes this a **total order** rather than a two-class
5665/// partition, and it is the whole gh #280 fix. Clamping the violation *up* to
5666/// `band` collapses every point inside the feasibility band to the single value
5667/// `band`, so within the band those points tie on feasibility and objective
5668/// decides — the intended, gh #267-preserving behaviour. Outside the band the
5669/// clamp is the identity, so the *actual* violation decides and the
5670/// less-infeasible point always wins. The earlier `(feasible_enough, objective)`
5671/// key was a two-class partition: once **both** points sat outside the band it
5672/// fell through to a bare `a_obj < b_obj`, reading neither violation — the exact
5673/// pre-#267 objective-only rule, which lets a strictly-more-infeasible point win
5674/// on objective (gh #280). Under the clamped key a strictly-more-infeasible
5675/// point can never rank better, at any band.
5676///
5677/// Pure and total, so the fallback's "never worse off" guarantee is a theorem
5678/// this function's unit tests prove by cases — host-independent by construction,
5679/// unlike an end-to-end objective comparison across two live nonconvex solves
5680/// (the trap gh #267 caught). [`IpoptAlgorithm::ranks_better`] supplies `band`
5681/// as `min(acceptable_constr_viol_tol, FEASIBLE_ENOUGH_CAP)`; both the record
5682/// and the read side route through here, so they cannot disagree.
5683/// Append `value` to a fixed-capacity oldest-first window, dropping the oldest
5684/// sample once the window is full (gh #534).
5685fn push_sample(buf: &mut [Number; DECLINE_PROGRESS_SAMPLES], len: &mut usize, value: Number) {
5686 if *len < DECLINE_PROGRESS_SAMPLES {
5687 buf[*len] = value;
5688 *len += 1;
5689 } else {
5690 buf.rotate_left(1);
5691 buf[DECLINE_PROGRESS_SAMPLES - 1] = value;
5692 }
5693}
5694
5695/// Whether every consecutive pair in `samples` (oldest first) contracted by at
5696/// least `ratio` — the gh #534 progress test, as a pure function.
5697///
5698/// A sample that is not finite, or a predecessor that is not strictly positive,
5699/// fails the window: neither is evidence of progress, and a zero predecessor
5700/// makes the ratio meaningless. A `ratio` of `1` admits any non-increasing
5701/// window, and a large one admits every finite window — which is how
5702/// `resto_decline_progress_ratio` doubles as the "drop the progress
5703/// requirement" switch.
5704///
5705/// Pure and total for the same reason [`ranks_better_within_band`] is: the two
5706/// traces the issue records — `eigena2` quartering and `eigenb2` rising — decide
5707/// what this must do, and a unit test can hold it to them exactly.
5708fn window_is_contracting(samples: &[Number], ratio: Number) -> bool {
5709 samples.windows(2).all(|w| {
5710 let (prev, next) = (w[0], w[1]);
5711 prev.is_finite() && prev > 0.0 && next.is_finite() && next <= ratio * prev
5712 })
5713}
5714
5715fn ranks_better_within_band(
5716 a_obj: Number,
5717 a_viol: Number,
5718 b_obj: Number,
5719 b_viol: Number,
5720 band: Number,
5721) -> bool {
5722 if !a_obj.is_finite() {
5723 return false;
5724 }
5725 if !b_obj.is_finite() {
5726 return true;
5727 }
5728 // Clamp each violation up to `band`: everything inside the feasibility band
5729 // maps to the single value `band` (so objective decides there), while outside
5730 // it the actual violation is kept (so the less-infeasible point strictly
5731 // wins). A non-finite violation is infinitely infeasible. This is a total
5732 // order — a strictly-more-infeasible point can never rank better (gh #280).
5733 let clamped = |v: Number| {
5734 if v.is_finite() {
5735 v.max(band)
5736 } else {
5737 Number::INFINITY
5738 }
5739 };
5740 let (a_key, b_key) = (clamped(a_viol), clamped(b_viol));
5741 if a_key != b_key {
5742 // The less-infeasible point wins outright, whatever the objectives.
5743 return a_key < b_key;
5744 }
5745 // Same clamped feasibility (both inside the band, or an exact tie outside it):
5746 // lower objective wins (the original within-band behaviour).
5747 a_obj < b_obj
5748}
5749
5750/// `||a - b||_2 / (1 + ||b||_2)`. Used by the restoration cycle
5751/// detector in [`IpoptAlgorithm::invoke_restoration`] to test whether
5752/// the outer iterate has moved between two consecutive restoration
5753/// entries.
5754fn relative_distance(a: &dyn Vector, b: &dyn Vector) -> Number {
5755 if a.dim() == 0 {
5756 return 0.0;
5757 }
5758 let mut diff = a.make_new_copy();
5759 diff.axpy(-1.0, b);
5760 diff.nrm2() / (1.0 + b.nrm2())
5761}
5762
5763/// `out = curr + α_p · δ` for the primal/equality blocks and
5764/// `out = curr + α_d · δ` for the bound multipliers, returned as a
5765/// fresh frozen `IteratesVector`. Mirrors `scaled_step` in the line
5766/// search; duplicated here for the tiny-step branch which bypasses
5767/// the line-search driver.
5768fn scaled_step_unchecked(
5769 curr: &crate::iterates_vector::IteratesVector,
5770 delta: &crate::iterates_vector::IteratesVector,
5771 alpha_primal: Number,
5772 alpha_dual: Number,
5773) -> crate::iterates_vector::IteratesVector {
5774 let mut out = curr.make_new_zeroed();
5775 out.add_one_vector(1.0, curr, 0.0);
5776 out.x.axpy(alpha_primal, &*delta.x);
5777 out.s.axpy(alpha_primal, &*delta.s);
5778 out.y_c.axpy(alpha_primal, &*delta.y_c);
5779 out.y_d.axpy(alpha_primal, &*delta.y_d);
5780 out.z_l.axpy(alpha_dual, &*delta.z_l);
5781 out.z_u.axpy(alpha_dual, &*delta.z_u);
5782 out.v_l.axpy(alpha_dual, &*delta.v_l);
5783 out.v_u.axpy(alpha_dual, &*delta.v_u);
5784 out.freeze()
5785}
5786
5787/// Allocate a fresh `Rc<dyn Vector>` with `kappa_sigma_clamp`
5788/// applied component-wise against the supplied `slack`. Inputs are
5789/// borrowed; the original `z` is never mutated. Ports the per-vector
5790/// piece of `IpIpoptAlg.cpp:1080-1133`.
5791fn clamp_against_slack(
5792 z: &dyn Vector,
5793 slack: &dyn Vector,
5794 mu: Number,
5795 kappa_sigma: Number,
5796) -> Rc<dyn Vector> {
5797 debug_assert_eq!(z.dim(), slack.dim());
5798 let n = z.dim() as usize;
5799 // Flatten both z and slack into contiguous slices so the
5800 // elementwise clamp doesn't care whether the inputs are
5801 // [`DenseVector`] (regular IPM path) or [`CompoundVector`]
5802 // (resto IPM path). The result is reconstructed into a
5803 // same-shape Vector via `Vector::make_new` + a flat-write
5804 // helper so the caller sees a vector with the same blocking as
5805 // its input.
5806 let mut buf = vec![0.0_f64; n];
5807 flat_read_into(z, &mut buf);
5808 let s_vals = flat_read_owned(slack);
5809 let _ = kappa_sigma_clamp(&mut buf, &s_vals, mu, kappa_sigma);
5810 let mut out: Box<dyn Vector> = z.make_new();
5811 flat_write_into(&mut *out, &buf);
5812 Rc::from(out)
5813}
5814
5815pub(crate) fn flat_read_into(v: &dyn Vector, dst: &mut [Number]) {
5816 if let Some(dv) = v
5817 .as_any()
5818 .downcast_ref::<pounce_linalg::dense_vector::DenseVector>()
5819 {
5820 let vs = dv.expanded_values();
5821 dst.copy_from_slice(&vs);
5822 return;
5823 }
5824 if let Some(cv) = v.as_any().downcast_ref::<pounce_linalg::CompoundVector>() {
5825 let mut off = 0usize;
5826 for k in 0..cv.n_comps() {
5827 let blk = cv.comp(k);
5828 let dim = blk.dim() as usize;
5829 let dblk = blk
5830 .as_any()
5831 .downcast_ref::<pounce_linalg::dense_vector::DenseVector>()
5832 .expect("clamp_against_slack: CompoundVector blocks must be DenseVectors");
5833 let vs = dblk.expanded_values();
5834 dst[off..off + dim].copy_from_slice(&vs);
5835 off += dim;
5836 }
5837 return;
5838 }
5839 panic!("clamp_against_slack: unsupported Vector kind");
5840}
5841
5842pub(crate) fn flat_read_owned(v: &dyn Vector) -> Vec<Number> {
5843 let mut out = vec![0.0; v.dim() as usize];
5844 flat_read_into(v, &mut out);
5845 out
5846}
5847
5848pub(crate) fn flat_write_into(v: &mut dyn Vector, src: &[Number]) {
5849 if let Some(dv) = v
5850 .as_any_mut()
5851 .downcast_mut::<pounce_linalg::dense_vector::DenseVector>()
5852 {
5853 dv.set_values(src);
5854 return;
5855 }
5856 if let Some(cv) = v
5857 .as_any_mut()
5858 .downcast_mut::<pounce_linalg::CompoundVector>()
5859 {
5860 let mut off = 0usize;
5861 for k in 0..cv.n_comps() {
5862 let blk = cv.comp_mut(k);
5863 let dim = blk.dim() as usize;
5864 let dblk = blk
5865 .as_any_mut()
5866 .downcast_mut::<pounce_linalg::dense_vector::DenseVector>()
5867 .expect("clamp_against_slack: CompoundVector blocks must be DenseVectors");
5868 dblk.set_values(&src[off..off + dim]);
5869 off += dim;
5870 }
5871 return;
5872 }
5873 panic!("clamp_against_slack: unsupported Vector kind");
5874}
5875
5876/// Per-element kappa-sigma clamp — the elementwise arithmetic at the
5877/// heart of `IpIpoptAlg.cpp:correct_bound_multiplier` (lines
5878/// 1090-1133). For each index `i`:
5879///
5880/// ```text
5881/// slack_i = max(slack_i, tiny_double) // avoid /0
5882/// z_lo_i = mu / (kappa_sigma * slack_i)
5883/// z_hi_i = kappa_sigma * mu / slack_i
5884/// z_i ← clamp(z_i, z_lo_i, z_hi_i)
5885/// ```
5886///
5887/// Returns the maximum elementwise correction magnitude (matching
5888/// upstream's `Max(max_correction_up, max_correction_low)`).
5889///
5890/// `kappa_sigma < 1` short-circuits to the identity per upstream's
5891/// guard at line 1065.
5892pub fn kappa_sigma_clamp(
5893 z: &mut [Number],
5894 slack: &[Number],
5895 mu: Number,
5896 kappa_sigma: Number,
5897) -> Number {
5898 debug_assert_eq!(z.len(), slack.len());
5899 if kappa_sigma < 1.0 {
5900 return 0.0;
5901 }
5902 let mut max_correction = 0.0_f64;
5903 for (zi, &si) in z.iter_mut().zip(slack.iter()) {
5904 let s_safe = si.max(Number::MIN_POSITIVE);
5905 let lo = mu / (kappa_sigma * s_safe);
5906 let hi = kappa_sigma * mu / s_safe;
5907 let clamped = zi.clamp(lo, hi);
5908 let delta = (clamped - *zi).abs();
5909 if delta > max_correction {
5910 max_correction = delta;
5911 }
5912 *zi = clamped;
5913 }
5914 max_correction
5915}
5916
5917#[cfg(test)]
5918mod tests {
5919 use super::*;
5920
5921 #[test]
5922 fn kappa_sigma_below_one_is_identity() {
5923 let mut z = vec![1.0, 2.0, 3.0];
5924 let slack = [1.0, 1.0, 1.0];
5925 let m = kappa_sigma_clamp(&mut z, &slack, 1.0, 0.5);
5926 assert_eq!(m, 0.0);
5927 assert_eq!(z, [1.0, 2.0, 3.0]);
5928 }
5929
5930 #[test]
5931 fn within_band_is_unchanged() {
5932 // mu=1, kappa=10, slack=1 → band [0.1, 10]. z=1 → unchanged.
5933 let mut z = vec![1.0];
5934 let slack = [1.0];
5935 let m = kappa_sigma_clamp(&mut z, &slack, 1.0, 10.0);
5936 assert_eq!(m, 0.0);
5937 assert_eq!(z, [1.0]);
5938 }
5939
5940 #[test]
5941 fn above_upper_clamped_down() {
5942 // mu=1, kappa=10, slack=1 → upper = 10. z=100 → 10.
5943 let mut z = vec![100.0];
5944 let slack = [1.0];
5945 let m = kappa_sigma_clamp(&mut z, &slack, 1.0, 10.0);
5946 assert!((m - 90.0).abs() < 1e-13);
5947 assert_eq!(z, [10.0]);
5948 }
5949
5950 #[test]
5951 fn below_lower_clamped_up() {
5952 // mu=1, kappa=10, slack=1 → lower = 0.1. z=0.001 → 0.1.
5953 let mut z = vec![0.001];
5954 let slack = [1.0];
5955 let m = kappa_sigma_clamp(&mut z, &slack, 1.0, 10.0);
5956 assert!((m - 0.099).abs() < 1e-13);
5957 assert!((z[0] - 0.1).abs() < 1e-15);
5958 }
5959
5960 #[test]
5961 fn returns_max_over_components() {
5962 let mut z = vec![100.0, 0.001];
5963 let slack = [1.0, 1.0];
5964 let m = kappa_sigma_clamp(&mut z, &slack, 1.0, 10.0);
5965 assert!((m - 90.0).abs() < 1e-13);
5966 assert_eq!(z[0], 10.0);
5967 assert!((z[1] - 0.1).abs() < 1e-15);
5968 }
5969
5970 #[test]
5971 fn slack_clamped_to_min_positive_avoids_division_by_zero() {
5972 let mut z = vec![1e100];
5973 let slack = [0.0];
5974 let _ = kappa_sigma_clamp(&mut z, &slack, 1.0, 10.0);
5975 assert!(z[0].is_finite() || z[0] == 1e100);
5976 }
5977
5978 /// The restoration slot is exercised structurally:
5979 /// `IpoptAlgorithm::with_restoration` accepts a
5980 /// `Box<dyn RestorationPhase>` and the trait's default
5981 /// `perform_restoration` returns `Failed`. End-to-end coverage
5982 /// (iterate() → line-search-Failed → restoration → recovered)
5983 /// lands in the Phase 9 integration suite alongside the nested
5984 /// IPM driver.
5985 struct _DummyResto;
5986 impl RestorationPhase for _DummyResto {}
5987
5988 // --------------------------------------------------------------
5989 // Best-acceptable fallback ranking (gh #267).
5990 //
5991 // These prove the "never worse off" guarantee host-independently, by
5992 // cases, on the pure ranking core — the property the earlier end-to-end
5993 // `hair_trigger_*` objective comparison could only *approximate* on one
5994 // host's basin luck (gh #267's secondary finding). `band` here stands in
5995 // for the resolved `min(acceptable_constr_viol_tol, FEASIBLE_ENOUGH_CAP)`.
5996 // --------------------------------------------------------------
5997
5998 #[test]
5999 fn ranks_better_is_a_strict_order_within_a_feasibility_class() {
6000 let band = 1e-2;
6001 // Both feasible: lower objective wins, strictly.
6002 assert!(ranks_better_within_band(-2.0, 1e-4, -1.0, 1e-4, band));
6003 assert!(!ranks_better_within_band(-1.0, 1e-4, -2.0, 1e-4, band));
6004 // Ties are not "strictly better" in either direction — so an equal
6005 // returned point is never displaced, matching the read side's
6006 // keep-current-on-tie contract.
6007 assert!(!ranks_better_within_band(-1.0, 1e-4, -1.0, 5e-3, band));
6008 assert!(!ranks_better_within_band(-1.0, 5e-3, -1.0, 1e-4, band));
6009 // Both infeasible: the less-infeasible point wins. Here it also has the
6010 // lower objective, so this held under the old objective-only fall-through
6011 // too — `ranks_better_puts_feasibility_first_among_two_infeasibles` is the
6012 // case that separates the two rules (gh #280).
6013 assert!(ranks_better_within_band(-2.0, 5.0, -1.0, 9.0, band));
6014 }
6015
6016 #[test]
6017 fn ranks_better_puts_feasibility_first_among_two_infeasibles() {
6018 // The gh #280 hole: once BOTH points sit outside the (capped) band the
6019 // old `(feasible_enough, objective)` partition read a_ok == b_ok == false
6020 // and fell through to `a_obj < b_obj` — objective alone, the exact
6021 // pre-#267 rule — so a strictly-MORE-infeasible point could win by having
6022 // a better objective. This is the deb7 swap the fallback made: incumbent
6023 // at viol 5.292e-1, recorded point at viol 9.951e-1 with a 36%-better
6024 // objective. The less-infeasible point must win regardless of objective.
6025 let band = 1e-2;
6026 // Less-infeasible incumbent, WORSE objective — must still win.
6027 assert!(ranks_better_within_band(
6028 89.0, 5.292e-1, 56.9, 9.951e-1, band
6029 ));
6030 // The more-infeasible, better-objective point must NOT win — the swap
6031 // gh #280 forbids.
6032 assert!(!ranks_better_within_band(
6033 56.9, 9.951e-1, 89.0, 5.292e-1, band
6034 ));
6035 // A strictly-more-infeasible point never replaces the incumbent even with
6036 // an arbitrarily better objective.
6037 assert!(!ranks_better_within_band(-1e12, 9.0, 0.0, 5.0, band));
6038 assert!(ranks_better_within_band(0.0, 5.0, -1e12, 9.0, band));
6039 }
6040
6041 #[test]
6042 fn ranks_better_puts_feasibility_first_regardless_of_objective() {
6043 let band = 1e-2;
6044 // The gh #267 case in miniature: a feasible point outranks an
6045 // arbitrarily-lower-objective infeasible one, and vice-versa.
6046 assert!(ranks_better_within_band(0.0, 1e-4, -1e9, 9.94, band));
6047 assert!(!ranks_better_within_band(-1e9, 9.94, 0.0, 1e-4, band));
6048 // Exactly at the band is still feasible_enough; just past it is not.
6049 assert!(ranks_better_within_band(
6050 1.0,
6051 band,
6052 -1.0,
6053 band * 1.000_001,
6054 band
6055 ));
6056 }
6057
6058 #[test]
6059 fn ranks_better_never_lets_a_widened_band_matter_past_the_cap() {
6060 // The band the method feeds is capped at FEASIBLE_ENOUGH_CAP, so a
6061 // point beyond the cap is never feasible_enough however loose the
6062 // user's `acceptable_constr_viol_tol`. Model that by passing the capped
6063 // band: the near-optimal-but-mildly-infeasible endpoint (viol 1.13e-4,
6064 // within the cap) must beat the grossly-infeasible lower-objective
6065 // point (viol 9.94, past it) — the exact swap the fix forbids.
6066 let band = IpoptAlgorithm::FEASIBLE_ENOUGH_CAP;
6067 assert!(ranks_better_within_band(
6068 -2303.99, 1.13e-4, -2307.32, 9.94, band
6069 ));
6070 assert!(!ranks_better_within_band(
6071 -2307.32, 9.94, -2303.99, 1.13e-4, band
6072 ));
6073 }
6074
6075 #[test]
6076 fn ranks_better_ranks_a_nonfinite_objective_worst() {
6077 let band = 1e-2;
6078 for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
6079 // A non-finite objective never ranks better than a finite one...
6080 assert!(!ranks_better_within_band(bad, 0.0, 0.0, 9.9, band));
6081 // ...and always loses to one, even on worse feasibility — so a
6082 // finite recorded point is restored over a NaN-objective return,
6083 // preserving the pre-fix `!(curr <= best)` NaN behaviour.
6084 assert!(ranks_better_within_band(0.0, 9.9, bad, 0.0, band));
6085 }
6086 }
6087
6088 #[test]
6089 fn ranks_better_treats_a_nonfinite_violation_as_infeasible() {
6090 let band = 1e-2;
6091 // A NaN violation is never feasible_enough, so a genuinely feasible
6092 // point outranks it regardless of objective.
6093 assert!(ranks_better_within_band(0.0, 0.0, -100.0, f64::NAN, band));
6094 assert!(!ranks_better_within_band(-100.0, f64::NAN, 0.0, 0.0, band));
6095 }
6096
6097 /// gh #534, the case the guard was stopping: `eigena2`'s dual infeasibility
6098 /// quarters on unit steps for four straight iterations, three short of a
6099 /// strict certificate. Quoted from the issue's own iteration table.
6100 #[test]
6101 fn eigena2_endgame_reads_as_contracting() {
6102 let eigena2 = [1.19e-05, 2.96e-06, 7.38e-07, 1.84e-07];
6103 assert!(window_is_contracting(
6104 &eigena2,
6105 DEFAULT_DECLINE_PROGRESS_RATIO
6106 ));
6107 }
6108
6109 /// gh #534, the case the guard was right about: `eigenb2`'s tail *rises*
6110 /// on heavily backtracked steps. The issue calls it a plausible genuine
6111 /// stall, so the progress test must refuse it and leave the guard alone.
6112 #[test]
6113 fn eigenb2_stall_does_not_read_as_contracting() {
6114 let eigenb2 = [1.88e-07, 2.69e-07, 2.89e-07, 2.93e-07];
6115 assert!(!window_is_contracting(
6116 &eigenb2,
6117 DEFAULT_DECLINE_PROGRESS_RATIO
6118 ));
6119 }
6120
6121 /// gh #534: `csfi2`'s window, measured on this build at the guard. Three
6122 /// healthy contractions and then a flat step — the solve has stopped
6123 /// moving, so the deferral must not fire however good the earlier ratios
6124 /// look. This is the shape every live guard firing reachable from the
6125 /// in-repo corpus has, which is why the whole window is tested and not
6126 /// just its first ratios.
6127 #[test]
6128 fn csfi2_flat_final_step_does_not_read_as_contracting() {
6129 let csfi2 = [3.267e0, 1.845e-6, 8.468e-8, 8.524e-8];
6130 assert!(!window_is_contracting(
6131 &csfi2,
6132 DEFAULT_DECLINE_PROGRESS_RATIO
6133 ));
6134 // ... and it is the *last* step that decides: drop it and the same
6135 // trace passes, which is exactly the distinction the test exists for.
6136 assert!(window_is_contracting(
6137 &csfi2[..3],
6138 DEFAULT_DECLINE_PROGRESS_RATIO
6139 ));
6140 }
6141
6142 /// gh #534: a large ratio drops the progress requirement, so the decline is
6143 /// deferred on any window. That is the "bypass the guard and see how far the
6144 /// solve gets" switch the issue asks for. A ratio of exactly `1` is the
6145 /// weaker "no backsliding" reading and still refuses `csfi2`, whose last
6146 /// step rises.
6147 #[test]
6148 fn a_large_ratio_accepts_a_stalled_window() {
6149 let csfi2 = [3.267e0, 1.845e-6, 8.468e-8, 8.524e-8];
6150 assert!(!window_is_contracting(&csfi2, 1.0));
6151 assert!(window_is_contracting(&[1e-8, 1e-8, 1e-8, 1e-8], 1.0));
6152 assert!(window_is_contracting(&csfi2, 1e20));
6153 // Still not a licence to read garbage as progress.
6154 assert!(!window_is_contracting(
6155 &[1.0, Number::NAN, 1e-9, 1e-12],
6156 1e20
6157 ));
6158 }
6159
6160 /// gh #534 edge cases: the ratio must never be evaluated against a
6161 /// non-positive or non-finite predecessor.
6162 #[test]
6163 fn degenerate_windows_never_read_as_contracting() {
6164 let r = DEFAULT_DECLINE_PROGRESS_RATIO;
6165 // A zero predecessor makes the ratio meaningless (0 <= 0.5*0 would
6166 // otherwise read as "contracting" forever).
6167 assert!(!window_is_contracting(&[0.0, 0.0, 0.0, 0.0], r));
6168 assert!(!window_is_contracting(&[1e-9, 0.0, 0.0, 0.0], r));
6169 assert!(!window_is_contracting(
6170 &[Number::INFINITY, 1e-3, 1e-6, 1e-9],
6171 r
6172 ));
6173 assert!(!window_is_contracting(&[1e-3, 1e-6, 1e-9, Number::NAN], r));
6174 // A genuine run down to exactly zero is progress, not a degenerate
6175 // window — the predecessor is positive at every step.
6176 assert!(window_is_contracting(&[1e-3, 1e-6, 1e-9, 0.0], r));
6177 }
6178
6179 /// gh #534: the window slides one sample per outer iteration and holds the
6180 /// most recent [`DECLINE_PROGRESS_SAMPLES`]. A short history is never a full
6181 /// window, which is what stops the first restoration entry of a solve from
6182 /// being deferred on no evidence at all — `nlp_err_contracting` requires
6183 /// `len == DECLINE_PROGRESS_SAMPLES` before it consults the samples.
6184 #[test]
6185 fn progress_window_slides_oldest_out() {
6186 let mut buf = [Number::NAN; DECLINE_PROGRESS_SAMPLES];
6187 let mut len = 0usize;
6188 for e in [1e-1, 1e-2, 1e-3] {
6189 push_sample(&mut buf, &mut len, e);
6190 }
6191 assert_eq!(len, 3);
6192 push_sample(&mut buf, &mut len, 1e-4);
6193 assert_eq!(len, DECLINE_PROGRESS_SAMPLES);
6194 assert_eq!(buf, [1e-1, 1e-2, 1e-3, 1e-4]);
6195 assert!(window_is_contracting(&buf, DEFAULT_DECLINE_PROGRESS_RATIO));
6196 // One flat iteration slides the oldest sample out and withdraws the
6197 // verdict.
6198 push_sample(&mut buf, &mut len, 1e-4);
6199 assert_eq!(len, DECLINE_PROGRESS_SAMPLES);
6200 assert_eq!(buf, [1e-2, 1e-3, 1e-4, 1e-4]);
6201 assert!(!window_is_contracting(&buf, DEFAULT_DECLINE_PROGRESS_RATIO));
6202 }
6203
6204 /// gh #505: no route may conclude `LocalInfeasibility` on its own.
6205 ///
6206 /// Three routes reach that verdict, and two of them independently shipped
6207 /// the same defect — building the terminate outcome directly, so the
6208 /// acceptable-point stash was never consulted and a good point the solve
6209 /// already had in hand was discarded. They were found one at a time,
6210 /// because nothing tied them together.
6211 ///
6212 /// The route a solve takes is an internal detail; the user sees one status
6213 /// either way. So what that status means is decided in one place — every
6214 /// route goes through [`IpoptAlgorithm::terminate_local_infeasibility`],
6215 /// or, for the cycle exits whose fallback is chosen between two statuses
6216 /// at the call site, through `terminate_acceptable_or`. Both consult the
6217 /// stash.
6218 ///
6219 /// **This is a tripwire, not a proof.** It is a substring scan of this
6220 /// file's source for the bare `IterateOutcome::Terminate(SolverReturn::
6221 /// LocalInfeasibility)` construction. A rustfmt line break through that
6222 /// expression, a `let` binding for the status, or a construction in
6223 /// another module all evade it — `application.rs` names the same variant
6224 /// on the SQP and ℓ₁ elastic paths and is deliberately out of scope. What
6225 /// it does buy is that the *obvious* way to add a fourth bare exit here
6226 /// fails loudly and points at the helper, which is the mistake that was
6227 /// actually made twice.
6228 ///
6229 /// The needle is assembled at runtime so this test's own source cannot
6230 /// satisfy the pattern it is checking for; an earlier version counted its
6231 /// own lines and failed against clean code.
6232 #[test]
6233 fn no_route_concludes_local_infeasibility_alone() {
6234 let needle = format!(
6235 "IterateOutcome::Terminate(SolverReturn::{})",
6236 "LocalInfeasibility"
6237 );
6238 let offenders: Vec<usize> = include_str!("ipopt_alg.rs")
6239 .lines()
6240 .enumerate()
6241 .filter(|(_, l)| {
6242 let t = l.trim_start();
6243 !t.starts_with("//") && !t.starts_with("///")
6244 })
6245 .filter(|(_, l)| l.contains(&needle))
6246 .map(|(i, _)| i + 1)
6247 .collect();
6248 assert!(
6249 offenders.is_empty(),
6250 "line(s) {offenders:?} build the local-infeasibility verdict directly. \
6251 Call `terminate_local_infeasibility()` instead — it consults the \
6252 acceptable-point stash first, so a solve that already passed through an \
6253 acceptable iterate returns that point rather than a hard failure. Two \
6254 routes shipped this bug before the helper existed (gh #505)."
6255 );
6256 }
6257}