#!/usr/bin/env python3 """Exact small-instance planning CLI. See the manuscript for the data contract.""" import argparse import json from pathlib import Path from calibration_core import (rational_matrix, residual_spaces, generic_profile, common_generic_design, numerical_profile, jsonable) def plan(spec): n = int(spec["n"]) supports, spaces = residual_spaces(n, spec["edges"], spec.get("trusted_rows", [])) channels = [rational_matrix(H, n) for H in spec["sensor_spaces"]] if len(channels) > 16: raise ValueError("this transparent exhaustive planner is limited to 16 channels") profiles = [generic_profile(E, channels) for E in spaces] distances = [p["distance"] for p in profiles if p["distance"] is not None] optimum = min(distances) if distances else None f = int(spec.get("erasures", 0)); t = int(spec.get("errors", 0)) if f < 0 or t < 0: raise ValueError("negative fault budget") design = common_generic_design(spaces, channels, seed=int(spec.get("seed", 13371337))) actual = None if design["status"] == "certified": actual = [numerical_profile(E, design["rows"]) for E in spaces] return jsonable({"contract": "known aligned spaces; trusted installed constraints; linear discrepancy observations", "supports": supports, "residual_bases": spaces, "dimensions": [len(E) for E in spaces], "total_dimension": sum(map(len, spaces)), "dense_no_fault_channels": max(map(len, spaces), default=0), "optimal_architecture_distance": optimum, "requested_erasures": f, "requested_errors": t, "requested_tolerance_possible": optimum is None or optimum > f+2*t, "generic_profiles": profiles, "design": design, "numerical_certificates": actual}) if __name__ == "__main__": p = argparse.ArgumentParser(description=__doc__) p.add_argument("input", type=Path) p.add_argument("--output", type=Path) a = p.parse_args() result = plan(json.loads(a.input.read_text())) text = json.dumps(result, indent=2)+"\n" if a.output: a.output.write_text(text) else: print(text, end="")