Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
run: sudo dot -c

- name: Install package and dependencies
run: uv sync --extra test --extra symbolic
run: uv sync --extra test --extra symbolic --extra global

- name: Run tests
env:
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
A second correctness review: every fix below was reproduced against a
brute-force reference or a closed-form value and has a regression test.

### Restricted global bound (SCIP)

- New `sofic.generators.global_bound`: `generator_global_bound(bidir, k)` minimizes the state entropy over the
coupled generator equations for `k` states with SCIP's spatial branch and bound (optional dependency,
`pip install sofic[global]`), with two-step path flows and the two-step edge law when the bidirectional edge
chain is known, sorted weights, optional caps and an objective limit. `lower` bounds the state entropy of
every generator with at most `k` states, and only those: it is a lower bound on `C_g` only once the number
of states of a minimal generator is bounded (open). It is numerical, not a certificate. Each entropy term is
evaluated at `max(pi_w, pi_min)`, and the overestimate (`k pi_min log2(1 / pi_min)`) is subtracted, so
states of tiny weight cannot slip under the bound.
- `hmm_generator_search(..., initial=[T])` polishes given `k`-state HMMs: their rounds keep the entropy term
off and move them to a nearby generator (from a near-generator, the entropy descent stalls at an infeasible
point). This turns a SCIP solution, which satisfies necessary conditions only, into a verified generator.
- `generative_model(..., global_bound=True, global_bound_states=k)` runs the bound after the belief-free
search (default `k = min(|S+|, |S-|) + 1`, half the remaining budget, the best candidate's entropy as
objective limit, models up to 2,000 variables), records `restricted_lower` and `restricted_states`, and
offers the polished solution as a candidate. `lower` and `certified` are unchanged. At
`(p, q) = (0.836, 0.788)` in the binary two-state family the bound at `k = 3` is 0.15563 (11 s), and the
polished solution is a generator of entropy 0.15571, 0.43 bits below `C_mu`.

### Belief-free generator search

- New `sofic.generators.hmm_search`: `hmm_generator_search(machine, k)` optimizes the symbol matrices of a
Expand Down
32 changes: 32 additions & 0 deletions docs/generators/generative_models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,36 @@ feeds them to :func:`~sofic.generators.generator_caps.generator_coverage_bound`
on :math:`C_g` from the forward ε-machine alone. On the :math:`(2, \infty)` process a
two-symbol window already gives :math:`C_g \ge 3/2`.

Restricted global bound
-----------------------

:func:`~sofic.generators.global_bound.generator_global_bound` (optional dependency PySCIPOpt,
``pip install sofic[global]``) minimizes :math:`\H{\pi}` over the coupled generator equations for
``k`` states, written in the flows :math:`F(w, x, v) = \pi_w K(x, v \mid w)` and solved by SCIP's
spatial branch and bound :cite:`Achterberg2009,Vigerske2018` through PySCIPOpt :cite:`Maher2016`.
With the bidirectional edge chain it adds two-step path flows and the two-step edge law. The
weights are sorted, so caps on the heaviest states enter as linear cuts, and an objective limit
(the entropy of a verified generator) lets SCIP prune. The result bounds the state entropy of every
generator with at most ``k`` states and says nothing about larger ones. It is therefore a lower
bound on :math:`C_g` only once the number of states of a minimal generator is bounded, which is open
(every certified case so far needs at most :math:`|S^+| + 1` states). It is also numerical, not a
certificate: the equations with data on the right-hand side are relaxed by ``data_tol``, and SCIP
works to its own tolerances. SCIP's solution satisfies necessary conditions only;
:func:`~sofic.generators.hmm_search.hmm_generator_search` with ``initial`` polishes it into a
generator. On the binary process with symbol matrices
:math:`T^{(0)} = \begin{pmatrix} p & 0 \\ q & 0 \end{pmatrix}` and
:math:`T^{(1)} = \begin{pmatrix} 0 & 1 - p \\ 1 - q & 0 \end{pmatrix}` at
:math:`(p, q) = (0.836, 0.788)`, where :math:`C_\mu = 0.5866`, the bound at ``k = 3`` is
:math:`0.15563` (11 s), and the polished solution is a generator of entropy :math:`0.15571`: no
generator with at most 3 states does :math:`10^{-4}` bits better.

