Exact Cover API Reference
Data
Data model for ExactCover use case.
ExactCoverData
Bases: UcData
Data for the Exact Cover Problem.
Given a universe of elements and a collection of subsets, the Exact Cover problem asks whether there exists a sub-collection of subsets such that every element is contained in exactly one subset.
Attributes:
-
name(Literal['exact_cover']) –Identifier for this data type.
-
subset_matrix(NumPyArray) –A matrix where each row represents a subset and each column an element. subset_matrix[i][j] = 1 if subset i contains element j, 0 otherwise.
-
n_elements(int) –Number of elements in the universe.
plot(*, ax: Axes | None = None) -> Axes
Plot the subset matrix as a binary heatmap.
Parameters:
-
ax(Axes | None, default:None) –Matplotlib axes to draw on. Creates a new figure if
None.
Returns:
-
Axes–The axes with the plot.
to_string() -> str
from_matrix(subset_matrix: list[list[int]] | NDArray[np.int_]) -> ExactCoverData
staticmethod
Create an ExactCoverData instance from a subset matrix.
Parameters:
-
subset_matrix(list[list[int]] | NDArray[int_]) –A matrix where each row is a subset and each column an element.
subset_matrix[i][j] = 1if subset i contains element j.
Returns:
-
ExactCoverData–An ExactCoverData instance with the given matrix.
Examples:
from_subsets(subsets: list[list[int | str]], elements: list[int | str] | None = None) -> ExactCoverData
staticmethod
Create an ExactCoverData instance from element and subset lists.
Parameters:
-
subsets(list[list[int | str]]) –List of subsets, where each subset is a list of elements.
-
elements(list[int | str] | None, default:None) –List of all elements in the universe. If
None, inferred aslist(range(n))where n is the largest integer element + 1.
Returns:
-
ExactCoverData–An ExactCoverData instance with the generated subset matrix.
Examples:
generate_random(n_elements: int = 5, n_subsets: int = 8, density: float = 0.4, seed: int | None = None) -> ExactCoverData
staticmethod
Generate a random exact cover instance.
Parameters:
-
n_elements(int, default:5) –Number of elements in the universe, by default 5.
-
n_subsets(int, default:8) –Number of subsets, by default 8.
-
density(float, default:0.4) –Probability that an element is included in a subset, by default 0.4.
-
seed(int | None, default:None) –Random seed for reproducibility, by default None.
Returns:
-
ExactCoverData–A randomly generated exact cover instance.
Formulation
Formulation for ExactCover use case.
ExactCoverFormulation
Bases: UcFormulation[ExactCoverData, ExactCoverSolution]
Constraint-based formulation for the Exact Cover Problem.
Mathematical Formulation
Decision Variables: x_s in {0,1} for each subset s: 1 if subset s is selected
Objective: minimize sum_s x_s (minimize number of subsets used)
Constraints: For each element e: sum_{s containing e} x_s == 1 (each element must be covered exactly once)
to_string(data: ExactCoverData) -> str
staticmethod
Return a string describing the formulation.
Parameters:
-
data(ExactCoverData) –The problem data.
Returns:
-
str–String representation of the formulation.
formulate(data: ExactCoverData) -> Model
staticmethod
Formulate the Exact Cover Problem using constraint-based approach.
Parameters:
-
data(ExactCoverData) –The Exact Cover instance data.
Returns:
-
Model–A Luna Model ready to be solved.
Raises:
-
EmptyDataError–If the subset matrix is empty or has zero size.
-
InvalidProblemStructureError–If any element is not covered by any subset (infeasible problem).
interpret(solution: Solution, data: ExactCoverData) -> ExactCoverSolution
staticmethod
Extract solution from quantum result.
Parameters:
-
solution(Solution) –The quantum solution.
-
data(ExactCoverData) –The problem data.
Returns:
-
ExactCoverSolution–Structured solution with metrics.
Solution
Solution model for ExactCover use case.
ExactCoverSolution
Bases: UcSolution
Solution for the Exact Cover Problem.
Attributes:
-
name(Literal['exact_cover']) –Identifier for this solution type.
-
selected_subsets(list[int]) –Indices of selected subsets forming the exact cover.
-
n_subsets_used(int) –Number of subsets used in the solution.
-
is_valid(bool) –Whether each element is covered exactly once.
plot(data: ExactCoverData | None = None, *, ax: Axes | None = None) -> Axes
Plot the exact cover solution.
Parameters:
-
data(ExactCoverData | None, default:None) –Problem data for context.
-
ax(Axes | None, default:None) –Matplotlib axes to draw on. Creates a new figure if
None.
Returns:
-
Axes–The axes with the plot.
to_string() -> str
Instance
Instance model for ExactCover use case.
ExactCoverInstance
Bases: UcInstance[ExactCoverData, ExactCoverFormulation, ExactCoverSolution]
Instance combining data and formulation for ExactCover.
Collection
Collection of ExactCover instances.
ExactCoverCollection
Bases: UcInstanceCollection[ExactCoverInstance]
Collection of Exact Cover instances.
This collection provides methods to generate benchmark instances with various characteristics for testing and evaluation.
from_random(min_num_elements: int, max_num_elements: int, num_instances: int = 1, *, density: float = 0.4, subset_ratio: float = 1.6, seed: int | None = None) -> ExactCoverCollection
classmethod
Generate random exact cover instances.
Parameters:
-
min_num_elements(int) –Minimum number of elements per instance.
-
max_num_elements(int) –Maximum number of elements per instance.
-
num_instances(int, default:1) –Number of instances per size, by default 1.
-
density(float, default:0.4) –Probability that an element is included in a subset, by default 0.4.
-
subset_ratio(float, default:1.6) –Ratio of subsets to elements, by default 1.6.
-
seed(int | None, default:None) –Random seed for reproducibility, by default None.
Returns:
-
ExactCoverCollection–Collection containing generated instances.