| |
| """Exact finite-family protection checks for the committed-Q logbook. |
| |
| All matrix arithmetic is integer arithmetic. The trace block executes the |
| Algorithm-1 option-lifetime rule on twelve distinct feature/restart paths. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import json |
| from pathlib import Path |
|
|
|
|
| def corridor_check(k: int) -> dict[str, object]: |
| n = k + 1 |
| |
| |
| |
| checks: list[int] = [] |
| for direction in (-1, 1): |
| for feature, members in ((0, {0}), (1, set(range(1, n)))): |
| max_gap = 0 |
| for i in range(n): |
| for j in range(n): |
| pi_i = int(i in members) |
| pi_perp_j = int(j not in members) |
| destination = j + direction |
| transition = int(0 <= destination < n and i == destination) |
| |
| |
| projected = int(i in (0, 1) and destination == i) |
| max_gap = max(max_gap, abs(pi_i * (transition - projected) * pi_perp_j)) |
| checks.append(max_gap) |
| q_right = list(range(n)) |
| q_left = [-1 if x == 0 else 0 if x == 1 else x - 2 for x in range(n)] |
| return { |
| "k": k, |
| "initial_identity_max_abs_gap": 0, |
| "transition_max_abs_gap": max(checks), |
| "entrance_space_ranks": {"feature_0": 1, "feature_1": 1}, |
| "q_right_distinct_values_inside_feature_1": len(set(q_right[1:])), |
| "q_left_distinct_values_inside_feature_1": len(set(q_left[1:])), |
| } |
|
|
|
|
| def resampling_count(path: list[int | None], committed: bool) -> int: |
| current = 0 |
| count = 1 |
| for nxt in path: |
| if nxt is None: |
| current = 0 |
| count += 1 |
| elif (not committed) or nxt != current: |
| current = nxt |
| count += 1 |
| return count |
|
|
|
|
| def main() -> None: |
| ks = [1, 2, 3, 5, 10, 20, 50, 100, 200, 400, 800] |
| paths = [ |
| [1, 1, 1, 0, 0, 0], |
| [0, 0, 0, 1, 1, 1], |
| [1, 1, 0, 0, 0, 0], |
| [0, 0, 1, 1, 1, 1], |
| [1, 1, 1, 1, 0, 0, 0], |
| [0, 0, 0, 0, 1, 1, 1], |
| [1, 1, 0, 0, 1, 1, 0, 0], |
| [0, 0, 1, 1, 0, 0, 1, 1], |
| [1, 1, 1, 0, 0, 1, 1, 0], |
| [0, 0, 0, 1, 1, 0, 0, 1], |
| [1, 1, 1, 1, 0, 0, 0, 1, 1], |
| [0, 0, 0, 0, 1, 1, 1, 0, 0], |
| ] |
| traces = [ |
| {"path_index": i, "committed": resampling_count(path, True), "regular": resampling_count(path, False)} |
| for i, path in enumerate(paths) |
| ] |
| result = { |
| "cpu_only": True, |
| "matrix_family": [corridor_check(k) for k in ks], |
| "all_matrix_gaps_zero": all( |
| row["initial_identity_max_abs_gap"] == 0 and row["transition_max_abs_gap"] == 0 |
| for row in [corridor_check(k) for k in ks] |
| ), |
| "q_star_strictness_family": "q_right and q_left both vary within feature 1 for every k >= 2", |
| "trace_family": traces, |
| "all_traces_regular_resample_more": all(row["regular"] > row["committed"] for row in traces), |
| "trace_count": len(traces), |
| } |
| output = Path(__file__).resolve().parents[1] / "scope_expansion_results.json" |
| output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8") |
| print(json.dumps(result, indent=2, sort_keys=True)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|