pounce-cli 0.12.0

Command-line driver for POUNCE — solves built-in TNLPs and AMPL .nl files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! gh#900: the `Variable bound violation` row reports a measurement, and the
//! POUNCE-only declared-model line is styled so it is not read past.
//!
//! Both console printers hardcoded `0.0` on that row
//! (`pounce-solve-report/src/console.rs`), so it read `0.00e+00` on every
//! solve — including the ones where the returned point genuinely sits outside
//! the box the caller wrote. It is the row a reader is told to check, and
//! upstream Ipopt is the only place the number was available.
//!
//! The fixture is the smallest model on which the answer is not a matter of
//! opinion:
//!
//! ```text
//! min 1e8*x + 0.5*x^2   s.t.  x >= 0
//! ```
//!
//! `f'(x) = 1e8 + x > 0` everywhere feasible, so `x* = 0` and `f* = 0`,
//! uniquely — the objective is strictly convex, so there is no second
//! minimum to have found instead. Widen the bound by `1e-8` and the solve
//! returns `f ≈ -1`: a value for a quantity that cannot go below zero, under
//! `EXIT: Optimal Solution Found`. The multiplier at the bound is `1e8` and
//! the shift is `δ · λ`, which is the LISWET/YAO family of
//! `benchmarks/BENCHMARK_REPORT.md` in one variable.
//!
//! Ipopt 3.14.20/MA57 on this model returns the same objective and prints
//! `9.9999090909090909e-09` on that row; POUNCE's NLP arm now prints the same
//! number to every digit the assertions below can portably pin. That is not
//! asserted exactly: the value is `δ` minus the barrier's last
//! fraction-to-boundary step, so it is trajectory-dependent and a bit-for-bit
//! pin would fail for a reason that has nothing to do with this row.
//!
//! **Which branch each case takes.** The row is computed by two independent
//! code paths — `IpoptCalculatedQuantities::curr_declared_box_violation_max`
//! on the NLP arm, `QpResiduals::bound_violation` against a
//! `BoundRelax::NONE` re-extraction on the convex one — so a test that
//! exercised one would say nothing about the other. Every case below is run
//! on both, at both settings of the widening, which is four corners: a
//! nonzero that must be reported and a zero that must not be fabricated, per
//! arm.

use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

use pounce_cli::solve_report::SolveReport;

/// `bound_relax_factor` at Ipopt's default. The convex arm no longer applies
/// it unless asked (gh#744/#745), so a test about the widening names it.
const RELAX: &str = "bound_relax_factor=1e-8";

fn pounce_exe() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_pounce"))
}

fn fixture() -> PathBuf {
    let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    p.push("tests/fixtures/bound_relax_cliff.nl");
    p
}

fn tmp_path(suffix: &str) -> PathBuf {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    let mut p = std::env::temp_dir();
    p.push(format!(
        "pounce_gh900_{}_{}_{suffix}",
        std::process::id(),
        n
    ));
    p
}

struct Run {
    stdout: String,
    report: SolveReport,
}

/// Solve the cliff fixture on `engine`, capturing both the console block and
/// the JSON report so the two can be held against each other.
fn run(engine: &str, extra: &[&str], color: bool) -> Run {
    let json_path = tmp_path("report.json");
    let sol_path = tmp_path("out.sol");
    let mut cmd = Command::new(pounce_exe());
    cmd.arg(fixture())
        .arg(&sol_path)
        .arg("--json-output")
        .arg(&json_path)
        .arg(format!("solver_selection={engine}"));
    for o in extra {
        cmd.arg(o);
    }
    if color {
        // The test harness captures stdout through a pipe, so `anstream`
        // strips the styling by default — which is the behaviour the plain
        // cases below rely on. Force it back on to see the styled bytes, and
        // clear `NO_COLOR`, which wins over the force and may be set in CI.
        cmd.env("CLICOLOR_FORCE", "1");
        cmd.env_remove("NO_COLOR");
    } else {
        cmd.env_remove("CLICOLOR_FORCE");
    }
    let out = cmd.output().expect("spawn pounce");
    let text = std::fs::read_to_string(&json_path).expect("read json report");
    let _ = std::fs::remove_file(&json_path);
    let _ = std::fs::remove_file(&sol_path);
    Run {
        stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
        report: serde_json::from_str(&text).expect("deserialize SolveReport"),
    }
}