The pipeline runs it with ``global_bound=True``, after the belief-free search, for
``global_bound_states`` states (default :math:`\min(|S^+|, |S^-|) + 1`). It gets half the remaining
budget (120 s without a budget) and the best candidate's entropy as its objective limit, and it is
skipped above 2,000 SCIP variables. The value goes to ``restricted_lower`` and ``restricted_states``,
not to ``lower``, so ``certified`` is unchanged; the polished solution is offered as a candidate. The
two-step flows and this restricted use are not from the literature.

API
===

Expand All @@ -397,6 +427,8 @@ Coupled generators and bounds
.. autofunction:: sofic.generators.generative_search.resolve_bidirectional
.. autofunction:: sofic.generators.hmm_search.hmm_generator_search
.. autofunction:: sofic.generators.hmm_search.coupled_seed_from_hmm
.. autofunction:: sofic.generators.global_bound.generator_global_bound
.. autoclass:: sofic.generators.global_bound.GlobalBoundResult
.. autoclass:: sofic.generators.generative_search.GenerativeModelResult
:members: certified, gap, g_optima_generate
.. autofunction:: sofic.generators.coverage_bound.coverage_entropy_lower
Expand Down
34 changes: 34 additions & 0 deletions docs/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -1865,3 +1865,37 @@ @article{Ruebeck2018
year = {2018},
doi = {10.1063/1.5003041},
}

@article{Achterberg2009,
author = {Achterberg, Tobias},
title = {{SCIP}: Solving Constraint Integer Programs},
journal = {Mathematical Programming Computation},
volume = {1},
number = {1},
pages = {1--41},
year = {2009},
doi = {10.1007/s12532-008-0001-1},
}

@article{Vigerske2018,
author = {Vigerske, Stefan and Gleixner, Ambros},
title = {{SCIP}: Global Optimization of Mixed-Integer Nonlinear Programs in a Branch-and-Cut Framework},
journal = {Optimization Methods and Software},
volume = {33},
number = {3},
pages = {563--593},
year = {2018},
doi = {10.1080/10556788.2017.1335312},
}

