Set Packing API Reference
Data
Data model for SetPacking use case.
SetPackingData
Bases: UcData
Data for the Set Packing Problem.
Given a universe of elements, a collection of subsets, and weights per subset, the Set Packing problem asks to find the maximum-weight collection of pairwise disjoint subsets.
Attributes:
-
name(Literal['set_packing']) –Identifier for this data type.
-
subset_matrix(list[list[int]]) –A matrix where each row represents a subset and each column an element.
subset_matrix[i][j] = 1if subset i contains element j, 0 otherwise. -
weights(list[float]) –Weight (value) associated with each subset.
plot(*, ax: Axes | None = None) -> Axes
Plot the subset matrix as a binary heatmap with weights.
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_values(subset_matrix: list[list[int]], weights: list[float]) -> SetPackingData
staticmethod
Create a SetPackingData instance from explicit values.
Parameters:
-
subset_matrix(list[list[int]]) –A matrix where each row represents a subset and each column an element.
subset_matrix[i][j] = 1if subset i contains element j, 0 otherwise. -
weights(list[float]) –Weight (value) associated with each subset.
Returns:
-
SetPackingData–A SetPackingData instance with the given values.
generate_random(n_elements: int = 5, n_subsets: int = 8, density: float = 0.3, seed: int | None = None) -> SetPackingData
staticmethod
Generate a random set packing 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.3) –Probability that an element is included in a subset, by default 0.3.
-
seed(int | None, default:None) –Random seed for reproducibility, by default None.
Returns:
-
SetPackingData–A randomly generated set packing instance.
Formulation
Formulation for SetPacking use case.
SetPackingFormulation
Bases: UcFormulation[SetPackingData, SetPackingSolution]
Constraint-based formulation for the Set Packing Problem.
Mathematical Formulation
to_string(data: SetPackingData) -> str
staticmethod
Return a string describing the formulation.
Parameters:
-
data(SetPackingData) –The problem data.
Returns:
-
str–String representation of the formulation.
formulate(data: SetPackingData) -> Model
staticmethod
Formulate the Set Packing Problem using constraint-based approach.
Parameters:
-
data(SetPackingData) –The Set Packing instance data.
Returns:
-
Model–A LunaModel ready to be solved.
interpret(solution: Solution, data: SetPackingData) -> SetPackingSolution
staticmethod
Extract solution from quantum result.
Parameters:
-
solution(Solution) –The quantum solution.
-
data(SetPackingData) –The problem data.
Returns:
-
SetPackingSolution–Structured solution with metrics.
Solution
Solution model for SetPacking use case.
SetPackingSolution
Bases: UcSolution
Solution for the Set Packing Problem.
Attributes:
-
name(Literal['set_packing']) –Identifier for this solution type.
-
selected_subsets(list[int]) –Indices of selected subsets.
-
total_weight(float) –Total weight of selected subsets.
-
is_valid(bool) –Whether selected subsets are pairwise disjoint.
plot(data: SetPackingData | None = None, *, ax: Axes | None = None) -> Axes
Plot the set packing solution.
Parameters:
-
data(SetPackingData | 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 SetPacking use case.
SetPackingInstance
Bases: UcInstance[SetPackingData, SetPackingFormulation, SetPackingSolution]
Instance combining data and formulation for SetPacking.
Collection
Collection of SetPacking instances.
SetPackingCollection
Bases: UcInstanceCollection[SetPackingInstance]
Collection of Set Packing instances.
This collection provides methods to generate benchmark instances with various characteristics for testing and evaluation.
from_random(min_num_elements: int | None = None, max_num_elements: int | None = None, num_instances: int = 1, *, sizes: Sequence[int] | None = None, density: float = 0.3, subset_ratio: float = 1.6, seed: int | None = None) -> SetPackingCollection
classmethod
Generate random set packing instances.
Parameters:
-
min_num_elements(int | None, default:None) –Minimum number of elements per instance.
-
max_num_elements(int | None, default:None) –Maximum number of elements per instance.
-
num_instances(int, default:1) –Number of instances per size, by default 1.
-
density(float, default:0.3) –Probability that an element is included in a subset, by default 0.3.
-
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.
-
sizes(Sequence[int] | None, default:None) –Explicit sizes to generate, e.g.
[10, 50, 100], instead of a range. Mutually exclusive withmin_num_elements/max_num_elements, by default None.
Returns:
-
SetPackingCollection–Collection containing generated instances.
filter_infeasible(max_runtime: float = 3600, *, quiet: bool = True) -> list[bool]
Drop the instances of this collection that have no feasible solution.
Every instance is formulated and handed to SCIP, which stops as soon as
it finds the first feasible solution. An instance is removed from the
collection when SCIP proves the model infeasible, when no solution turns
up within max_runtime, or when formulating it fails altogether. This
keeps randomly generated instances from breaking a downstream pipeline.
Parameters:
-
max_runtime(float, default:3600) –SCIP time limit per instance in seconds. Must be positive. Defaults to 3600 seconds.
-
quiet(bool, default:True) –Suppress the SCIP solver output.
Returns:
-
list[bool]–Feasibility mask over the instances as they were before filtering, in that order:
Truewhere the instance was kept,Falsewhere it was removed.
Raises:
-
ValueError–If
max_runtimeis not positive.