Anytime Primal-Dual Neural Robustness Radius / benchmark.py
Mechanism failed
1import json
2import time
3import numpy as np
4from anytime_pd import solve_full, hierarchy, stop_level
5
6
7def one(seed, n=24):
8 rng = np.random.default_rng(seed)
9 # Bounded feasible LP with a dense coupling block.
10 A = np.vstack([np.eye(n), -np.eye(n), rng.normal(size=(n // 2, n))])
11 b = np.concatenate([np.ones(n), np.ones(n), 1.5 * np.ones(n // 2)])
12 c = rng.uniform(0.05, 1.0, n)
13 t0 = time.perf_counter()
14 lp, ld, _, ystar = solve_full(A, b, c)
15 full_time = time.perf_counter() - t0
16 bases = [np.eye(n)[:, :k] for k in range(2, n + 1, 2)]
17 # This is deliberately marked oracle-seeded: it supplies a known feasible
18 # dual point, allowing us to test certificate mechanics independently of
19 # the difficult problem of finding the first dual feasible point.
20 dual_bases = [np.column_stack([ystar, np.eye(len(b))[:, :k]])
21 for k in range(2, n + 1, 2)]
22 t1 = time.perf_counter()
23 rows = hierarchy(A, b, c, bases, dual_bases, full_opt=lp)
24 anytime_time = time.perf_counter() - t1
25 level = stop_level(rows, tau=0.02)
26 return {
27 "seed": seed, "full_time_sec": full_time,
28 "anytime_all_levels_sec": anytime_time,
29 "target_level": level, "n_levels": len(rows),
30 "full_opt": lp, "final_lower": rows[-1].lower,
31 "coverage": all(x.covers_full for x in rows),
32 "monotone_width": all(x.width >= y.width - 1e-8
33 for x, y in zip(rows, rows[1:])),
34 "widths": [x.width for x in rows],
35 }
36
37
38def main():
39 rows = [one(s) for s in range(5)]
40 print(json.dumps({"trials": rows,
41 "median_full_sec": float(np.median([x["full_time_sec"] for x in rows])),
42 "median_anytime_sec": float(np.median([x["anytime_all_levels_sec"] for x in rows])),
43 "all_coverage": all(x["coverage"] for x in rows),
44 "all_monotone": all(x["monotone_width"] for x in rows)}, indent=2))
45
46
47if __name__ == "__main__":
48 main()