/// The two columns of a summary row, by label.
fn row(stdout: &str, label: &str) -> (f64, f64) {
    let line = stdout
        .lines()
        .find(|l| l.starts_with(label))
        .unwrap_or_else(|| panic!("no `{label}` row in:\n{stdout}"));
    let rest = line.split_once(':').expect("labelled row").1;
    let mut it = rest.split_whitespace();
    let parse = |s: Option<&str>| {
        s.unwrap_or_else(|| panic!("missing column in `{line}`"))
            .parse::<f64>()
            .unwrap_or_else(|e| panic!("unparsable column in `{line}`: {e}"))
    };
    (parse(it.next()), parse(it.next()))
}

fn bound_violation_row(stdout: &str) -> (f64, f64) {
    row(stdout, "Variable bound violation")
}

/// One widening, `min(1e-8 * max(|0|, 1), 1e-4)`.
const DELTA: f64 = 1e-8;

// ── the widened corner: a number, where there used to be a zero ──────────────

#[test]
fn the_nlp_arm_reports_the_box_violation_it_incurred() {
    // This arm widens by default: a feasible-iterate log-barrier needs `x`
    // strictly inside its bounds, so the widening is not opt-in here.
    let r = run("nlp", &[], false);
    let (scaled, unscaled) = bound_violation_row(&r.stdout);
    assert!(
        (scaled - DELTA).abs() <= 0.01 * DELTA,
        "the point sits one widening outside the declared bound `x >= 0`, so \
         the row should read ~{DELTA:e}; got {scaled:e}. A `0.0` here is the \
         gh#900 defect."
    );
    // Variable bounds carry no scaling — POUNCE scales the objective and the
    // constraint rows only — so the one measurement is right in both columns
    // rather than one of them being a placeholder.
    assert_eq!(
        scaled, unscaled,
        "the box violation has no scaled/unscaled distinction"
    );
    // What the row is evidence *of*: an objective below a floor of zero.
    let obj = r.report.solution.objective;
    assert!(
        obj < -0.9,
        "the widened bound should buy `δ · λ = 1e-8 · 1e8 ≈ 1` of objective \
         on a problem whose true minimum is 0; got {obj:e}. Without that this \
         fixture has stopped exercising the gap."
    );
}

#[test]
fn the_convex_arm_reports_the_box_violation_it_incurred() {
    let r = run("auto", &[RELAX], false);
    assert_eq!(
        r.report.solution.engine, "cvx-qp",
        "this case exists to cover the convex printer's own path to the row; \
         if `auto` stops routing here it is covering the NLP one twice"
    );
    let (scaled, unscaled) = bound_violation_row(&r.stdout);
    assert!(
        (scaled - DELTA).abs() <= 0.01 * DELTA,
        "expected ~{DELTA:e} on the convex arm too; got {scaled:e}"
    );
    assert_eq!(scaled, unscaled);
    let obj = r.report.solution.objective;
    assert!(obj < -0.9, "expected an objective near -1; got {obj:e}");
}

// ── the unwidened corner: a zero, and not a fabricated one ───────────────────
//
// The opposite failure to gh#900 and just as bad: a row that always reports
// something would be as uninformative as one that always reports nothing.

