#!/usr/bin/env python3 """Job #1111 (route 8 rescue): how large is the exact object? The exact witness is x = M[C,B]^-1 b_C with M[C,B] a sparse +-1 matrix (measured: every coefficient is +-1, mean degree 4.47, order 8537) and b_C a single unit vector on the normalisation row. Its rational coordinates therefore have denominators dividing det M[C,B], and the integer witness that the unchanged checker consumes is that same x scaled by the denominators. This script estimates * log10|det| from the pivots of a sparse LU factorisation, * the smallest and largest pivot magnitudes (a proxy for conditioning), * the float solution and its residual, * whether the float solution already satisfies the auxiliary LP inequalities, so that the cost of the exact solve is bounded before anyone spends compute on it. It is an estimate from floating point pivots, not an exact determinant, and it decides nothing about feasibility or about the existence of the witness. stdout is the artifact (deterministic). Progress goes to stderr. """ import json import os import sys import time from pathlib import Path for _k in ('OMP_NUM_THREADS', 'OPENBLAS_NUM_THREADS', 'MKL_NUM_THREADS'): os.environ.setdefault(_k, '1') import numpy as np # noqa: E402 from scipy.sparse import csr_matrix # noqa: E402 from scipy.sparse.linalg import splu # noqa: E402 HERE = Path(__file__).parent try: sys.stdout.reconfigure(newline='\n') # portable LF stdout sys.stderr.reconfigure(newline='\n') except Exception: pass T0 = time.process_time() def log(**kw): print(json.dumps(kw, sort_keys=True), file=sys.stderr, flush=True) def load_reduced(path): """Read reduced_system.txt: active row indices, basic column indices, entries (row, col, v).""" rows, cols, entries, cur = None, None, [], None with open(path, encoding='utf-8') as fh: for line in fh: line = line.strip() if line.startswith('#'): continue if line.startswith('active_rows: '): rows = [int(v) for v in line.split(': ', 1)[1].split(',')] elif line.startswith('basic_columns: '): cols = [int(v) for v in line.split(': ', 1)[1].split(',')] elif line.startswith('frozen_nonbasic_columns_at_zero: '): continue elif line.startswith('c '): cur = int(line.split()[1]) elif line: r, v = line.split() entries.append((cur, int(r), int(v))) assert rows is not None and cols is not None return rows, cols, entries def main(): rows, cols, entries = load_reduced(HERE / 'reduced_system.txt') k = len(rows) ri = np.array([e[0] for e in entries], dtype=np.int32) ci = np.array([e[1] for e in entries], dtype=np.int32) vv = np.array([e[2] for e in entries], dtype=float) M = csr_matrix((vv, (ri, ci)), shape=(k, k)).tocsc() # the RHS is 1 on the active row that is the auxiliary LP's normalisation row f^T z <= 1; # that row has the largest auxiliary-LP row index (all column inequalities precede it) rhs = np.zeros(k) rhs[rows.index(max(rows))] = 1.0 log(stage='loaded', order=k, nnz=int(M.nnz)) lu = splu(M) diag = lu.U.diagonal() piv = np.abs(diag) logdet = float(np.sum(np.log10(piv))) x = lu.solve(rhs) resid = float(np.max(np.abs(M.dot(x) - rhs))) art = { 'job': 1111, 'route_id': 8, 'kind': 'reduced_exact_system_cost_estimate', 'system': {'order': k, 'nnz': int(M.nnz), 'coefficients': 'all +-1 (measured in reduced.py)'}, 'float_lu': { 'log10_abs_determinant_estimate': round(logdet, 3), 'min_pivot': float(piv.min()), 'max_pivot': float(piv.max()), 'pivot_ratio': float(piv.max() / piv.min()), 'max_residual_of_float_solution': resid, 'note': ('log10|det| bounds the size of the integers a scaled exact witness can ' 'need; it is a floating-point pivot estimate, not an exact determinant'), }, 'float_solution': { 'min_abs_nonzero': float(np.min(np.abs(x[np.abs(x) > 0.0]))) if np.any(x) else 0.0, 'max_abs': float(np.max(np.abs(x))), 'decades': (float(np.log10(np.max(np.abs(x)) / np.min(np.abs(x[np.abs(x) > 0.0])))) if np.any(x) else 0.0), 'sign_pattern_negative': int(np.sum(x < 0)), }, } print(json.dumps(art, sort_keys=True, indent=2)) log(stage='done', cpu_seconds=round(time.process_time() - T0, 2)) return 0 if __name__ == '__main__': raise SystemExit(main())