omnibias.discrete.matroid
Shared discrete-problem contracts, annealed relaxations, hard decoders, and optimality-gap evidence.
import omnibias.discrete.matroidModule contract & mathematical conventions
Shared, representation-neutral matroid independence / rank kernel.
The public home of the canonical independence and rank definitions for the three
matroid families the stack shares -- **uniform**, **partition**, and **graphic** -- on
the mathematical frozenset[int] representation. Both matroid lenses build on it:
omnibias.combinatorics(polytope / LP-certification lens) delegates itsfrozensetindependence / rank here directly;omnibias.submodular(greedy / soft-oracle lens) thresholds its0/1vectors to the selected index set and delegates the independence question here.
Routing both through one kernel keeps the two graphic matroids from drifting on acyclicity and the two partition matroids from drifting on the capacity rule. The specialized surfaces (rank inequalities, linear-maximization oracle, differentiable soft basis) stay in their own packages.
Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.
MatroidCore
omnibias.discrete.matroid.MatroidCoreSource ↗class MatroidCore(Protocol)The minimal matroid contract on ground set range(ground_size).
A matroid is fully determined for the stack's purposes by its ground-set size, its
independence oracle, and its rank function on frozenset[int] subsets. Each lens
adds its specialized surface (rank inequalities / linear oracle) on top of this core.
Regression example · independent sets enumerates via protocol
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_independent_sets_enumerates_via_protocol() -> None:
class _Uniform:
ground_size = 4
def is_independent(self, subset: frozenset[int]) -> bool:
return uniform_independent(subset, 2)
def rank(self, subset: frozenset[int]) -> int:
return uniform_rank(subset, 2)
m = _Uniform()
assert isinstance(m, MatroidCore)
enumerated = set(independent_sets(m))
expected = {s for s in _all_subsets(4) if len(s) <= 2}
assert enumerated == expectedpython -m pytest packages/omnibias-discrete/tests/test_matroid_kernel.py -k test_independent_sets_enumerates_via_protocol -qFields & methods
ground_sizeproperty
ground_size(self) -> intSee the class contract above and the exact method implementation below.
Returns: int
Method implementation
def ground_size(self) -> int: ...is_independentmethod
is_independent(self, subset: frozenset[int]) -> boolSee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
Returns: bool
Method implementation
def is_independent(self, subset: frozenset[int]) -> bool: ...rankmethod
rank(self, subset: frozenset[int]) -> intSee the class contract above and the exact method implementation below.
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
Returns: int
Method implementation
def rank(self, subset: frozenset[int]) -> int: ...Read the implementation
class MatroidCore(Protocol):
r"""The minimal matroid contract on ground set ``range(ground_size)``.
A matroid is fully determined for the stack's purposes by its ground-set size, its
independence oracle, and its rank function on ``frozenset[int]`` subsets. Each lens
adds its specialized surface (rank inequalities / linear oracle) on top of this core.
"""
@property
def ground_size(self) -> int: ...
def is_independent(self, subset: frozenset[int]) -> bool: ...
def rank(self, subset: frozenset[int]) -> int: ...graphic_independent
omnibias.discrete.matroid.graphic_independentSource ↗graphic_independent(subset: frozenset[int], edges: Sequence[tuple[int, int]], n_nodes: int) -> boolWhether the edges indexed by subset form a forest (are acyclic) over n_nodes.
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
edgespositional-or-keyword | Sequence[tuple[int, int]]Required | See the operation contract and implementation below. |
n_nodespositional-or-keyword | intRequired | See the operation contract and implementation below. |
boolExecuted regression · graphic matches acyclicity spec
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_graphic_matches_acyclicity_spec() -> None:
edges = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4)]
n = 5
for s in _all_subsets(len(edges)):
ref_forest, ref_rank = _ref_graph(s, edges, n)
assert graphic_independent(s, edges, n) == ref_forest
assert graphic_rank(s, edges, n) == ref_rankpython -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_graphic_matches_acyclicity_spec" -qRead the implementation
def graphic_independent(
subset: frozenset[int], edges: Sequence[tuple[int, int]], n_nodes: int
) -> bool:
r"""Whether the edges indexed by ``subset`` form a forest (are acyclic) over ``n_nodes``."""
uf = UnionFind(n_nodes)
for e in subset:
u, v = edges[e]
if not uf.union(u, v):
return False
return Truegraphic_rank
omnibias.discrete.matroid.graphic_rankSource ↗graphic_rank(subset: frozenset[int], edges: Sequence[tuple[int, int]], n_nodes: int) -> intRank of subset in the graphic matroid (the number of tree edges it contributes).
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
edgespositional-or-keyword | Sequence[tuple[int, int]]Required | See the operation contract and implementation below. |
n_nodespositional-or-keyword | intRequired | See the operation contract and implementation below. |
intExecuted regression · graphic matches acyclicity spec
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_graphic_matches_acyclicity_spec() -> None:
edges = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4)]
n = 5
for s in _all_subsets(len(edges)):
ref_forest, ref_rank = _ref_graph(s, edges, n)
assert graphic_independent(s, edges, n) == ref_forest
assert graphic_rank(s, edges, n) == ref_rankpython -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_graphic_matches_acyclicity_spec" -qRead the implementation
def graphic_rank(subset: frozenset[int], edges: Sequence[tuple[int, int]], n_nodes: int) -> int:
r"""Rank of ``subset`` in the graphic matroid (the number of tree edges it contributes)."""
uf = UnionFind(n_nodes)
rank = 0
for e in subset:
u, v = edges[e]
if uf.union(u, v):
rank += 1
return rankindependent_sets
omnibias.discrete.matroid.independent_setsSource ↗independent_sets(matroid: MatroidCore) -> Iterator[frozenset[int]]Enumerate every independent set of *matroid* (exponential; small ground sets only).
Parameters
| Name | Type & default | Contract |
|---|---|---|
matroidpositional-or-keyword | MatroidCoreRequired | See the operation contract and implementation below. |
Iterator[frozenset[int]]Executed regression · independent sets enumerates via protocol
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_independent_sets_enumerates_via_protocol() -> None:
class _Uniform:
ground_size = 4
def is_independent(self, subset: frozenset[int]) -> bool:
return uniform_independent(subset, 2)
def rank(self, subset: frozenset[int]) -> int:
return uniform_rank(subset, 2)
m = _Uniform()
assert isinstance(m, MatroidCore)
enumerated = set(independent_sets(m))
expected = {s for s in _all_subsets(4) if len(s) <= 2}
assert enumerated == expectedpython -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_independent_sets_enumerates_via_protocol" -qRead the implementation
def independent_sets(matroid: MatroidCore) -> Iterator[frozenset[int]]:
r"""Enumerate every independent set of *matroid* (exponential; small ground sets only)."""
elements = range(matroid.ground_size)
for size in range(matroid.ground_size + 1):
for combo in combinations(elements, size):
subset = frozenset(combo)
if matroid.is_independent(subset):
yield subsetpartition_independent
omnibias.discrete.matroid.partition_independentSource ↗partition_independent(subset: frozenset[int], groups: Iterable[Iterable[int]], caps: Iterable[int]) -> boolWhether subset obeys every group capacity (|subset cap group_i| <= caps[i]).
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
groupspositional-or-keyword | Iterable[Iterable[int]]Required | See the operation contract and implementation below. |
capspositional-or-keyword | Iterable[int]Required | See the operation contract and implementation below. |
boolExecuted regression · partition matches capacity spec
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_partition_matches_capacity_spec() -> None:
groups = ((0, 1, 2), (3, 4), (5, 6))
caps = (2, 1, 2)
for s in _all_subsets(7):
ok = all(len(s & frozenset(g)) <= c for g, c in zip(groups, caps, strict=True))
rank = sum(min(len(s & frozenset(g)), c) for g, c in zip(groups, caps, strict=True))
assert partition_independent(s, groups, caps) == ok
assert partition_rank(s, groups, caps) == rankpython -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_partition_matches_capacity_spec" -qRead the implementation
def partition_independent(
subset: frozenset[int], groups: Iterable[Iterable[int]], caps: Iterable[int]
) -> bool:
r"""Whether ``subset`` obeys every group capacity (``|subset cap group_i| <= caps[i]``)."""
for group, cap in zip(groups, caps, strict=True):
if len(subset & frozenset(group)) > cap:
return False
return Truepartition_rank
omnibias.discrete.matroid.partition_rankSource ↗partition_rank(subset: frozenset[int], groups: Iterable[Iterable[int]], caps: Iterable[int]) -> intRank of subset in a partition matroid (sum_i min(|subset cap group_i|, caps[i])).
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
groupspositional-or-keyword | Iterable[Iterable[int]]Required | See the operation contract and implementation below. |
capspositional-or-keyword | Iterable[int]Required | See the operation contract and implementation below. |
intExecuted regression · partition matches capacity spec
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_partition_matches_capacity_spec() -> None:
groups = ((0, 1, 2), (3, 4), (5, 6))
caps = (2, 1, 2)
for s in _all_subsets(7):
ok = all(len(s & frozenset(g)) <= c for g, c in zip(groups, caps, strict=True))
rank = sum(min(len(s & frozenset(g)), c) for g, c in zip(groups, caps, strict=True))
assert partition_independent(s, groups, caps) == ok
assert partition_rank(s, groups, caps) == rankpython -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_partition_matches_capacity_spec" -qRead the implementation
def partition_rank(
subset: frozenset[int], groups: Iterable[Iterable[int]], caps: Iterable[int]
) -> int:
r"""Rank of ``subset`` in a partition matroid (``sum_i min(|subset cap group_i|, caps[i])``)."""
return sum(
min(len(subset & frozenset(group)), cap)
for group, cap in zip(groups, caps, strict=True)
)uniform_independent
omnibias.discrete.matroid.uniform_independentSource ↗uniform_independent(subset: frozenset[int], k: int) -> boolWhether subset is independent in the uniform matroid U(n, k) (|subset| <= k).
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
boolExecuted regression · uniform matches cardinality spec
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_uniform_matches_cardinality_spec() -> None:
n, k = 7, 3
for s in _all_subsets(n):
assert uniform_independent(s, k) == (len(s) <= k)
assert uniform_rank(s, k) == min(len(s), k)python -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_uniform_matches_cardinality_spec" -qRegression example · independent sets enumerates via protocol
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_independent_sets_enumerates_via_protocol() -> None:
class _Uniform:
ground_size = 4
def is_independent(self, subset: frozenset[int]) -> bool:
return uniform_independent(subset, 2)
def rank(self, subset: frozenset[int]) -> int:
return uniform_rank(subset, 2)
m = _Uniform()
assert isinstance(m, MatroidCore)
enumerated = set(independent_sets(m))
expected = {s for s in _all_subsets(4) if len(s) <= 2}
assert enumerated == expectedpython -m pytest packages/omnibias-discrete/tests/test_matroid_kernel.py -k test_independent_sets_enumerates_via_protocol -qRead the implementation
def uniform_independent(subset: frozenset[int], k: int) -> bool:
r"""Whether ``subset`` is independent in the uniform matroid ``U(n, k)`` (``|subset| <= k``)."""
return len(subset) <= kuniform_rank
omnibias.discrete.matroid.uniform_rankSource ↗uniform_rank(subset: frozenset[int], k: int) -> intRank of subset in U(n, k) (min(|subset|, k)).
Parameters
| Name | Type & default | Contract |
|---|---|---|
subsetpositional-or-keyword | frozenset[int]Required | See the operation contract and implementation below. |
kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
intExecuted regression · uniform matches cardinality spec
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_uniform_matches_cardinality_spec() -> None:
n, k = 7, 3
for s in _all_subsets(n):
assert uniform_independent(s, k) == (len(s) <= k)
assert uniform_rank(s, k) == min(len(s), k)python -m pytest "packages/omnibias-discrete/tests/test_matroid_kernel.py::test_uniform_matches_cardinality_spec" -qRegression example · independent sets enumerates via protocol
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_independent_sets_enumerates_via_protocol() -> None:
class _Uniform:
ground_size = 4
def is_independent(self, subset: frozenset[int]) -> bool:
return uniform_independent(subset, 2)
def rank(self, subset: frozenset[int]) -> int:
return uniform_rank(subset, 2)
m = _Uniform()
assert isinstance(m, MatroidCore)
enumerated = set(independent_sets(m))
expected = {s for s in _all_subsets(4) if len(s) <= 2}
assert enumerated == expectedpython -m pytest packages/omnibias-discrete/tests/test_matroid_kernel.py -k test_independent_sets_enumerates_via_protocol -qRead the implementation
def uniform_rank(subset: frozenset[int], k: int) -> int:
r"""Rank of ``subset`` in ``U(n, k)`` (``min(|subset|, k)``)."""
return min(len(subset), k)