#[test]
fn the_nlp_arm_reports_zero_when_it_did_not_widen() {
    let r = run("nlp", &["bound_relax_factor=0"], false);
    let (scaled, _) = bound_violation_row(&r.stdout);
    assert_eq!(
        scaled, 0.0,
        "with no widening the point is inside its declared box and the row \
         must say so; got {scaled:e}"
    );
    // The answer is the analytic one from the correct side. A converged
    // barrier iterate stops strictly inside the box, so `x > 0` and hence
    // `f > 0` by a barrier-sized margin — measured `9.1e-6`, i.e. `x` about
    // `9.1e-14` times the `1e8` slope. Pinning `|f| < 1e-6` would be pinning
    // the final `mu`, which is trajectory-dependent and not what this test is
    // about. What is about this test is the *sign*: `f* = 0` is a floor the
    // unwidened solve respects and the widened one is five orders below.
    let obj = r.report.solution.objective;
    assert!(
        (0.0..1e-3).contains(&obj),
        "the unwidened answer must approach `f* = 0` from inside the box, so \
         `0 <= f << 1`; got {obj:e}"
    );
}

#[test]
fn the_convex_arm_reports_zero_at_its_default() {
    // The convex arm's default is no widening (gh#744/#745), so this is what
    // an ordinary `pounce model.nl` prints.
    let r = run("auto", &[], false);
    let (scaled, _) = bound_violation_row(&r.stdout);
    assert_eq!(scaled, 0.0, "expected an unviolated box; got {scaled:e}");
    // Same floor as the NLP arm above, reached far more tightly: the convex
    // interior-point run lands at `f = 2.8e-12`.
    let obj = r.report.solution.objective;
    assert!(
        (0.0..1e-3).contains(&obj),
        "the analytic answer `f* = 0`, approached from inside; got {obj:e}"
    );
}

// ── the console and the JSON report agree ────────────────────────────────────

#[test]
fn the_json_report_carries_the_same_number_as_the_row() {
    for (engine, extra) in [("nlp", &[][..]), ("auto", &[RELAX][..])] {
        let r = run(engine, extra, false);
        let (printed, _) = bound_violation_row(&r.stdout);
        let reported = r.report.statistics.final_declared_box_viol;
        // Not `assert_eq!`. The two agree bit-for-bit in the file — the
        // console renders the `f64` at 17 significant digits and `serde_json`
        // writes the shortest round-tripping form of the same bits — but
        // `serde_json` is built here without its `float_roundtrip` feature, so
        // *parsing* the report back can land an ulp away. That is the test
        // harness's arithmetic, not the solver's, and pinning equality here
        // would be pinning a dependency's default. What the row and the field
        // must be is one measurement, which a few ulps says and an exact
        // compare would overstate.
        let tol = 8.0 * f64::EPSILON * reported.abs().max(printed.abs());
        assert!(
            (printed - reported).abs() <= tol,
            "`final_declared_box_viol` and the printed row must be one \
             measurement, not two, on the {engine} arm: {reported:e} vs \
             {printed:e}"
        );
        assert!(
            reported > 0.0,
            "and both must be the widening, not a zero, on the {engine} arm"
        );
    }
}

// ── the styling ──────────────────────────────────────────────────────────────

/// The declared-model line is bold red on a terminal.
#[test]
fn the_declared_violation_line_is_styled_when_color_is_on() {
    let r = run("nlp", &[], true);
    let line = r
        .stdout
        .lines()
        .find(|l| l.contains("Violation of the model as declared"))
        .unwrap_or_else(|| panic!("no declared-violation line in:\n{}", r.stdout));
    assert!(
        line.contains("\u{1b}[1m") && line.contains("\u{1b}[31m"),
        "expected bold red; got {line:?}"
    );
    assert!(
        line.ends_with("\u{1b}[0m"),
        "and a reset at the end of the line; got {line:?}"
    );
}

