File size: 3,370 Bytes
7956103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CPU environment and finite regression smoke checks, not research results."""

import hashlib
import json
import os
from pathlib import Path
import platform
import subprocess
import sys

for key in ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS"):
    os.environ[key] = "1"
os.environ["MPLBACKEND"] = "Agg"

import numpy as np
import scipy
from scipy.optimize import linprog
from scipy.stats import binom
import matplotlib
matplotlib.use("Agg")
from matplotlib import pyplot as plt


def main():
    project = Path(__file__).resolve().parents[1]
    pmf = binom.pmf(np.arange(129), 128, 0.25)
    np.testing.assert_allclose(pmf.sum(), 1.0, atol=1e-12, rtol=0)
    np.testing.assert_allclose(pmf @ np.arange(129), 32.0, atol=1e-11, rtol=0)

    # Two allowed actions for one fair pair: serial mismatch mean 1/8, or
    # co-sampled mismatch mean 1/2. Hidden mixing can match target mean 1/4.
    # Variables: serial weight, co-sample weight, absolute discrepancy.
    lp = linprog(
        c=[0.0, 0.0, 1.0],
        A_ub=[[0.125, 0.5, -1.0], [-0.125, -0.5, -1.0]],
        b_ub=[0.25, -0.25],
        A_eq=[[1.0, 1.0, 0.0]], b_eq=[1.0],
        bounds=[(0, None), (0, None), (0, None)], method="highs",
    )
    if not lp.success:
        raise RuntimeError(lp.message)
    np.testing.assert_allclose(lp.fun, 0.0, atol=1e-9, rtol=0)
    np.testing.assert_allclose(lp.x[:2], [2 / 3, 1 / 3], atol=1e-9, rtol=0)
    fig, ax = plt.subplots()
    ax.plot(np.arange(129), pmf)
    fig.canvas.draw()
    plt.close(fig)

    checker = project / "src" / "verify_main_examples.py"
    completed = subprocess.run(
        [sys.executable, str(checker)], check=True, capture_output=True,
        text=True, timeout=180,
    )
    regression = json.loads(completed.stdout)
    if regression.get("status") != "PASS":
        raise RuntimeError(regression)
    record = {
        "status": "PASS",
        "scope": "CPU environment and finite regression checks only; no main experiments run",
        "python_executable": sys.executable,
        "python_version": platform.python_version(),
        "platform": platform.platform(),
        "hostname": platform.node(),
        "packages": {"numpy": np.__version__, "scipy": scipy.__version__,
                     "matplotlib": matplotlib.__version__},
        "environment_mode": "project venv sharing existing Conda base site-packages",
        "visible_cpu_count": os.cpu_count(),
        "gpu_used": False,
        "library_thread_settings": {key: os.environ[key] for key in
            ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS")},
        "binomial_mass_residual": float(abs(pmf.sum() - 1)),
        "highs_randomization_smoke": {
            "objective": float(lp.fun), "weights": lp.x[:2].tolist(),
            "equality_residual": float(np.max(np.abs(lp.eqlin.residual))),
            "scope": "two-action LP only, not a general policy optimizer",
        },
        "headless_plotting": "PASS (canvas only; no publication figure generated)",
        "finite_regression": regression,
        "finite_regression_sha256": hashlib.sha256(checker.read_bytes()).hexdigest(),
    }
    output = project / "logs" / "environment_check.json"
    output.write_text(json.dumps(record, indent=2) + "\n", encoding="utf-8")
    print(json.dumps(record, indent=2))


if __name__ == "__main__":
    main()