pounce-cli 0.17.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
//! `.nl` complementarity rows (AMPL `complements`, `r`-segment kind 5)
//! through the CLI: read as pairs, solved by
//! `pounce_restoration::mpcc_driver::solve_mpcc`, certified.
//!
//! The fixtures live in `tests/fixtures_mpcc/`, outside `tests/fixtures/`, so
//! the fixture sweep's corpus is unchanged by them.

use std::path::PathBuf;
use std::process::Command;

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

fn fixture(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures_mpcc")
        .join(name)
}

struct Run {
    code: Option<i32>,
    stderr: String,
    json: Option<serde_json::Value>,
}

fn run(name: &str, extra: &[&str]) -> Run {
    // One directory per call: tests run in parallel, and two of them solving
    // the same fixture would otherwise write and read the same report.json.
    static CALL: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
    let call = CALL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let dir = std::env::temp_dir().join(format!(
        "pounce-mpcc-{}-{}-{}",
        name,
        std::process::id(),
        call
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let json_path = dir.join("report.json");
    let out = Command::new(pounce_exe())
        .arg(fixture(name))
        .arg("--no-sol")
        .arg("--json-output")
        .arg(&json_path)
        .args(extra)
        .output()
        .expect("spawn pounce");
    let json = std::fs::read_to_string(&json_path)
        .ok()
        .and_then(|s| serde_json::from_str(&s).ok());
    Run {
        code: out.status.code(),
        stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
        json,
    }
}

fn x_of(r: &Run) -> Vec<f64> {
    r.json.as_ref().unwrap()["solution"]["x"]
        .as_array()
        .unwrap()
        .iter()
        .map(|v| v.as_f64().unwrap())
        .collect()
}

/// `qpec_small` (two pairs, the second biactive at the answer `(1, 1, 0)`):
/// read from `.nl`, solved, and certified strongly stationary.
#[test]
fn a_qpec_written_with_complements_is_solved_and_certified() {
    let r = run("qpec_small.nl", &[]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    // 1e-6, not looser: the exact-product stage's own point,
    // (1.000019, 1.0000095, 0), is within 1e-4 of the answer, and the report
    // once carried it in place of the continuation's, which was the one
    // accepted and summarized.
    let x = x_of(&r);
    for (got, want) in x.iter().zip([1.0, 1.0, 0.0]) {
        assert!((got - want).abs() < 1e-6, "x = {x:?}");
    }
    assert!(
        r.stderr.contains("2 pairs, 1 biactive; stationarity: S"),
        "{}",
        r.stderr
    );
    // The structure classifier reads the rows as a QP; with the pairing it is
    // not one, and the solve must not go to a convex engine.
    assert_eq!(
        r.json.as_ref().unwrap()["solution"]["engine"].as_str(),
        Some("nlp"),
        "{:?}",
        r.json
    );
}

/// `ralph1`: the answer, the origin, admits no S-stationary multiplier. The
/// run converges and says so: W, not S.
#[test]
fn a_converged_but_not_strongly_stationary_answer_is_labelled() {
    let r = run("ralph1.nl", &[]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    assert!(x_of(&r).iter().all(|v| v.abs() < 1e-4), "{:?}", x_of(&r));
    assert!(r.stderr.contains("stationarity: W only"), "{}", r.stderr);
}

/// A two-sided (mixed) complementarity, `0 <= y <= 1 ⟂ y - x` (`.nl` flag
/// 3), is solved, not refused or dropped. Its optimum is the origin (`f* = 0`;
/// the branches `y = 1` and `y = x` are worse), where the pair is biactive at
/// the lower bound and only weakly stationary, as in `ralph1` it extends.
/// Without the pairing the model is unbounded below in `y`'s direction.
#[test]
fn a_mixed_pair_is_solved() {
    let r = run("mixed_pair.nl", &[]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    let x = x_of(&r);
    assert!(x.iter().all(|v| v.abs() < 1e-4), "x = {x:?}");
    assert!(r.stderr.contains("stationarity: W"), "{}", r.stderr);
}

/// Presolve renumbers rows and variables, which the pairs index; asking for
/// it is honoured by declining it, out loud.
#[test]
fn presolve_stands_down_for_complementarity() {
    let r = run("qpec_small.nl", &["presolve=yes"]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    assert!(r.stderr.contains("disabling presolve"), "{}", r.stderr);
}

/// The JSON report carries the driver's record (`complementarity`), keyed like
/// Python's `info["complementarity"]`. `statistics.iteration_count` is the
/// reported lowered solve's alone; `total_iterations` is what the run cost,
/// and a report without it read a multi-stage solve as a single fast one.
#[test]
fn the_json_report_carries_the_complementarity_block() {
    let r = run("qpec_small.nl", &[]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    let j = r.json.as_ref().expect("report");
    let c = &j["complementarity"];
    assert_eq!(c["strategy"], "both", "{c}");
    assert_eq!(c["reported"], "answer", "{c}");
    assert!(c["accepted_by"].is_string(), "{c}");
    assert_eq!(c["certificate"]["class"], "S", "{c}");
    assert_eq!(c["certificate"]["feasible"], true, "{c}");
    let solves = c["solves"].as_array().expect("solves");
    let sum: u64 = solves
        .iter()
        .map(|s| s["iterations"].as_u64().unwrap())
        .sum();
    assert_eq!(c["total_iterations"].as_u64(), Some(sum), "{c}");
    assert!(
        sum > j["statistics"]["iteration_count"].as_u64().unwrap(),
        "the total must exceed the reported solve's own count: {c}"
    );
    assert!(
        solves
            .iter()
            .all(|s| s["status"].as_str().unwrap().contains('_')),
        "upstream status names: {c}"
    );

    // A weakly stationary fallback is not an answer.
    let r = run("ralph1.nl", &[]);
    let c = &r.json.as_ref().expect("report")["complementarity"];
    assert_eq!(c["reported"], "weak_fallback", "{c}");
    assert!(c.get("accepted_by").is_none(), "{c}");
    assert_eq!(c["certificate"]["class"], "W", "{c}");

    // The strategy is recorded, and only its stages (and the penalty) ran.
    let r = run("qpec_small.nl", &["complementarity_strategy=exact"]);
    let c = &r.json.as_ref().expect("report")["complementarity"];
    assert_eq!(c["strategy"], "exact", "{c}");
    assert!(
        c["solves"]
            .as_array()
            .unwrap()
            .iter()
            .all(|s| s["stage"] != "Scholtes continuation"),
        "{c}"
    );
}

/// A model without complementarity pairs carries no `complementarity` block.
#[test]
fn a_model_without_pairs_has_no_complementarity_block() {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("nonconvex_qp.nl");
    let dir = std::env::temp_dir().join(format!("pounce-no-mpcc-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let json_path = dir.join("report.json");
    let out = Command::new(pounce_exe())
        .arg(&path)
        .arg("--no-sol")
        .arg("--json-output")
        .arg(&json_path)
        .output()
        .expect("spawn pounce");
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let j: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&json_path).unwrap()).unwrap();
    assert!(j.get("complementarity").is_none(), "{j}");
}

/// `x` by `.col` name, for a fixture that ships its Pyomo column names.
fn x_by_name(r: &Run, nl: &str) -> std::collections::HashMap<String, f64> {
    let col = std::fs::read_to_string(fixture(&nl.replace(".nl", ".col"))).unwrap();
    col.split_whitespace()
        .map(String::from)
        .zip(x_of(r))
        .collect()
}

/// The largest `min(r_h, s_h)` over the OPF fixtures' six hinge pairs.
fn natural_residual(x: &std::collections::HashMap<String, f64>) -> f64 {
    (1..=6)
        .map(|h| x[&format!("r[{h}]")].min(x[&format!("s[{h}]")]).abs())
        .fold(0.0, f64::max)
}

fn last_stage(r: &Run) -> (String, String) {
    let s = r.json.as_ref().unwrap()["complementarity"]["solves"]
        .as_array()
        .unwrap()
        .last()
        .unwrap()
        .clone();
    (
        s["stage"].as_str().unwrap().to_string(),
        s["status"].as_str().unwrap().to_string(),
    )
}

/// The polish's accepted branch (`mpcc_driver`, `natural_residual`). At a
/// knot of the volt-var curve the hinge pair is biactive, and every stage
/// stops at a product near the last `τ` with both sides near `sqrt(τ)`:
/// `r = 7.8e-5`, `s = 1.3e-4` at a product of `1e-8`, which put `q`
/// 5.9e-4 off its curve (slope 7.5) while reporting `Solve_Succeeded`. The
/// re-solve on the branch that holds each pair's smaller side at zero
/// removes it.
#[test]
fn a_nearly_biactive_answer_is_re_solved_onto_its_branch() {
    let r = run("opf_vv_lower_knee.nl", &[]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    assert_eq!(
        r.json.as_ref().unwrap()["solution"]["status_upstream"],
        "Solve_Succeeded"
    );
    assert_eq!(last_stage(&r).0, "branch polish");
    let x = x_by_name(&r, "opf_vv_lower_knee.nl");
    assert!(
        natural_residual(&x) <= 1e-9,
        "min(r, s) = {:e}",
        natural_residual(&x)
    );
    // q on its volt-var curve, evaluated directly: (0.94, 0.3) to (0.98, 0).
    let (v, q) = (x["v[2]"], x["q[1]"]);
    let vv = if v <= 0.94 {
        0.3
    } else if v >= 0.98 {
        0.0
    } else {
        0.3 * (0.98 - v) / 0.04
    };
    assert!((q - vv).abs() <= 1e-7, "v = {v}, q = {q}, VV(v) = {vv}");
    // The report says so, without the reader recomputing it: the reported
    // point's residual, and the re-solve that produced it.
    let c = &r.json.as_ref().unwrap()["complementarity"];
    let nr = c["natural_residual"].as_f64().expect("natural_residual");
    assert!(nr <= 1e-9, "natural_residual = {nr:e}");
    let p = &c["polish"];
    assert_eq!(p["taken"], true, "{p}");
    assert!(p["residual_before"].as_f64().unwrap() > 1e-5, "{p}");
    assert_eq!(p["residual_after"].as_f64(), Some(nr), "{p}");
    assert!(p["reason"].is_null(), "{p}");
    assert!(c["solves"][0]["seconds"].as_f64().is_some(), "{c}");
}

/// The polish's other branch: `opf_vv_lower_knee_held.nl` adds `r[1] >=
/// 5e-5`, which the stages' near-biactive point satisfies and the branch
/// holding `r[1]` at zero cannot. The re-solve is refused as infeasible and
/// the answer is reported as it was, unpolished; it is never replaced by
/// the failed re-solve's point.
#[test]
fn a_branch_the_answer_cannot_hold_leaves_the_answer_as_it_was() {
    let r = run("opf_vv_lower_knee_held.nl", &[]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    let (stage, status) = last_stage(&r);
    assert_eq!(stage, "branch polish");
    assert_eq!(status, "Infeasible_Problem_Detected");
    assert_eq!(
        r.json.as_ref().unwrap()["solution"]["status_upstream"],
        "Solve_Succeeded"
    );
    let x = x_by_name(&r, "opf_vv_lower_knee_held.nl");
    assert!(x["r[1]"] >= 5e-5 - 1e-9, "r[1] = {}", x["r[1]"]);
    assert!(
        natural_residual(&x) > 1e-6,
        "the kept point is the stages' own"
    );
    // The report says the answer is loose, and why it stayed so: a reader
    // of `certificate.class` alone would take it for a tight S answer.
    let c = &r.json.as_ref().unwrap()["complementarity"];
    let nr = c["natural_residual"].as_f64().expect("natural_residual");
    assert!(nr > 1e-6, "natural_residual = {nr:e}");
    let p = &c["polish"];
    assert_eq!(p["taken"], false, "{p}");
    assert_eq!(p["residual_before"].as_f64(), Some(nr), "{p}");
    assert_eq!(
        p["reason"], "the re-solve returned Infeasible_Problem_Detected",
        "{p}"
    );
}

/// A point already complementary to `tol` is not re-solved, and the report
/// says how complementary it is with no `polish` record. Pinned to the
/// schedule: it is this branch's fixture, and under the coupled continuation
/// `qpec_small`'s answer starts at a residual of 1e-6 and is polished (to 0),
/// which is the other branch.
#[test]
fn a_tight_answer_reports_its_residual_and_no_polish() {
    let r = run("qpec_small.nl", &["complementarity_continuation=schedule"]);
    assert_eq!(r.code, Some(0), "{}", r.stderr);
    let c = &r.json.as_ref().unwrap()["complementarity"];
    let nr = c["natural_residual"].as_f64().expect("natural_residual");
    assert!(nr <= 1e-7, "natural_residual = {nr:e}");
    assert!(c.get("polish").is_none(), "{c}");
}

/// The ladder's last branch: no formulation answers (`infeasible_pair.nl`
/// has `x >= 1`, `y >= 1` and `x ⟂ y`), so every stage runs, the l1 penalty
/// included, and the exact-product solve's verdict is reported. Not an
/// answer, so it is neither certified feasible nor polished.
#[test]
fn a_pair_no_point_satisfies_reports_the_verdict() {
    let r = run("infeasible_pair.nl", &[]);
    let j = r.json.as_ref().expect("report");
    assert_eq!(
        j["solution"]["status_upstream"], "Infeasible_Problem_Detected",
        "{}",
        r.stderr
    );
    let c = &j["complementarity"];
    assert_eq!(c["reported"], "verdict", "{c}");
    assert!(
        c.get("accepted_by").is_none() && c.get("fallback").is_none(),
        "{c}"
    );
    assert_eq!(c["certificate"]["feasible"], false, "{c}");
    assert!(c.get("polish").is_none(), "{c}");
    let stages: Vec<&str> = c["solves"]
        .as_array()
        .unwrap()
        .iter()
        .map(|s| s["stage"].as_str().unwrap())
        .collect();
    assert!(stages.contains(&"l1 penalty"), "{stages:?}");
}

fn continuation_taus(r: &Run) -> Vec<Option<f64>> {
    r.json.as_ref().unwrap()["complementarity"]["solves"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|s| s["stage"] == "Scholtes continuation")
        .map(|s| s["tau"].as_f64())
        .collect()
}

/// The coupled continuation's other branch (review of #1135, item 2): its
/// answer does not pass the schedule's test, so the schedule resumes at
/// `τ = 0.1` warm from the `τ = 1` solve. `ralph1` under limited memory is
/// the measured case (the sweep's lbfgs leg): the coupled solve reaches the
/// floor, all three finish attempts miss, and the schedule's eight rungs and
/// finish follow. The answer is the schedule's own.
#[test]
fn a_coupled_continuation_that_does_not_answer_hands_over_to_the_schedule() {
    let lbfgs = "hessian_approximation=limited-memory";
    let r = run("ralph1.nl", &[lbfgs]);
    assert!(
        r.stderr.contains("resuming the schedule at τ = 1e-1"),
        "{}",
        r.stderr
    );
    let taus = continuation_taus(&r);
    assert_eq!(taus[0], Some(1.0), "{taus:?}");
    let resumed = taus
        .iter()
        .position(|t| *t == Some(0.1))
        .expect("schedule resumed");
    assert_eq!(
        &taus[resumed..resumed + 8],
        &[1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8].map(Some),
        "{taus:?}"
    );
    let s = run(
        "ralph1.nl",
        &[lbfgs, "complementarity_continuation=schedule"],
    );
    let f = |r: &Run| {
        r.json.as_ref().unwrap()["solution"]["objective"]
            .as_f64()
            .unwrap()
    };
    assert!((f(&r) - f(&s)).abs() <= 1e-9, "{} vs {}", f(&r), f(&s));
}

/// The coupling rides the interior-point loop's μ updates; under another
/// engine there are none, so the schedule runs from the start (review of
/// #1135, item 5).
#[test]
fn the_active_set_engine_runs_the_schedule() {
    let r = run("qpec_small.nl", &["algorithm=active-set-sqp"]);
    let mut want: Vec<Option<f64>> = [1.0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8]
        .map(Some)
        .to_vec();
    want.push(None);
    assert_eq!(continuation_taus(&r), want, "{}", r.stderr);
}

/// The lifted path at the default (#1137 review, item 3). Every other
/// fixture here has paired rows narrower than `LIFT_AUTO_MIN_ENTRIES`, so the
/// lift was reached only under `complementarity_lift=yes`, and the sweep's
/// `cc:` lines diffed empty on it by construction. MacMPEC `design-cent-3`
/// pairs three rows of exactly the threshold width (8 Jacobian entries), so
/// `auto` lifts all three with an exact Hessian. Lifted or not, it must be
/// the same strongly stationary answer; MacMPEC's best known is 3.7237
/// (maximize).
#[test]
fn a_wide_paired_row_is_lifted_at_the_default() {
    let f = |r: &Run| {
        r.json.as_ref().unwrap()["solution"]["objective"]
            .as_f64()
            .unwrap()
    };
    let cc = |r: &Run| r.json.as_ref().unwrap()["complementarity"].clone();
    let lifted = run("design_cent_3.nl", &[]);
    let plain = run("design_cent_3.nl", &["complementarity_lift=no"]);
    for (r, n) in [(&lifted, 3), (&plain, 0)] {
        assert_eq!(r.code, Some(0), "stderr: {}", r.stderr);
        assert_eq!(cc(r)["lifted"], n, "stderr: {}", r.stderr);
        assert_eq!(cc(r)["certificate"]["class"], "S", "stderr: {}", r.stderr);
        assert!((f(r) - 3.7237).abs() < 1e-3, "objective {}", f(r));
    }
    assert!(
        (f(&lifted) - f(&plain)).abs() < 1e-8,
        "lifted {} vs unlifted {}",
        f(&lifted),
        f(&plain)
    );
    // Under limited memory `auto` does not lift: lifted, this model ends
    // Restoration_Failed with no answer, where unlifted it is the S answer.
    let lbfgs = run(
        "design_cent_3.nl",
        &["hessian_approximation=limited-memory"],
    );
    assert_eq!(cc(&lbfgs)["lifted"], 0, "stderr: {}", lbfgs.stderr);
    assert_eq!(
        cc(&lbfgs)["certificate"]["class"],
        "S",
        "stderr: {}",
        lbfgs.stderr
    );
    assert!((f(&lbfgs) - 3.7237).abs() < 1e-3, "objective {}", f(&lbfgs));
}