@inproceedings{Maher2016,
author = {Maher, Stephen and Miltenberger, Matthias and Pedroso, Jo{\~a}o Pedro and Rehfeldt, Daniel and Schwarz, Robert and Serrano, Felipe},
title = {{PySCIPOpt}: Mathematical Programming in {Python} with the {SCIP} Optimization Suite},
booktitle = {Mathematical Software -- {ICMS} 2016},
series = {Lecture Notes in Computer Science},
volume = {9725},
pages = {301--307},
publisher = {Springer},
year = {2016},
doi = {10.1007/978-3-319-42432-3_37},
}
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ docs = [
"sofic[symbolic]",
]
dev = [
"sofic[test,viz,capacity]",
"sofic[test,viz,capacity,global]",
"sofic[docs]",
"ruff",
"ty",
Expand All @@ -78,6 +78,9 @@ sat = [
capacity = [
"cvxpy>=1.4",
]
global = [
"pyscipopt>=5",
]
symbolic = [
"sympy>=1.12",
"dit[symbolic]>=2.4",
Expand Down Expand Up @@ -208,6 +211,8 @@ allowed-unresolved-imports = [
"sympy.**",
"pysat",
"pysat.**",
"pyscipopt",
"pyscipopt.**",
]

[tool.ty.rules]
Expand Down
81 changes: 80 additions & 1 deletion sofic/generators/generative_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
(: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).
6. *Restricted global bound* (opt-in, ``global_bound``; runs after the belief-free search). SCIP's lower bound
on the state entropy of generators with at most ``global_bound_states`` states
(:func:`~sofic.generators.global_bound.generator_global_bound`) is reported as ``restricted_lower``. It does
not enter ``lower``: no bound on the number of states of a minimal generator is known. SCIP's solution,
polished by the belief-free search, is offered as a candidate.

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
Expand Down Expand Up @@ -95,6 +100,12 @@
# 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
# The restricted global bound runs only on SCIP models up to this many variables (two-step flows included;
# 3-state binary processes at 4 states have about 1,200), and for this many seconds when there is no budget.
_GLOBAL_BOUND_MAX_VARS = 2_000
_GLOBAL_BOUND_SECONDS = 120.0
# SCIP solution states lighter than this are dropped before the belief-free polish.
_GLOBAL_BOUND_MIN_WEIGHT = 1e-6


def _entropy(p: np.ndarray) -> float:
Expand Down Expand Up @@ -127,6 +138,10 @@ class GenerativeModelResult:
``decomposition`` holds the generator itself as plain lists, for saving and later study: state names (as in
``model``), forward and reverse causal-state labels, symbols, weights ``pi``, beliefs ``Q`` (over
:math:`S^+`) and ``R`` (over :math:`S^-`), and the kernel ``K[w][x][w']``.

``restricted_lower`` (set by the opt-in ``global_bound`` stage) bounds the state entropy of generators with at
most ``restricted_states`` states only (:func:`~sofic.generators.global_bound.generator_global_bound`). It is
numerical, and it is not part of ``lower`` or ``certified``: a minimal generator might have more states.
"""

model: CoupledGenerativeModel
Expand All @@ -147,6 +162,8 @@ class GenerativeModelResult:
reproduction_error: float | None = None
structure: dict[str, Any] | None = None
decomposition: dict[str, Any] | None = None
restricted_lower: float | None = None
restricted_states: int | None = None

@property
def certified(self) -> bool:
Expand Down Expand Up @@ -183,6 +200,8 @@ def _reliability_summary(result: GenerativeModelResult) -> dict[str, Any]:
"budget_exhausted": result.budget_exhausted,
"reproduction_error": result.reproduction_error,
"structure": result.structure,
"restricted_lower": result.restricted_lower,
"restricted_states": result.restricted_states,
}


Expand Down Expand Up @@ -424,6 +443,8 @@ def _bracket_search(
generator_heaviest_term: bool = True,
hmm_search: bool = True,
hmm_search_starts: int = 8,
global_bound: bool = False,
global_bound_states: int | None = None,
generator_caps: int = 2,
targeted: bool = True,
entropy_sweep_starts: int = 80,
Expand Down Expand Up @@ -896,6 +917,49 @@ def maybe_return_early() -> GenerativeModelResult | None:
found += 1
log.append({"stage": "belief-free HMM search", "k": list(ks), "starts": n_starts, "generators": found})

restricted_lower: float | None = None
restricted_states: int | None = None
if global_bound and not wyner and not out_of_time("restricted global bound"):
from sofic.generators.global_bound import _model_size, generator_global_bound

k = global_bound_states if global_bound_states is not None else min(problem.n_plus, problem.n_minus) + 1
size = _model_size(problem, k)
stage_deadline = share(0.5)
limit = min((c[0] for c in candidates), default=None)
if size > _GLOBAL_BOUND_MAX_VARS:
log.append({"stage": "restricted global bound", "k": k, "skipped": f"{size} SCIP variables"})
else:
seconds = _GLOBAL_BOUND_SECONDS if stage_deadline is None else stage_deadline - time.monotonic()
try:
gb = generator_global_bound(problem, k, objective_limit=limit, time_limit=seconds)
except ImportError as exc:
log.append({"stage": "restricted global bound", "k": k, "skipped": str(exc)})
else:
restricted_lower, restricted_states = gb.lower, k
log.append(
{
"stage": "restricted global bound",
"k": k,
"value": gb.lower,
"status": gb.status,
"objective limit": limit,
"two-step": gb.two_step,
"nodes": gb.nodes,
"seconds": gb.seconds,
}
)
if gb.seed is not None:
pi, _, _, K = gb.seed
keep = np.flatnonzero(pi > _GLOBAL_BOUND_MIN_WEIGHT)
T = [K[:, x, :][np.ix_(keep, keep)] for x in range(K.shape[1])]
rows = sum(T).sum(axis=1, keepdims=True)
polished = hmm_generator_search(
reference, len(keep), starts=0, initial=[[M / rows for M in T]], deadline=deadline
)
name = f"polished global-bound solution k={len(keep)}"
for res in polished:
offer(name, coupled_seed_from_hmm(problem, res.matrices))

best_value = min((c[0] for c in candidates), default=np.inf)
if targeted and optima and best_value - lower > _TARGETED_GAP and not out_of_time("targeted line-split search"):
seeds = targeted_line_split_seeds(problem, optima, residual_atol=residual_atol, deadline=share(0.8), rng=rng)
Expand All @@ -922,6 +986,7 @@ def maybe_return_early() -> GenerativeModelResult | None:
)
if result is None:
raise StochasticValidationError(f"{objective} generator search: no candidate reproduced the source process")
result.restricted_lower, result.restricted_states = restricted_lower, restricted_states
# 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 (
Expand Down Expand Up @@ -973,6 +1038,8 @@ def generative_model_from_bidirectional(
generator_heaviest_term: bool = True,
hmm_search: bool = True,
hmm_search_starts: int = 8,
global_bound: bool = False,
global_bound_states: int | None = None,
generator_caps: int = 2,
targeted: bool = True,
entropy_sweep_starts: int = 80,
Expand All @@ -985,7 +1052,12 @@ def generative_model_from_bidirectional(
time_budget: float | None = None,
rng: np.random.Generator | None = None,
) -> GenerativeModelResult:
"""Run the generator-search pipeline on an existing bidirectional machine; see the module docstring."""
"""Run the generator-search pipeline on an existing bidirectional machine; see the module docstring.

``global_bound`` (needs PySCIPOpt, ``pip install sofic[global]``) adds the restricted global bound for
generators with at most ``global_bound_states`` states (default :math:`\\min(|S^+|, |S^-|) + 1`), reported as
``restricted_lower``; see :class:`GenerativeModelResult`.
"""
return _bracket_search(
bidir,
objective="entropy",
Expand All @@ -1005,6 +1077,8 @@ def generative_model_from_bidirectional(
generator_heaviest_term=generator_heaviest_term,
hmm_search=hmm_search,
hmm_search_starts=hmm_search_starts,
global_bound=global_bound,
global_bound_states=global_bound_states,
generator_caps=generator_caps,
targeted=targeted,
entropy_sweep_starts=entropy_sweep_starts,
Expand Down Expand Up @@ -1103,6 +1177,8 @@ def generative_model(
generator_heaviest_term: bool = True,
hmm_search: bool = True,
hmm_search_starts: int = 8,
global_bound: bool = False,
global_bound_states: int | None = None,
generator_caps: int = 2,
targeted: bool = True,
entropy_sweep_starts: int = 80,
Expand All @@ -1129,6 +1205,7 @@ def generative_model(
The check runs between stages (and between line-split seeds and coupled-search sizes), so a single
long stage can overrun it. The bidirectional build's retry ladder counts against the budget
(see :func:`~sofic.generators.bidirectional_build.build_bidirectional_with_ladder`).
``global_bound`` and ``global_bound_states`` are as in :func:`generative_model_from_bidirectional`.
"""
start = time.monotonic()
bidir, build_meta = resolve_bidirectional(
Expand All @@ -1154,6 +1231,8 @@ def generative_model(
generator_heaviest_term=generator_heaviest_term,
hmm_search=hmm_search,
hmm_search_starts=hmm_search_starts,
global_bound=global_bound,
global_bound_states=global_bound_states,
generator_caps=generator_caps,
targeted=targeted,
entropy_sweep_starts=entropy_sweep_starts,
Expand Down
Loading
Loading