Skip to main content

pounce_cli/
qp_extract.rs

1//! Extract a `pounce_convex::QpProblem` (standard form) from a parsed
2//! `.nl` problem, for the LP/QP dispatch path (Phase 2).
3//!
4//! The classifier (`crate::dispatch`) has already decided the problem is
5//! an LP or convex QP; this module marshals the parsed `NlProblem` into
6//! the standard form the convex IPM consumes:
7//!
8//! ```text
9//! minimize    ½ xᵀP x + cᵀx
10//! subject to  A x = b          (equalities)
11//!             G x ≤ h          (inequalities)
12//!             lb ≤ x ≤ ub      (the variable box)
13//! ```
14//!
15//! Mapping from the `.nl` representation:
16//! - **Objective.** `P` is the Hessian of the (degree-≤2) objective —
17//!   recovered with the same `analyze_quadratic` the classifier uses, so
18//!   `P` here is exactly the matrix whose definiteness was tested. `c`
19//!   is the objective's linear part. A `maximize` objective is negated
20//!   into a minimization.
21//! - **Constraints.** Each row has a linear part and bounds `g_l ≤ row ≤
22//!   g_u`. An equality (`g_l == g_u`) becomes a row of `A`; a one- or
23//!   two-sided inequality becomes one or two rows of `G` (`row ≤ g_u`
24//!   and/or `−row ≤ −g_l`).
25//! - **Variable bounds.** Present `x_l`/`x_u` become the solver's explicit
26//!   box (see [`extract_box`] for why they are no longer emitted as `G`
27//!   rows). The `.nl` "infinity" sentinel is read directionally: `x_l ≤
28//!   -1e19` is no lower bound, `x_u ≥ 1e19` is no upper bound. A bound past
29//!   the *opposite* sentinel (an upper bound of `-5e20`) is an ordinary
30//!   bound and is kept.
31
32use crate::nl_reader::NlProblem;
33// Bound presence is read **directionally** — a lower bound is absent only at
34// or below `-1e19`, an upper bound only at or above `+1e19`. This file used a
35// symmetric `|v| < 1e19` test (gh #401): a real upper bound of `-5e20` failed
36// it and was dropped from `G` entirely, so the QP was solved over a strictly
37// larger box and reported `Optimal` at a point the model excludes.
38use pounce_common::types::{lower_bound_present, upper_bound_present};
39use pounce_convex::{ConeSpec, QpProblem, QpResiduals, QpSolution, Triplet};
40
41/// Ipopt's `bound_relax_factor` widening, as the convex extractors apply it.
42///
43/// The NLP path widens `x_L/x_U` and the inequality-row bounds `d_L/d_U`
44/// before the algorithm ever sees them (`OrigIpoptNlp::relax_bounds`, driven
45/// from `Application` with `bound_relax_factor` — Ipopt default `1e-8` —
46/// capped by `constr_viol_tol`, default `1e-4`). The convex path did not,
47/// so the *same binary* solved a materially different model depending on
48/// `solver_selection`.
49///
50/// That is not a hairline difference on a constraint-degenerate model. On
51/// `LISWET1` (gh #744) every one of the 10 000 monotonicity rows is active at
52/// the optimum and the multipliers sum to `1.6e9`, so a `1e-8` widening of the
53/// rows buys `9.0` of objective — the convex arm returned the exact optimum
54/// `36.1224` and the NLP arm (and Ipopt-MA57) the relaxed one, `27.1221`, and
55/// the 33% gap was read as a convex-solver bug. Both arms now relax, so both
56/// report `27.1221`, and `bound_relax_factor=0` gets `36.1224` from either.
57///
58/// Faithful to `relax_bounds` in three details that matter:
59/// * **Equality rows are not relaxed.** Upstream they live in `c(x) = 0`,
60///   which `relax_bounds` never touches; only `d_L/d_U` (inequality rows) and
61///   the variable box are widened.
62/// * **Rows use the scale-relative width** `min(factor, cap)·|b|` (with `|b|`
63///   read as `1` at a declared-zero bound), the gh #385 form. The variable box
64///   keeps the upstream absolute formula `min(factor·max(|b|,1), cap)`.
65/// * **Fixed variables (`x_l == x_u`) keep their bounds.** Under the default
66///   `fixed_variable_treatment=make_parameter` upstream removes them before
67///   `relax_bounds` runs, so they are never widened.
68#[derive(Debug, Clone, Copy, PartialEq)]
69pub struct BoundRelax {
70    /// `bound_relax_factor`. Non-positive disables the widening entirely.
71    pub factor: f64,
72    /// `constr_viol_tol` — the cap on the widening.
73    pub cap: f64,
74}
75
76impl BoundRelax {
77    /// No widening — the model exactly as declared. What the convex path did
78    /// unconditionally before gh #744, and what `bound_relax_factor=0` selects.
79    pub const NONE: Self = Self {
80        factor: 0.0,
81        cap: 0.0,
82    };
83
84    /// Whether any widening is actually applied. Public so a caller can tell
85    /// "the declared model and the solved model coincide" from "they differ",
86    /// which struct equality against [`Self::NONE`] cannot: `bound_relax_factor=0`
87    /// zeroes the factor but leaves `cap` at `constr_viol_tol`, so the pair is
88    /// inactive without being `NONE`.
89    pub fn active(self) -> bool {
90        self.factor > 0.0 && self.cap > 0.0
91    }
92
93    /// Widening of a variable bound `b`: `min(factor·max(|b|,1), cap)`.
94    fn var_delta(self, b: f64) -> f64 {
95        if !self.active() {
96            return 0.0;
97        }
98        (self.factor.abs() * b.abs().max(1.0)).min(self.cap)
99    }
100
101    /// Widening of an inequality-row bound `b`: `min(factor, cap)·|b|`, with a
102    /// declared-zero bound taking the absolute width (it has no scale).
103    /// The widening to apply to a row whose declared sides are `lo`/`hi`.
104    ///
105    /// A crossed pair (`lo > hi`) declares an *empty* feasible set — an
106    /// inconsistent model, which the NLP path rejects as
107    /// `Invalid_Problem_Definition` before `relax_bounds` is ever reached.
108    /// Widening both sides of one closes the gap whenever the crossing is
109    /// narrower than the relaxation, turning "this model has no feasible
110    /// point" into an optimal answer. A crossed row is therefore passed
111    /// through exactly as declared, so the emptiness screen still sees it
112    /// (gh #491).
113    fn for_row(self, lo: f64, hi: f64) -> Self {
114        if lower_bound_present(lo) && upper_bound_present(hi) && lo > hi {
115            Self::NONE
116        } else {
117            self
118        }
119    }
120
121    fn row_delta(self, b: f64) -> f64 {
122        if !self.active() {
123            return 0.0;
124        }
125        let scale = if b == 0.0 { 1.0 } else { b.abs() };
126        self.factor.abs().min(self.cap) * scale
127    }
128}
129
130/// Convert a classified LP/convex-QP `NlProblem` into `QpProblem`
131/// standard form. Returns `None` if the objective is not actually a
132/// degree-≤2 polynomial (should not happen for a problem the classifier
133/// routed here, but the conversion is total and falls back gracefully).
134pub fn extract_qp(prob: &NlProblem, relax: BoundRelax) -> Option<QpProblem> {
135    Some(extract_qp_with_map(prob, relax)?.0) // drops con_map + reporting constant
136}
137
138/// Where each `.nl` constraint's rows landed in the standard-form QP, so
139/// the QP's multipliers can be mapped back to a per-`.nl`-constraint
140/// dual for the `.sol`. One entry per original constraint, in order.
141#[derive(Debug, Clone)]
142pub enum ConRowMap {
143    /// Equality constraint → row `a_row` of `A` (multiplier `y[a_row]`).
144    Eq { a_row: usize },
145    /// Inequality / range constraint → up to two rows of `G`: the
146    /// `row ≤ g_u` upper bound and/or the `−row ≤ −g_l` lower bound
147    /// (multipliers `z[..]`, each ≥ 0).
148    Ineq {
149        upper: Option<usize>,
150        lower: Option<usize>,
151    },
152}
153
154/// The residuals of `sol` measured against the model **as declared**, before
155/// the [`BoundRelax`] widening.
156///
157/// [`QpSolution::kkt_residuals`] measures the problem the solver was handed,
158/// whose inequality rows and variable box are widened by
159/// `bound_relax_factor`. That is correct for the convergence test — the
160/// solver must converge on the model it is solving, and pounce-convex's own
161/// acceptance tests call it on exactly that model — and wrong for the number
162/// a caller reads as "how well does my model hold".
163///
164/// The gap is the widening itself, `min(factor, cap)·|b|` per row. On
165/// `afiro` the returned point sits `4.99e-06` outside the declared row
166/// `b = 500` — precisely `1e-8 · 500` — while the widened measurement reads
167/// `8.68e-13`, seven orders tighter, because the point does satisfy the
168/// widened row. `25fv47` reports `2.19e-11` against a declared `1.97e-05`.
169/// Neither is a solver defect: both are the widening working as designed and
170/// then being reported against the wrong model.
171///
172/// Returns `None` when no widening was applied (the two measurements
173/// coincide by construction, so the caller keeps the one it already has) or
174/// when re-extraction fails.
175pub fn declared_residuals_qp(
176    prob: &NlProblem,
177    sol: &QpSolution,
178    relax: BoundRelax,
179) -> Option<QpResiduals> {
180    if !relax.active() {
181        return None;
182    }
183    let (declared, _, _) = extract_qp_with_map(prob, BoundRelax::NONE)?;
184    // Structurally the re-extraction differs from the solved model only in
185    // the bound VALUES, so the shapes must agree; a mismatch would silently
186    // measure one model's point against another's rows.
187    debug_assert_eq!(declared.n, sol.x.len(), "declared re-extraction changed n");
188    // Only `primal_infeasibility` is consumed. The dual and complementarity
189    // terms this also computes are measured against a model whose multipliers
190    // are not the ones that produced them, so they are meaningless here and
191    // deliberately unused rather than reported.
192    Some(sol.kkt_residuals(&declared))
193}
194
195/// [`declared_residuals_qp`] for the conic arm: measures each block with its
196/// own cone, as [`QpSolution::kkt_residuals_conic`] does, so a converged SOC
197/// block is not read as infeasible (pounce#209). The cones come from the
198/// re-extraction rather than the caller, so the blocks and the bounds
199/// describe one model.
200pub fn declared_residuals_socp(
201    prob: &NlProblem,
202    sol: &QpSolution,
203    relax: BoundRelax,
204) -> Option<QpResiduals> {
205    if !relax.active() {
206        return None;
207    }
208    let (declared, _, _, cones) = extract_socp_with_map(prob, BoundRelax::NONE)?;
209    debug_assert_eq!(declared.n, sol.x.len(), "declared re-extraction changed n");
210    // As above: only `primal_infeasibility` is consumed.
211    Some(sol.kkt_residuals_conic(&declared, &cones))
212}
213
214/// Extract the QP, the constraint→row provenance map, and the objective
215/// constant folded into the nonlinear tree (see below), together.
216///
217/// The third return value is the **degree-0 term of the nonlinear
218/// objective** (e.g. the `+9` of `(x₀−3)²` that AMPL/Pyomo emit inside the
219/// nonlinear tree rather than in `NlProblem::obj_constant`). The QP itself
220/// ignores it — it does not move the minimizer — but the caller must add
221/// it to the *reported* objective so the convex solve agrees with the NLP
222/// path. It is returned in the problem's natural (user) sense, *not*
223/// multiplied by the maximize/minimize `sign`.
224pub fn extract_qp_with_map(
225    prob: &NlProblem,
226    relax: BoundRelax,
227) -> Option<(QpProblem, Vec<ConRowMap>, f64)> {
228    let n = prob.n;
229    let sign = if prob.minimize { 1.0 } else { -1.0 };
230
231    // --- objective Hessian P (lower triangle) + nonlinear-tree linear part
232    //     + nonlinear-tree constant (degree-0 term, for reporting only) ---
233    let (hess, obj_nl_linear, obj_nl_constant) = prob.obj_nonlinear.analyze_quadratic_full()?;
234    let mut p_lower: Vec<Triplet> = Vec::with_capacity(hess.len());
235    for ((i, j), v) in &hess {
236        // analyze_quadratic returns (i ≤ j) upper-ish keys; store as
237        // lower triangle (row ≥ col) for the solver.
238        let (row, col) = if i >= j { (*i, *j) } else { (*j, *i) };
239        p_lower.push(Triplet::new(row, col, sign * v));
240    }
241
242    // --- objective linear term c ---
243    // Two disjoint sources, exactly as the NLP path's eval_f sums them:
244    // the `.nl` linear section (`obj_linear`) and the degree-1 terms AMPL
245    // kept inside the nonlinear objective tree (e.g. the `−6·x₀` of
246    // `(x₀−3)²`). Dropping the latter silently solves the wrong objective.
247    let mut c = vec![0.0; n];
248    for (var, coef) in &prob.obj_linear {
249        c[*var] += sign * coef;
250    }
251    for (var, coef) in &obj_nl_linear {
252        c[*var] += sign * coef;
253    }
254
255    // --- constraints: equalities → A x = b, inequalities → G x ≤ h ---
256    let mut a: Vec<Triplet> = Vec::new();
257    let mut b: Vec<f64> = Vec::new();
258    let mut g: Vec<Triplet> = Vec::new();
259    let mut h: Vec<f64> = Vec::new();
260    let mut con_map: Vec<ConRowMap> = Vec::with_capacity(prob.con_linear.len());
261
262    for (row, lin) in prob.con_linear.iter().enumerate() {
263        let lo = prob.g_l[row];
264        let hi = prob.g_u[row];
265
266        // Combine the `.nl` linear section with any degree-≤1 terms AMPL
267        // folded into the (here empty-Hessian) nonlinear tree — the
268        // classifier admits constraint rows whose nonlinear expression
269        // reduces to degree ≤ 1 (`dispatch.rs`), e.g. defined variables
270        // or a quadratic the writer wrote out and cancelled exactly, and
271        // those linear/constant terms live in `con_nonlinear`, not
272        // `con_linear`. Dropping them silently solves the wrong
273        // constraint. (A row whose coefficients cancelled *in the
274        // recognizer's own arithmetic* does not reach here at all: it
275        // never classifies LP/QP — gh #685.) The folded constant
276        // shifts the bounds: `g_l ≤ row + k ≤ g_u  ⇔  g_l−k ≤ row ≤ g_u−k`.
277        // This mirrors the SOCP extractor's linear-constraint handling.
278        let (nl_lin, const_shift) = prob.con_nonlinear[row]
279            .analyze_quadratic_full()
280            .map(|(_, l, k)| (l, k))
281            .unwrap_or_default();
282        let mut coef = vec![0.0; n];
283        for (var, v) in lin {
284            coef[*var] += *v;
285        }
286        for (var, v) in &nl_lin {
287            coef[*var] += *v;
288        }
289        let nonzeros = || coef.iter().enumerate().filter(|(_, v)| **v != 0.0);
290
291        if lo == hi && lower_bound_present(lo) && upper_bound_present(hi) {
292            // Equality row.
293            let eq_row = next_row(&b);
294            for (var, v) in nonzeros() {
295                a.push(Triplet::new(eq_row, var, *v));
296            }
297            b.push(lo - const_shift);
298            con_map.push(ConRowMap::Eq { a_row: eq_row });
299        } else {
300            // Inequality row. Both sides carry the `bound_relax_factor`
301            // widening the NLP path applies to `d_L/d_U` (see [`BoundRelax`]);
302            // it is zero when the caller passed `BoundRelax::NONE`, and on a
303            // crossed row, which must stay crossed.
304            let relax = relax.for_row(lo, hi);
305            // Upper bound: row ≤ hi.
306            let upper = if upper_bound_present(hi) {
307                let gr = next_row(&h);
308                for (var, v) in nonzeros() {
309                    g.push(Triplet::new(gr, var, *v));
310                }
311                h.push(hi + relax.row_delta(hi) - const_shift);
312                Some(gr)
313            } else {
314                None
315            };
316            // Lower bound: row ≥ lo  ⇔  −row ≤ −lo.
317            let lower = if lower_bound_present(lo) {
318                let gr = next_row(&h);
319                for (var, v) in nonzeros() {
320                    g.push(Triplet::new(gr, var, -*v));
321                }
322                h.push(-(lo - relax.row_delta(lo) - const_shift));
323                Some(gr)
324            } else {
325                None
326            };
327            con_map.push(ConRowMap::Ineq { upper, lower });
328        }
329    }
330
331    // --- variable bounds as the explicit box (not as `G` rows) ---
332    let (lb, ub) = extract_box(prob, relax);
333
334    Some((
335        QpProblem {
336            n,
337            p_lower,
338            c,
339            a,
340            b,
341            g,
342            h,
343            lb,
344            ub,
345        },
346        con_map,
347        obj_nl_constant,
348    ))
349}
350
351/// The `.nl` variable bounds as `pounce-convex`'s explicit box, with an
352/// absent bound spelled `∓∞`.
353///
354/// Both extractors used to emit each finite bound as a `G` row (`x_i ≤ x_u`,
355/// `−x_i ≤ −x_l`) and leave `lb`/`ub` empty. That was never wrong — the IPM
356/// re-expands finite bounds into exactly those rows internally — but it threw
357/// away the one thing the solvers can only get from the box: **that these
358/// rows are a box**. Three consequences, all real:
359///
360/// * The empty-box screen ([`pounce_convex`]'s `screen_variable_box`, gh #491)
361///   reads `lb`/`ub`, so a model with a reversed bound arrived as a pair of
362///   contradictory rows instead — an infeasibility that has to be *certified*
363///   numerically rather than seen. The interior-point method managed that at
364///   most widths but returned `NumericalFailure` at a `NaN` iterate for
365///   crossings around `1e-8`.
366/// * The active-set engine handles a box with bound *statuses*, not with
367///   constraint rows; feeding it `2n` extra rows made every `.nl` QP that much
368///   larger in the one dimension an active-set method pays combinatorially
369///   for.
370/// * Presolve reasons about `tlb`/`tub` directly, so bounds hidden in rows had
371///   to be rediscovered by activity-based tightening before any box reduction
372///   could fire.
373///
374/// The bound *multipliers* now come back in the solution's `z_lb`/`z_ub`
375/// rather than being decoded out of `z` by row position.
376fn extract_box(prob: &NlProblem, relax: BoundRelax) -> (Vec<f64>, Vec<f64>) {
377    // Two declared boxes are passed through untouched.
378    //
379    // A variable pinned by `x_l == x_u` is fixed, and upstream's default
380    // `fixed_variable_treatment=make_parameter` lifts it out of the problem
381    // before `relax_bounds` runs — so it is never widened. Keep it pinned
382    // here too; widening it would hand the solver two decision variables'
383    // worth of slack that the NLP path does not have.
384    //
385    // A *crossed* box (`x_l > x_u`) is an empty set, and the NLP path rejects
386    // it as `Invalid_Problem_Definition` before relaxation. Widening it by
387    // more than the crossing would close the gap and return an optimal point
388    // for a model with no feasible one — gh #491's `1e-8` fixture crosses by
389    // less than the default `2 × 1e-8` widening. Leave it crossed so the
390    // empty-box screen downstream still sees it.
391    let as_declared = |i: usize| {
392        let (l, u) = (prob.x_l[i], prob.x_u[i]);
393        lower_bound_present(l) && upper_bound_present(u) && l >= u
394    };
395    let lb = (0..prob.n)
396        .map(|i| {
397            let v = prob.x_l[i];
398            if !lower_bound_present(v) {
399                f64::NEG_INFINITY
400            } else if as_declared(i) {
401                v
402            } else {
403                v - relax.var_delta(v)
404            }
405        })
406        .collect();
407    let ub = (0..prob.n)
408        .map(|i| {
409            let v = prob.x_u[i];
410            if !upper_bound_present(v) {
411                f64::INFINITY
412            } else if as_declared(i) {
413                v
414            } else {
415                v + relax.var_delta(v)
416            }
417        })
418        .collect();
419    (lb, ub)
420}
421
422/// Map the QP solver's multipliers `(y, z)` back to a per-`.nl`-
423/// constraint dual vector (length `prob.m`), in the AMPL `.sol`
424/// convention used by POUNCE's NLP path.
425///
426/// The QP solver enforces stationarity `∇f + Aᵀy + Gᵀz = 0` with
427/// `z ≥ 0`, where each inequality `.nl` row contributes a `row ≤ g_u`
428/// (`+row`) and/or `−row ≤ −g_l` (`−row`) `G` row. The per-constraint
429/// `.nl`/Ipopt multiplier `λ` is recovered as:
430/// - equality: `λ = sign · y[a_row]`;
431/// - inequality: `λ = sign · (z_upper − z_lower)` — at most one of the
432///   two bound rows is active at a solution.
433///
434/// The inequality sign (`z_upper − z_lower`, *not* `z_lower − z_upper`)
435/// is fixed to match POUNCE's NLP path, which is the reference for what
436/// a POUNCE `.sol` carries; this is verified empirically against the NLP
437/// solve in the crate tests. `sign` undoes the maximize→minimize
438/// negation so the reported dual is in the user's original sense.
439pub fn recover_duals(prob: &NlProblem, con_map: &[ConRowMap], y: &[f64], z: &[f64]) -> Vec<f64> {
440    let sign = if prob.minimize { 1.0 } else { -1.0 };
441    con_map
442        .iter()
443        .map(|m| match m {
444            ConRowMap::Eq { a_row } => sign * y[*a_row],
445            ConRowMap::Ineq { upper, lower } => {
446                let zu = upper.map(|r| z[r]).unwrap_or(0.0);
447                let zl = lower.map(|r| z[r]).unwrap_or(0.0);
448                sign * (zu - zl)
449            }
450        })
451        .collect()
452}
453
454/// The next 0-based row index for a constraint block keyed by its RHS
455/// vector's current length.
456fn next_row(rhs: &[f64]) -> usize {
457    rhs.len()
458}
459
460/// Recover the per-variable **bound multipliers** from a solved QP or SOCP.
461///
462/// Both extractors put the `.nl` variable bounds in the explicit box
463/// ([`extract_box`]), so the solver returns their multipliers directly in
464/// `z_lb`/`z_ub` and this is a length-normalizing read rather than the
465/// row-position decode it used to be. Variables are 1:1 with the `.nl`
466/// variables in both extractors, so no index remap is needed; a slot without
467/// a finite bound stays `0.0` because no bound was active there.
468///
469/// The returned `z_lb` / `z_ub` are the raw non-negative multipliers of the
470/// *internal minimize* problem (a maximize objective was negated during
471/// extraction); the caller applies the maximize `sign` and the Ipopt
472/// `ipopt_zL_out = +z_l`, `ipopt_zU_out = −z_u` output convention.
473pub fn recover_bound_mults(prob: &NlProblem, sol: &QpSolution) -> (Vec<f64>, Vec<f64>) {
474    let read = |v: &[f64]| -> Vec<f64> {
475        (0..prob.n)
476            .map(|i| v.get(i).copied().unwrap_or(0.0))
477            .collect()
478    };
479    (read(&sol.z_lb), read(&sol.z_ub))
480}
481
482// ===========================================================================
483// QCQP → SOCP extraction
484// ===========================================================================
485
486/// Where each `.nl` constraint landed in the standard-form **conic** program,
487/// so the cone multipliers can be mapped back to a per-`.nl`-constraint dual.
488/// One entry per original constraint, in order. (Analogue of [`ConRowMap`] for
489/// the SOCP path produced by [`extract_socp_with_map`].)
490#[derive(Debug, Clone)]
491pub enum ConSocpMap {
492    /// Linear equality → row `a_row` of `A` (multiplier `y[a_row]`).
493    Eq { a_row: usize },
494    /// Linear inequality / range → up to two rows of the nonnegative `G`
495    /// block (`row ≤ g_u` and/or `−row ≤ −g_l`), multipliers `z[..] ≥ 0`.
496    Ineq {
497        upper: Option<usize>,
498        lower: Option<usize>,
499    },
500    /// Convex quadratic inequality `g(x) ≤ g_u`, reformulated to one
501    /// second-order cone. The first two cone rows both carry the linear
502    /// coefficient vector `a/κ`, so the original constraint multiplier is
503    /// recovered as `(z[r0] + z[r1])/κ` (see [`recover_socp_duals`]; `κ` is
504    /// the rotated cone's balancing factor, `1` unless `|b| > 1`).
505    Quad {
506        z_row0: usize,
507        z_row1: usize,
508        kappa: f64,
509    },
510}
511
512/// A deferred second-order-cone block, built after the nonnegative `G` rows
513/// are known so the cones partition `G` in row order (nonneg block first,
514/// then the SOCs).
515/// Everything here is sized by the row's own **support** `k` — the variables
516/// that actually appear in it — never by the problem width `n`. A QCQP row
517/// typically touches a handful of variables out of `n` in the hundreds of
518/// thousands (`nql180`: `k = 2`, `n = 129 601`), so an `n`-sized structure per
519/// row is the difference between kilobytes and tens of gigabytes.
520struct SocBlock {
521    /// Index in `con_map` of the originating constraint, to patch with the
522    /// final cone-row indices once they are assigned.
523    con_idx: usize,
524    /// Linear coefficients of the constraint as `(variable, coefficient)`,
525    /// ascending by variable and with zeros dropped.
526    a: Vec<(usize, f64)>,
527    /// `b_eff = (nonlinear constant) − g_u`, the constraint's degree-0 term
528    /// after moving the upper bound to the right: `½xᵀQx + aᵀx + b_eff ≤ 0`.
529    b_eff: f64,
530    /// Rows of the factor `F` with `FᵀF = Q`, each a sparse
531    /// `(variable, coefficient)` list in the problem's own indexing.
532    ///
533    /// Sparse rather than length-`n` (or even length-`k`) dense: a diagonal
534    /// `Q` — the `qssp180`/`nql180` regime — has rank `k` and one nonzero per
535    /// factor row, so a dense factor would be `k²` to hold a `k`-nonzero
536    /// object.
537    f_rows: Vec<Vec<(usize, f64)>>,
538}
539
540/// Convert a classified **convex QCQP** `NlProblem` into the conic standard
541/// form the SOCP IPM consumes:
542///
543/// ```text
544/// minimize    ½ xᵀP x + cᵀx
545/// subject to  A x = b
546///             h − G x  ∈  K        (K = nonneg orthant × second-order cones)
547/// ```
548///
549/// Returns `(QpProblem, con_map, obj_nl_constant, cones)`:
550/// - the objective `P`/`c` exactly as the LP/QP path builds them;
551/// - linear equalities in `A`/`b`; linear inequalities and finite variable
552///   bounds as a leading **nonnegative** `G` block; and each convex quadratic
553///   inequality `g(x) ≤ g_u` as one **second-order cone** block appended
554///   after it (so `cones` covers the `G` rows in order);
555/// - `con_map` mapping each original constraint to its rows for dual recovery;
556/// - `obj_nl_constant`, the objective's folded degree-0 term (added back to the
557///   reported value, exactly as in [`extract_qp_with_map`]).
558///
559/// `None` if the objective is not degree-≤2 (should not happen for a problem
560/// the classifier routed here). The reformulation of a convex quadratic
561/// `½xᵀQx + aᵀx + b_eff ≤ 0` (with `Q = FᵀF ⪰ 0`) is the standard rotated→
562/// standard SOC: writing `s = −(aᵀx + b_eff)`, the cone slack
563/// `(s+1, s−1, √2·Fx)` lies in the second-order cone iff `‖Fx‖² ≤ 2s`, i.e.
564/// iff the original constraint holds.
565///
566/// The cone is **centred** first (gh #1041): the part of `a` in the range of
567/// `Fᵀ` is absorbed by completing the square, `Fᵀw = a_∥`, so the constraint
568/// becomes `½‖Fx + w‖² + a_⊥ᵀx + (b_eff − ½‖w‖²) ≤ 0` and the cone slack is
569/// `(s+1, s−1, √2·(Fx + w))` with `s = −(a_⊥ᵀx + b_eff − ½‖w‖²)`. See
570/// [`centre_soc`] for why the uncentred form fails on a ball far from the
571/// origin.
572pub fn extract_socp_with_map(
573    prob: &NlProblem,
574    relax: BoundRelax,
575) -> Option<(QpProblem, Vec<ConSocpMap>, f64, Vec<ConeSpec>)> {
576    let n = prob.n;
577    let sign = if prob.minimize { 1.0 } else { -1.0 };
578
579    // --- objective P (lower triangle) + folded linear / constant terms ---
580    let (hess, obj_nl_linear, obj_nl_constant) = prob.obj_nonlinear.analyze_quadratic_full()?;
581    let mut p_lower: Vec<Triplet> = Vec::with_capacity(hess.len());
582    for ((i, j), v) in &hess {
583        let (row, col) = if i >= j { (*i, *j) } else { (*j, *i) };
584        p_lower.push(Triplet::new(row, col, sign * v));
585    }
586    let mut c = vec![0.0; n];
587    for (var, coef) in &prob.obj_linear {
588        c[*var] += sign * coef;
589    }
590    for (var, coef) in &obj_nl_linear {
591        c[*var] += sign * coef;
592    }
593
594    // --- constraints: equalities → A; linear ineqs → nonneg G block;
595    //     convex quadratics → deferred SOC blocks (added after the nonneg
596    //     rows so the cones partition G in row order) ---
597    let mut a: Vec<Triplet> = Vec::new();
598    let mut b: Vec<f64> = Vec::new();
599    let mut g: Vec<Triplet> = Vec::new();
600    let mut h: Vec<f64> = Vec::new();
601    let mut con_map: Vec<ConSocpMap> = Vec::with_capacity(prob.m);
602    let mut soc_blocks: Vec<SocBlock> = Vec::new();
603
604    for (row, lin) in prob.con_linear.iter().enumerate() {
605        let lo = prob.g_l[row];
606        let hi = prob.g_u[row];
607        let nl = &prob.con_nonlinear[row];
608        let quad = nl.analyze_quadratic_full();
609        let is_quadratic = matches!(&quad, Some((hmap, _, _)) if !hmap.is_empty());
610
611        if is_quadratic {
612            // Convex quadratic inequality `g(x) ≤ g_u` (the classifier
613            // guarantees an upper-only bound with PSD Hessian). Build the
614            // factor F (FᵀF = Q) and defer the SOC rows.
615            let (hmap, nl_lin, nl_const) = quad.expect("checked above");
616            // Linear coefficients a = linear-section + folded nonlinear-tree
617            // linear part, accumulated sparsely: a QCQP row's linear part is
618            // as narrow as its quadratic part, and `n` here can be six digits.
619            let mut a_map: std::collections::BTreeMap<usize, f64> =
620                std::collections::BTreeMap::new();
621            for (var, coef) in lin {
622                *a_map.entry(*var).or_insert(0.0) += *coef;
623            }
624            for (var, coef) in &nl_lin {
625                *a_map.entry(*var).or_insert(0.0) += *coef;
626            }
627            let a_vec: Vec<(usize, f64)> = a_map.into_iter().filter(|&(_, c)| c != 0.0).collect();
628
629            let f_rows = socp_factor_rows(&hmap);
630            let con_idx = con_map.len();
631            con_map.push(ConSocpMap::Quad {
632                z_row0: 0,
633                z_row1: 0,
634                kappa: 1.0,
635            }); // patched in the SOC pass below
636            soc_blocks.push(SocBlock {
637                con_idx,
638                a: a_vec,
639                b_eff: nl_const - (hi + relax.row_delta(hi)),
640                f_rows,
641            });
642            continue;
643        }
644
645        // Linear constraint. Combine the `.nl` linear section with any
646        // degree-≤1 terms AMPL folded into the (here empty-Hessian)
647        // nonlinear tree, and shift the bounds by the folded constant.
648        let (nl_lin, const_shift) = quad.map(|(_, l, k)| (l, k)).unwrap_or_default();
649        let mut coef = vec![0.0; n];
650        for (var, v) in lin {
651            coef[*var] += *v;
652        }
653        for (var, v) in &nl_lin {
654            coef[*var] += *v;
655        }
656        let nonzeros = || coef.iter().enumerate().filter(|(_, v)| **v != 0.0);
657        if lo == hi && lower_bound_present(lo) && upper_bound_present(hi) {
658            let eq_row = next_row(&b);
659            for (var, v) in nonzeros() {
660                a.push(Triplet::new(eq_row, var, *v));
661            }
662            b.push(lo - const_shift);
663            con_map.push(ConSocpMap::Eq { a_row: eq_row });
664        } else {
665            let relax = relax.for_row(lo, hi);
666            let upper = if upper_bound_present(hi) {
667                let gr = next_row(&h);
668                for (var, v) in nonzeros() {
669                    g.push(Triplet::new(gr, var, *v));
670                }
671                h.push(hi + relax.row_delta(hi) - const_shift);
672                Some(gr)
673            } else {
674                None
675            };
676            let lower = if lower_bound_present(lo) {
677                let gr = next_row(&h);
678                for (var, v) in nonzeros() {
679                    g.push(Triplet::new(gr, var, -*v));
680                }
681                h.push(-(lo - relax.row_delta(lo) - const_shift));
682                Some(gr)
683            } else {
684                None
685            };
686            con_map.push(ConSocpMap::Ineq { upper, lower });
687        }
688    }
689
690    // Variable bounds go in the explicit box, not into this orthant block —
691    // see [`extract_box`]. `solve_socp_ipm` appends them as a trailing
692    // nonnegative block of its own, *after* the cones, so they stay outside
693    // the partition `cones` has to cover.
694    let (lb, ub) = extract_box(prob, relax);
695
696    // The nonnegative block is every G row built so far. The cones list must
697    // cover G in row order: this orthant block, then one SOC per quadratic.
698    let num_nonneg = h.len();
699    let mut cones: Vec<ConeSpec> = Vec::with_capacity(1 + soc_blocks.len());
700    if num_nonneg > 0 {
701        cones.push(ConeSpec::Nonneg(num_nonneg));
702    }
703
704    // --- emit the deferred second-order cones ---
705    for blk in soc_blocks {
706        let r = blk.f_rows.len();
707        let dim = r + 2;
708        // Complete the square: `a = Fᵀw + a_⊥`, `b = b_eff − ½‖w‖²`.
709        let (a_perp, w) = centre_soc(&blk.f_rows, &blk.a);
710        let b_c = blk.b_eff - 0.5 * w.iter().map(|v| v * v).sum::<f64>();
711        // Balance the rotated cone's two factors (gh #1056): see
712        // [`rotated_cone_balance`]. `κ = 1` is the historical `(s+1, s−1)`.
713        let kappa = rotated_cone_balance(b_c);
714        let row0 = next_row(&h);
715        // s0 = s/κ + κ, s = −b − a_⊥ᵀx  →  G row = a_⊥/κ, h = −b/κ + κ.
716        for &(var, coef) in &a_perp {
717            g.push(Triplet::new(row0, var, coef / kappa));
718        }
719        h.push(-b_c / kappa + kappa);
720        let row1 = next_row(&h);
721        // s1 = s/κ − κ  →  G row = a_⊥/κ, h = −b/κ − κ.
722        for &(var, coef) in &a_perp {
723            g.push(Triplet::new(row1, var, coef / kappa));
724        }
725        h.push(-b_c / kappa - kappa);
726        // s_{2+k} = √2·(Fx + w)_k  →  G row = −√2·F_k, h = √2·w_k.
727        let sqrt2 = std::f64::consts::SQRT_2;
728        for (f, &wk) in blk.f_rows.iter().zip(&w) {
729            let gr = next_row(&h);
730            for &(var, fv) in f {
731                g.push(Triplet::new(gr, var, -sqrt2 * fv));
732            }
733            h.push(sqrt2 * wk);
734        }
735        cones.push(ConeSpec::SecondOrder(dim));
736        con_map[blk.con_idx] = ConSocpMap::Quad {
737            z_row0: row0,
738            z_row1: row1,
739            kappa,
740        };
741    }
742
743    Some((
744        QpProblem {
745            n,
746            p_lower,
747            c,
748            a,
749            b,
750            g,
751            h,
752            lb,
753            ub,
754        },
755        con_map,
756        obj_nl_constant,
757        cones,
758    ))
759}
760
761/// Map the SOCP solver's multipliers `(y, z)` back to a per-`.nl`-constraint
762/// dual vector (length `prob.m`), in POUNCE's NLP-path `.sol` convention.
763///
764/// Linear rows reuse the QP-path recovery (`y[a_row]` for an equality;
765/// `z_upper − z_lower` for an inequality). For a convex quadratic
766/// `g(x) ≤ g_u` reformulated to a second-order cone, the constraint
767/// multiplier is recovered as the sum of the two cone duals on the rows
768/// carrying the linear coefficient vector `a`: `λ = z[r0] + z[r1]`. (At a
769/// KKT point stationarity reads `λ(∇g) = (z[r0]+z[r1])·a + …`, so this sum is
770/// the original multiplier; the cone's remaining rows reconstruct the `Qx`
771/// part.) `sign` undoes the maximize→minimize negation.
772pub fn recover_socp_duals(
773    prob: &NlProblem,
774    con_map: &[ConSocpMap],
775    y: &[f64],
776    z: &[f64],
777) -> Vec<f64> {
778    let sign = if prob.minimize { 1.0 } else { -1.0 };
779    con_map
780        .iter()
781        .map(|m| match m {
782            ConSocpMap::Eq { a_row } => sign * y[*a_row],
783            ConSocpMap::Ineq { upper, lower } => {
784                let zu = upper.map(|r| z[r]).unwrap_or(0.0);
785                let zl = lower.map(|r| z[r]).unwrap_or(0.0);
786                sign * (zu - zl)
787            }
788            ConSocpMap::Quad {
789                z_row0,
790                z_row1,
791                kappa,
792            } => sign * (z[*z_row0] + z[*z_row1]) / kappa,
793        })
794        .collect()
795}
796
797/// The balancing factor `κ` for a rotated cone whose constant is `b`
798/// (gh #1056).
799///
800/// `½‖y‖² ≤ s` is the rotated cone `2·u·v ≥ ‖y‖²` with `u·v = s`, emitted as
801/// the standard cone `(u + v, u − v, √2·y)`. Taking `v = 1` — the historical
802/// `(s + 1, s − 1)` — puts the whole magnitude of `s` in one factor, and in
803/// doubles `1e20 ± 1` are both `1e20`: on `x₀² + x₁² ≤ 1e20` the row was
804/// `(1e20, 1e20, 2x₀, 2x₁)`, whose determinant is `−4‖x‖²`, so the declared
805/// cone was empty but for `x = 0` and the conic IPM ended `Numerical
806/// failure`. Well before the `1` vanishes the imbalance costs: the same
807/// inactive ball takes 159 iterations at `1e6`, and an *active* ball of
808/// radius `1e4` already failed.
809///
810/// `u = s/κ`, `v = κ` keeps `4uv = 4s`, so cone membership is unchanged, and
811/// with `κ ≈ √|b|` the two factors are the same size wherever `s` is near its
812/// constant. A power of two, so `s/κ` and `a/κ` are exact; `1` when
813/// `|b| ≤ 1`, which leaves every such row exactly as it was. The multiplier
814/// maps back as `(z₀ + z₁)/κ` ([`recover_socp_duals`]).
815pub(crate) fn rotated_cone_balance(b: f64) -> f64 {
816    if !(b.abs() > 1.0) || !b.is_finite() {
817        return 1.0;
818    }
819    let k = 2.0_f64.powi((0.5 * b.abs().log2()).round() as i32);
820    if k.is_finite() && k > 0.0 { k } else { 1.0 }
821}
822
823/// Split a quadratic row's linear part `a` into `Fᵀw + a_⊥` (gh #1041),
824/// returning `(a_⊥, w)` with `w` indexed like `f_rows`.
825///
826/// Without this the rotated cone carries `‖Fx‖²` and `2s = −2(aᵀx + b_eff)`
827/// as two separate, huge quantities whose *difference* is the constraint: on
828/// `(x − 1000)² ≤ 1` both are ~2·10⁶ at the optimum while the ball's radius is
829/// 1, so the cone is pinned to its boundary to six digits and the conic IPM
830/// stalls at an infeasible point (x ≈ 1003.4). Centred, the same row is
831/// `(2, 0, 2(x − 1000))` — the explicit SOC `pounce.solve_socp` solves
832/// exactly. The cancellation still happens, but once, in the scalar
833/// `b_eff − ½‖w‖²`, not inside every iterate.
834///
835/// `w` is the least-squares solution of `Fᵀw = a` (normal equations
836/// `FFᵀw = Fa`, positive definite because the factor rows are linearly
837/// independent by construction). A diagonal factor — one variable per row —
838/// takes the closed form `w_k = a_i / f_k` and leaves `a_⊥` exactly zero on
839/// the factored variables. Otherwise a residual entry at roundoff level of the
840/// two terms it is the difference of is set to zero, so a ball whose centre
841/// is in range does not keep a spurious `1e-16`-scale linear term.
842fn centre_soc(f_rows: &[Vec<(usize, f64)>], a: &[(usize, f64)]) -> (Vec<(usize, f64)>, Vec<f64>) {
843    use std::collections::BTreeMap;
844    let r = f_rows.len();
845    if r == 0 {
846        return (a.to_vec(), Vec::new());
847    }
848    let a_map: BTreeMap<usize, f64> = a.iter().copied().collect();
849    let a_at = |var: usize| a_map.get(&var).copied().unwrap_or(0.0);
850
851    let diagonal = f_rows.iter().all(|f| f.len() == 1) && {
852        let mut vars: Vec<usize> = f_rows.iter().map(|f| f[0].0).collect();
853        vars.sort_unstable();
854        vars.windows(2).all(|p| p[0] != p[1])
855    };
856    if diagonal {
857        let w: Vec<f64> = f_rows.iter().map(|f| a_at(f[0].0) / f[0].1).collect();
858        let covered: std::collections::BTreeSet<usize> = f_rows.iter().map(|f| f[0].0).collect();
859        let a_perp = a
860            .iter()
861            .copied()
862            .filter(|(v, _)| !covered.contains(v))
863            .collect();
864        return (a_perp, w);
865    }
866
867    // Normal equations `M w = F a`, `M = F Fᵀ` (r×r, SPD).
868    let dense_rows: Vec<BTreeMap<usize, f64>> =
869        f_rows.iter().map(|f| f.iter().copied().collect()).collect();
870    let dot = |x: &BTreeMap<usize, f64>, y: &[(usize, f64)]| -> f64 {
871        y.iter()
872            .map(|(v, c)| c * x.get(v).copied().unwrap_or(0.0))
873            .sum()
874    };
875    let mut m = vec![0.0; r * r];
876    for i in 0..r {
877        for j in 0..=i {
878            let v = dot(&dense_rows[i], &f_rows[j]);
879            m[i * r + j] = v;
880            m[j * r + i] = v;
881        }
882    }
883    let mut w: Vec<f64> = f_rows
884        .iter()
885        .map(|f| f.iter().map(|&(v, c)| c * a_at(v)).sum())
886        .collect();
887    // Cholesky M = L Lᵀ in place (lower triangle), then two triangular solves.
888    for j in 0..r {
889        let diag = m[j * r + j];
890        let mut d = diag;
891        for k in 0..j {
892            d -= m[j * r + k] * m[j * r + k];
893        }
894        // A dependent row's pivot is the difference of `diag` and what the
895        // earlier rows explain of it, so it lands at roundoff of `diag` —
896        // often just *above* zero — not at zero. Testing `d <= 0` alone let
897        // that through and solved with a pivot of pure noise.
898        if d.is_nan() || d <= 16.0 * f64::EPSILON * diag {
899            // Numerically rank-deficient factor: leave the cone uncentred,
900            // which is exactly the pre-gh#1041 reformulation. Traced, because
901            // an offset ball that lands here is back to the conditioning
902            // gh#1041 fixed and would otherwise fail with no clue why.
903            tracing::debug!(
904                target: "pounce::socp",
905                rank = r, pivot = j, value = d,
906                "centre_soc: FFᵀ not positive definite; SOC block left uncentred"
907            );
908            return (a.to_vec(), vec![0.0; r]);
909        }
910        let d = d.sqrt();
911        m[j * r + j] = d;
912        for i in (j + 1)..r {
913            let mut v = m[i * r + j];
914            for k in 0..j {
915                v -= m[i * r + k] * m[j * r + k];
916            }
917            m[i * r + j] = v / d;
918        }
919    }
920    for i in 0..r {
921        for k in 0..i {
922            w[i] -= m[i * r + k] * w[k];
923        }
924        w[i] /= m[i * r + i];
925    }
926    for i in (0..r).rev() {
927        for k in (i + 1)..r {
928            w[i] -= m[k * r + i] * w[k];
929        }
930        w[i] /= m[i * r + i];
931    }
932
933    // a_⊥ = a − Fᵀw over the union of a's and F's supports.
934    let mut ftw: BTreeMap<usize, f64> = BTreeMap::new();
935    for (f, &wk) in f_rows.iter().zip(&w) {
936        for &(v, c) in f {
937            *ftw.entry(v).or_insert(0.0) += c * wk;
938        }
939    }
940    let mut vars: Vec<usize> = a_map.keys().chain(ftw.keys()).copied().collect();
941    vars.sort_unstable();
942    vars.dedup();
943    let a_perp = vars
944        .into_iter()
945        .filter_map(|v| {
946            let (av, fv) = (a_at(v), ftw.get(&v).copied().unwrap_or(0.0));
947            let res = av - fv;
948            let roundoff = 16.0 * f64::EPSILON * av.abs().max(fv.abs());
949            (res.abs() > roundoff).then_some((v, res))
950        })
951        .collect();
952    (a_perp, w)
953}
954
955/// Factor one quadratic row's Hessian `Q` into sparse rows `f_k` (in the
956/// problem's variable indexing) with `Σ_k f_k f_kᵀ = Q`.
957///
958/// Two paths, and the cheap one is the common one:
959///
960/// * **Diagonal `Q`** — `f` is one row per positive diagonal entry, each with a
961///   single nonzero `√d_i`. `O(k)` time and `O(k)` space in the row's support.
962///   This is the `qssp180`/`nql180` regime, where the general path's `O(k³)`
963///   factorization and `k²` factor would both be ruinous for no benefit.
964/// * **Otherwise** — pivoted Cholesky on a dense `k×k` over the row's support,
965///   then scatter the rows back to problem indices, dropping zeros. Sized by
966///   `k`, never by `n`.
967fn socp_factor_rows(
968    hmap: &std::collections::BTreeMap<(usize, usize), f64>,
969) -> Vec<Vec<(usize, f64)>> {
970    let support = quad_support(hmap);
971    let k = support.len();
972
973    if !hmap.keys().any(|&(i, j)| i != j) {
974        // Diagonal: one factor row per variable, holding that entry's square
975        // root. Nonpositive diagonal entries are the zero eigenvalues of a PSD
976        // diagonal matrix (convexity is already established before we get
977        // here), and contribute no row.
978        //
979        // Two details make this a *shortcut* rather than an approximation, so
980        // the fast path and the general path emit bit-identical factors and a
981        // problem's trajectory cannot depend on which one ran:
982        //
983        //  * the tolerance is the same expression `psd_outer_factor` uses, so
984        //    both drop exactly the same entries and agree on the cone's
985        //    dimension. Since gh #703 that expression is **relative to the
986        //    entry's own magnitude**, not to the largest diagonal: on a
987        //    diagonal `Q` no downdate ever touches another pivot, so every
988        //    positive entry is a genuine eigenvalue whatever its size, and the
989        //    filter here is simply `v > 0`. Cutting at `1e-12 · max_diag`
990        //    instead discarded real directions on a column-scaled model — see
991        //    `psd_outer_factor`;
992        //  * the value is `d / √d`, not `√d`. They differ by an ulp — `√2` is
993        //    `0x1.6a09e667f3bcdp+0` and `2/√2` is `0x1.6a09e667f3bccp+0` — and
994        //    `d / √d` is what the general path's `a[i][p] / d_pivot` computes.
995        //    Reproducing it is free; not reproducing it moved `qcqp_ball` from
996        //    17 conic iterations to 12 on a 2-ulp perturbation of one `G`
997        //    entry, which is precisely the kind of invisible trajectory change
998        //    `scripts/sweep-fixtures.sh` exists to catch.
999        //  * the rows come out in **pivot order**, largest diagonal first. On a
1000        //    diagonal matrix the rank-1 downdate only zeros the pivot, so the
1001        //    general path's complete pivoting visits entries in descending
1002        //    order with ties going to the lower index — which is what a
1003        //    *stable* sort of the (ascending-by-index) map entries gives.
1004        //    `‖Fx‖` does not care about row order, but `G` does: the rows land
1005        //    in the KKT matrix and the ordering feeds the fill-reducing
1006        //    permutation.
1007        let mut diag: Vec<(usize, f64)> = hmap
1008            .iter()
1009            .filter(|&(_, &v)| v > 0.0)
1010            .map(|(&(i, _), &v)| (i, v))
1011            .collect();
1012        diag.sort_by(|a, b| b.1.partial_cmp(&a.1).expect("PSD diagonal is finite"));
1013        return diag
1014            .into_iter()
1015            .map(|(i, v)| vec![(i, v / v.sqrt())])
1016            .collect();
1017    }
1018
1019    let dense = dense_symmetric_on_support(hmap, &support);
1020    psd_outer_factor(dense, k)
1021        .into_iter()
1022        .map(|f| {
1023            f.into_iter()
1024                .enumerate()
1025                .filter(|&(_, fv)| fv != 0.0)
1026                .map(|(loc, fv)| (support[loc], fv))
1027                .collect()
1028        })
1029        .collect()
1030}
1031
1032/// The variables a quadratic row touches, ascending and deduplicated.
1033///
1034/// This is the row's *support*, and it is what every downstream structure is
1035/// sized by. `hmap` stores only `i ≤ j`, so both coordinates must be collected.
1036fn quad_support(hmap: &std::collections::BTreeMap<(usize, usize), f64>) -> Vec<usize> {
1037    let mut s: Vec<usize> = hmap.keys().flat_map(|&(i, j)| [i, j]).collect();
1038    s.sort_unstable();
1039    s.dedup();
1040    s
1041}
1042
1043/// Build a dense symmetric `k×k` matrix over a row's `support` from a
1044/// [`QuadHessian`]-style map of `(i ≤ j) → Hessian entry` (diagonal entries are
1045/// the full `∂²/∂xᵢ²`, so `½xᵀHx` reproduces the quadratic form). Off-diagonals
1046/// are mirrored.
1047///
1048/// Sized by `k`, never by `n`: the previous `n×n` version asked for 134 GB on a
1049/// two-variable row of `nql180`.
1050fn dense_symmetric_on_support(
1051    hmap: &std::collections::BTreeMap<(usize, usize), f64>,
1052    support: &[usize],
1053) -> Vec<f64> {
1054    let k = support.len();
1055    // `support` is sorted, so a binary search is the local index.
1056    let loc = |v: usize| support.binary_search(&v).expect("key came from support");
1057    let mut dense = vec![0.0; k * k];
1058    for (&(i, j), &v) in hmap {
1059        let (li, lj) = (loc(i), loc(j));
1060        dense[li * k + lj] = v;
1061        dense[lj * k + li] = v;
1062    }
1063    dense
1064}
1065
1066/// Symmetric **pivoted (rank-revealing) Cholesky** of a PSD matrix `H`
1067/// (row-major `n×n`, consumed as scratch), returning the factor rows `f_k`
1068/// (each length `n`) such that `Σ_k f_k f_kᵀ = H` — equivalently `FᵀF = H`
1069/// with `F` the matrix whose rows are the `f_k`.
1070///
1071/// Callers pass a **row's support size** `k` here, not the problem width: this
1072/// is `O(n³)` in whatever it is handed, so the distinction is what makes a wide
1073/// QCQP extractable at all.
1074///
1075/// The number of rows is the
1076/// numerical rank, so a rank-deficient `Q` (e.g. `Q = vvᵀ`) yields the
1077/// minimal cone. Complete diagonal pivoting keeps the factorization stable
1078/// on the indefinite-looking-but-PSD matrices finite precision can produce.
1079///
1080/// # The rank test is relative to each pivot's own starting magnitude (gh #703)
1081///
1082/// This used to cut at `1e-12 · max_diag` — a *global* threshold — and that
1083/// is not a rank test, it is a units test. Rank deficiency is what the
1084/// rank-1 downdate reveals: a direction already spanned by the pivots taken
1085/// so far has its residual diagonal driven from `Q_pp` to (numerically)
1086/// zero. A direction that is merely *small in the coordinates the model was
1087/// written in* has its residual diagonal stay a healthy fraction of `Q_pp`,
1088/// and is a genuine eigenvalue however far below `max_diag` it sits.
1089///
1090/// The global cut confused the two, and silently. On
1091/// `qcqp_columns_illcond.nl` — the well-conditioned fixture under the exact
1092/// substitution `x_j → x_j / c_j`, so a matrix of provably identical rank —
1093/// the diagonal spans `[1.5e-7, 4.3e9]`, `1e-12 · max_diag ≈ 4.3e-3`
1094/// discarded **7 of 24** directions, and the cone `‖Fx‖ ≤ t` stopped
1095/// constraining them. The conic solver then satisfied *its* cone to
1096/// `2.66e-15` and reported `SolveSucceeded` at an objective 10% away from
1097/// the true optimum, on a point that violates the original quadratic row by
1098/// `4.948e+01` — 38% of its right-hand side. A relative residual check
1099/// would not have caught it either: measured against `‖Q‖ = 4.3e9` the
1100/// reconstruction error is `5.4e-13`. The dropped rank is the only signal.
1101///
1102/// `a[p][p] > 1e-12 · Q_pp` is that test, and it is invariant under the
1103/// diagonal congruence `Q → CQC` that provoked the bug, since both sides
1104/// scale by `c_p²`. It changes nothing about the pivot *order* (still the
1105/// largest remaining diagonal), so a well-scaled matrix factors bit for bit
1106/// as before, and it keeps the diagonal shortcut above interchangeable with
1107/// this path: on a diagonal `Q` no downdate touches a pivot, so both keep
1108/// exactly the positive entries.
1109fn psd_outer_factor(mut a: Vec<f64>, n: usize) -> Vec<Vec<f64>> {
1110    let mut rows: Vec<Vec<f64>> = Vec::new();
1111    // Each pivot's *initial* diagonal, so the rank test below can ask how far
1112    // the downdate has moved it rather than how it compares to the model's
1113    // units. Clamped at zero: a PSD matrix has `Q_ii ≥ 0`, and an entry that
1114    // finite precision has pushed slightly negative must not produce a
1115    // negative threshold that admits it.
1116    let d0: Vec<f64> = (0..n).map(|i| a[i * n + i].max(0.0)).collect();
1117    // Columns already decided — either factored out, or ruled a zero
1118    // eigenvalue. A pivoted Cholesky never revisits a pivot, and the residual
1119    // it leaves on that diagonal is roundoff, not a candidate.
1120    let mut settled = vec![false; n];
1121    for _ in 0..n {
1122        // Largest undecided diagonal pivot.
1123        let mut p = usize::MAX;
1124        let mut best = f64::NEG_INFINITY;
1125        for i in 0..n {
1126            if settled[i] {
1127                continue;
1128            }
1129            let d = a[i * n + i];
1130            if d > best {
1131                best = d;
1132                p = i;
1133            }
1134        }
1135        if p == usize::MAX {
1136            break;
1137        }
1138        // Rule it a zero eigenvalue when the downdate has reduced it to a
1139        // negligible fraction of where it started — that is the residual
1140        // saying the direction is already spanned. `best <= 0` (including a
1141        // pivot whose `d0` is zero, where the threshold is zero) rules it out
1142        // too.
1143        //
1144        // `continue`, not `break`: with an *absolute* threshold the pivot
1145        // order and the rank order were the same order, so the first failure
1146        // ended it. A relative threshold decouples them — a column whose `d0`
1147        // is `1e-20` is still live at a residual of `1e-20`, while a spent
1148        // column with `d0 = 2` is dead at the `4e-16` of roundoff it carries,
1149        // and the dead one sorts first. Breaking there would drop the live
1150        // column and make the rank depend on the model's units again, which
1151        // is the whole defect this test was rewritten to fix.
1152        if best <= 1e-12 * d0[p] || best <= 0.0 {
1153            settled[p] = true;
1154            continue;
1155        }
1156        settled[p] = true;
1157        let d = best.sqrt();
1158        // f = column p of the residual, scaled by 1/d.
1159        let mut f = vec![0.0; n];
1160        for i in 0..n {
1161            f[i] = a[i * n + p] / d;
1162        }
1163        // Rank-1 downdate: A ← A − f fᵀ.
1164        for i in 0..n {
1165            let fi = f[i];
1166            if fi == 0.0 {
1167                continue;
1168            }
1169            for j in 0..n {
1170                a[i * n + j] -= fi * f[j];
1171            }
1172        }
1173        rows.push(f);
1174    }
1175    rows
1176}
1177
1178#[cfg(test)]
1179mod tests {
1180    use super::*;
1181    use crate::nl_reader::NlBody;
1182    use crate::nl_reader::{BinOp, Expr};
1183    use pounce_convex::{QpOptions, QpStatus, solve_qp_ipm, solve_socp_ipm};
1184    use pounce_feral::FeralSolverInterface;
1185    use pounce_linsol::SparseSymLinearSolverInterface;
1186
1187    fn backend() -> Box<dyn SparseSymLinearSolverInterface> {
1188        Box::new(FeralSolverInterface::new())
1189    }
1190
1191    fn pow2(var: usize) -> Expr {
1192        Expr::Binary(
1193            BinOp::Pow,
1194            Box::new(Expr::Var(var)),
1195            Box::new(Expr::Const(2.0)),
1196        )
1197    }
1198
1199    /// min −x0 − x1  s.t.  x0² + x1² ≤ 1  → x* = (1/√2, 1/√2), f* = −√2.
1200    /// Exercises the QCQP→SOCP reformulation end-to-end: a rank-2 ball
1201    /// constraint becomes one second-order cone, no nonnegative block.
1202    #[test]
1203    fn extract_and_solve_socp_ball() {
1204        let prob = NlProblem {
1205            src: None,
1206            cse_bodies: Vec::new(),
1207            n: 2,
1208            m: 1,
1209            num_obj: 1,
1210            minimize: true,
1211            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
1212            obj_linear: vec![(0, -1.0), (1, -1.0)],
1213            obj_constant: 0.0,
1214            con_nonlinear: vec![NlBody::Tree(Expr::Binary(
1215                BinOp::Add,
1216                Box::new(pow2(0)),
1217                Box::new(pow2(1)),
1218            ))],
1219            con_linear: vec![vec![]],
1220            x_l: vec![-2e19, -2e19],
1221            x_u: vec![2e19, 2e19],
1222            g_l: vec![-2e19],
1223            g_u: vec![1.0],
1224            x0: vec![0.0, 0.0],
1225            lambda0: vec![0.0],
1226            suffixes: Default::default(),
1227            imported_funcs: Vec::new(),
1228            ampl_options: Vec::new(),
1229            nl_counts: None,
1230            n_discrete: None,
1231            var_names: Vec::new(),
1232            con_names: Vec::new(),
1233        };
1234        let (qp, con_map, obj_const, cones) =
1235            extract_socp_with_map(&prob, BoundRelax::NONE).expect("extract");
1236        assert_eq!(obj_const, 0.0);
1237        // No linear inequalities / bounds → no nonneg block; one SOC of
1238        // dimension rank(Q)+2 = 2+2 = 4.
1239        assert_eq!(cones, vec![ConeSpec::SecondOrder(4)]);
1240        assert_eq!(qp.m_ineq(), 4);
1241
1242        let sol = solve_socp_ipm(&qp, &cones, &QpOptions::default(), backend);
1243        assert_eq!(sol.status, QpStatus::Optimal);
1244        let inv_sqrt2 = 1.0 / 2.0_f64.sqrt();
1245        assert!((sol.x[0] - inv_sqrt2).abs() < 1e-5, "x0={}", sol.x[0]);
1246        assert!((sol.x[1] - inv_sqrt2).abs() < 1e-5, "x1={}", sol.x[1]);
1247        assert!(
1248            (sol.obj - (-2.0_f64.sqrt())).abs() < 1e-5,
1249            "obj={}",
1250            sol.obj
1251        );
1252
1253        // Analytic multiplier: c + λ·2x = 0 ⇒ λ = 1/(2x0) = √2/2 ≈ 0.7071,
1254        // positive (active upper bound), matching the `.sol` sign convention.
1255        let lambda = recover_socp_duals(&prob, &con_map, &sol.y, &sol.z);
1256        assert_eq!(lambda.len(), 1);
1257        assert!(
1258            (lambda[0] - 0.5 * 2.0_f64.sqrt()).abs() < 1e-3,
1259            "ball constraint dual={}",
1260            lambda[0]
1261        );
1262    }
1263
1264    /// min x0  s.t.  (x0−3)² ≤ 1  → feasible x0 ∈ [2, 4], optimum x0 = 2.
1265    /// The constraint's linear (`−6x0`) and constant (`+9`) terms are folded
1266    /// into the nonlinear tree; the reformulation must recover `b_eff = 9 − 1`
1267    /// so the cone encodes `x0² − 6x0 + 8 ≤ 0`, not `x0² ≤ 1`.
1268    #[test]
1269    fn extract_and_solve_socp_folds_constraint_constant() {
1270        let con = Expr::Binary(
1271            BinOp::Pow,
1272            Box::new(Expr::Binary(
1273                BinOp::Sub,
1274                Box::new(Expr::Var(0)),
1275                Box::new(Expr::Const(3.0)),
1276            )),
1277            Box::new(Expr::Const(2.0)),
1278        );
1279        let prob = NlProblem {
1280            src: None,
1281            cse_bodies: Vec::new(),
1282            n: 1,
1283            m: 1,
1284            num_obj: 1,
1285            minimize: true,
1286            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
1287            obj_linear: vec![(0, 1.0)],
1288            obj_constant: 0.0,
1289            con_nonlinear: vec![NlBody::Tree(con)],
1290            con_linear: vec![vec![]],
1291            x_l: vec![-2e19],
1292            x_u: vec![2e19],
1293            g_l: vec![-2e19],
1294            g_u: vec![1.0],
1295            x0: vec![0.0],
1296            lambda0: vec![0.0],
1297            suffixes: Default::default(),
1298            imported_funcs: Vec::new(),
1299            ampl_options: Vec::new(),
1300            nl_counts: None,
1301            n_discrete: None,
1302            var_names: Vec::new(),
1303            con_names: Vec::new(),
1304        };
1305        let (qp, _con_map, obj_const, cones) =
1306            extract_socp_with_map(&prob, BoundRelax::NONE).expect("extract");
1307        assert_eq!(obj_const, 0.0);
1308        assert_eq!(cones, vec![ConeSpec::SecondOrder(3)]); // rank 1 + 2.
1309
1310        let sol = solve_socp_ipm(&qp, &cones, &QpOptions::default(), backend);
1311        assert_eq!(sol.status, QpStatus::Optimal);
1312        assert!((sol.x[0] - 2.0).abs() < 1e-5, "x0={}", sol.x[0]);
1313    }
1314
1315    /// Build `min −x_i − x_j  s.t.  x_i² + x_j² ≤ 1` over `n` variables, where
1316    /// `i` and `j` are neither low-numbered nor adjacent.
1317    fn wide_ball(n: usize, i: usize, j: usize) -> NlProblem {
1318        let sq = |v: usize| {
1319            Expr::Binary(
1320                BinOp::Pow,
1321                Box::new(Expr::Var(v)),
1322                Box::new(Expr::Const(2.0)),
1323            )
1324        };
1325        NlProblem {
1326            src: None,
1327            cse_bodies: Vec::new(),
1328            n,
1329            m: 1,
1330            num_obj: 1,
1331            minimize: true,
1332            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
1333            obj_linear: vec![(i, -1.0), (j, -1.0)],
1334            obj_constant: 0.0,
1335            con_nonlinear: vec![NlBody::Tree(Expr::Binary(
1336                BinOp::Add,
1337                Box::new(sq(i)),
1338                Box::new(sq(j)),
1339            ))],
1340            con_linear: vec![vec![]],
1341            x_l: vec![-10.0; n],
1342            x_u: vec![10.0; n],
1343            g_l: vec![-2e19],
1344            g_u: vec![1.0],
1345            x0: vec![0.0; n],
1346            lambda0: vec![0.0],
1347            suffixes: Default::default(),
1348            imported_funcs: Vec::new(),
1349            ampl_options: Vec::new(),
1350            nl_counts: None,
1351            n_discrete: None,
1352            var_names: Vec::new(),
1353            con_names: Vec::new(),
1354        }
1355    }
1356
1357    /// The cone factor is built on the row's **support**, so its columns are
1358    /// support-local and must be scattered back through `support` on emission.
1359    /// If that scatter is dropped, a row touching `x11`/`x37` silently
1360    /// constrains `x0`/`x1` instead — a wrong answer, not a crash.
1361    #[test]
1362    fn socp_factor_columns_scatter_back_to_original_variables() {
1363        let prob = wide_ball(40, 11, 37);
1364        let (qp, _con_map, _obj_const, cones) =
1365            extract_socp_with_map(&prob, BoundRelax::NONE).expect("extract");
1366        assert_eq!(cones, vec![ConeSpec::SecondOrder(4)]); // rank 2 + 2.
1367
1368        // Every G entry in the two factor rows must sit in column 11 or 37.
1369        // Rows 0 and 1 are the `a`-rows (here empty: no linear part).
1370        let factor_cols: std::collections::BTreeSet<usize> =
1371            qp.g.iter().filter(|t| t.row >= 2).map(|t| t.col).collect();
1372        assert_eq!(
1373            factor_cols,
1374            [11usize, 37].into_iter().collect(),
1375            "factor rows must reference the row's own variables, got {factor_cols:?}"
1376        );
1377    }
1378
1379    /// The extractor must be sized by a row's support, not by the problem
1380    /// width. A two-variable quadratic row in a 50 000-variable problem needs
1381    /// kilobytes; sizing it `n×n` would ask for 20 GB and abort the process.
1382    #[test]
1383    fn socp_extraction_is_sized_by_support_not_problem_width() {
1384        let n = 50_000;
1385        let prob = wide_ball(n, 7, n - 3);
1386        let (qp, _con_map, _obj_const, cones) =
1387            extract_socp_with_map(&prob, BoundRelax::NONE).expect("extract");
1388        assert_eq!(cones, vec![ConeSpec::SecondOrder(4)]);
1389        // The cone contributes exactly two nonzeros per factor row.
1390        assert_eq!(qp.g.iter().filter(|t| t.row >= 2).count(), 2);
1391    }
1392
1393    /// The same scatter, but through the **dense** path: a cross term makes `Q`
1394    /// non-diagonal, so the row goes through the pivoted Cholesky and its
1395    /// support-local columns must be mapped back. Rank 1, so one factor row.
1396    #[test]
1397    fn socp_dense_factor_columns_scatter_back_to_original_variables() {
1398        // (x11 + x37)² ≤ 1 — off-diagonal Q, rank 1.
1399        let con = Expr::Binary(
1400            BinOp::Pow,
1401            Box::new(Expr::Binary(
1402                BinOp::Add,
1403                Box::new(Expr::Var(11)),
1404                Box::new(Expr::Var(37)),
1405            )),
1406            Box::new(Expr::Const(2.0)),
1407        );
1408        let mut prob = wide_ball(40, 11, 37);
1409        prob.con_nonlinear = vec![NlBody::Tree(con)];
1410
1411        let (qp, _con_map, _obj_const, cones) =
1412            extract_socp_with_map(&prob, BoundRelax::NONE).expect("extract");
1413        assert_eq!(cones, vec![ConeSpec::SecondOrder(3)], "rank 1 + 2");
1414        let factor_cols: std::collections::BTreeSet<usize> =
1415            qp.g.iter().filter(|t| t.row >= 2).map(|t| t.col).collect();
1416        assert_eq!(factor_cols, [11usize, 37].into_iter().collect());
1417    }
1418
1419    /// A diagonal `Q` must not be densified. `socp_factor_rows` returns one
1420    /// single-nonzero row per positive diagonal entry, so the factor is `O(k)`
1421    /// — this is what makes the very large diagonal QCQPs (`qssp180`,
1422    /// `nql180`) representable at all.
1423    #[test]
1424    fn diagonal_hessian_factors_in_linear_space() {
1425        let mut h = std::collections::BTreeMap::new();
1426        for i in 0..1000usize {
1427            h.insert((i, i), 4.0);
1428        }
1429        let rows = socp_factor_rows(&h);
1430        assert_eq!(rows.len(), 1000);
1431        assert!(
1432            rows.iter().all(|r| r.len() == 1),
1433            "a diagonal Q must give one nonzero per factor row"
1434        );
1435        for (k, r) in rows.iter().enumerate() {
1436            assert_eq!(r[0].0, k);
1437            assert!((r[0].1 - 2.0).abs() < 1e-12, "√4 = 2, got {}", r[0].1);
1438        }
1439    }
1440
1441    /// Both factor paths must satisfy the same contract, `Σ_k f_k f_kᵀ = Q`,
1442    /// and must agree on the rank. There are now two of them — an `O(k)`
1443    /// diagonal shortcut and the general pivoted Cholesky — so the contract is
1444    /// asserted directly rather than inferred from the shortcut's derivation.
1445    ///
1446    /// The near-zero diagonal entry is the rank agreement. Since gh #703 the
1447    /// rank test is relative to each pivot's *own* starting magnitude rather
1448    /// than to `max_diag`, so a diagonal matrix has no zero eigenvalues at
1449    /// all: `1e-20` on its own row is a genuine, tiny eigenvalue and both
1450    /// paths must **keep** it. What must not differ is which of them thinks
1451    /// so — a disagreement would build cones of different dimension for the
1452    /// same constraint.
1453    #[test]
1454    fn both_factor_paths_reconstruct_q_and_agree_on_rank() {
1455        let recon = |rows: &[Vec<(usize, f64)>]| {
1456            let mut q: std::collections::BTreeMap<(usize, usize), f64> = Default::default();
1457            for r in rows {
1458                for &(i, fi) in r {
1459                    for &(j, fj) in r {
1460                        *q.entry((i, j)).or_insert(0.0) += fi * fj;
1461                    }
1462                }
1463            }
1464            q.retain(|_, v| v.abs() > 1e-12);
1465            q
1466        };
1467
1468        // Diagonal path, with one entry twenty orders of magnitude below the
1469        // largest. Deliberately *not* descending by index: the largest entry
1470        // sits on the highest variable, so a shortcut that emitted in index
1471        // order would produce the right cone with the rows in the wrong order.
1472        let diag: std::collections::BTreeMap<(usize, usize), f64> =
1473            [((3, 3), 2.0), ((8, 8), 9.0), ((9, 9), 1e-20)]
1474                .into_iter()
1475                .collect();
1476        let drows = socp_factor_rows(&diag);
1477        assert_eq!(
1478            drows.len(),
1479            3,
1480            "1e-20 on its own row is a small eigenvalue, not a missing one"
1481        );
1482
1483        // The shortcut must be *bit*-identical to the general path, not merely
1484        // close: a 2-ulp difference in one `G` entry visibly moved `qcqp_ball`'s
1485        // conic trajectory (17 → 12 iterations). Run the same matrix through
1486        // `psd_outer_factor` and compare the raw bits.
1487        let support = quad_support(&diag);
1488        let general = psd_outer_factor(dense_symmetric_on_support(&diag, &support), support.len());
1489        let general_vals: Vec<f64> = general
1490            .iter()
1491            .map(|f| f.iter().copied().find(|v| *v != 0.0).expect("one nonzero"))
1492            .collect();
1493        let short_vals: Vec<f64> = drows.iter().map(|r| r[0].1).collect();
1494        assert_eq!(
1495            short_vals.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
1496            general_vals.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
1497            "diagonal shortcut must reproduce psd_outer_factor bit for bit \
1498             (got {short_vals:?} vs {general_vals:?})"
1499        );
1500        assert_eq!(drows[0][0].0, 8, "largest diagonal pivots first");
1501        assert_eq!(drows[1][0].0, 3);
1502        assert_eq!(drows[2][0].0, 9, "then the smallest, still emitted");
1503        assert!(
1504            (drows[2][0].1 - 1e-10).abs() < 1e-22,
1505            "√1e-20 = 1e-10, got {}",
1506            drows[2][0].1
1507        );
1508        let dq = recon(&drows);
1509        // Two entries, not three: `recon` drops anything under `1e-12`, and
1510        // `(1e-10)² = 1e-20` is under it. The row is in the factor; its
1511        // contribution to `Q` is genuinely below what a reconstruction can
1512        // see, which is the whole reason the *pivot* test cannot be absolute.
1513        assert_eq!(dq.len(), 2);
1514        assert!((dq[&(3, 3)] - 2.0).abs() < 1e-12);
1515        assert!((dq[&(8, 8)] - 9.0).abs() < 1e-12);
1516
1517        // General path: same matrix plus a cross term, so the diagonal
1518        // shortcut no longer applies. Q = [[2,1],[1,9]] on {3,8} is positive
1519        // definite, so full rank 2.
1520        let mut coupled = diag.clone();
1521        coupled.insert((3, 8), 1.0);
1522        let grows = socp_factor_rows(&coupled);
1523        assert_eq!(grows.len(), 3, "2 from the coupled block, 1 from `1e-20`");
1524        let gq = recon(&grows);
1525        assert!((gq[&(3, 3)] - 2.0).abs() < 1e-10);
1526        assert!((gq[&(8, 8)] - 9.0).abs() < 1e-10);
1527        assert!((gq[&(3, 8)] - 1.0).abs() < 1e-10);
1528        assert!((gq[&(8, 3)] - 1.0).abs() < 1e-10);
1529        assert!(
1530            !gq.contains_key(&(9, 9)),
1531            "1e-20 is below what the reconstruction resolves"
1532        );
1533    }
1534
1535    /// `psd_outer_factor` recovers a rank-1 `Q = vvᵀ` with a single factor row
1536    /// (minimal cone), and reconstructs `Q` exactly.
1537    #[test]
1538    fn psd_outer_factor_is_rank_revealing() {
1539        // Q = [[1,2],[2,4]] = v vᵀ with v = (1,2): rank 1.
1540        let q = vec![1.0, 2.0, 2.0, 4.0];
1541        let rows = psd_outer_factor(q.clone(), 2);
1542        assert_eq!(rows.len(), 1, "rank-1 Q must give one factor row");
1543        // Reconstruct Σ f fᵀ and compare to Q.
1544        let mut recon = vec![0.0; 4];
1545        for f in &rows {
1546            for i in 0..2 {
1547                for j in 0..2 {
1548                    recon[i * 2 + j] += f[i] * f[j];
1549                }
1550            }
1551        }
1552        for k in 0..4 {
1553            assert!((recon[k] - q[k]).abs() < 1e-9, "recon[{k}]={}", recon[k]);
1554        }
1555    }
1556
1557    /// **The rank of `Q` is a property of `Q`, not of the units its variables
1558    /// are measured in.** `psd_outer_factor` decides the dimension of the cone
1559    /// a QCQP row becomes, so if a change of units can change that number, the
1560    /// solver builds a different — smaller — feasible set for the same model
1561    /// and reports success on the answer to a different problem.
1562    ///
1563    /// That is exactly what gh #703 hit. The rank test used to be
1564    /// `1e-12 · max_diag`, one absolute cut for the whole matrix, which asks
1565    /// how a pivot compares to the *largest* entry rather than how far its own
1566    /// downdate has moved it. Rescaling the columns of `qcqp_columns` by
1567    /// `10^{-4}…10^{4}` spread `max_diag` over nineteen orders of magnitude and
1568    /// took the rank of a full-rank 24×24 row from 24 to **17** — seven real
1569    /// directions dropped, `SolveSucceeded`, a self-reported violation of
1570    /// `2.66e-15` against an actual one of `4.948e+01`, and an objective 10%
1571    /// off its well-conditioned twin.
1572    ///
1573    /// Diagonal congruence `Q → C Q C` with `C ≻ 0` diagonal is precisely a
1574    /// change of units, and it preserves rank exactly (Sylvester). So the test
1575    /// is: factor the same `Q` under a spread of column scalings and require
1576    /// the row count never to move.
1577    #[test]
1578    fn rank_does_not_depend_on_the_units_the_columns_are_measured_in() {
1579        // A 4×4 PSD matrix of exact rank 3: `Q = Σ_{k<3} v_k v_kᵀ` over three
1580        // independent vectors, so one direction is genuinely absent and the
1581        // factorization must find that too — an invariance test that only ever
1582        // returned `n` would be satisfied by a rank test that never fires.
1583        let vs = [
1584            [1.0, 2.0, 0.0, -1.0],
1585            [0.0, 1.0, 3.0, 1.0],
1586            [2.0, 0.0, 1.0, 4.0],
1587        ];
1588        let n = 4;
1589        let mut q = vec![0.0; n * n];
1590        for v in &vs {
1591            for i in 0..n {
1592                for j in 0..n {
1593                    q[i * n + j] += v[i] * v[j];
1594                }
1595            }
1596        }
1597        assert_eq!(psd_outer_factor(q.clone(), n).len(), 3, "unscaled rank");
1598
1599        // `C = diag(10^e)`. The exponents run over the same range the
1600        // `qcqp_columns` fixtures use, and are deliberately *not* uniform: a
1601        // uniform scaling is a scalar multiple, which even an absolute
1602        // threshold survives.
1603        for spread in [1i32, 2, 3, 4, 6] {
1604            for sign in [1i32, -1] {
1605                let c: Vec<f64> = (0..n)
1606                    .map(|i| 10f64.powi(sign * spread * (i as i32 - 1)))
1607                    .collect();
1608                let mut scaled = vec![0.0; n * n];
1609                for i in 0..n {
1610                    for j in 0..n {
1611                        scaled[i * n + j] = c[i] * q[i * n + j] * c[j];
1612                    }
1613                }
1614                let rank = psd_outer_factor(scaled, n).len();
1615                assert_eq!(
1616                    rank, 3,
1617                    "C Q C with C = diag(10^({sign}·{spread}·(i−1))) has the \
1618                     same rank as Q; got {rank}"
1619                );
1620            }
1621        }
1622    }
1623
1624    /// The companion property, on the other side of the same threshold: a
1625    /// direction that *is* spanned must still be dropped, however the columns
1626    /// are scaled. Rank invariance alone would be satisfied by never cutting
1627    /// anything, which would hand every row a full-dimensional cone and cost
1628    /// the `qssp180`-class models their whole reason for taking the conic
1629    /// route.
1630    #[test]
1631    fn a_spanned_direction_is_dropped_at_every_column_scaling() {
1632        // Exactly rank 1: `Q = v vᵀ`, so three of four directions are spanned.
1633        let v = [1.0, 2.0, -3.0, 0.5];
1634        let n = 4;
1635        let mut q = vec![0.0; n * n];
1636        for i in 0..n {
1637            for j in 0..n {
1638                q[i * n + j] = v[i] * v[j];
1639            }
1640        }
1641        for e in [-8i32, -4, 0, 4, 8] {
1642            let c: Vec<f64> = (0..n).map(|i| 10f64.powi(e * (i as i32 - 1))).collect();
1643            let mut scaled = vec![0.0; n * n];
1644            for i in 0..n {
1645                for j in 0..n {
1646                    scaled[i * n + j] = c[i] * q[i * n + j] * c[j];
1647                }
1648            }
1649            assert_eq!(
1650                psd_outer_factor(scaled, n).len(),
1651                1,
1652                "rank-1 Q stays rank 1 under diag(10^({e}·(i−1)))"
1653            );
1654        }
1655    }
1656
1657    /// A pivot the downdate has *not* spent must survive even when a spent one
1658    /// sorts above it. This is the case that made the fix more than a change of
1659    /// threshold: `√2 · √2 ≠ 2` in binary, so a factored-out column of size 2
1660    /// carries `4.4e-16` of roundoff on its diagonal afterwards, which is
1661    /// larger than a genuine `1e-20` eigenvalue sitting untouched on another
1662    /// column. Complete pivoting picks the roundoff first. With an absolute
1663    /// threshold that did not matter — both failed the same cut. With a
1664    /// relative one they disagree, so the loop has to *settle* the failing
1665    /// pivot and keep looking rather than stop at the first failure.
1666    #[test]
1667    fn a_live_pivot_below_a_spent_ones_roundoff_is_still_found() {
1668        let n = 2;
1669        // diag(2, 1e-20), the smaller entry twenty orders down.
1670        let rows = psd_outer_factor(vec![2.0, 0.0, 0.0, 1e-20], n);
1671        assert_eq!(
1672            rows.len(),
1673            2,
1674            "both diagonal entries are eigenvalues; got {rows:?}"
1675        );
1676        assert!(
1677            (rows[1][1] - 1e-10).abs() < 1e-22,
1678            "the surviving row is √1e-20, got {}",
1679            rows[1][1]
1680        );
1681    }
1682
1683    /// gh #1056: the rotated cone's two leading rows are `h₀ = −b/κ + κ` and
1684    /// `h₁ = −b/κ − κ`, and their whole job is `h₀² − h₁² = −4b`. With the
1685    /// historical `κ = 1`, at `b = −1e20` both round to `1e20` and the
1686    /// difference is `0` — the declared cone is empty. Balanced, it is exact,
1687    /// and `κ` is a power of two (so `a/κ` is exact) and `1` for `|b| ≤ 1`.
1688    #[test]
1689    fn the_rotated_cone_keeps_its_constant_at_any_magnitude() {
1690        for b in [-0.5, -1.0, 1.0, -4.0, -1e6, -1e20, -1e100, 3e40] {
1691            let k = super::rotated_cone_balance(b);
1692            assert!(k > 0.0 && k.log2().fract() == 0.0, "b = {b:e}: κ = {k}");
1693            if b.abs() <= 1.0 {
1694                assert_eq!(k, 1.0, "b = {b:e}");
1695            }
1696            let (h0, h1) = (-b / k + k, -b / k - k);
1697            let got = (h0 - h1) * (h0 + h1);
1698            assert!(
1699                ((got + 4.0 * b) / (4.0 * b)).abs() < 1e-12,
1700                "b = {b:e}, κ = {k}: h₀² − h₁² = {got:e}, want {:e}",
1701                -4.0 * b
1702            );
1703        }
1704        // What κ = 1 did.
1705        assert_eq!(1.0 - -1e20, -(1.0 + -1e20));
1706    }
1707
1708    /// gh #1041: `centre_soc` must split `a` into `Fᵀw + a_⊥` exactly, on
1709    /// both of its branches — the diagonal closed form and the normal
1710    /// equations — and leave `a_⊥` zero where `a` is in range.
1711    #[test]
1712    fn centre_soc_splits_a_into_range_and_complement() {
1713        let check = |f_rows: &[Vec<(usize, f64)>], a: &[(usize, f64)], n: usize| {
1714            let (a_perp, w) = centre_soc(f_rows, a);
1715            let mut back = vec![0.0; n];
1716            for (f, wk) in f_rows.iter().zip(&w) {
1717                for &(v, c) in f {
1718                    back[v] += c * wk;
1719                }
1720            }
1721            for &(v, c) in &a_perp {
1722                back[v] += c;
1723            }
1724            let mut want = vec![0.0; n];
1725            for &(v, c) in a {
1726                want[v] += c;
1727            }
1728            for i in 0..n {
1729                assert!(
1730                    (back[i] - want[i]).abs() <= 1e-9 * want[i].abs().max(1.0),
1731                    "Fᵀw + a_⊥ ≠ a at {i}: {back:?} vs {want:?}"
1732                );
1733            }
1734            a_perp
1735        };
1736        // Diagonal: (x0 − 1000)² + (x1 − 5)² with a linear term in x2.
1737        let s2 = std::f64::consts::SQRT_2;
1738        let a_perp = check(
1739            &[vec![(0, s2)], vec![(1, s2)]],
1740            &[(0, -2000.0), (1, -10.0), (2, 3.0)],
1741            3,
1742        );
1743        assert_eq!(a_perp, vec![(2, 3.0)], "in-range part must leave exactly");
1744        // Coupled: Q = [[8, −4], [−4, 8]] (from (x+y)² + 3(x−y)², doubled),
1745        // a = Q·(1000, 1000) − e₂ so the residual is the x2 term only.
1746        let f = psd_outer_factor(vec![8.0, -4.0, -4.0, 8.0], 2)
1747            .into_iter()
1748            .map(|r| {
1749                r.into_iter()
1750                    .enumerate()
1751                    .filter(|&(_, v)| v != 0.0)
1752                    .collect()
1753            })
1754            .collect::<Vec<Vec<(usize, f64)>>>();
1755        let a_perp = check(&f, &[(0, -4000.0), (1, -4000.0), (2, -1.0)], 3);
1756        assert_eq!(a_perp, vec![(2, -1.0)], "roundoff in range must be dropped");
1757    }
1758
1759    /// The fallback branch of `centre_soc`: factor rows that are linearly
1760    /// dependent make `FFᵀ` singular, and the block must come back exactly
1761    /// uncentred (`a_⊥ = a`, `w = 0`). Before the pivot test was relative,
1762    /// this case's second pivot came out at roundoff *above* zero and the
1763    /// solve returned `w = (48, −2048)` from a pivot of pure noise.
1764    #[test]
1765    fn centre_soc_falls_back_to_uncentred_on_a_singular_gram() {
1766        let row = vec![(0, 1.0), (1, 1.0)];
1767        let a = [(0, -2000.0), (1, -2000.0)];
1768        let (a_perp, w) = centre_soc(&[row.clone(), row], &a);
1769        assert_eq!(a_perp, a.to_vec());
1770        assert_eq!(w, vec![0.0, 0.0]);
1771    }
1772
1773    /// min (x0)^2 + (x1)^2 s.t. x0 + x1 = 2, no var bounds → (1,1), f*=2.
1774    #[test]
1775    fn extract_and_solve_equality_qp() {
1776        let prob = NlProblem {
1777            src: None,
1778            cse_bodies: Vec::new(),
1779            n: 2,
1780            m: 1,
1781            num_obj: 1,
1782            minimize: true,
1783            obj_nonlinear: NlBody::Tree(Expr::Binary(
1784                BinOp::Add,
1785                Box::new(pow2(0)),
1786                Box::new(pow2(1)),
1787            )),
1788            obj_linear: vec![],
1789            obj_constant: 0.0,
1790            con_nonlinear: vec![NlBody::Tree(Expr::Const(0.0))],
1791            con_linear: vec![vec![(0, 1.0), (1, 1.0)]],
1792            x_l: vec![-2e19, -2e19],
1793            x_u: vec![2e19, 2e19],
1794            g_l: vec![2.0],
1795            g_u: vec![2.0],
1796            x0: vec![0.0, 0.0],
1797            lambda0: vec![0.0],
1798            suffixes: Default::default(),
1799            imported_funcs: Vec::new(),
1800            ampl_options: Vec::new(),
1801            nl_counts: None,
1802            n_discrete: None,
1803            var_names: Vec::new(),
1804            con_names: Vec::new(),
1805        };
1806        let (qp, con_map, obj_const) =
1807            extract_qp_with_map(&prob, BoundRelax::NONE).expect("extract");
1808        // No constant anywhere in this objective.
1809        assert_eq!(obj_const, 0.0);
1810        // P = 2I → two diagonal entries.
1811        assert_eq!(qp.p_lower.len(), 2);
1812        assert_eq!(qp.m_eq(), 1);
1813        assert_eq!(qp.m_ineq(), 0);
1814
1815        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
1816        assert_eq!(sol.status, QpStatus::Optimal);
1817        assert!((sol.x[0] - 1.0).abs() < 1e-6, "x0={}", sol.x[0]);
1818        assert!((sol.x[1] - 1.0).abs() < 1e-6, "x1={}", sol.x[1]);
1819        assert!((sol.obj - 2.0).abs() < 1e-6, "obj={}", sol.obj);
1820
1821        // KKT for the equality: ∇f + y·∇g = 0 → 2x_i + y = 0 at x=1 → y=−2.
1822        let lambda = recover_duals(&prob, &con_map, &sol.y, &sol.z);
1823        assert_eq!(lambda.len(), 1);
1824        assert!(
1825            (lambda[0] - (-2.0)).abs() < 1e-5,
1826            "equality dual={}",
1827            lambda[0]
1828        );
1829    }
1830
1831    /// Regression for the dropped-linear-term bug: the objective `(x0-3)²`
1832    /// lives entirely in the nonlinear tree, so its linear part (`−6·x0`)
1833    /// must be folded into `c`. Without it the solve minimizes `x0²`
1834    /// (optimum 0) instead of `(x0-3)²` (optimum 3).
1835    #[test]
1836    fn extract_keeps_linear_term_from_nonlinear_tree() {
1837        // (x0 - 3)^2 = x0^2 - 6 x0 + 9, all in obj_nonlinear.
1838        let obj = Expr::Binary(
1839            BinOp::Pow,
1840            Box::new(Expr::Binary(
1841                BinOp::Sub,
1842                Box::new(Expr::Var(0)),
1843                Box::new(Expr::Const(3.0)),
1844            )),
1845            Box::new(Expr::Const(2.0)),
1846        );
1847        let prob = NlProblem {
1848            src: None,
1849            cse_bodies: Vec::new(),
1850            n: 1,
1851            m: 0,
1852            num_obj: 1,
1853            minimize: true,
1854            obj_nonlinear: NlBody::Tree(obj),
1855            obj_linear: vec![],
1856            obj_constant: 0.0,
1857            con_nonlinear: vec![],
1858            con_linear: vec![],
1859            x_l: vec![-2e19],
1860            x_u: vec![2e19],
1861            g_l: vec![],
1862            g_u: vec![],
1863            x0: vec![0.0],
1864            lambda0: vec![],
1865            suffixes: Default::default(),
1866            imported_funcs: Vec::new(),
1867            ampl_options: Vec::new(),
1868            nl_counts: None,
1869            n_discrete: None,
1870            var_names: Vec::new(),
1871            con_names: Vec::new(),
1872        };
1873        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
1874        assert_eq!(qp.c.len(), 1);
1875        assert!(
1876            (qp.c[0] - (-6.0)).abs() < 1e-12,
1877            "c[0]={} — linear term from the nonlinear tree was dropped",
1878            qp.c[0]
1879        );
1880        // P = 2 (one diagonal entry).
1881        assert_eq!(qp.p_lower.len(), 1);
1882
1883        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
1884        assert_eq!(sol.status, QpStatus::Optimal);
1885        assert!(
1886            (sol.x[0] - 3.0).abs() < 1e-6,
1887            "x0={} (expected 3)",
1888            sol.x[0]
1889        );
1890    }
1891
1892    /// Inequality dual sign/magnitude. min x0² s.t. x0 ≥ 1 (a one-sided
1893    /// inequality g_l=1, g_u=+inf). Optimum x0=1, active. The expected
1894    /// dual −2.0 is the value POUNCE's *NLP* path writes for this exact
1895    /// problem (verified by running `solver_selection=nlp` on the same
1896    /// `.nl`); recover_duals must match that reference convention.
1897    #[test]
1898    fn inequality_dual_recovered() {
1899        let prob = NlProblem {
1900            src: None,
1901            cse_bodies: Vec::new(),
1902            n: 1,
1903            m: 1,
1904            num_obj: 1,
1905            minimize: true,
1906            obj_nonlinear: NlBody::Tree(pow2(0)),
1907            obj_linear: vec![],
1908            obj_constant: 0.0,
1909            con_nonlinear: vec![NlBody::Tree(Expr::Const(0.0))],
1910            con_linear: vec![vec![(0, 1.0)]], // g(x) = x0
1911            x_l: vec![-2e19],
1912            x_u: vec![2e19],
1913            g_l: vec![1.0], // x0 ≥ 1
1914            g_u: vec![2e19],
1915            x0: vec![0.0],
1916            lambda0: vec![0.0],
1917            suffixes: Default::default(),
1918            imported_funcs: Vec::new(),
1919            ampl_options: Vec::new(),
1920            nl_counts: None,
1921            n_discrete: None,
1922            var_names: Vec::new(),
1923            con_names: Vec::new(),
1924        };
1925        let (qp, con_map, obj_const) =
1926            extract_qp_with_map(&prob, BoundRelax::NONE).expect("extract");
1927        // This model puts its constant in the `obj_constant` field, not the
1928        // nonlinear tree, so the tree constant is 0 here.
1929        assert_eq!(obj_const, 0.0);
1930        // One inequality row (the lower bound row −x0 ≤ −1); no upper.
1931        assert_eq!(qp.m_ineq(), 1);
1932        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
1933        assert_eq!(sol.status, QpStatus::Optimal);
1934        assert!((sol.x[0] - 1.0).abs() < 1e-6, "x0={}", sol.x[0]);
1935        let lambda = recover_duals(&prob, &con_map, &sol.y, &sol.z);
1936        assert!((lambda[0] - (-2.0)).abs() < 1e-5, "ineq dual={}", lambda[0]);
1937    }
1938
1939    /// Regression (M11): a *constraint* whose linear and constant
1940    /// terms are folded into the nonlinear tree (not the `con_linear`
1941    /// section) must still reach `A`/`G`. AMPL/Pyomo emit this shape for
1942    /// rows the classifier admits as degree-≤1 (cancelled quadratics,
1943    /// defined variables): the whole `x0 − 3` lives in `con_nonlinear`
1944    /// and `con_linear[0]` is empty.
1945    ///
1946    ///     min x0   s.t.   x0 − 3 ≥ 0     (body in the nonlinear tree)
1947    ///
1948    /// True optimum: x0 = 3. The QP extractor used to build `A`/`G` from
1949    /// `con_linear` only — dropping the folded `+x0` *and* the `−3`
1950    /// shift, leaving a vacuous `0 ≤ 0` row, so `min x0` came out
1951    /// unbounded (or otherwise wrong) on the convex path.
1952    #[test]
1953    fn constraint_linear_terms_folded_in_tree_are_recovered() {
1954        // con body = x0 − 3, entirely in the nonlinear tree.
1955        let con = Expr::Binary(
1956            BinOp::Sub,
1957            Box::new(Expr::Var(0)),
1958            Box::new(Expr::Const(3.0)),
1959        );
1960        let prob = NlProblem {
1961            src: None,
1962            cse_bodies: Vec::new(),
1963            n: 1,
1964            m: 1,
1965            num_obj: 1,
1966            minimize: true,
1967            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
1968            obj_linear: vec![(0, 1.0)],
1969            obj_constant: 0.0,
1970            con_nonlinear: vec![NlBody::Tree(con)],
1971            con_linear: vec![vec![]], // the `+x0` lives in the TREE
1972            x_l: vec![-2e19],
1973            x_u: vec![2e19],
1974            g_l: vec![0.0], // x0 − 3 ≥ 0
1975            g_u: vec![2e19],
1976            x0: vec![0.0],
1977            lambda0: vec![0.0],
1978            suffixes: Default::default(),
1979            imported_funcs: Vec::new(),
1980            ampl_options: Vec::new(),
1981            nl_counts: None,
1982            n_discrete: None,
1983            var_names: Vec::new(),
1984            con_names: Vec::new(),
1985        };
1986        let (qp, con_map, _obj_const) =
1987            extract_qp_with_map(&prob, BoundRelax::NONE).expect("extract");
1988        // One inequality row: −x0 ≤ −3 (the lower bound, constant-shifted).
1989        assert_eq!(qp.m_ineq(), 1);
1990        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
1991        assert_eq!(sol.status, QpStatus::Optimal);
1992        assert!((sol.x[0] - 3.0).abs() < 1e-5, "x0={}", sol.x[0]);
1993        // Dual is recoverable and finite (the row carries a real coef now).
1994        let lambda = recover_duals(&prob, &con_map, &sol.y, &sol.z);
1995        assert_eq!(lambda.len(), 1);
1996        assert!(lambda[0].is_finite(), "dual={}", lambda[0]);
1997    }
1998
1999    /// Regression: a constant folded into the *nonlinear objective tree*
2000    /// (not the `obj_constant` field) must still reach the reported
2001    /// objective. This is the real `.nl` shape AMPL/Pyomo emit for
2002    /// `min (x0-3)^2` — the whole `x0^2 - 6 x0 + 9` lives in the nonlinear
2003    /// tree and `obj_constant == 0`. The convex path used to drop the `+9`
2004    /// and report an objective 9 too small (cf. HS35 in the benchmark
2005    /// comparison). The minimizer is x0 = 1 (upper bound binds), where the
2006    /// true objective is (1-3)^2 = 4.
2007    #[test]
2008    fn tree_embedded_objective_constant_is_recovered() {
2009        // (x0 - 3)^2 as a single nonlinear tree: Pow(Sub(x0, 3), 2).
2010        let obj = Expr::Binary(
2011            BinOp::Pow,
2012            Box::new(Expr::Binary(
2013                BinOp::Sub,
2014                Box::new(Expr::Var(0)),
2015                Box::new(Expr::Const(3.0)),
2016            )),
2017            Box::new(Expr::Const(2.0)),
2018        );
2019        let prob = NlProblem {
2020            src: None,
2021            cse_bodies: Vec::new(),
2022            n: 1,
2023            m: 0,
2024            num_obj: 1,
2025            minimize: true,
2026            obj_nonlinear: NlBody::Tree(obj),
2027            obj_linear: vec![],
2028            obj_constant: 0.0, // the +9 is in the TREE, not here
2029            con_nonlinear: vec![],
2030            con_linear: vec![],
2031            x_l: vec![0.0],
2032            x_u: vec![1.0],
2033            g_l: vec![],
2034            g_u: vec![],
2035            x0: vec![0.0],
2036            lambda0: vec![],
2037            suffixes: Default::default(),
2038            imported_funcs: Vec::new(),
2039            ampl_options: Vec::new(),
2040            nl_counts: None,
2041            n_discrete: None,
2042            var_names: Vec::new(),
2043            con_names: Vec::new(),
2044        };
2045        let (qp, _con_map, obj_const) =
2046            extract_qp_with_map(&prob, BoundRelax::NONE).expect("extract");
2047        // The degree-0 term of (x0-3)^2 is +9, recovered from the tree.
2048        assert!((obj_const - 9.0).abs() < 1e-12, "tree constant={obj_const}");
2049        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
2050        assert_eq!(sol.status, QpStatus::Optimal);
2051        assert!((sol.x[0] - 1.0).abs() < 1e-6, "x0={}", sol.x[0]);
2052        // Reported objective = (½xᵀPx + cᵀx) + obj_const must equal the true
2053        // (1-3)^2 = 4, not the constant-dropped −5.
2054        let reported = sol.obj + obj_const;
2055        assert!((reported - 4.0).abs() < 1e-5, "reported obj={reported}");
2056    }
2057
2058    /// Bound-constrained: min (x0-3)^2 = x0^2 - 6 x0 + 9, 0 ≤ x0 ≤ 1.
2059    /// Optimum x0 = 1 (upper bound binds). Here the constant 9 is carried
2060    /// in the `obj_constant` field (not the tree), so the extracted tree
2061    /// constant is 0 (asserted inside).
2062    #[test]
2063    fn extract_and_solve_bounded_qp() {
2064        let prob = NlProblem {
2065            src: None,
2066            cse_bodies: Vec::new(),
2067            n: 1,
2068            m: 0,
2069            num_obj: 1,
2070            minimize: true,
2071            obj_nonlinear: NlBody::Tree(pow2(0)),
2072            obj_linear: vec![(0, -6.0)],
2073            obj_constant: 9.0,
2074            con_nonlinear: vec![],
2075            con_linear: vec![],
2076            x_l: vec![0.0],
2077            x_u: vec![1.0],
2078            g_l: vec![],
2079            g_u: vec![],
2080            x0: vec![0.0],
2081            lambda0: vec![],
2082            suffixes: Default::default(),
2083            imported_funcs: Vec::new(),
2084            ampl_options: Vec::new(),
2085            nl_counts: None,
2086            n_discrete: None,
2087            var_names: Vec::new(),
2088            con_names: Vec::new(),
2089        };
2090        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
2091        // The bounds are the box, not `G` rows.
2092        assert_eq!(qp.m_ineq(), 0);
2093        assert_eq!((qp.lb[0], qp.ub[0]), (0.0, 1.0));
2094        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
2095        assert_eq!(sol.status, QpStatus::Optimal);
2096        assert!((sol.x[0] - 1.0).abs() < 1e-6, "x0={}", sol.x[0]);
2097    }
2098
2099    /// LP: min −x0 − x1, 0 ≤ x ≤ 1 → (1,1).
2100    #[test]
2101    fn extract_and_solve_lp() {
2102        let prob = NlProblem {
2103            src: None,
2104            cse_bodies: Vec::new(),
2105            n: 2,
2106            m: 0,
2107            num_obj: 1,
2108            minimize: true,
2109            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2110            obj_linear: vec![(0, -1.0), (1, -1.0)],
2111            obj_constant: 0.0,
2112            con_nonlinear: vec![],
2113            con_linear: vec![],
2114            x_l: vec![0.0, 0.0],
2115            x_u: vec![1.0, 1.0],
2116            g_l: vec![],
2117            g_u: vec![],
2118            x0: vec![0.0, 0.0],
2119            lambda0: vec![],
2120            suffixes: Default::default(),
2121            imported_funcs: Vec::new(),
2122            ampl_options: Vec::new(),
2123            nl_counts: None,
2124            n_discrete: None,
2125            var_names: Vec::new(),
2126            con_names: Vec::new(),
2127        };
2128        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
2129        assert!(qp.p_lower.is_empty(), "LP has no Hessian");
2130        assert_eq!(qp.m_ineq(), 0, "bounds are the box, not `G` rows");
2131        assert_eq!(qp.lb, vec![0.0, 0.0]);
2132        assert_eq!(qp.ub, vec![1.0, 1.0]);
2133        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
2134        assert_eq!(sol.status, QpStatus::Optimal);
2135        assert!((sol.x[0] - 1.0).abs() < 1e-6);
2136        assert!((sol.x[1] - 1.0).abs() < 1e-6);
2137    }
2138
2139    /// maximize x0 s.t. 0 ≤ x0 ≤ 5 → x0 = 5. Tests sign flip on a
2140    /// maximize objective.
2141    #[test]
2142    fn extract_maximize_negates() {
2143        let prob = NlProblem {
2144            src: None,
2145            cse_bodies: Vec::new(),
2146            n: 1,
2147            m: 0,
2148            num_obj: 1,
2149            minimize: false,
2150            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2151            obj_linear: vec![(0, 1.0)],
2152            obj_constant: 0.0,
2153            con_nonlinear: vec![],
2154            con_linear: vec![],
2155            x_l: vec![0.0],
2156            x_u: vec![5.0],
2157            g_l: vec![],
2158            g_u: vec![],
2159            x0: vec![0.0],
2160            lambda0: vec![],
2161            suffixes: Default::default(),
2162            imported_funcs: Vec::new(),
2163            ampl_options: Vec::new(),
2164            nl_counts: None,
2165            n_discrete: None,
2166            var_names: Vec::new(),
2167            con_names: Vec::new(),
2168        };
2169        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
2170        // minimize −x0.
2171        assert_eq!(qp.c[0], -1.0);
2172        let sol = solve_qp_ipm(&qp, &QpOptions::default(), backend);
2173        assert_eq!(sol.status, QpStatus::Optimal);
2174        assert!((sol.x[0] - 5.0).abs() < 1e-6, "x0={}", sol.x[0]);
2175    }
2176
2177    /// **gh #401.** A real bound past the *opposite* absent-bound sentinel is
2178    /// an ordinary bound, and must survive into `G`.
2179    ///
2180    /// `is_finite_bound` was `|v| < 1e19`, a symmetric magnitude test. An upper
2181    /// bound of `-5e20` failed it and the row `x_0 <= -5e20` never entered `G`,
2182    /// so the QP was solved over a strictly larger box — `min x_0` subject to
2183    /// nothing, which the IPM answers `Optimal` at a point the model excludes.
2184    #[test]
2185    fn variable_bound_past_the_opposite_sentinel_is_kept() {
2186        let prob = NlProblem {
2187            src: None,
2188            cse_bodies: Vec::new(),
2189            n: 1,
2190            m: 0,
2191            num_obj: 1,
2192            minimize: false, // maximize x0, so the -5e20 upper bound binds
2193            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2194            obj_linear: vec![(0, 1.0)],
2195            obj_constant: 0.0,
2196            con_nonlinear: vec![],
2197            con_linear: vec![],
2198            // No lower bound (`-1e21` is past the lower sentinel, so absent);
2199            // a real upper bound of `-5e20`, which is *not*.
2200            x_l: vec![-1e21],
2201            x_u: vec![-5e20],
2202            g_l: vec![],
2203            g_u: vec![],
2204            x0: vec![-7e20],
2205            lambda0: vec![],
2206            suffixes: Default::default(),
2207            imported_funcs: Vec::new(),
2208            ampl_options: Vec::new(),
2209            nl_counts: None,
2210            n_discrete: None,
2211            var_names: Vec::new(),
2212            con_names: Vec::new(),
2213        };
2214        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
2215        assert_eq!(
2216            qp.ub[0], -5e20,
2217            "`x0 <= -5e20` is a real bound and must reach the box; the \
2218             symmetric |v| < 1e19 test dropped it, leaving an \
2219             unbounded-above box the model does not declare"
2220        );
2221    }
2222
2223    /// **gh #401.** A row with equal bounds past the sentinel used to vanish
2224    /// from the problem *entirely* — contributing nothing to `A` and nothing
2225    /// to `G`.
2226    ///
2227    /// Directionally, `g_l = g_u = -5e20` is not an equality at all: the lower
2228    /// bound is absent (it is past `-1e19`) and the upper bound is real, so the
2229    /// row is the one-sided `x0 + x1 <= -5e20`. The old code got there by a
2230    /// different route and lost it: `lo == hi && is_finite_bound(lo)` failed, so
2231    /// the row fell into the inequality branch — where `is_finite_bound(hi)` and
2232    /// `is_finite_bound(lo)` were false too, leaving `upper` and `lower` both
2233    /// `None`. Silently deleted.
2234    ///
2235    /// (Note there is no such thing as an equality row outside `±1e19` under
2236    /// this convention: an equality needs both bounds present, and the two
2237    /// presence tests only overlap inside the band.)
2238    #[test]
2239    fn a_row_with_equal_bounds_past_the_sentinel_does_not_vanish() {
2240        let prob = NlProblem {
2241            src: None,
2242            cse_bodies: Vec::new(),
2243            n: 2,
2244            m: 1,
2245            num_obj: 1,
2246            minimize: true,
2247            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2248            obj_linear: vec![(0, 1.0)],
2249            obj_constant: 0.0,
2250            con_nonlinear: vec![NlBody::Tree(Expr::Const(0.0))],
2251            con_linear: vec![vec![(0, 1.0), (1, 1.0)]],
2252            x_l: vec![-2e19, -2e19],
2253            x_u: vec![2e19, 2e19],
2254            g_l: vec![-5e20],
2255            g_u: vec![-5e20],
2256            x0: vec![0.0, 0.0],
2257            lambda0: vec![0.0],
2258            suffixes: Default::default(),
2259            imported_funcs: Vec::new(),
2260            ampl_options: Vec::new(),
2261            nl_counts: None,
2262            n_discrete: None,
2263            var_names: Vec::new(),
2264            con_names: Vec::new(),
2265        };
2266        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
2267        assert_eq!(
2268            qp.m_eq(),
2269            0,
2270            "the lower bound is absent, so this is no equality"
2271        );
2272        assert_eq!(
2273            qp.m_ineq(),
2274            1,
2275            "`x0 + x1 <= -5e20` is a real constraint and must reach G; it used \
2276             to disappear from the problem entirely"
2277        );
2278        assert_eq!(qp.h[0], -5e20);
2279    }
2280
2281    /// **gh #401.** The box is built with the *directional* presence test, so
2282    /// a bound past the opposite sentinel survives while a genuinely absent
2283    /// one becomes `∓∞`. Pins the case only the directional reading admits.
2284    ///
2285    /// This used to assert instead that `recover_bound_mults` walked the same
2286    /// `G`-row layout the builder emitted — a real hazard when the two agreed
2287    /// only by construction, and one that no longer exists: bounds are the
2288    /// box, and their multipliers come back in `z_lb`/`z_ub` with no layout
2289    /// to keep in step.
2290    #[test]
2291    fn the_box_is_built_with_the_directional_bound_test() {
2292        let prob = NlProblem {
2293            src: None,
2294            cse_bodies: Vec::new(),
2295            n: 2,
2296            m: 0,
2297            num_obj: 1,
2298            minimize: true,
2299            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2300            obj_linear: vec![(0, 1.0), (1, 1.0)],
2301            obj_constant: 0.0,
2302            con_nonlinear: vec![],
2303            con_linear: vec![],
2304            // x0: upper bound past the *lower* sentinel, no lower bound.
2305            // x1: an ordinary two-sided box.
2306            x_l: vec![-2e19, 0.0],
2307            x_u: vec![-5e20, 1.0],
2308            g_l: vec![],
2309            g_u: vec![],
2310            x0: vec![-6e20, 0.5],
2311            lambda0: vec![],
2312            suffixes: Default::default(),
2313            imported_funcs: Vec::new(),
2314            ampl_options: Vec::new(),
2315            nl_counts: None,
2316            n_discrete: None,
2317            var_names: Vec::new(),
2318            con_names: Vec::new(),
2319        };
2320        let qp = extract_qp(&prob, BoundRelax::NONE).expect("extract");
2321        assert_eq!(qp.m_ineq(), 0, "bounds are the box, not `G` rows");
2322        // x0: no lower bound; a real upper bound past the *lower* sentinel.
2323        assert_eq!(qp.lb[0], f64::NEG_INFINITY);
2324        assert_eq!(qp.ub[0], -5e20);
2325        // x1: an ordinary two-sided box, carried through unchanged.
2326        assert_eq!(qp.lb[1], 0.0);
2327        assert_eq!(qp.ub[1], 1.0);
2328    }
2329
2330    /// The bound multipliers a solve produces are handed back per variable,
2331    /// not decoded from a row layout. A short `z_lb`/`z_ub` (a driver that
2332    /// returned early without them) reads as "no bound active" rather than
2333    /// panicking on the index.
2334    #[test]
2335    fn bound_multipliers_come_back_per_variable() {
2336        let prob = NlProblem {
2337            src: None,
2338            cse_bodies: Vec::new(),
2339            n: 2,
2340            m: 0,
2341            num_obj: 1,
2342            minimize: true,
2343            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2344            obj_linear: vec![(0, 1.0), (1, 1.0)],
2345            obj_constant: 0.0,
2346            con_nonlinear: vec![],
2347            con_linear: vec![],
2348            x_l: vec![0.0, 0.0],
2349            x_u: vec![1.0, 1.0],
2350            g_l: vec![],
2351            g_u: vec![],
2352            x0: vec![0.5, 0.5],
2353            lambda0: vec![],
2354            suffixes: Default::default(),
2355            imported_funcs: Vec::new(),
2356            ampl_options: Vec::new(),
2357            nl_counts: None,
2358            n_discrete: None,
2359            var_names: Vec::new(),
2360            con_names: Vec::new(),
2361        };
2362        let sol = QpSolution {
2363            status: pounce_convex::QpStatus::Optimal,
2364            x: vec![0.0, 1.0],
2365            y: vec![],
2366            z: vec![],
2367            z_lb: vec![7.0, 0.0],
2368            z_ub: vec![0.0, 9.0],
2369            obj: 0.0,
2370            iters: 0,
2371            iterates: Vec::new(),
2372        };
2373        let (z_lb, z_ub) = recover_bound_mults(&prob, &sol);
2374        assert_eq!(z_lb, vec![7.0, 0.0]);
2375        assert_eq!(z_ub, vec![0.0, 9.0]);
2376
2377        let empty = QpSolution {
2378            z_lb: Vec::new(),
2379            z_ub: Vec::new(),
2380            ..sol
2381        };
2382        let (z_lb, z_ub) = recover_bound_mults(&prob, &empty);
2383        assert_eq!(z_lb, vec![0.0, 0.0]);
2384        assert_eq!(z_ub, vec![0.0, 0.0]);
2385    }
2386
2387    /// gh #744/#745: `bound_relax_factor` reaches the extracted model.
2388    ///
2389    /// One inequality row (`x0 + x1 >= 2`), one two-sided range row, one
2390    /// equality row, a bounded variable, a fixed variable, and a free
2391    /// variable — so every case the widening treats differently is present.
2392    fn relax_fixture() -> NlProblem {
2393        NlProblem {
2394            src: None,
2395            cse_bodies: Vec::new(),
2396            n: 3,
2397            m: 3,
2398            num_obj: 1,
2399            minimize: true,
2400            obj_nonlinear: NlBody::Tree(Expr::Const(0.0)),
2401            obj_linear: vec![(0, 1.0)],
2402            obj_constant: 0.0,
2403            con_nonlinear: vec![
2404                NlBody::Tree(Expr::Const(0.0)),
2405                NlBody::Tree(Expr::Const(0.0)),
2406                NlBody::Tree(Expr::Const(0.0)),
2407            ],
2408            con_linear: vec![
2409                vec![(0, 1.0), (1, 1.0)],
2410                vec![(1, 1.0), (2, 1.0)],
2411                vec![(0, 1.0), (2, 1.0)],
2412            ],
2413            // x0 bounded above and below, x1 fixed, x2 free.
2414            x_l: vec![-4.0, 5.0, -2e19],
2415            x_u: vec![8.0, 5.0, 2e19],
2416            // row 0: >= 2 (lower only); row 1: -3 <= . <= 6 (range);
2417            // row 2: == 7 (equality).
2418            g_l: vec![2.0, -3.0, 7.0],
2419            g_u: vec![2e19, 6.0, 7.0],
2420            x0: vec![0.0, 0.0, 0.0],
2421            lambda0: vec![0.0, 0.0, 0.0],
2422            suffixes: Default::default(),
2423            imported_funcs: Vec::new(),
2424            ampl_options: Vec::new(),
2425            nl_counts: None,
2426            n_discrete: None,
2427            var_names: Vec::new(),
2428            con_names: Vec::new(),
2429        }
2430    }
2431
2432    #[test]
2433    fn bound_relax_none_leaves_the_declared_model_alone() {
2434        let prob = relax_fixture();
2435        let (qp, _, _) = extract_qp_with_map(&prob, BoundRelax::NONE).expect("extract");
2436        assert_eq!(qp.lb, vec![-4.0, 5.0, f64::NEG_INFINITY]);
2437        assert_eq!(qp.ub, vec![8.0, 5.0, f64::INFINITY]);
2438        assert_eq!(qp.b, vec![7.0]);
2439        // Rows, in emission order: row0's `>= 2` as `-x0-x1 <= -2`;
2440        // row1's `<= 6` then its `>= -3` as `<= 3`.
2441        assert_eq!(qp.h, vec![-2.0, 6.0, 3.0]);
2442    }
2443
2444    #[test]
2445    fn bound_relax_widens_inequality_rows_and_the_free_box_only() {
2446        let prob = relax_fixture();
2447        let relax = BoundRelax {
2448            factor: 1e-8,
2449            cap: 1e-4,
2450        };
2451        let (qp, _, _) = extract_qp_with_map(&prob, relax).expect("extract");
2452
2453        // Variable box: upstream's absolute formula `min(f*max(|b|,1), cap)`.
2454        // x0's bounds widen outward; x1 is *fixed* and must not move (upstream
2455        // removes fixed variables before `relax_bounds` runs); x2 is free.
2456        assert!((qp.lb[0] - (-4.0 - 4e-8)).abs() < 1e-18);
2457        assert!((qp.ub[0] - (8.0 + 8e-8)).abs() < 1e-18);
2458        assert_eq!(qp.lb[1], 5.0);
2459        assert_eq!(qp.ub[1], 5.0);
2460        assert_eq!(qp.lb[2], f64::NEG_INFINITY);
2461        assert_eq!(qp.ub[2], f64::INFINITY);
2462
2463        // Equality rows are never relaxed — upstream keeps them in `c(x) = 0`,
2464        // which `relax_bounds` does not touch.
2465        assert_eq!(qp.b, vec![7.0]);
2466
2467        // Inequality rows use the scale-relative width `min(f, cap)*|b|`.
2468        // `x0+x1 >= 2` → `-x0-x1 <= -(2 - 2e-8)`.
2469        assert!((qp.h[0] - -(2.0 - 2e-8)).abs() < 1e-18);
2470        // `. <= 6` → `<= 6 + 6e-8`; `. >= -3` → `<= 3 + 3e-8`.
2471        assert!((qp.h[1] - (6.0 + 6e-8)).abs() < 1e-18);
2472        assert!((qp.h[2] - (3.0 + 3e-8)).abs() < 1e-18);
2473    }
2474
2475    #[test]
2476    fn bound_relax_caps_the_widening_and_floors_a_zero_row_bound() {
2477        let mut prob = relax_fixture();
2478        // A huge row bound: the relative width `min(f, cap)*|b|` would be
2479        // enormous without the `min` against `cap` in the *factor*.
2480        prob.g_l[0] = 0.0; // declared-zero bound: no scale, absolute width.
2481        let relax = BoundRelax {
2482            factor: 1e-2,
2483            cap: 1e-4,
2484        };
2485        let (qp, _, _) = extract_qp_with_map(&prob, relax).expect("extract");
2486        // Zero bound → width is `min(1e-2, 1e-4) * 1 = 1e-4`.
2487        assert!((qp.h[0] - 1e-4).abs() < 1e-18, "{}", qp.h[0]);
2488        // Variable box is capped by `cap` outright: `1e-2*max(4,1) = 4e-2`,
2489        // capped to `1e-4`.
2490        assert!((qp.lb[0] - (-4.0 - 1e-4)).abs() < 1e-18);
2491    }
2492
2493    /// An empty declared set must survive extraction empty. Relaxation runs
2494    /// *after* upstream's consistency check, and the emptiness screens on the
2495    /// convex side read the extracted `lb`/`ub` and row pairs — so widening a
2496    /// crossing narrower than the relaxation would silently make an
2497    /// inconsistent model solvable (gh #491, gh #744).
2498    #[test]
2499    fn bound_relax_does_not_close_a_crossed_box_or_a_crossed_row() {
2500        let mut prob = relax_fixture();
2501        // x0's box crossed by 1e-8, narrower than the 2*4e-8 it would widen by.
2502        prob.x_l[0] = 0.0;
2503        prob.x_u[0] = -1e-8;
2504        // Row 1 crossed by 1e-8 too: `1e-8 <= x1 + x2 <= 0`.
2505        prob.g_l[1] = 1e-8;
2506        prob.g_u[1] = 0.0;
2507        let relax = BoundRelax {
2508            factor: 1e-8,
2509            cap: 1e-4,
2510        };
2511        let (qp, _, _) = extract_qp_with_map(&prob, relax).expect("extract");
2512        assert_eq!(qp.lb[0], 0.0);
2513        assert_eq!(qp.ub[0], -1e-8);
2514        // Row 1's pair: `<= 0` and `>= 1e-8` (as `<= -1e-8`), both verbatim.
2515        assert_eq!(qp.h[1], 0.0);
2516        assert_eq!(qp.h[2], -1e-8);
2517        // The uncrossed row 0 is still widened.
2518        assert!((qp.h[0] - -(2.0 - 2e-8)).abs() < 1e-18);
2519    }
2520}