/// ...and plain text everywhere else, so redirected logs, the benchmark
/// harness's stdout scrapes and every `assert!(stdout.contains(..))` in the
/// suite are byte-for-byte unaffected. `anstream` owes this to `NO_COLOR` and
/// to a non-TTY sink; the test harness's pipe is the non-TTY sink.
#[test]
fn nothing_is_styled_when_stdout_is_not_a_terminal() {
    let r = run("nlp", &[], false);
    assert!(
        r.stdout.contains(
            "Violation of the model as declared (before the bound_relax_factor widening):"
        ),
        "the line itself must still be there, unstyled:\n{}",
        r.stdout
    );
    assert!(
        !r.stdout.contains('\u{1b}'),
        "no escape byte may reach a redirected stdout"
    );
}

// ── the active-set SQP arm ───────────────────────────────────────────────────
//
// The third code path, and the one this file's header did not know about: it
// printed `nan` on that row for every solve while the two arms above printed
// measurements. `IpoptApplication::optimize_sqp_tnlp` never called
// `relax_bounds` — correctly, because this arm applies no widening — and the
// declared-box snapshot that the row is computed from was taken inside it.
//
// The cliff fixture cannot carry these: it is a convex QP, so `auto` routes it
// to pounce-convex and `algorithm=active-set-sqp` never reaches the SQP driver
// at all. A genuine NLP is needed to exercise this arm.

/// `hs71_obj1e8.nl` — a real NLP (so it reaches the SQP driver) with a finite
/// lower and upper bound on every variable, so the box is not vacuous.
fn nlp_fixture() -> PathBuf {
    let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    p.push("tests/fixtures/hs71_obj1e8.nl");
    p
}

fn run_sqp(extra: &[&str]) -> Run {
    let json_path = tmp_path("sqp_report.json");
    let sol_path = tmp_path("sqp_out.sol");
    let mut cmd = Command::new(pounce_exe());
    cmd.arg(nlp_fixture())
        .arg(&sol_path)
        .arg("--json-output")
        .arg(&json_path)
        .arg("algorithm=active-set-sqp");
    for o in extra {
        cmd.arg(o);
    }
    cmd.env_remove("CLICOLOR_FORCE");
    let out = cmd.output().expect("spawn pounce");
    let text = std::fs::read_to_string(&json_path).expect("read json report");
    let _ = std::fs::remove_file(&json_path);
    let _ = std::fs::remove_file(&sol_path);
    Run {
        stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
        report: serde_json::from_str(&text).expect("deserialize SolveReport"),
    }
}

/// The row carries a number on this arm too. `nan` is the defect: it is not a
/// measurement, and it is what the row read on every active-set SQP solve.
#[test]
fn the_sqp_arm_reports_a_number_on_the_bound_violation_row() {
    let r = run_sqp(&[]);
    // Confirm the SQP driver actually ran — this fixture must not quietly
    // route elsewhere, or the test measures the wrong arm.
    assert_eq!(
        r.report.solution.engine, "sqp-active-set",
        "expected the active-set SQP arm; got {:?}",
        r.report.solution.engine
    );
    let (scaled, unscaled) = bound_violation_row(&r.stdout);
    assert!(
        scaled.is_finite() && unscaled.is_finite(),
        "the row must be a measurement, not `nan`: {scaled} / {unscaled}"
    );
    assert!(
        r.report.statistics.final_declared_box_viol.is_finite(),
        "and the JSON field too: {}",
        r.report.statistics.final_declared_box_viol
    );
    let tol = 8.0 * f64::EPSILON * scaled.abs().max(1.0);
    assert!(
        (scaled - r.report.statistics.final_declared_box_viol).abs() <= tol,
        "row and field must be one measurement: {scaled:e} vs {:e}",
        r.report.statistics.final_declared_box_viol
    );
}

