From 1859df94f7a7db6d9b6ef3a6b6900daf7cef509d Mon Sep 17 00:00:00 2001 From: Ryan James Date: Mon, 16 Nov 2026 22:37:21 -0700 Subject: [PATCH] Run the generator heaviest-term bound last and by default; time-limit its LPs The generator heaviest-term bound now runs after the best generator is verified, aiming at its entropy with the remaining budget, and is on by default (joints up to 14 causal states with a budget; 7 states and 2,000 boxes without one). On the 106 open C_g brackets of the binary 2-/3-state dataset, 900 s per process raised the lower bound on 48 and closed 9. Each generator-cap LP inside the bound now gets the time left before the deadline: a single degenerate HiGHS solve previously overran a 900 s deadline by more than half an hour. Co-authored-by: Cursor --- CHANGELOG.md | 13 ++++++ docs/generators/generative_models.rst | 12 ++++- sofic/generators/exact_ci_bounds.py | 13 +++--- sofic/generators/generative_search.py | 63 +++++++++++++++++---------- tests/test_exact_ci_bounds.py | 35 +++++++++++++++ tests/test_generative_model.py | 14 ++++++ 6 files changed, 118 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fa89ba..c92b6e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,19 @@ A second correctness review: every fix below was reproduced against a brute-force reference or a closed-form value and has a regression test. +### Generator heaviest-term bound by default + +- `generative_model` now runs `generator_heaviest_term_bound` last, once the best generator is + verified, aiming at its entropy with the remaining budget (`generator_heaviest_term=True` is the new + default). With a time budget it runs on joints with up to 14 causal states; without one it stays at + `|S+| + |S-| <= 7` and 2,000 boxes. On the 106 open `C_g` brackets of a dataset of binary 2- and + 3-state ε-machines, 900 s per process raised the lower bound on 48 of 105 and closed 9 (median + bracket width 0.066 → 0.039 bits). Some of the closed ones cannot be certified by caps on the + heaviest weights at all, since another generator has a heavier state. +- `generator_heaviest_term_bound` limits each generator-cap LP to the time left before its + `deadline`. A single degenerate HiGHS solve previously ran more than half an hour past a 900 s + deadline (`n3-t045-d3`); it now returns on time. + ### Finite-state rounded HMM filters - `quantized_filter` builds the reachable log-odds-grid observer of Feng & Meng diff --git a/docs/generators/generative_models.rst b/docs/generators/generative_models.rst index a80fa88..83e9846 100644 --- a/docs/generators/generative_models.rst +++ b/docs/generators/generative_models.rst @@ -314,8 +314,16 @@ weight is also capped by the one-explicit-state generator-cap LP over the box of :math:`\op{G}{}`-optimal heavy term the bound exceeds :math:`\op{G}{S^+ : S^-}`: on ``((0,1),(0,2),(0,1))`` it certifies :math:`C_g \ge 0.928 > \op{G}{} = 0.924`, but only after about 220,000 boxes (half an hour), because the LP excludes that term only in boxes of radius about -:math:`10^{-4}`. The pipeline therefore runs it only on request (``generator_heaviest_term=True``). -This combination is not from the literature. +:math:`10^{-4}`. Elsewhere it converges much faster. On the 106 open :math:`C_g` brackets of a +dataset of binary 2- and 3-state ε-machines (joints from :math:`2 \times 2` to :math:`3 \times 11`), +900 s per process raised the lower bound on 48 of 105 and closed 9, taking the median bracket width +from 0.066 to 0.039 bits. The closed ones include processes where some generator's heaviest state +outweighs the minimal generator's, which caps on the heaviest weights alone cannot certify. The +pipeline therefore runs it last, once the upper bound is verified, aiming at that bound with the +remaining budget (``generator_heaviest_term=True``, the default), on joints with up to 14 causal +states; without a budget it stays at 7 causal states and 2,000 boxes. Each generator-cap LP inside it +is limited to the time left, because a single degenerate LP otherwise ran past the deadline by more +than half an hour. This combination is not from the literature. Lifted generators get their own bound. :func:`~sofic.generators.exact_ci_bounds.lifted_heaviest_term_bound` uses that a lifted generator draws diff --git a/sofic/generators/exact_ci_bounds.py b/sofic/generators/exact_ci_bounds.py index 7af4fbb..adba276 100644 --- a/sofic/generators/exact_ci_bounds.py +++ b/sofic/generators/exact_ci_bounds.py @@ -200,13 +200,12 @@ def generator_heaviest_term_bound( relaxation = _HeaviestStatesRelaxation(problem, 1) P = problem.lifted.plus.T @ (problem.p_pair[:, None] * problem.lifted.minus) - return heaviest_term_bound( - P, - target=target, - max_nodes=max_nodes, - deadline=deadline, - weight_cap=lambda box: relaxation.bound((box[:4],)), - ) + + def weight_cap(box: Box) -> float | None: + left = None if deadline is None else deadline - time.monotonic() + return relaxation.bound((box[:4],), left) + + return heaviest_term_bound(P, target=target, max_nodes=max_nodes, deadline=deadline, weight_cap=weight_cap) class _LiftedTermTest: diff --git a/sofic/generators/generative_search.py b/sofic/generators/generative_search.py index 1d9f975..d2b223c 100644 --- a/sofic/generators/generative_search.py +++ b/sofic/generators/generative_search.py @@ -21,6 +21,10 @@ (:func:`~sofic.generators.coupled_generators.line_split_seeds`), memory lifts, the exact minimal functional generator (:func:`~sofic.generators.functional_generators.functional_cover_search`), lifted channels (small joints), coupled search. +5. *Final lower bound.* While the bracket is open, the generator heaviest-term bound + (:func:`~sofic.generators.exact_ci_bounds.generator_heaviest_term_bound`, ``generator_heaviest_term``) + aims at the verified upper bound with the rest of the budget, on joints with at most 14 causal states + (7 without a budget, where it is capped at 2,000 boxes). Every candidate is word-verified. ``certified`` reports when ``lower`` meets ``upper``. :func:`minimal_wyner_model` runs the same stages ranked by :math:`\I{W : S^+, S^-}`; the functional @@ -83,6 +87,11 @@ # The heaviest-term bound branches over |S+| + |S-| - 1 coordinates; run it only up to this many causal # states (|S+| + |S-|), where it converges within a stage budget. _HEAVIEST_TERM_MAX_DIMS = 7 +# Node cap for the final generator heaviest-term stage when there is no time budget (about 10 s on +# 2 x 2 and 3 x 3 joints); with a budget the deadline governs, and joints up to this many causal states +# are worth it (900 s raised the bound on 3 x 5 to 3 x 11 joints of the binary 3-state dataset too). +_GENERATOR_HEAVIEST_TERM_NODES = 2_000 +_GENERATOR_HEAVIEST_TERM_MAX_DIMS = 14 def _entropy(p: np.ndarray) -> float: @@ -398,7 +407,7 @@ def _bracket_search( coverage_mu_starts: int = 25, coverage_k_max: int = 8, coverage_certified_only: bool = False, - generator_heaviest_term: bool = False, + generator_heaviest_term: bool = True, generator_caps: int = 2, targeted: bool = True, entropy_sweep_starts: int = 80, @@ -684,26 +693,6 @@ def maybe_return_early() -> GenerativeModelResult | None: ) if htb.value > lower + 1e-9: lower, lower_source, lower_certified = htb.value, "heaviest-term bound (certified)", True - best_upper = min((c[0] for c in candidates), default=np.inf) - if ( - generator_heaviest_term - and not wyner - and lower < best_upper - _CERT_TOL - and problem.n_plus + problem.n_minus <= _HEAVIEST_TERM_MAX_DIMS - and not out_of_time("generator heaviest-term bound") - ): - gtb = generator_heaviest_term_bound(problem, target=best_upper - 0.5 * _CERT_TOL, deadline=share(0.3)) - log.append( - { - "stage": "generator heaviest-term bound (certified)", - "value": gtb.value, - "nodes": gtb.nodes, - "reached": gtb.reached, - } - ) - if gtb.value > lower + 1e-9: - lower, lower_source = gtb.value, "generator heaviest-term bound (certified)" - lower_certified = True if bilevel and n_generating == 0 and optima and not out_of_time("bilevel search"): for ent, viol, c in bilevel_search(bidir, optima[:8], deadline=share(0.15))[:4]: log.append({"stage": "bilevel search", "entropy": ent, "kernel violation": viol}) @@ -902,6 +891,34 @@ def maybe_return_early() -> GenerativeModelResult | None: ) if result is None: raise StochasticValidationError(f"{objective} generator search: no candidate reproduced the source process") + # last, so it aims at the verified upper bound and gets the whole remaining budget + max_dims = _HEAVIEST_TERM_MAX_DIMS if deadline is None else _GENERATOR_HEAVIEST_TERM_MAX_DIMS + if ( + generator_heaviest_term + and not wyner + and result.lower < result.upper - _CERT_TOL + and problem.n_plus + problem.n_minus <= max_dims + and not out_of_time("generator heaviest-term bound") + ): + stage_deadline = share(1.0) + gtb = generator_heaviest_term_bound( + problem, + target=result.upper - 0.5 * _CERT_TOL, + max_nodes=_GENERATOR_HEAVIEST_TERM_NODES if stage_deadline is None else 200_000, + deadline=stage_deadline, + ) + log.append( + { + "stage": "generator heaviest-term bound (certified)", + "value": gtb.value, + "nodes": gtb.nodes, + "reached": gtb.reached, + } + ) + if gtb.value > result.lower + 1e-9: + result.lower = float(min(gtb.value, result.upper)) + result.lower_source = "generator heaviest-term bound (certified)" + result.lower_certified = True result.budget_exhausted = bool(skipped) return _finalize_result(result) @@ -922,7 +939,7 @@ def generative_model_from_bidirectional( coverage_mu_starts: int = 25, coverage_k_max: int = 8, coverage_certified_only: bool = False, - generator_heaviest_term: bool = False, + generator_heaviest_term: bool = True, generator_caps: int = 2, targeted: bool = True, entropy_sweep_starts: int = 80, @@ -1048,7 +1065,7 @@ def generative_model( coverage_mu_starts: int = 25, coverage_k_max: int = 8, coverage_certified_only: bool = False, - generator_heaviest_term: bool = False, + generator_heaviest_term: bool = True, generator_caps: int = 2, targeted: bool = True, entropy_sweep_starts: int = 80, diff --git a/tests/test_exact_ci_bounds.py b/tests/test_exact_ci_bounds.py index 464de94..d62e28f 100644 --- a/tests/test_exact_ci_bounds.py +++ b/tests/test_exact_ci_bounds.py @@ -3,6 +3,7 @@ from __future__ import annotations import numpy as np +import pytest from hypothesis import given, settings from hypothesis import strategies as st @@ -113,6 +114,40 @@ def test_generator_heaviest_term_bound_is_below_a_known_generator() -> None: assert 0.85 < result.value <= 0.9697134 + 1e-9 # entropy of a word-verified generator +def test_generator_heaviest_term_bound_stops_at_its_deadline() -> None: + import time + + from sofic.generators.exact_ci_bounds import generator_heaviest_term_bound + + start = time.monotonic() + generator_heaviest_term_bound(_survey_case_a(), deadline=start + 2.0) + assert time.monotonic() - start < 10.0 + + +def _dataset_case_t006(): + """Dataset process ``n2-t006-d0`` (code ((0,1),(0,0))): C_g = 0.8551484, coverage bound 0.8379.""" + from sofic.examples.epsilon_machines import from_symbol_matrices + from sofic.generators.bidirectional_epsilon_machine import BidirectionalEpsilonMachine + from sofic.generators.coupled_generators import _GeneratorProblem + from sofic.generators.epsilon_machine import EpsilonMachine + + a, b = 0.6115387860108349, 0.2662431875066565 + mats = {0: np.array([[a, 0], [b, 0]]), 1: np.array([[0, 1 - a], [1 - b, 0]])} + machine = from_symbol_matrices((0, 1), (0, 1), mats) + forward = machine if isinstance(machine, EpsilonMachine) else EpsilonMachine.from_hmm(machine) + return _GeneratorProblem(BidirectionalEpsilonMachine.from_forward(forward)) + + +@pytest.mark.slow +def test_generator_heaviest_term_bound_closes_a_two_state_bracket() -> None: + from sofic.generators.exact_ci_bounds import generator_heaviest_term_bound + + upper = 0.8551483886521953 # entropy of a word-verified generator + result = generator_heaviest_term_bound(_dataset_case_t006(), target=upper - 5e-5) + assert result.reached + assert result.value <= upper + 1e-9 + + def _dataset_case_t055(): """Dataset process ``n3-t055-d2`` (code ((0,1),(0,2),(0,2))): C_g = G = 1.42254, minimal lifted model 1.4815+.""" from sofic.examples.epsilon_machines import from_symbol_matrices diff --git a/tests/test_generative_model.py b/tests/test_generative_model.py index a5d35de..b2b108f 100644 --- a/tests/test_generative_model.py +++ b/tests/test_generative_model.py @@ -107,6 +107,20 @@ def test_zero_time_budget_returns_a_valid_bracket_from_the_machines() -> None: assert result.stages[-1]["budget_exhausted"] is True +def test_generator_heaviest_term_stage_runs_last_on_open_small_brackets() -> None: + from tests.test_exact_ci_bounds import _dataset_case_t006 + + bidir = _dataset_case_t006().bidir + result = generative_model_from_bidirectional(bidir, time_budget=0.0, rng=np.random.default_rng(0)) + names = [s["stage"] for s in result.stages] + assert names[-2] == "generator heaviest-term bound" + assert result.stages[-2]["skipped"] == "time budget exhausted" + off = generative_model_from_bidirectional( + bidir, time_budget=0.0, generator_heaviest_term=False, rng=np.random.default_rng(0) + ) + assert "generator heaviest-term bound" not in {s["stage"] for s in off.stages} + + def test_unlimited_budget_is_not_reported_as_exhausted() -> None: result = generative_model(golden_mean(0.5), rng=np.random.default_rng(0)) assert not result.budget_exhausted