Skip to main content

pounce_algorithm/
application.rs

1//! User-facing application object — port of `Interfaces/IpIpoptApplication.{hpp,cpp}`.
2//!
3//! # Crate placement
4//!
5//! `IpoptApplication` lives in `pounce-algorithm` (rather than
6//! alongside the other Interfaces-side ports in `pounce-nlp`) because
7//! `optimize_tnlp` needs to drive the full IPM: it constructs a
8//! `TNLPAdapter` + `OrigIpoptNlp` (from `pounce-nlp`) and hands the
9//! NLP off to an [`IpoptAlgorithm`] (this crate). `pounce-nlp` cannot
10//! depend on `pounce-algorithm` (the reverse already exists), so
11//! orchestration must live on the algorithm side. Public callers
12//! continue to import via `pounce_algorithm::IpoptApplication`.
13//!
14//! `optimize_tnlp` routes every problem — constrained or not —
15//! through the same primal-dual IPM, exactly as upstream Ipopt does:
16//! it builds the algorithm via [`crate::alg_builder::AlgorithmBuilder`]
17//! (default backend MA57 from `pounce-hsl`) and runs
18//! [`IpoptAlgorithm::optimize`].
19
20use crate::alg_builder::{
21    AlgorithmBuilder, HessianApproxChoice, LineSearchChoice, LinearBackendFactory,
22    LinearSolverChoice, MuStrategyChoice,
23};
24use crate::hess::lim_mem_quasi_newton::UpdateType;
25use crate::ipopt_alg::IpoptAlgorithm;
26use crate::ipopt_cq::IpoptCalculatedQuantities;
27use crate::ipopt_data::IpoptData as AlgIpoptData;
28use crate::ipopt_nlp::IpoptNlp;
29use crate::iterates_vector::IteratesVector;
30use crate::restoration::RestorationPhase;
31use crate::upstream_options::register_all_upstream_options;
32
33/// Factory that constructs a fresh restoration-phase strategy on
34/// demand. The outer algorithm owns at most one restoration object,
35/// so the factory is invoked once per `optimize_tnlp` call. The
36/// factory is `FnMut` to allow callers to capture a builder that
37/// internally reuses caches across builds.
38pub type RestorationFactory = Box<dyn FnMut() -> Box<dyn RestorationPhase>>;
39
40/// Provider that mints fresh [`RestorationFactory`] instances on
41/// demand. Used by drivers that need to run the inner IPM more than
42/// once per `optimize_tnlp` call — notably the Phase-3 ℓ₁-exact
43/// penalty-barrier outer loop (pounce#10), which the existing
44/// `RestorationFactory` cannot support because pounce's default
45/// `make_default_restoration_factory` is a one-shot. Callers wire
46/// this via [`IpoptApplication::set_restoration_factory_provider`].
47pub type RestorationFactoryProvider = Box<dyn FnMut() -> RestorationFactory>;
48
49/// Callback fired by [`IpoptApplication::optimize_constrained`] once
50/// the IPM has converged (status `SolveSucceeded` or
51/// `SolvedToAcceptableLevel`) and before the user TNLP's
52/// `finalize_solution` runs. Receives borrowed handles into the
53/// algorithm's converged state.
54///
55/// **Use case**: post-optimal sensitivity analysis (pounce#7 /
56/// `pounce-sensitivity`). The callback receives a shared handle to
57/// the PD solver so a `SensBacksolver` adapter can run backsolves
58/// against the converged KKT factor — and so that handle may outlive
59/// the call frame (e.g. the public `Solver` session API retains the
60/// factor for repeated `parametric_step` / `kkt_solve` calls);
61/// receives the data / cq / nlp handles so the adapter can reproduce
62/// the augmented-system coefficient layout the IPM converged at.
63///
64/// **Not** the same as `set_intermediate_callback` (per-iteration
65/// progress notification) — this fires exactly once per `optimize_*`
66/// call, only on success.
67pub type ConvergedCallback = Box<
68    dyn FnMut(
69        &crate::ipopt_data::IpoptDataHandle,
70        &crate::ipopt_cq::IpoptCqHandle,
71        &Rc<RefCell<dyn pounce_nlp::ipopt_nlp::IpoptNlp>>,
72        Rc<RefCell<crate::kkt::pd_full_space_solver::PdFullSpaceSolver>>,
73    ),
74>;
75use pounce_common::diagnostics::DiagnosticsState;
76use pounce_common::exception::{ExceptionKind, SolverException};
77use pounce_common::journalist::{JournalLevel, Journalist};
78use pounce_common::options_list::OptionsList;
79use pounce_common::reg_options::{PrintOptionsMode, RegisteredOptions};
80use pounce_common::timing::TimingStatistics;
81use pounce_common::types::{Index, Number};
82use pounce_linalg::dense_vector::DenseVectorSpace;
83use pounce_linsol::summary::LinearSolverSummary;
84use pounce_linsol::SparseSymLinearSolverInterface;
85use pounce_nlp::alg_types::SolverReturn;
86use pounce_nlp::orig_ipopt_nlp::{ConstObjScaling, OrigIpoptNlp, ScalingMethod};
87use pounce_nlp::return_codes::ApplicationReturnStatus;
88use pounce_nlp::solve_statistics::SolveStatistics;
89use pounce_nlp::tnlp::{
90    IpoptCq as TnlpIpoptCq, IpoptData as TnlpIpoptData, NlpInfo, Solution, TNLP,
91};
92use pounce_nlp::tnlp_adapter::{
93    FixedVarTreatment, TNLPAdapter, DEFAULT_NLP_LOWER_BOUND_INF, DEFAULT_NLP_UPPER_BOUND_INF,
94};
95use std::cell::RefCell;
96use std::fmt;
97use std::path::Path;
98use std::rc::Rc;
99use std::sync::{Arc, Mutex};
100use std::time::Instant;
101
102pub struct IpoptApplication {
103    options: OptionsList,
104    reg_options: Rc<RegisteredOptions>,
105    journalist: Rc<Journalist>,
106    statistics: RefCell<SolveStatistics>,
107    /// Shared per-subsystem timing accumulator. Re-created at the top of
108    /// every solve (so back-to-back `optimize_tnlp` calls don't bleed
109    /// timings across invocations) and handed to the data, the NLP, and
110    /// any other consumer via `Rc`. Reported by [`Self::timing_stats`]
111    /// after the solve completes.
112    timing: RefCell<Rc<TimingStatistics>>,
113    /// Optional override factory for the symmetric linear-solver
114    /// backend. When `None`, we ship the workspace default (MA57 via
115    /// `pounce-hsl`). Tests can plug a stub via [`Self::set_linear_backend_factory`].
116    linear_backend_factory: Option<LinearBackendFactory>,
117    /// Optional factory for the restoration phase. Lives outside this
118    /// crate because `pounce-algorithm` cannot depend on
119    /// `pounce-restoration` (the dep edge is the other way). Callers
120    /// that need restoration plug a factory via
121    /// [`Self::set_restoration_factory`]; when unset, the outer
122    /// algorithm runs without a restoration fallback and surfaces
123    /// `RestorationFailure` as soon as the line-search would otherwise
124    /// jump into restoration.
125    restoration_factory: Option<RestorationFactory>,
126    /// Shared diagnostic-dump state, installed by the CLI when the
127    /// user passes `--dump <cat>:<spec>`. When set, the application
128    /// propagates an `Rc<DiagnosticsState>` into [`IpoptAlgorithm`]
129    /// via [`IpoptAlgorithm::with_diagnostics`] so the KKT solver and
130    /// other dump sites can consult per-iter gating.
131    diagnostics: Option<Rc<DiagnosticsState>>,
132    /// Optional interactive debugger hook. When set, it is moved into
133    /// the main [`IpoptAlgorithm`] for the next `optimize_*` call via
134    /// [`IpoptAlgorithm::with_debug_hook`], so a REPL or agent can pause
135    /// at each iteration to inspect / mutate live state. Consumed on use
136    /// (one solve per installed hook).
137    debug_hook: Option<std::rc::Rc<std::cell::RefCell<dyn crate::debug::DebugHook>>>,
138    /// Provider for the BNW outer loop (pounce#10 Phase 3). When set,
139    /// `optimize_constrained` consults the provider before each inner
140    /// solve, replacing `restoration_factory` with a fresh one so
141    /// multi-pass drivers can run the inner IPM repeatedly without
142    /// tripping the default factory's one-shot guard.
143    restoration_factory_provider: Option<RestorationFactoryProvider>,
144    /// Optional hook fired once per `optimize_*` call on convergence,
145    /// before the user TNLP's `finalize_solution`. See
146    /// [`ConvergedCallback`].
147    on_converged: Option<ConvergedCallback>,
148    /// When `true`, the per-iteration `IterRecord` trajectory is
149    /// captured into [`SolveStatistics::iterations`] for downstream
150    /// consumers (the JSON solve report in pounce-cli, pounce#8). Off
151    /// by default so library callers that never read the iterations
152    /// vector don't pay the per-iter alloc.
153    record_iter_history: bool,
154    /// Shared sink that the linear-solver backend writes a rolling
155    /// [`LinearSolverSummary`] into after every factor. Reset at the
156    /// top of every solve (so back-to-back `optimize_tnlp` calls don't
157    /// bleed stats across invocations) and read out via
158    /// [`Self::linear_solver_summary`] once the solve returns. Only
159    /// the workspace-default FERAL backend (via
160    /// [`default_backend_factory_with_sink`]) wires the sink today;
161    /// custom factories plugged through [`Self::set_linear_backend_factory`]
162    /// and the HSL MA57 backend leave the sink empty.
163    linsol_summary_sink: Arc<Mutex<LinearSolverSummary>>,
164    /// Phase 5c (§6) SQP warm-start input. When `Some`, the next
165    /// `optimize_tnlp` call on the SQP path consumes the iterate
166    /// instead of cold-starting; consumed once per solve, then
167    /// auto-cleared. The IPM path ignores this field. Wire-set
168    /// via [`Self::set_sqp_warm_start`].
169    sqp_warm_start: Option<crate::sqp::SqpIterates>,
170    /// Phase 5c (§6) SQP warm-start output. Populated by every
171    /// `optimize_sqp_tnlp` call with the final QP working set.
172    /// Stays valid until the next solve (which overwrites it).
173    /// Accessed via [`Self::last_sqp_working_set`].
174    sqp_last_working_set: Option<pounce_qp::WorkingSet>,
175    /// Full primal-dual warm-start iterate for the IPM path, captured by
176    /// the interactive debugger's `resolve` command. When `Some`, the
177    /// next `optimize_tnlp` installs this 8-vector (algorithm space)
178    /// directly onto `data.curr` before the iterate initializer runs, so
179    /// a warm `resolve` continues from the paused interior point rather
180    /// than cold-restarting the duals. Consumed once per solve, then
181    /// auto-cleared. Requires `warm_start_init_point=yes` so the
182    /// re-optimize branch of `WarmStartIterateInitializer` keeps the
183    /// installed iterate. Wire-set via [`Self::set_warm_start_iterate`].
184    warm_start_iterate: Option<crate::debug::IterateSnapshot>,
185}
186
187impl fmt::Debug for IpoptApplication {
188    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189        f.debug_struct("IpoptApplication")
190            .field("options", &self.options)
191            .field("statistics", &self.statistics)
192            .finish_non_exhaustive()
193    }
194}
195
196impl Default for IpoptApplication {
197    fn default() -> Self {
198        Self::new()
199    }
200}
201
202impl IpoptApplication {
203    /// New application with empty options and a default journalist.
204    /// Equivalent to `IpoptApplication::IpoptApplication(true,true)`.
205    pub fn new() -> Self {
206        let reg = RegisteredOptions::default();
207        // Registration of a fresh registry can only fail on a duplicate
208        // name, which would be a programming error in `reg_op`.
209        register_all_upstream_options(&reg)
210            .unwrap_or_else(|e| panic!("Upstream options registration failed: {e}"));
211        pounce_presolve::register_options(&reg)
212            .unwrap_or_else(|e| panic!("Presolve options registration failed: {e}"));
213        let reg = Rc::new(reg);
214        Self {
215            options: OptionsList::with_registered(Rc::clone(&reg)),
216            reg_options: reg,
217            journalist: Rc::new(Journalist::new()),
218            statistics: RefCell::new(SolveStatistics::new()),
219            timing: RefCell::new(Rc::new(TimingStatistics::new())),
220            linear_backend_factory: None,
221            restoration_factory: None,
222            diagnostics: None,
223            debug_hook: None,
224            restoration_factory_provider: None,
225            on_converged: None,
226            record_iter_history: false,
227            linsol_summary_sink: Arc::new(Mutex::new(LinearSolverSummary::default())),
228            sqp_warm_start: None,
229            sqp_last_working_set: None,
230            warm_start_iterate: None,
231        }
232    }
233
234    pub fn options(&self) -> &OptionsList {
235        &self.options
236    }
237
238    pub fn options_mut(&mut self) -> &mut OptionsList {
239        &mut self.options
240    }
241
242    pub fn registered_options(&self) -> &Rc<RegisteredOptions> {
243        &self.reg_options
244    }
245
246    pub fn journalist(&self) -> &Rc<Journalist> {
247        &self.journalist
248    }
249
250    /// Plug a custom symmetric-linear-solver factory. Useful for tests
251    /// that want to swap MA57 for a stub. Production callers should
252    /// leave this unset — the default ([`default_backend_factory`])
253    /// returns the workspace's MA57 binding.
254    pub fn set_linear_backend_factory(&mut self, factory: LinearBackendFactory) {
255        self.linear_backend_factory = Some(factory);
256    }
257
258    /// Plug a restoration-phase factory. Called once per
259    /// `optimize_tnlp` invocation to mint a fresh
260    /// `Box<dyn RestorationPhase>` that the outer algorithm uses as
261    /// its line-search restoration fallback. Lives behind a setter
262    /// (rather than at construction) because the concrete restoration
263    /// strategies live in `pounce-restoration`, which depends on this
264    /// crate; consumers in `pounce-cli` / integration tests wire the
265    /// factory at the application boundary.
266    pub fn set_restoration_factory(&mut self, factory: RestorationFactory) {
267        self.restoration_factory = Some(factory);
268    }
269
270    /// Install the shared diagnostics state. Once set, every
271    /// subsequent `optimize_tnlp` call forwards the state into the
272    /// algorithm via [`IpoptAlgorithm::with_diagnostics`] so the KKT
273    /// solver can emit `--dump kkt:...` artifacts.
274    pub fn set_diagnostics(&mut self, diag: Rc<DiagnosticsState>) {
275        self.diagnostics = Some(diag);
276    }
277
278    /// Install an interactive debugger hook for the next `optimize_*`
279    /// call. The hook is moved into the main [`IpoptAlgorithm`] and
280    /// consumed by that solve; reinstall it to debug a subsequent solve.
281    pub fn set_debug_hook(
282        &mut self,
283        hook: std::rc::Rc<std::cell::RefCell<dyn crate::debug::DebugHook>>,
284    ) {
285        self.debug_hook = Some(hook);
286    }
287
288    /// Read-side accessor for the installed diagnostics state, if any.
289    /// Lets the CLI write the top-level manifest/timing files after
290    /// the solve completes.
291    pub fn diagnostics(&self) -> Option<Rc<DiagnosticsState>> {
292        self.diagnostics.as_ref().map(Rc::clone)
293    }
294
295    /// Plug a restoration-phase **factory provider** for drivers that
296    /// need to run the inner IPM more than once per `optimize_tnlp`
297    /// call (notably the Phase-3 ℓ₁-exact penalty-barrier outer loop,
298    /// pounce#10). On each inner solve, the application consults the
299    /// provider to mint a fresh [`RestorationFactory`], replacing any
300    /// stale one, so the default one-shot restoration factory does
301    /// not panic on its second invocation. If both `set_restoration_factory`
302    /// and this are configured, the provider wins.
303    pub fn set_restoration_factory_provider(&mut self, provider: RestorationFactoryProvider) {
304        self.restoration_factory_provider = Some(provider);
305    }
306
307    /// Register a callback to run once the IPM has converged (status
308    /// [`ApplicationReturnStatus::SolveSucceeded`] or
309    /// [`ApplicationReturnStatus::SolvedToAcceptableLevel`]) but before
310    /// `finalize_solution` flows back to the TNLP. See
311    /// [`ConvergedCallback`] for the use case (post-optimal sensitivity).
312    pub fn set_on_converged(&mut self, cb: ConvergedCallback) {
313        self.on_converged = Some(cb);
314    }
315
316    /// Enable per-iteration trajectory capture. After the solve
317    /// returns, [`Self::statistics()`] exposes
318    /// [`pounce_nlp::solve_statistics::SolveStatistics::iterations`]
319    /// populated with one [`pounce_nlp::solve_statistics::IterRecord`]
320    /// per accepted iterate. Off by default — the `pounce_sens` and
321    /// `pounce` binaries opt in when `--json-output` is passed.
322    pub fn enable_iter_history(&mut self) {
323        self.record_iter_history = true;
324    }
325
326    /// Read an `ipopt.opt`-format options file. Equivalent to
327    /// `IpoptApplication::Initialize(const std::string& options_file)`.
328    pub fn initialize_with_options_file(&mut self, path: &Path) -> Result<(), SolverException> {
329        let txt = std::fs::read_to_string(path).map_err(|e| {
330            SolverException::new(
331                ExceptionKind::IPOPT_APPLICATION_ERROR,
332                format!("could not read options file {}: {}", path.display(), e),
333                file!(),
334                line!() as Index,
335            )
336        })?;
337        self.options.read_from_str(&txt, true)?;
338        self.open_output_file_journal();
339        Ok(())
340    }
341
342    /// Read options from a string in `ipopt.opt` format. Useful for
343    /// tests and embedded callers.
344    pub fn initialize_with_options_str(&mut self, s: &str) -> Result<(), SolverException> {
345        self.options.read_from_str(s, true)?;
346        self.open_output_file_journal();
347        Ok(())
348    }
349
350    /// Honor `output_file` / `file_print_level` / `file_append`: when
351    /// `output_file` is non-empty, attach a `FileJournal` named
352    /// `"OutputFile:<fname>"` at the requested level. Mirrors
353    /// `IpoptApplication::OpenOutputFile` (called from `Initialize`).
354    /// No-op if `output_file` is unset, empty, or could not be opened.
355    ///
356    /// NOTE: pounce's iteration output currently bypasses the
357    /// journalist and writes directly to stdout. The file journal is
358    /// attached and the timing report (gated by `print_timing_statistics`)
359    /// is mirrored to it; per-iter rows will start landing in the file
360    /// once the iter-output path is routed through the journalist.
361    fn open_output_file_journal(&self) {
362        let fname = match self.options.get_string_value("output_file", "") {
363            Ok((v, true)) if !v.is_empty() => v,
364            _ => return,
365        };
366        let level_int = self
367            .options
368            .get_integer_value("file_print_level", "")
369            .ok()
370            .and_then(|(v, f)| f.then_some(v))
371            .unwrap_or(5);
372        let level = journal_level_from_int(level_int);
373        let append = self
374            .options
375            .get_bool_value("file_append", "")
376            .ok()
377            .and_then(|(v, f)| f.then_some(v))
378            .unwrap_or(false);
379        let jname = format!("OutputFile:{}", fname);
380        let _ = self
381            .journalist
382            .add_file_journal(&jname, &fname, level, append);
383    }
384
385    /// No-op initialize (just succeeds). Mirrors
386    /// `IpoptApplication::Initialize(bool allow_clobber)` with no
387    /// options file.
388    pub fn initialize(&mut self) -> Result<(), SolverException> {
389        Ok(())
390    }
391
392    /// Mirror `IpoptApplication::OpenOutputFile`. Sets the `output_file`
393    /// / `file_print_level` options and attaches a matching
394    /// `FileJournal` named `OutputFile:<fname>` to the journalist.
395    /// Returns `false` if the file could not be opened or the option
396    /// store rejected the request (e.g. clamped print level).
397    pub fn open_output_file(&mut self, fname: &str, print_level: i32) -> bool {
398        if self
399            .options
400            .set_string_value("output_file", fname, true, false)
401            .is_err()
402        {
403            return false;
404        }
405        if self
406            .options
407            .set_integer_value("file_print_level", print_level as Index, true, false)
408            .is_err()
409        {
410            return false;
411        }
412        let level = journal_level_from_int(print_level);
413        let jname = format!("OutputFile:{}", fname);
414        // Drop any previous file journal so a second call switches files
415        // cleanly. `add_file_journal` would otherwise refuse to attach
416        // a duplicate by name; remove-by-name isn't in the journalist
417        // API, so we settle for the name-collision case here.
418        self.journalist
419            .add_file_journal(&jname, fname, level, false)
420            .is_some()
421    }
422
423    /// Wrap a TNLP and report problem dimensions. Used in tests until
424    /// the full IPM path covers every entry shape.
425    pub fn problem_dimensions(&self, tnlp: &mut dyn TNLP) -> Option<NlpInfo> {
426        tnlp.get_nlp_info()
427    }
428
429    pub fn statistics(&self) -> SolveStatistics {
430        self.statistics.borrow().clone()
431    }
432
433    /// Shared timing accumulator from the most recent `optimize_tnlp`
434    /// call. Each subsystem (algorithm, NLP, KKT solver) bumped its own
435    /// fields during the solve; consumers read totals out of the
436    /// returned `Rc`. The instance is replaced at the top of every
437    /// subsequent solve, so cloning the `Rc` and holding it past a
438    /// re-solve will give you the previous solve's timings — by design.
439    pub fn timing_stats(&self) -> Rc<TimingStatistics> {
440        Rc::clone(&self.timing.borrow())
441    }
442
443    /// Aggregate linear-solver post-mortem from the most recent
444    /// `optimize_tnlp` call. `Some` when the workspace-default FERAL
445    /// backend ran at least one factor; `None` when no factors were
446    /// recorded (custom factory plugged via
447    /// [`Self::set_linear_backend_factory`], or solve aborted before
448    /// the first KKT factor). Reset at the top of every solve.
449    pub fn linear_solver_summary(&self) -> Option<LinearSolverSummary> {
450        let guard = self.linsol_summary_sink.lock().ok()?;
451        if guard.is_empty() {
452            None
453        } else {
454            Some(guard.clone())
455        }
456    }
457
458    /// Drive a solve.
459    ///
460    /// * Constrained problems (`m > 0`) take the primal-dual IPM path:
461    ///   build a `TNLPAdapter` → `OrigIpoptNlp`, run the
462    ///   [`AlgorithmBuilder`] with the workspace MA57 backend, and
463    ///   call [`IpoptAlgorithm::optimize`]. The `SolverReturn` →
464    ///   `ApplicationReturnStatus` mapping mirrors the table in
465    ///   `ref/Ipopt/AGENT_REFERENCE/MAIN_LOOP.md` ("exception →
466    ///   SolverReturn map").
467    /// * Unconstrained problems (`m == 0`) keep going through the
468    ///   in-`pounce-nlp` Newton driver so the trivial path is
469    ///   independent of the linear-solver backend.
470    pub fn optimize_tnlp(&mut self, tnlp: Rc<RefCell<dyn TNLP>>) -> ApplicationReturnStatus {
471        // Top-level algorithm dispatch (Phase 5b §7.1). When the
472        // `algorithm` option resolves to "active-set-sqp", route
473        // to the Phase 5b SQP path; otherwise fall through to the
474        // existing IPM flow unchanged.
475        if self.is_sqp_algorithm_selected() {
476            return self.optimize_sqp_tnlp(tnlp);
477        }
478        let info = match tnlp.borrow_mut().get_nlp_info() {
479            Some(info) => info,
480            None => return ApplicationReturnStatus::InvalidProblemDefinition,
481        };
482        // ℓ₁-exact penalty-barrier opt-in (pounce#10).
483        // Phase 3 wraps the user TNLP and runs an outer Byrd-Nocedal-
484        // Waltz ρ-escalation loop around the constrained IPM, with a
485        // honest-infeasibility status upgrade when the slacks fail to
486        // collapse at saturated ρ. Phase-1/2 one-shot use is preserved
487        // when `l1_penalty_max_outer_iter == 1`. The wrapper is a
488        // no-op for problems with no equality rows, so the
489        // unconstrained dispatch below is unaffected when there is
490        // nothing to wrap.
491        if info.m > 0 && self.is_l1_penalty_enabled() {
492            if let Some(status) = self.run_l1_penalty_outer_loop(Rc::clone(&tnlp)) {
493                return status;
494            }
495            // Falls through: wrapper construction failed (inner refused
496            // get_nlp_info / get_bounds_info) or no equality rows to
497            // slack. Standard dispatch runs unmodified.
498        }
499        // Phase 3.5 auto-fallback (pounce#10): if the standard solve
500        // ends in a trigger-class status, retry transparently with
501        // the wrapper. Promote the retry's status only if it returns
502        // SolveSucceeded — otherwise return the original. Skipped if
503        // the user already opted into the wrapper above (this avoids
504        // a double pass and keeps semantics predictable).
505        if info.m > 0 && self.is_l1_fallback_enabled() && !self.is_l1_penalty_enabled() {
506            return self.run_with_l1_fallback(tnlp);
507        }
508        // Every problem — constrained or not — goes through the same
509        // primal-dual IPM, exactly as upstream Ipopt does. There is no
510        // separate "unconstrained Newton" path: the linear-solver
511        // backend (FERAL/MA57) handles the augmented system, so the
512        // sparse IPM covers `m == 0` at any `n` without a dense-Hessian
513        // blowup.
514        self.optimize_constrained(tnlp)
515    }
516
517    /// Read the ℓ₁ wrapper master switch from the OptionsList.
518    /// Default `false` when the option is not set.
519    fn is_l1_penalty_enabled(&self) -> bool {
520        self.options
521            .get_bool_value("l1_exact_penalty_barrier", "")
522            .ok()
523            .and_then(|(v, found)| found.then_some(v))
524            .unwrap_or(false)
525    }
526
527    fn l1_penalty_init(&self) -> Number {
528        self.options
529            .get_numeric_value("l1_penalty_init", "")
530            .ok()
531            .and_then(|(v, found)| found.then_some(v))
532            .unwrap_or(1.0)
533    }
534    fn l1_penalty_max(&self) -> Number {
535        self.options
536            .get_numeric_value("l1_penalty_max", "")
537            .ok()
538            .and_then(|(v, found)| found.then_some(v))
539            .unwrap_or(1.0e6)
540    }
541    fn l1_penalty_increase_factor(&self) -> Number {
542        self.options
543            .get_numeric_value("l1_penalty_increase_factor", "")
544            .ok()
545            .and_then(|(v, found)| found.then_some(v))
546            .unwrap_or(8.0)
547    }
548    fn l1_penalty_max_outer_iter(&self) -> usize {
549        self.options
550            .get_integer_value("l1_penalty_max_outer_iter", "")
551            .ok()
552            .and_then(|(v, found)| found.then_some(v))
553            .unwrap_or(8) as usize
554    }
555    fn l1_slack_tol(&self) -> Number {
556        self.options
557            .get_numeric_value("l1_slack_tol", "")
558            .ok()
559            .and_then(|(v, found)| found.then_some(v))
560            .unwrap_or(1.0e-6)
561    }
562    fn l1_steering_factor(&self) -> Number {
563        self.options
564            .get_numeric_value("l1_steering_factor", "")
565            .ok()
566            .and_then(|(v, found)| found.then_some(v))
567            .unwrap_or(10.0)
568    }
569    fn is_l1_fallback_enabled(&self) -> bool {
570        self.options
571            .get_bool_value("l1_fallback_on_restoration_failure", "")
572            .ok()
573            .and_then(|(v, found)| found.then_some(v))
574            .unwrap_or(false)
575    }
576
577    /// Has the user set `algorithm = active-set-sqp`? Reads the
578    /// string option and matches case-insensitively against the
579    /// design-note §7.1 spelling. Any value other than
580    /// "active-set-sqp" (including absence) routes to the
581    /// default IPM path.
582    /// Stash a warm-start iterate for the SQP path. Consumed by
583    /// the next `optimize_tnlp` call when the `algorithm` option
584    /// resolves to `active-set-sqp`; the IPM path ignores it.
585    /// Phase 5c (§6) — the parametric / MPC warm-start hand-off.
586    ///
587    /// The iterate is auto-cleared after use, so a follow-up
588    /// solve without an intervening `set_sqp_warm_start` call
589    /// cold-starts.
590    pub fn set_sqp_warm_start(&mut self, warm: crate::sqp::SqpIterates) {
591        self.sqp_warm_start = Some(warm);
592    }
593
594    /// Drop any pending warm-start iterate without solving.
595    pub fn clear_sqp_warm_start(&mut self) {
596        self.sqp_warm_start = None;
597    }
598
599    /// Install a full primal-dual warm-start iterate for the next IPM
600    /// `optimize_tnlp`. Captured by the debugger's `resolve` so the
601    /// re-solve continues from the paused interior point. The caller is
602    /// responsible for also enabling `warm_start_init_point=yes` (and
603    /// usually `warm_start_target_mu=<μ>`) so the re-optimize branch of
604    /// `WarmStartIterateInitializer` preserves the installed iterate.
605    /// Consumed once per solve, then auto-cleared.
606    pub fn set_warm_start_iterate(&mut self, snap: crate::debug::IterateSnapshot) {
607        self.warm_start_iterate = Some(snap);
608    }
609
610    /// Return the final QP working set from the most recent SQP
611    /// solve, or `None` if the last solve wasn't SQP, didn't
612    /// produce a working set (cold-start declared the iterate
613    /// optimal before solving any QP), or no SQP solve has run.
614    pub fn last_sqp_working_set(&self) -> Option<&pounce_qp::WorkingSet> {
615        self.sqp_last_working_set.as_ref()
616    }
617
618    fn is_sqp_algorithm_selected(&self) -> bool {
619        match self.options.get_string_value("algorithm", "") {
620            Ok((v, true)) => v.eq_ignore_ascii_case("active-set-sqp"),
621            _ => false,
622        }
623    }
624
625    /// Phase 5b SQP entry point. Builds the same NLP chain
626    /// (`TNLPAdapter` → `OrigIpoptNlp` → `IpoptNlpAdapter`) the
627    /// IPM uses, then runs `SqpAlgorithm::optimize`. Maps the
628    /// `SqpResult.status` back to `ApplicationReturnStatus` and
629    /// hands the final iterate to the user TNLP's
630    /// `finalize_solution` callback via `finalize_via_sqp`.
631    fn optimize_sqp_tnlp(&mut self, tnlp: Rc<RefCell<dyn TNLP>>) -> ApplicationReturnStatus {
632        use pounce_nlp::orig_ipopt_nlp::OrigIpoptNlp;
633        use pounce_nlp::tnlp_adapter::TNLPAdapter;
634        use pounce_nlp::ConstObjScaling;
635
636        let adapter = match TNLPAdapter::new(Rc::clone(&tnlp)) {
637            Ok(a) => Rc::new(RefCell::new(a)),
638            Err(_) => return ApplicationReturnStatus::InvalidProblemDefinition,
639        };
640        // The SQP path never runs gradient-based scaling, but the
641        // constant `obj_scaling_factor` (negative ⇒ maximize) still
642        // applies via the OrigIpoptNlp constructor.
643        let obj_scaling_factor = self
644            .options
645            .get_numeric_value("obj_scaling_factor", "")
646            .ok()
647            .and_then(|(v, f)| f.then_some(v))
648            .unwrap_or(1.0);
649        let orig_nlp = match OrigIpoptNlp::new(
650            Rc::clone(&adapter),
651            Rc::new(ConstObjScaling(obj_scaling_factor)),
652        ) {
653            Ok(n) => n,
654            Err(_) => return ApplicationReturnStatus::InternalError,
655        };
656        let nlp_rc: Rc<RefCell<dyn IpoptNlp>> = Rc::new(RefCell::new(orig_nlp));
657
658        let mut sqp_adapter = crate::sqp::IpoptNlpAdapter::new(Rc::clone(&nlp_rc));
659
660        let mut builder = self.algorithm_builder_snapshot();
661        builder.algorithm = crate::alg_builder::AlgorithmChoice::ActiveSetSqp;
662        let factory = self.make_backend_factory();
663        let mut alg = match builder.build_sqp_with_backend(factory) {
664            Some(a) => a,
665            None => return ApplicationReturnStatus::InternalError,
666        };
667
668        // Phase 5c (§6): consume any stashed warm-start iterate.
669        // `optimize_with_warm_start(warm=None)` is equivalent to
670        // `optimize`, so cold callers see no change.
671        let warm = self.sqp_warm_start.take();
672        let res = match alg.optimize_with_warm_start(&mut sqp_adapter, warm) {
673            Ok(r) => r,
674            Err(e) => {
675                if std::env::var_os("POUNCE_DBG_SQP").is_some() {
676                    tracing::warn!(target: "pounce::sqp", "[SQP] optimize_with_warm_start error: {e:?}");
677                }
678                return ApplicationReturnStatus::InternalError;
679            }
680        };
681        // Stash the result's working set so the next solve in a
682        // sequence can fetch it via `last_sqp_working_set`.
683        self.sqp_last_working_set = res.working_set.clone();
684        // Populate the shared `SolveStatistics` so the Python /
685        // C-API post-solve accessors (`GetIpoptIterCount`,
686        // `info["iter_count"]`, etc.) report the SQP outer-iter
687        // count rather than zero. Constraint-violation /
688        // dual-infeasibility residuals get the SQP-side values
689        // too. The IPM path overwrites this dict on its own
690        // solves, so SQP-vs-IPM mixing across solves stays
691        // honest.
692        {
693            let mut stats = self.statistics.borrow_mut();
694            stats.iteration_count = res.n_iter as Index;
695            stats.final_objective = res.obj;
696            stats.final_dual_inf = res.final_stationarity;
697            stats.final_constr_viol = res.final_constr_viol;
698            stats.final_compl = 0.0; // SQP has no barrier — no compl term.
699        }
700        let (app_status, solver_status) = match res.status {
701            crate::sqp::SqpStatus::Optimal => (
702                ApplicationReturnStatus::SolveSucceeded,
703                pounce_nlp::SolverReturn::Success,
704            ),
705            crate::sqp::SqpStatus::MaxIter => (
706                ApplicationReturnStatus::MaximumIterationsExceeded,
707                pounce_nlp::SolverReturn::MaxiterExceeded,
708            ),
709            crate::sqp::SqpStatus::InfeasibleSubproblem => (
710                ApplicationReturnStatus::InfeasibleProblemDetected,
711                pounce_nlp::SolverReturn::LocalInfeasibility,
712            ),
713            crate::sqp::SqpStatus::LineSearchFailed => (
714                ApplicationReturnStatus::SearchDirectionBecomesTooSmall,
715                pounce_nlp::SolverReturn::ErrorInStepComputation,
716            ),
717        };
718
719        // Forward to the user TNLP's finalize_solution. We pass
720        // the SQP iterate and recovered multipliers via the
721        // OrigIpoptNlp's lifting hooks. Failure here is silent
722        // (we still return the algorithm's status) — the user
723        // sees the right ApplicationReturnStatus regardless.
724        let _ = finalize_via_sqp(&nlp_rc, &res, solver_status, &tnlp);
725
726        app_status
727    }
728
729    /// Build a *copy* of the algorithm builder configured per the
730    /// current options. The SQP path uses this so it gets a
731    /// fresh builder without mutating the application's state.
732    fn algorithm_builder_snapshot(&self) -> AlgorithmBuilder {
733        let mut builder = AlgorithmBuilder::default();
734        apply_sqp_options(&self.options, &mut builder.sqp);
735        apply_qp_subproblem_options(&self.options, &mut builder.sqp_qp);
736        builder
737    }
738
739    /// Construct a LinearBackendFactory honoring the
740    /// `linear_solver` option. Default FERAL; HSL MA57 when
741    /// built with the `ma57` feature.
742    fn make_backend_factory(&self) -> LinearBackendFactory {
743        Box::new(
744            |_choice| -> Box<dyn pounce_linsol::SparseSymLinearSolverInterface> {
745                Box::new(pounce_feral::FeralSolverInterface::new())
746            },
747        )
748    }
749
750    /// Phase 3.5 auto-fallback driver.
751    ///
752    /// Runs the standard solve (no wrapper) first. If it ends in a
753    /// trigger-class status (`Restoration_Failed`, `Infeasible_Problem_Detected`,
754    /// `Solved_To_Acceptable_Level`, `Maximum_Iterations_Exceeded`, or
755    /// `Not_Enough_Degrees_Of_Freedom`), retries transparently with
756    /// the ℓ₁ wrapper enabled. Promotes the retry's status only if
757    /// it returns `Solve_Succeeded`; otherwise returns the original
758    /// status.
759    ///
760    /// Caveat: the user TNLP's `finalize_solution` runs once per
761    /// attempt. When the retry doesn't promote, the user's captured
762    /// fields hold the retry's iterate (the ℓ₁-best least-infeasible
763    /// point) even though the returned status is the original's.
764    /// Documented on the option's help text; tightening this is a
765    /// Phase-4 follow-up.
766    fn run_with_l1_fallback(&mut self, tnlp: Rc<RefCell<dyn TNLP>>) -> ApplicationReturnStatus {
767        // First attempt: the standard IPM solve, no ℓ₁ wrapper. Only
768        // reached for `m > 0`, so `optimize_constrained` is exact.
769        let first_status = self.optimize_constrained(Rc::clone(&tnlp));
770        if !is_l1_fallback_trigger(first_status) {
771            return first_status;
772        }
773        // Trigger fired. Flip the wrapper option for the retry and
774        // restore it after — keeps the user's option-table view of the
775        // session exactly as they left it.
776        let prev = self
777            .options
778            .get_string_value("l1_exact_penalty_barrier", "")
779            .ok();
780        let _ = self
781            .options
782            .set_string_value("l1_exact_penalty_barrier", "yes", true, false);
783        let retry_status = self
784            .run_l1_penalty_outer_loop(Rc::clone(&tnlp))
785            .unwrap_or(ApplicationReturnStatus::InternalError);
786        let _ = self.options.set_string_value(
787            "l1_exact_penalty_barrier",
788            prev.as_ref().map(|(v, _)| v.as_str()).unwrap_or("no"),
789            true,
790            false,
791        );
792        if matches!(retry_status, ApplicationReturnStatus::SolveSucceeded) {
793            retry_status
794        } else {
795            first_status
796        }
797    }
798
799    /// Phase-3 ℓ₁-exact penalty-barrier outer loop.
800    ///
801    /// Builds an [`L1PenaltyBarrierTnlp`] wrapper around the user
802    /// TNLP, runs the constrained IPM at the current ρ, escalates ρ
803    /// per Byrd-Nocedal-Waltz steering, and terminates on any of:
804    ///   - slack sum collapses (`Σ(p+n) ≤ l1_slack_tol`)
805    ///   - inner solve returns non-Optimal (escalation won't fix
806    ///     numerical / restoration failure at this ρ)
807    ///   - ρ already at `l1_penalty_max`
808    ///   - `l1_penalty_max_outer_iter` reached
809    ///
810    /// After the loop, if the inner status is `SolveSucceeded` or
811    /// `SolvedToAcceptableLevel` but slacks didn't collapse, override
812    /// to `Infeasible_Problem_Detected` — the returned point is the
813    /// ℓ₁-best least-infeasible iterate, which is informative even
814    /// though the original constraints are not satisfied.
815    ///
816    /// Returns `Some(status)` if the wrapper ran the solve, `None` if
817    /// wrapper construction failed (caller should fall through to the
818    /// standard dispatch path).
819    fn run_l1_penalty_outer_loop(
820        &mut self,
821        tnlp: Rc<RefCell<dyn TNLP>>,
822    ) -> Option<ApplicationReturnStatus> {
823        let rho_init = self.l1_penalty_init();
824        let rho_max = self.l1_penalty_max().max(rho_init);
825        let factor = self.l1_penalty_increase_factor().max(1.0);
826        let tau = self.l1_steering_factor();
827        let slack_tol = self.l1_slack_tol();
828        let max_outer = self.l1_penalty_max_outer_iter().max(1);
829
830        let mut wrapper = pounce_l1penalty::L1PenaltyBarrierTnlp::new(Rc::clone(&tnlp), rho_init)?;
831        if wrapper.m_eq() == 0 {
832            // Nothing to slack — let the standard dispatch path handle
833            // this TNLP unmodified.
834            return None;
835        }
836        wrapper.set_defer_inner_finalize(true);
837        let wrapper_rc = Rc::new(RefCell::new(wrapper));
838
839        let mut rho = rho_init;
840        let mut last_status = ApplicationReturnStatus::InternalError;
841        for _outer in 0..max_outer {
842            wrapper_rc.borrow_mut().set_rho(rho);
843            let dyn_tnlp: Rc<RefCell<dyn TNLP>> = wrapper_rc.clone();
844            last_status = self.optimize_constrained(dyn_tnlp);
845
846            let w = wrapper_rc.borrow();
847            if !w.has_solution() {
848                // Inner solve aborted before producing an iterate.
849                drop(w);
850                break;
851            }
852            let slack_sum = w.last_slack_sum();
853            let y_eq_inf = w.last_y_eq_inf_norm();
854            drop(w);
855
856            // Termination decisions.
857            let inner_ok = matches!(
858                last_status,
859                ApplicationReturnStatus::SolveSucceeded
860                    | ApplicationReturnStatus::SolvedToAcceptableLevel
861            );
862            if !inner_ok {
863                break;
864            }
865            if slack_sum.is_finite() && slack_sum <= slack_tol {
866                break;
867            }
868            if rho >= rho_max {
869                break;
870            }
871            // BNW steering: ρ_new = max(ρ·factor, τ·‖y_eq‖∞ + ε)
872            let geom = rho * factor;
873            let steer = tau * y_eq_inf + 1.0e-12;
874            rho = geom.max(steer).min(rho_max);
875        }
876
877        // Forward to the user's inner.finalize_solution exactly once.
878        let w = wrapper_rc.borrow();
879        if w.has_solution() {
880            let x_trunc: Vec<Number> = w.last_x_trunc().to_vec();
881            let lambda: Vec<Number> = w.last_lambda().to_vec();
882            let z_l: Vec<Number> = w.last_z_l_trunc().to_vec();
883            let z_u: Vec<Number> = w.last_z_u_trunc().to_vec();
884            let solver_status = w.last_status().unwrap_or(SolverReturn::InternalError);
885            let slack_sum = w.last_slack_sum();
886            drop(w);
887
888            // Honest-infeasibility upgrade (Phase 3): if the inner
889            // solve says SolveSucceeded / SolvedToAcceptableLevel but
890            // the slacks did not collapse, the original problem is
891            // locally infeasible at the returned point. Override the
892            // application status; the user-visible Solution.status is
893            // updated below to the matching SolverReturn so the inner
894            // TNLP sees a consistent picture.
895            let infeasible_certificate = matches!(
896                last_status,
897                ApplicationReturnStatus::SolveSucceeded
898                    | ApplicationReturnStatus::SolvedToAcceptableLevel
899            ) && slack_sum.is_finite()
900                && slack_sum > slack_tol;
901            let final_app_status = if infeasible_certificate {
902                ApplicationReturnStatus::InfeasibleProblemDetected
903            } else {
904                last_status
905            };
906            let final_solver_status = if infeasible_certificate {
907                SolverReturn::LocalInfeasibility
908            } else {
909                solver_status
910            };
911
912            // Recompute f(x*) and c(x*) on the inner.
913            let f_inner = tnlp
914                .borrow_mut()
915                .eval_f(&x_trunc, true)
916                .unwrap_or(Number::NAN);
917            let m = tnlp
918                .borrow_mut()
919                .get_nlp_info()
920                .map(|i| i.m as usize)
921                .unwrap_or(0);
922            let mut g_inner = vec![0.0; m];
923            if m > 0 {
924                let _ = tnlp.borrow_mut().eval_g(&x_trunc, false, &mut g_inner);
925            }
926            tnlp.borrow_mut().finalize_solution(
927                Solution {
928                    status: final_solver_status,
929                    x: &x_trunc,
930                    z_l: &z_l,
931                    z_u: &z_u,
932                    g: &g_inner,
933                    lambda: &lambda,
934                    obj_value: f_inner,
935                },
936                &TnlpIpoptData::default(),
937                &TnlpIpoptCq::default(),
938            );
939            return Some(final_app_status);
940        }
941        // No solution captured at all — pass the inner status through.
942        Some(last_status)
943    }
944
945    /// Constrained-NLP path: build adapter → OrigIpoptNlp → algorithm
946    /// bundle, run `optimize`, populate statistics, and call
947    /// `finalize_solution` on the user's TNLP.
948    fn optimize_constrained(&mut self, tnlp: Rc<RefCell<dyn TNLP>>) -> ApplicationReturnStatus {
949        let t_start = Instant::now();
950
951        // `print_user_options yes` — dump the OptionsList before the
952        // solve. Mirrors `IpoptApplication::call_optimize` (upstream
953        // calls `Jnlst().Printf(.., "%s", options_->PrintUserOptions())`).
954        let print_opts = self
955            .options
956            .get_bool_value("print_user_options", "")
957            .ok()
958            .and_then(|(v, f)| f.then_some(v))
959            .unwrap_or(false);
960        if print_opts {
961            print!(
962                "\nList of user-set options:\n\n{}",
963                self.options.print_user_options()
964            );
965        }
966
967        // `print_options_documentation yes` — dump the full registry
968        // (every option with type, default, valid range/strings, and
969        // long description) before the solve. Honors
970        // `print_options_mode` (`text` / `latex` / `doxygen`; only
971        // `text` is implemented today, the others fall through with a
972        // one-line note) and `print_advanced_options`. Mirrors
973        // upstream `IpoptApplication::call_optimize`'s
974        // `print_options_documentation` branch and `Common/IpRegOptions.cpp`
975        // `OutputOptionDocumentation`.
976        let print_doc = self
977            .options
978            .get_bool_value("print_options_documentation", "")
979            .ok()
980            .and_then(|(v, f)| f.then_some(v))
981            .unwrap_or(false);
982        if print_doc {
983            let mode = self
984                .options
985                .get_string_value("print_options_mode", "")
986                .ok()
987                .map(|(v, _)| PrintOptionsMode::from_tag(&v))
988                .unwrap_or(PrintOptionsMode::Text);
989            let advanced = self
990                .options
991                .get_bool_value("print_advanced_options", "")
992                .ok()
993                .map(|(v, _)| v)
994                .unwrap_or(false);
995            print!(
996                "\n# Pounce options registry\n\n{}",
997                self.reg_options.print_options_documentation(mode, advanced)
998            );
999        }
1000
1001        // Mint a fresh `TimingStatistics` for this solve — shared (via
1002        // `Rc`) with the data and the NLP below so every `eval_*` and
1003        // every iterate-phase records into the same accumulator. The
1004        // application keeps its own `Rc` so callers can read totals out
1005        // via [`Self::timing_stats`].
1006        let timing = Rc::new(TimingStatistics::new());
1007        *self.timing.borrow_mut() = Rc::clone(&timing);
1008        timing.overall_alg.start();
1009
1010        // Reset the linear-solver summary sink so back-to-back solves
1011        // don't bleed factor counters / extremal pivots into each
1012        // other. Surviving the lock failure with a debug-assert keeps
1013        // a poisoned mutex from sinking a release build that doesn't
1014        // even consume the summary.
1015        if let Ok(mut guard) = self.linsol_summary_sink.lock() {
1016            *guard = LinearSolverSummary::default();
1017        } else {
1018            debug_assert!(false, "linsol summary sink mutex poisoned");
1019        }
1020
1021        // Build adapter + Nlp. Honor `fixed_variable_treatment` (default
1022        // `make_parameter`; pounce additionally implements `relax_bounds`,
1023        // which the adapter also auto-selects as a fallback when
1024        // `make_parameter` would leave `n_x_var < n_c` — mirrors upstream
1025        // `IpTNLPAdapter.cpp:623-633`).
1026        let lo_inf = self
1027            .options
1028            .get_numeric_value("nlp_lower_bound_inf", "")
1029            .ok()
1030            .and_then(|(v, f)| f.then_some(v))
1031            .unwrap_or(DEFAULT_NLP_LOWER_BOUND_INF);
1032        let up_inf = self
1033            .options
1034            .get_numeric_value("nlp_upper_bound_inf", "")
1035            .ok()
1036            .and_then(|(v, f)| f.then_some(v))
1037            .unwrap_or(DEFAULT_NLP_UPPER_BOUND_INF);
1038        let fixed_treatment = match self
1039            .options
1040            .get_string_value("fixed_variable_treatment", "")
1041            .ok()
1042            .and_then(|(v, f)| f.then_some(v))
1043            .as_deref()
1044        {
1045            Some("relax_bounds") => FixedVarTreatment::RelaxBounds,
1046            // `make_constraint` / `make_parameter_nodual` not yet
1047            // implemented; fall back to `make_parameter` (auto-retry to
1048            // `relax_bounds` will still kick in if DOF runs short).
1049            _ => FixedVarTreatment::MakeParameter,
1050        };
1051        let adapter = match TNLPAdapter::new_with_options(
1052            Rc::clone(&tnlp),
1053            lo_inf,
1054            up_inf,
1055            fixed_treatment,
1056        ) {
1057            Ok(a) => Rc::new(RefCell::new(a)),
1058            Err(_) => {
1059                timing.overall_alg.end();
1060                return ApplicationReturnStatus::InvalidProblemDefinition;
1061            }
1062        };
1063        // Carry the user's constant `obj_scaling_factor` (default 1.0;
1064        // negative ⇒ maximize) into the NLP. Until pounce#128's
1065        // follow-up this option was registered but never read, so it
1066        // was silently a no-op — maximization diverged because the
1067        // algorithm minimized the unscaled objective.
1068        let obj_scaling_factor = self
1069            .options
1070            .get_numeric_value("obj_scaling_factor", "")
1071            .ok()
1072            .and_then(|(v, f)| f.then_some(v))
1073            .unwrap_or(1.0);
1074        let mut orig_nlp = match OrigIpoptNlp::new(
1075            Rc::clone(&adapter),
1076            Rc::new(ConstObjScaling(obj_scaling_factor)),
1077        ) {
1078            Ok(n) => n,
1079            Err(_) => {
1080                timing.overall_alg.end();
1081                return ApplicationReturnStatus::InternalError;
1082            }
1083        };
1084        orig_nlp.set_timing_stats(Rc::clone(&timing));
1085
1086        // Mirror upstream `OrigIpoptNLP::InitializeStructures` (IpOrigIpoptNLP.cpp:299):
1087        // bail out with NotEnoughDegreesOfFreedom when there are fewer free
1088        // variables than equality constraints. Without this gate, square /
1089        // over-determined systems push the algorithm into restoration on
1090        // iter 0 and exit Restoration_Failed instead of the cleaner DOF code.
1091        let n_x_var = orig_nlp.x_space().dim();
1092        let n_c = orig_nlp.c_space().dim();
1093        if n_x_var > 0 && n_x_var < n_c {
1094            timing.overall_alg.end();
1095            return ApplicationReturnStatus::NotEnoughDegreesOfFreedom;
1096        }
1097
1098        // Relax `x_L / x_U / d_L / d_U` by `bound_relax_factor` (default
1099        // 1e-8), capped by `constr_viol_tol` (default 1e-4). Matches
1100        // `OrigIpoptNLP::InitializeStructures` lines 343-358.
1101        let bound_relax_factor = self
1102            .options
1103            .get_numeric_value("bound_relax_factor", "")
1104            .ok()
1105            .and_then(|(v, f)| f.then_some(v))
1106            .unwrap_or(1e-8);
1107        let constr_viol_tol = self
1108            .options
1109            .get_numeric_value("constr_viol_tol", "")
1110            .ok()
1111            .and_then(|(v, f)| f.then_some(v))
1112            .unwrap_or(1e-4);
1113        orig_nlp.relax_bounds(bound_relax_factor, constr_viol_tol);
1114
1115        // Apply automatic NLP scaling per `nlp_scaling_method` option
1116        // (port of `OrigIpoptNLP::InitializeStructures` →
1117        // `NLPScalingObject::DetermineScaling`). Default is
1118        // `gradient-based` to match upstream Ipopt 3.14.
1119        let scaling_method = self
1120            .options
1121            .get_string_value("nlp_scaling_method", "")
1122            .ok()
1123            .and_then(|(v, f)| f.then_some(v))
1124            .unwrap_or_else(|| "gradient-based".to_string());
1125        let scaling_method = match scaling_method.as_str() {
1126            "none" => ScalingMethod::None,
1127            "gradient-based" => ScalingMethod::GradientBased,
1128            "user-scaling" => ScalingMethod::UserScaling,
1129            // `equilibration-based` is registered upstream but not yet
1130            // implemented in pounce; fall back to gradient-based (the
1131            // upstream default) to keep behavior predictable.
1132            _ => ScalingMethod::GradientBased,
1133        };
1134        let max_gradient = self
1135            .options
1136            .get_numeric_value("nlp_scaling_max_gradient", "")
1137            .ok()
1138            .and_then(|(v, f)| f.then_some(v))
1139            .unwrap_or(100.0);
1140        let min_value = self
1141            .options
1142            .get_numeric_value("nlp_scaling_min_value", "")
1143            .ok()
1144            .and_then(|(v, f)| f.then_some(v))
1145            .unwrap_or(1e-8);
1146        let obj_target_gradient = self
1147            .options
1148            .get_numeric_value("nlp_scaling_obj_target_gradient", "")
1149            .ok()
1150            .and_then(|(v, f)| f.then_some(v))
1151            .unwrap_or(0.0);
1152        let constr_target_gradient = self
1153            .options
1154            .get_numeric_value("nlp_scaling_constr_target_gradient", "")
1155            .ok()
1156            .and_then(|(v, f)| f.then_some(v))
1157            .unwrap_or(0.0);
1158        orig_nlp.determine_scaling_from_starting_point(
1159            scaling_method,
1160            max_gradient,
1161            min_value,
1162            obj_target_gradient,
1163            constr_target_gradient,
1164        );
1165
1166        let nlp_handle: Rc<RefCell<dyn IpoptNlp>> = Rc::new(RefCell::new(orig_nlp));
1167
1168        // Build the algorithm strategy bundle. Read coarse knobs from
1169        // the OptionsList where we have them; fall through to defaults
1170        // otherwise. The full upstream parsing surface (mu_strategy,
1171        // hessian_approximation, line_search_method, ...) is wired by
1172        // `AlgBuilder::RegisterOptions` in upstream — that registry
1173        // hookup lands as a follow-up; default builder is correct for
1174        // HS71-class problems.
1175        let builder = self.algorithm_builder_from_options();
1176
1177        // Linear-solver backend. The default factory is option-aware
1178        // — it reads the `feral_*` extension options off the same
1179        // `OptionsList` that drove the IPM-level builder above so
1180        // per-problem `.opt` files can flip backend knobs without
1181        // rebuilding pounce.
1182        let feral_cfg = feral_config_from_options(&self.options);
1183        let factory = self.linear_backend_factory.take().unwrap_or_else(|| {
1184            default_backend_factory_with_sink(feral_cfg, Arc::clone(&self.linsol_summary_sink))
1185        });
1186        let bundle = builder.build_with_backend(factory);
1187
1188        // Wire the data / cq pair around the NLP. Install the shared
1189        // `TimingStatistics` so the algorithm's iterate phases
1190        // (output, convergence, hessian, μ, search-direction,
1191        // line-search, accept) all record into the same accumulator
1192        // the application exposes via `timing_stats()`.
1193        let data: crate::ipopt_data::IpoptDataHandle = Rc::new(RefCell::new(AlgIpoptData::new()));
1194        data.borrow_mut().timing = Rc::clone(&timing);
1195        let cq: crate::ipopt_cq::IpoptCqHandle = Rc::new(RefCell::new(
1196            IpoptCalculatedQuantities::new(Rc::clone(&data), Rc::clone(&nlp_handle)),
1197        ));
1198        // Correction size for very small slacks (default mach_eps^{3/4});
1199        // drives the safe-slack bound-adjustment mechanism.
1200        if let Ok((v, true)) = self.options.get_numeric_value("slack_move", "") {
1201            cq.borrow_mut().slack_move = v;
1202        }
1203
1204        // Seed `data.curr` with a zero-valued iterate of the correct
1205        // dimensions. The `IterateInitializer` consumes these as its
1206        // template (it overwrites `x`, `s`, multipliers in place); we
1207        // just need the dim metadata.
1208        {
1209            let nlp_borrow = nlp_handle.borrow();
1210            let n_x = nlp_borrow.n();
1211            let n_s = nlp_borrow.m_ineq();
1212            let n_yc = nlp_borrow.m_eq();
1213            let n_yd = nlp_borrow.m_ineq();
1214            let n_zl = nlp_borrow.x_l().dim();
1215            let n_zu = nlp_borrow.x_u().dim();
1216            let n_vl = nlp_borrow.d_l().dim();
1217            let n_vu = nlp_borrow.d_u().dim();
1218            drop(nlp_borrow);
1219            let iv = IteratesVector::new(
1220                Rc::new(DenseVectorSpace::new(n_x).make_new_dense()),
1221                Rc::new(DenseVectorSpace::new(n_s).make_new_dense()),
1222                Rc::new(DenseVectorSpace::new(n_yc).make_new_dense()),
1223                Rc::new(DenseVectorSpace::new(n_yd).make_new_dense()),
1224                Rc::new(DenseVectorSpace::new(n_zl).make_new_dense()),
1225                Rc::new(DenseVectorSpace::new(n_zu).make_new_dense()),
1226                Rc::new(DenseVectorSpace::new(n_vl).make_new_dense()),
1227                Rc::new(DenseVectorSpace::new(n_vu).make_new_dense()),
1228            );
1229            data.borrow_mut().set_curr(iv);
1230        }
1231
1232        // Full primal-dual warm restart (debugger `resolve`): if a
1233        // captured iterate is queued, install it onto `data.curr` over
1234        // the placeholder so the `WarmStartIterateInitializer`'s
1235        // re-optimize branch (x already initialized) keeps it and only
1236        // clamps multipliers / sets target_mu — no cold re-seed from the
1237        // NLP. Skipped (with a warning) if the dimensions don't line up,
1238        // e.g. an option changed the problem structure between solves.
1239        if let Some(snap) = self.warm_start_iterate.take() {
1240            let dims_match = {
1241                let borrow = data.borrow();
1242                borrow
1243                    .curr
1244                    .as_ref()
1245                    .map(|c| iterates_dims(c) == iterates_dims(snap.iterates()))
1246                    .unwrap_or(false)
1247            };
1248            if dims_match {
1249                data.borrow_mut().set_curr(snap.iterates().clone());
1250                data.borrow_mut().curr_mu = snap.mu();
1251            } else {
1252                tracing::warn!(
1253                    target: "pounce::warm_start",
1254                    "debugger warm-restart iterate dimensions differ from the fresh \
1255                     solve; ignoring the captured iterate and seeding normally"
1256                );
1257            }
1258        }
1259
1260        let max_iter = self
1261            .options
1262            .get_integer_value("max_iter", "")
1263            .ok()
1264            .and_then(|(v, f)| f.then_some(v))
1265            .unwrap_or(3000);
1266        let tol = self
1267            .options
1268            .get_numeric_value("tol", "")
1269            .ok()
1270            .and_then(|(v, f)| f.then_some(v))
1271            .unwrap_or(1e-8);
1272        data.borrow_mut().tol = tol;
1273
1274        let mut alg = IpoptAlgorithm::new(data, cq, bundle)
1275            .with_nlp(Rc::clone(&nlp_handle))
1276            .with_tnlp(Rc::clone(&tnlp));
1277        // Mint a fresh restoration factory per inner solve if a
1278        // provider is configured (pounce#10 Phase 3). Falls back to
1279        // the legacy one-shot `restoration_factory` slot when no
1280        // provider is set, preserving single-shot caller behavior.
1281        if let Some(provider) = self.restoration_factory_provider.as_mut() {
1282            self.restoration_factory = Some(provider());
1283        }
1284        if let Some(factory) = self.restoration_factory.as_mut() {
1285            alg = alg.with_restoration(factory());
1286        }
1287        if let Some(diag) = self.diagnostics.as_ref() {
1288            alg = alg.with_diagnostics(Rc::clone(diag));
1289        }
1290        // Move the interactive debugger hook (if any) into the main
1291        // algorithm. Taken — not cloned — so it drives exactly this
1292        // solve; a subsequent solve must reinstall it.
1293        if let Some(hook) = self.debug_hook.take() {
1294            alg = alg.with_debug_hook(hook);
1295        }
1296        alg.max_iter = max_iter;
1297        // Honor `print_level == 0`: suppress the per-iteration table
1298        // that the algorithm writes straight to stdout. (The Phase-7
1299        // journalist surface respects `print_level` already; this is
1300        // the legacy direct-print site that needs the same gate.)
1301        if let Ok((v, found)) = self.options.get_integer_value("print_level", "") {
1302            if found && v <= 0 {
1303                alg.print_iter_output = false;
1304                // The nested restoration IPM is built inside the
1305                // restoration driver, not by `IpoptAlgorithm::new`, so
1306                // it never sees this gate unless we forward it.
1307                if let Some(resto) = alg.restoration.as_mut() {
1308                    resto.set_print_iter_output(false);
1309                }
1310            }
1311        }
1312
1313        // Per-iteration history (pounce#71): when requested, capture the
1314        // `pounce::iteration` events emitted during the solve into an
1315        // `IterRecord` trajectory via the observability collector layer.
1316        // This replaces the old in-loop `iter_history` accumulation; it
1317        // requires the collector to be installed in the active
1318        // subscriber (the CLI / Python / C frontends install it via
1319        // `pounce_observability::init_subscriber`; tests call
1320        // `init_for_tests`). The collector scopes out restoration
1321        // sub-solve iterations via the `restoration` span, so the
1322        // trajectory matches the previous behavior (outer iters only).
1323        let iter_capture = self
1324            .record_iter_history
1325            .then(pounce_observability::IterCaptureGuard::start);
1326
1327        let solver_status = alg.optimize();
1328
1329        let captured_iters = iter_capture.map(|g| g.finish()).unwrap_or_default();
1330        // Close the overall-algorithm timer on the success path. The
1331        // early-return arms above end it themselves before bailing out;
1332        // this one matches upstream `IpoptApplication::call_optimize`
1333        // (which calls `EndCpuTime()` on overall_alg right after
1334        // `Optimize` returns, regardless of solver_status).
1335        timing.overall_alg.end();
1336
1337        // Drain counters / iter count off the algorithm.
1338        {
1339            let mut stats = self.statistics.borrow_mut();
1340            {
1341                let d = alg.data.borrow();
1342                stats.iteration_count = d.iter_count;
1343                // Converged barrier parameter μ — threaded forward into a
1344                // warm-started corrector's `mu_init` / `warm_start_target_mu`
1345                // for predictor–corrector path following (pounce#86).
1346                stats.final_mu = d.curr_mu;
1347            }
1348            stats.total_wallclock_time_secs = t_start.elapsed().as_secs_f64();
1349            // Restoration-phase audit counters (pounce#12). Zero on
1350            // problems where restoration never fires; populated by
1351            // `IpoptAlgorithm::invoke_restoration`.
1352            stats.restoration_calls = alg.resto_calls;
1353            stats.restoration_inner_iters = alg.resto_inner_iters;
1354            stats.restoration_outer_iters = alg.resto_outer_iters;
1355            stats.restoration_wall_secs = alg.resto_wall_secs;
1356            stats.iterations = captured_iters;
1357            // Capture the final *scaled* objective at the algorithm's
1358            // (compressed `x_var`-space) iterate via the NLP: the
1359            // algorithm-side `eval_f` returns `f * obj_scale_factor`.
1360            // `final_objective` is seeded with it only as a best-effort
1361            // fallback; the success path below overwrites it with the
1362            // true unscaled objective from `finalize_via_orig_nlp`
1363            // (which evaluates the user TNLP directly).
1364            let curr_x = alg.data.borrow().curr.as_ref().map(|c| c.x.clone());
1365            if let Some(x) = curr_x {
1366                if let Ok(f) = try_eval_curr_f(&nlp_handle, &x) {
1367                    stats.final_objective = f;
1368                    stats.final_scaled_objective = f;
1369                }
1370            }
1371            // Final residuals straight off the cq cache. These mirror
1372            // the values upstream prints in its end-of-run summary
1373            // ("Dual infeasibility / Constraint violation /
1374            // Complementarity / Overall NLP error").
1375            let cq = alg.cq.borrow();
1376            stats.final_dual_inf = cq.curr_dual_infeasibility_max();
1377            stats.final_constr_viol = cq.curr_primal_infeasibility_max();
1378            // Infinity-norm complementarity, max over all four bound
1379            // blocks (s_xl·z_l, s_xu·z_u, s_sl·v_l, s_su·v_u). The
1380            // empty-bound blocks return `0` from amax(), so the max is
1381            // safe even when only one side has bounds.
1382            let compl = cq
1383                .curr_compl_x_l()
1384                .amax()
1385                .max(cq.curr_compl_x_u().amax())
1386                .max(cq.curr_compl_s_l().amax())
1387                .max(cq.curr_compl_s_u().amax());
1388            stats.final_compl = compl;
1389            stats.final_kkt_error = cq.curr_nlp_error();
1390        }
1391
1392        // Map SolverReturn → ApplicationReturnStatus per
1393        // MAIN_LOOP.md's exception table.
1394        let app_status = solver_return_to_app_status(solver_status);
1395
1396        // On convergence, fire the user-supplied callback (post-optimal
1397        // sensitivity hook, pounce#16) before flowing back through
1398        // `finalize_via_orig_nlp`. Borrowed handles into the converged
1399        // KKT state stay alive for the duration of the closure.
1400        if matches!(
1401            app_status,
1402            ApplicationReturnStatus::SolveSucceeded
1403                | ApplicationReturnStatus::SolvedToAcceptableLevel
1404        ) {
1405            if let Some(cb) = self.on_converged.as_mut() {
1406                if let Some(sd) = alg.search_dir.as_mut() {
1407                    let pd = sd.pd_solver_rc();
1408                    cb(&alg.data, &alg.cq, &nlp_handle, pd);
1409                }
1410            }
1411        }
1412
1413        // Finalize: forward the final iterate to the user's TNLP. The
1414        // returned objective is evaluated on the *user* TNLP at the
1415        // unscaled iterate, so it overrides the scaled best-effort
1416        // value stashed in `final_objective` above (the algorithm-side
1417        // `eval_f` returns `f * obj_scale_factor`).
1418        match finalize_via_orig_nlp(&nlp_handle, &alg, solver_status, app_status, &tnlp) {
1419            Ok(f_unscaled) => {
1420                self.statistics.borrow_mut().final_objective = f_unscaled;
1421            }
1422            Err(()) => {
1423                // Couldn't finalize; keep the scaled fallback and
1424                // surface the original status.
1425            }
1426        }
1427
1428        // End-of-solve timing report. Gated on `print_timing_statistics`
1429        // (default "no"); mirrors upstream's
1430        // `IpoptApplication::call_optimize` →
1431        // `IpTimingStatistics::PrintAllValues` call site. The report
1432        // goes to stdout (for parity with the banner / iter-row output
1433        // path) and is also fanned out to the journalist so an
1434        // `output_file` attached via `Initialize` picks it up.
1435        let print_timing = self
1436            .options
1437            .get_bool_value("print_timing_statistics", "")
1438            .ok()
1439            .and_then(|(v, f)| f.then_some(v))
1440            .unwrap_or(false);
1441        if print_timing {
1442            let report = timing.report();
1443            print!("{}", report);
1444            use pounce_common::journalist::{JournalCategory, JournalLevel};
1445            self.journalist.print(
1446                JournalLevel::J_SUMMARY,
1447                JournalCategory::J_TIMING_STATISTICS,
1448                &report,
1449            );
1450        }
1451
1452        app_status
1453    }
1454
1455    /// Build an [`AlgorithmBuilder`] populated from the app's
1456    /// [`OptionsList`]. Public so callers wiring the restoration
1457    /// factory can hand the *inner* IPM a builder that mirrors the
1458    /// outer's `mu_strategy`/`mu_oracle`/line-search choices —
1459    /// matching upstream `IpAlgBuilder::BuildRestoIpoptAlgorithm`,
1460    /// which reads the same `mu_strategy` option with prefix `"resto."
1461    /// + prefix` and falls back to the outer setting.
1462    pub fn algorithm_builder_from_options(&self) -> AlgorithmBuilder {
1463        let mut builder = AlgorithmBuilder::new();
1464
1465        // `mehrotra_algorithm` is parsed first so its cascading
1466        // defaults (mu_strategy=adaptive, mu_oracle=probing) can be
1467        // overridden by an explicit user setting of those keys
1468        // below. Mirrors `IpAlgBuilder.cpp:Mehrotra`.
1469        let mut mehrotra_on = false;
1470        if let Ok((v, found)) = self.options.get_string_value("mehrotra_algorithm", "") {
1471            if found && v == "yes" {
1472                mehrotra_on = true;
1473                builder.mehrotra_algorithm = true;
1474                builder.mu_strategy = MuStrategyChoice::Adaptive;
1475                builder.mu_oracle = crate::mu::adaptive::MuOracleKind::Probing;
1476                // `accept_every_trial_step` short-circuits the alpha
1477                // loop / filter — Mehrotra steps would otherwise be
1478                // rejected by the filter on LP-shaped problems because
1479                // the barrier objective is non-monotone along the
1480                // corrector. Mirrors upstream `IpAlgBuilder.cpp:Mehrotra`.
1481                builder.line_search.accept_every_trial_step = true;
1482                // Aggressive iterate-push defaults (`SetNumericValueIfUnset`
1483                // in upstream). The explicit user parses below will
1484                // overwrite these if the user set them explicitly.
1485                builder.init.bound_push = 10.0;
1486                builder.init.bound_frac = 0.2;
1487                builder.init.slack_bound_push = 10.0;
1488                builder.init.slack_bound_frac = 0.2;
1489                builder.init.bound_mult_init_val = 10.0;
1490                builder.init.constr_mult_init_max = 0.0;
1491                // `alpha_for_y=bound_mult` — Mehrotra wants the
1492                // equality multipliers to advance with the dual
1493                // alpha so they stay in step with z/v. Mirrors
1494                // upstream `IpIpoptAlg.cpp:InitializeImpl`.
1495                builder.line_search.alpha_for_y =
1496                    crate::line_search::backtracking::AlphaForY::BoundMult;
1497                // `adaptive_mu_globalization=never-monotone-mode` —
1498                // upstream `IpIpoptAlg.cpp:148-154` enforces this:
1499                // Mehrotra disables the globalization switch entirely
1500                // (no fallback to monotone mode when convergence
1501                // stalls). Required for the unsafeguarded Mehrotra
1502                // path to function.
1503                builder.mu.adaptive_mu_globalization =
1504                    crate::mu::adaptive::AdaptiveMuGlobalization::NeverMonotoneMode;
1505                // `least_square_init_primal=yes` — upstream
1506                // `IpIpoptAlg.cpp:182` enables this for the Mehrotra
1507                // cascade. Replaces the user's starting `x` with the
1508                // min-norm primal that satisfies the linearized
1509                // equality+inequality constraints. Critical on
1510                // LP-shaped problems where the user's starting point
1511                // can be wildly infeasible (e.g. nuffield2_trap).
1512                builder.init.least_square_init_primal = true;
1513            }
1514        }
1515
1516        if let Ok((v, found)) = self.options.get_string_value("mu_strategy", "") {
1517            if found {
1518                let parsed = match v.as_str() {
1519                    "adaptive" => MuStrategyChoice::Adaptive,
1520                    _ => MuStrategyChoice::Monotone,
1521                };
1522                if mehrotra_on && matches!(parsed, MuStrategyChoice::Monotone) {
1523                    // Upstream Ipopt refuses this combination: Mehrotra
1524                    // needs an affine step every iter, which only the
1525                    // adaptive path computes. Keep adaptive and warn.
1526                    tracing::warn!(target: "pounce::algorithm",
1527                        "pounce: mehrotra_algorithm=yes requires \
1528                         mu_strategy=adaptive; ignoring \
1529                         mu_strategy=monotone."
1530                    );
1531                } else {
1532                    builder.mu_strategy = parsed;
1533                }
1534            }
1535        }
1536        if let Ok((v, found)) = self.options.get_string_value("mu_oracle", "") {
1537            if found {
1538                builder.mu_oracle = match v.as_str() {
1539                    "loqo" => crate::mu::adaptive::MuOracleKind::Loqo,
1540                    "probing" => crate::mu::adaptive::MuOracleKind::Probing,
1541                    _ => crate::mu::adaptive::MuOracleKind::QualityFunction,
1542                };
1543            }
1544        }
1545        if let Ok((v, found)) = self
1546            .options
1547            .get_string_value("adaptive_mu_globalization", "")
1548        {
1549            if found {
1550                use crate::mu::adaptive::AdaptiveMuGlobalization;
1551                builder.mu.adaptive_mu_globalization = match v.as_str() {
1552                    "kkt-error" => AdaptiveMuGlobalization::KktError,
1553                    "never-monotone-mode" => AdaptiveMuGlobalization::NeverMonotoneMode,
1554                    _ => AdaptiveMuGlobalization::ObjConstrFilter,
1555                };
1556            }
1557        }
1558        if let Ok((v, found)) = self.options.get_string_value("hessian_approximation", "") {
1559            if found {
1560                builder.hessian_approximation = match v.as_str() {
1561                    "limited-memory" => HessianApproxChoice::LimitedMemory,
1562                    _ => HessianApproxChoice::Exact,
1563                };
1564            }
1565        }
1566        // Limited-memory quasi-Newton update formula. Registered upstream
1567        // (`limited_memory_update_type`, IpLimMemQuasiNewtonUpdater.cpp) but
1568        // until now read nowhere on the IPM path — the updater was hard-wired
1569        // to Powell-damped BFGS. SR1 is honored too (the updater and the
1570        // low-rank/inertia path already handle its indefinite models).
1571        if let Ok((v, found)) = self
1572            .options
1573            .get_string_value("limited_memory_update_type", "")
1574        {
1575            if found {
1576                builder.limited_memory_update_type = match v.as_str() {
1577                    "sr1" => UpdateType::Sr1,
1578                    _ => UpdateType::Bfgs,
1579                };
1580            }
1581        }
1582        // Limited-memory history length (`limited_memory_max_history`).
1583        if let Ok((v, found)) = self
1584            .options
1585            .get_integer_value("limited_memory_max_history", "")
1586        {
1587            if found && v >= 0 {
1588                builder.limited_memory_max_history = v as Index;
1589            }
1590        }
1591        if let Ok((v, found)) = self.options.get_string_value("line_search_method", "") {
1592            if found {
1593                builder.line_search_method = match v.as_str() {
1594                    "cg-penalty" => LineSearchChoice::CgPenalty,
1595                    "penalty" => LineSearchChoice::Penalty,
1596                    _ => LineSearchChoice::Filter,
1597                };
1598            }
1599        }
1600        // `accept_every_trial_step` — direct user override. Parsed
1601        // after the Mehrotra cascade so an explicit `no` still wins.
1602        if let Ok((v, found)) = self.options.get_string_value("accept_every_trial_step", "") {
1603            if found {
1604                builder.line_search.accept_every_trial_step = v == "yes";
1605            }
1606        }
1607        // `alpha_for_y` — direct user override. Parsed after the
1608        // Mehrotra cascade so an explicit value still wins.
1609        if let Ok((v, found)) = self.options.get_string_value("alpha_for_y", "") {
1610            if found {
1611                use crate::line_search::backtracking::AlphaForY;
1612                builder.line_search.alpha_for_y = match v.as_str() {
1613                    "primal" => AlphaForY::Primal,
1614                    "bound-mult" | "bound_mult" => AlphaForY::BoundMult,
1615                    "full" => AlphaForY::Full,
1616                    "min" => AlphaForY::Min,
1617                    "max" => AlphaForY::Max,
1618                    "primal-and-full" | "dual-and-full" => AlphaForY::Primal,
1619                    _ => AlphaForY::Primal,
1620                };
1621            }
1622        }
1623        // `nlp_scaling_method` is consumed NLP-side in
1624        // `OrigIpoptNlp::determine_scaling_from_starting_point` (see the
1625        // `determine_scaling_from_starting_point` call earlier in this
1626        // method); there is no algorithm-side scaling strategy to wire.
1627
1628        // Unlike the other options here, we always honor the registry
1629        // value (not just when the user set it explicitly): the option
1630        // registry default is "ma57" but `AlgorithmBuilder::default`
1631        // has `linear_solver: Feral`, so gating on `found` would
1632        // silently route default runs through Feral while the banner
1633        // (and ipopt-compatible behavior) advertises MA57.
1634        if let Ok((v, _found)) = self.options.get_string_value("linear_solver", "") {
1635            builder.linear_solver = match v.as_str() {
1636                "ma57" => LinearSolverChoice::Ma57,
1637                _ => LinearSolverChoice::Feral,
1638            };
1639        }
1640
1641        // `linear_system_scaling` — symmetric scaling of the augmented
1642        // KKT matrix before factorization. Port of
1643        // `IpTSymLinearSolver.cpp:RegisterOptions` plumbing. Default
1644        // "none"; "ruiz" invokes the Ruiz-2001 symmetric ∞-norm
1645        // equilibration in `RuizTSymScalingMethod`. "mc19" and
1646        // "slack-based" are accepted by the registry but not yet
1647        // implemented at this layer; they fall back to no scaling
1648        // with a one-line stderr notice.
1649        if let Ok((v, found)) = self.options.get_string_value("linear_system_scaling", "") {
1650            if found {
1651                builder.linear_system_scaling = match v.as_str() {
1652                    "ruiz" => crate::alg_builder::LinearSystemScalingChoice::Ruiz,
1653                    "mc19" => crate::alg_builder::LinearSystemScalingChoice::Mc19,
1654                    _ => crate::alg_builder::LinearSystemScalingChoice::None,
1655                };
1656            }
1657        }
1658        if let Ok((v, found)) = self.options.get_bool_value("linear_scaling_on_demand", "") {
1659            if found {
1660                builder.linear_scaling_on_demand = v;
1661            }
1662        }
1663
1664        // Convergence tolerances (port of `IpOptErrorConvCheck.cpp`'s
1665        // `RegisterOptions` consumers). Defaults already match upstream
1666        // — only override when the user set the key explicitly.
1667        let read_num = |key: &str| -> Option<f64> {
1668            self.options
1669                .get_numeric_value(key, "")
1670                .ok()
1671                .and_then(|(v, f)| f.then_some(v))
1672        };
1673        let read_int = |key: &str| -> Option<i32> {
1674            self.options
1675                .get_integer_value(key, "")
1676                .ok()
1677                .and_then(|(v, f)| f.then_some(v))
1678        };
1679        if let Some(v) = read_num("tol") {
1680            builder.conv_check.tol = v;
1681        }
1682        if let Some(v) = read_num("dual_inf_tol") {
1683            builder.conv_check.dual_inf_tol = v;
1684        }
1685        if let Some(v) = read_num("constr_viol_tol") {
1686            builder.conv_check.constr_viol_tol = v;
1687        }
1688        if let Some(v) = read_num("compl_inf_tol") {
1689            builder.conv_check.compl_inf_tol = v;
1690        }
1691        if let Some(v) = read_int("max_iter") {
1692            builder.conv_check.max_iter = v;
1693        }
1694        if let Some(v) = read_num("max_cpu_time") {
1695            builder.conv_check.max_cpu_time = v;
1696        }
1697        if let Some(v) = read_num("max_wall_time") {
1698            builder.conv_check.max_wall_time = v;
1699        }
1700        if let Some(v) = read_num("acceptable_tol") {
1701            builder.conv_check.acceptable_tol = v;
1702        }
1703        if let Some(v) = read_num("acceptable_dual_inf_tol") {
1704            builder.conv_check.acceptable_dual_inf_tol = v;
1705        }
1706        if let Some(v) = read_num("acceptable_constr_viol_tol") {
1707            builder.conv_check.acceptable_constr_viol_tol = v;
1708        }
1709        if let Some(v) = read_num("acceptable_compl_inf_tol") {
1710            builder.conv_check.acceptable_compl_inf_tol = v;
1711        }
1712        if let Some(v) = read_num("acceptable_obj_change_tol") {
1713            builder.conv_check.acceptable_obj_change_tol = v;
1714        }
1715        if let Some(v) = read_int("acceptable_iter") {
1716            builder.conv_check.acceptable_iter = v;
1717        }
1718        if let Some(v) = read_num("infeas_stationarity_tol") {
1719            builder.conv_check.infeas_stationarity_tol = v;
1720        }
1721        if let Some(v) = read_num("infeas_viol_kappa") {
1722            builder.conv_check.infeas_viol_kappa = v;
1723        }
1724        if let Some(v) = read_int("infeas_max_streak") {
1725            builder.conv_check.infeas_max_streak = v;
1726        }
1727
1728        // Barrier-parameter (μ) options — consumers in
1729        // `IpMonotoneMuUpdate.cpp` / `IpAdaptiveMuUpdate.cpp`. Both
1730        // updaters share the same option names; the builder forwards
1731        // each into whichever strategy is assembled.
1732        if let Some(v) = read_num("mu_init") {
1733            builder.mu.mu_init = v;
1734        }
1735        if let Some(v) = read_num("mu_max") {
1736            builder.mu.mu_max = v;
1737        }
1738        if let Some(v) = read_num("mu_max_fact") {
1739            builder.mu.mu_max_fact = v;
1740        }
1741        if let Some(v) = read_num("mu_min") {
1742            builder.mu.mu_min = v;
1743        }
1744        if let Some(v) = read_num("mu_target") {
1745            builder.mu.mu_target = v;
1746        }
1747        if let Some(v) = read_num("mu_linear_decrease_factor") {
1748            builder.mu.mu_linear_decrease_factor = v;
1749        }
1750        if let Some(v) = read_num("mu_superlinear_decrease_power") {
1751            builder.mu.mu_superlinear_decrease_power = v;
1752        }
1753        if let Ok((v, found)) = self
1754            .options
1755            .get_string_value("mu_allow_fast_monotone_decrease", "")
1756        {
1757            if found {
1758                builder.mu.mu_allow_fast_monotone_decrease = v == "yes";
1759            }
1760        }
1761        if let Some(v) = read_num("barrier_tol_factor") {
1762            builder.mu.barrier_tol_factor = v;
1763        }
1764        if let Some(v) = read_num("sigma_max") {
1765            builder.mu.sigma_max = v;
1766        }
1767        if let Some(v) = read_num("sigma_min") {
1768            builder.mu.sigma_min = v;
1769        }
1770
1771        // Quality-function oracle knobs — consumers in
1772        // `IpQualityFunctionMuOracle.cpp:RegisterOptions`. Forwarded
1773        // to the oracle on every free-mode call.
1774        if let Ok((v, found)) = self
1775            .options
1776            .get_string_value("quality_function_norm_type", "")
1777        {
1778            if found {
1779                use crate::mu::oracle::quality_function::NormType;
1780                builder.mu.quality_function_norm_type = match v.as_str() {
1781                    "1-norm" => NormType::OneNorm,
1782                    "2-norm" => NormType::TwoNorm,
1783                    "max-norm" => NormType::MaxNorm,
1784                    _ => NormType::TwoNormSquared,
1785                };
1786            }
1787        }
1788        if let Ok((v, found)) = self
1789            .options
1790            .get_string_value("quality_function_centrality", "")
1791        {
1792            if found {
1793                use crate::mu::oracle::quality_function::CentralityType;
1794                builder.mu.quality_function_centrality = match v.as_str() {
1795                    "log" => CentralityType::LogCenter,
1796                    "reciprocal" => CentralityType::ReciprocalCenter,
1797                    "cubed-reciprocal" => CentralityType::CubedReciprocalCenter,
1798                    _ => CentralityType::None,
1799                };
1800            }
1801        }
1802        if let Ok((v, found)) = self
1803            .options
1804            .get_string_value("quality_function_balancing_term", "")
1805        {
1806            if found {
1807                use crate::mu::oracle::quality_function::BalancingTermType;
1808                builder.mu.quality_function_balancing_term = match v.as_str() {
1809                    "cubic" => BalancingTermType::CubicTerm,
1810                    _ => BalancingTermType::None,
1811                };
1812            }
1813        }
1814        if let Some(v) = read_int("quality_function_max_section_steps") {
1815            builder.mu.quality_function_max_section_steps = v;
1816        }
1817        if let Some(v) = read_num("quality_function_section_sigma_tol") {
1818            builder.mu.quality_function_section_sigma_tol = v;
1819        }
1820        if let Some(v) = read_num("quality_function_section_qf_tol") {
1821            builder.mu.quality_function_section_qf_tol = v;
1822        }
1823
1824        // `probing_iterate_quality_factor` — pounce-specific guard
1825        // (pounce#58) on the probing μ-oracle's input iterate. When
1826        // `curr_avrg_compl / curr_mu` exceeds this factor, the
1827        // μ-update layer signals restoration via
1828        // `IpoptData::request_resto` instead of letting probing
1829        // return `σ · mu_curr` ≫ previous μ. Default 1e4; set to ≤ 0
1830        // to disable. No upstream Ipopt counterpart.
1831        if let Some(v) = read_num("probing_iterate_quality_factor") {
1832            builder.mu.probing_iterate_quality_factor = v;
1833        }
1834
1835        // Adaptive-μ extras — consumers in
1836        // `IpAdaptiveMuUpdate.cpp:RegisterOptions`. Only active when
1837        // `mu_strategy=adaptive`.
1838        if let Some(v) = read_num("adaptive_mu_safeguard_factor") {
1839            builder.mu.adaptive_mu_safeguard_factor = v;
1840        }
1841        if let Some(v) = read_num("adaptive_mu_monotone_init_factor") {
1842            builder.mu.adaptive_mu_monotone_init_factor = v;
1843        }
1844        if let Ok((v, found)) = self
1845            .options
1846            .get_bool_value("adaptive_mu_restore_previous_iterate", "")
1847        {
1848            if found {
1849                builder.mu.adaptive_mu_restore_previous_iterate = v;
1850            }
1851        }
1852        if let Some(v) = read_int("adaptive_mu_kkterror_red_iters") {
1853            if v >= 0 {
1854                builder.mu.adaptive_mu_kkterror_red_iters = v as usize;
1855            }
1856        }
1857        if let Some(v) = read_num("adaptive_mu_kkterror_red_fact") {
1858            builder.mu.adaptive_mu_kkterror_red_fact = v;
1859        }
1860        if let Ok((v, found)) = self
1861            .options
1862            .get_string_value("adaptive_mu_kkt_norm_type", "")
1863        {
1864            if found {
1865                use crate::mu::adaptive::AdaptiveMuKktNorm;
1866                builder.mu.adaptive_mu_kkt_norm_type = match v.as_str() {
1867                    "1-norm" => AdaptiveMuKktNorm::OneNorm,
1868                    "2-norm" => AdaptiveMuKktNorm::TwoNorm,
1869                    "max-norm" => AdaptiveMuKktNorm::MaxNorm,
1870                    _ => AdaptiveMuKktNorm::TwoNormSquared,
1871                };
1872            }
1873        }
1874
1875        // Watchdog options — consumers in
1876        // `IpBacktrackingLineSearch.cpp:RegisterOptions`. Baked into
1877        // the `BacktrackingLineSearch` at build time.
1878        if let Some(v) = read_int("watchdog_shortened_iter_trigger") {
1879            builder.line_search.watchdog_shortened_iter_trigger = v;
1880        }
1881        if let Some(v) = read_int("watchdog_trial_iter_max") {
1882            builder.line_search.watchdog_trial_iter_max = v;
1883        }
1884        if let Some(v) = read_num("soft_resto_pderror_reduction_factor") {
1885            builder.line_search.soft_resto_pderror_reduction_factor = v;
1886        }
1887        if let Some(v) = read_int("max_soft_resto_iters") {
1888            builder.line_search.max_soft_resto_iters = v;
1889        }
1890
1891        // Iteration-output options — consumed by `OrigIterationOutput`.
1892        if let Some(v) = read_int("print_frequency_iter") {
1893            builder.output.print_frequency_iter = v;
1894        }
1895        if let Some(v) = read_num("print_frequency_time") {
1896            builder.output.print_frequency_time = v;
1897        }
1898        if let Ok((v, found)) = self.options.get_bool_value("print_info_string", "") {
1899            if found {
1900                builder.output.print_info_string = v;
1901            }
1902        }
1903        if let Ok((v, found)) = self.options.get_string_value("inf_pr_output", "") {
1904            if found {
1905                builder.output.inf_pr_output_internal = v == "internal";
1906            }
1907        }
1908
1909        // Warm-start options — consumed by `WarmStartIterateInitializer`
1910        // (port of `IpWarmStartIterateInitializer.cpp:RegisterOptions`).
1911        // `warm_start_init_point` is the toggle that picks between the
1912        // default (cold) and warm-start initializers; the remaining
1913        // knobs are baked onto the chosen initializer at build time.
1914        if let Ok((v, found)) = self.options.get_bool_value("warm_start_init_point", "") {
1915            if found {
1916                builder.warm_start_init_point = v;
1917            }
1918        }
1919        if let Ok((v, found)) = self.options.get_bool_value("warm_start_same_structure", "") {
1920            if found {
1921                builder.warm.same_structure = v;
1922            }
1923        }
1924        if let Some(v) = read_num("warm_start_bound_push") {
1925            builder.warm.bound_push = v;
1926        }
1927        if let Some(v) = read_num("warm_start_bound_frac") {
1928            builder.warm.bound_frac = v;
1929        }
1930        if let Some(v) = read_num("warm_start_slack_bound_push") {
1931            builder.warm.slack_bound_push = v;
1932        }
1933        if let Some(v) = read_num("warm_start_slack_bound_frac") {
1934            builder.warm.slack_bound_frac = v;
1935        }
1936        if let Some(v) = read_num("warm_start_mult_bound_push") {
1937            builder.warm.mult_bound_push = v;
1938        }
1939        if let Some(v) = read_num("warm_start_mult_init_max") {
1940            builder.warm.mult_init_max = v;
1941        }
1942        if let Some(v) = read_num("warm_start_target_mu") {
1943            builder.warm.target_mu = v;
1944        }
1945        if let Ok((v, found)) = self
1946            .options
1947            .get_string_value("warm_start_entire_iterate", "")
1948        {
1949            if found {
1950                builder.warm.entire_iterate = v == "yes";
1951            }
1952        }
1953
1954        // `DefaultIterateInitializer` knobs — parsed after the Mehrotra
1955        // cascade so explicit user values win
1956        // (mirrors upstream's `SetNumericValueIfUnset` semantics).
1957        if let Some(v) = read_num("bound_push") {
1958            builder.init.bound_push = v;
1959        }
1960        if let Some(v) = read_num("bound_frac") {
1961            builder.init.bound_frac = v;
1962        }
1963        if let Some(v) = read_num("slack_bound_push") {
1964            builder.init.slack_bound_push = v;
1965        }
1966        if let Some(v) = read_num("slack_bound_frac") {
1967            builder.init.slack_bound_frac = v;
1968        }
1969        if let Some(v) = read_num("constr_mult_init_max") {
1970            builder.init.constr_mult_init_max = v;
1971        }
1972        if let Some(v) = read_num("bound_mult_init_val") {
1973            builder.init.bound_mult_init_val = v;
1974        }
1975        if let Ok((v, found)) = self.options.get_string_value("bound_mult_init_method", "") {
1976            if found {
1977                builder.init.bound_mult_init_method = v;
1978            }
1979        }
1980        if let Ok((v, found)) = self
1981            .options
1982            .get_string_value("least_square_init_primal", "")
1983        {
1984            if found {
1985                builder.init.least_square_init_primal = v == "yes";
1986            }
1987        }
1988        builder
1989    }
1990}
1991
1992/// Map the integer `print_level` / `file_print_level` option to the
1993/// matching [`JournalLevel`] variant. Mirrors upstream's
1994/// `static_cast<EJournalLevel>(int_value)` with clamping.
1995/// The eight block dimensions of an iterate, in canonical order
1996/// (x, s, y_c, y_d, z_l, z_u, v_l, v_u). Used to guard the debugger's
1997/// warm-restart install against a structural mismatch between solves.
1998fn iterates_dims(c: &IteratesVector) -> [i32; 8] {
1999    [
2000        c.x.dim(),
2001        c.s.dim(),
2002        c.y_c.dim(),
2003        c.y_d.dim(),
2004        c.z_l.dim(),
2005        c.z_u.dim(),
2006        c.v_l.dim(),
2007        c.v_u.dim(),
2008    ]
2009}
2010
2011fn journal_level_from_int(v: i32) -> JournalLevel {
2012    match v.clamp(0, 12) {
2013        0 => JournalLevel::J_NONE,
2014        1 => JournalLevel::J_ERROR,
2015        2 => JournalLevel::J_STRONGWARNING,
2016        3 => JournalLevel::J_SUMMARY,
2017        4 => JournalLevel::J_WARNING,
2018        5 => JournalLevel::J_ITERSUMMARY,
2019        6 => JournalLevel::J_DETAILED,
2020        7 => JournalLevel::J_MOREDETAILED,
2021        8 => JournalLevel::J_VECTOR,
2022        9 => JournalLevel::J_MOREVECTOR,
2023        10 => JournalLevel::J_MATRIX,
2024        11 => JournalLevel::J_MOREMATRIX,
2025        _ => JournalLevel::J_ALL,
2026    }
2027}
2028
2029/// Default symmetric linear-solver factory, parameterized by the
2030/// pounce-extension FERAL knobs read off the application's
2031/// `OptionsList`.
2032///
2033/// FERAL (pure-Rust) is the shipping default. The HSL MA57 backend is
2034/// available when the `ma57` cargo feature is enabled; without it,
2035/// requesting `linear_solver = ma57` falls back to FERAL with a
2036/// warning printed by the journalist (see [`AlgorithmBuilder`]).
2037pub fn default_backend_factory(feral_cfg: pounce_feral::FeralConfig) -> LinearBackendFactory {
2038    Box::new(
2039        move |choice: LinearSolverChoice| -> Box<dyn SparseSymLinearSolverInterface> {
2040            match choice {
2041                LinearSolverChoice::Feral => Box::new(
2042                    pounce_feral::FeralSolverInterface::with_config(feral_cfg.clone()),
2043                ),
2044                LinearSolverChoice::Ma57 => {
2045                    #[cfg(feature = "ma57")]
2046                    {
2047                        Box::new(pounce_hsl::Ma57SolverInterface::new())
2048                    }
2049                    #[cfg(not(feature = "ma57"))]
2050                    {
2051                        // ma57 feature not compiled in — fall back to FERAL.
2052                        Box::new(pounce_feral::FeralSolverInterface::with_config(
2053                            feral_cfg.clone(),
2054                        ))
2055                    }
2056                }
2057            }
2058        },
2059    )
2060}
2061
2062/// Sink-aware variant of [`default_backend_factory`]. Identical
2063/// dispatch, but the FERAL backend is constructed with a
2064/// `LinearSolverSummary` sink so [`IpoptApplication`] can read out
2065/// aggregate post-mortem stats (factor counts, fill ratio, extremal
2066/// pivots, final inertia) after the solve returns. MA57 ignores the
2067/// sink — the HSL backend doesn't carry the same instrumentation yet.
2068pub fn default_backend_factory_with_sink(
2069    feral_cfg: pounce_feral::FeralConfig,
2070    sink: Arc<Mutex<LinearSolverSummary>>,
2071) -> LinearBackendFactory {
2072    Box::new(
2073        move |choice: LinearSolverChoice| -> Box<dyn SparseSymLinearSolverInterface> {
2074            match choice {
2075                LinearSolverChoice::Feral => Box::new(
2076                    pounce_feral::FeralSolverInterface::with_config(feral_cfg.clone())
2077                        .with_summary_sink(Arc::clone(&sink)),
2078                ),
2079                LinearSolverChoice::Ma57 => {
2080                    #[cfg(feature = "ma57")]
2081                    {
2082                        Box::new(pounce_hsl::Ma57SolverInterface::new())
2083                    }
2084                    #[cfg(not(feature = "ma57"))]
2085                    {
2086                        Box::new(
2087                            pounce_feral::FeralSolverInterface::with_config(feral_cfg.clone())
2088                                .with_summary_sink(Arc::clone(&sink)),
2089                        )
2090                    }
2091                }
2092            }
2093        },
2094    )
2095}
2096
2097/// Read the `feral_*` extension options off `options`, falling
2098/// back to the env-var defaults baked into [`pounce_feral::FeralConfig::from_env`]
2099/// for any knob the caller did not set explicitly. The returned
2100/// config is what every default-factory invocation (main IPM and
2101/// restoration sub-IPM) consumes.
2102pub fn feral_config_from_options(
2103    options: &pounce_common::options_list::OptionsList,
2104) -> pounce_feral::FeralConfig {
2105    let mut cfg = pounce_feral::FeralConfig::from_env();
2106    // Tri-state: the `(_, true)` arm only fires when the user set the
2107    // option explicitly. Leaving it unset keeps `cfg.cascade_break` at
2108    // `None`, which inherits FERAL's `NumericParams::default()` (CB on
2109    // as of FERAL Phase B / pounce#55). `Some(false)` explicitly
2110    // disarms (reproduces pre-Phase-B behaviour, surfaces FERAL's
2111    // `DelayBudgetExceeded` on non-root cascade victims).
2112    if let Ok((v, true)) = options.get_bool_value("feral_cascade_break", "") {
2113        cfg.cascade_break = Some(v);
2114    }
2115    if let Ok((v, true)) = options.get_bool_value("feral_fma", "") {
2116        cfg.fma = v;
2117    }
2118    if let Ok((v, true)) = options.get_bool_value("feral_refine", "") {
2119        cfg.refine = v;
2120    }
2121    if let Ok((v, true)) = options.get_numeric_value("feral_singular_pivot_floor", "") {
2122        cfg.singular_pivot_floor = v;
2123    }
2124    if let Ok((v, true)) = options.get_numeric_value("feral_pivtol", "") {
2125        cfg.pivtol = v;
2126    }
2127    // Only override on explicit set so `from_env` (which itself
2128    // defaults to OrderingMethod::Auto) keeps governing unset cases.
2129    // Unrecognized tags are silently ignored — the registered enum
2130    // restricts inputs at the OptionsList layer.
2131    if let Ok((v, true)) = options.get_string_value("feral_ordering", "") {
2132        if let Some(m) = pounce_feral::parse_ordering_method(&v) {
2133            cfg.ordering = m;
2134        }
2135    }
2136    // Same explicit-set discipline as `feral_ordering`: `from_env`
2137    // defaults to ScalingStrategy::Auto (FERAL's current default), so
2138    // leaving the option unset preserves existing behaviour exactly.
2139    if let Ok((v, true)) = options.get_string_value("feral_scaling", "") {
2140        if let Some(s) = pounce_feral::parse_scaling_strategy(&v) {
2141            cfg.scaling = s;
2142        }
2143    }
2144    cfg
2145}
2146
2147/// Map upstream `SolverReturn` codes to `ApplicationReturnStatus`.
2148/// Mirrors the table in
2149/// `ref/Ipopt/AGENT_REFERENCE/MAIN_LOOP.md` ("exception → SolverReturn
2150/// map") and the corresponding switch in
2151/// `IpIpoptApplication.cpp:call_optimize`.
2152fn solver_return_to_app_status(s: SolverReturn) -> ApplicationReturnStatus {
2153    match s {
2154        SolverReturn::Success => ApplicationReturnStatus::SolveSucceeded,
2155        SolverReturn::StopAtAcceptablePoint => ApplicationReturnStatus::SolvedToAcceptableLevel,
2156        SolverReturn::FeasiblePointFound => ApplicationReturnStatus::FeasiblePointFound,
2157        SolverReturn::MaxiterExceeded => ApplicationReturnStatus::MaximumIterationsExceeded,
2158        SolverReturn::CpuTimeExceeded => ApplicationReturnStatus::MaximumCpuTimeExceeded,
2159        SolverReturn::WallTimeExceeded => ApplicationReturnStatus::MaximumWallTimeExceeded,
2160        SolverReturn::StopAtTinyStep => ApplicationReturnStatus::SearchDirectionBecomesTooSmall,
2161        SolverReturn::LocalInfeasibility => ApplicationReturnStatus::InfeasibleProblemDetected,
2162        SolverReturn::UserRequestedStop => ApplicationReturnStatus::UserRequestedStop,
2163        SolverReturn::DivergingIterates => ApplicationReturnStatus::DivergingIterates,
2164        SolverReturn::RestorationFailure => ApplicationReturnStatus::RestorationFailed,
2165        SolverReturn::ErrorInStepComputation => ApplicationReturnStatus::ErrorInStepComputation,
2166        SolverReturn::InvalidNumberDetected => ApplicationReturnStatus::InvalidNumberDetected,
2167        SolverReturn::TooFewDegreesOfFreedom => ApplicationReturnStatus::NotEnoughDegreesOfFreedom,
2168        SolverReturn::InvalidOption => ApplicationReturnStatus::InvalidOption,
2169        SolverReturn::OutOfMemory => ApplicationReturnStatus::InsufficientMemory,
2170        SolverReturn::InternalError | SolverReturn::Unassigned => {
2171            ApplicationReturnStatus::InternalError
2172        }
2173    }
2174}
2175
2176/// Best-effort evaluation of the objective at the algorithm's final
2177/// `x`. Returns the *scaled* objective (`f * obj_scale_factor`); used
2178/// to populate `SolveStatistics::final_scaled_objective`.
2179fn try_eval_curr_f(
2180    nlp: &Rc<RefCell<dyn IpoptNlp>>,
2181    x: &Rc<dyn pounce_linalg::Vector>,
2182) -> Result<Number, ()> {
2183    let mut nlp_mut = nlp.borrow_mut();
2184    Ok(nlp_mut.eval_f(&**x))
2185}
2186
2187/// Trigger predicate for the Phase-3.5 ℓ₁ auto-fallback path. Returns
2188/// `true` when a status warrants a retry through the wrapper. Mirrors
2189/// ripopt#23's trigger set, extended per the audit's Refinement B
2190/// (pounce-side `Not_Enough_Degrees_Of_Freedom` is added because
2191/// pounce's DOF early-exit blocks NE-suffix problems that ripopt's
2192/// equivalent would let pass to the wrapper).
2193fn is_l1_fallback_trigger(status: ApplicationReturnStatus) -> bool {
2194    matches!(
2195        status,
2196        ApplicationReturnStatus::RestorationFailed
2197            | ApplicationReturnStatus::InfeasibleProblemDetected
2198            | ApplicationReturnStatus::SolvedToAcceptableLevel
2199            | ApplicationReturnStatus::MaximumIterationsExceeded
2200            | ApplicationReturnStatus::NotEnoughDegreesOfFreedom
2201    )
2202}
2203
2204/// Forward the final iterate back to the user's `TNLP::finalize_solution`.
2205/// We pull `x` (compressed in `x_var`-space) off the algorithm's
2206/// `data.curr`, lift it back to full TNLP indexing, and pass empty
2207/// multipliers for now (the algorithm's `y_c`, `y_d`, `z_l`, `z_u` are
2208/// in compressed split form — re-assembling them into the user's
2209/// `lambda` / `z_l` / `z_u` is mechanical but lives behind a
2210/// `OrigIpoptNlp::finalize_solution_*` accessor that's still being
2211/// fleshed out). On success returns the unscaled objective evaluated
2212/// on the user TNLP at the final iterate; returns `Err` if the final
2213/// iterate is missing.
2214fn finalize_via_orig_nlp(
2215    nlp: &Rc<RefCell<dyn IpoptNlp>>,
2216    alg: &IpoptAlgorithm,
2217    solver_status: SolverReturn,
2218    _app_status: ApplicationReturnStatus,
2219    tnlp: &Rc<RefCell<dyn TNLP>>,
2220) -> Result<Number, ()> {
2221    let curr = alg.data.borrow().curr.clone().ok_or(())?;
2222    // Lift compressed x_var → full-x (length `info.n`) so the user
2223    // TNLP receives the same shape it provided. With `make_parameter`
2224    // the fixed components are spliced back in by the IpoptNlp.
2225    let nlp_borrow = nlp.borrow();
2226    let x_vec: Vec<Number> = nlp_borrow.lift_x_to_full(&*curr.x);
2227    let info = tnlp.borrow_mut().get_nlp_info().ok_or(())?;
2228    let n = info.n as usize;
2229    let m = info.m as usize;
2230    debug_assert_eq!(x_vec.len(), n);
2231    // Lift algorithm-side multipliers back into user-space (pounce#11).
2232    // Use the `finalize_solution_*` family (not the `pack_*` family): the
2233    // final solution duals must be reported in the user's *unscaled-
2234    // Lagrangian* convention `∇f + λ·∇g + z = 0`, which divides out the
2235    // `obj_scale_factor` the algorithm threads through `eval_h`. The `pack_*`
2236    // family deliberately omits that division because it feeds the scaled
2237    // `eval_h`; calling it here left every dual scaled by `obj_scale_factor`
2238    // whenever gradient-based scaling triggered (pounce#11 F1).
2239    // Backends without overrides return empty; fall back to zero stubs so the
2240    // user sees a length-consistent vector.
2241    let mut z_l = nlp_borrow.finalize_solution_z_l(&*curr.z_l);
2242    if z_l.is_empty() {
2243        z_l = vec![0.0; n];
2244    }
2245    let mut z_u = nlp_borrow.finalize_solution_z_u(&*curr.z_u);
2246    if z_u.is_empty() {
2247        z_u = vec![0.0; n];
2248    }
2249    let mut lambda = nlp_borrow.finalize_solution_lambda(&*curr.y_c, &*curr.y_d);
2250    if lambda.is_empty() {
2251        lambda = vec![0.0; m];
2252    }
2253    drop(nlp_borrow);
2254    // Compute g(x) via the user TNLP so the final residual is
2255    // populated for the user.
2256    let mut g_final = vec![0.0; m];
2257    let _ = tnlp.borrow_mut().eval_g(&x_vec, true, &mut g_final);
2258    let f_final = tnlp
2259        .borrow_mut()
2260        .eval_f(&x_vec, true)
2261        .unwrap_or(Number::NAN);
2262    tnlp.borrow_mut().finalize_solution(
2263        Solution {
2264            status: solver_status,
2265            x: &x_vec,
2266            z_l: &z_l,
2267            z_u: &z_u,
2268            g: &g_final,
2269            lambda: &lambda,
2270            obj_value: f_final,
2271        },
2272        &TnlpIpoptData::default(),
2273        &TnlpIpoptCq::default(),
2274    );
2275    Ok(f_final)
2276}
2277
2278/// Bind SQP suboptions registered in `upstream_options.rs`
2279/// (`sqp_globalization`, `sqp_hessian`, `sqp_max_iter`, `sqp_tol`,
2280/// `sqp_constr_viol_tol`, `sqp_dual_inf_tol`, `sqp_l1_penalty`,
2281/// `sqp_bt_reduction`, `sqp_bt_min_alpha`, `sqp_print_level`,
2282/// `sqp_lbfgs_max_history`) onto
2283/// `opts`. Used by [`IpoptApplication::algorithm_builder_snapshot`]
2284/// before constructing an SQP algorithm.
2285fn apply_sqp_options(options: &OptionsList, opts: &mut crate::sqp::SqpOptions) {
2286    use crate::sqp::{SqpGlobalization, SqpHessianSource};
2287
2288    if let Ok((s, true)) = options.get_string_value("sqp_globalization", "") {
2289        opts.globalization = match s.as_str() {
2290            "filter" => SqpGlobalization::Filter,
2291            "l1-elastic" => SqpGlobalization::L1Elastic,
2292            _ => opts.globalization,
2293        };
2294    }
2295    if let Ok((s, true)) = options.get_string_value("sqp_hessian", "") {
2296        opts.hessian = match s.as_str() {
2297            "exact" => SqpHessianSource::Exact,
2298            "damped-bfgs" => SqpHessianSource::DampedBfgs,
2299            "lbfgs" => SqpHessianSource::Lbfgs,
2300            _ => opts.hessian,
2301        };
2302    }
2303    if let Ok((v, true)) = options.get_integer_value("sqp_max_iter", "") {
2304        if v >= 0 {
2305            opts.max_iter = v as u32;
2306        }
2307    }
2308    if let Ok((v, true)) = options.get_numeric_value("sqp_tol", "") {
2309        opts.tol = v;
2310    }
2311    if let Ok((v, true)) = options.get_numeric_value("sqp_constr_viol_tol", "") {
2312        opts.constr_viol_tol = v;
2313    }
2314    if let Ok((v, true)) = options.get_numeric_value("sqp_dual_inf_tol", "") {
2315        opts.dual_inf_tol = v;
2316    }
2317    if let Ok((v, true)) = options.get_numeric_value("sqp_l1_penalty", "") {
2318        opts.l1_penalty = v;
2319    }
2320    if let Ok((v, true)) = options.get_numeric_value("sqp_l1_penalty_safety", "") {
2321        opts.l1_penalty_safety = v;
2322    }
2323    if let Ok((v, true)) = options.get_numeric_value("sqp_l1_penalty_max", "") {
2324        opts.l1_penalty_max = v;
2325    }
2326    if let Ok((v, true)) = options.get_numeric_value("sqp_bt_reduction", "") {
2327        opts.bt_reduction = v;
2328    }
2329    if let Ok((v, true)) = options.get_numeric_value("sqp_bt_min_alpha", "") {
2330        opts.bt_min_alpha = v;
2331    }
2332    if let Ok((v, true)) = options.get_integer_value("sqp_print_level", "") {
2333        opts.print_level = v.clamp(0, u8::MAX as i32) as u8;
2334    }
2335    if let Ok((v, true)) = options.get_integer_value("sqp_lbfgs_max_history", "") {
2336        if v >= 1 {
2337            opts.lbfgs_max_history = v as u32;
2338        }
2339    }
2340}
2341
2342/// Populate the active-set SQP **QP-subproblem** options
2343/// ([`pounce_qp::QpOptions`]) from the `sqp_qp_*` option family.
2344///
2345/// Sister to [`apply_sqp_options`], which handles the SQP *outer-loop*
2346/// options ([`crate::sqp::SqpOptions`]); this one feeds the inner QP
2347/// solver that `SqpAlgorithm` delegates each subproblem to. Consulted
2348/// only on the `ActiveSetSqp` path. Each knob is forwarded only when
2349/// the user explicitly set it (the `true` flag), so the `pounce_qp`
2350/// defaults stand otherwise.
2351fn apply_qp_subproblem_options(options: &OptionsList, opts: &mut pounce_qp::QpOptions) {
2352    use pounce_qp::AntiCyclingChoice;
2353
2354    if let Ok((v, true)) = options.get_integer_value("sqp_qp_max_iter", "") {
2355        if v >= 0 {
2356            opts.max_iter = v as u32;
2357        }
2358    }
2359    if let Ok((v, true)) = options.get_numeric_value("sqp_qp_feas_tol", "") {
2360        opts.feas_tol = v;
2361    }
2362    if let Ok((v, true)) = options.get_numeric_value("sqp_qp_opt_tol", "") {
2363        opts.opt_tol = v;
2364    }
2365    if let Ok((v, true)) = options.get_numeric_value("sqp_qp_elastic_gamma", "") {
2366        opts.elastic_gamma = v;
2367    }
2368    if let Ok((s, true)) = options.get_string_value("sqp_qp_anti_cycling", "") {
2369        opts.anti_cycling = match s.as_str() {
2370            "expand" => AntiCyclingChoice::Expand,
2371            "bland" => AntiCyclingChoice::Bland,
2372            "none" => AntiCyclingChoice::None,
2373            _ => opts.anti_cycling,
2374        };
2375    }
2376}
2377
2378/// SQP-side analog of [`finalize_via_orig_nlp`]. Hands the SQP
2379/// solution iterate to the user TNLP via the standard
2380/// `finalize_solution` callback. Multiplier lifting goes through
2381/// the same OrigIpoptNlp hooks so the user sees the same shape
2382/// regardless of which algorithm produced the iterate.
2383///
2384/// Returns the user-space objective value on success.
2385fn finalize_via_sqp(
2386    nlp: &Rc<RefCell<dyn IpoptNlp>>,
2387    res: &crate::sqp::SqpResult,
2388    solver_status: pounce_nlp::SolverReturn,
2389    tnlp: &Rc<RefCell<dyn TNLP>>,
2390) -> Result<Number, ()> {
2391    use pounce_linalg::dense_vector::DenseVectorSpace;
2392
2393    let info = tnlp.borrow_mut().get_nlp_info().ok_or(())?;
2394    let n = info.n as usize;
2395    let m = info.m as usize;
2396
2397    // Wrap SQP slices in DenseVectors so we can pass them through
2398    // the OrigIpoptNlp lift_x_to_full / pack_*_for_user hooks.
2399    let nlp_borrow = nlp.borrow();
2400    let n_alg = nlp_borrow.n() as usize;
2401    let m_eq = nlp_borrow.m_eq() as usize;
2402    let m_ineq = nlp_borrow.m_ineq() as usize;
2403    debug_assert_eq!(res.x.len(), n_alg);
2404    debug_assert_eq!(res.lambda_g.len(), m_eq + m_ineq);
2405    debug_assert_eq!(res.lambda_x.len(), n_alg);
2406
2407    let x_space = DenseVectorSpace::new(n_alg as Index);
2408    let c_space = DenseVectorSpace::new(m_eq as Index);
2409    let d_space = DenseVectorSpace::new(m_ineq as Index);
2410
2411    let mut x_dv = x_space.make_new_dense();
2412    x_dv.set_values(&res.x);
2413    let x_vec: Vec<Number> = nlp_borrow.lift_x_to_full(&x_dv);
2414    debug_assert_eq!(x_vec.len(), n);
2415
2416    // λ_x is packed signed (z_l − z_u). Split for lift.
2417    let mut z_l_compressed = x_space.make_new_dense();
2418    let mut z_u_compressed = x_space.make_new_dense();
2419    let zl_vals: Vec<Number> = res.lambda_x.iter().map(|v| v.max(0.0)).collect();
2420    let zu_vals: Vec<Number> = res.lambda_x.iter().map(|v| (-v).max(0.0)).collect();
2421    z_l_compressed.set_values(&zl_vals);
2422    z_u_compressed.set_values(&zu_vals);
2423    // `finalize_solution_*` (not `pack_*`): report unscaled-Lagrangian duals,
2424    // dividing out `obj_scale_factor` — see `finalize_via_orig_nlp` (F1).
2425    let mut z_l = nlp_borrow.finalize_solution_z_l(&z_l_compressed);
2426    if z_l.is_empty() {
2427        z_l = vec![0.0; n];
2428    }
2429    let mut z_u = nlp_borrow.finalize_solution_z_u(&z_u_compressed);
2430    if z_u.is_empty() {
2431        z_u = vec![0.0; n];
2432    }
2433
2434    // λ_g is [y_c; y_d]; split into the c/d blocks for lift.
2435    let mut y_c_dv = c_space.make_new_dense();
2436    let mut y_d_dv = d_space.make_new_dense();
2437    if m_eq > 0 {
2438        y_c_dv.set_values(&res.lambda_g[..m_eq]);
2439    }
2440    if m_ineq > 0 {
2441        y_d_dv.set_values(&res.lambda_g[m_eq..]);
2442    }
2443    let mut lambda = nlp_borrow.finalize_solution_lambda(&y_c_dv, &y_d_dv);
2444    if lambda.is_empty() {
2445        lambda = vec![0.0; m];
2446    }
2447    drop(nlp_borrow);
2448
2449    let mut g_final = vec![0.0; m];
2450    let _ = tnlp.borrow_mut().eval_g(&x_vec, true, &mut g_final);
2451    let f_final = tnlp
2452        .borrow_mut()
2453        .eval_f(&x_vec, true)
2454        .unwrap_or(Number::NAN);
2455    tnlp.borrow_mut().finalize_solution(
2456        pounce_nlp::tnlp::Solution {
2457            status: solver_status,
2458            x: &x_vec,
2459            z_l: &z_l,
2460            z_u: &z_u,
2461            g: &g_final,
2462            lambda: &lambda,
2463            obj_value: f_final,
2464        },
2465        &TnlpIpoptData::default(),
2466        &TnlpIpoptCq::default(),
2467    );
2468    Ok(f_final)
2469}
2470
2471#[cfg(test)]
2472mod tests {
2473    use super::*;
2474    use pounce_nlp::tnlp::{
2475        BoundsInfo, IndexStyle, IpoptCq, IpoptData, NlpInfo, Solution, SparsityRequest,
2476        StartingPoint,
2477    };
2478
2479    struct Hs071Stub;
2480    impl TNLP for Hs071Stub {
2481        fn get_nlp_info(&mut self) -> Option<NlpInfo> {
2482            // HS071 dimensions: n=4, m=2, dense Jacobian (8 nz),
2483            // dense lower-triangular Hessian (10 nz).
2484            Some(NlpInfo {
2485                n: 4,
2486                m: 2,
2487                nnz_jac_g: 8,
2488                nnz_h_lag: 10,
2489                index_style: IndexStyle::C,
2490            })
2491        }
2492        fn get_bounds_info(&mut self, b: BoundsInfo<'_>) -> bool {
2493            b.x_l.copy_from_slice(&[1.0; 4]);
2494            b.x_u.copy_from_slice(&[5.0; 4]);
2495            b.g_l.copy_from_slice(&[25.0, 40.0]);
2496            b.g_u.copy_from_slice(&[2.0e19, 40.0]);
2497            true
2498        }
2499        fn get_starting_point(&mut self, sp: StartingPoint<'_>) -> bool {
2500            sp.x.copy_from_slice(&[1.0, 5.0, 5.0, 1.0]);
2501            true
2502        }
2503        fn eval_f(&mut self, x: &[Number], _new_x: bool) -> Option<Number> {
2504            Some(x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2])
2505        }
2506        fn eval_grad_f(&mut self, _x: &[Number], _new_x: bool, grad: &mut [Number]) -> bool {
2507            grad.fill(0.0);
2508            true
2509        }
2510        fn eval_g(&mut self, _x: &[Number], _new_x: bool, g: &mut [Number]) -> bool {
2511            g.fill(0.0);
2512            true
2513        }
2514        fn eval_jac_g(
2515            &mut self,
2516            _x: Option<&[Number]>,
2517            _new_x: bool,
2518            mode: SparsityRequest<'_>,
2519        ) -> bool {
2520            if let SparsityRequest::Structure { irow, jcol } = mode {
2521                irow.copy_from_slice(&[0, 0, 0, 0, 1, 1, 1, 1]);
2522                jcol.copy_from_slice(&[0, 1, 2, 3, 0, 1, 2, 3]);
2523            }
2524            true
2525        }
2526        fn finalize_solution(&mut self, _sol: Solution<'_>, _d: &IpoptData, _q: &IpoptCq) {}
2527    }
2528
2529    #[test]
2530    fn application_default_does_not_select_sqp() {
2531        let mut app = IpoptApplication::new();
2532        app.initialize().unwrap();
2533        assert!(!app.is_sqp_algorithm_selected());
2534    }
2535
2536    #[test]
2537    fn application_routes_to_sqp_when_algorithm_option_set() {
2538        let mut app = IpoptApplication::new();
2539        app.initialize().unwrap();
2540        app.initialize_with_options_str("algorithm active-set-sqp\n")
2541            .unwrap();
2542        assert!(app.is_sqp_algorithm_selected());
2543    }
2544
2545    /// Convex equality NLP fixture for end-to-end SQP testing
2546    /// through `IpoptApplication`:
2547    ///
2548    ///     min ½(x₁² + x₂²) − x₁ − 2x₂  s.t.  x₁ + x₂ = 1
2549    ///
2550    /// Closed form: x* = (0, 1), obj = -1.5, λ_g = 1.
2551    struct ConvexEqTnlp {
2552        finalize_called: std::rc::Rc<std::cell::RefCell<Option<(Vec<Number>, Number)>>>,
2553    }
2554    impl TNLP for ConvexEqTnlp {
2555        fn get_nlp_info(&mut self) -> Option<NlpInfo> {
2556            Some(NlpInfo {
2557                n: 2,
2558                m: 1,
2559                nnz_jac_g: 2,
2560                nnz_h_lag: 2,
2561                index_style: IndexStyle::C,
2562            })
2563        }
2564        fn get_bounds_info(&mut self, b: BoundsInfo<'_>) -> bool {
2565            b.x_l.copy_from_slice(&[-2.0e19; 2]);
2566            b.x_u.copy_from_slice(&[2.0e19; 2]);
2567            b.g_l.copy_from_slice(&[1.0]);
2568            b.g_u.copy_from_slice(&[1.0]);
2569            true
2570        }
2571        fn get_starting_point(&mut self, sp: StartingPoint<'_>) -> bool {
2572            sp.x.copy_from_slice(&[0.0, 0.0]);
2573            true
2574        }
2575        fn eval_f(&mut self, x: &[Number], _new_x: bool) -> Option<Number> {
2576            Some(0.5 * (x[0] * x[0] + x[1] * x[1]) - x[0] - 2.0 * x[1])
2577        }
2578        fn eval_grad_f(&mut self, x: &[Number], _new_x: bool, grad: &mut [Number]) -> bool {
2579            grad[0] = x[0] - 1.0;
2580            grad[1] = x[1] - 2.0;
2581            true
2582        }
2583        fn eval_g(&mut self, x: &[Number], _new_x: bool, g: &mut [Number]) -> bool {
2584            g[0] = x[0] + x[1];
2585            true
2586        }
2587        fn eval_jac_g(
2588            &mut self,
2589            _x: Option<&[Number]>,
2590            _new_x: bool,
2591            mode: SparsityRequest<'_>,
2592        ) -> bool {
2593            match mode {
2594                SparsityRequest::Structure { irow, jcol } => {
2595                    irow.copy_from_slice(&[0, 0]);
2596                    jcol.copy_from_slice(&[0, 1]);
2597                }
2598                SparsityRequest::Values { values, .. } => {
2599                    values.copy_from_slice(&[1.0, 1.0]);
2600                }
2601            }
2602            true
2603        }
2604        fn eval_h(
2605            &mut self,
2606            _x: Option<&[Number]>,
2607            _new_x: bool,
2608            _obj_factor: Number,
2609            _lambda: Option<&[Number]>,
2610            _new_lambda: bool,
2611            mode: SparsityRequest<'_>,
2612        ) -> bool {
2613            match mode {
2614                SparsityRequest::Structure { irow, jcol } => {
2615                    irow.copy_from_slice(&[0, 1]);
2616                    jcol.copy_from_slice(&[0, 1]);
2617                }
2618                SparsityRequest::Values { values, .. } => {
2619                    values.copy_from_slice(&[1.0, 1.0]);
2620                }
2621            }
2622            true
2623        }
2624        fn finalize_solution(&mut self, sol: Solution<'_>, _d: &IpoptData, _q: &IpoptCq) {
2625            *self.finalize_called.borrow_mut() = Some((sol.x.to_vec(), sol.obj_value));
2626        }
2627    }
2628
2629    #[test]
2630    fn application_sqp_path_solves_convex_eq_nlp_and_finalizes() {
2631        let finalize_slot = std::rc::Rc::new(std::cell::RefCell::new(None));
2632        let tnlp = std::rc::Rc::new(std::cell::RefCell::new(ConvexEqTnlp {
2633            finalize_called: std::rc::Rc::clone(&finalize_slot),
2634        }));
2635
2636        let mut app = IpoptApplication::new();
2637        app.initialize().unwrap();
2638        app.initialize_with_options_str("algorithm active-set-sqp\n")
2639            .unwrap();
2640        let status = app.optimize_tnlp(tnlp);
2641        assert_eq!(status, ApplicationReturnStatus::SolveSucceeded);
2642
2643        // The TNLP's finalize_solution must have been invoked.
2644        let recv = finalize_slot.borrow().clone();
2645        let (x_recv, obj_recv) = recv.expect("finalize_solution was not called");
2646        assert_eq!(x_recv.len(), 2);
2647        assert!((x_recv[0] - 0.0).abs() < 1e-6, "x[0] = {}", x_recv[0]);
2648        assert!((x_recv[1] - 1.0).abs() < 1e-6, "x[1] = {}", x_recv[1]);
2649        assert!(
2650            (obj_recv - (-1.5)).abs() < 1e-6,
2651            "obj = {} but expected -1.5",
2652            obj_recv
2653        );
2654    }
2655
2656    #[test]
2657    fn application_routes_to_sqp_case_insensitively() {
2658        let mut app = IpoptApplication::new();
2659        app.initialize().unwrap();
2660        app.initialize_with_options_str("algorithm Active-Set-SQP\n")
2661            .unwrap();
2662        // get_string_value may return the value as-stored (no
2663        // normalization); the dispatch must handle case
2664        // insensitively per the c11 design choice.
2665        assert!(app.is_sqp_algorithm_selected());
2666    }
2667
2668    #[test]
2669    fn application_constructs_and_loads_options() {
2670        let mut app = IpoptApplication::new();
2671        app.initialize().unwrap();
2672        // ipopt.opt-style file: an integer-typed option registered by
2673        // the Interfaces layer.
2674        app.initialize_with_options_str("print_level 5\nfile_print_level 7\n")
2675            .unwrap();
2676        let (level, found) = app.options().get_integer_value("print_level", "").unwrap();
2677        assert!(found);
2678        assert_eq!(level, 5);
2679    }
2680
2681    #[test]
2682    fn application_sqp_suboptions_propagate_to_builder() {
2683        // All SQP suboptions are read by algorithm_builder_snapshot
2684        // and baked into the builder's `sqp` field.
2685        let mut app = IpoptApplication::new();
2686        app.initialize().unwrap();
2687        app.initialize_with_options_str(
2688            "algorithm active-set-sqp\n\
2689             sqp_globalization l1-elastic\n\
2690             sqp_hessian lbfgs\n\
2691             sqp_max_iter 17\n\
2692             sqp_tol 1e-7\n\
2693             sqp_constr_viol_tol 1e-5\n\
2694             sqp_dual_inf_tol 1e-3\n\
2695             sqp_l1_penalty 2.5\n\
2696             sqp_bt_reduction 0.25\n\
2697             sqp_bt_min_alpha 1e-10\n\
2698             sqp_print_level 2\n\
2699             sqp_lbfgs_max_history 12\n",
2700        )
2701        .unwrap();
2702        let snap = app.algorithm_builder_snapshot();
2703        assert_eq!(
2704            snap.sqp.globalization,
2705            crate::sqp::SqpGlobalization::L1Elastic
2706        );
2707        assert_eq!(snap.sqp.hessian, crate::sqp::SqpHessianSource::Lbfgs);
2708        assert_eq!(snap.sqp.max_iter, 17);
2709        assert!((snap.sqp.tol - 1e-7).abs() < 1e-18);
2710        assert!((snap.sqp.constr_viol_tol - 1e-5).abs() < 1e-18);
2711        assert!((snap.sqp.dual_inf_tol - 1e-3).abs() < 1e-18);
2712        assert!((snap.sqp.l1_penalty - 2.5).abs() < 1e-18);
2713        assert!((snap.sqp.bt_reduction - 0.25).abs() < 1e-18);
2714        assert!((snap.sqp.bt_min_alpha - 1e-10).abs() < 1e-18);
2715        assert_eq!(snap.sqp.print_level, 2);
2716        assert_eq!(snap.sqp.lbfgs_max_history, 12);
2717    }
2718
2719    #[test]
2720    fn application_limited_memory_options_propagate_to_builder() {
2721        use crate::hess::lim_mem_quasi_newton::UpdateType;
2722
2723        // Default: no options set -> bit-exact with Ipopt's default
2724        // (bfgs, history 6). This is what the IPM path runs unless the
2725        // user opts in, so it must not drift.
2726        let mut app = IpoptApplication::new();
2727        app.initialize().unwrap();
2728        let def = app.algorithm_builder_from_options();
2729        assert_eq!(def.limited_memory_update_type, UpdateType::Bfgs);
2730        assert_eq!(def.limited_memory_max_history, 6);
2731
2732        // `limited_memory_update_type=sr1` and a custom history length
2733        // must reach the builder (these were registered upstream but
2734        // read nowhere on the IPM path before — see #131). Honoring
2735        // them is what lets SR1 break the monotone L-BFGS stall.
2736        let mut app = IpoptApplication::new();
2737        app.initialize().unwrap();
2738        app.initialize_with_options_str(
2739            "hessian_approximation limited-memory\n\
2740             limited_memory_update_type sr1\n\
2741             limited_memory_max_history 9\n",
2742        )
2743        .unwrap();
2744        let snap = app.algorithm_builder_from_options();
2745        assert_eq!(snap.limited_memory_update_type, UpdateType::Sr1);
2746        assert_eq!(snap.limited_memory_max_history, 9);
2747    }
2748
2749    #[test]
2750    fn application_sqp_warm_start_round_trip() {
2751        // Drive the convex-equality TNLP through the SQP path
2752        // twice. The first solve produces a working set; the
2753        // second is warm-started from it. The second must converge
2754        // with zero QP solves (the first KKT check declares
2755        // optimality immediately).
2756        let finalize_slot = std::rc::Rc::new(std::cell::RefCell::new(None));
2757        let tnlp_rc: std::rc::Rc<std::cell::RefCell<dyn TNLP>> =
2758            std::rc::Rc::new(std::cell::RefCell::new(ConvexEqTnlp {
2759                finalize_called: std::rc::Rc::clone(&finalize_slot),
2760            }));
2761
2762        let mut app = IpoptApplication::new();
2763        app.initialize().unwrap();
2764        app.initialize_with_options_str("algorithm active-set-sqp\n")
2765            .unwrap();
2766
2767        // Cold solve.
2768        let status_a = app.optimize_tnlp(std::rc::Rc::clone(&tnlp_rc));
2769        assert_eq!(status_a, ApplicationReturnStatus::SolveSucceeded);
2770        let ws = app.last_sqp_working_set().cloned();
2771        assert!(ws.is_some(), "cold solve must yield a working set");
2772
2773        // Build the warm-start iterate from the converged finalize
2774        // payload (just x; pad multipliers to 0 since the test
2775        // problem is convex).
2776        let (x_recv, _) = finalize_slot.borrow().clone().unwrap();
2777        let warm = crate::sqp::SqpIterates {
2778            x: x_recv,
2779            lambda_g: vec![1.0],
2780            lambda_x: vec![0.0, 0.0],
2781            working: ws,
2782        };
2783        app.set_sqp_warm_start(warm);
2784
2785        // Warm solve.
2786        let status_b = app.optimize_tnlp(std::rc::Rc::clone(&tnlp_rc));
2787        assert_eq!(status_b, ApplicationReturnStatus::SolveSucceeded);
2788        assert!(app.last_sqp_working_set().is_some());
2789    }
2790
2791    #[test]
2792    fn application_sqp_warm_start_auto_clears_after_use() {
2793        let finalize_slot = std::rc::Rc::new(std::cell::RefCell::new(None));
2794        let tnlp_rc: std::rc::Rc<std::cell::RefCell<dyn TNLP>> =
2795            std::rc::Rc::new(std::cell::RefCell::new(ConvexEqTnlp {
2796                finalize_called: std::rc::Rc::clone(&finalize_slot),
2797            }));
2798        let mut app = IpoptApplication::new();
2799        app.initialize().unwrap();
2800        app.initialize_with_options_str("algorithm active-set-sqp\n")
2801            .unwrap();
2802        app.set_sqp_warm_start(crate::sqp::SqpIterates {
2803            x: vec![0.0, 1.0],
2804            lambda_g: vec![1.0],
2805            lambda_x: vec![0.0, 0.0],
2806            working: None,
2807        });
2808        assert!(app.sqp_warm_start.is_some());
2809        let _ = app.optimize_tnlp(std::rc::Rc::clone(&tnlp_rc));
2810        assert!(
2811            app.sqp_warm_start.is_none(),
2812            "warm-start input must be auto-cleared after use"
2813        );
2814    }
2815
2816    #[test]
2817    fn application_sqp_suboptions_default_when_unset() {
2818        // Without any sqp_* settings, the snapshot should equal
2819        // SqpOptions::default().
2820        let mut app = IpoptApplication::new();
2821        app.initialize().unwrap();
2822        let snap = app.algorithm_builder_snapshot();
2823        let d = crate::sqp::SqpOptions::default();
2824        assert_eq!(snap.sqp.globalization, d.globalization);
2825        assert_eq!(snap.sqp.hessian, d.hessian);
2826        assert_eq!(snap.sqp.max_iter, d.max_iter);
2827        assert!((snap.sqp.tol - d.tol).abs() < 1e-18);
2828        assert!((snap.sqp.constr_viol_tol - d.constr_viol_tol).abs() < 1e-18);
2829        assert!((snap.sqp.dual_inf_tol - d.dual_inf_tol).abs() < 1e-18);
2830        assert!((snap.sqp.l1_penalty - d.l1_penalty).abs() < 1e-18);
2831        assert!((snap.sqp.bt_reduction - d.bt_reduction).abs() < 1e-18);
2832        assert!((snap.sqp.bt_min_alpha - d.bt_min_alpha).abs() < 1e-18);
2833        assert_eq!(snap.sqp.print_level, d.print_level);
2834        assert_eq!(snap.sqp.lbfgs_max_history, d.lbfgs_max_history);
2835    }
2836
2837    #[test]
2838    fn application_reports_problem_dimensions() {
2839        let app = IpoptApplication::new();
2840        let mut tnlp = Hs071Stub;
2841        let info = app.problem_dimensions(&mut tnlp).unwrap();
2842        assert_eq!(info.n, 4);
2843        assert_eq!(info.m, 2);
2844        assert_eq!(info.nnz_jac_g, 8);
2845        assert_eq!(info.nnz_h_lag, 10);
2846    }
2847}