/// The row is a live measurement on this arm, not a constant that happens to
/// read zero.
///
/// This is the case that makes the zero trustworthy, and it exists because the
/// arm follows the convex arm's `bound_relax_factor` rule (gh#745): unset
/// means solve the model as declared, so the answer is inside the box and the
/// row is genuinely `0`; named means the caller asked for the widening and
/// gets it, so the answer sits `~δ` outside the box they wrote and the row
/// says so.
///
/// Before the arm honoured the option there was no way to move this number
/// from the outside at all, and a hardcoded `0.0` would have been
/// indistinguishable from a measurement.
#[test]
fn the_sqp_bound_violation_is_zero_as_declared_and_nonzero_when_widened() {
    let as_declared = run_sqp(&[]);
    let (declared_row, _) = bound_violation_row(&as_declared.stdout);
    assert_eq!(
        declared_row, 0.0,
        "unset `bound_relax_factor` solves the model as declared, so the \
         returned point is inside the box the caller wrote; got {declared_row:e}"
    );

    let widened = run_sqp(&[RELAX]);
    let (widened_row, _) = bound_violation_row(&widened.stdout);
    assert!(
        widened_row > 0.0,
        "a named `bound_relax_factor` is honoured on this arm, so the answer \
         sits outside the declared box and the row must report it; got \
         {widened_row:e}"
    );
    // The widening is `min(factor·max(|b|,1), cap)` per bound, so with
    // `factor = 1e-8` and `cap = constr_viol_tol = 1e-4` the distance outside
    // is of order `1e-8` for an `O(1)` bound. Asserted as an order of
    // magnitude rather than a value: how far the solve actually settles from
    // the widened bound is trajectory-dependent.
    assert!(
        (1e-9..1e-7).contains(&widened_row),
        "expected a violation of order the 1e-8 widening; got {widened_row:e}"
    );
}

/// Naming the option gives this arm the interior-point arm's model, which is
/// the whole point of honouring it: before, the same binary solved two
/// different models depending on `algorithm`, and a caller comparing the arms
/// under a named `bound_relax_factor` was comparing answers to different
/// questions without being told.
///
/// Compared on the UNSCALED objective, which is the one both arms report in
/// the model's own units; the NLP arm additionally scales its internal
/// objective, so the scaled columns are not comparable across arms.
#[test]
fn a_named_bound_relax_factor_gives_both_arms_the_same_model() {
    let sqp = run_sqp(&[RELAX]);
    let (_, sqp_obj) = row(&sqp.stdout, "Objective...............");

    let json_path = tmp_path("nlp_relax.json");
    let sol_path = tmp_path("nlp_relax.sol");
    let out = Command::new(pounce_exe())
        .arg(nlp_fixture())
        .arg(&sol_path)
        .arg("--json-output")
        .arg(&json_path)
        .arg(RELAX)
        .output()
        .expect("spawn pounce");
    let nlp_stdout = String::from_utf8_lossy(&out.stdout).into_owned();
    let _ = std::fs::remove_file(&json_path);
    let _ = std::fs::remove_file(&sol_path);
    let (_, nlp_obj) = row(&nlp_stdout, "Objective...............");

    let rel = (sqp_obj - nlp_obj).abs() / nlp_obj.abs().max(1.0);
    assert!(
        rel < 1e-6,
        "both arms must answer the same widened model: SQP {sqp_obj:e} vs \
         NLP {nlp_obj:e} (relative {rel:e})"
    );
}

/// The residual table stays free of styling even with color forced on. It is
/// diffed against `ipopt`'s own output byte-for-byte, which is the reason the
/// styling went on the POUNCE-only line instead of the row that would most
/// obviously carry it.
#[test]
fn the_upstream_compatible_residual_table_is_never_styled() {
    let r = run("nlp", &[], true);
    for label in [
        "Objective...............",
        "Dual infeasibility......",
        "Constraint violation....",
        "Variable bound violation",
        "Complementarity.........",
        "Overall NLP error.......",
    ] {
        let line = r
            .stdout
            .lines()
            .find(|l| l.starts_with(label))
            .unwrap_or_else(|| panic!("no `{label}` row in:\n{}", r.stdout));
        assert!(
            !line.contains('\u{1b}'),
            "`{label}` must stay byte-compatible with upstream's block; got \
             {line:?}"
        );
    }
}